Bug 15072: (followup) fix spaces and consistency
[koha.git] / C4 / SIP / ILS.pm
blob099aa9c7327cbb4544a3e19ad3f3ccce4b3ae0cc
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,
179 $current_loc, $item_props, $cancel) = @_;
180 my ($patron, $item, $circ);
182 $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);
188 } else {
189 $circ->alert(1);
190 $circ->alert_type(99);
191 $circ->screen_msg('Invalid Item');
193 # It's ok to check it in if it exists, and if it was checked out
194 $circ->ok($item && $item->{patron});
196 if (!defined($item->{patron})) {
197 $circ->screen_msg("Item not checked out");
198 } else {
199 if ($circ->ok) {
200 $circ->patron($patron = C4::SIP::ILS::Patron->new( $item->{patron}));
201 delete $item->{patron};
202 delete $item->{due_date};
203 $patron->{items} = [ grep {$_ ne $item_id} @{$patron->{items}} ];
206 # END TRANSACTION
208 return $circ;
211 # If the ILS caches patron information, this lets it free
212 # it up
213 sub end_patron_session {
214 my ($self, $patron_id) = @_;
216 # success?, screen_msg, print_line
217 return (1, 'Thank you !', '');
220 sub pay_fee {
221 my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
222 $pay_type, $fee_id, $trans_id, $currency) = @_;
223 my $trans;
225 $trans = C4::SIP::ILS::Transaction::FeePayment->new();
228 $trans->transaction_id($trans_id);
229 my $patron;
230 $trans->patron($patron = C4::SIP::ILS::Patron->new($patron_id));
231 if (!$patron) {
232 $trans->screen_msg('Invalid patron barcode.');
233 return $trans;
235 $trans->pay($patron->{borrowernumber},$fee_amt, $pay_type);
236 $trans->ok(1);
238 return $trans;
241 sub add_hold {
242 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
243 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
244 my ($patron, $item);
246 my $trans = C4::SIP::ILS::Transaction::Hold->new();
248 $patron = C4::SIP::ILS::Patron->new( $patron_id);
249 if (!$patron
250 || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
251 $trans->screen_msg("Invalid Patron.");
252 return $trans;
255 unless ($item = C4::SIP::ILS::Item->new($item_id || $title_id)) {
256 $trans->screen_msg("No such item.");
257 return $trans;
260 if ( $patron->holds_blocked_by_excessive_fees() ) {
261 $trans->screen_msg("Excessive fees blocking placement of hold.");
264 if ($item->fee and $fee_ack ne 'Y') {
265 $trans->screen_msg = "Fee required to place hold.";
266 return $trans;
269 my $hold = {
270 item_id => $item->id,
271 patron_id => $patron->id,
272 expiration_date => $expiry_date,
273 pickup_location => $pickup_location,
274 hold_type => $hold_type,
277 $trans->ok(1);
278 $trans->patron($patron);
279 $trans->item($item);
280 $trans->pickup_location($pickup_location);
281 $trans->do_hold;
283 push(@{$item->hold_queue}, $hold);
284 push(@{$patron->{hold_items}}, $hold);
286 return $trans;
289 sub cancel_hold {
290 my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
291 my ($patron, $item, $hold);
293 my $trans = C4::SIP::ILS::Transaction::Hold->new();
295 $patron = C4::SIP::ILS::Patron->new( $patron_id );
296 if (!$patron) {
297 $trans->screen_msg("Invalid patron barcode.");
298 return $trans;
299 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
300 $trans->screen_msg('Invalid patron password.');
301 return $trans;
304 unless ($item = C4::SIP::ILS::Item->new($item_id || $title_id)) {
305 $trans->screen_msg("No such item.");
306 return $trans;
309 $trans->patron($patron);
310 $trans->item($item);
311 $trans->drop_hold;
312 unless ($trans->ok) {
313 $trans->screen_msg("Error with transaction drop_hold: " . $trans->screen_msg);
314 return $trans;
316 # Remove the hold from the patron's record first
317 $trans->ok($patron->drop_hold($item_id)); # different than the transaction drop!
319 unless ($trans->ok) {
320 # We didn't find it on the patron record
321 $trans->screen_msg("No such hold on patron record.");
322 return $trans;
325 # Now, remove it from the item record. If it was on the patron
326 # record but not on the item record, we'll treat that as success.
327 foreach my $i (0 .. scalar @{$item->hold_queue}) {
328 $hold = $item->hold_queue->[$i];
329 if ($item->barcode_is_borrowernumber($patron->id, $hold->{borrowernumber})) {
330 # found it: delete it.
331 splice @{$item->hold_queue}, $i, 1;
332 last; # ?? should we keep going, in case there are multiples
336 $trans->screen_msg("Hold Cancelled.");
338 return $trans;
342 # The patron and item id's can't be altered, but the
343 # date, location, and type can.
344 sub alter_hold {
345 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
346 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
347 my ($patron, $item);
348 my $hold;
349 my $trans;
351 $trans = C4::SIP::ILS::Transaction::Hold->new();
353 # BEGIN TRANSACTION
354 $patron = C4::SIP::ILS::Patron->new( $patron_id );
355 unless ($patron) {
356 $trans->screen_msg("Invalid patron barcode: '$patron_id'.");
357 return $trans;
360 foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
361 $hold = $patron->{hold_items}[$i];
363 if ($hold->{item_id} eq $item_id) {
364 # Found it. So fix it.
365 $hold->{expiration_date} = $expiry_date if $expiry_date;
366 $hold->{pickup_location} = $pickup_location if $pickup_location;
367 $hold->{hold_type} = $hold_type if $hold_type;
368 $trans->change_hold();
369 # $trans->ok(1);
370 $trans->screen_msg("Hold updated.");
371 $trans->patron($patron);
372 $trans->item(C4::SIP::ILS::Item->new( $hold->{item_id}));
373 last;
377 # The same hold structure is linked into both the patron's
378 # list of hold items and into the queue of outstanding holds
379 # for the item, so we don't need to search the hold queue for
380 # the item, since it's already been updated by the patron code.
382 if (!$trans->ok) {
383 $trans->screen_msg("No such outstanding hold.");
386 return $trans;
389 sub renew {
390 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
391 $no_block, $nb_due_date, $third_party,
392 $item_props, $fee_ack) = @_;
393 my ($patron, $item);
394 my $trans;
396 $trans = C4::SIP::ILS::Transaction::Renew->new();
397 $trans->patron($patron = C4::SIP::ILS::Patron->new( $patron_id ));
399 if (!$patron) {
400 $trans->screen_msg("Invalid patron barcode.");
401 return $trans;
402 } elsif (!$patron->renew_ok) {
403 $trans->screen_msg("Renewals not allowed.");
404 return $trans;
407 # Previously: renewing a title, rather than an item (sort of)
408 # This is gross, but in a real ILS it would be better
410 # if (defined($title_id)) {
411 # foreach my $i (@{$patron->{items}}) {
412 # $item = new ILS::Item $i;
413 # last if ($title_id eq $item->title_id);
414 # $item = undef;
416 # } else {
417 my $j = 0;
418 my $count = scalar @{$patron->{items}};
419 foreach my $i (@{$patron->{items}}) {
420 unless (defined $i->{barcode}) { # FIXME: using data instead of objects may violate the abstraction layer
421 syslog("LOG_ERR", "No barcode for item %s of %s: $item_id", $j+1, $count);
422 next;
424 syslog("LOG_DEBUG", "checking item %s of %s: $item_id vs. %s", ++$j, $count, $i->{barcode});
425 if ($i->{barcode} eq $item_id) {
426 # We have it checked out
427 $item = C4::SIP::ILS::Item->new( $item_id );
428 last;
433 $trans->item($item);
435 if (!defined($item)) {
436 $trans->screen_msg("Item not checked out to " . $patron->name); # not checked out to $patron_id
437 $trans->ok(0);
438 } else {
439 $trans->do_renew();
440 if ($trans->renewal_ok()) {
441 $item->{due_date} = $trans->{due};
442 $trans->desensitize(0);
446 return $trans;
449 sub renew_all {
450 my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
451 my ($patron, $item_id);
452 my $trans;
454 $trans = C4::SIP::ILS::Transaction::RenewAll->new();
456 $trans->patron($patron = C4::SIP::ILS::Patron->new( $patron_id ));
457 if (defined $patron) {
458 syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s", $patron->name, $patron->renew_ok);
459 } else {
460 syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'", $patron_id);
463 if (!defined($patron)) {
464 $trans->screen_msg("Invalid patron barcode.");
465 return $trans;
466 } elsif (!$patron->renew_ok) {
467 $trans->screen_msg("Renewals not allowed.");
468 return $trans;
469 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
470 $trans->screen_msg("Invalid patron password.");
471 return $trans;
474 $trans->do_renew_all;
475 $trans->ok(1);
476 return $trans;
480 __END__