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