Bug 32431: Display expiry date on SIP checkout when patron is expired
[koha.git] / C4 / SIP / ILS.pm
1 #
2 # ILS.pm: Koha ILS interface module
3 #
4
5 package C4::SIP::ILS;
6
7 use warnings;
8 use strict;
9 use C4::SIP::Sip qw( siplog );
10 use Koha::DateUtils qw( dt_from_string output_pref );
11 use Data::Dumper;
12
13 use C4::SIP::ILS::Item;
14 use C4::SIP::ILS::Patron;
15 use C4::SIP::ILS::Transaction;
16 use C4::SIP::ILS::Transaction::Checkout;
17 use C4::SIP::ILS::Transaction::Checkin;
18 use C4::SIP::ILS::Transaction::FeePayment;
19 use C4::SIP::ILS::Transaction::Hold;
20 use C4::SIP::ILS::Transaction::Renew;
21 use C4::SIP::ILS::Transaction::RenewAll;
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"              => 1,
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     siplog("LOG_DEBUG", "new ILS '%s'", $institution->{id});
49     $self->{institution} = $institution;
50     return bless $self, $type;
51 }
52
53 sub find_patron {
54     my $self = shift;
55     return C4::SIP::ILS::Patron->new(@_);
56 }
57
58 sub find_item {
59     my $self = shift;
60     return C4::SIP::ILS::Item->new(@_);
61 }
62
63 sub institution {
64     my $self = shift;
65     return $self->{institution}->{id};  # consider making this return the whole institution
66 }
67
68 sub institution_id {
69     my $self = shift;
70     return $self->{institution}->{id};
71 }
72
73 sub supports {
74     my ($self, $op) = @_;
75     return (exists($supports{$op}) && $supports{$op});
76 }
77
78 sub check_inst_id {
79     my ($self, $id, $whence) = @_;
80     if ($id ne $self->{institution}->{id}) {
81         siplog("LOG_WARNING", "%s: received institution '%s', expected '%s'", $whence, $id, $self->{institution}->{id});
82         # Just an FYI check, we don't expect the user to change location from that in SIPconfig.xml
83     }
84 }
85
86 sub to_bool {
87     my $bool = shift;
88     # If it's defined, and matches a true sort of string, or is
89     # a non-zero number, then it's true.
90     defined($bool) or return;                   # false
91     ($bool =~ /true|y|yes/i) and return 1;      # true
92     return ($bool =~ /^\d+$/ and $bool != 0);   # true for non-zero numbers, false otherwise
93 }
94
95 sub checkout_ok {
96     my $self = shift;
97     return (exists($self->{institution}->{policy}->{checkout})
98             && to_bool($self->{institution}->{policy}->{checkout}));
99 }
100 sub checkin_ok {
101     my $self = shift;
102     return (exists($self->{institution}->{policy}->{checkin})
103             && to_bool($self->{institution}->{policy}->{checkin}));
104 }
105 sub status_update_ok {
106     my $self = shift;
107     return (exists($self->{institution}->{policy}->{status_update})
108             && to_bool($self->{institution}->{policy}->{status_update}));
109 }
110 sub offline_ok {
111     my $self = shift;
112     return (exists($self->{institution}->{policy}->{offline})
113             && to_bool($self->{institution}->{policy}->{offline}));
114 }
115
116 #
117 # Checkout(patron_id, item_id, sc_renew):
118 #    patron_id & item_id are the identifiers send by the terminal
119 #    sc_renew is the renewal policy configured on the terminal
120 # returns a status opject that can be queried for the various bits
121 # of information that the protocol (SIP or NCIP) needs to generate
122 # the response.
123 #
124 sub checkout {
125     my ( $self, $patron_id, $item_id, $sc_renew, $fee_ack, $account, $no_block_due_date ) = @_;
126     my ( $patron, $item, $circ );
127     $circ = C4::SIP::ILS::Transaction::Checkout->new();
128     # BEGIN TRANSACTION
129     $circ->patron( $patron = C4::SIP::ILS::Patron->new($patron_id) );
130     $circ->item( $item     = C4::SIP::ILS::Item->new($item_id) );
131     if ($fee_ack) {
132         $circ->fee_ack($fee_ack);
133     }
134     if ( !$patron ) {
135         $circ->screen_msg("Invalid Patron");
136     }
137     elsif ( !$patron->charge_ok ) {
138         if ($patron->debarred) {
139             $circ->screen_msg("Patron debarred");
140         } elsif ($patron->expired) {
141             $circ->screen_msg("Patron expired on " . output_pref({ dt => dt_from_string( $patron->dateexpiry_iso, 'iso' ), dateonly => 1 }));
142         } elsif ($patron->fine_blocked) {
143             $circ->screen_msg("Patron has fines");
144         } else {
145             $circ->screen_msg("Patron blocked");
146         }
147     }
148     elsif ( !$item ) {
149         $circ->screen_msg("Invalid Item");
150     }
151     elsif ( $item->{borrowernumber}
152         && !_ci_cardnumber_cmp( $item->{borrowernumber}, $patron->borrowernumber ) )
153     {
154         $circ->screen_msg("Item checked out to another patron");
155     }
156     else {
157         $circ->do_checkout($account, $no_block_due_date);
158         if ( $circ->ok ) {
159
160             # If the item is already associated with this patron, then
161             # we're renewing it.
162             $circ->renew_ok( $item->{borrowernumber}
163                   && _ci_cardnumber_cmp( $item->{borrowernumber}, $patron->borrowernumber ) );
164
165             $item->{borrowernumber}   = $patron_id;
166             $item->{due_date} = $circ->{due};
167             push( @{ $patron->{items} }, { barcode => $item_id } );
168             $circ->desensitize( !$item->magnetic_media );
169
170             siplog(
171                 "LOG_DEBUG", "ILS::Checkout: patron %s has checked out %s",
172                 $patron_id, join( ', ', map{ $_->{barcode} } @{ $patron->{items} } )
173             );
174         }
175         else {
176             siplog( "LOG_ERR", "ILS::Checkout Issue failed" );
177         }
178     }
179
180     # END TRANSACTION
181
182     return $circ;
183 }
184
185 sub _ci_cardnumber_cmp {
186     my ( $s1, $s2) = @_;
187     # As the database is case insensitive we need to normalize two strings
188     # before comparing them
189     return ( uc($s1) eq uc($s2) );
190 }
191
192 # wrapper which allows above to be called for testing
193
194 sub test_cardnumber_compare {
195     my ($self, $str1, $str2) = @_;
196     return _ci_cardnumber_cmp($str1, $str2);
197 }
198
199 sub checkin {
200     my ( $self, $item_id, $trans_date, $return_date, $current_loc, $item_props, $cancel, $account ) = @_;
201
202     my $checked_in_ok     = $account->{checked_in_ok};
203     my $cv_triggers_alert = $account->{cv_triggers_alert};
204     my $holds_block_checkin  = $account->{holds_block_checkin};
205
206     my ( $patron, $item, $circ );
207
208     $circ = C4::SIP::ILS::Transaction::Checkin->new();
209
210     # BEGIN TRANSACTION
211     $circ->item( $item = C4::SIP::ILS::Item->new($item_id) );
212
213     my $data;
214     if ($item) {
215         $data = $circ->do_checkin( $current_loc, $return_date, $account );
216     }
217     else {
218         $circ->alert(1);
219         $circ->alert_type(99);
220         $circ->ok( 0 );
221         $circ->screen_msg('Invalid Item');
222         return $circ;
223     }
224
225     if ( !$circ->ok && $circ->alert_type && $circ->alert_type == 98 ) { # data corruption
226         $circ->screen_msg("Checkin failed: data problem");
227         siplog( "LOG_WARNING", "Problem with issue_id in issues and old_issues; check the about page" );
228     } elsif ( $data->{messages}->{ResFound} && !$circ->ok && $holds_block_checkin ) {
229         $circ->screen_msg("Item is on hold, please return to circulation desk");
230         siplog ("LOG_DEBUG", "C4::SIP::ILS::Checkin - item on hold");
231     } elsif ( $data->{messages}->{withdrawn} && !$circ->ok && C4::Context->preference("BlockReturnOfWithdrawnItems") ) {
232             $circ->screen_msg("Item withdrawn, return not allowed");
233             siplog ("LOG_DEBUG", "C4::SIP::ILS::Checkin - item withdrawn");
234     } elsif ( $data->{messages}->{WasLost} && !$circ->ok && C4::Context->preference("BlockReturnOfLostItems") ) {
235             $circ->screen_msg("Item lost, return not allowed");
236             siplog("LOG_DEBUG", "C4::SIP::ILS::Checkin - item lost");
237     } elsif ( !$item->{borrowernumber} ) {
238         if ( $checked_in_ok ) { # Mark checkin ok although book not checked out
239             $circ->ok( 1 );
240             siplog("LOG_DEBUG", "C4::SIP::ILS::Checkin - using checked_in_ok");
241         } else {
242             $circ->screen_msg("Item not checked out");
243             siplog("LOG_DEBUG", "C4::SIP::ILS::Checkin - item not checked out");
244         }
245     } elsif ( $circ->ok ) {
246         $circ->patron( $patron = C4::SIP::ILS::Patron->new( { borrowernumber => $item->{borrowernumber} } ) );
247         delete $item->{borrowernumber};
248         delete $item->{due_date};
249         $patron->{items} = [ grep { $_ ne $item_id } @{ $patron->{items} } ];
250     } else {
251         # Checkin failed: Wrongbranch or withdrawn?
252         # Bug 10748 with pref BlockReturnOfLostItems adds another case to come
253         # here: returning a lost item when the pref is set.
254         $circ->screen_msg("Checkin failed");
255         siplog( "LOG_WARNING", "Checkin failed: probably for Wrongbranch or withdrawn" );
256     }
257
258     return $circ;
259 }
260
261 # If the ILS caches patron information, this lets it free
262 # it up
263 sub end_patron_session {
264     my ($self, $patron_id) = @_;
265
266     # success?, screen_msg, print_line
267     return (1, 'Thank you !', '');
268 }
269
270 sub pay_fee {
271     my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type, $pay_type, $fee_id, $trans_id, $currency, $is_writeoff, $disallow_overpayment, $register_id) = @_;
272
273     my $trans = C4::SIP::ILS::Transaction::FeePayment->new();
274
275     $trans->transaction_id($trans_id);
276     my $patron;
277     $trans->patron($patron = C4::SIP::ILS::Patron->new($patron_id));
278     if (!$patron) {
279         $trans->screen_msg('Invalid patron barcode.');
280         return $trans;
281     }
282     my $trans_result = $trans->pay( $patron->{borrowernumber}, $fee_amt, $pay_type, $fee_id, $is_writeoff, $disallow_overpayment, $register_id );
283     my $ok = $trans_result->{ok};
284     $trans->ok($ok);
285
286     return {
287         status       => $trans,
288         pay_response => $trans_result->{pay_response}
289     };
290 }
291
292 sub add_hold {
293     my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
294         $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
295     my ($patron, $item);
296
297         my $trans = C4::SIP::ILS::Transaction::Hold->new();
298
299     $patron = C4::SIP::ILS::Patron->new( $patron_id );
300     if ( !$patron ) {
301         $trans->screen_msg("Invalid patron barcode.");
302         return $trans;
303     }
304
305         unless ($item = C4::SIP::ILS::Item->new($item_id || $title_id)) {
306                 $trans->screen_msg("No such item.");
307                 return $trans;
308         }
309
310     if ( $patron->holds_blocked_by_excessive_fees() ) {
311         $trans->screen_msg("Excessive fees blocking placement of hold.");
312     }
313
314    if ($item->fee and $fee_ack ne 'Y') {
315                 $trans->screen_msg = "Fee required to place hold.";
316                 return $trans;
317     }
318
319     my $hold = {
320         item_id         => $item->id,
321         patron_id       => $patron->id,
322         expiration_date => $expiry_date,
323         pickup_location => $pickup_location,
324         hold_type       => $hold_type,
325     };
326
327     $trans->ok(1);
328     $trans->patron($patron);
329     $trans->item($item);
330     $trans->pickup_location($pickup_location);
331         $trans->do_hold;
332
333     push(@{$item->hold_queue},     $hold);
334     push(@{$patron->{hold_items}}, $hold);
335
336     return $trans;
337 }
338
339 sub cancel_hold {
340     my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
341     my ($patron, $item, $hold);
342
343         my $trans = C4::SIP::ILS::Transaction::Hold->new();
344
345     $patron = C4::SIP::ILS::Patron->new( $patron_id );
346     if (!$patron) {
347                 $trans->screen_msg("Invalid patron barcode.");
348                 return $trans;
349     }
350
351     unless ($item = C4::SIP::ILS::Item->new($item_id || $title_id)) {
352                 $trans->screen_msg("No such item.");
353                 return $trans;
354     }
355
356     $trans->patron($patron);
357     $trans->item($item);
358         $trans->drop_hold;
359         unless ($trans->ok) {
360                 $trans->screen_msg("Error with transaction drop_hold: " . $trans->screen_msg);
361                 return $trans;
362         }
363     # Remove the hold from the patron's record first
364     $patron->drop_hold($item_id); # different than the transaction drop!
365
366     # Now, remove it from the item record.  If it was on the patron
367     # record but not on the item record, we'll treat that as success.
368     foreach my $i (0 .. scalar @{$item->hold_queue}) {
369                 $hold = $item->hold_queue->[$i];
370                 if ($item->barcode_is_borrowernumber($patron->id, $hold->{borrowernumber})) {
371                     # found it: delete it.
372                     splice @{$item->hold_queue}, $i, 1;
373                     last;               # ?? should we keep going, in case there are multiples
374                 }
375     }
376
377     $trans->screen_msg("Hold Cancelled.");
378
379     return $trans;
380 }
381
382
383 # The patron and item id's can't be altered, but the
384 # date, location, and type can.
385 sub alter_hold {
386     my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
387         $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
388     my ($patron, $item);
389     my $hold;
390     my $trans;
391
392     $trans = C4::SIP::ILS::Transaction::Hold->new();
393
394     # BEGIN TRANSACTION
395     $patron = C4::SIP::ILS::Patron->new( $patron_id );
396     unless ($patron) {
397                 $trans->screen_msg("Invalid patron barcode: '$patron_id'.");
398                 return $trans;
399     }
400
401     foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
402                 $hold = $patron->{hold_items}[$i];
403
404         if ($hold->{item_id} eq $item_id) {
405             # Found it.  So fix it.
406             $hold->{expiration_date} = $expiry_date     if $expiry_date;
407             $hold->{pickup_location} = $pickup_location if $pickup_location;
408             $hold->{hold_type}       = $hold_type       if $hold_type;
409                 $trans->change_hold();
410             # $trans->ok(1);
411             $trans->screen_msg("Hold updated.");
412             $trans->patron($patron);
413             $trans->item(C4::SIP::ILS::Item->new( $hold->{item_id}));
414             last;
415         }
416     }
417
418     # The same hold structure is linked into both the patron's
419     # list of hold items and into the queue of outstanding holds
420     # for the item, so we don't need to search the hold queue for
421     # the item, since it's already been updated by the patron code.
422
423     if (!$trans->ok) {
424                 $trans->screen_msg("No such outstanding hold.");
425     }
426
427     return $trans;
428 }
429
430 sub renew {
431     my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
432         $no_block, $nb_due_date, $third_party,
433         $item_props, $fee_ack) = @_;
434     my ($patron, $item);
435     my $trans;
436
437     $trans = C4::SIP::ILS::Transaction::Renew->new();
438     $trans->patron($patron = C4::SIP::ILS::Patron->new( $patron_id ));
439
440     if (!$patron) {
441                 $trans->screen_msg("Invalid patron barcode.");
442                 return $trans;
443     } elsif (!$patron->renew_ok) {
444                 $trans->screen_msg("Renewals not allowed.");
445                 return $trans;
446     }
447
448         # Previously: renewing a title, rather than an item (sort of)
449         # This is gross, but in a real ILS it would be better
450
451     # if (defined($title_id)) {
452         #       foreach my $i (@{$patron->{items}}) {
453         #               $item = new ILS::Item $i;
454         #               last if ($title_id eq $item->title_id);
455         #               $item = undef;
456         #       }
457     # } else {
458                 my $j = 0;
459                 my $count = scalar @{$patron->{items}};
460                 foreach my $i (@{$patron->{items}}) {
461             unless (defined $i->{barcode}) {    # FIXME: using data instead of objects may violate the abstraction layer
462                 siplog("LOG_ERR", "No barcode for item %s of %s: $item_id", $j+1, $count);
463                 next;
464             }
465             siplog("LOG_DEBUG", "checking item %s of %s: $item_id vs. %s", ++$j, $count, $i->{barcode});
466             if ($i->{barcode} eq $item_id) {
467                                 # We have it checked out
468                                 $item = C4::SIP::ILS::Item->new( $item_id );
469                                 last;
470                         }
471                 }
472     # }
473
474     $trans->item($item);
475
476     if (!defined($item)) {
477                 $trans->screen_msg("Item not checked out to " . $patron->name);     # not checked out to $patron_id
478         $trans->ok(0);
479     } else {
480         $trans->do_renew();
481         if ($trans->renewal_ok()) {
482             $item->{due_date} = $trans->{due};
483             $trans->desensitize(0);
484         }
485     }
486
487     return $trans;
488 }
489
490 sub renew_all {
491     my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
492     my ($patron, $item_id);
493     my $trans;
494
495     $trans = C4::SIP::ILS::Transaction::RenewAll->new();
496
497     $trans->patron($patron = C4::SIP::ILS::Patron->new( $patron_id ));
498     if (defined $patron) {
499         siplog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s", $patron->name, $patron->renew_ok);
500     } else {
501         siplog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'", $patron_id);
502     }
503
504     if (!$patron) {
505                 $trans->screen_msg("Invalid patron barcode.");
506                 return $trans;
507     } elsif (!$patron->renew_ok) {
508                 $trans->screen_msg("Renewals not allowed.");
509                 return $trans;
510     }
511
512         $trans->do_renew_all;
513     $trans->ok(1);
514     return $trans;
515 }
516
517 1;
518 __END__
519