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