]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/SIP/Item.pm
updated some logging, added script support to SIP for determining magnetic, etc....
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / SIP / Item.pm
1 #
2 #
3 # A Class for hiding the ILS's concept of the item from the OpenSIP
4 # system
5 #
6
7 package OpenILS::SIP::Item;
8 use strict; use warnings;
9
10 use Sys::Syslog qw(syslog);
11
12 use OpenILS::SIP;
13 use OpenILS::SIP::Transaction;
14 use OpenILS::Application::AppUtils;
15 use OpenILS::Application::Circ::ScriptBuilder;
16 use Data::Dumper;
17 my $U = 'OpenILS::Application::AppUtils';
18
19 my %item_db;
20
21 sub new {
22     my ($class, $item_id) = @_;
23     my $type = ref($class) || $class;
24     my $self = bless( {}, $type );
25
26         syslog('LOG_DEBUG', "OILS: Loading item $item_id...");
27         return undef unless $item_id;
28
29         my $e = OpenILS::SIP->editor();
30
31         my $copy = $e->search_asset_copy(
32                 [
33                         { barcode => $item_id },
34                         {
35                                 flesh => 3,
36                                 flesh_fields => {
37                                         acp => [ 'circ_lib', 'call_number', 'status' ],
38                                         acn => [ 'owning_lib', 'record' ],
39                                 }
40                         }
41                 ]
42         );
43
44
45         $copy = $$copy[0];
46
47         if(!$copy) {
48                 syslog("LOG_DEBUG", "OILS: Item '%s' : not found", $item_id);
49                 return undef;
50         }
51
52         my ($circ) = $U->fetch_open_circulation($copy->id);
53         if($circ) {
54                 # if i am checked out, set $self->{patron} to the user's barcode
55                 my $user = $e->retrieve_actor_user(
56                         [
57                                 $circ->usr,
58                                 { flesh => 1, flesh_fields => { "au" => [ 'card' ] } }
59                         ]
60                 );
61
62                 my $bc = ($user) ? $user->card->barcode : "";
63                 $self->{patron} = $bc;
64                 $self->{patron_object} = $user;
65
66                 syslog('LOG_DEBUG', "OILS: Open circulation exists on $item_id : user = $bc");
67         }
68
69         $self->{id}                     = $item_id;
70         $self->{copy}           = $copy;
71         $self->{volume} = $copy->call_number;
72         $self->{record} = $copy->call_number->record;
73         $self->{mods}           = $U->record_to_mvr($self->{record}) if $self->{record}->marc;
74
75         syslog("LOG_DEBUG", "OILS: Item('$item_id'): found with title '%s'", $self->title_id);
76
77         return $self;
78 }
79
80 sub run_attr_script {
81         my $self = shift;
82         return 1 if $self->{ran_script};
83         $self->{ran_script} = 1;
84
85         my $config = OpenILS::SIP->config();
86         my $path = $config->{implementation_config}->{scripts}->{path};
87         my $item_config_script = $config->{implementation_config}->{scripts}->{item_config};
88
89         syslog('LOG_DEBUG', "OILS: Script path = $path, Item config script = $item_config_script");
90
91         my $runner = 
92                 OpenILS::Application::Circ::ScriptBuilder->build(
93                         {
94                                 copy => $self->{copy},
95                                 editor => OpenILS::SIP->editor(),
96                         }
97                 );
98
99         $runner->add_path($path);
100         $runner->load($item_config_script);
101
102         unless( $self->{item_config_result} = $runner->run ) {
103                 warn "Item config script [$path : $item_config_script] failed to run: $@\n";
104                 syslog('LOG_ERR', "OILS: Item config script [$path : $item_config_script] failed to run: $@");
105                 return undef;
106         }
107
108         return 1;
109 }
110
111 sub magnetic {
112     my $self = shift;
113          return 0 unless $self->run_attr_script;
114          syslog('LOG_DEBUG', "OILS: ITEM CONFIG => ". Dumper($self->{item_config_result}));
115          my $mag = $self->{item_config_result}->{magneticMedia};
116          syslog('LOG_DEBUG', "OILS: magnetic = $mag");
117          return ($mag and $mag eq 't') ? 1 : 0;
118 }
119
120 sub sip_media_type {
121     my $self = shift;
122          return '001';
123 }
124
125 sub sip_item_properties {
126     my $self = shift;
127          return "";
128 }
129
130 sub status_update {
131     my ($self, $props) = @_;
132     my $status = OpenILS::SIP::Transaction->new;
133     $self->{sip_item_properties} = $props;
134     $status->{ok} = 1;
135     return $status;
136 }
137
138
139 sub id {
140     my $self = shift;
141     return $self->{id};
142 }
143
144 sub title_id {
145     my $self = shift;
146     return ($self->{mods}) ? $self->{mods}->title : $self->{copy}->dummy_title;
147 }
148
149 sub permanent_location {
150     my $self = shift;
151          return $self->{volume}->owning_lib->name;
152 }
153
154 sub current_location {
155     my $self = shift;
156          return $self->{copy}->circ_lib->name;
157 }
158
159
160 # 2 chars 0-99 
161 # 01 Other
162 # 02 On order
163 # 03 Available
164 # 04 Charged
165 # 05 Charged; not to be recalled until earliest recall date
166 # 06 In process
167 # 07 Recalled
168 # 08 Waiting on hold shelf
169 # 09 Waiting to be re-shelved
170 # 10 In transit between library locations
171 # 11 Claimed returned
172 # 12 Lost
173 # 13 Missing 
174 sub sip_circulation_status {
175         my $self = shift;
176         return '03' if $self->{copy}->status->name =~ /available/i;
177         return '04' if $self->{copy}->status->name =~ /checked out/i;
178         return '06' if $self->{copy}->status->name =~ /in process/i;
179         return '08' if $self->{copy}->status->name =~ /on holds shelf/i;
180         return '09' if $self->{copy}->status->name =~ /reshelving/i;
181         return '10' if $self->{copy}->status->name =~ /in transit/i;
182         return '12' if $self->{copy}->status->name =~ /lost/i;
183         return 01;
184 }
185
186 sub sip_security_marker {
187     return '02';
188 }
189
190 sub sip_fee_type {
191     return '01';
192 }
193
194 sub fee {
195     my $self = shift;
196          return 0;
197 }
198
199
200 sub fee_currency {
201     my $self = shift;
202     'USD';
203 }
204
205 sub owner {
206     my $self = shift;
207          return $self->{volume}->owning_lib->name;
208 }
209
210 sub hold_queue {
211     my $self = shift;
212          return [];
213 }
214
215 sub hold_queue_position {
216     my ($self, $patron_id) = @_;
217          return 1;
218 }
219
220 sub due_date {
221         my $self = shift;
222         my $e = OpenILS::SIP->editor();
223
224         my $circ = $e->search_action_circulation(
225                 { target_copy => $self->{copy}->id, stop_fines => undef } )->[0];
226
227         if(!$circ) {
228                 # if not, lets look for other circs we can check in
229                 $circ = $e->search_action_circulation(
230                         { 
231                                 target_copy => $self->{copy}->id, 
232                                 xact_finish => undef,
233                                 stop_fines      => [ 'CLAIMSRETURNED', 'LOST', 'LONGOVERDUE' ]
234                         } )->[0];
235         }
236
237         return $circ->due_date if $circ;
238         return 0;
239 }
240
241 sub recall_date {
242     my $self = shift;
243     return 0;
244 }
245
246 sub hold_pickup_date {
247     my $self = shift;
248          return 0;
249 }
250
251 # message to display on console
252 sub screen_msg {
253     my $self = shift;
254     return $self->{screen_msg} || '';
255 }
256
257
258 # reciept printer
259 sub print_line {
260      my $self = shift;
261      return $self->{print_line} || '';
262 }
263
264
265 # An item is available for a patron if
266 # 1) It's not checked out and (there's no hold queue OR patron
267 #    is at the front of the queue)
268 # OR
269 # 2) It's checked out to the patron and there's no hold queue
270 sub available {
271      my ($self, $for_patron) = @_;
272           return 1;
273 }
274
275
276 1;