2 # ILS.pm: Test 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;
47 syslog("LOG_DEBUG", "new ILS '%s'", $institution->{id});
48 $self->{institution} = $institution;
50 return bless $self, $type;
56 return ILS::Patron->new(@_);
62 return ILS::Item->new(@_);
68 return $self->{institution}->{id};
74 return (exists($supports{$op}) && $supports{$op});
78 my ($self, $id, $whence) = @_;
80 if ($id ne $self->{institution}->{id}) {
81 syslog("LOG_WARNING", "%s: received institution '%s', expected '%s'",
82 $whence, $id, $self->{institution}->{id});
89 # If it's defined, and matches a true sort of string, or is
90 # a non-zero number, then it's true.
91 return defined($bool) && (($bool =~ /true|y|yes/i) || $bool != 0);
97 return (exists($self->{policy}->{checkout})
98 && to_bool($self->{policy}->{checkout}));
104 return (exists($self->{policy}->{checkin})
105 && to_bool($self->{policy}->{checkin}));
108 sub status_update_ok {
111 return (exists($self->{policy}->{status_update})
112 && to_bool($self->{policy}->{status_update}));
119 return (exists($self->{policy}->{offline})
120 && to_bool($self->{policy}->{offline}));
124 # Checkout(patron_id, item_id, sc_renew):
125 # patron_id & item_id are the identifiers send by the terminal
126 # sc_renew is the renewal policy configured on the terminal
127 # returns a status opject that can be queried for the various bits
128 # of information that the protocol (SIP or NCIP) needs to generate
132 my ($self, $patron_id, $item_id, $sc_renew) = @_;
133 my ($patron, $item, $circ);
135 $circ = new ILS::Transaction::Checkout;
138 $circ->patron($patron = new ILS::Patron $patron_id);
139 $circ->item($item = new ILS::Item $item_id);
142 $circ->screen_msg("Invalid Patron");
143 } elsif (!$patron->charge_ok) {
144 $circ->screen_msg("Patron Blocked");
146 $circ->screen_msg("Invalid Item");
147 } elsif (@{$item->hold_queue} && ($patron_id ne $item->hold_queue->[0])) {
148 $circ->screen_msg("Item on Hold for Another User");
149 } elsif ($item->{patron} && ($item->{patron} ne $patron_id)) {
150 # I can't deal with this right now
151 $circ->screen_msg("Item checked out to another patron");
154 # If the item is already associated with this patron, then
156 $circ->renew_ok($item->{patron} && ($item->{patron} eq $patron_id));
157 $item->{patron} = $patron_id;
158 $item->{due_date} = time + (14*24*60*60); # two weeks
159 push(@{$patron->{items}}, $item_id);
160 $circ->desensitize(!$item->magnetic);
162 syslog("LOG_DEBUG", "ILS::Checkout: patron %s has checked out %s",
163 $patron_id, join(', ', @{$patron->{items}}));
172 my ($self, $item_id, $trans_date, $return_date,
173 $current_loc, $item_props, $cancel) = @_;
174 my ($patron, $item, $circ);
176 $circ = new ILS::Transaction::Checkin;
178 $circ->item($item = new ILS::Item $item_id);
180 # It's ok to check it in if it exists, and if it was checked out
181 $circ->ok($item && $item->{patron});
184 $circ->patron($patron = new ILS::Patron $item->{patron});
185 delete $item->{patron};
186 delete $item->{due_date};
187 $patron->{items} = [ grep {$_ ne $item_id} @{$patron->{items}} ];
194 # If the ILS caches patron information, this lets it free
196 sub end_patron_session {
197 my ($self, $patron_id) = @_;
199 # success?, screen_msg, print_line
200 return (1, 'Thank you for using Evergreen!', '');
204 my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
205 $pay_type, $fee_id, $trans_id, $currency) = @_;
209 $trans = new ILS::Transaction::FeePayment;
211 $patron = new ILS::Patron $patron_id;
213 $trans->transaction_id($trans_id);
214 $trans->patron($patron);
221 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
222 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
228 $trans = new ILS::Transaction::Hold;
231 $patron = new ILS::Patron $patron_id;
233 || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
234 $trans->screen_msg("Invalid Patron.");
239 $item = new ILS::Item ($item_id || $title_id);
241 $trans->screen_msg("No such item.");
243 # END TRANSACTION (conditionally)
245 } elsif ($item->fee && ($fee_ack ne 'Y')) {
246 $trans->screen_msg = "Fee required to place hold.";
248 # END TRANSACTION (conditionally)
253 item_id => $item->id,
254 patron_id => $patron->id,
255 expiration_date => $expiry_date,
256 pickup_location => $pickup_location,
257 hold_type => $hold_type,
261 $trans->patron($patron);
263 $trans->pickup_location($pickup_location);
265 push(@{$item->hold_queue}, $hold);
266 push(@{$patron->{hold_items}}, $hold);
274 my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
275 my ($patron, $item, $hold);
278 $trans = new ILS::Transaction::Hold;
281 $patron = new ILS::Patron $patron_id;
283 $trans->screen_msg("Invalid patron barcode.");
286 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
287 $trans->screen_msg('Invalid patron password.');
292 $item = new ILS::Item ($item_id || $title_id);
294 $trans->screen_msg("No such item.");
296 # END TRANSACTION (conditionally)
300 # Remove the hold from the patron's record first
301 $trans->ok($patron->drop_hold($item_id));
304 # We didn't find it on the patron record
305 $trans->screen_msg("No such hold on patron record.");
307 # END TRANSACTION (conditionally)
311 # Now, remove it from the item record. If it was on the patron
312 # record but not on the item record, we'll treat that as success.
313 foreach my $i (0 .. scalar @{$item->hold_queue}) {
314 $hold = $item->hold_queue->[$i];
316 if ($hold->{patron_id} eq $patron->id) {
317 # found it: delete it.
318 splice @{$item->hold_queue}, $i, 1;
323 $trans->screen_msg("Hold Cancelled.");
324 $trans->patron($patron);
331 # The patron and item id's can't be altered, but the
332 # date, location, and type can.
334 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
335 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
340 $trans = new ILS::Transaction::Hold;
343 $patron = new ILS::Patron $patron_id;
345 $trans->screen_msg("Invalid patron barcode.");
350 foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
351 $hold = $patron->{hold_items}[$i];
353 if ($hold->{item_id} eq $item_id) {
354 # Found it. So fix it.
355 $hold->{expiration_date} = $expiry_date if $expiry_date;
356 $hold->{pickup_location} = $pickup_location if $pickup_location;
357 $hold->{hold_type} = $hold_type if $hold_type;
360 $trans->screen_msg("Hold updated.");
361 $trans->patron($patron);
362 $trans->item(new ILS::Item $hold->{item_id});
367 # The same hold structure is linked into both the patron's
368 # list of hold items and into the queue of outstanding holds
369 # for the item, so we don't need to search the hold queue for
370 # the item, since it's already been updated by the patron code.
373 $trans->screen_msg("No such outstanding hold.");
380 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
381 $no_block, $nb_due_date, $third_party,
382 $item_props, $fee_ack) = @_;
386 $trans = new ILS::Transaction::Renew;
388 $trans->patron($patron = new ILS::Patron $patron_id);
391 $trans->screen_msg("Invalid patron barcode.");
394 } elsif (!$patron->renew_ok) {
395 $trans->screen_msg("Renewals not allowed.");
400 if (defined($title_id)) {
401 # renewing a title, rather than an item (sort of)
402 # This is gross, but in a real ILS it would be better
403 foreach my $i (@{$patron->{items}}) {
404 $item = new ILS::Item $i;
405 last if ($title_id eq $item->title_id);
409 foreach my $i (@{$patron->{items}}) {
410 if ($i == $item_id) {
411 # We have it checked out
412 $item = new ILS::Item $item_id;
420 if (!defined($item)) {
421 # It's not checked out to $patron_id
422 $trans->screen_msg("Item not checked out to " . $patron->name);
423 } elsif (!$item->available($patron_id)) {
424 $trans->screen_msg("Item has outstanding holds");
426 $trans->renewal_ok(1);
428 $trans->desensitize(0); # It's already checked out
430 if ($no_block eq 'Y') {
431 $item->{due_date} = $nb_due_date;
433 $item->{due_date} = time + (14*24*60*60); # two weeks
436 $item->{sip_item_properties} = $item_props;
439 $trans->renewal_ok(1);
448 my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
449 my ($patron, $item_id);
452 $trans = new ILS::Transaction::RenewAll;
454 $trans->patron($patron = new ILS::Patron $patron_id);
455 if (defined $patron) {
456 syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s",
457 $patron->name, $patron->renew_ok);
459 syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'",
463 if (!defined($patron)) {
464 $trans->screen_msg("Invalid patron barcode.");
466 } elsif (!$patron->renew_ok) {
467 $trans->screen_msg("Renewals not allowed.");
469 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
470 $trans->screen_msg("Invalid patron password.");
474 foreach $item_id (@{$patron->{items}}) {
475 my $item = new ILS::Item $item_id;
477 if (!defined($item)) {
478 syslog("LOG_WARNING",
479 "renew_all: Invalid item id associated with patron '%s'",
484 if (@{$item->hold_queue}) {
485 # Can't renew if there are outstanding holds
486 push @{$trans->unrenewed}, $item_id;
488 $item->{due_date} = time + (14*24*60*60); # two weeks hence
489 push @{$trans->renewed}, $item_id;