2 # ILS.pm: Test ILS interface module
4 # Copyright (C) 2006-2008 Georgia Public Library Service
6 # Author: David J. Fiander
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of version 2 of the GNU General Public
10 # License as published by the Free Software Foundation.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public
18 # License along with this program; if not, write to the Free
19 # Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 use Sys::Syslog qw(syslog);
31 use ILS::Transaction::Checkout;
32 use ILS::Transaction::Checkin;
33 use ILS::Transaction::FeePayment;
34 use ILS::Transaction::Hold;
35 use ILS::Transaction::Renew;
36 use ILS::Transaction::RenewAll;
39 'magnetic media' => 1,
40 'security inhibit' => 0,
41 'offline operation' => 0,
42 "patron status request" => 1,
48 "patron information" => 1,
49 "end patron session" => 1,
51 "item information" => 1,
52 "item status update" => 0,
60 my ($class, $institution) = @_;
61 my $type = ref($class) || $class;
64 syslog("LOG_DEBUG", "new ILS '%s'", $institution->{id});
65 $self->{institution} = $institution;
67 return bless $self, $type;
73 return ILS::Patron->new(@_);
79 return ILS::Item->new(@_);
85 return $self->{institution}->{id};
91 return (exists($supports{$op}) && $supports{$op});
95 my ($self, $id, $whence) = @_;
97 if ($id ne $self->{institution}->{id}) {
98 syslog("LOG_WARNING", "%s: received institution '%s', expected '%s'",
99 $whence, $id, $self->{institution}->{id});
106 # If it's defined, and matches a true sort of string, or is
107 # a non-zero number, then it's true.
108 return defined($bool) && (($bool =~ /true|y|yes/i) || $bool != 0);
114 return (exists($self->{policy}->{checkout})
115 && to_bool($self->{policy}->{checkout}));
121 return (exists($self->{policy}->{checkin})
122 && to_bool($self->{policy}->{checkin}));
125 sub status_update_ok {
128 return (exists($self->{policy}->{status_update})
129 && to_bool($self->{policy}->{status_update}));
136 return (exists($self->{policy}->{offline})
137 && to_bool($self->{policy}->{offline}));
141 # Checkout(patron_id, item_id, sc_renew):
142 # patron_id & item_id are the identifiers send by the terminal
143 # sc_renew is the renewal policy configured on the terminal
144 # returns a status opject that can be queried for the various bits
145 # of information that the protocol (SIP or NCIP) needs to generate
149 my ($self, $patron_id, $item_id, $sc_renew) = @_;
150 my ($patron, $item, $circ);
152 $circ = new ILS::Transaction::Checkout;
155 $circ->patron($patron = new ILS::Patron $patron_id);
156 $circ->item($item = new ILS::Item $item_id);
159 $circ->screen_msg("Invalid Patron");
160 } elsif (!$patron->charge_ok) {
161 $circ->screen_msg("Patron Blocked");
163 $circ->screen_msg("Invalid Item");
164 } elsif (@{$item->hold_queue} && ($patron_id ne $item->hold_queue->[0])) {
165 $circ->screen_msg("Item on Hold for Another User");
166 } elsif ($item->{patron} && ($item->{patron} ne $patron_id)) {
167 # I can't deal with this right now
168 $circ->screen_msg("Item checked out to another patron");
171 # If the item is already associated with this patron, then
173 $circ->renew_ok($item->{patron} && ($item->{patron} eq $patron_id));
174 $item->{patron} = $patron_id;
175 $item->{due_date} = time + (14*24*60*60); # two weeks
176 push(@{$patron->{items}}, $item_id);
177 $circ->desensitize(!$item->magnetic);
179 syslog("LOG_DEBUG", "ILS::Checkout: patron %s has checked out %s",
180 $patron_id, join(', ', @{$patron->{items}}));
189 my ($self, $item_id, $trans_date, $return_date,
190 $current_loc, $item_props, $cancel) = @_;
191 my ($patron, $item, $circ);
193 $circ = new ILS::Transaction::Checkin;
195 $circ->item($item = new ILS::Item $item_id);
197 # It's ok to check it in if it exists, and if it was checked out
198 $circ->ok($item && $item->{patron});
201 $circ->patron($patron = new ILS::Patron $item->{patron});
202 delete $item->{patron};
203 delete $item->{due_date};
204 $patron->{items} = [ grep {$_ ne $item_id} @{$patron->{items}} ];
211 # If the ILS caches patron information, this lets it free
213 sub end_patron_session {
214 my ($self, $patron_id) = @_;
216 # success?, screen_msg, print_line
217 return (1, 'Thank you for using Evergreen!', '');
221 my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
222 $pay_type, $fee_id, $trans_id, $currency) = @_;
226 $trans = new ILS::Transaction::FeePayment;
228 $patron = new ILS::Patron $patron_id;
230 $trans->transaction_id($trans_id);
231 $trans->patron($patron);
238 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
239 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
245 $trans = new ILS::Transaction::Hold;
248 $patron = new ILS::Patron $patron_id;
250 || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
251 $trans->screen_msg("Invalid Patron.");
256 $item = new ILS::Item ($item_id || $title_id);
258 $trans->screen_msg("No such item.");
260 # END TRANSACTION (conditionally)
262 } elsif ($item->fee && ($fee_ack ne 'Y')) {
263 $trans->screen_msg = "Fee required to place hold.";
265 # END TRANSACTION (conditionally)
270 item_id => $item->id,
271 patron_id => $patron->id,
272 expiration_date => $expiry_date,
273 pickup_location => $pickup_location,
274 hold_type => $hold_type,
278 $trans->patron($patron);
280 $trans->pickup_location($pickup_location);
282 push(@{$item->hold_queue}, $hold);
283 push(@{$patron->{hold_items}}, $hold);
291 my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
292 my ($patron, $item, $hold);
295 $trans = new ILS::Transaction::Hold;
298 $patron = new ILS::Patron $patron_id;
300 $trans->screen_msg("Invalid patron barcode.");
303 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
304 $trans->screen_msg('Invalid patron password.');
309 $item = new ILS::Item ($item_id || $title_id);
311 $trans->screen_msg("No such item.");
313 # END TRANSACTION (conditionally)
317 # Remove the hold from the patron's record first
318 $trans->ok($patron->drop_hold($item_id));
321 # We didn't find it on the patron record
322 $trans->screen_msg("No such hold on patron record.");
324 # END TRANSACTION (conditionally)
328 # Now, remove it from the item record. If it was on the patron
329 # record but not on the item record, we'll treat that as success.
330 foreach my $i (0 .. scalar @{$item->hold_queue}) {
331 $hold = $item->hold_queue->[$i];
333 if ($hold->{patron_id} eq $patron->id) {
334 # found it: delete it.
335 splice @{$item->hold_queue}, $i, 1;
340 $trans->screen_msg("Hold Cancelled.");
341 $trans->patron($patron);
348 # The patron and item id's can't be altered, but the
349 # date, location, and type can.
351 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
352 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
357 $trans = new ILS::Transaction::Hold;
360 $patron = new ILS::Patron $patron_id;
362 $trans->screen_msg("Invalid patron barcode.");
367 foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
368 $hold = $patron->{hold_items}[$i];
370 if ($hold->{item_id} eq $item_id) {
371 # Found it. So fix it.
372 $hold->{expiration_date} = $expiry_date if $expiry_date;
373 $hold->{pickup_location} = $pickup_location if $pickup_location;
374 $hold->{hold_type} = $hold_type if $hold_type;
377 $trans->screen_msg("Hold updated.");
378 $trans->patron($patron);
379 $trans->item(new ILS::Item $hold->{item_id});
384 # The same hold structure is linked into both the patron's
385 # list of hold items and into the queue of outstanding holds
386 # for the item, so we don't need to search the hold queue for
387 # the item, since it's already been updated by the patron code.
390 $trans->screen_msg("No such outstanding hold.");
397 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
398 $no_block, $nb_due_date, $third_party,
399 $item_props, $fee_ack) = @_;
403 $trans = new ILS::Transaction::Renew;
405 $trans->patron($patron = new ILS::Patron $patron_id);
408 $trans->screen_msg("Invalid patron barcode.");
411 } elsif (!$patron->renew_ok) {
412 $trans->screen_msg("Renewals not allowed.");
417 if (defined($title_id)) {
418 # renewing a title, rather than an item (sort of)
419 # This is gross, but in a real ILS it would be better
420 foreach my $i (@{$patron->{items}}) {
421 $item = new ILS::Item $i;
422 last if ($title_id eq $item->title_id);
426 foreach my $i (@{$patron->{items}}) {
427 if ($i == $item_id) {
428 # We have it checked out
429 $item = new ILS::Item $item_id;
437 if (!defined($item)) {
438 # It's not checked out to $patron_id
439 $trans->screen_msg("Item not checked out to " . $patron->name);
440 } elsif (!$item->available($patron_id)) {
441 $trans->screen_msg("Item has outstanding holds");
443 $trans->renewal_ok(1);
445 $trans->desensitize(0); # It's already checked out
447 if ($no_block eq 'Y') {
448 $item->{due_date} = $nb_due_date;
450 $item->{due_date} = time + (14*24*60*60); # two weeks
453 $item->{sip_item_properties} = $item_props;
456 $trans->renewal_ok(1);
465 my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
466 my ($patron, $item_id);
469 $trans = new ILS::Transaction::RenewAll;
471 $trans->patron($patron = new ILS::Patron $patron_id);
472 if (defined $patron) {
473 syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s",
474 $patron->name, $patron->renew_ok);
476 syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'",
480 if (!defined($patron)) {
481 $trans->screen_msg("Invalid patron barcode.");
483 } elsif (!$patron->renew_ok) {
484 $trans->screen_msg("Renewals not allowed.");
486 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
487 $trans->screen_msg("Invalid patron password.");
491 foreach $item_id (@{$patron->{items}}) {
492 my $item = new ILS::Item $item_id;
494 if (!defined($item)) {
495 syslog("LOG_WARNING",
496 "renew_all: Invalid item id associated with patron '%s'",
501 if (@{$item->hold_queue}) {
502 # Can't renew if there are outstanding holds
503 push @{$trans->unrenewed}, $item_id;
505 $item->{due_date} = time + (14*24*60*60); # two weeks hence
506 push @{$trans->renewed}, $item_id;