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.
90 # FIXME: this test is faulty
91 # We're running under warnings and strict.
92 # But if we don't match regexp, then we go ahead and numerical compare?
93 # That means we'd generate a warning on 'FALSE' or ''.
94 return defined($bool) && (($bool =~ /true|y|yes/i) || $bool != 0);
99 return (exists($self->{policy}->{checkout})
100 && to_bool($self->{policy}->{checkout}));
104 return (exists($self->{policy}->{checkin})
105 && to_bool($self->{policy}->{checkin}));
107 sub status_update_ok {
109 return (exists($self->{policy}->{status_update})
110 && to_bool($self->{policy}->{status_update}));
114 return (exists($self->{policy}->{offline})
115 && to_bool($self->{policy}->{offline}));
119 # Checkout(patron_id, item_id, sc_renew):
120 # patron_id & item_id are the identifiers send by the terminal
121 # sc_renew is the renewal policy configured on the terminal
122 # returns a status opject that can be queried for the various bits
123 # of information that the protocol (SIP or NCIP) needs to generate
127 my ($self, $patron_id, $item_id, $sc_renew) = @_;
128 my ($patron, $item, $circ);
130 $circ = new ILS::Transaction::Checkout;
132 $circ->patron($patron = new ILS::Patron $patron_id);
133 $circ->item($item = new ILS::Item $item_id);
136 $circ->screen_msg("Invalid Patron");
137 } elsif (!$patron->charge_ok) {
138 $circ->screen_msg("Patron Blocked");
140 $circ->screen_msg("Invalid Item");
141 } elsif ($item->hold_queue && @{$item->hold_queue} && ! $item->barcode_is_borrowernumber($patron_id, $item->hold_queue->[0]->{borrowernumber})) {
142 $circ->screen_msg("Item on Hold for Another User");
143 } elsif ($item->{patron} && ($item->{patron} ne $patron_id)) {
144 # I can't deal with this right now
145 $circ->screen_msg("Item checked out to another patron");
147 $circ->do_checkout();
149 $debug and warn "circ is ok";
150 # If the item is already associated with this patron, then
152 $circ->renew_ok($item->{patron} && ($item->{patron} eq $patron_id));
154 $item->{patron} = $patron_id;
155 $item->{due_date} = $circ->{due};
156 push(@{$patron->{items}}, $item_id);
157 $circ->desensitize(!$item->magnetic);
159 syslog("LOG_DEBUG", "ILS::Checkout: patron %s has checked out %s",
160 $patron_id, join(', ', @{$patron->{items}}));
163 syslog("LOG_ERR", "ILS::Checkout Issue failed");
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);
181 # It's ok to check it in if it exists, and if it was checked out
182 $circ->ok($item && $item->{patron});
185 $circ->patron($patron = new ILS::Patron $item->{patron});
186 delete $item->{patron};
187 delete $item->{due_date};
188 $patron->{items} = [ grep {$_ ne $item_id} @{$patron->{items}} ];
195 # If the ILS caches patron information, this lets it free
197 sub end_patron_session {
198 my ($self, $patron_id) = @_;
200 # success?, screen_msg, print_line
201 return (1, 'Thank you !', '');
205 my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
206 $pay_type, $fee_id, $trans_id, $currency) = @_;
210 # $trans = new ILS::Transaction::FeePayment;
212 $patron = new ILS::Patron $patron_id;
214 $trans->transaction_id($trans_id);
215 $trans->patron($patron);
222 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
223 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
226 my $trans = new ILS::Transaction::Hold;
228 $patron = new ILS::Patron $patron_id;
230 || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
231 $trans->screen_msg("Invalid Patron.");
235 unless ($item = new ILS::Item ($item_id || $title_id)) {
236 $trans->screen_msg("No such item.");
240 if ($item->fee and $fee_ack ne 'Y') {
241 $trans->screen_msg = "Fee required to place hold.";
246 item_id => $item->id,
247 patron_id => $patron->id,
248 expiration_date => $expiry_date,
249 pickup_location => $pickup_location,
250 hold_type => $hold_type,
254 $trans->patron($patron);
256 $trans->pickup_location($pickup_location);
259 push(@{$item->hold_queue}, $hold);
260 push(@{$patron->{hold_items}}, $hold);
266 my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
267 my ($patron, $item, $hold);
269 my $trans = new ILS::Transaction::Hold;
271 $patron = new ILS::Patron $patron_id;
273 $trans->screen_msg("Invalid patron barcode.");
275 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
276 $trans->screen_msg('Invalid patron password.');
280 unless ($item = new ILS::Item ($item_id || $title_id)) {
281 $trans->screen_msg("No such item.");
285 $trans->patron($patron);
288 unless ($trans->ok) {
289 $trans->screen_msg("Error with transaction drop_hold: " . $trans->screen_msg);
292 # Remove the hold from the patron's record first
293 $trans->ok($patron->drop_hold($item_id)); # different than the transaction drop!
295 unless ($trans->ok) {
296 # We didn't find it on the patron record
297 $trans->screen_msg("No such hold on patron record.");
301 # Now, remove it from the item record. If it was on the patron
302 # record but not on the item record, we'll treat that as success.
303 foreach my $i (0 .. scalar @{$item->hold_queue}) {
304 $hold = $item->hold_queue->[$i];
305 if ($item->barcode_is_borrowernumber($patron->id, $hold->{borrowernumber})) {
306 # found it: delete it.
307 splice @{$item->hold_queue}, $i, 1;
308 last; # ?? should we keep going, in case there are multiples
312 $trans->screen_msg("Hold Cancelled.");
318 # The patron and item id's can't be altered, but the
319 # date, location, and type can.
321 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
322 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
327 $trans = new ILS::Transaction::Hold;
330 $patron = new ILS::Patron $patron_id;
332 $trans->screen_msg("Invalid patron barcode: '$patron_id'.");
336 foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
337 $hold = $patron->{hold_items}[$i];
339 if ($hold->{item_id} eq $item_id) {
340 # Found it. So fix it.
341 $hold->{expiration_date} = $expiry_date if $expiry_date;
342 $hold->{pickup_location} = $pickup_location if $pickup_location;
343 $hold->{hold_type} = $hold_type if $hold_type;
344 $trans->change_hold();
346 $trans->screen_msg("Hold updated.");
347 $trans->patron($patron);
348 $trans->item(new ILS::Item $hold->{item_id});
353 # The same hold structure is linked into both the patron's
354 # list of hold items and into the queue of outstanding holds
355 # for the item, so we don't need to search the hold queue for
356 # the item, since it's already been updated by the patron code.
359 $trans->screen_msg("No such outstanding hold.");
366 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
367 $no_block, $nb_due_date, $third_party,
368 $item_props, $fee_ack) = @_;
372 $trans = new ILS::Transaction::Renew;
373 $trans->patron($patron = new ILS::Patron $patron_id);
376 $trans->screen_msg("Invalid patron barcode.");
378 } elsif (!$patron->renew_ok) {
379 $trans->screen_msg("Renewals not allowed.");
383 # Previously: renewing a title, rather than an item (sort of)
384 # This is gross, but in a real ILS it would be better
386 # if (defined($title_id)) {
387 # foreach my $i (@{$patron->{items}}) {
388 # $item = new ILS::Item $i;
389 # last if ($title_id eq $item->title_id);
394 my $count = scalar @{$patron->{items}};
395 foreach my $i (@{$patron->{items}}) {
396 syslog("LOG_DEBUG", "checking item %s of %s: $item_id vs. %s", ++$j, $count, $i);
397 if ($i eq $item_id) {
398 # We have it checked out
399 $item = new ILS::Item $item_id;
407 if (!defined($item)) {
408 # It's not checked out to $patron_id
409 $trans->screen_msg("Item not checked out to " . $patron->name);
410 } elsif (!$item->available($patron_id)) {
411 $trans->screen_msg("Item unavailable due to outstanding holds");
413 $trans->renewal_ok(1);
414 $trans->desensitize(0); # It's already checked out
416 syslog("LOG_DEBUG", "done renew (%s): %s renews %s", $trans->renewal_ok(1),$patron_id,$item_id);
418 # if ($no_block eq 'Y') {
419 # $item->{due_date} = $nb_due_date;
421 # $item->{due_date} = time + (14*24*60*60); # two weeks
424 # $item->{sip_item_properties} = $item_props;
434 my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
435 my ($patron, $item_id);
438 $trans = new ILS::Transaction::RenewAll;
440 $trans->patron($patron = new ILS::Patron $patron_id);
441 if (defined $patron) {
442 syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s",
443 $patron->name, $patron->renew_ok);
445 syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'",
449 if (!defined($patron)) {
450 $trans->screen_msg("Invalid patron barcode.");
452 } elsif (!$patron->renew_ok) {
453 $trans->screen_msg("Renewals not allowed.");
455 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
456 $trans->screen_msg("Invalid patron password.");
460 $trans->do_renew_all;