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