Allow SIP checkout to pre-empt unfilled holds.
[koha-equinox.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     # holds checked inside do_checkout
142     # } elsif ($item->hold_queue && @{$item->hold_queue} && ! $item->barcode_is_borrowernumber($patron_id, $item->hold_queue->[0]->{borrowernumber})) {
143         #       $circ->screen_msg("Item on Hold for Another User");
144     } elsif ($item->{patron} && ($item->{patron} ne $patron_id)) {
145         # I can't deal with this right now
146                 $circ->screen_msg("Item checked out to another patron");
147     } else {
148                 $circ->do_checkout();
149                 if ($circ->ok){
150                         $debug and warn "circ is ok";
151                         # If the item is already associated with this patron, then
152                         # we're renewing it.
153                         $circ->renew_ok($item->{patron} && ($item->{patron} eq $patron_id));
154                 
155                         $item->{patron} = $patron_id;
156                         $item->{due_date} = $circ->{due};
157                         push(@{$patron->{items}}, $item_id);
158                         $circ->desensitize(!$item->magnetic);
159
160                         syslog("LOG_DEBUG", "ILS::Checkout: patron %s has checked out %s",
161                                 $patron_id, join(', ', @{$patron->{items}}));
162                 }
163                 else {
164                         syslog("LOG_ERR", "ILS::Checkout Issue failed");
165                 }
166     }
167     # END TRANSACTION
168
169     return $circ;
170 }
171
172 sub checkin {
173     my ($self, $item_id, $trans_date, $return_date,
174         $current_loc, $item_props, $cancel) = @_;
175     my ($patron, $item, $circ);
176
177     $circ = new ILS::Transaction::Checkin;
178     # BEGIN TRANSACTION
179     $circ->item($item = new ILS::Item $item_id);
180     
181     $circ->do_checkin();    
182         # It's ok to check it in if it exists, and if it was checked out
183         $circ->ok($item && $item->{patron});
184
185     if ($circ->ok) {
186                 $circ->patron($patron = new ILS::Patron $item->{patron});
187                 delete $item->{patron};
188                 delete $item->{due_date};
189                 $patron->{items} = [ grep {$_ ne $item_id} @{$patron->{items}} ];
190     }
191     # END TRANSACTION
192
193     return $circ;
194 }
195
196 # If the ILS caches patron information, this lets it free
197 # it up
198 sub end_patron_session {
199     my ($self, $patron_id) = @_;
200
201     # success?, screen_msg, print_line
202     return (1, 'Thank you !', '');
203 }
204
205 sub pay_fee {
206     my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
207         $pay_type, $fee_id, $trans_id, $currency) = @_;
208     my $trans;
209     my $patron;
210
211 #    $trans = new ILS::Transaction::FeePayment;
212
213     $patron = new ILS::Patron $patron_id;
214
215     $trans->transaction_id($trans_id);
216     $trans->patron($patron);
217     $trans->ok(1);
218
219     return $trans;
220 }
221
222 sub add_hold {
223     my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
224         $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
225     my ($patron, $item);
226
227         my $trans = new ILS::Transaction::Hold;
228
229     $patron = new ILS::Patron $patron_id;
230     if (!$patron
231         || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
232                 $trans->screen_msg("Invalid Patron.");
233                 return $trans;
234     }
235
236         unless ($item = new ILS::Item ($item_id || $title_id)) {
237                 $trans->screen_msg("No such item.");
238                 return $trans;
239         }
240
241    if ($item->fee and $fee_ack ne 'Y') {
242                 $trans->screen_msg = "Fee required to place hold.";
243                 return $trans;
244     }
245
246     my $hold = {
247         item_id         => $item->id,
248         patron_id       => $patron->id,
249         expiration_date => $expiry_date,
250         pickup_location => $pickup_location,
251         hold_type       => $hold_type,
252     };
253
254     $trans->ok(1);
255     $trans->patron($patron);
256     $trans->item($item);
257     $trans->pickup_location($pickup_location);
258         $trans->do_hold;
259
260     push(@{$item->hold_queue},     $hold);
261     push(@{$patron->{hold_items}}, $hold);
262
263     return $trans;
264 }
265
266 sub cancel_hold {
267     my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
268     my ($patron, $item, $hold);
269
270         my $trans = new ILS::Transaction::Hold;
271
272     $patron = new ILS::Patron $patron_id;
273     if (!$patron) {
274                 $trans->screen_msg("Invalid patron barcode.");
275                 return $trans;
276     } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
277                 $trans->screen_msg('Invalid patron password.');
278                 return $trans;
279     }
280
281     unless ($item = new ILS::Item ($item_id || $title_id)) {
282                 $trans->screen_msg("No such item.");
283                 return $trans;
284     }
285
286     $trans->patron($patron);
287     $trans->item($item);
288         $trans->drop_hold;
289         unless ($trans->ok) {
290                 $trans->screen_msg("Error with transaction drop_hold: " . $trans->screen_msg);
291                 return $trans;
292         }
293     # Remove the hold from the patron's record first
294     $trans->ok($patron->drop_hold($item_id));   # different than the transaction drop!
295
296     unless ($trans->ok) {
297                 # We didn't find it on the patron record
298                 $trans->screen_msg("No such hold on patron record.");
299                 return $trans;
300     }
301
302     # Now, remove it from the item record.  If it was on the patron
303     # record but not on the item record, we'll treat that as success.
304     foreach my $i (0 .. scalar @{$item->hold_queue}) {
305                 $hold = $item->hold_queue->[$i];
306                 if ($item->barcode_is_borrowernumber($patron->id, $hold->{borrowernumber})) {
307                     # found it: delete it.
308                     splice @{$item->hold_queue}, $i, 1;
309                     last;               # ?? should we keep going, in case there are multiples
310                 }
311     }
312
313     $trans->screen_msg("Hold Cancelled.");
314
315     return $trans;
316 }
317
318
319 # The patron and item id's can't be altered, but the
320 # date, location, and type can.
321 sub alter_hold {
322     my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
323         $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
324     my ($patron, $item);
325     my $hold;
326     my $trans;
327
328     $trans = new ILS::Transaction::Hold;
329
330     # BEGIN TRANSACTION
331     $patron = new ILS::Patron $patron_id;
332     unless ($patron) {
333                 $trans->screen_msg("Invalid patron barcode: '$patron_id'.");
334                 return $trans;
335     }
336
337     foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
338                 $hold = $patron->{hold_items}[$i];
339
340         if ($hold->{item_id} eq $item_id) {
341             # Found it.  So fix it.
342             $hold->{expiration_date} = $expiry_date     if $expiry_date;
343             $hold->{pickup_location} = $pickup_location if $pickup_location;
344             $hold->{hold_type}       = $hold_type       if $hold_type;
345                 $trans->change_hold();
346             # $trans->ok(1);
347             $trans->screen_msg("Hold updated.");
348             $trans->patron($patron);
349             $trans->item(new ILS::Item $hold->{item_id});
350             last;
351         }
352     }
353
354     # The same hold structure is linked into both the patron's
355     # list of hold items and into the queue of outstanding holds
356     # for the item, so we don't need to search the hold queue for
357     # the item, since it's already been updated by the patron code.
358
359     if (!$trans->ok) {
360                 $trans->screen_msg("No such outstanding hold.");
361     }
362
363     return $trans;
364 }
365
366 sub renew {
367     my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
368         $no_block, $nb_due_date, $third_party,
369         $item_props, $fee_ack) = @_;
370     my ($patron, $item);
371     my $trans;
372
373     $trans = new ILS::Transaction::Renew;
374     $trans->patron($patron = new ILS::Patron $patron_id);
375
376     if (!$patron) {
377                 $trans->screen_msg("Invalid patron barcode.");
378                 return $trans;
379     } elsif (!$patron->renew_ok) {
380                 $trans->screen_msg("Renewals not allowed.");
381                 return $trans;
382     }
383
384         # Previously: renewing a title, rather than an item (sort of)
385         # This is gross, but in a real ILS it would be better
386
387     # if (defined($title_id)) {
388         #       foreach my $i (@{$patron->{items}}) {
389         #               $item = new ILS::Item $i;
390         #               last if ($title_id eq $item->title_id);
391         #               $item = undef;
392         #       }
393     # } else {
394                 my $j = 0;
395                 my $count = scalar @{$patron->{items}};
396                 foreach my $i (@{$patron->{items}}) {
397                         syslog("LOG_DEBUG", "checking item %s of %s: $item_id vs. %s", ++$j, $count, $i);
398                         if ($i eq $item_id) {
399                                 # We have it checked out
400                                 $item = new ILS::Item $item_id;
401                                 last;
402                         }
403                 }
404     # }
405
406     $trans->item($item);
407
408     if (!defined($item)) {
409         # It's not checked out to $patron_id
410                 $trans->screen_msg("Item not checked out to " . $patron->name);
411     } elsif (!$item->available($patron_id)) {
412                 $trans->screen_msg("Item unavailable due to outstanding holds");
413     } else {
414                 $trans->renewal_ok(1);
415                 $trans->desensitize(0); # It's already checked out
416                 $trans->do_renew();
417                 syslog("LOG_DEBUG", "done renew (%s): %s renews %s", $trans->renewal_ok(1),$patron_id,$item_id);
418
419 #               if ($no_block eq 'Y') {
420 #                       $item->{due_date} = $nb_due_date;
421 #               } else {
422 #                       $item->{due_date} = time + (14*24*60*60); # two weeks
423 #               }
424 #               if ($item_props) {
425 #                       $item->{sip_item_properties} = $item_props;
426 #               }
427 #               $trans->ok(1);
428 #               return $trans;
429     }
430
431     return $trans;
432 }
433
434 sub renew_all {
435     my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
436     my ($patron, $item_id);
437     my $trans;
438
439     $trans = new ILS::Transaction::RenewAll;
440
441     $trans->patron($patron = new ILS::Patron $patron_id);
442     if (defined $patron) {
443         syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s",
444                $patron->name, $patron->renew_ok);
445     } else {
446         syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'",
447                $patron_id);
448     }
449
450     if (!defined($patron)) {
451                 $trans->screen_msg("Invalid patron barcode.");
452                 return $trans;
453     } elsif (!$patron->renew_ok) {
454                 $trans->screen_msg("Renewals not allowed.");
455                 return $trans;
456     } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
457                 $trans->screen_msg("Invalid patron password.");
458                 return $trans;
459     }
460
461         $trans->do_renew_all;
462     $trans->ok(1);
463     return $trans;
464 }
465
466 1;
467 __END__
468