Bug 5605 [ENH] SIP: Add Support for the Fee Paid Message
[koha.git] / C4 / SIP / ILS.pm
1 #
2 # ILS.pm: Koha ILS interface module
3 #
4
5 package ILS;
6
7 use warnings;
8 use strict;
9 use Sys::Syslog qw(syslog);
10
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;
20
21 my $debug = 0;
22
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,
42 );
43
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;
53 }
54
55 sub find_patron {
56     my $self = shift;
57         $debug and warn "ILS: finding patron";
58     return ILS::Patron->new(@_);
59 }
60
61 sub find_item {
62     my $self = shift;
63         $debug and warn "ILS: finding item";
64     return ILS::Item->new(@_);
65 }
66
67 sub institution {
68     my $self = shift;
69     return $self->{institution}->{id};  # consider making this return the whole institution
70 }
71
72 sub institution_id {
73     my $self = shift;
74     return $self->{institution}->{id};
75 }
76
77 sub supports {
78     my ($self, $op) = @_;
79     return (exists($supports{$op}) && $supports{$op});
80 }
81
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
87     }
88 }
89
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
97 }
98
99 sub checkout_ok {
100     my $self = shift;
101     return (exists($self->{institution}->{policy}->{checkout})
102             && to_bool($self->{institution}->{policy}->{checkout}));
103 }
104 sub checkin_ok {
105     my $self = shift;
106     return (exists($self->{institution}->{policy}->{checkin})
107             && to_bool($self->{institution}->{policy}->{checkin}));
108 }
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}));
113 }
114 sub offline_ok {
115     my $self = shift;
116     return (exists($self->{institution}->{policy}->{offline})
117             && to_bool($self->{institution}->{policy}->{offline}));
118 }
119
120 #
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.
127 #
128 sub checkout {
129     my ($self, $patron_id, $item_id, $sc_renew) = @_;
130     my ($patron, $item, $circ);
131
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);
136
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));
156                 
157                         $item->{patron} = $patron_id;
158                         $item->{due_date} = $circ->{due};
159                         push(@{$patron->{items}}, $item_id);
160                         $circ->desensitize(!$item->magnetic_media);
161
162                         syslog("LOG_DEBUG", "ILS::Checkout: patron %s has checked out %s",
163                                 $patron_id, join(', ', @{$patron->{items}}));
164                 }
165                 else {
166                         syslog("LOG_ERR", "ILS::Checkout Issue failed");
167                 }
168     }
169     # END TRANSACTION
170
171     return $circ;
172 }
173
174 sub checkin {
175     my ($self, $item_id, $trans_date, $return_date,
176         $current_loc, $item_props, $cancel) = @_;
177     my ($patron, $item, $circ);
178
179     $circ = new ILS::Transaction::Checkin;
180     # BEGIN TRANSACTION
181     $circ->item($item = new ILS::Item $item_id);
182
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');
189     }
190         # It's ok to check it in if it exists, and if it was checked out
191         $circ->ok($item && $item->{patron});
192
193     if ($circ->ok) {
194                 $circ->patron($patron = new ILS::Patron $item->{patron});
195                 delete $item->{patron};
196                 delete $item->{due_date};
197                 $patron->{items} = [ grep {$_ ne $item_id} @{$patron->{items}} ];
198     }
199     # END TRANSACTION
200
201     return $circ;
202 }
203
204 # If the ILS caches patron information, this lets it free
205 # it up
206 sub end_patron_session {
207     my ($self, $patron_id) = @_;
208
209     # success?, screen_msg, print_line
210     return (1, 'Thank you !', '');
211 }
212
213 sub pay_fee {
214     my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
215         $pay_type, $fee_id, $trans_id, $currency) = @_;
216     my $trans;
217
218     $trans = ILS::Transaction::FeePayment->new();
219
220
221     $trans->transaction_id($trans_id);
222     my $patron;
223     $trans->patron($patron = ILS::Patron->new($patron_id));
224     if (!$patron) {
225         $trans->screen_msg('Invalid patron barcode.');
226         return $trans;
227     }
228     $trans->pay($patron->{borrowernumber},$fee_amt);
229     $trans->ok(1);
230
231     return $trans;
232 }
233
234 sub add_hold {
235     my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
236         $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
237     my ($patron, $item);
238
239         my $trans = new ILS::Transaction::Hold;
240
241     $patron = new ILS::Patron $patron_id;
242     if (!$patron
243         || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
244                 $trans->screen_msg("Invalid Patron.");
245                 return $trans;
246     }
247
248         unless ($item = new ILS::Item ($item_id || $title_id)) {
249                 $trans->screen_msg("No such item.");
250                 return $trans;
251         }
252
253    if ($item->fee and $fee_ack ne 'Y') {
254                 $trans->screen_msg = "Fee required to place hold.";
255                 return $trans;
256     }
257
258     my $hold = {
259         item_id         => $item->id,
260         patron_id       => $patron->id,
261         expiration_date => $expiry_date,
262         pickup_location => $pickup_location,
263         hold_type       => $hold_type,
264     };
265
266     $trans->ok(1);
267     $trans->patron($patron);
268     $trans->item($item);
269     $trans->pickup_location($pickup_location);
270         $trans->do_hold;
271
272     push(@{$item->hold_queue},     $hold);
273     push(@{$patron->{hold_items}}, $hold);
274
275     return $trans;
276 }
277
278 sub cancel_hold {
279     my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
280     my ($patron, $item, $hold);
281
282         my $trans = new ILS::Transaction::Hold;
283
284     $patron = new ILS::Patron $patron_id;
285     if (!$patron) {
286                 $trans->screen_msg("Invalid patron barcode.");
287                 return $trans;
288     } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
289                 $trans->screen_msg('Invalid patron password.');
290                 return $trans;
291     }
292
293     unless ($item = new ILS::Item ($item_id || $title_id)) {
294                 $trans->screen_msg("No such item.");
295                 return $trans;
296     }
297
298     $trans->patron($patron);
299     $trans->item($item);
300         $trans->drop_hold;
301         unless ($trans->ok) {
302                 $trans->screen_msg("Error with transaction drop_hold: " . $trans->screen_msg);
303                 return $trans;
304         }
305     # Remove the hold from the patron's record first
306     $trans->ok($patron->drop_hold($item_id));   # different than the transaction drop!
307
308     unless ($trans->ok) {
309                 # We didn't find it on the patron record
310                 $trans->screen_msg("No such hold on patron record.");
311                 return $trans;
312     }
313
314     # Now, remove it from the item record.  If it was on the patron
315     # record but not on the item record, we'll treat that as success.
316     foreach my $i (0 .. scalar @{$item->hold_queue}) {
317                 $hold = $item->hold_queue->[$i];
318                 if ($item->barcode_is_borrowernumber($patron->id, $hold->{borrowernumber})) {
319                     # found it: delete it.
320                     splice @{$item->hold_queue}, $i, 1;
321                     last;               # ?? should we keep going, in case there are multiples
322                 }
323     }
324
325     $trans->screen_msg("Hold Cancelled.");
326
327     return $trans;
328 }
329
330
331 # The patron and item id's can't be altered, but the
332 # date, location, and type can.
333 sub alter_hold {
334     my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
335         $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
336     my ($patron, $item);
337     my $hold;
338     my $trans;
339
340     $trans = new ILS::Transaction::Hold;
341
342     # BEGIN TRANSACTION
343     $patron = new ILS::Patron $patron_id;
344     unless ($patron) {
345                 $trans->screen_msg("Invalid patron barcode: '$patron_id'.");
346                 return $trans;
347     }
348
349     foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
350                 $hold = $patron->{hold_items}[$i];
351
352         if ($hold->{item_id} eq $item_id) {
353             # Found it.  So fix it.
354             $hold->{expiration_date} = $expiry_date     if $expiry_date;
355             $hold->{pickup_location} = $pickup_location if $pickup_location;
356             $hold->{hold_type}       = $hold_type       if $hold_type;
357                 $trans->change_hold();
358             # $trans->ok(1);
359             $trans->screen_msg("Hold updated.");
360             $trans->patron($patron);
361             $trans->item(new ILS::Item $hold->{item_id});
362             last;
363         }
364     }
365
366     # The same hold structure is linked into both the patron's
367     # list of hold items and into the queue of outstanding holds
368     # for the item, so we don't need to search the hold queue for
369     # the item, since it's already been updated by the patron code.
370
371     if (!$trans->ok) {
372                 $trans->screen_msg("No such outstanding hold.");
373     }
374
375     return $trans;
376 }
377
378 sub renew {
379     my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
380         $no_block, $nb_due_date, $third_party,
381         $item_props, $fee_ack) = @_;
382     my ($patron, $item);
383     my $trans;
384
385     $trans = new ILS::Transaction::Renew;
386     $trans->patron($patron = new ILS::Patron $patron_id);
387
388     if (!$patron) {
389                 $trans->screen_msg("Invalid patron barcode.");
390                 return $trans;
391     } elsif (!$patron->renew_ok) {
392                 $trans->screen_msg("Renewals not allowed.");
393                 return $trans;
394     }
395
396         # Previously: renewing a title, rather than an item (sort of)
397         # This is gross, but in a real ILS it would be better
398
399     # if (defined($title_id)) {
400         #       foreach my $i (@{$patron->{items}}) {
401         #               $item = new ILS::Item $i;
402         #               last if ($title_id eq $item->title_id);
403         #               $item = undef;
404         #       }
405     # } else {
406                 my $j = 0;
407                 my $count = scalar @{$patron->{items}};
408                 foreach my $i (@{$patron->{items}}) {
409             unless (defined $i->{barcode}) {    # FIXME: using data instead of objects may violate the abstraction layer
410                 syslog("LOG_ERR", "No barcode for item %s of %s: $item_id", $j+1, $count);
411                 next;
412             }
413             syslog("LOG_DEBUG", "checking item %s of %s: $item_id vs. %s", ++$j, $count, $i->{barcode});
414             if ($i->{barcode} eq $item_id) {
415                                 # We have it checked out
416                                 $item = new ILS::Item $item_id;
417                                 last;
418                         }
419                 }
420     # }
421
422     $trans->item($item);
423
424     if (!defined($item)) {
425                 $trans->screen_msg("Item not checked out to " . $patron->name);     # not checked out to $patron_id
426         $trans->ok(0);
427     } elsif (!$item->available($patron_id)) {
428                 $trans->screen_msg("Item unavailable due to outstanding holds");
429         $trans->ok(0);
430     } else {
431                 $trans->renewal_ok(1);
432                 $trans->desensitize(0); # It's already checked out
433                 $trans->do_renew();
434                 syslog("LOG_DEBUG", "done renew (ok:%s): %s renews %s", $trans->renewal_ok, $patron_id, $item_id);
435
436 #               $item->{due_date} = $nb_due_date if $no_block eq 'Y';
437 #               $item->{sip_item_properties} = $item_props if $item_props;
438     }
439
440     return $trans;
441 }
442
443 sub renew_all {
444     my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
445     my ($patron, $item_id);
446     my $trans;
447
448     $trans = new ILS::Transaction::RenewAll;
449
450     $trans->patron($patron = new ILS::Patron $patron_id);
451     if (defined $patron) {
452         syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s", $patron->name, $patron->renew_ok);
453     } else {
454         syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'", $patron_id);
455     }
456
457     if (!defined($patron)) {
458                 $trans->screen_msg("Invalid patron barcode.");
459                 return $trans;
460     } elsif (!$patron->renew_ok) {
461                 $trans->screen_msg("Renewals not allowed.");
462                 return $trans;
463     } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
464                 $trans->screen_msg("Invalid patron password.");
465                 return $trans;
466     }
467
468         $trans->do_renew_all;
469     $trans->ok(1);
470     return $trans;
471 }
472
473 1;
474 __END__
475