2 # ILS.pm: Koha ILS interface module
9 use Sys::Syslog qw(syslog);
14 use ILS::Transaction::Checkout;
15 use ILS::Transaction::Checkin;
16 use ILS::Transaction::FeePayment;
17 use ILS::Transaction::Hold;
18 use ILS::Transaction::Renew;
19 use 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;
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 ILS::Patron->new(@_);
63 $debug and warn "ILS: finding item";
64 return 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) = @_;
130 my ($patron, $item, $circ);
132 $circ = new ILS::Transaction::Checkout;
134 $circ->patron($patron = new ILS::Patron $patron_id);
135 $circ->item($item = new ILS::Item $item_id);
138 $circ->screen_msg("Invalid Patron");
139 } elsif (!$patron->charge_ok) {
140 $circ->screen_msg("Patron Blocked");
142 $circ->screen_msg("Invalid Item");
143 # holds checked inside do_checkout
144 # } elsif ($item->hold_queue && @{$item->hold_queue} && ! $item->barcode_is_borrowernumber($patron_id, $item->hold_queue->[0]->{borrowernumber})) {
145 # $circ->screen_msg("Item on Hold for Another User");
146 } elsif ($item->{patron} && ($item->{patron} ne $patron_id)) {
147 # I can't deal with this right now
148 $circ->screen_msg("Item checked out to another patron");
150 $circ->do_checkout();
152 $debug and warn "circ is ok";
153 # If the item is already associated with this patron, then
155 $circ->renew_ok($item->{patron} && ($item->{patron} eq $patron_id));
157 $item->{patron} = $patron_id;
158 $item->{due_date} = $circ->{due};
159 push(@{$patron->{items}}, $item_id);
160 $circ->desensitize(!$item->magnetic_media);
162 syslog("LOG_DEBUG", "ILS::Checkout: patron %s has checked out %s",
163 $patron_id, join(', ', @{$patron->{items}}));
166 syslog("LOG_ERR", "ILS::Checkout Issue failed");
175 my ($self, $item_id, $trans_date, $return_date,
176 $current_loc, $item_props, $cancel) = @_;
177 my ($patron, $item, $circ);
179 $circ = new ILS::Transaction::Checkin;
181 $circ->item($item = new ILS::Item $item_id);
184 $circ->do_checkin($current_loc, $return_date);
187 $circ->alert_type(99);
188 $circ->screen_msg('Invalid Item');
190 # It's ok to check it in if it exists, and if it was checked out
191 $circ->ok($item && $item->{patron});
194 $circ->patron($patron = new ILS::Patron $item->{patron});
195 delete $item->{patron};
196 delete $item->{due_date};
197 $patron->{items} = [ grep {$_ ne $item_id} @{$patron->{items}} ];
204 # If the ILS caches patron information, this lets it free
206 sub end_patron_session {
207 my ($self, $patron_id) = @_;
209 # success?, screen_msg, print_line
210 return (1, 'Thank you !', '');
214 my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
215 $pay_type, $fee_id, $trans_id, $currency) = @_;
219 # $trans = new ILS::Transaction::FeePayment;
221 $patron = new ILS::Patron $patron_id;
223 $trans->transaction_id($trans_id);
224 $trans->patron($patron);
231 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
232 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
235 my $trans = new ILS::Transaction::Hold;
237 $patron = new ILS::Patron $patron_id;
239 || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
240 $trans->screen_msg("Invalid Patron.");
244 unless ($item = new ILS::Item ($item_id || $title_id)) {
245 $trans->screen_msg("No such item.");
249 if ($item->fee and $fee_ack ne 'Y') {
250 $trans->screen_msg = "Fee required to place hold.";
255 item_id => $item->id,
256 patron_id => $patron->id,
257 expiration_date => $expiry_date,
258 pickup_location => $pickup_location,
259 hold_type => $hold_type,
263 $trans->patron($patron);
265 $trans->pickup_location($pickup_location);
268 push(@{$item->hold_queue}, $hold);
269 push(@{$patron->{hold_items}}, $hold);
275 my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
276 my ($patron, $item, $hold);
278 my $trans = new ILS::Transaction::Hold;
280 $patron = new ILS::Patron $patron_id;
282 $trans->screen_msg("Invalid patron barcode.");
284 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
285 $trans->screen_msg('Invalid patron password.');
289 unless ($item = new ILS::Item ($item_id || $title_id)) {
290 $trans->screen_msg("No such item.");
294 $trans->patron($patron);
297 unless ($trans->ok) {
298 $trans->screen_msg("Error with transaction drop_hold: " . $trans->screen_msg);
301 # Remove the hold from the patron's record first
302 $trans->ok($patron->drop_hold($item_id)); # different than the transaction drop!
304 unless ($trans->ok) {
305 # We didn't find it on the patron record
306 $trans->screen_msg("No such hold on patron record.");
310 # Now, remove it from the item record. If it was on the patron
311 # record but not on the item record, we'll treat that as success.
312 foreach my $i (0 .. scalar @{$item->hold_queue}) {
313 $hold = $item->hold_queue->[$i];
314 if ($item->barcode_is_borrowernumber($patron->id, $hold->{borrowernumber})) {
315 # found it: delete it.
316 splice @{$item->hold_queue}, $i, 1;
317 last; # ?? should we keep going, in case there are multiples
321 $trans->screen_msg("Hold Cancelled.");
327 # The patron and item id's can't be altered, but the
328 # date, location, and type can.
330 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
331 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
336 $trans = new ILS::Transaction::Hold;
339 $patron = new ILS::Patron $patron_id;
341 $trans->screen_msg("Invalid patron barcode: '$patron_id'.");
345 foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
346 $hold = $patron->{hold_items}[$i];
348 if ($hold->{item_id} eq $item_id) {
349 # Found it. So fix it.
350 $hold->{expiration_date} = $expiry_date if $expiry_date;
351 $hold->{pickup_location} = $pickup_location if $pickup_location;
352 $hold->{hold_type} = $hold_type if $hold_type;
353 $trans->change_hold();
355 $trans->screen_msg("Hold updated.");
356 $trans->patron($patron);
357 $trans->item(new ILS::Item $hold->{item_id});
362 # The same hold structure is linked into both the patron's
363 # list of hold items and into the queue of outstanding holds
364 # for the item, so we don't need to search the hold queue for
365 # the item, since it's already been updated by the patron code.
368 $trans->screen_msg("No such outstanding hold.");
375 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
376 $no_block, $nb_due_date, $third_party,
377 $item_props, $fee_ack) = @_;
381 $trans = new ILS::Transaction::Renew;
382 $trans->patron($patron = new ILS::Patron $patron_id);
385 $trans->screen_msg("Invalid patron barcode.");
387 } elsif (!$patron->renew_ok) {
388 $trans->screen_msg("Renewals not allowed.");
392 # Previously: renewing a title, rather than an item (sort of)
393 # This is gross, but in a real ILS it would be better
395 # if (defined($title_id)) {
396 # foreach my $i (@{$patron->{items}}) {
397 # $item = new ILS::Item $i;
398 # last if ($title_id eq $item->title_id);
403 my $count = scalar @{$patron->{items}};
404 foreach my $i (@{$patron->{items}}) {
405 unless (defined $i->{barcode}) { # FIXME: using data instead of objects may violate the abstraction layer
406 syslog("LOG_ERR", "No barcode for item %s of %s: $item_id", $j+1, $count);
409 syslog("LOG_DEBUG", "checking item %s of %s: $item_id vs. %s", ++$j, $count, $i->{barcode});
410 if ($i->{barcode} eq $item_id) {
411 # We have it checked out
412 $item = new ILS::Item $item_id;
420 if (!defined($item)) {
421 $trans->screen_msg("Item not checked out to " . $patron->name); # not checked out to $patron_id
423 } elsif (!$item->available($patron_id)) {
424 $trans->screen_msg("Item unavailable due to outstanding holds");
427 $trans->renewal_ok(1);
428 $trans->desensitize(0); # It's already checked out
430 syslog("LOG_DEBUG", "done renew (ok:%s): %s renews %s", $trans->renewal_ok, $patron_id, $item_id);
432 # $item->{due_date} = $nb_due_date if $no_block eq 'Y';
433 # $item->{sip_item_properties} = $item_props if $item_props;
440 my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
441 my ($patron, $item_id);
444 $trans = new ILS::Transaction::RenewAll;
446 $trans->patron($patron = new ILS::Patron $patron_id);
447 if (defined $patron) {
448 syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s", $patron->name, $patron->renew_ok);
450 syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'", $patron_id);
453 if (!defined($patron)) {
454 $trans->screen_msg("Invalid patron barcode.");
456 } elsif (!$patron->renew_ok) {
457 $trans->screen_msg("Renewals not allowed.");
459 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
460 $trans->screen_msg("Invalid patron password.");
464 $trans->do_renew_all;