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 );
126 $circ = C4::SIP::ILS::Transaction::Checkout->new();
128 $circ->patron( $patron = C4::SIP::ILS::Patron->new($patron_id) );
129 $circ->item( $item = C4::SIP::ILS::Item->new($item_id) );
131 $circ->fee_ack($fee_ack);
134 $circ->screen_msg("Invalid Patron");
136 elsif ( !$patron->charge_ok ) {
137 if ($patron->debarred) {
138 $circ->screen_msg("Patron debarred");
139 } elsif ($patron->expired) {
140 $circ->screen_msg("Patron expired");
141 } elsif ($patron->fine_blocked) {
142 $circ->screen_msg("Patron has fines");
144 $circ->screen_msg("Patron blocked");
148 $circ->screen_msg("Invalid Item");
150 elsif ( $item->{borrowernumber}
151 && !_ci_cardnumber_cmp( $item->{borrowernumber}, $patron->borrowernumber ) )
153 $circ->screen_msg("Item checked out to another patron");
156 $circ->do_checkout($account);
159 # If the item is already associated with this patron, then
161 $circ->renew_ok( $item->{borrowernumber}
162 && _ci_cardnumber_cmp( $item->{borrowernumber}, $patron->borrowernumber ) );
164 $item->{borrowernumber} = $patron_id;
165 $item->{due_date} = $circ->{due};
166 push( @{ $patron->{items} }, { barcode => $item_id } );
167 $circ->desensitize( !$item->magnetic_media );
170 "LOG_DEBUG", "ILS::Checkout: patron %s has checked out %s",
171 $patron_id, join( ', ', map{ $_->{barcode} } @{ $patron->{items} } )
175 siplog( "LOG_ERR", "ILS::Checkout Issue failed" );
184 sub _ci_cardnumber_cmp {
186 # As the database is case insensitive we need to normalize two strings
187 # before comparing them
188 return ( uc($s1) eq uc($s2) );
191 # wrapper which allows above to be called for testing
193 sub test_cardnumber_compare {
194 my ($self, $str1, $str2) = @_;
195 return _ci_cardnumber_cmp($str1, $str2);
199 my ( $self, $item_id, $trans_date, $return_date, $current_loc, $item_props, $cancel, $account ) = @_;
201 my $checked_in_ok = $account->{checked_in_ok};
202 my $cv_triggers_alert = $account->{cv_triggers_alert};
203 my $holds_block_checkin = $account->{holds_block_checkin};
205 my ( $patron, $item, $circ );
207 $circ = C4::SIP::ILS::Transaction::Checkin->new();
210 $circ->item( $item = C4::SIP::ILS::Item->new($item_id) );
214 $data = $circ->do_checkin( $current_loc, $return_date, $account );
218 $circ->alert_type(99);
220 $circ->screen_msg('Invalid Item');
224 if ( !$circ->ok && $circ->alert_type && $circ->alert_type == 98 ) { # data corruption
225 $circ->screen_msg("Checkin failed: data problem");
226 siplog( "LOG_WARNING", "Problem with issue_id in issues and old_issues; check the about page" );
227 } elsif ( $data->{messages}->{ResFound} && !$circ->ok && $holds_block_checkin ) {
228 $circ->screen_msg("Item is on hold, please return to circulation desk");
229 siplog ("LOG_DEBUG", "C4::SIP::ILS::Checkin - item on hold");
230 } elsif ( $data->{messages}->{withdrawn} && !$circ->ok && C4::Context->preference("BlockReturnOfWithdrawnItems") ) {
231 $circ->screen_msg("Item withdrawn, return not allowed");
232 siplog ("LOG_DEBUG", "C4::SIP::ILS::Checkin - item withdrawn");
233 } elsif ( $data->{messages}->{WasLost} && !$circ->ok && C4::Context->preference("BlockReturnOfLostItems") ) {
234 $circ->screen_msg("Item lost, return not allowed");
235 siplog("LOG_DEBUG", "C4::SIP::ILS::Checkin - item lost");
236 } elsif ( !$item->{borrowernumber} ) {
237 if ( $checked_in_ok ) { # Mark checkin ok although book not checked out
239 siplog("LOG_DEBUG", "C4::SIP::ILS::Checkin - using checked_in_ok");
241 $circ->screen_msg("Item not checked out");
242 siplog("LOG_DEBUG", "C4::SIP::ILS::Checkin - item not checked out");
244 } elsif ( $circ->ok ) {
245 $circ->patron( $patron = C4::SIP::ILS::Patron->new( { borrowernumber => $item->{borrowernumber} } ) );
246 delete $item->{borrowernumber};
247 delete $item->{due_date};
248 $patron->{items} = [ grep { $_ ne $item_id } @{ $patron->{items} } ];
250 # Checkin failed: Wrongbranch or withdrawn?
251 # Bug 10748 with pref BlockReturnOfLostItems adds another case to come
252 # here: returning a lost item when the pref is set.
253 $circ->screen_msg("Checkin failed");
254 siplog( "LOG_WARNING", "Checkin failed: probably for Wrongbranch or withdrawn" );
260 # If the ILS caches patron information, this lets it free
262 sub end_patron_session {
263 my ($self, $patron_id) = @_;
265 # success?, screen_msg, print_line
266 return (1, 'Thank you !', '');
270 my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type, $pay_type, $fee_id, $trans_id, $currency, $is_writeoff, $disallow_overpayment, $register_id) = @_;
272 my $trans = C4::SIP::ILS::Transaction::FeePayment->new();
274 $trans->transaction_id($trans_id);
276 $trans->patron($patron = C4::SIP::ILS::Patron->new($patron_id));
278 $trans->screen_msg('Invalid patron barcode.');
281 my $trans_result = $trans->pay( $patron->{borrowernumber}, $fee_amt, $pay_type, $fee_id, $is_writeoff, $disallow_overpayment, $register_id );
282 my $ok = $trans_result->{ok};
287 pay_response => $trans_result->{pay_response}
292 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
293 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
296 my $trans = C4::SIP::ILS::Transaction::Hold->new();
298 $patron = C4::SIP::ILS::Patron->new( $patron_id );
300 $trans->screen_msg("Invalid patron barcode.");
304 unless ($item = C4::SIP::ILS::Item->new($item_id || $title_id)) {
305 $trans->screen_msg("No such item.");
309 if ( $patron->holds_blocked_by_excessive_fees() ) {
310 $trans->screen_msg("Excessive fees blocking placement of hold.");
313 if ($item->fee and $fee_ack ne 'Y') {
314 $trans->screen_msg = "Fee required to place hold.";
319 item_id => $item->id,
320 patron_id => $patron->id,
321 expiration_date => $expiry_date,
322 pickup_location => $pickup_location,
323 hold_type => $hold_type,
327 $trans->patron($patron);
329 $trans->pickup_location($pickup_location);
332 push(@{$item->hold_queue}, $hold);
333 push(@{$patron->{hold_items}}, $hold);
339 my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
340 my ($patron, $item, $hold);
342 my $trans = C4::SIP::ILS::Transaction::Hold->new();
344 $patron = C4::SIP::ILS::Patron->new( $patron_id );
346 $trans->screen_msg("Invalid patron barcode.");
350 unless ($item = C4::SIP::ILS::Item->new($item_id || $title_id)) {
351 $trans->screen_msg("No such item.");
355 $trans->patron($patron);
358 unless ($trans->ok) {
359 $trans->screen_msg("Error with transaction drop_hold: " . $trans->screen_msg);
362 # Remove the hold from the patron's record first
363 $patron->drop_hold($item_id); # different than the transaction drop!
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
376 $trans->screen_msg("Hold Cancelled.");
382 # The patron and item id's can't be altered, but the
383 # date, location, and type can.
385 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
386 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
391 $trans = C4::SIP::ILS::Transaction::Hold->new();
394 $patron = C4::SIP::ILS::Patron->new( $patron_id );
396 $trans->screen_msg("Invalid patron barcode: '$patron_id'.");
400 foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
401 $hold = $patron->{hold_items}[$i];
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();
410 $trans->screen_msg("Hold updated.");
411 $trans->patron($patron);
412 $trans->item(C4::SIP::ILS::Item->new( $hold->{item_id}));
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.
423 $trans->screen_msg("No such outstanding hold.");
430 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
431 $no_block, $nb_due_date, $third_party,
432 $item_props, $fee_ack) = @_;
436 $trans = C4::SIP::ILS::Transaction::Renew->new();
437 $trans->patron($patron = C4::SIP::ILS::Patron->new( $patron_id ));
440 $trans->screen_msg("Invalid patron barcode.");
442 } elsif (!$patron->renew_ok) {
443 $trans->screen_msg("Renewals not allowed.");
447 # Previously: renewing a title, rather than an item (sort of)
448 # This is gross, but in a real ILS it would be better
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);
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 siplog("LOG_ERR", "No barcode for item %s of %s: $item_id", $j+1, $count);
464 siplog("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 );
475 if (!defined($item)) {
476 $trans->screen_msg("Item not checked out to " . $patron->name); # not checked out to $patron_id
480 if ($trans->renewal_ok()) {
481 $item->{due_date} = $trans->{due};
482 $trans->desensitize(0);
490 my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
491 my ($patron, $item_id);
494 $trans = C4::SIP::ILS::Transaction::RenewAll->new();
496 $trans->patron($patron = C4::SIP::ILS::Patron->new( $patron_id ));
497 if (defined $patron) {
498 siplog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s", $patron->name, $patron->renew_ok);
500 siplog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'", $patron_id);
504 $trans->screen_msg("Invalid patron barcode.");
506 } elsif (!$patron->renew_ok) {
507 $trans->screen_msg("Renewals not allowed.");
511 $trans->do_renew_all;