2 # ILS.pm: Koha ILS interface module
9 use C4::SIP::Sip qw( siplog );
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;
23 'magnetic media' => 1,
24 'security inhibit' => 0,
25 'offline operation' => 0,
26 "patron status request" => 1,
32 "patron information" => 1,
33 "end patron session" => 1,
35 "item information" => 1,
36 "item status update" => 0,
44 my ($class, $institution) = @_;
45 my $type = ref($class) || $class;
47 siplog("LOG_DEBUG", "new ILS '%s'", $institution->{id});
48 $self->{institution} = $institution;
49 return bless $self, $type;
54 return C4::SIP::ILS::Patron->new(@_);
59 return C4::SIP::ILS::Item->new(@_);
64 return $self->{institution}->{id}; # consider making this return the whole institution
69 return $self->{institution}->{id};
74 return (exists($supports{$op}) && $supports{$op});
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
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
96 return (exists($self->{institution}->{policy}->{checkout})
97 && to_bool($self->{institution}->{policy}->{checkout}));
101 return (exists($self->{institution}->{policy}->{checkin})
102 && to_bool($self->{institution}->{policy}->{checkin}));
104 sub status_update_ok {
106 return (exists($self->{institution}->{policy}->{status_update})
107 && to_bool($self->{institution}->{policy}->{status_update}));
111 return (exists($self->{institution}->{policy}->{offline})
112 && to_bool($self->{institution}->{policy}->{offline}));
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
124 my ( $self, $patron_id, $item_id, $sc_renew, $fee_ack, $account ) = @_;
125 my ( $patron, $item, $circ );
127 $circ = C4::SIP::ILS::Transaction::Checkout->new();
130 $circ->patron( $patron = C4::SIP::ILS::Patron->new($patron_id) );
131 $circ->item( $item = C4::SIP::ILS::Item->new($item_id) );
133 $circ->fee_ack($fee_ack);
137 $circ->screen_msg("Invalid Patron");
139 elsif ( !$patron->charge_ok ) {
140 $circ->screen_msg("Patron Blocked");
143 $circ->screen_msg("Invalid Item");
145 elsif ( $item->{borrowernumber}
146 && !_ci_cardnumber_cmp( $item->{borrowernumber}, $patron->borrowernumber ) )
148 $circ->screen_msg("Item checked out to another patron");
151 $circ->do_checkout($account);
154 # If the item is already associated with this patron, then
156 $circ->renew_ok( $item->{borrowernumber}
157 && _ci_cardnumber_cmp( $item->{borrowernumber}, $patron->borrowernumber ) );
159 $item->{borrowernumber} = $patron_id;
160 $item->{due_date} = $circ->{due};
161 push( @{ $patron->{items} }, { barcode => $item_id } );
162 $circ->desensitize( !$item->magnetic_media );
165 "LOG_DEBUG", "ILS::Checkout: patron %s has checked out %s",
166 $patron_id, join( ', ', map{ $_->{barcode} } @{ $patron->{items} } )
170 siplog( "LOG_ERR", "ILS::Checkout Issue failed" );
179 sub _ci_cardnumber_cmp {
181 # As the database is case insensitive we need to normalize two strings
182 # before comparing them
183 return ( uc($s1) eq uc($s2) );
186 # wrapper which allows above to be called for testing
188 sub test_cardnumber_compare {
189 my ($self, $str1, $str2) = @_;
190 return _ci_cardnumber_cmp($str1, $str2);
194 my ( $self, $item_id, $trans_date, $return_date, $current_loc, $item_props, $cancel, $account ) = @_;
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};
200 my ( $patron, $item, $circ );
202 $circ = C4::SIP::ILS::Transaction::Checkin->new();
205 $circ->item( $item = C4::SIP::ILS::Item->new($item_id) );
209 $data = $circ->do_checkin( $current_loc, $return_date, $account );
213 $circ->alert_type(99);
215 $circ->screen_msg('Invalid Item');
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
234 siplog("LOG_DEBUG", "C4::SIP::ILS::Checkin - using checked_in_ok");
236 $circ->screen_msg("Item not checked out");
237 siplog("LOG_DEBUG", "C4::SIP::ILS::Checkin - item not checked out");
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} } ];
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" );
255 # If the ILS caches patron information, this lets it free
257 sub end_patron_session {
258 my ($self, $patron_id) = @_;
260 # success?, screen_msg, print_line
261 return (1, 'Thank you !', '');
265 my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type, $pay_type, $fee_id, $trans_id, $currency, $is_writeoff, $disallow_overpayment, $register_id) = @_;
267 my $trans = C4::SIP::ILS::Transaction::FeePayment->new();
269 $trans->transaction_id($trans_id);
271 $trans->patron($patron = C4::SIP::ILS::Patron->new($patron_id));
273 $trans->screen_msg('Invalid patron barcode.');
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};
282 pay_response => $trans_result->{pay_response}
287 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
288 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
291 my $trans = C4::SIP::ILS::Transaction::Hold->new();
293 $patron = C4::SIP::ILS::Patron->new( $patron_id );
295 $trans->screen_msg("Invalid patron barcode.");
299 unless ($item = C4::SIP::ILS::Item->new($item_id || $title_id)) {
300 $trans->screen_msg("No such item.");
304 if ( $patron->holds_blocked_by_excessive_fees() ) {
305 $trans->screen_msg("Excessive fees blocking placement of hold.");
308 if ($item->fee and $fee_ack ne 'Y') {
309 $trans->screen_msg = "Fee required to place 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,
322 $trans->patron($patron);
324 $trans->pickup_location($pickup_location);
327 push(@{$item->hold_queue}, $hold);
328 push(@{$patron->{hold_items}}, $hold);
334 my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
335 my ($patron, $item, $hold);
337 my $trans = C4::SIP::ILS::Transaction::Hold->new();
339 $patron = C4::SIP::ILS::Patron->new( $patron_id );
341 $trans->screen_msg("Invalid patron barcode.");
345 unless ($item = C4::SIP::ILS::Item->new($item_id || $title_id)) {
346 $trans->screen_msg("No such item.");
350 $trans->patron($patron);
353 unless ($trans->ok) {
354 $trans->screen_msg("Error with transaction drop_hold: " . $trans->screen_msg);
357 # Remove the hold from the patron's record first
358 $patron->drop_hold($item_id); # different than the transaction drop!
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
371 $trans->screen_msg("Hold Cancelled.");
377 # The patron and item id's can't be altered, but the
378 # date, location, and type can.
380 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
381 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
386 $trans = C4::SIP::ILS::Transaction::Hold->new();
389 $patron = C4::SIP::ILS::Patron->new( $patron_id );
391 $trans->screen_msg("Invalid patron barcode: '$patron_id'.");
395 foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
396 $hold = $patron->{hold_items}[$i];
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();
405 $trans->screen_msg("Hold updated.");
406 $trans->patron($patron);
407 $trans->item(C4::SIP::ILS::Item->new( $hold->{item_id}));
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.
418 $trans->screen_msg("No such outstanding hold.");
425 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
426 $no_block, $nb_due_date, $third_party,
427 $item_props, $fee_ack) = @_;
431 $trans = C4::SIP::ILS::Transaction::Renew->new();
432 $trans->patron($patron = C4::SIP::ILS::Patron->new( $patron_id ));
435 $trans->screen_msg("Invalid patron barcode.");
437 } elsif (!$patron->renew_ok) {
438 $trans->screen_msg("Renewals not allowed.");
442 # Previously: renewing a title, rather than an item (sort of)
443 # This is gross, but in a real ILS it would be better
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);
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);
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 );
470 if (!defined($item)) {
471 $trans->screen_msg("Item not checked out to " . $patron->name); # not checked out to $patron_id
475 if ($trans->renewal_ok()) {
476 $item->{due_date} = $trans->{due};
477 $trans->desensitize(0);
485 my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
486 my ($patron, $item_id);
489 $trans = C4::SIP::ILS::Transaction::RenewAll->new();
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);
495 siplog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'", $patron_id);
499 $trans->screen_msg("Invalid patron barcode.");
501 } elsif (!$patron->renew_ok) {
502 $trans->screen_msg("Renewals not allowed.");
506 $trans->do_renew_all;