Bug 12505 - Acq claim: Show error message when no order is selected
[koha.git] / C4 / SIP / ILS.pm
blob8086bc6bb3e295b55e1fd640ef18967173aacadd
2 # ILS.pm: Koha ILS interface module
5 package ILS;
7 use warnings;
8 use strict;
9 use Sys::Syslog qw(syslog);
11 use ILS::Item;
12 use ILS::Patron;
13 use ILS::Transaction;
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;
21 my $debug = 0;
23 my %supports = (
24 'magnetic media' => 1,
25 'security inhibit' => 0,
26 'offline operation' => 0,
27 "patron status request" => 1,
28 "checkout" => 1,
29 "checkin" => 1,
30 "block patron" => 1,
31 "acs status" => 1,
32 "login" => 1,
33 "patron information" => 1,
34 "end patron session" => 1,
35 "fee paid" => 1,
36 "item information" => 1,
37 "item status update" => 0,
38 "patron enable" => 1,
39 "hold" => 1,
40 "renew" => 1,
41 "renew all" => 1,
44 sub new {
45 my ($class, $institution) = @_;
46 my $type = ref($class) || $class;
47 my $self = {};
48 use Data::Dumper;
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 ILS::Patron->new(@_);
61 sub find_item {
62 my $self = shift;
63 $debug and warn "ILS: finding item";
64 return 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) = @_;
130 my ($patron, $item, $circ);
132 $circ = new ILS::Transaction::Checkout;
133 # BEGIN TRANSACTION
134 $circ->patron($patron = new ILS::Patron $patron_id);
135 $circ->item($item = new ILS::Item $item_id);
137 if (!$patron) {
138 $circ->screen_msg("Invalid Patron");
139 } elsif (!$patron->charge_ok) {
140 $circ->screen_msg("Patron Blocked");
141 } elsif (!$item) {
142 $circ->screen_msg("Invalid Item");
143 # holds checked inside do_checkout
144 # } elsif ($item->hold_queue && @{$item->hold_queue} && ! $item->barcode_is_borrowernumber($patron_id, $item->hold_queue->[0]->{borrowernumber})) {
145 # $circ->screen_msg("Item on Hold for Another User");
146 } elsif ($item->{patron} && ($item->{patron} ne $patron_id)) {
147 # I can't deal with this right now
148 $circ->screen_msg("Item checked out to another patron");
149 } else {
150 $circ->do_checkout();
151 if ($circ->ok){
152 $debug and warn "circ is ok";
153 # If the item is already associated with this patron, then
154 # we're renewing it.
155 $circ->renew_ok($item->{patron} && ($item->{patron} eq $patron_id));
157 $item->{patron} = $patron_id;
158 $item->{due_date} = $circ->{due};
159 push(@{$patron->{items}}, $item_id);
160 $circ->desensitize(!$item->magnetic_media);
162 syslog("LOG_DEBUG", "ILS::Checkout: patron %s has checked out %s",
163 $patron_id, join(', ', @{$patron->{items}}));
165 else {
166 syslog("LOG_ERR", "ILS::Checkout Issue failed");
169 # END TRANSACTION
171 return $circ;
174 sub checkin {
175 my ($self, $item_id, $trans_date, $return_date,
176 $current_loc, $item_props, $cancel) = @_;
177 my ($patron, $item, $circ);
179 $circ = new ILS::Transaction::Checkin;
180 # BEGIN TRANSACTION
181 $circ->item($item = new ILS::Item $item_id);
183 if ($item) {
184 $circ->do_checkin($current_loc, $return_date);
185 } else {
186 $circ->alert(1);
187 $circ->alert_type(99);
188 $circ->screen_msg('Invalid Item');
190 # It's ok to check it in if it exists, and if it was checked out
191 $circ->ok($item && $item->{patron});
193 if (!defined($item->{patron})) {
194 $circ->screen_msg("Item not checked out");
195 } else {
196 if ($circ->ok) {
197 $circ->patron($patron = new ILS::Patron $item->{patron});
198 delete $item->{patron};
199 delete $item->{due_date};
200 $patron->{items} = [ grep {$_ ne $item_id} @{$patron->{items}} ];
203 # END TRANSACTION
205 return $circ;
208 # If the ILS caches patron information, this lets it free
209 # it up
210 sub end_patron_session {
211 my ($self, $patron_id) = @_;
213 # success?, screen_msg, print_line
214 return (1, 'Thank you !', '');
217 sub pay_fee {
218 my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
219 $pay_type, $fee_id, $trans_id, $currency) = @_;
220 my $trans;
222 $trans = ILS::Transaction::FeePayment->new();
225 $trans->transaction_id($trans_id);
226 my $patron;
227 $trans->patron($patron = ILS::Patron->new($patron_id));
228 if (!$patron) {
229 $trans->screen_msg('Invalid patron barcode.');
230 return $trans;
232 $trans->pay($patron->{borrowernumber},$fee_amt, $pay_type);
233 $trans->ok(1);
235 return $trans;
238 sub add_hold {
239 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
240 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
241 my ($patron, $item);
243 my $trans = new ILS::Transaction::Hold;
245 $patron = new ILS::Patron $patron_id;
246 if (!$patron
247 || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
248 $trans->screen_msg("Invalid Patron.");
249 return $trans;
252 unless ($item = new ILS::Item ($item_id || $title_id)) {
253 $trans->screen_msg("No such item.");
254 return $trans;
257 if ( $patron->holds_blocked_by_excessive_fees() ) {
258 $trans->screen_msg("Excessive fees blocking placement of hold.");
261 if ($item->fee and $fee_ack ne 'Y') {
262 $trans->screen_msg = "Fee required to place hold.";
263 return $trans;
266 my $hold = {
267 item_id => $item->id,
268 patron_id => $patron->id,
269 expiration_date => $expiry_date,
270 pickup_location => $pickup_location,
271 hold_type => $hold_type,
274 $trans->ok(1);
275 $trans->patron($patron);
276 $trans->item($item);
277 $trans->pickup_location($pickup_location);
278 $trans->do_hold;
280 push(@{$item->hold_queue}, $hold);
281 push(@{$patron->{hold_items}}, $hold);
283 return $trans;
286 sub cancel_hold {
287 my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
288 my ($patron, $item, $hold);
290 my $trans = new ILS::Transaction::Hold;
292 $patron = new ILS::Patron $patron_id;
293 if (!$patron) {
294 $trans->screen_msg("Invalid patron barcode.");
295 return $trans;
296 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
297 $trans->screen_msg('Invalid patron password.');
298 return $trans;
301 unless ($item = new ILS::Item ($item_id || $title_id)) {
302 $trans->screen_msg("No such item.");
303 return $trans;
306 $trans->patron($patron);
307 $trans->item($item);
308 $trans->drop_hold;
309 unless ($trans->ok) {
310 $trans->screen_msg("Error with transaction drop_hold: " . $trans->screen_msg);
311 return $trans;
313 # Remove the hold from the patron's record first
314 $trans->ok($patron->drop_hold($item_id)); # different than the transaction drop!
316 unless ($trans->ok) {
317 # We didn't find it on the patron record
318 $trans->screen_msg("No such hold on patron record.");
319 return $trans;
322 # Now, remove it from the item record. If it was on the patron
323 # record but not on the item record, we'll treat that as success.
324 foreach my $i (0 .. scalar @{$item->hold_queue}) {
325 $hold = $item->hold_queue->[$i];
326 if ($item->barcode_is_borrowernumber($patron->id, $hold->{borrowernumber})) {
327 # found it: delete it.
328 splice @{$item->hold_queue}, $i, 1;
329 last; # ?? should we keep going, in case there are multiples
333 $trans->screen_msg("Hold Cancelled.");
335 return $trans;
339 # The patron and item id's can't be altered, but the
340 # date, location, and type can.
341 sub alter_hold {
342 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
343 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
344 my ($patron, $item);
345 my $hold;
346 my $trans;
348 $trans = new ILS::Transaction::Hold;
350 # BEGIN TRANSACTION
351 $patron = new ILS::Patron $patron_id;
352 unless ($patron) {
353 $trans->screen_msg("Invalid patron barcode: '$patron_id'.");
354 return $trans;
357 foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
358 $hold = $patron->{hold_items}[$i];
360 if ($hold->{item_id} eq $item_id) {
361 # Found it. So fix it.
362 $hold->{expiration_date} = $expiry_date if $expiry_date;
363 $hold->{pickup_location} = $pickup_location if $pickup_location;
364 $hold->{hold_type} = $hold_type if $hold_type;
365 $trans->change_hold();
366 # $trans->ok(1);
367 $trans->screen_msg("Hold updated.");
368 $trans->patron($patron);
369 $trans->item(new ILS::Item $hold->{item_id});
370 last;
374 # The same hold structure is linked into both the patron's
375 # list of hold items and into the queue of outstanding holds
376 # for the item, so we don't need to search the hold queue for
377 # the item, since it's already been updated by the patron code.
379 if (!$trans->ok) {
380 $trans->screen_msg("No such outstanding hold.");
383 return $trans;
386 sub renew {
387 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
388 $no_block, $nb_due_date, $third_party,
389 $item_props, $fee_ack) = @_;
390 my ($patron, $item);
391 my $trans;
393 $trans = new ILS::Transaction::Renew;
394 $trans->patron($patron = new ILS::Patron $patron_id);
396 if (!$patron) {
397 $trans->screen_msg("Invalid patron barcode.");
398 return $trans;
399 } elsif (!$patron->renew_ok) {
400 $trans->screen_msg("Renewals not allowed.");
401 return $trans;
404 # Previously: renewing a title, rather than an item (sort of)
405 # This is gross, but in a real ILS it would be better
407 # if (defined($title_id)) {
408 # foreach my $i (@{$patron->{items}}) {
409 # $item = new ILS::Item $i;
410 # last if ($title_id eq $item->title_id);
411 # $item = undef;
413 # } else {
414 my $j = 0;
415 my $count = scalar @{$patron->{items}};
416 foreach my $i (@{$patron->{items}}) {
417 unless (defined $i->{barcode}) { # FIXME: using data instead of objects may violate the abstraction layer
418 syslog("LOG_ERR", "No barcode for item %s of %s: $item_id", $j+1, $count);
419 next;
421 syslog("LOG_DEBUG", "checking item %s of %s: $item_id vs. %s", ++$j, $count, $i->{barcode});
422 if ($i->{barcode} eq $item_id) {
423 # We have it checked out
424 $item = new ILS::Item $item_id;
425 last;
430 $trans->item($item);
432 if (!defined($item)) {
433 $trans->screen_msg("Item not checked out to " . $patron->name); # not checked out to $patron_id
434 $trans->ok(0);
435 } else {
436 $trans->do_renew();
437 if ($trans->renewal_ok()) {
438 $item->{due_date} = $trans->{due};
439 $trans->desensitize(0);
443 return $trans;
446 sub renew_all {
447 my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
448 my ($patron, $item_id);
449 my $trans;
451 $trans = new ILS::Transaction::RenewAll;
453 $trans->patron($patron = new ILS::Patron $patron_id);
454 if (defined $patron) {
455 syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s", $patron->name, $patron->renew_ok);
456 } else {
457 syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'", $patron_id);
460 if (!defined($patron)) {
461 $trans->screen_msg("Invalid patron barcode.");
462 return $trans;
463 } elsif (!$patron->renew_ok) {
464 $trans->screen_msg("Renewals not allowed.");
465 return $trans;
466 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
467 $trans->screen_msg("Invalid patron password.");
468 return $trans;
471 $trans->do_renew_all;
472 $trans->ok(1);
473 return $trans;
477 __END__