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;
139 $circ->patron($patron = new ILS::Patron $patron_id);
140 $circ->item($item = new ILS::Item $item_id);
143 $circ->screen_msg("Invalid Patron");
144 } elsif (!$patron->charge_ok) {
145 $circ->screen_msg("Patron Blocked");
147 $circ->screen_msg("Invalid Item");
148 } elsif ($item->hold_queue && @{$item->hold_queue} && ($patron_id ne $item->hold_queue->[0])) {
149 $circ->screen_msg("Item on Hold for Another User");
150 } elsif ($item->{patron} && ($item->{patron} ne $patron_id)) {
151 # I can't deal with this right now
152 $circ->screen_msg("Item checked out to another patron");
154 $circ->do_checkout();
157 # If the item is already associated with this patron, then
159 $circ->renew_ok($item->{patron} && ($item->{patron} eq $patron_id));
161 $item->{patron} = $patron_id;
162 $item->{due_date} = $circ->{due};
163 push(@{$patron->{items}}, $item_id);
164 $circ->desensitize(!$item->magnetic);
166 syslog("LOG_DEBUG", "ILS::Checkout: patron %s has checked out %s",
167 $patron_id, join(', ', @{$patron->{items}}));
170 syslog("LOG_DEBUG", "ILS::Checkout Issue failed");
181 my ($self, $item_id, $trans_date, $return_date,
182 $current_loc, $item_props, $cancel) = @_;
183 my ($patron, $item, $circ);
185 $circ = new ILS::Transaction::Checkin;
187 $circ->item($item = new ILS::Item $item_id);
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}} ];
205 # If the ILS caches patron information, this lets it free
207 sub end_patron_session {
208 my ($self, $patron_id) = @_;
210 # success?, screen_msg, print_line
211 return (1, 'Thank you !', '');
215 my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
216 $pay_type, $fee_id, $trans_id, $currency) = @_;
220 # $trans = new ILS::Transaction::FeePayment;
222 $patron = new ILS::Patron $patron_id;
224 $trans->transaction_id($trans_id);
225 $trans->patron($patron);
232 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
233 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
239 $trans = new ILS::Transaction::Hold;
242 $patron = new ILS::Patron $patron_id;
244 || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
245 $trans->screen_msg("Invalid Patron.");
250 $item = new ILS::Item ($item_id || $title_id);
252 $trans->screen_msg("No such item.");
254 # END TRANSACTION (conditionally)
256 } elsif ($item->fee && ($fee_ack ne 'Y')) {
257 $trans->screen_msg = "Fee required to place hold.";
259 # END TRANSACTION (conditionally)
264 item_id => $item->id,
265 patron_id => $patron->id,
266 expiration_date => $expiry_date,
267 pickup_location => $pickup_location,
268 hold_type => $hold_type,
272 $trans->patron($patron);
274 $trans->pickup_location($pickup_location);
276 push(@{$item->hold_queue}, $hold);
277 push(@{$patron->{hold_items}}, $hold);
285 my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
286 my ($patron, $item, $hold);
289 $trans = new ILS::Transaction::Hold;
292 $patron = new ILS::Patron $patron_id;
294 $trans->screen_msg("Invalid patron barcode.");
297 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
298 $trans->screen_msg('Invalid patron password.');
303 $item = new ILS::Item ($item_id || $title_id);
305 $trans->screen_msg("No such item.");
307 # END TRANSACTION (conditionally)
311 # Remove the hold from the patron's record first
312 $trans->ok($patron->drop_hold($item_id));
315 # We didn't find it on the patron record
316 $trans->screen_msg("No such hold on patron record.");
318 # END TRANSACTION (conditionally)
322 # Now, remove it from the item record. If it was on the patron
323 # record but not on the item record, we'll treat that as success.
324 foreach my $i (0 .. scalar @{$item->hold_queue}) {
325 $hold = $item->hold_queue->[$i];
327 if ($hold->{patron_id} eq $patron->id) {
328 # found it: delete it.
329 splice @{$item->hold_queue}, $i, 1;
334 $trans->screen_msg("Hold Cancelled.");
335 $trans->patron($patron);
342 # The patron and item id's can't be altered, but the
343 # date, location, and type can.
345 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
346 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
351 $trans = new ILS::Transaction::Hold;
354 $patron = new ILS::Patron $patron_id;
356 $trans->screen_msg("Invalid patron barcode.");
361 foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
362 $hold = $patron->{hold_items}[$i];
364 if ($hold->{item_id} eq $item_id) {
365 # Found it. So fix it.
366 $hold->{expiration_date} = $expiry_date if $expiry_date;
367 $hold->{pickup_location} = $pickup_location if $pickup_location;
368 $hold->{hold_type} = $hold_type if $hold_type;
371 $trans->screen_msg("Hold updated.");
372 $trans->patron($patron);
373 $trans->item(new ILS::Item $hold->{item_id});
378 # The same hold structure is linked into both the patron's
379 # list of hold items and into the queue of outstanding holds
380 # for the item, so we don't need to search the hold queue for
381 # the item, since it's already been updated by the patron code.
384 $trans->screen_msg("No such outstanding hold.");
391 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
392 $no_block, $nb_due_date, $third_party,
393 $item_props, $fee_ack) = @_;
397 $trans = new ILS::Transaction::Renew;
398 $trans->patron($patron = new ILS::Patron $patron_id);
401 $trans->screen_msg("Invalid patron barcode.");
404 } elsif (!$patron->renew_ok) {
405 $trans->screen_msg("Renewals not allowed.");
410 if (defined($title_id)) {
411 # renewing a title, rather than an item (sort of)
412 # This is gross, but in a real ILS it would be better
413 foreach my $i (@{$patron->{items}}) {
414 $item = new ILS::Item $i;
415 last if ($title_id eq $item->title_id);
419 foreach my $i (@{$patron->{items}}) {
420 if ($i == $item_id) {
421 # We have it checked out
422 $item = new ILS::Item $item_id;
430 if (!defined($item)) {
431 # It's not checked out to $patron_id
432 $trans->screen_msg("Item not checked out to " . $patron->name);
433 } elsif (!$item->available($patron_id)) {
434 $trans->screen_msg("Item has outstanding holds");
436 $trans->renewal_ok(1);
438 $trans->desensitize(0); # It's already checked out
440 # warn "done renew $trans->renewal_ok(1);";
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;
458 my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
459 my ($patron, $item_id);
462 $trans = new ILS::Transaction::RenewAll;
464 $trans->patron($patron = new ILS::Patron $patron_id);
465 if (defined $patron) {
466 syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s",
467 $patron->name, $patron->renew_ok);
469 syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'",
473 if (!defined($patron)) {
474 $trans->screen_msg("Invalid patron barcode.");
476 } elsif (!$patron->renew_ok) {
477 $trans->screen_msg("Renewals not allowed.");
479 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
480 $trans->screen_msg("Invalid patron password.");
484 foreach $item_id (@{$patron->{items}}) {
485 my $item = new ILS::Item $item_id;
487 if (!defined($item)) {
488 syslog("LOG_WARNING",
489 "renew_all: Invalid item id associated with patron '%s'",
494 if (@{$item->hold_queue}) {
495 # Can't renew if there are outstanding holds
496 push @{$trans->unrenewed}, $item_id;
498 $item->{due_date} = time + (14*24*60*60); # two weeks hence
499 push @{$trans->renewed}, $item_id;