Bug 13411: (QA followup) Add logging
[koha.git] / C4 / SIP / ILS.pm
blob7d702b79fa6da4aea3a3f5aade4f1db54b977c77
2 # ILS.pm: Koha ILS interface module
5 package C4::SIP::ILS;
7 use warnings;
8 use strict;
9 use Sys::Syslog qw(syslog);
10 use Data::Dumper;
12 use C4::SIP::ILS::Item;
13 use C4::SIP::ILS::Patron;
14 use C4::SIP::ILS::Transaction;
15 use C4::SIP::ILS::Transaction::Checkout;
16 use C4::SIP::ILS::Transaction::Checkin;
17 use C4::SIP::ILS::Transaction::FeePayment;
18 use C4::SIP::ILS::Transaction::Hold;
19 use C4::SIP::ILS::Transaction::Renew;
20 use C4::SIP::ILS::Transaction::RenewAll;
22 my $debug = 0;
24 my %supports = (
25 'magnetic media' => 1,
26 'security inhibit' => 0,
27 'offline operation' => 0,
28 "patron status request" => 1,
29 "checkout" => 1,
30 "checkin" => 1,
31 "block patron" => 1,
32 "acs status" => 1,
33 "login" => 1,
34 "patron information" => 1,
35 "end patron session" => 1,
36 "fee paid" => 1,
37 "item information" => 1,
38 "item status update" => 0,
39 "patron enable" => 1,
40 "hold" => 1,
41 "renew" => 1,
42 "renew all" => 1,
45 sub new {
46 my ($class, $institution) = @_;
47 my $type = ref($class) || $class;
48 my $self = {};
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;
55 sub find_patron {
56 my $self = shift;
57 $debug and warn "ILS: finding patron";
58 return C4::SIP::ILS::Patron->new(@_);
61 sub find_item {
62 my $self = shift;
63 $debug and warn "ILS: finding item";
64 return C4::SIP::ILS::Item->new(@_);
67 sub institution {
68 my $self = shift;
69 return $self->{institution}->{id}; # consider making this return the whole institution
72 sub institution_id {
73 my $self = shift;
74 return $self->{institution}->{id};
77 sub supports {
78 my ($self, $op) = @_;
79 return (exists($supports{$op}) && $supports{$op});
82 sub check_inst_id {
83 my ($self, $id, $whence) = @_;
84 if ($id ne $self->{institution}->{id}) {
85 syslog("LOG_WARNING", "%s: received institution '%s', expected '%s'", $whence, $id, $self->{institution}->{id});
86 # Just an FYI check, we don't expect the user to change location from that in SIPconfig.xml
90 sub to_bool {
91 my $bool = shift;
92 # If it's defined, and matches a true sort of string, or is
93 # a non-zero number, then it's true.
94 defined($bool) or return; # false
95 ($bool =~ /true|y|yes/i) and return 1; # true
96 return ($bool =~ /^\d+$/ and $bool != 0); # true for non-zero numbers, false otherwise
99 sub checkout_ok {
100 my $self = shift;
101 return (exists($self->{institution}->{policy}->{checkout})
102 && to_bool($self->{institution}->{policy}->{checkout}));
104 sub checkin_ok {
105 my $self = shift;
106 return (exists($self->{institution}->{policy}->{checkin})
107 && to_bool($self->{institution}->{policy}->{checkin}));
109 sub status_update_ok {
110 my $self = shift;
111 return (exists($self->{institution}->{policy}->{status_update})
112 && to_bool($self->{institution}->{policy}->{status_update}));
114 sub offline_ok {
115 my $self = shift;
116 return (exists($self->{institution}->{policy}->{offline})
117 && to_bool($self->{institution}->{policy}->{offline}));
121 # Checkout(patron_id, item_id, sc_renew):
122 # patron_id & item_id are the identifiers send by the terminal
123 # sc_renew is the renewal policy configured on the terminal
124 # returns a status opject that can be queried for the various bits
125 # of information that the protocol (SIP or NCIP) needs to generate
126 # the response.
128 sub checkout {
129 my ($self, $patron_id, $item_id, $sc_renew, $fee_ack) = @_;
130 my ($patron, $item, $circ);
132 $circ = C4::SIP::ILS::Transaction::Checkout->new();
133 # BEGIN TRANSACTION
134 $circ->patron($patron = C4::SIP::ILS::Patron->new( $patron_id));
135 $circ->item($item = C4::SIP::ILS::Item->new( $item_id));
136 if ($fee_ack) {
137 $circ->fee_ack($fee_ack);
140 if (!$patron) {
141 $circ->screen_msg("Invalid Patron");
142 } elsif (!$patron->charge_ok) {
143 $circ->screen_msg("Patron Blocked");
144 } elsif (!$item) {
145 $circ->screen_msg("Invalid Item");
146 # holds checked inside do_checkout
147 # } elsif ($item->hold_queue && @{$item->hold_queue} && ! $item->barcode_is_borrowernumber($patron_id, $item->hold_queue->[0]->{borrowernumber})) {
148 # $circ->screen_msg("Item on Hold for Another User");
149 } elsif ($item->{patron} && ($item->{patron} ne $patron_id)) {
150 # I can't deal with this right now
151 $circ->screen_msg("Item checked out to another patron");
152 } else {
153 $circ->do_checkout();
154 if ($circ->ok){
155 $debug and warn "circ is ok";
156 # If the item is already associated with this patron, then
157 # we're renewing it.
158 $circ->renew_ok($item->{patron} && ($item->{patron} eq $patron_id));
160 $item->{patron} = $patron_id;
161 $item->{due_date} = $circ->{due};
162 push(@{$patron->{items}}, $item_id);
163 $circ->desensitize(!$item->magnetic_media);
165 syslog("LOG_DEBUG", "ILS::Checkout: patron %s has checked out %s",
166 $patron_id, join(', ', @{$patron->{items}}));
168 else {
169 syslog("LOG_ERR", "ILS::Checkout Issue failed");
172 # END TRANSACTION
174 return $circ;
177 sub checkin {
178 my ( $self, $item_id, $trans_date, $return_date, $current_loc, $item_props, $cancel, $checked_in_ok ) = @_;
179 my ( $patron, $item, $circ );
181 $circ = C4::SIP::ILS::Transaction::Checkin->new();
183 # BEGIN TRANSACTION
184 $circ->item( $item = C4::SIP::ILS::Item->new($item_id) );
186 if ($item) {
187 $circ->do_checkin( $current_loc, $return_date );
189 else {
190 $circ->alert(1);
191 $circ->alert_type(99);
192 $circ->screen_msg('Invalid Item');
195 # It's ok to check it in if it exists, and if it was checked out
196 # or it was not checked out but the checked_in_ok flag was set
197 $circ->ok( ( $checked_in_ok && $item ) || ( $item && $item->{patron} ) );
198 syslog("LOG_DEBUG", "C4::SIP::ILS::checkin - using checked_in_ok") if $checked_in_ok;
200 if ( !defined( $item->{patron} ) ) {
201 $circ->screen_msg("Item not checked out") unless $checked_in_ok;
202 syslog("LOG_DEBUG", "C4::SIP::ILS::checkin - item not checked out");
204 else {
205 if ( $circ->ok ) {
206 $circ->patron( $patron = C4::SIP::ILS::Patron->new( $item->{patron} ) );
207 delete $item->{patron};
208 delete $item->{due_date};
209 $patron->{items} = [ grep { $_ ne $item_id } @{ $patron->{items} } ];
213 # END TRANSACTION
215 return $circ;
218 # If the ILS caches patron information, this lets it free
219 # it up
220 sub end_patron_session {
221 my ($self, $patron_id) = @_;
223 # success?, screen_msg, print_line
224 return (1, 'Thank you !', '');
227 sub pay_fee {
228 my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
229 $pay_type, $fee_id, $trans_id, $currency) = @_;
230 my $trans;
232 $trans = C4::SIP::ILS::Transaction::FeePayment->new();
235 $trans->transaction_id($trans_id);
236 my $patron;
237 $trans->patron($patron = C4::SIP::ILS::Patron->new($patron_id));
238 if (!$patron) {
239 $trans->screen_msg('Invalid patron barcode.');
240 return $trans;
242 $trans->pay($patron->{borrowernumber},$fee_amt, $pay_type);
243 $trans->ok(1);
245 return $trans;
248 sub add_hold {
249 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
250 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
251 my ($patron, $item);
253 my $trans = C4::SIP::ILS::Transaction::Hold->new();
255 $patron = C4::SIP::ILS::Patron->new( $patron_id);
256 if (!$patron
257 || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
258 $trans->screen_msg("Invalid Patron.");
259 return $trans;
262 unless ($item = C4::SIP::ILS::Item->new($item_id || $title_id)) {
263 $trans->screen_msg("No such item.");
264 return $trans;
267 if ( $patron->holds_blocked_by_excessive_fees() ) {
268 $trans->screen_msg("Excessive fees blocking placement of hold.");
271 if ($item->fee and $fee_ack ne 'Y') {
272 $trans->screen_msg = "Fee required to place hold.";
273 return $trans;
276 my $hold = {
277 item_id => $item->id,
278 patron_id => $patron->id,
279 expiration_date => $expiry_date,
280 pickup_location => $pickup_location,
281 hold_type => $hold_type,
284 $trans->ok(1);
285 $trans->patron($patron);
286 $trans->item($item);
287 $trans->pickup_location($pickup_location);
288 $trans->do_hold;
290 push(@{$item->hold_queue}, $hold);
291 push(@{$patron->{hold_items}}, $hold);
293 return $trans;
296 sub cancel_hold {
297 my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
298 my ($patron, $item, $hold);
300 my $trans = C4::SIP::ILS::Transaction::Hold->new();
302 $patron = C4::SIP::ILS::Patron->new( $patron_id );
303 if (!$patron) {
304 $trans->screen_msg("Invalid patron barcode.");
305 return $trans;
306 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
307 $trans->screen_msg('Invalid patron password.');
308 return $trans;
311 unless ($item = C4::SIP::ILS::Item->new($item_id || $title_id)) {
312 $trans->screen_msg("No such item.");
313 return $trans;
316 $trans->patron($patron);
317 $trans->item($item);
318 $trans->drop_hold;
319 unless ($trans->ok) {
320 $trans->screen_msg("Error with transaction drop_hold: " . $trans->screen_msg);
321 return $trans;
323 # Remove the hold from the patron's record first
324 $trans->ok($patron->drop_hold($item_id)); # different than the transaction drop!
326 unless ($trans->ok) {
327 # We didn't find it on the patron record
328 $trans->screen_msg("No such hold on patron record.");
329 return $trans;
332 # Now, remove it from the item record. If it was on the patron
333 # record but not on the item record, we'll treat that as success.
334 foreach my $i (0 .. scalar @{$item->hold_queue}) {
335 $hold = $item->hold_queue->[$i];
336 if ($item->barcode_is_borrowernumber($patron->id, $hold->{borrowernumber})) {
337 # found it: delete it.
338 splice @{$item->hold_queue}, $i, 1;
339 last; # ?? should we keep going, in case there are multiples
343 $trans->screen_msg("Hold Cancelled.");
345 return $trans;
349 # The patron and item id's can't be altered, but the
350 # date, location, and type can.
351 sub alter_hold {
352 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
353 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
354 my ($patron, $item);
355 my $hold;
356 my $trans;
358 $trans = C4::SIP::ILS::Transaction::Hold->new();
360 # BEGIN TRANSACTION
361 $patron = C4::SIP::ILS::Patron->new( $patron_id );
362 unless ($patron) {
363 $trans->screen_msg("Invalid patron barcode: '$patron_id'.");
364 return $trans;
367 foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
368 $hold = $patron->{hold_items}[$i];
370 if ($hold->{item_id} eq $item_id) {
371 # Found it. So fix it.
372 $hold->{expiration_date} = $expiry_date if $expiry_date;
373 $hold->{pickup_location} = $pickup_location if $pickup_location;
374 $hold->{hold_type} = $hold_type if $hold_type;
375 $trans->change_hold();
376 # $trans->ok(1);
377 $trans->screen_msg("Hold updated.");
378 $trans->patron($patron);
379 $trans->item(C4::SIP::ILS::Item->new( $hold->{item_id}));
380 last;
384 # The same hold structure is linked into both the patron's
385 # list of hold items and into the queue of outstanding holds
386 # for the item, so we don't need to search the hold queue for
387 # the item, since it's already been updated by the patron code.
389 if (!$trans->ok) {
390 $trans->screen_msg("No such outstanding hold.");
393 return $trans;
396 sub renew {
397 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
398 $no_block, $nb_due_date, $third_party,
399 $item_props, $fee_ack) = @_;
400 my ($patron, $item);
401 my $trans;
403 $trans = C4::SIP::ILS::Transaction::Renew->new();
404 $trans->patron($patron = C4::SIP::ILS::Patron->new( $patron_id ));
406 if (!$patron) {
407 $trans->screen_msg("Invalid patron barcode.");
408 return $trans;
409 } elsif (!$patron->renew_ok) {
410 $trans->screen_msg("Renewals not allowed.");
411 return $trans;
414 # Previously: renewing a title, rather than an item (sort of)
415 # This is gross, but in a real ILS it would be better
417 # if (defined($title_id)) {
418 # foreach my $i (@{$patron->{items}}) {
419 # $item = new ILS::Item $i;
420 # last if ($title_id eq $item->title_id);
421 # $item = undef;
423 # } else {
424 my $j = 0;
425 my $count = scalar @{$patron->{items}};
426 foreach my $i (@{$patron->{items}}) {
427 unless (defined $i->{barcode}) { # FIXME: using data instead of objects may violate the abstraction layer
428 syslog("LOG_ERR", "No barcode for item %s of %s: $item_id", $j+1, $count);
429 next;
431 syslog("LOG_DEBUG", "checking item %s of %s: $item_id vs. %s", ++$j, $count, $i->{barcode});
432 if ($i->{barcode} eq $item_id) {
433 # We have it checked out
434 $item = C4::SIP::ILS::Item->new( $item_id );
435 last;
440 $trans->item($item);
442 if (!defined($item)) {
443 $trans->screen_msg("Item not checked out to " . $patron->name); # not checked out to $patron_id
444 $trans->ok(0);
445 } else {
446 $trans->do_renew();
447 if ($trans->renewal_ok()) {
448 $item->{due_date} = $trans->{due};
449 $trans->desensitize(0);
453 return $trans;
456 sub renew_all {
457 my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
458 my ($patron, $item_id);
459 my $trans;
461 $trans = C4::SIP::ILS::Transaction::RenewAll->new();
463 $trans->patron($patron = C4::SIP::ILS::Patron->new( $patron_id ));
464 if (defined $patron) {
465 syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s", $patron->name, $patron->renew_ok);
466 } else {
467 syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'", $patron_id);
470 if (!defined($patron)) {
471 $trans->screen_msg("Invalid patron barcode.");
472 return $trans;
473 } elsif (!$patron->renew_ok) {
474 $trans->screen_msg("Renewals not allowed.");
475 return $trans;
476 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
477 $trans->screen_msg("Invalid patron password.");
478 return $trans;
481 $trans->do_renew_all;
482 $trans->ok(1);
483 return $trans;
487 __END__