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' => 1,
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;
47 #warn " INSTITUTION:";
48 #warn Dumper($institution);
49 syslog("LOG_DEBUG", "new ILS '%s'", $institution->{id});
50 $self->{institution} = $institution;
52 return bless $self, $type;
57 warn "finding patron";
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 && @{$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");
155 $circ->do_checkout();
158 # If the item is already associated with this patron, then
160 $circ->renew_ok($item->{patron} && ($item->{patron} eq $patron_id));
162 $item->{patron} = $patron_id;
163 $item->{due_date} = time + (14*24*60*60); # two weeks
164 # $item->{due_date} = $circ->{due};
165 warn "$item->{due_date}";
166 push(@{$patron->{items}}, $item_id);
167 $circ->desensitize(!$item->magnetic);
169 syslog("LOG_DEBUG", "ILS::Checkout: patron %s has checked out %s",
170 $patron_id, join(', ', @{$patron->{items}}));
173 syslog("LOG_DEBUG", "ILS::Checkout Issue failed");
184 my ($self, $item_id, $trans_date, $return_date,
185 $current_loc, $item_props, $cancel) = @_;
186 my ($patron, $item, $circ);
188 $circ = new ILS::Transaction::Checkin;
190 $circ->item($item = new ILS::Item $item_id);
192 # It's ok to check it in if it exists, and if it was checked out
193 $circ->ok($item && $item->{patron});
196 $circ->patron($patron = new ILS::Patron $item->{patron});
197 delete $item->{patron};
198 delete $item->{due_date};
199 $patron->{items} = [ grep {$_ ne $item_id} @{$patron->{items}} ];
206 # If the ILS caches patron information, this lets it free
208 sub end_patron_session {
209 my ($self, $patron_id) = @_;
211 # success?, screen_msg, print_line
212 return (1, 'Thank you !', '');
216 my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
217 $pay_type, $fee_id, $trans_id, $currency) = @_;
221 # $trans = new ILS::Transaction::FeePayment;
223 $patron = new ILS::Patron $patron_id;
225 $trans->transaction_id($trans_id);
226 $trans->patron($patron);
233 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
234 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
240 $trans = new ILS::Transaction::Hold;
243 $patron = new ILS::Patron $patron_id;
245 || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
246 $trans->screen_msg("Invalid Patron.");
251 $item = new ILS::Item ($item_id || $title_id);
253 $trans->screen_msg("No such item.");
255 # END TRANSACTION (conditionally)
257 } elsif ($item->fee && ($fee_ack ne 'Y')) {
258 $trans->screen_msg = "Fee required to place hold.";
260 # END TRANSACTION (conditionally)
265 item_id => $item->id,
266 patron_id => $patron->id,
267 expiration_date => $expiry_date,
268 pickup_location => $pickup_location,
269 hold_type => $hold_type,
273 $trans->patron($patron);
275 $trans->pickup_location($pickup_location);
277 push(@{$item->hold_queue}, $hold);
278 push(@{$patron->{hold_items}}, $hold);
286 my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
287 my ($patron, $item, $hold);
290 $trans = new ILS::Transaction::Hold;
293 $patron = new ILS::Patron $patron_id;
295 $trans->screen_msg("Invalid patron barcode.");
298 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
299 $trans->screen_msg('Invalid patron password.');
304 $item = new ILS::Item ($item_id || $title_id);
306 $trans->screen_msg("No such item.");
308 # END TRANSACTION (conditionally)
312 # Remove the hold from the patron's record first
313 $trans->ok($patron->drop_hold($item_id));
316 # We didn't find it on the patron record
317 $trans->screen_msg("No such hold on patron record.");
319 # END TRANSACTION (conditionally)
323 # Now, remove it from the item record. If it was on the patron
324 # record but not on the item record, we'll treat that as success.
325 foreach my $i (0 .. scalar @{$item->hold_queue}) {
326 $hold = $item->hold_queue->[$i];
328 if ($hold->{patron_id} eq $patron->id) {
329 # found it: delete it.
330 splice @{$item->hold_queue}, $i, 1;
335 $trans->screen_msg("Hold Cancelled.");
336 $trans->patron($patron);
343 # The patron and item id's can't be altered, but the
344 # date, location, and type can.
346 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
347 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
352 $trans = new ILS::Transaction::Hold;
355 $patron = new ILS::Patron $patron_id;
357 $trans->screen_msg("Invalid patron barcode.");
362 foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
363 $hold = $patron->{hold_items}[$i];
365 if ($hold->{item_id} eq $item_id) {
366 # Found it. So fix it.
367 $hold->{expiration_date} = $expiry_date if $expiry_date;
368 $hold->{pickup_location} = $pickup_location if $pickup_location;
369 $hold->{hold_type} = $hold_type if $hold_type;
372 $trans->screen_msg("Hold updated.");
373 $trans->patron($patron);
374 $trans->item(new ILS::Item $hold->{item_id});
379 # The same hold structure is linked into both the patron's
380 # list of hold items and into the queue of outstanding holds
381 # for the item, so we don't need to search the hold queue for
382 # the item, since it's already been updated by the patron code.
385 $trans->screen_msg("No such outstanding hold.");
392 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
393 $no_block, $nb_due_date, $third_party,
394 $item_props, $fee_ack) = @_;
398 $trans = new ILS::Transaction::Renew;
400 $trans->patron($patron = new ILS::Patron $patron_id);
403 $trans->screen_msg("Invalid patron barcode.");
406 } elsif (!$patron->renew_ok) {
407 $trans->screen_msg("Renewals not allowed.");
412 if (defined($title_id)) {
413 # renewing a title, rather than an item (sort of)
414 # This is gross, but in a real ILS it would be better
415 foreach my $i (@{$patron->{items}}) {
416 $item = new ILS::Item $i;
417 last if ($title_id eq $item->title_id);
421 foreach my $i (@{$patron->{items}}) {
422 if ($i == $item_id) {
423 # We have it checked out
424 $item = new ILS::Item $item_id;
432 if (!defined($item)) {
433 # It's not checked out to $patron_id
434 $trans->screen_msg("Item not checked out to " . $patron->name);
435 } elsif (!$item->available($patron_id)) {
436 $trans->screen_msg("Item has outstanding holds");
438 $trans->renewal_ok(1);
440 $trans->desensitize(0); # It's already checked out
442 if ($no_block eq 'Y') {
443 $item->{due_date} = $nb_due_date;
445 $item->{due_date} = time + (14*24*60*60); # two weeks
448 $item->{sip_item_properties} = $item_props;
451 $trans->renewal_ok(1);
460 my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
461 my ($patron, $item_id);
464 $trans = new ILS::Transaction::RenewAll;
466 $trans->patron($patron = new ILS::Patron $patron_id);
467 if (defined $patron) {
468 syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s",
469 $patron->name, $patron->renew_ok);
471 syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'",
475 if (!defined($patron)) {
476 $trans->screen_msg("Invalid patron barcode.");
478 } elsif (!$patron->renew_ok) {
479 $trans->screen_msg("Renewals not allowed.");
481 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
482 $trans->screen_msg("Invalid patron password.");
486 foreach $item_id (@{$patron->{items}}) {
487 my $item = new ILS::Item $item_id;
489 if (!defined($item)) {
490 syslog("LOG_WARNING",
491 "renew_all: Invalid item id associated with patron '%s'",
496 if (@{$item->hold_queue}) {
497 # Can't renew if there are outstanding holds
498 push @{$trans->unrenewed}, $item_id;
500 $item->{due_date} = time + (14*24*60*60); # two weeks hence
501 push @{$trans->renewed}, $item_id;