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