]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/SIP/Item.pm
add passive event validator to stop events that are too far past the delay_field...
[working/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         $path = ref($path) eq 'ARRAY' ? $path : [$path];
87
88         syslog('LOG_DEBUG', "OILS: Script path = $path, Item config script = $item_config_script");
89
90         my $runner = 
91                 OpenILS::Application::Circ::ScriptBuilder->build(
92                         {
93                                 copy => $self->{copy},
94                                 editor => OpenILS::SIP->editor(),
95                         }
96                 );
97
98         $runner->add_path($_) for @$path;
99         $runner->load($item_config_script);
100
101         unless( $self->{item_config_result} = $runner->run ) {
102                 $runner->cleanup;
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         $runner->cleanup;
109         return 1;
110 }
111
112 sub magnetic {
113     my $self = shift;
114          return 0 unless $self->run_attr_script;
115          my $mag = $self->{item_config_result}->{item_config}->{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 0 unless $self->run_attr_script;
123          my $media = $self->{item_config_result}->{item_config}->{SIPMediaType};
124          syslog('LOG_DEBUG', "OILS: media type = $media");
125          return ($media) ? $media : '001';
126 }
127
128 sub sip_item_properties {
129     my $self = shift;
130          return "";
131 }
132
133 sub status_update {
134     my ($self, $props) = @_;
135     my $status = OpenILS::SIP::Transaction->new;
136     $self->{sip_item_properties} = $props;
137     $status->{ok} = 1;
138     return $status;
139 }
140
141
142 sub id {
143     my $self = shift;
144     return $self->{id};
145 }
146
147 sub title_id {
148     my $self = shift;
149     my $t =  ($self->{mods}) ? $self->{mods}->title : $self->{copy}->dummy_title;
150     $t =~ s/\pM+//og;
151     return $t;
152 }
153
154 sub permanent_location {
155     my $self = shift;
156          return $self->{volume}->owning_lib->name;
157 }
158
159 sub current_location {
160     my $self = shift;
161          return $self->{copy}->circ_lib->name;
162 }
163
164
165 # 2 chars 0-99 
166 # 01 Other
167 # 02 On order
168 # 03 Available
169 # 04 Charged
170 # 05 Charged; not to be recalled until earliest recall date
171 # 06 In process
172 # 07 Recalled
173 # 08 Waiting on hold shelf
174 # 09 Waiting to be re-shelved
175 # 10 In transit between library locations
176 # 11 Claimed returned
177 # 12 Lost
178 # 13 Missing 
179 sub sip_circulation_status {
180         my $self = shift;
181         my $stat = $self->{copy}->status->id;
182
183         return '02' if $stat == OILS_COPY_STATUS_ON_ORDER;
184         return '03' if $stat == OILS_COPY_STATUS_AVAILABLE;
185         return '04' if $stat == OILS_COPY_STATUS_CHECKED_OUT;
186         return '06' if $stat == OILS_COPY_STATUS_IN_PROCESS;
187         return '08' if $stat == OILS_COPY_STATUS_ON_HOLDS_SHELF;
188         return '09' if $stat == OILS_COPY_STATUS_RESHELVING;
189         return '10' if $stat == OILS_COPY_STATUS_IN_TRANSIT;
190         return '12' if $stat == OILS_COPY_STATUS_LOST;
191         return '13' if $stat == OILS_COPY_STATUS_MISSING;
192                 
193         return 01;
194 }
195
196 sub sip_security_marker {
197     return '02';
198 }
199
200 sub sip_fee_type {
201     return '01';
202 }
203
204 sub fee {
205     my $self = shift;
206          return 0;
207 }
208
209
210 sub fee_currency {
211         my $self = shift;
212         return OpenILS::SIP->config()->{implementation_config}->{currency};
213 }
214
215 sub owner {
216     my $self = shift;
217          return $self->{volume}->owning_lib->name;
218 }
219
220 sub hold_queue {
221     my $self = shift;
222          return [];
223 }
224
225 sub hold_queue_position {
226     my ($self, $patron_id) = @_;
227          return 1;
228 }
229
230 sub due_date {
231         my $self = shift;
232
233         # this should force correct circ fetching
234         require OpenILS::Utils::CStoreEditor;
235         my $e = OpenILS::Utils::CStoreEditor->new(xact => 1);
236         #my $e = OpenILS::SIP->editor();
237
238         my $circ = $e->search_action_circulation(
239                 { target_copy => $self->{copy}->id, checkin_time => undef } )->[0];
240
241         $e->rollback;
242
243         if( !$circ ) {
244                 syslog('LOG_INFO', "OILS: No open circ found for copy");
245                 return 0;
246         }
247
248         my $due = OpenILS::SIP->format_date($circ->due_date, 'due');
249         syslog('LOG_DEBUG', "OILS: Found item due date = $due");
250         return $due;
251 }
252
253 sub recall_date {
254     my $self = shift;
255     return 0;
256 }
257
258 sub hold_pickup_date {
259     my $self = shift;
260          return 0;
261 }
262
263 # message to display on console
264 sub screen_msg {
265     my $self = shift;
266     return $self->{screen_msg} || '';
267 }
268
269
270 # reciept printer
271 sub print_line {
272      my $self = shift;
273      return $self->{print_line} || '';
274 }
275
276
277 # An item is available for a patron if
278 # 1) It's not checked out and (there's no hold queue OR patron
279 #    is at the front of the queue)
280 # OR
281 # 2) It's checked out to the patron and there's no hold queue
282 sub available {
283         my ($self, $for_patron) = @_;
284
285         my $stat = $self->{copy}->status->id;
286         return 1 if 
287                 $stat == OILS_COPY_STATUS_AVAILABLE or
288                 $stat == OILS_COPY_STATUS_RESHELVING;
289         
290         return 0;
291 }
292
293
294 1;