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;
24 'magnetic media' => 1,
25 'security inhibit' => 0,
26 'offline operation' => 0,
27 "patron status request" => 1,
33 "patron information" => 1,
34 "end patron session" => 1,
36 "item information" => 1,
37 "item status update" => 0,
45 my ($class, $institution) = @_;
46 my $type = ref($class) || $class;
49 $debug and warn "new ILS: INSTITUTION: " . Dumper($institution);
50 syslog("LOG_DEBUG", "new ILS '%s'", $institution->{id});
51 $self->{institution} = $institution;
52 return bless $self, $type;
57 $debug and warn "ILS: finding patron";
58 return ILS::Patron->new(@_);
63 $debug and warn "ILS: finding item";
64 return ILS::Item->new(@_);
69 return $self->{institution}->{id};
74 return (exists($supports{$op}) && $supports{$op});
78 my ($self, $id, $whence) = @_;
79 if ($id ne $self->{institution}->{id}) {
80 syslog("LOG_WARNING", "%s: received institution '%s', expected '%s'",
81 $whence, $id, $self->{institution}->{id});
87 # If it's defined, and matches a true sort of string, or is
88 # a non-zero number, then it's true.
89 defined($bool) or return; # false
90 ($bool =~ /true|y|yes/i) and return 1; # true
91 return ($bool =~ /^\d+$/ and $bool != 0); # true for non-zero numbers, false otherwise
96 return (exists($self->{institution}->{policy}->{checkout})
97 && to_bool($self->{institution}->{policy}->{checkout}));
101 return (exists($self->{institution}->{policy}->{checkin})
102 && to_bool($self->{institution}->{policy}->{checkin}));
104 sub status_update_ok {
106 return (exists($self->{institution}->{policy}->{status_update})
107 && to_bool($self->{institution}->{policy}->{status_update}));
111 return (exists($self->{institution}->{policy}->{offline})
112 && to_bool($self->{institution}->{policy}->{offline}));
116 # Checkout(patron_id, item_id, sc_renew):
117 # patron_id & item_id are the identifiers send by the terminal
118 # sc_renew is the renewal policy configured on the terminal
119 # returns a status opject that can be queried for the various bits
120 # of information that the protocol (SIP or NCIP) needs to generate
124 my ($self, $patron_id, $item_id, $sc_renew) = @_;
125 my ($patron, $item, $circ);
127 $circ = new ILS::Transaction::Checkout;
129 $circ->patron($patron = new ILS::Patron $patron_id);
130 $circ->item($item = new ILS::Item $item_id);
133 $circ->screen_msg("Invalid Patron");
134 } elsif (!$patron->charge_ok) {
135 $circ->screen_msg("Patron Blocked");
137 $circ->screen_msg("Invalid Item");
138 # holds checked inside do_checkout
139 # } elsif ($item->hold_queue && @{$item->hold_queue} && ! $item->barcode_is_borrowernumber($patron_id, $item->hold_queue->[0]->{borrowernumber})) {
140 # $circ->screen_msg("Item on Hold for Another User");
141 } elsif ($item->{patron} && ($item->{patron} ne $patron_id)) {
142 # I can't deal with this right now
143 $circ->screen_msg("Item checked out to another patron");
145 $circ->do_checkout();
147 $debug and warn "circ is ok";
148 # If the item is already associated with this patron, then
150 $circ->renew_ok($item->{patron} && ($item->{patron} eq $patron_id));
152 $item->{patron} = $patron_id;
153 $item->{due_date} = $circ->{due};
154 push(@{$patron->{items}}, $item_id);
155 $circ->desensitize(!$item->magnetic);
157 syslog("LOG_DEBUG", "ILS::Checkout: patron %s has checked out %s",
158 $patron_id, join(', ', @{$patron->{items}}));
161 syslog("LOG_ERR", "ILS::Checkout Issue failed");
170 my ($self, $item_id, $trans_date, $return_date,
171 $current_loc, $item_props, $cancel) = @_;
172 my ($patron, $item, $circ);
174 $circ = new ILS::Transaction::Checkin;
176 $circ->item($item = new ILS::Item $item_id);
179 # It's ok to check it in if it exists, and if it was checked out
180 $circ->ok($item && $item->{patron});
183 $circ->patron($patron = new ILS::Patron $item->{patron});
184 delete $item->{patron};
185 delete $item->{due_date};
186 $patron->{items} = [ grep {$_ ne $item_id} @{$patron->{items}} ];
193 # If the ILS caches patron information, this lets it free
195 sub end_patron_session {
196 my ($self, $patron_id) = @_;
198 # success?, screen_msg, print_line
199 return (1, 'Thank you !', '');
203 my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
204 $pay_type, $fee_id, $trans_id, $currency) = @_;
208 # $trans = new ILS::Transaction::FeePayment;
210 $patron = new ILS::Patron $patron_id;
212 $trans->transaction_id($trans_id);
213 $trans->patron($patron);
220 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
221 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
224 my $trans = new ILS::Transaction::Hold;
226 $patron = new ILS::Patron $patron_id;
228 || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
229 $trans->screen_msg("Invalid Patron.");
233 unless ($item = new ILS::Item ($item_id || $title_id)) {
234 $trans->screen_msg("No such item.");
238 if ($item->fee and $fee_ack ne 'Y') {
239 $trans->screen_msg = "Fee required to place hold.";
244 item_id => $item->id,
245 patron_id => $patron->id,
246 expiration_date => $expiry_date,
247 pickup_location => $pickup_location,
248 hold_type => $hold_type,
252 $trans->patron($patron);
254 $trans->pickup_location($pickup_location);
257 push(@{$item->hold_queue}, $hold);
258 push(@{$patron->{hold_items}}, $hold);
264 my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
265 my ($patron, $item, $hold);
267 my $trans = new ILS::Transaction::Hold;
269 $patron = new ILS::Patron $patron_id;
271 $trans->screen_msg("Invalid patron barcode.");
273 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
274 $trans->screen_msg('Invalid patron password.');
278 unless ($item = new ILS::Item ($item_id || $title_id)) {
279 $trans->screen_msg("No such item.");
283 $trans->patron($patron);
286 unless ($trans->ok) {
287 $trans->screen_msg("Error with transaction drop_hold: " . $trans->screen_msg);
290 # Remove the hold from the patron's record first
291 $trans->ok($patron->drop_hold($item_id)); # different than the transaction drop!
293 unless ($trans->ok) {
294 # We didn't find it on the patron record
295 $trans->screen_msg("No such hold on patron record.");
299 # Now, remove it from the item record. If it was on the patron
300 # record but not on the item record, we'll treat that as success.
301 foreach my $i (0 .. scalar @{$item->hold_queue}) {
302 $hold = $item->hold_queue->[$i];
303 if ($item->barcode_is_borrowernumber($patron->id, $hold->{borrowernumber})) {
304 # found it: delete it.
305 splice @{$item->hold_queue}, $i, 1;
306 last; # ?? should we keep going, in case there are multiples
310 $trans->screen_msg("Hold Cancelled.");
316 # The patron and item id's can't be altered, but the
317 # date, location, and type can.
319 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
320 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
325 $trans = new ILS::Transaction::Hold;
328 $patron = new ILS::Patron $patron_id;
330 $trans->screen_msg("Invalid patron barcode: '$patron_id'.");
334 foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
335 $hold = $patron->{hold_items}[$i];
337 if ($hold->{item_id} eq $item_id) {
338 # Found it. So fix it.
339 $hold->{expiration_date} = $expiry_date if $expiry_date;
340 $hold->{pickup_location} = $pickup_location if $pickup_location;
341 $hold->{hold_type} = $hold_type if $hold_type;
342 $trans->change_hold();
344 $trans->screen_msg("Hold updated.");
345 $trans->patron($patron);
346 $trans->item(new ILS::Item $hold->{item_id});
351 # The same hold structure is linked into both the patron's
352 # list of hold items and into the queue of outstanding holds
353 # for the item, so we don't need to search the hold queue for
354 # the item, since it's already been updated by the patron code.
357 $trans->screen_msg("No such outstanding hold.");
364 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
365 $no_block, $nb_due_date, $third_party,
366 $item_props, $fee_ack) = @_;
370 $trans = new ILS::Transaction::Renew;
371 $trans->patron($patron = new ILS::Patron $patron_id);
374 $trans->screen_msg("Invalid patron barcode.");
376 } elsif (!$patron->renew_ok) {
377 $trans->screen_msg("Renewals not allowed.");
381 # Previously: renewing a title, rather than an item (sort of)
382 # This is gross, but in a real ILS it would be better
384 # if (defined($title_id)) {
385 # foreach my $i (@{$patron->{items}}) {
386 # $item = new ILS::Item $i;
387 # last if ($title_id eq $item->title_id);
392 my $count = scalar @{$patron->{items}};
393 foreach my $i (@{$patron->{items}}) {
394 syslog("LOG_DEBUG", "checking item %s of %s: $item_id vs. %s", ++$j, $count, $i);
395 if ($i eq $item_id) {
396 # We have it checked out
397 $item = new ILS::Item $item_id;
405 if (!defined($item)) {
406 # It's not checked out to $patron_id
407 $trans->screen_msg("Item not checked out to " . $patron->name);
408 } elsif (!$item->available($patron_id)) {
409 $trans->screen_msg("Item unavailable due to outstanding holds");
411 $trans->renewal_ok(1);
412 $trans->desensitize(0); # It's already checked out
414 syslog("LOG_DEBUG", "done renew (%s): %s renews %s", $trans->renewal_ok(1),$patron_id,$item_id);
416 # if ($no_block eq 'Y') {
417 # $item->{due_date} = $nb_due_date;
419 # $item->{due_date} = time + (14*24*60*60); # two weeks
422 # $item->{sip_item_properties} = $item_props;
432 my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
433 my ($patron, $item_id);
436 $trans = new ILS::Transaction::RenewAll;
438 $trans->patron($patron = new ILS::Patron $patron_id);
439 if (defined $patron) {
440 syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s",
441 $patron->name, $patron->renew_ok);
443 syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'",
447 if (!defined($patron)) {
448 $trans->screen_msg("Invalid patron barcode.");
450 } elsif (!$patron->renew_ok) {
451 $trans->screen_msg("Renewals not allowed.");
453 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
454 $trans->screen_msg("Invalid patron password.");
458 $trans->do_renew_all;