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