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;
22 'magnetic media' => 0,
23 'security inhibit' => 0,
24 'offline operation' => 0,
25 "patron status request" => 1,
31 "patron information" => 1,
32 "end patron session" => 1,
34 "item information" => 1,
35 "item status update" => 0,
43 my ($class, $institution) = @_;
44 my $type = ref($class) || $class;
48 warn Dumper($institution);
49 syslog("LOG_DEBUG", "new ILS '%s'", $institution->{id});
50 $self->{institution} = $institution;
52 return bless $self, $type;
58 return ILS::Patron->new(@_);
64 return ILS::Item->new(@_);
70 return $self->{institution}->{id};
76 return (exists($supports{$op}) && $supports{$op});
80 my ($self, $id, $whence) = @_;
82 if ($id ne $self->{institution}->{id}) {
83 syslog("LOG_WARNING", "%s: received institution '%s', expected '%s'",
84 $whence, $id, $self->{institution}->{id});
91 # If it's defined, and matches a true sort of string, or is
92 # a non-zero number, then it's true.
93 return defined($bool) && (($bool =~ /true|y|yes/i) || $bool != 0);
99 return (exists($self->{policy}->{checkout})
100 && to_bool($self->{policy}->{checkout}));
106 return (exists($self->{policy}->{checkin})
107 && to_bool($self->{policy}->{checkin}));
110 sub status_update_ok {
113 return (exists($self->{policy}->{status_update})
114 && to_bool($self->{policy}->{status_update}));
121 return (exists($self->{policy}->{offline})
122 && to_bool($self->{policy}->{offline}));
126 # Checkout(patron_id, item_id, sc_renew):
127 # patron_id & item_id are the identifiers send by the terminal
128 # sc_renew is the renewal policy configured on the terminal
129 # returns a status opject that can be queried for the various bits
130 # of information that the protocol (SIP or NCIP) needs to generate
134 my ($self, $patron_id, $item_id, $sc_renew) = @_;
135 my ($patron, $item, $circ);
137 $circ = new ILS::Transaction::Checkout;
140 $circ->patron($patron = new ILS::Patron $patron_id);
141 $circ->item($item = new ILS::Item $item_id);
144 $circ->screen_msg("Invalid Patron");
145 } elsif (!$patron->charge_ok) {
146 $circ->screen_msg("Patron Blocked");
148 $circ->screen_msg("Invalid Item");
149 } elsif (@{$item->hold_queue} && ($patron_id ne $item->hold_queue->[0])) {
150 $circ->screen_msg("Item on Hold for Another User");
151 } elsif ($item->{patron} && ($item->{patron} ne $patron_id)) {
152 # I can't deal with this right now
153 $circ->screen_msg("Item checked out to another patron");
156 # If the item is already associated with this patron, then
158 $circ->renew_ok($item->{patron} && ($item->{patron} eq $patron_id));
159 $item->{patron} = $patron_id;
160 $item->{due_date} = time + (14*24*60*60); # two weeks
161 push(@{$patron->{items}}, $item_id);
162 $circ->desensitize(!$item->magnetic);
164 syslog("LOG_DEBUG", "ILS::Checkout: patron %s has checked out %s",
165 $patron_id, join(', ', @{$patron->{items}}));
174 my ($self, $item_id, $trans_date, $return_date,
175 $current_loc, $item_props, $cancel) = @_;
176 my ($patron, $item, $circ);
178 $circ = new ILS::Transaction::Checkin;
180 $circ->item($item = new ILS::Item $item_id);
182 # It's ok to check it in if it exists, and if it was checked out
183 $circ->ok($item && $item->{patron});
186 $circ->patron($patron = new ILS::Patron $item->{patron});
187 delete $item->{patron};
188 delete $item->{due_date};
189 $patron->{items} = [ grep {$_ ne $item_id} @{$patron->{items}} ];
196 # If the ILS caches patron information, this lets it free
198 sub end_patron_session {
199 my ($self, $patron_id) = @_;
201 # success?, screen_msg, print_line
202 return (1, 'Thank you !', '');
206 my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
207 $pay_type, $fee_id, $trans_id, $currency) = @_;
211 $trans = new ILS::Transaction::FeePayment;
213 $patron = new ILS::Patron $patron_id;
215 $trans->transaction_id($trans_id);
216 $trans->patron($patron);
223 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
224 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
230 $trans = new ILS::Transaction::Hold;
233 $patron = new ILS::Patron $patron_id;
235 || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
236 $trans->screen_msg("Invalid Patron.");
241 $item = new ILS::Item ($item_id || $title_id);
243 $trans->screen_msg("No such item.");
245 # END TRANSACTION (conditionally)
247 } elsif ($item->fee && ($fee_ack ne 'Y')) {
248 $trans->screen_msg = "Fee required to place hold.";
250 # END TRANSACTION (conditionally)
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);
267 push(@{$item->hold_queue}, $hold);
268 push(@{$patron->{hold_items}}, $hold);
276 my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
277 my ($patron, $item, $hold);
280 $trans = new ILS::Transaction::Hold;
283 $patron = new ILS::Patron $patron_id;
285 $trans->screen_msg("Invalid patron barcode.");
288 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
289 $trans->screen_msg('Invalid patron password.');
294 $item = new ILS::Item ($item_id || $title_id);
296 $trans->screen_msg("No such item.");
298 # END TRANSACTION (conditionally)
302 # Remove the hold from the patron's record first
303 $trans->ok($patron->drop_hold($item_id));
306 # We didn't find it on the patron record
307 $trans->screen_msg("No such hold on patron record.");
309 # END TRANSACTION (conditionally)
313 # Now, remove it from the item record. If it was on the patron
314 # record but not on the item record, we'll treat that as success.
315 foreach my $i (0 .. scalar @{$item->hold_queue}) {
316 $hold = $item->hold_queue->[$i];
318 if ($hold->{patron_id} eq $patron->id) {
319 # found it: delete it.
320 splice @{$item->hold_queue}, $i, 1;
325 $trans->screen_msg("Hold Cancelled.");
326 $trans->patron($patron);
333 # The patron and item id's can't be altered, but the
334 # date, location, and type can.
336 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
337 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
342 $trans = new ILS::Transaction::Hold;
345 $patron = new ILS::Patron $patron_id;
347 $trans->screen_msg("Invalid patron barcode.");
352 foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
353 $hold = $patron->{hold_items}[$i];
355 if ($hold->{item_id} eq $item_id) {
356 # Found it. So fix it.
357 $hold->{expiration_date} = $expiry_date if $expiry_date;
358 $hold->{pickup_location} = $pickup_location if $pickup_location;
359 $hold->{hold_type} = $hold_type if $hold_type;
362 $trans->screen_msg("Hold updated.");
363 $trans->patron($patron);
364 $trans->item(new ILS::Item $hold->{item_id});
369 # The same hold structure is linked into both the patron's
370 # list of hold items and into the queue of outstanding holds
371 # for the item, so we don't need to search the hold queue for
372 # the item, since it's already been updated by the patron code.
375 $trans->screen_msg("No such outstanding hold.");
382 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
383 $no_block, $nb_due_date, $third_party,
384 $item_props, $fee_ack) = @_;
388 $trans = new ILS::Transaction::Renew;
390 $trans->patron($patron = new ILS::Patron $patron_id);
393 $trans->screen_msg("Invalid patron barcode.");
396 } elsif (!$patron->renew_ok) {
397 $trans->screen_msg("Renewals not allowed.");
402 if (defined($title_id)) {
403 # renewing a title, rather than an item (sort of)
404 # This is gross, but in a real ILS it would be better
405 foreach my $i (@{$patron->{items}}) {
406 $item = new ILS::Item $i;
407 last if ($title_id eq $item->title_id);
411 foreach my $i (@{$patron->{items}}) {
412 if ($i == $item_id) {
413 # We have it checked out
414 $item = new ILS::Item $item_id;
422 if (!defined($item)) {
423 # It's not checked out to $patron_id
424 $trans->screen_msg("Item not checked out to " . $patron->name);
425 } elsif (!$item->available($patron_id)) {
426 $trans->screen_msg("Item has outstanding holds");
428 $trans->renewal_ok(1);
430 $trans->desensitize(0); # It's already checked out
432 if ($no_block eq 'Y') {
433 $item->{due_date} = $nb_due_date;
435 $item->{due_date} = time + (14*24*60*60); # two weeks
438 $item->{sip_item_properties} = $item_props;
441 $trans->renewal_ok(1);
450 my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
451 my ($patron, $item_id);
454 $trans = new ILS::Transaction::RenewAll;
456 $trans->patron($patron = new ILS::Patron $patron_id);
457 if (defined $patron) {
458 syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s",
459 $patron->name, $patron->renew_ok);
461 syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'",
465 if (!defined($patron)) {
466 $trans->screen_msg("Invalid patron barcode.");
468 } elsif (!$patron->renew_ok) {
469 $trans->screen_msg("Renewals not allowed.");
471 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
472 $trans->screen_msg("Invalid patron password.");
476 foreach $item_id (@{$patron->{items}}) {
477 my $item = new ILS::Item $item_id;
479 if (!defined($item)) {
480 syslog("LOG_WARNING",
481 "renew_all: Invalid item id associated with patron '%s'",
486 if (@{$item->hold_queue}) {
487 # Can't renew if there are outstanding holds
488 push @{$trans->unrenewed}, $item_id;
490 $item->{due_date} = time + (14*24*60*60); # two weeks hence
491 push @{$trans->renewed}, $item_id;