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 modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 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(', ', encode_utf8(@{$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) = @_;
224 my $trans = ILS::Transaction::FeePayment->new();
225 my $patron = ILS::Patron->new($patron_id);
227 $trans->transaction_id($trans_id);
228 $trans->patron($patron);
235 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
236 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
239 my $trans = new ILS::Transaction::Hold;
240 my $patron = new ILS::Patron $patron_id;
243 || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
244 $trans->screen_msg("Invalid Patron.");
248 my $item = new ILS::Item ($item_id || $title_id);
250 $trans->screen_msg("No such item.");
252 } elsif ($item->fee && ($fee_ack ne 'Y')) {
253 $trans->screen_msg = "Fee required to place hold.";
258 item_id => $item->id,
259 patron_id => $patron->id,
260 expiration_date => $expiry_date,
261 pickup_location => $pickup_location,
262 hold_type => $hold_type,
266 $trans->patron($patron);
268 $trans->pickup_location($pickup_location);
270 push(@{$item->hold_queue}, $hold);
271 push(@{$patron->{hold_items}}, $hold);
279 my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
280 my ($patron, $item, $hold);
283 $trans = new ILS::Transaction::Hold;
286 $patron = new ILS::Patron $patron_id;
288 $trans->screen_msg("Invalid patron barcode.");
291 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
292 $trans->screen_msg('Invalid patron password.');
297 $item = new ILS::Item ($item_id || $title_id);
299 $trans->screen_msg("No such item.");
301 # END TRANSACTION (conditionally)
305 # Remove the hold from the patron's record first
306 $trans->ok($patron->drop_hold($item_id));
309 # We didn't find it on the patron record
310 $trans->screen_msg("No such hold on patron record.");
312 # END TRANSACTION (conditionally)
316 # Now, remove it from the item record. If it was on the patron
317 # record but not on the item record, we'll treat that as success.
318 foreach my $i (0 .. scalar @{$item->hold_queue}) {
319 $hold = $item->hold_queue->[$i];
321 if ($hold->{patron_id} eq $patron->id) {
322 # found it: delete it.
323 splice @{$item->hold_queue}, $i, 1;
328 $trans->screen_msg("Hold Cancelled.");
329 $trans->patron($patron);
336 # The patron and item id's can't be altered, but the
337 # date, location, and type can.
339 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
340 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
345 $trans = new ILS::Transaction::Hold;
348 $patron = new ILS::Patron $patron_id;
350 $trans->screen_msg("Invalid patron barcode.");
355 foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
356 $hold = $patron->{hold_items}[$i];
358 if ($hold->{item_id} eq $item_id) {
359 # Found it. So fix it.
360 $hold->{expiration_date} = $expiry_date if $expiry_date;
361 $hold->{pickup_location} = $pickup_location if $pickup_location;
362 $hold->{hold_type} = $hold_type if $hold_type;
365 $trans->screen_msg("Hold updated.");
366 $trans->patron($patron);
367 $trans->item(new ILS::Item $hold->{item_id});
372 # The same hold structure is linked into both the patron's
373 # list of hold items and into the queue of outstanding holds
374 # for the item, so we don't need to search the hold queue for
375 # the item, since it's already been updated by the patron code.
378 $trans->screen_msg("No such outstanding hold.");
385 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
386 $no_block, $nb_due_date, $third_party,
387 $item_props, $fee_ack) = @_;
391 $trans = new ILS::Transaction::Renew;
393 $trans->patron($patron = new ILS::Patron $patron_id);
396 $trans->screen_msg("Invalid patron barcode.");
399 } elsif (!$patron->renew_ok) {
400 $trans->screen_msg("Renewals not allowed.");
405 if (defined($title_id)) {
406 # renewing a title, rather than an item (sort of)
407 # This is gross, but in a real ILS it would be better
408 foreach my $i (@{$patron->{items}}) {
409 $item = new ILS::Item $i;
410 last if ($title_id eq $item->title_id);
414 foreach my $i (@{$patron->{items}}) {
415 if ($i == $item_id) {
416 # We have it checked out
417 $item = new ILS::Item $item_id;
425 if (!defined($item)) {
426 # It's not checked out to $patron_id
427 $trans->screen_msg("Item not checked out to " . $patron->name);
428 } elsif (!$item->available($patron_id)) {
429 $trans->screen_msg("Item has outstanding holds");
431 $trans->renewal_ok(1);
433 $trans->desensitize(0); # It's already checked out
435 if ($no_block eq 'Y') {
436 $item->{due_date} = $nb_due_date;
438 $item->{due_date} = time + (14*24*60*60); # two weeks
441 $item->{sip_item_properties} = $item_props;
444 $trans->renewal_ok(1);
453 my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
454 my ($patron, $item_id);
457 $trans = new ILS::Transaction::RenewAll;
459 $trans->patron($patron = new ILS::Patron $patron_id);
460 if (defined $patron) {
461 syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s",
462 $patron->name, $patron->renew_ok);
464 syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'",
468 if (!defined($patron)) {
469 $trans->screen_msg("Invalid patron barcode.");
471 } elsif (!$patron->renew_ok) {
472 $trans->screen_msg("Renewals not allowed.");
474 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
475 $trans->screen_msg("Invalid patron password.");
479 foreach $item_id (@{$patron->{items}}) {
480 my $item = new ILS::Item $item_id;
482 if (!defined($item)) {
483 syslog("LOG_WARNING",
484 "renew_all: Invalid item id associated with patron '%s'",
489 if (@{$item->hold_queue}) {
490 # Can't renew if there are outstanding holds
491 push @{$trans->unrenewed}, $item_id;
493 $item->{due_date} = time + (14*24*60*60); # two weeks hence
494 push @{$trans->renewed}, $item_id;