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 # holds checked inside do_checkout
142 # } elsif ($item->hold_queue && @{$item->hold_queue} && ! $item->barcode_is_borrowernumber($patron_id, $item->hold_queue->[0]->{borrowernumber})) {
143 # $circ->screen_msg("Item on Hold for Another User");
144 } elsif ($item->{patron} && ($item->{patron} ne $patron_id)) {
145 # I can't deal with this right now
146 $circ->screen_msg("Item checked out to another patron");
148 $circ->do_checkout();
150 $debug and warn "circ is ok";
151 # If the item is already associated with this patron, then
153 $circ->renew_ok($item->{patron} && ($item->{patron} eq $patron_id));
155 $item->{patron} = $patron_id;
156 $item->{due_date} = $circ->{due};
157 push(@{$patron->{items}}, $item_id);
158 $circ->desensitize(!$item->magnetic);
160 syslog("LOG_DEBUG", "ILS::Checkout: patron %s has checked out %s",
161 $patron_id, join(', ', @{$patron->{items}}));
164 syslog("LOG_ERR", "ILS::Checkout Issue failed");
173 my ($self, $item_id, $trans_date, $return_date,
174 $current_loc, $item_props, $cancel) = @_;
175 my ($patron, $item, $circ);
177 $circ = new ILS::Transaction::Checkin;
179 $circ->item($item = new ILS::Item $item_id);
182 # It's ok to check it in if it exists, and if it was checked out
183 $circ->ok($item && $item->{patron});
186 $circ->patron($patron = new ILS::Patron $item->{patron});
187 delete $item->{patron};
188 delete $item->{due_date};
189 $patron->{items} = [ grep {$_ ne $item_id} @{$patron->{items}} ];
196 # If the ILS caches patron information, this lets it free
198 sub end_patron_session {
199 my ($self, $patron_id) = @_;
201 # success?, screen_msg, print_line
202 return (1, 'Thank you !', '');
206 my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
207 $pay_type, $fee_id, $trans_id, $currency) = @_;
211 # $trans = new ILS::Transaction::FeePayment;
213 $patron = new ILS::Patron $patron_id;
215 $trans->transaction_id($trans_id);
216 $trans->patron($patron);
223 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
224 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
227 my $trans = new ILS::Transaction::Hold;
229 $patron = new ILS::Patron $patron_id;
231 || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
232 $trans->screen_msg("Invalid Patron.");
236 unless ($item = new ILS::Item ($item_id || $title_id)) {
237 $trans->screen_msg("No such item.");
241 if ($item->fee and $fee_ack ne 'Y') {
242 $trans->screen_msg = "Fee required to place hold.";
247 item_id => $item->id,
248 patron_id => $patron->id,
249 expiration_date => $expiry_date,
250 pickup_location => $pickup_location,
251 hold_type => $hold_type,
255 $trans->patron($patron);
257 $trans->pickup_location($pickup_location);
260 push(@{$item->hold_queue}, $hold);
261 push(@{$patron->{hold_items}}, $hold);
267 my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
268 my ($patron, $item, $hold);
270 my $trans = new ILS::Transaction::Hold;
272 $patron = new ILS::Patron $patron_id;
274 $trans->screen_msg("Invalid patron barcode.");
276 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
277 $trans->screen_msg('Invalid patron password.');
281 unless ($item = new ILS::Item ($item_id || $title_id)) {
282 $trans->screen_msg("No such item.");
286 $trans->patron($patron);
289 unless ($trans->ok) {
290 $trans->screen_msg("Error with transaction drop_hold: " . $trans->screen_msg);
293 # Remove the hold from the patron's record first
294 $trans->ok($patron->drop_hold($item_id)); # different than the transaction drop!
296 unless ($trans->ok) {
297 # We didn't find it on the patron record
298 $trans->screen_msg("No such hold on patron record.");
302 # Now, remove it from the item record. If it was on the patron
303 # record but not on the item record, we'll treat that as success.
304 foreach my $i (0 .. scalar @{$item->hold_queue}) {
305 $hold = $item->hold_queue->[$i];
306 if ($item->barcode_is_borrowernumber($patron->id, $hold->{borrowernumber})) {
307 # found it: delete it.
308 splice @{$item->hold_queue}, $i, 1;
309 last; # ?? should we keep going, in case there are multiples
313 $trans->screen_msg("Hold Cancelled.");
319 # The patron and item id's can't be altered, but the
320 # date, location, and type can.
322 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
323 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
328 $trans = new ILS::Transaction::Hold;
331 $patron = new ILS::Patron $patron_id;
333 $trans->screen_msg("Invalid patron barcode: '$patron_id'.");
337 foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
338 $hold = $patron->{hold_items}[$i];
340 if ($hold->{item_id} eq $item_id) {
341 # Found it. So fix it.
342 $hold->{expiration_date} = $expiry_date if $expiry_date;
343 $hold->{pickup_location} = $pickup_location if $pickup_location;
344 $hold->{hold_type} = $hold_type if $hold_type;
345 $trans->change_hold();
347 $trans->screen_msg("Hold updated.");
348 $trans->patron($patron);
349 $trans->item(new ILS::Item $hold->{item_id});
354 # The same hold structure is linked into both the patron's
355 # list of hold items and into the queue of outstanding holds
356 # for the item, so we don't need to search the hold queue for
357 # the item, since it's already been updated by the patron code.
360 $trans->screen_msg("No such outstanding hold.");
367 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
368 $no_block, $nb_due_date, $third_party,
369 $item_props, $fee_ack) = @_;
373 $trans = new ILS::Transaction::Renew;
374 $trans->patron($patron = new ILS::Patron $patron_id);
377 $trans->screen_msg("Invalid patron barcode.");
379 } elsif (!$patron->renew_ok) {
380 $trans->screen_msg("Renewals not allowed.");
384 # Previously: renewing a title, rather than an item (sort of)
385 # This is gross, but in a real ILS it would be better
387 # if (defined($title_id)) {
388 # foreach my $i (@{$patron->{items}}) {
389 # $item = new ILS::Item $i;
390 # last if ($title_id eq $item->title_id);
395 my $count = scalar @{$patron->{items}};
396 foreach my $i (@{$patron->{items}}) {
397 syslog("LOG_DEBUG", "checking item %s of %s: $item_id vs. %s", ++$j, $count, $i);
398 if ($i eq $item_id) {
399 # We have it checked out
400 $item = new ILS::Item $item_id;
408 if (!defined($item)) {
409 # It's not checked out to $patron_id
410 $trans->screen_msg("Item not checked out to " . $patron->name);
411 } elsif (!$item->available($patron_id)) {
412 $trans->screen_msg("Item unavailable due to outstanding holds");
414 $trans->renewal_ok(1);
415 $trans->desensitize(0); # It's already checked out
417 syslog("LOG_DEBUG", "done renew (%s): %s renews %s", $trans->renewal_ok(1),$patron_id,$item_id);
419 # if ($no_block eq 'Y') {
420 # $item->{due_date} = $nb_due_date;
422 # $item->{due_date} = time + (14*24*60*60); # two weeks
425 # $item->{sip_item_properties} = $item_props;
435 my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
436 my ($patron, $item_id);
439 $trans = new ILS::Transaction::RenewAll;
441 $trans->patron($patron = new ILS::Patron $patron_id);
442 if (defined $patron) {
443 syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s",
444 $patron->name, $patron->renew_ok);
446 syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'",
450 if (!defined($patron)) {
451 $trans->screen_msg("Invalid patron barcode.");
453 } elsif (!$patron->renew_ok) {
454 $trans->screen_msg("Renewals not allowed.");
456 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
457 $trans->screen_msg("Invalid patron password.");
461 $trans->do_renew_all;