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);
32 use ILS::Transaction::Checkout;
33 use ILS::Transaction::Checkin;
34 use ILS::Transaction::FeePayment;
35 use ILS::Transaction::Hold;
36 use ILS::Transaction::Renew;
37 use ILS::Transaction::RenewAll;
40 'magnetic media' => 1,
41 'security inhibit' => 0,
42 'offline operation' => 0,
43 "patron status request" => 1,
49 "patron information" => 1,
50 "end patron session" => 1,
52 "item information" => 1,
53 "item status update" => 0,
61 my ($class, $institution) = @_;
62 my $type = ref($class) || $class;
65 syslog("LOG_DEBUG", "new ILS '%s'", $institution->{id});
66 $self->{institution} = $institution;
68 return bless $self, $type;
74 return ILS::Patron->new(@_);
80 return ILS::Item->new(@_);
86 return $self->{institution}->{id};
92 return (exists($supports{$op}) && $supports{$op});
96 my ($self, $id, $whence) = @_;
98 if ($id ne $self->{institution}->{id}) {
99 syslog("LOG_WARNING", "%s: received institution '%s', expected '%s'",
100 $whence, $id, $self->{institution}->{id});
107 # If it's defined, and matches a true sort of string, or is
108 # a non-zero number, then it's true.
109 return defined($bool) && (($bool =~ /true|y|yes/i) || $bool != 0);
115 return (exists($self->{policy}->{checkout})
116 && to_bool($self->{policy}->{checkout}));
122 return (exists($self->{policy}->{checkin})
123 && to_bool($self->{policy}->{checkin}));
126 sub status_update_ok {
129 return (exists($self->{policy}->{status_update})
130 && to_bool($self->{policy}->{status_update}));
137 return (exists($self->{policy}->{offline})
138 && to_bool($self->{policy}->{offline}));
142 # Checkout(patron_id, item_id, sc_renew):
143 # patron_id & item_id are the identifiers send by the terminal
144 # sc_renew is the renewal policy configured on the terminal
145 # returns a status opject that can be queried for the various bits
146 # of information that the protocol (SIP or NCIP) needs to generate
150 my ($self, $patron_id, $item_id, $sc_renew) = @_;
151 my ($patron, $item, $circ);
153 $circ = new ILS::Transaction::Checkout;
156 $circ->patron($patron = new ILS::Patron $patron_id);
157 $circ->item($item = new ILS::Item $item_id);
160 $circ->screen_msg("Invalid Patron");
161 } elsif (!$patron->charge_ok) {
162 $circ->screen_msg("Patron Blocked");
164 $circ->screen_msg("Invalid Item");
165 } elsif (@{$item->hold_queue} && ($patron_id ne $item->hold_queue->[0])) {
166 $circ->screen_msg("Item on Hold for Another User");
167 } elsif ($item->{patron} && ($item->{patron} ne $patron_id)) {
168 # I can't deal with this right now
169 $circ->screen_msg("Item checked out to another patron");
172 # If the item is already associated with this patron, then
174 $circ->renew_ok($item->{patron} && ($item->{patron} eq $patron_id));
175 $item->{patron} = $patron_id;
176 $item->{due_date} = time + (14*24*60*60); # two weeks
177 push(@{$patron->{items}}, $item_id);
178 $circ->desensitize(!$item->magnetic);
180 syslog("LOG_DEBUG", "ILS::Checkout: patron %s has checked out %s",
181 $patron_id, join(', ', encode_utf8(@{$patron->{items}})));
190 my ($self, $item_id, $trans_date, $return_date,
191 $current_loc, $item_props, $cancel) = @_;
192 my ($patron, $item, $circ);
194 $circ = new ILS::Transaction::Checkin;
196 $circ->item($item = new ILS::Item $item_id);
198 # It's ok to check it in if it exists, and if it was checked out
199 $circ->ok($item && $item->{patron});
202 $circ->patron($patron = new ILS::Patron $item->{patron});
203 delete $item->{patron};
204 delete $item->{due_date};
205 $patron->{items} = [ grep {$_ ne $item_id} @{$patron->{items}} ];
212 # If the ILS caches patron information, this lets it free
214 sub end_patron_session {
215 my ($self, $patron_id) = @_;
217 # success?, screen_msg, print_line
218 return (1, 'Thank you for using Evergreen!', '');
222 my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
223 $pay_type, $fee_id, $trans_id, $currency) = @_;
225 my $trans = ILS::Transaction::FeePayment->new();
226 my $patron = ILS::Patron->new($patron_id);
228 $trans->transaction_id($trans_id);
229 $trans->patron($patron);
236 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
237 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
240 my $trans = new ILS::Transaction::Hold;
241 my $patron = new ILS::Patron $patron_id;
244 || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
245 $trans->screen_msg("Invalid Patron.");
249 my $item = new ILS::Item ($item_id || $title_id);
251 $trans->screen_msg("No such item.");
253 } elsif ($item->fee && ($fee_ack ne 'Y')) {
254 $trans->screen_msg = "Fee required to place hold.";
259 item_id => $item->id,
260 patron_id => $patron->id,
261 expiration_date => $expiry_date,
262 pickup_location => $pickup_location,
263 hold_type => $hold_type,
267 $trans->patron($patron);
269 $trans->pickup_location($pickup_location);
271 push(@{$item->hold_queue}, $hold);
272 push(@{$patron->{hold_items}}, $hold);
280 my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
281 my ($patron, $item, $hold);
284 $trans = new ILS::Transaction::Hold;
287 $patron = new ILS::Patron $patron_id;
289 $trans->screen_msg("Invalid patron barcode.");
292 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
293 $trans->screen_msg('Invalid patron password.');
298 $item = new ILS::Item ($item_id || $title_id);
300 $trans->screen_msg("No such item.");
302 # END TRANSACTION (conditionally)
306 # Remove the hold from the patron's record first
307 $trans->ok($patron->drop_hold($item_id));
310 # We didn't find it on the patron record
311 $trans->screen_msg("No such hold on patron record.");
313 # END TRANSACTION (conditionally)
317 # Now, remove it from the item record. If it was on the patron
318 # record but not on the item record, we'll treat that as success.
319 foreach my $i (0 .. scalar @{$item->hold_queue}) {
320 $hold = $item->hold_queue->[$i];
322 if ($hold->{patron_id} eq $patron->id) {
323 # found it: delete it.
324 splice @{$item->hold_queue}, $i, 1;
329 $trans->screen_msg("Hold Cancelled.");
330 $trans->patron($patron);
337 # The patron and item id's can't be altered, but the
338 # date, location, and type can.
340 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
341 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
346 $trans = new ILS::Transaction::Hold;
349 $patron = new ILS::Patron $patron_id;
351 $trans->screen_msg("Invalid patron barcode.");
356 foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
357 $hold = $patron->{hold_items}[$i];
359 if ($hold->{item_id} eq $item_id) {
360 # Found it. So fix it.
361 $hold->{expiration_date} = $expiry_date if $expiry_date;
362 $hold->{pickup_location} = $pickup_location if $pickup_location;
363 $hold->{hold_type} = $hold_type if $hold_type;
366 $trans->screen_msg("Hold updated.");
367 $trans->patron($patron);
368 $trans->item(new ILS::Item $hold->{item_id});
373 # The same hold structure is linked into both the patron's
374 # list of hold items and into the queue of outstanding holds
375 # for the item, so we don't need to search the hold queue for
376 # the item, since it's already been updated by the patron code.
379 $trans->screen_msg("No such outstanding hold.");
386 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
387 $no_block, $nb_due_date, $third_party,
388 $item_props, $fee_ack) = @_;
392 $trans = new ILS::Transaction::Renew;
394 $trans->patron($patron = new ILS::Patron $patron_id);
397 $trans->screen_msg("Invalid patron barcode.");
400 } elsif (!$patron->renew_ok) {
401 $trans->screen_msg("Renewals not allowed.");
406 if (defined($title_id)) {
407 # renewing a title, rather than an item (sort of)
408 # This is gross, but in a real ILS it would be better
409 foreach my $i (@{$patron->{items}}) {
410 $item = new ILS::Item $i;
411 last if ($title_id eq $item->title_id);
415 foreach my $i (@{$patron->{items}}) {
416 if ($i == $item_id) {
417 # We have it checked out
418 $item = new ILS::Item $item_id;
426 if (!defined($item)) {
427 # It's not checked out to $patron_id
428 $trans->screen_msg("Item not checked out to " . $patron->name);
429 } elsif (!$item->available($patron_id)) {
430 $trans->screen_msg("Item has outstanding holds");
432 $trans->renewal_ok(1);
434 $trans->desensitize(0); # It's already checked out
436 if ($no_block eq 'Y') {
437 $item->{due_date} = $nb_due_date;
439 $item->{due_date} = time + (14*24*60*60); # two weeks
442 $item->{sip_item_properties} = $item_props;
445 $trans->renewal_ok(1);
454 my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
455 my ($patron, $item_id);
458 $trans = new ILS::Transaction::RenewAll;
460 $trans->patron($patron = new ILS::Patron $patron_id);
461 if (defined $patron) {
462 syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s",
463 $patron->name, $patron->renew_ok);
465 syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'",
469 if (!defined($patron)) {
470 $trans->screen_msg("Invalid patron barcode.");
472 } elsif (!$patron->renew_ok) {
473 $trans->screen_msg("Renewals not allowed.");
475 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
476 $trans->screen_msg("Invalid patron password.");
480 foreach $item_id (@{$patron->{items}}) {
481 my $item = new ILS::Item $item_id;
483 if (!defined($item)) {
484 syslog("LOG_WARNING",
485 "renew_all: Invalid item id associated with patron '%s'",
490 if (@{$item->hold_queue}) {
491 # Can't renew if there are outstanding holds
492 push @{$trans->unrenewed}, $item_id;
494 $item->{due_date} = time + (14*24*60*60); # two weeks hence
495 push @{$trans->renewed}, $item_id;