2 # ILS.pm: Koha ILS interface module
9 use Sys
::Syslog
qw(syslog);
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
;
25 'magnetic media' => 1,
26 'security inhibit' => 0,
27 'offline operation' => 0,
28 "patron status request" => 1,
34 "patron information" => 1,
35 "end patron session" => 1,
37 "item information" => 1,
38 "item status update" => 0,
46 my ($class, $institution) = @_;
47 my $type = ref($class) || $class;
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;
57 $debug and warn "ILS: finding patron";
58 return C4
::SIP
::ILS
::Patron
->new(@_);
63 $debug and warn "ILS: finding item";
64 return C4
::SIP
::ILS
::Item
->new(@_);
69 return $self->{institution
}->{id
}; # consider making this return the whole institution
74 return $self->{institution
}->{id
};
79 return (exists($supports{$op}) && $supports{$op});
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
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
101 return (exists($self->{institution
}->{policy
}->{checkout
})
102 && to_bool
($self->{institution
}->{policy
}->{checkout
}));
106 return (exists($self->{institution
}->{policy
}->{checkin
})
107 && to_bool
($self->{institution
}->{policy
}->{checkin
}));
109 sub status_update_ok
{
111 return (exists($self->{institution
}->{policy
}->{status_update
})
112 && to_bool
($self->{institution
}->{policy
}->{status_update
}));
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
129 my ( $self, $patron_id, $item_id, $sc_renew, $fee_ack ) = @_;
130 my ( $patron, $item, $circ );
132 $circ = C4
::SIP
::ILS
::Transaction
::Checkout
->new();
135 $circ->patron( $patron = C4
::SIP
::ILS
::Patron
->new($patron_id) );
136 $circ->item( $item = C4
::SIP
::ILS
::Item
->new($item_id) );
138 $circ->fee_ack($fee_ack);
142 $circ->screen_msg("Invalid Patron");
144 elsif ( !$patron->charge_ok ) {
145 $circ->screen_msg("Patron Blocked");
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");
156 $circ->do_checkout();
158 $debug and warn "circ is ok";
160 # If the item is already associated with this patron, then
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 );
171 "LOG_DEBUG", "ILS::Checkout: patron %s has checked out %s",
172 $patron_id, join( ', ', @
{ $patron->{items
} } )
176 syslog
( "LOG_ERR", "ILS::Checkout Issue failed" );
185 sub _ci_cardnumber_cmp
{
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);
200 my ( $self, $item_id, $trans_date, $return_date, $current_loc, $item_props, $cancel, $checked_in_ok, $cv_triggers_alert ) = @_;
201 my ( $patron, $item, $circ );
203 $circ = C4
::SIP
::ILS
::Transaction
::Checkin
->new();
206 $circ->item( $item = C4
::SIP
::ILS
::Item
->new($item_id) );
210 $data = $circ->do_checkin( $current_loc, $return_date, $cv_triggers_alert, $checked_in_ok );
214 $circ->alert_type(99);
216 $circ->screen_msg('Invalid Item');
220 if ( !$circ->ok && $circ->alert_type && $circ->alert_type == 98 ) { # data corruption
221 $circ->screen_msg("Checkin failed: data problem");
222 syslog
( "LOG_WARNING", "Problem with issue_id in issues and old_issues; check the about page" );
223 } elsif ( $data->{messages
}->{withdrawn
} && !$circ->ok ) {
224 $circ->screen_msg("Item withdrawn, return not allowed");
225 syslog
("LOG_DEBUG", "C4::SIP::ILS::Checkin - item withdrawn");
226 } elsif ( $data->{messages
}->{WasLost
} && !$circ->ok ) {
227 $circ->screen_msg("Item lost, return not allowed");
228 syslog
("LOG_DEBUG", "C4::SIP::ILS::Checkin - item lost");
229 } elsif ( !$item->{patron
} ) {
230 if ( $checked_in_ok ) { # Mark checkin ok although book not checked out
232 syslog
("LOG_DEBUG", "C4::SIP::ILS::Checkin - using checked_in_ok");
234 $circ->screen_msg("Item not checked out");
235 syslog
("LOG_DEBUG", "C4::SIP::ILS::Checkin - item not checked out");
237 } elsif ( $circ->ok ) {
238 $circ->patron( $patron = C4
::SIP
::ILS
::Patron
->new( $item->{patron
} ) );
239 delete $item->{patron
};
240 delete $item->{due_date
};
241 $patron->{items
} = [ grep { $_ ne $item_id } @
{ $patron->{items
} } ];
243 # Checkin failed: Wrongbranch or withdrawn?
244 # Bug 10748 with pref BlockReturnOfLostItems adds another case to come
245 # here: returning a lost item when the pref is set.
246 $circ->screen_msg("Checkin failed");
247 syslog
( "LOG_WARNING", "Checkin failed: probably for Wrongbranch or withdrawn" );
253 # If the ILS caches patron information, this lets it free
255 sub end_patron_session
{
256 my ($self, $patron_id) = @_;
258 # success?, screen_msg, print_line
259 return (1, 'Thank you !', '');
263 my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type, $pay_type, $fee_id, $trans_id, $currency, $is_writeoff, $disallow_overpayment ) = @_;
265 my $trans = C4
::SIP
::ILS
::Transaction
::FeePayment
->new();
267 $trans->transaction_id($trans_id);
269 $trans->patron($patron = C4
::SIP
::ILS
::Patron
->new($patron_id));
271 $trans->screen_msg('Invalid patron barcode.');
274 my $ok = $trans->pay( $patron->{borrowernumber
}, $fee_amt, $pay_type, $fee_id, $is_writeoff, $disallow_overpayment );
281 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
282 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
285 my $trans = C4
::SIP
::ILS
::Transaction
::Hold
->new();
287 $patron = C4
::SIP
::ILS
::Patron
->new( $patron_id);
289 || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
290 $trans->screen_msg("Invalid Patron.");
294 unless ($item = C4
::SIP
::ILS
::Item
->new($item_id || $title_id)) {
295 $trans->screen_msg("No such item.");
299 if ( $patron->holds_blocked_by_excessive_fees() ) {
300 $trans->screen_msg("Excessive fees blocking placement of hold.");
303 if ($item->fee and $fee_ack ne 'Y') {
304 $trans->screen_msg = "Fee required to place hold.";
309 item_id
=> $item->id,
310 patron_id
=> $patron->id,
311 expiration_date
=> $expiry_date,
312 pickup_location
=> $pickup_location,
313 hold_type
=> $hold_type,
317 $trans->patron($patron);
319 $trans->pickup_location($pickup_location);
322 push(@
{$item->hold_queue}, $hold);
323 push(@
{$patron->{hold_items
}}, $hold);
329 my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
330 my ($patron, $item, $hold);
332 my $trans = C4
::SIP
::ILS
::Transaction
::Hold
->new();
334 $patron = C4
::SIP
::ILS
::Patron
->new( $patron_id );
336 $trans->screen_msg("Invalid patron barcode.");
338 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
339 $trans->screen_msg('Invalid patron password.');
343 unless ($item = C4
::SIP
::ILS
::Item
->new($item_id || $title_id)) {
344 $trans->screen_msg("No such item.");
348 $trans->patron($patron);
351 unless ($trans->ok) {
352 $trans->screen_msg("Error with transaction drop_hold: " . $trans->screen_msg);
355 # Remove the hold from the patron's record first
356 $trans->ok($patron->drop_hold($item_id)); # different than the transaction drop!
358 unless ($trans->ok) {
359 # We didn't find it on the patron record
360 $trans->screen_msg("No such hold on patron record.");
364 # Now, remove it from the item record. If it was on the patron
365 # record but not on the item record, we'll treat that as success.
366 foreach my $i (0 .. scalar @
{$item->hold_queue}) {
367 $hold = $item->hold_queue->[$i];
368 if ($item->barcode_is_borrowernumber($patron->id, $hold->{borrowernumber
})) {
369 # found it: delete it.
370 splice @
{$item->hold_queue}, $i, 1;
371 last; # ?? should we keep going, in case there are multiples
375 $trans->screen_msg("Hold Cancelled.");
381 # The patron and item id's can't be altered, but the
382 # date, location, and type can.
384 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
385 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
390 $trans = C4
::SIP
::ILS
::Transaction
::Hold
->new();
393 $patron = C4
::SIP
::ILS
::Patron
->new( $patron_id );
395 $trans->screen_msg("Invalid patron barcode: '$patron_id'.");
399 foreach my $i (0 .. scalar @
{$patron->{hold_items
}}) {
400 $hold = $patron->{hold_items
}[$i];
402 if ($hold->{item_id
} eq $item_id) {
403 # Found it. So fix it.
404 $hold->{expiration_date
} = $expiry_date if $expiry_date;
405 $hold->{pickup_location
} = $pickup_location if $pickup_location;
406 $hold->{hold_type
} = $hold_type if $hold_type;
407 $trans->change_hold();
409 $trans->screen_msg("Hold updated.");
410 $trans->patron($patron);
411 $trans->item(C4
::SIP
::ILS
::Item
->new( $hold->{item_id
}));
416 # The same hold structure is linked into both the patron's
417 # list of hold items and into the queue of outstanding holds
418 # for the item, so we don't need to search the hold queue for
419 # the item, since it's already been updated by the patron code.
422 $trans->screen_msg("No such outstanding hold.");
429 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
430 $no_block, $nb_due_date, $third_party,
431 $item_props, $fee_ack) = @_;
435 $trans = C4
::SIP
::ILS
::Transaction
::Renew
->new();
436 $trans->patron($patron = C4
::SIP
::ILS
::Patron
->new( $patron_id ));
439 $trans->screen_msg("Invalid patron barcode.");
441 } elsif (!$patron->renew_ok) {
442 $trans->screen_msg("Renewals not allowed.");
446 # Previously: renewing a title, rather than an item (sort of)
447 # This is gross, but in a real ILS it would be better
449 # if (defined($title_id)) {
450 # foreach my $i (@{$patron->{items}}) {
451 # $item = new ILS::Item $i;
452 # last if ($title_id eq $item->title_id);
457 my $count = scalar @
{$patron->{items
}};
458 foreach my $i (@
{$patron->{items
}}) {
459 unless (defined $i->{barcode
}) { # FIXME: using data instead of objects may violate the abstraction layer
460 syslog
("LOG_ERR", "No barcode for item %s of %s: $item_id", $j+1, $count);
463 syslog
("LOG_DEBUG", "checking item %s of %s: $item_id vs. %s", ++$j, $count, $i->{barcode
});
464 if ($i->{barcode
} eq $item_id) {
465 # We have it checked out
466 $item = C4
::SIP
::ILS
::Item
->new( $item_id );
474 if (!defined($item)) {
475 $trans->screen_msg("Item not checked out to " . $patron->name); # not checked out to $patron_id
479 if ($trans->renewal_ok()) {
480 $item->{due_date
} = $trans->{due
};
481 $trans->desensitize(0);
489 my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
490 my ($patron, $item_id);
493 $trans = C4
::SIP
::ILS
::Transaction
::RenewAll
->new();
495 $trans->patron($patron = C4
::SIP
::ILS
::Patron
->new( $patron_id ));
496 if (defined $patron) {
497 syslog
("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s", $patron->name, $patron->renew_ok);
499 syslog
("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'", $patron_id);
502 if (!defined($patron)) {
503 $trans->screen_msg("Invalid patron barcode.");
505 } elsif (!$patron->renew_ok) {
506 $trans->screen_msg("Renewals not allowed.");
508 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
509 $trans->screen_msg("Invalid patron password.");
513 $trans->do_renew_all;