e6f3794c97f3bab1dd89e65b652a10225e51c9c0
[koha-equinox.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 Sys::Syslog qw(syslog);
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 $debug = 0;
23
24 my %supports = (
25     'magnetic media'        => 1,
26     'security inhibit'      => 0,
27     'offline operation'     => 0,
28     "patron status request" => 1,
29     "checkout"              => 1,
30     "checkin"               => 1,
31     "block patron"          => 1,
32     "acs status"            => 1,
33     "login"                 => 1,
34     "patron information"    => 1,
35     "end patron session"    => 1,
36     "fee paid"              => 1,
37     "item information"      => 1,
38     "item status update"    => 0,
39     "patron enable"         => 1,
40     "hold"                  => 1,
41     "renew"                 => 1,
42     "renew all"             => 1,
43 );
44
45 sub new {
46     my ($class, $institution) = @_;
47     my $type = ref($class) || $class;
48     my $self = {};
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 C4::SIP::ILS::Patron->new(@_);
59 }
60
61 sub find_item {
62     my $self = shift;
63         $debug and warn "ILS: finding item";
64     return C4::SIP::ILS::Item->new(@_);
65 }
66
67 sub institution {
68     my $self = shift;
69     return $self->{institution}->{id};  # consider making this return the whole institution
70 }
71
72 sub institution_id {
73     my $self = shift;
74     return $self->{institution}->{id};
75 }
76
77 sub supports {
78     my ($self, $op) = @_;
79     return (exists($supports{$op}) && $supports{$op});
80 }
81
82 sub check_inst_id {
83     my ($self, $id, $whence) = @_;
84     if ($id ne $self->{institution}->{id}) {
85         syslog("LOG_WARNING", "%s: received institution '%s', expected '%s'", $whence, $id, $self->{institution}->{id});
86         # Just an FYI check, we don't expect the user to change location from that in SIPconfig.xml
87     }
88 }
89
90 sub to_bool {
91     my $bool = shift;
92     # If it's defined, and matches a true sort of string, or is
93     # a non-zero number, then it's true.
94     defined($bool) or return;                   # false
95     ($bool =~ /true|y|yes/i) and return 1;      # true
96     return ($bool =~ /^\d+$/ and $bool != 0);   # true for non-zero numbers, false otherwise
97 }
98
99 sub checkout_ok {
100     my $self = shift;
101     return (exists($self->{institution}->{policy}->{checkout})
102             && to_bool($self->{institution}->{policy}->{checkout}));
103 }
104 sub checkin_ok {
105     my $self = shift;
106     return (exists($self->{institution}->{policy}->{checkin})
107             && to_bool($self->{institution}->{policy}->{checkin}));
108 }
109 sub status_update_ok {
110     my $self = shift;
111     return (exists($self->{institution}->{policy}->{status_update})
112             && to_bool($self->{institution}->{policy}->{status_update}));
113 }
114 sub offline_ok {
115     my $self = shift;
116     return (exists($self->{institution}->{policy}->{offline})
117             && to_bool($self->{institution}->{policy}->{offline}));
118 }
119
120 #
121 # Checkout(patron_id, item_id, sc_renew):
122 #    patron_id & item_id are the identifiers send by the terminal
123 #    sc_renew is the renewal policy configured on the terminal
124 # returns a status opject that can be queried for the various bits
125 # of information that the protocol (SIP or NCIP) needs to generate
126 # the response.
127 #
128 sub checkout {
129     my ( $self, $patron_id, $item_id, $sc_renew, $fee_ack ) = @_;
130     my ( $patron, $item, $circ );
131
132     $circ = C4::SIP::ILS::Transaction::Checkout->new();
133
134     # BEGIN TRANSACTION
135     $circ->patron( $patron = C4::SIP::ILS::Patron->new($patron_id) );
136     $circ->item( $item     = C4::SIP::ILS::Item->new($item_id) );
137     if ($fee_ack) {
138         $circ->fee_ack($fee_ack);
139     }
140
141     if ( !$patron ) {
142         $circ->screen_msg("Invalid Patron");
143     }
144     elsif ( !$patron->charge_ok ) {
145         $circ->screen_msg("Patron Blocked");
146     }
147     elsif ( !$item ) {
148         $circ->screen_msg("Invalid Item");
149     }
150     elsif ( $item->{patron}
151         && !_ci_cardnumber_cmp( $item->{patron}, $patron_id ) )
152     {
153         $circ->screen_msg("Item checked out to another patron");
154     }
155     else {
156         $circ->do_checkout();
157         if ( $circ->ok ) {
158             $debug and warn "circ is ok";
159
160             # If the item is already associated with this patron, then
161             # we're renewing it.
162             $circ->renew_ok( $item->{patron}
163                   && _ci_cardnumber_cmp( $item->{patron}, $patron_id ) );
164
165             $item->{patron}   = $patron_id;
166             $item->{due_date} = $circ->{due};
167             push( @{ $patron->{items} }, $item_id );
168             $circ->desensitize( !$item->magnetic_media );
169
170             syslog(
171                 "LOG_DEBUG", "ILS::Checkout: patron %s has checked out %s",
172                 $patron_id, join( ', ', @{ $patron->{items} } )
173             );
174         }
175         else {
176             syslog( "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, $checked_in_ok, $cv_triggers_alert ) = @_;
201     my ( $patron, $item, $circ );
202
203     $circ = C4::SIP::ILS::Transaction::Checkin->new();
204
205     # BEGIN TRANSACTION
206     $circ->item( $item = C4::SIP::ILS::Item->new($item_id) );
207
208     my $data;
209     if ($item) {
210         $data = $circ->do_checkin( $current_loc, $return_date, $cv_triggers_alert );
211     }
212     else {
213         $circ->alert(1);
214         $circ->alert_type(99);
215         $circ->ok( 0 );
216         $circ->screen_msg('Invalid Item');
217         return $circ;
218     }
219
220     if ( !$circ->ok && $circ->alert_type && $circ->alert_type == 98 ) { # data corruption
221         $circ->screen_msg("Checkin failed: data problem");
222         syslog( "LOG_WARNING", "Problem with issue_id in issues and old_issues; check the about page" );
223     } elsif ( $data->{messages}->{withdrawn} && !$circ->ok ) {
224             $circ->screen_msg("Item withdrawn, return not allowed");
225             syslog("LOG_DEBUG", "C4::SIP::ILS::Checkin - item withdrawn");
226     } elsif ( $data->{messages}->{WasLost} && !$circ->ok ) {
227             $circ->screen_msg("Item lost, return not allowed");
228             syslog("LOG_DEBUG", "C4::SIP::ILS::Checkin - item lost");
229     } elsif ( !$item->{patron} ) {
230         if ( $checked_in_ok ) { # Mark checkin ok although book not checked out
231             $circ->ok( 1 );
232             $circ->alert( 0 );
233             syslog("LOG_DEBUG", "C4::SIP::ILS::Checkin - using checked_in_ok");
234         } else {
235             $circ->screen_msg("Item not checked out");
236             syslog("LOG_DEBUG", "C4::SIP::ILS::Checkin - item not checked out");
237         }
238     } elsif ( $circ->ok ) {
239         $circ->patron( $patron = C4::SIP::ILS::Patron->new( $item->{patron} ) );
240         delete $item->{patron};
241         delete $item->{due_date};
242         $patron->{items} = [ grep { $_ ne $item_id } @{ $patron->{items} } ];
243     } else {
244         # Checkin failed: Wrongbranch or withdrawn?
245         # Bug 10748 with pref BlockReturnOfLostItems adds another case to come
246         # here: returning a lost item when the pref is set.
247         $circ->screen_msg("Checkin failed");
248         syslog( "LOG_WARNING", "Checkin failed: probably for Wrongbranch or withdrawn" );
249     }
250
251     return $circ;
252 }
253
254 # If the ILS caches patron information, this lets it free
255 # it up
256 sub end_patron_session {
257     my ($self, $patron_id) = @_;
258
259     # success?, screen_msg, print_line
260     return (1, 'Thank you !', '');
261 }
262
263 sub pay_fee {
264     my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type, $pay_type, $fee_id, $trans_id, $currency, $is_writeoff, $disallow_overpayment ) = @_;
265
266     my $trans = C4::SIP::ILS::Transaction::FeePayment->new();
267
268     $trans->transaction_id($trans_id);
269     my $patron;
270     $trans->patron($patron = C4::SIP::ILS::Patron->new($patron_id));
271     if (!$patron) {
272         $trans->screen_msg('Invalid patron barcode.');
273         return $trans;
274     }
275     my $ok = $trans->pay( $patron->{borrowernumber}, $fee_amt, $pay_type, $fee_id, $is_writeoff, $disallow_overpayment );
276     $trans->ok($ok);
277
278     return $trans;
279 }
280
281 sub add_hold {
282     my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
283         $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
284     my ($patron, $item);
285
286         my $trans = C4::SIP::ILS::Transaction::Hold->new();
287
288     $patron = C4::SIP::ILS::Patron->new( $patron_id);
289     if (!$patron
290         || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
291                 $trans->screen_msg("Invalid Patron.");
292                 return $trans;
293     }
294
295         unless ($item = C4::SIP::ILS::Item->new($item_id || $title_id)) {
296                 $trans->screen_msg("No such item.");
297                 return $trans;
298         }
299
300     if ( $patron->holds_blocked_by_excessive_fees() ) {
301         $trans->screen_msg("Excessive fees blocking placement of hold.");
302     }
303
304    if ($item->fee and $fee_ack ne 'Y') {
305                 $trans->screen_msg = "Fee required to place hold.";
306                 return $trans;
307     }
308
309     my $hold = {
310         item_id         => $item->id,
311         patron_id       => $patron->id,
312         expiration_date => $expiry_date,
313         pickup_location => $pickup_location,
314         hold_type       => $hold_type,
315     };
316
317     $trans->ok(1);
318     $trans->patron($patron);
319     $trans->item($item);
320     $trans->pickup_location($pickup_location);
321         $trans->do_hold;
322
323     push(@{$item->hold_queue},     $hold);
324     push(@{$patron->{hold_items}}, $hold);
325
326     return $trans;
327 }
328
329 sub cancel_hold {
330     my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
331     my ($patron, $item, $hold);
332
333         my $trans = C4::SIP::ILS::Transaction::Hold->new();
334
335     $patron = C4::SIP::ILS::Patron->new( $patron_id );
336     if (!$patron) {
337                 $trans->screen_msg("Invalid patron barcode.");
338                 return $trans;
339     } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
340                 $trans->screen_msg('Invalid patron password.');
341                 return $trans;
342     }
343
344     unless ($item = C4::SIP::ILS::Item->new($item_id || $title_id)) {
345                 $trans->screen_msg("No such item.");
346                 return $trans;
347     }
348
349     $trans->patron($patron);
350     $trans->item($item);
351         $trans->drop_hold;
352         unless ($trans->ok) {
353                 $trans->screen_msg("Error with transaction drop_hold: " . $trans->screen_msg);
354                 return $trans;
355         }
356     # Remove the hold from the patron's record first
357     $trans->ok($patron->drop_hold($item_id));   # different than the transaction drop!
358
359     unless ($trans->ok) {
360                 # We didn't find it on the patron record
361                 $trans->screen_msg("No such hold on patron record.");
362                 return $trans;
363     }
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                 syslog("LOG_ERR", "No barcode for item %s of %s: $item_id", $j+1, $count);
462                 next;
463             }
464             syslog("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         syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s", $patron->name, $patron->renew_ok);
499     } else {
500         syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'", $patron_id);
501     }
502
503     if (!defined($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     } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
510                 $trans->screen_msg("Invalid patron password.");
511                 return $trans;
512     }
513
514         $trans->do_renew_all;
515     $trans->ok(1);
516     return $trans;
517 }
518
519 1;
520 __END__
521