SIP Holds processing on checkout
[koha.git] / C4 / SIP / ILS.pm
1 #
2 # ILS.pm: Koha ILS interface module
3 #
4
5 package ILS;
6
7 use warnings;
8 use strict;
9 use Sys::Syslog qw(syslog);
10
11 use ILS::Item;
12 use ILS::Patron;
13 use ILS::Transaction;
14 use ILS::Transaction::Checkout;
15 use ILS::Transaction::Checkin;
16 use ILS::Transaction::FeePayment;
17 use ILS::Transaction::Hold;
18 use ILS::Transaction::Renew;
19 use ILS::Transaction::RenewAll;
20
21 my $debug = 0;
22
23 my %supports = (
24                 'magnetic media'        => 1,
25                 'security inhibit'      => 0,
26                 'offline operation'     => 0,
27                 "patron status request" => 1,
28                 "checkout"              => 1,
29                 "checkin"               => 1,
30                 "block patron"          => 1,
31                 "acs status"            => 1,
32                 "login"                 => 1,
33                 "patron information"    => 1,
34                 "end patron session"    => 1,
35                 "fee paid"              => 0,
36                 "item information"      => 1,
37                 "item status update"    => 0,
38                 "patron enable"         => 1,
39                 "hold"                  => 1,
40                 "renew"                 => 1,
41                 "renew all"             => 1,
42                );
43
44 sub new {
45     my ($class, $institution) = @_;
46     my $type = ref($class) || $class;
47     my $self = {};
48         use Data::Dumper;
49         $debug and warn "new ILS: INSTITUTION: " . Dumper($institution);
50     syslog("LOG_DEBUG", "new ILS '%s'", $institution->{id});
51     $self->{institution} = $institution;
52     return bless $self, $type;
53 }
54
55 sub find_patron {
56     my $self = shift;
57         $debug and warn "ILS: finding patron";
58     return ILS::Patron->new(@_);
59 }
60
61 sub find_item {
62     my $self = shift;
63         $debug and warn "ILS: finding item";
64     return ILS::Item->new(@_);
65 }
66
67 sub institution {
68     my $self = shift;
69     return $self->{institution}->{id};
70 }
71
72 sub supports {
73     my ($self, $op) = @_;
74     return (exists($supports{$op}) && $supports{$op});
75 }
76
77 sub check_inst_id {
78     my ($self, $id, $whence) = @_;
79     if ($id ne $self->{institution}->{id}) {
80         syslog("LOG_WARNING", "%s: received institution '%s', expected '%s'",
81                $whence, $id, $self->{institution}->{id});
82     }
83 }
84
85 sub to_bool {
86     my $bool = shift;
87     # If it's defined, and matches a true sort of string, or is
88     # a non-zero number, then it's true.
89         #
90         # FIXME: this test is faulty
91         #       We're running under warnings and strict.
92         #       But if we don't match regexp, then we go ahead and numerical compare?
93         #       That means we'd generate a warning on 'FALSE' or ''.
94     return defined($bool) && (($bool =~ /true|y|yes/i) || $bool != 0);
95 }
96
97 sub checkout_ok {
98     my $self = shift;
99     return (exists($self->{policy}->{checkout})
100             && to_bool($self->{policy}->{checkout}));
101 }
102 sub checkin_ok {
103     my $self = shift;
104     return (exists($self->{policy}->{checkin})
105             && to_bool($self->{policy}->{checkin}));
106 }
107 sub status_update_ok {
108     my $self = shift;
109     return (exists($self->{policy}->{status_update})
110             && to_bool($self->{policy}->{status_update}));
111 }
112 sub offline_ok {
113     my $self = shift;
114     return (exists($self->{policy}->{offline})
115             && to_bool($self->{policy}->{offline}));
116 }
117
118 #
119 # Checkout(patron_id, item_id, sc_renew):
120 #    patron_id & item_id are the identifiers send by the terminal
121 #    sc_renew is the renewal policy configured on the terminal
122 # returns a status opject that can be queried for the various bits
123 # of information that the protocol (SIP or NCIP) needs to generate
124 # the response.
125 #
126 sub checkout {
127     my ($self, $patron_id, $item_id, $sc_renew) = @_;
128     my ($patron, $item, $circ);
129
130     $circ = new ILS::Transaction::Checkout;
131     # BEGIN TRANSACTION
132     $circ->patron($patron = new ILS::Patron $patron_id);
133     $circ->item($item = new ILS::Item $item_id);
134
135     if (!$patron) {
136                 $circ->screen_msg("Invalid Patron");
137     } elsif (!$patron->charge_ok) {
138                 $circ->screen_msg("Patron Blocked");
139     } elsif (!$item) {
140                 $circ->screen_msg("Invalid Item");
141     } elsif ($item->hold_queue && @{$item->hold_queue} && ! $item->barcode_is_borrowernumber($patron_id, $item->hold_queue->[0]->{borrowernumber})) {
142                 $circ->screen_msg("Item on Hold for Another User");
143     } elsif ($item->{patron} && ($item->{patron} ne $patron_id)) {
144         # I can't deal with this right now
145                 $circ->screen_msg("Item checked out to another patron");
146     } else {
147                 $circ->do_checkout();
148                 if ($circ->ok){
149                         $debug and warn "circ is ok";
150                         # If the item is already associated with this patron, then
151                         # we're renewing it.
152                         $circ->renew_ok($item->{patron} && ($item->{patron} eq $patron_id));
153                 
154                         $item->{patron} = $patron_id;
155                         $item->{due_date} = $circ->{due};
156                         push(@{$patron->{items}}, $item_id);
157                         $circ->desensitize(!$item->magnetic);
158
159                         syslog("LOG_DEBUG", "ILS::Checkout: patron %s has checked out %s",
160                                 $patron_id, join(', ', @{$patron->{items}}));
161                 }
162                 else {
163                         syslog("LOG_ERR", "ILS::Checkout Issue failed");
164                 }
165     }
166     # END TRANSACTION
167
168     return $circ;
169 }
170
171 sub checkin {
172     my ($self, $item_id, $trans_date, $return_date,
173         $current_loc, $item_props, $cancel) = @_;
174     my ($patron, $item, $circ);
175
176     $circ = new ILS::Transaction::Checkin;
177     # BEGIN TRANSACTION
178     $circ->item($item = new ILS::Item $item_id);
179     
180     $circ->do_checkin();    
181         # It's ok to check it in if it exists, and if it was checked out
182         $circ->ok($item && $item->{patron});
183
184     if ($circ->ok) {
185                 $circ->patron($patron = new ILS::Patron $item->{patron});
186                 delete $item->{patron};
187                 delete $item->{due_date};
188                 $patron->{items} = [ grep {$_ ne $item_id} @{$patron->{items}} ];
189     }
190     # END TRANSACTION
191
192     return $circ;
193 }
194
195 # If the ILS caches patron information, this lets it free
196 # it up
197 sub end_patron_session {
198     my ($self, $patron_id) = @_;
199
200     # success?, screen_msg, print_line
201     return (1, 'Thank you !', '');
202 }
203
204 sub pay_fee {
205     my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
206         $pay_type, $fee_id, $trans_id, $currency) = @_;
207     my $trans;
208     my $patron;
209
210 #    $trans = new ILS::Transaction::FeePayment;
211
212     $patron = new ILS::Patron $patron_id;
213
214     $trans->transaction_id($trans_id);
215     $trans->patron($patron);
216     $trans->ok(1);
217
218     return $trans;
219 }
220
221 sub add_hold {
222     my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
223         $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
224     my ($patron, $item);
225
226         my $trans = new ILS::Transaction::Hold;
227
228     $patron = new ILS::Patron $patron_id;
229     if (!$patron
230         || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
231                 $trans->screen_msg("Invalid Patron.");
232                 return $trans;
233     }
234
235         unless ($item = new ILS::Item ($item_id || $title_id)) {
236                 $trans->screen_msg("No such item.");
237                 return $trans;
238         }
239
240    if ($item->fee and $fee_ack ne 'Y') {
241                 $trans->screen_msg = "Fee required to place hold.";
242                 return $trans;
243     }
244
245     my $hold = {
246         item_id         => $item->id,
247         patron_id       => $patron->id,
248         expiration_date => $expiry_date,
249         pickup_location => $pickup_location,
250         hold_type       => $hold_type,
251     };
252
253     $trans->ok(1);
254     $trans->patron($patron);
255     $trans->item($item);
256     $trans->pickup_location($pickup_location);
257         $trans->do_hold;
258
259     push(@{$item->hold_queue},     $hold);
260     push(@{$patron->{hold_items}}, $hold);
261
262     return $trans;
263 }
264
265 sub cancel_hold {
266     my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
267     my ($patron, $item, $hold);
268
269         my $trans = new ILS::Transaction::Hold;
270
271     $patron = new ILS::Patron $patron_id;
272     if (!$patron) {
273                 $trans->screen_msg("Invalid patron barcode.");
274                 return $trans;
275     } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
276                 $trans->screen_msg('Invalid patron password.');
277                 return $trans;
278     }
279
280     unless ($item = new ILS::Item ($item_id || $title_id)) {
281                 $trans->screen_msg("No such item.");
282                 return $trans;
283     }
284
285     $trans->patron($patron);
286     $trans->item($item);
287         $trans->drop_hold;
288         unless ($trans->ok) {
289                 $trans->screen_msg("Error with transaction drop_hold: " . $trans->screen_msg);
290                 return $trans;
291         }
292     # Remove the hold from the patron's record first
293     $trans->ok($patron->drop_hold($item_id));   # different than the transaction drop!
294
295     unless ($trans->ok) {
296                 # We didn't find it on the patron record
297                 $trans->screen_msg("No such hold on patron record.");
298                 return $trans;
299     }
300
301     # Now, remove it from the item record.  If it was on the patron
302     # record but not on the item record, we'll treat that as success.
303     foreach my $i (0 .. scalar @{$item->hold_queue}) {
304                 $hold = $item->hold_queue->[$i];
305                 if ($item->barcode_is_borrowernumber($patron->id, $hold->{borrowernumber})) {
306                     # found it: delete it.
307                     splice @{$item->hold_queue}, $i, 1;
308                     last;               # ?? should we keep going, in case there are multiples
309                 }
310     }
311
312     $trans->screen_msg("Hold Cancelled.");
313
314     return $trans;
315 }
316
317
318 # The patron and item id's can't be altered, but the
319 # date, location, and type can.
320 sub alter_hold {
321     my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
322         $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
323     my ($patron, $item);
324     my $hold;
325     my $trans;
326
327     $trans = new ILS::Transaction::Hold;
328
329     # BEGIN TRANSACTION
330     $patron = new ILS::Patron $patron_id;
331     unless ($patron) {
332                 $trans->screen_msg("Invalid patron barcode: '$patron_id'.");
333                 return $trans;
334     }
335
336     foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
337                 $hold = $patron->{hold_items}[$i];
338
339         if ($hold->{item_id} eq $item_id) {
340             # Found it.  So fix it.
341             $hold->{expiration_date} = $expiry_date     if $expiry_date;
342             $hold->{pickup_location} = $pickup_location if $pickup_location;
343             $hold->{hold_type}       = $hold_type       if $hold_type;
344                 $trans->change_hold();
345             # $trans->ok(1);
346             $trans->screen_msg("Hold updated.");
347             $trans->patron($patron);
348             $trans->item(new ILS::Item $hold->{item_id});
349             last;
350         }
351     }
352
353     # The same hold structure is linked into both the patron's
354     # list of hold items and into the queue of outstanding holds
355     # for the item, so we don't need to search the hold queue for
356     # the item, since it's already been updated by the patron code.
357
358     if (!$trans->ok) {
359                 $trans->screen_msg("No such outstanding hold.");
360     }
361
362     return $trans;
363 }
364
365 sub renew {
366     my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
367         $no_block, $nb_due_date, $third_party,
368         $item_props, $fee_ack) = @_;
369     my ($patron, $item);
370     my $trans;
371
372     $trans = new ILS::Transaction::Renew;
373     $trans->patron($patron = new ILS::Patron $patron_id);
374
375     if (!$patron) {
376                 $trans->screen_msg("Invalid patron barcode.");
377                 return $trans;
378     } elsif (!$patron->renew_ok) {
379                 $trans->screen_msg("Renewals not allowed.");
380                 return $trans;
381     }
382
383         # Previously: renewing a title, rather than an item (sort of)
384         # This is gross, but in a real ILS it would be better
385
386     # if (defined($title_id)) {
387         #       foreach my $i (@{$patron->{items}}) {
388         #               $item = new ILS::Item $i;
389         #               last if ($title_id eq $item->title_id);
390         #               $item = undef;
391         #       }
392     # } else {
393                 my $j = 0;
394                 my $count = scalar @{$patron->{items}};
395                 foreach my $i (@{$patron->{items}}) {
396                         syslog("LOG_DEBUG", "checking item %s of %s: $item_id vs. %s", ++$j, $count, $i);
397                         if ($i eq $item_id) {
398                                 # We have it checked out
399                                 $item = new ILS::Item $item_id;
400                                 last;
401                         }
402                 }
403     # }
404
405     $trans->item($item);
406
407     if (!defined($item)) {
408         # It's not checked out to $patron_id
409                 $trans->screen_msg("Item not checked out to " . $patron->name);
410     } elsif (!$item->available($patron_id)) {
411                 $trans->screen_msg("Item unavailable due to outstanding holds");
412     } else {
413                 $trans->renewal_ok(1);
414                 $trans->desensitize(0); # It's already checked out
415                 $trans->do_renew();
416                 syslog("LOG_DEBUG", "done renew (%s): %s renews %s", $trans->renewal_ok(1),$patron_id,$item_id);
417
418 #               if ($no_block eq 'Y') {
419 #                       $item->{due_date} = $nb_due_date;
420 #               } else {
421 #                       $item->{due_date} = time + (14*24*60*60); # two weeks
422 #               }
423 #               if ($item_props) {
424 #                       $item->{sip_item_properties} = $item_props;
425 #               }
426 #               $trans->ok(1);
427 #               return $trans;
428     }
429
430     return $trans;
431 }
432
433 sub renew_all {
434     my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
435     my ($patron, $item_id);
436     my $trans;
437
438     $trans = new ILS::Transaction::RenewAll;
439
440     $trans->patron($patron = new ILS::Patron $patron_id);
441     if (defined $patron) {
442         syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s",
443                $patron->name, $patron->renew_ok);
444     } else {
445         syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'",
446                $patron_id);
447     }
448
449     if (!defined($patron)) {
450                 $trans->screen_msg("Invalid patron barcode.");
451                 return $trans;
452     } elsif (!$patron->renew_ok) {
453                 $trans->screen_msg("Renewals not allowed.");
454                 return $trans;
455     } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
456                 $trans->screen_msg("Invalid patron password.");
457                 return $trans;
458     }
459
460         $trans->do_renew_all;
461     $trans->ok(1);
462     return $trans;
463 }
464
465 1;
466 __END__
467