2 # ILS.pm: Koha ILS interface module
9 use C4::SIP::Sip qw( siplog );
10 use Koha::DateUtils qw( dt_from_string output_pref );
13 use C4::SIP::ILS::Item;
14 use C4::SIP::ILS::Patron;
15 use C4::SIP::ILS::Transaction;
16 use C4::SIP::ILS::Transaction::Checkout;
17 use C4::SIP::ILS::Transaction::Checkin;
18 use C4::SIP::ILS::Transaction::FeePayment;
19 use C4::SIP::ILS::Transaction::Hold;
20 use C4::SIP::ILS::Transaction::Renew;
21 use C4::SIP::ILS::Transaction::RenewAll;
24 'magnetic media' => 1,
25 'security inhibit' => 0,
26 'offline operation' => 0,
27 "patron status request" => 1,
33 "patron information" => 1,
34 "end patron session" => 1,
36 "item information" => 1,
37 "item status update" => 0,
45 my ($class, $institution) = @_;
46 my $type = ref($class) || $class;
48 siplog("LOG_DEBUG", "new ILS '%s'", $institution->{id});
49 $self->{institution} = $institution;
50 return bless $self, $type;
55 return C4::SIP::ILS::Patron->new(@_);
60 return C4::SIP::ILS::Item->new(@_);
65 return $self->{institution}->{id}; # consider making this return the whole institution
70 return $self->{institution}->{id};
75 return (exists($supports{$op}) && $supports{$op});
79 my ($self, $id, $whence) = @_;
80 if ($id ne $self->{institution}->{id}) {
81 siplog("LOG_WARNING", "%s: received institution '%s', expected '%s'", $whence, $id, $self->{institution}->{id});
82 # Just an FYI check, we don't expect the user to change location from that in SIPconfig.xml
88 # If it's defined, and matches a true sort of string, or is
89 # a non-zero number, then it's true.
90 defined($bool) or return; # false
91 ($bool =~ /true|y|yes/i) and return 1; # true
92 return ($bool =~ /^\d+$/ and $bool != 0); # true for non-zero numbers, false otherwise
97 return (exists($self->{institution}->{policy}->{checkout})
98 && to_bool($self->{institution}->{policy}->{checkout}));
102 return (exists($self->{institution}->{policy}->{checkin})
103 && to_bool($self->{institution}->{policy}->{checkin}));
105 sub status_update_ok {
107 return (exists($self->{institution}->{policy}->{status_update})
108 && to_bool($self->{institution}->{policy}->{status_update}));
112 return (exists($self->{institution}->{policy}->{offline})
113 && to_bool($self->{institution}->{policy}->{offline}));
117 # Checkout(patron_id, item_id, sc_renew):
118 # patron_id & item_id are the identifiers send by the terminal
119 # sc_renew is the renewal policy configured on the terminal
120 # returns a status opject that can be queried for the various bits
121 # of information that the protocol (SIP or NCIP) needs to generate
125 my ( $self, $patron_id, $item_id, $sc_renew, $fee_ack, $account, $no_block_due_date ) = @_;
126 my ( $patron, $item, $circ );
127 my @blocked_item_types;
128 if (defined $account->{blocked_item_types}) {
129 @blocked_item_types = split /\|/, $account->{blocked_item_types};
131 $circ = C4::SIP::ILS::Transaction::Checkout->new();
133 $circ->patron( $patron = C4::SIP::ILS::Patron->new($patron_id) );
134 $circ->item( $item = C4::SIP::ILS::Item->new($item_id) );
136 $circ->fee_ack($fee_ack);
139 $circ->screen_msg("Invalid Patron");
141 elsif ( !$patron->charge_ok ) {
142 if ($patron->debarred) {
143 $circ->screen_msg("Patron debarred");
144 } elsif ($patron->expired) {
145 $circ->screen_msg("Patron expired on " . output_pref({ dt => dt_from_string( $patron->dateexpiry_iso, 'iso' ), dateonly => 1 }));
146 } elsif ($patron->fine_blocked) {
147 my $message = "Patron has fines";
148 if ($account->{show_outstanding_amount}) {
149 my $patron_account = Koha::Account->new( { patron_id => $patron->{borrowernumber} });
150 my $balance = $patron_account->balance;
152 $message .= (" - You owe " . Koha::Number::Price->new( $balance )->format({ with_symbol => 1}) . ".");
155 $circ->screen_msg($message);
157 $circ->screen_msg("Patron blocked");
161 $circ->screen_msg("Invalid Item");
163 elsif ( $item->{borrowernumber}
164 && !_ci_cardnumber_cmp( $item->{borrowernumber}, $patron->borrowernumber ) )
166 $circ->screen_msg("Item checked out to another patron");
168 elsif (grep { $_ eq $item->{itemtype} } @blocked_item_types) {
169 $circ->screen_msg("Item type cannot be checked out at this checkout location");
172 $circ->do_checkout($account, $no_block_due_date);
175 # If the item is already associated with this patron, then
177 $circ->renew_ok( $item->{borrowernumber}
178 && _ci_cardnumber_cmp( $item->{borrowernumber}, $patron->borrowernumber ) );
180 $item->{borrowernumber} = $patron_id;
181 $item->{due_date} = $circ->{due};
182 push( @{ $patron->{items} }, { barcode => $item_id } );
183 $circ->desensitize( !$item->magnetic_media );
186 "LOG_DEBUG", "ILS::Checkout: patron %s has checked out %s",
187 $patron_id, join( ', ', map{ $_->{barcode} } @{ $patron->{items} } )
191 siplog( "LOG_ERR", "ILS::Checkout Issue failed" );
200 sub _ci_cardnumber_cmp {
202 # As the database is case insensitive we need to normalize two strings
203 # before comparing them
204 return ( uc($s1) eq uc($s2) );
207 # wrapper which allows above to be called for testing
209 sub test_cardnumber_compare {
210 my ($self, $str1, $str2) = @_;
211 return _ci_cardnumber_cmp($str1, $str2);
215 my ( $self, $item_id, $trans_date, $return_date, $current_loc, $item_props, $cancel, $account ) = @_;
217 my $checked_in_ok = $account->{checked_in_ok};
218 my $cv_triggers_alert = $account->{cv_triggers_alert};
219 my $holds_block_checkin = $account->{holds_block_checkin};
221 my ( $patron, $item, $circ );
223 $circ = C4::SIP::ILS::Transaction::Checkin->new();
226 $circ->item( $item = C4::SIP::ILS::Item->new($item_id) );
230 $data = $circ->do_checkin( $current_loc, $return_date, $account );
234 $circ->alert_type(99);
236 $circ->screen_msg('Invalid Item');
240 if ( !$circ->ok && $circ->alert_type && $circ->alert_type == 98 ) { # data corruption
241 $circ->screen_msg("Checkin failed: data problem");
242 siplog( "LOG_WARNING", "Problem with issue_id in issues and old_issues; check the about page" );
243 } elsif ( $data->{messages}->{ResFound} && !$circ->ok && $holds_block_checkin ) {
244 $circ->screen_msg("Item is on hold, please return to circulation desk");
245 siplog ("LOG_DEBUG", "C4::SIP::ILS::Checkin - item on hold");
246 } elsif ( $data->{messages}->{withdrawn} && !$circ->ok && C4::Context->preference("BlockReturnOfWithdrawnItems") ) {
247 $circ->screen_msg("Item withdrawn, return not allowed");
248 siplog ("LOG_DEBUG", "C4::SIP::ILS::Checkin - item withdrawn");
249 } elsif ( $data->{messages}->{WasLost} && !$circ->ok && C4::Context->preference("BlockReturnOfLostItems") ) {
250 $circ->screen_msg("Item lost, return not allowed");
251 siplog("LOG_DEBUG", "C4::SIP::ILS::Checkin - item lost");
252 } elsif ( !$item->{borrowernumber} ) {
253 if ( $checked_in_ok ) { # Mark checkin ok although book not checked out
255 siplog("LOG_DEBUG", "C4::SIP::ILS::Checkin - using checked_in_ok");
257 $circ->screen_msg("Item not checked out");
258 siplog("LOG_DEBUG", "C4::SIP::ILS::Checkin - item not checked out");
260 } elsif ( $circ->ok ) {
261 $circ->patron( $patron = C4::SIP::ILS::Patron->new( { borrowernumber => $item->{borrowernumber} } ) );
262 delete $item->{borrowernumber};
263 delete $item->{due_date};
264 $patron->{items} = [ grep { $_ ne $item_id } @{ $patron->{items} } ];
267 if ( $account->{show_checkin_message} ) {
268 my $permanent_location;
269 if ( C4::Context->preference("UseLocationAsAQInSIP") ) {
270 $permanent_location = $item->{'permanent_location'};
272 $permanent_location = Koha::Libraries->find( $item->{permanent_location} )->branchname;
274 $message .= "Item checked-in: $permanent_location - $item->{location}.";
277 # Check for overdue fines to display
278 if ($account->{show_outstanding_amount}) {
279 my $kohaitem = Koha::Items->find( { barcode => $item_id } );
281 my $charges = Koha::Account::Lines->search(
283 borrowernumber => $patron->{borrowernumber},
284 amountoutstanding => { '>' => 0 },
285 debit_type_code => [ 'OVERDUE' ],
286 itemnumber => $kohaitem->itemnumber
290 $message .= "You owe " . Koha::Number::Price->new( $charges->total_outstanding )->format({ with_symbol => 1}) . " for this item.";
295 $circ->screen_msg($message);
298 # Checkin failed: Wrongbranch or withdrawn?
299 # Bug 10748 with pref BlockReturnOfLostItems adds another case to come
300 # here: returning a lost item when the pref is set.
301 $circ->screen_msg("Checkin failed");
302 siplog( "LOG_WARNING", "Checkin failed: probably for Wrongbranch or withdrawn" );
308 # If the ILS caches patron information, this lets it free
310 sub end_patron_session {
311 my ($self, $patron_id) = @_;
313 # success?, screen_msg, print_line
314 return (1, 'Thank you !', '');
318 my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type, $pay_type, $fee_id, $trans_id, $currency, $is_writeoff, $disallow_overpayment, $register_id) = @_;
320 my $trans = C4::SIP::ILS::Transaction::FeePayment->new();
322 $trans->transaction_id($trans_id);
324 $trans->patron($patron = C4::SIP::ILS::Patron->new($patron_id));
326 $trans->screen_msg('Invalid patron barcode.');
329 my $trans_result = $trans->pay( $patron->{borrowernumber}, $fee_amt, $pay_type, $fee_id, $is_writeoff, $disallow_overpayment, $register_id );
330 my $ok = $trans_result->{ok};
335 pay_response => $trans_result->{pay_response},
336 error => $trans_result->{error},
341 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
342 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
345 my $trans = C4::SIP::ILS::Transaction::Hold->new();
347 $patron = C4::SIP::ILS::Patron->new( $patron_id );
349 $trans->screen_msg("Invalid patron barcode.");
353 unless ($item = C4::SIP::ILS::Item->new($item_id || $title_id)) {
354 $trans->screen_msg("No such item.");
358 if ( $patron->holds_blocked_by_excessive_fees() ) {
359 $trans->screen_msg("Excessive fees blocking placement of hold.");
362 if ($item->fee and $fee_ack ne 'Y') {
363 $trans->screen_msg = "Fee required to place hold.";
368 item_id => $item->id,
369 patron_id => $patron->id,
370 expiration_date => $expiry_date,
371 pickup_location => $pickup_location,
372 hold_type => $hold_type,
376 $trans->patron($patron);
378 $trans->pickup_location($pickup_location);
381 push(@{$item->hold_queue}, $hold);
382 push(@{$patron->{hold_items}}, $hold);
388 my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
389 my ($patron, $item, $hold);
391 my $trans = C4::SIP::ILS::Transaction::Hold->new();
393 $patron = C4::SIP::ILS::Patron->new( $patron_id );
395 $trans->screen_msg("Invalid patron barcode.");
399 unless ($item = C4::SIP::ILS::Item->new($item_id || $title_id)) {
400 $trans->screen_msg("No such item.");
404 $trans->patron($patron);
407 unless ($trans->ok) {
408 $trans->screen_msg("Error with transaction drop_hold: " . $trans->screen_msg);
411 # Remove the hold from the patron's record first
412 $patron->drop_hold($item_id); # different than the transaction drop!
414 # Now, remove it from the item record. If it was on the patron
415 # record but not on the item record, we'll treat that as success.
416 foreach my $i (0 .. scalar @{$item->hold_queue}) {
417 $hold = $item->hold_queue->[$i];
418 if ($item->barcode_is_borrowernumber($patron->id, $hold->{borrowernumber})) {
419 # found it: delete it.
420 splice @{$item->hold_queue}, $i, 1;
421 last; # ?? should we keep going, in case there are multiples
425 $trans->screen_msg("Hold Cancelled.");
431 # The patron and item id's can't be altered, but the
432 # date, location, and type can.
434 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
435 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
440 $trans = C4::SIP::ILS::Transaction::Hold->new();
443 $patron = C4::SIP::ILS::Patron->new( $patron_id );
445 $trans->screen_msg("Invalid patron barcode: '$patron_id'.");
449 foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
450 $hold = $patron->{hold_items}[$i];
452 if ($hold->{item_id} eq $item_id) {
453 # Found it. So fix it.
454 $hold->{expiration_date} = $expiry_date if $expiry_date;
455 $hold->{pickup_location} = $pickup_location if $pickup_location;
456 $hold->{hold_type} = $hold_type if $hold_type;
457 $trans->change_hold();
459 $trans->screen_msg("Hold updated.");
460 $trans->patron($patron);
461 $trans->item(C4::SIP::ILS::Item->new( $hold->{item_id}));
466 # The same hold structure is linked into both the patron's
467 # list of hold items and into the queue of outstanding holds
468 # for the item, so we don't need to search the hold queue for
469 # the item, since it's already been updated by the patron code.
472 $trans->screen_msg("No such outstanding hold.");
479 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
480 $no_block, $nb_due_date, $third_party,
481 $item_props, $fee_ack) = @_;
485 $trans = C4::SIP::ILS::Transaction::Renew->new();
486 $trans->patron($patron = C4::SIP::ILS::Patron->new( $patron_id ));
488 $trans->screen_msg("Invalid patron barcode.");
490 } elsif (!$patron->renew_ok) {
491 $trans->screen_msg("Renewals not allowed.");
495 # Previously: renewing a title, rather than an item (sort of)
496 # This is gross, but in a real ILS it would be better
498 # if (defined($title_id)) {
499 # foreach my $i (@{$patron->{items}}) {
500 # $item = new ILS::Item $i;
501 # last if ($title_id eq $item->title_id);
506 my $count = scalar @{$patron->{items}};
507 foreach my $i (@{$patron->{items}}) {
508 unless (defined $i->{barcode}) { # FIXME: using data instead of objects may violate the abstraction layer
509 siplog("LOG_ERR", "No barcode for item %s of %s: $item_id", $j+1, $count);
512 siplog("LOG_DEBUG", "checking item %s of %s: $item_id vs. %s", ++$j, $count, $i->{barcode});
513 if ($i->{barcode} eq $item_id) {
514 # We have it checked out
515 $item = C4::SIP::ILS::Item->new( $item_id );
523 $trans->fee_ack($fee_ack);
526 if (!defined($item)) {
527 $trans->screen_msg("Item not checked out to " . $patron->name); # not checked out to $patron_id
531 if ($trans->renewal_ok()) {
532 $item->{due_date} = $trans->{due};
533 $trans->desensitize(0);
541 my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
542 my ($patron, $item_id);
545 $trans = C4::SIP::ILS::Transaction::RenewAll->new();
547 $trans->fee_ack($fee_ack);
550 $trans->patron($patron = C4::SIP::ILS::Patron->new( $patron_id ));
551 if (defined $patron) {
552 siplog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s", $patron->name, $patron->renew_ok);
554 siplog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'", $patron_id);
558 $trans->screen_msg("Invalid patron barcode.");
560 } elsif (!$patron->renew_ok) {
561 $trans->screen_msg("Renewals not allowed.");
565 $trans->do_renew_all;