Bug 18110: Folllowup to fix alternative address and add missing class
[koha.git] / C4 / SIP / ILS.pm
blob35879afc7913e0fad222b63c7cf1a8a1b25dc693
2 # ILS.pm: Koha ILS interface module
5 package C4::SIP::ILS;
7 use warnings;
8 use strict;
9 use Sys::Syslog qw(syslog);
10 use Data::Dumper;
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;
22 my $debug = 0;
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,
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;
55 sub find_patron {
56 my $self = shift;
57 $debug and warn "ILS: finding patron";
58 return C4::SIP::ILS::Patron->new(@_);
61 sub find_item {
62 my $self = shift;
63 $debug and warn "ILS: finding item";
64 return C4::SIP::ILS::Item->new(@_);
67 sub institution {
68 my $self = shift;
69 return $self->{institution}->{id}; # consider making this return the whole institution
72 sub institution_id {
73 my $self = shift;
74 return $self->{institution}->{id};
77 sub supports {
78 my ($self, $op) = @_;
79 return (exists($supports{$op}) && $supports{$op});
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
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
99 sub checkout_ok {
100 my $self = shift;
101 return (exists($self->{institution}->{policy}->{checkout})
102 && to_bool($self->{institution}->{policy}->{checkout}));
104 sub checkin_ok {
105 my $self = shift;
106 return (exists($self->{institution}->{policy}->{checkin})
107 && to_bool($self->{institution}->{policy}->{checkin}));
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}));
114 sub offline_ok {
115 my $self = shift;
116 return (exists($self->{institution}->{policy}->{offline})
117 && to_bool($self->{institution}->{policy}->{offline}));
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.
128 sub checkout {
129 my ( $self, $patron_id, $item_id, $sc_renew, $fee_ack ) = @_;
130 my ( $patron, $item, $circ );
132 $circ = C4::SIP::ILS::Transaction::Checkout->new();
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);
141 if ( !$patron ) {
142 $circ->screen_msg("Invalid Patron");
144 elsif ( !$patron->charge_ok ) {
145 $circ->screen_msg("Patron Blocked");
147 elsif ( !$item ) {
148 $circ->screen_msg("Invalid Item");
150 elsif ( $item->{patron}
151 && !_ci_cardnumber_cmp( $item->{patron}, $patron_id ) )
153 $circ->screen_msg("Item checked out to another patron");
155 else {
156 $circ->do_checkout();
157 if ( $circ->ok ) {
158 $debug and warn "circ is ok";
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 ) );
165 $item->{patron} = $patron_id;
166 $item->{due_date} = $circ->{due};
167 push( @{ $patron->{items} }, $item_id );
168 $circ->desensitize( !$item->magnetic_media );
170 syslog(
171 "LOG_DEBUG", "ILS::Checkout: patron %s has checked out %s",
172 $patron_id, join( ', ', @{ $patron->{items} } )
175 else {
176 syslog( "LOG_ERR", "ILS::Checkout Issue failed" );
180 # END TRANSACTION
182 return $circ;
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) );
192 # wrapper which allows above to be called for testing
194 sub test_cardnumber_compare {
195 my ($self, $str1, $str2) = @_;
196 return _ci_cardnumber_cmp($str1, $str2);
199 sub checkin {
200 my ( $self, $item_id, $trans_date, $return_date, $current_loc, $item_props, $cancel, $checked_in_ok ) = @_;
201 my ( $patron, $item, $circ );
203 $circ = C4::SIP::ILS::Transaction::Checkin->new();
205 # BEGIN TRANSACTION
206 $circ->item( $item = C4::SIP::ILS::Item->new($item_id) );
208 if ($item) {
209 $circ->do_checkin( $current_loc, $return_date );
211 else {
212 $circ->alert(1);
213 $circ->alert_type(99);
214 $circ->screen_msg('Invalid Item');
217 # It's ok to check it in if it exists, and if it was checked out
218 # or it was not checked out but the checked_in_ok flag was set
219 $circ->ok( ( $checked_in_ok && $item ) || ( $item && $item->{patron} ) );
220 syslog("LOG_DEBUG", "C4::SIP::ILS::checkin - using checked_in_ok") if $checked_in_ok;
222 if ( !defined( $item->{patron} ) ) {
223 $circ->screen_msg("Item not checked out") unless $checked_in_ok;
224 syslog("LOG_DEBUG", "C4::SIP::ILS::checkin - item not checked out");
226 else {
227 if ( $circ->ok ) {
228 $circ->patron( $patron = C4::SIP::ILS::Patron->new( $item->{patron} ) );
229 delete $item->{patron};
230 delete $item->{due_date};
231 $patron->{items} = [ grep { $_ ne $item_id } @{ $patron->{items} } ];
235 # END TRANSACTION
237 return $circ;
240 # If the ILS caches patron information, this lets it free
241 # it up
242 sub end_patron_session {
243 my ($self, $patron_id) = @_;
245 # success?, screen_msg, print_line
246 return (1, 'Thank you !', '');
249 sub pay_fee {
250 my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type, $pay_type, $fee_id, $trans_id, $currency, $is_writeoff) = @_;
252 my $trans = C4::SIP::ILS::Transaction::FeePayment->new();
254 $trans->transaction_id($trans_id);
255 my $patron;
256 $trans->patron($patron = C4::SIP::ILS::Patron->new($patron_id));
257 if (!$patron) {
258 $trans->screen_msg('Invalid patron barcode.');
259 return $trans;
261 my $ok = $trans->pay( $patron->{borrowernumber}, $fee_amt, $pay_type, $fee_id, $is_writeoff );
262 $trans->ok($ok);
264 return $trans;
267 sub add_hold {
268 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
269 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
270 my ($patron, $item);
272 my $trans = C4::SIP::ILS::Transaction::Hold->new();
274 $patron = C4::SIP::ILS::Patron->new( $patron_id);
275 if (!$patron
276 || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
277 $trans->screen_msg("Invalid Patron.");
278 return $trans;
281 unless ($item = C4::SIP::ILS::Item->new($item_id || $title_id)) {
282 $trans->screen_msg("No such item.");
283 return $trans;
286 if ( $patron->holds_blocked_by_excessive_fees() ) {
287 $trans->screen_msg("Excessive fees blocking placement of hold.");
290 if ($item->fee and $fee_ack ne 'Y') {
291 $trans->screen_msg = "Fee required to place hold.";
292 return $trans;
295 my $hold = {
296 item_id => $item->id,
297 patron_id => $patron->id,
298 expiration_date => $expiry_date,
299 pickup_location => $pickup_location,
300 hold_type => $hold_type,
303 $trans->ok(1);
304 $trans->patron($patron);
305 $trans->item($item);
306 $trans->pickup_location($pickup_location);
307 $trans->do_hold;
309 push(@{$item->hold_queue}, $hold);
310 push(@{$patron->{hold_items}}, $hold);
312 return $trans;
315 sub cancel_hold {
316 my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
317 my ($patron, $item, $hold);
319 my $trans = C4::SIP::ILS::Transaction::Hold->new();
321 $patron = C4::SIP::ILS::Patron->new( $patron_id );
322 if (!$patron) {
323 $trans->screen_msg("Invalid patron barcode.");
324 return $trans;
325 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
326 $trans->screen_msg('Invalid patron password.');
327 return $trans;
330 unless ($item = C4::SIP::ILS::Item->new($item_id || $title_id)) {
331 $trans->screen_msg("No such item.");
332 return $trans;
335 $trans->patron($patron);
336 $trans->item($item);
337 $trans->drop_hold;
338 unless ($trans->ok) {
339 $trans->screen_msg("Error with transaction drop_hold: " . $trans->screen_msg);
340 return $trans;
342 # Remove the hold from the patron's record first
343 $trans->ok($patron->drop_hold($item_id)); # different than the transaction drop!
345 unless ($trans->ok) {
346 # We didn't find it on the patron record
347 $trans->screen_msg("No such hold on patron record.");
348 return $trans;
351 # Now, remove it from the item record. If it was on the patron
352 # record but not on the item record, we'll treat that as success.
353 foreach my $i (0 .. scalar @{$item->hold_queue}) {
354 $hold = $item->hold_queue->[$i];
355 if ($item->barcode_is_borrowernumber($patron->id, $hold->{borrowernumber})) {
356 # found it: delete it.
357 splice @{$item->hold_queue}, $i, 1;
358 last; # ?? should we keep going, in case there are multiples
362 $trans->screen_msg("Hold Cancelled.");
364 return $trans;
368 # The patron and item id's can't be altered, but the
369 # date, location, and type can.
370 sub alter_hold {
371 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
372 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
373 my ($patron, $item);
374 my $hold;
375 my $trans;
377 $trans = C4::SIP::ILS::Transaction::Hold->new();
379 # BEGIN TRANSACTION
380 $patron = C4::SIP::ILS::Patron->new( $patron_id );
381 unless ($patron) {
382 $trans->screen_msg("Invalid patron barcode: '$patron_id'.");
383 return $trans;
386 foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
387 $hold = $patron->{hold_items}[$i];
389 if ($hold->{item_id} eq $item_id) {
390 # Found it. So fix it.
391 $hold->{expiration_date} = $expiry_date if $expiry_date;
392 $hold->{pickup_location} = $pickup_location if $pickup_location;
393 $hold->{hold_type} = $hold_type if $hold_type;
394 $trans->change_hold();
395 # $trans->ok(1);
396 $trans->screen_msg("Hold updated.");
397 $trans->patron($patron);
398 $trans->item(C4::SIP::ILS::Item->new( $hold->{item_id}));
399 last;
403 # The same hold structure is linked into both the patron's
404 # list of hold items and into the queue of outstanding holds
405 # for the item, so we don't need to search the hold queue for
406 # the item, since it's already been updated by the patron code.
408 if (!$trans->ok) {
409 $trans->screen_msg("No such outstanding hold.");
412 return $trans;
415 sub renew {
416 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
417 $no_block, $nb_due_date, $third_party,
418 $item_props, $fee_ack) = @_;
419 my ($patron, $item);
420 my $trans;
422 $trans = C4::SIP::ILS::Transaction::Renew->new();
423 $trans->patron($patron = C4::SIP::ILS::Patron->new( $patron_id ));
425 if (!$patron) {
426 $trans->screen_msg("Invalid patron barcode.");
427 return $trans;
428 } elsif (!$patron->renew_ok) {
429 $trans->screen_msg("Renewals not allowed.");
430 return $trans;
433 # Previously: renewing a title, rather than an item (sort of)
434 # This is gross, but in a real ILS it would be better
436 # if (defined($title_id)) {
437 # foreach my $i (@{$patron->{items}}) {
438 # $item = new ILS::Item $i;
439 # last if ($title_id eq $item->title_id);
440 # $item = undef;
442 # } else {
443 my $j = 0;
444 my $count = scalar @{$patron->{items}};
445 foreach my $i (@{$patron->{items}}) {
446 unless (defined $i->{barcode}) { # FIXME: using data instead of objects may violate the abstraction layer
447 syslog("LOG_ERR", "No barcode for item %s of %s: $item_id", $j+1, $count);
448 next;
450 syslog("LOG_DEBUG", "checking item %s of %s: $item_id vs. %s", ++$j, $count, $i->{barcode});
451 if ($i->{barcode} eq $item_id) {
452 # We have it checked out
453 $item = C4::SIP::ILS::Item->new( $item_id );
454 last;
459 $trans->item($item);
461 if (!defined($item)) {
462 $trans->screen_msg("Item not checked out to " . $patron->name); # not checked out to $patron_id
463 $trans->ok(0);
464 } else {
465 $trans->do_renew();
466 if ($trans->renewal_ok()) {
467 $item->{due_date} = $trans->{due};
468 $trans->desensitize(0);
472 return $trans;
475 sub renew_all {
476 my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
477 my ($patron, $item_id);
478 my $trans;
480 $trans = C4::SIP::ILS::Transaction::RenewAll->new();
482 $trans->patron($patron = C4::SIP::ILS::Patron->new( $patron_id ));
483 if (defined $patron) {
484 syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s", $patron->name, $patron->renew_ok);
485 } else {
486 syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'", $patron_id);
489 if (!defined($patron)) {
490 $trans->screen_msg("Invalid patron barcode.");
491 return $trans;
492 } elsif (!$patron->renew_ok) {
493 $trans->screen_msg("Renewals not allowed.");
494 return $trans;
495 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
496 $trans->screen_msg("Invalid patron password.");
497 return $trans;
500 $trans->do_renew_all;
501 $trans->ok(1);
502 return $trans;
506 __END__