rename internal function
[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 %supports = (
22                 'magnetic media'        => 1,
23                 'security inhibit'      => 0,
24                 'offline operation'     => 0,
25                 "patron status request" => 1,
26                 "checkout"              => 1,
27                 "checkin"               => 1,
28                 "block patron"          => 1,
29                 "acs status"            => 1,
30                 "login"                 => 1,
31                 "patron information"    => 1,
32                 "end patron session"    => 1,
33                 "fee paid"              => 0,
34                 "item information"      => 1,
35                 "item status update"    => 0,
36                 "patron enable"         => 1,
37                 "hold"                  => 1,
38                 "renew"                 => 1,
39                 "renew all"             => 0,
40                );
41
42 sub new {
43     my ($class, $institution) = @_;
44     my $type = ref($class) || $class;
45     my $self = {};
46 #use Data::Dumper;
47 #warn " INSTITUTION:";
48 #warn Dumper($institution);
49     syslog("LOG_DEBUG", "new ILS '%s'", $institution->{id});
50     $self->{institution} = $institution;
51
52     return bless $self, $type;
53 }
54
55 sub find_patron {
56     my $self = shift;
57 warn "finding patron";
58     return ILS::Patron->new(@_);
59 }
60
61 sub find_item {
62     my $self = shift;
63 warn "find item";
64     return ILS::Item->new(@_);
65 }
66
67 sub institution {
68     my $self = shift;
69
70     return $self->{institution}->{id};
71 }
72
73 sub supports {
74     my ($self, $op) = @_;
75
76     return (exists($supports{$op}) && $supports{$op});
77 }
78
79 sub check_inst_id {
80     my ($self, $id, $whence) = @_;
81
82     if ($id ne $self->{institution}->{id}) {
83         syslog("LOG_WARNING", "%s: received institution '%s', expected '%s'",
84                $whence, $id, $self->{institution}->{id});
85     }
86 }
87
88 sub to_bool {
89     my $bool = shift;
90
91     # If it's defined, and matches a true sort of string, or is
92     # a non-zero number, then it's true.
93     return defined($bool) && (($bool =~ /true|y|yes/i) || $bool != 0);
94 }
95
96 sub checkout_ok {
97     my $self = shift;
98
99     return (exists($self->{policy}->{checkout})
100             && to_bool($self->{policy}->{checkout}));
101 }
102
103 sub checkin_ok {
104     my $self = shift;
105
106     return (exists($self->{policy}->{checkin})
107             && to_bool($self->{policy}->{checkin}));
108 }
109
110 sub status_update_ok {
111     my $self = shift;
112
113     return (exists($self->{policy}->{status_update})
114             && to_bool($self->{policy}->{status_update}));
115
116 }
117
118 sub offline_ok {
119     my $self = shift;
120
121     return (exists($self->{policy}->{offline})
122             && to_bool($self->{policy}->{offline}));
123 }
124
125 #
126 # Checkout(patron_id, item_id, sc_renew):
127 #    patron_id & item_id are the identifiers send by the terminal
128 #    sc_renew is the renewal policy configured on the terminal
129 # returns a status opject that can be queried for the various bits
130 # of information that the protocol (SIP or NCIP) needs to generate
131 # the response.
132 #
133 sub checkout {
134     my ($self, $patron_id, $item_id, $sc_renew) = @_;
135     my ($patron, $item, $circ);
136
137     $circ = new ILS::Transaction::Checkout;
138     # BEGIN TRANSACTION
139     $circ->patron($patron = new ILS::Patron $patron_id);
140     $circ->item($item = new ILS::Item $item_id);
141
142     if (!$patron) {
143                 $circ->screen_msg("Invalid Patron");
144     } elsif (!$patron->charge_ok) {
145                 $circ->screen_msg("Patron Blocked");
146     } elsif (!$item) {
147                 $circ->screen_msg("Invalid Item");
148     } elsif ($item->hold_queue && @{$item->hold_queue} && ($patron_id ne $item->hold_queue->[0])) {
149                 $circ->screen_msg("Item on Hold for Another User");
150     } elsif ($item->{patron} && ($item->{patron} ne $patron_id)) {
151         # I can't deal with this right now
152                 $circ->screen_msg("Item checked out to another patron");
153     } else {
154                 $circ->do_checkout();
155                 if ($circ->ok){
156                         warn "circ is ok";
157                         # If the item is already associated with this patron, then
158                         # we're renewing it.
159                         $circ->renew_ok($item->{patron} && ($item->{patron} eq $patron_id));
160                 
161                         $item->{patron} = $patron_id;
162                         $item->{due_date} = $circ->{due};
163                         push(@{$patron->{items}}, $item_id);
164                         $circ->desensitize(!$item->magnetic);
165
166                         syslog("LOG_DEBUG", "ILS::Checkout: patron %s has checked out %s",
167                                 $patron_id, join(', ', @{$patron->{items}}));
168                 }
169                 else {
170                         syslog("LOG_DEBUG", "ILS::Checkout Issue failed");
171                         
172                 }
173     }
174
175     # END TRANSACTION
176
177     return $circ;
178 }
179
180 sub checkin {
181     my ($self, $item_id, $trans_date, $return_date,
182         $current_loc, $item_props, $cancel) = @_;
183     my ($patron, $item, $circ);
184
185     $circ = new ILS::Transaction::Checkin;
186     # BEGIN TRANSACTION
187     $circ->item($item = new ILS::Item $item_id);
188     
189     $circ->do_checkin();    
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     }
200     # END TRANSACTION
201
202     return $circ;
203 }
204
205 # If the ILS caches patron information, this lets it free
206 # it up
207 sub end_patron_session {
208     my ($self, $patron_id) = @_;
209
210     # success?, screen_msg, print_line
211     return (1, 'Thank you !', '');
212 }
213
214 sub pay_fee {
215     my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
216         $pay_type, $fee_id, $trans_id, $currency) = @_;
217     my $trans;
218     my $patron;
219
220 #    $trans = new ILS::Transaction::FeePayment;
221
222     $patron = new ILS::Patron $patron_id;
223
224     $trans->transaction_id($trans_id);
225     $trans->patron($patron);
226     $trans->ok(1);
227
228     return $trans;
229 }
230
231 sub add_hold {
232     my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
233         $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
234     my ($patron, $item);
235     my $hold;
236     my $trans;
237
238
239     $trans = new ILS::Transaction::Hold;
240
241     # BEGIN TRANSACTION
242     $patron = new ILS::Patron $patron_id;
243     if (!$patron
244         || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
245         $trans->screen_msg("Invalid Patron.");
246
247         return $trans;
248     }
249
250     $item = new ILS::Item ($item_id || $title_id);
251     if (!$item) {
252         $trans->screen_msg("No such item.");
253
254         # END TRANSACTION (conditionally)
255         return $trans;
256     } elsif ($item->fee && ($fee_ack ne 'Y')) {
257         $trans->screen_msg = "Fee required to place hold.";
258
259         # END TRANSACTION (conditionally)
260         return $trans;
261     }
262
263     $hold = {
264         item_id         => $item->id,
265         patron_id       => $patron->id,
266         expiration_date => $expiry_date,
267         pickup_location => $pickup_location,
268         hold_type       => $hold_type,
269     };
270
271     $trans->ok(1);
272     $trans->patron($patron);
273     $trans->item($item);
274     $trans->pickup_location($pickup_location);
275
276     push(@{$item->hold_queue}, $hold);
277     push(@{$patron->{hold_items}}, $hold);
278
279
280     # END TRANSACTION
281     return $trans;
282 }
283
284 sub cancel_hold {
285     my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
286     my ($patron, $item, $hold);
287     my $trans;
288
289     $trans = new ILS::Transaction::Hold;
290
291     # BEGIN TRANSACTION
292     $patron = new ILS::Patron $patron_id;
293     if (!$patron) {
294         $trans->screen_msg("Invalid patron barcode.");
295
296         return $trans;
297     } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
298         $trans->screen_msg('Invalid patron password.');
299
300         return $trans;
301     }
302
303     $item = new ILS::Item ($item_id || $title_id);
304     if (!$item) {
305         $trans->screen_msg("No such item.");
306
307         # END TRANSACTION (conditionally)
308         return $trans;
309     }
310
311     # Remove the hold from the patron's record first
312     $trans->ok($patron->drop_hold($item_id));
313
314     if (!$trans->ok) {
315         # We didn't find it on the patron record
316         $trans->screen_msg("No such hold on patron record.");
317
318         # END TRANSACTION (conditionally)
319         return $trans;
320     }
321
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
327         if ($hold->{patron_id} eq $patron->id) {
328             # found it: delete it.
329             splice @{$item->hold_queue}, $i, 1;
330             last;
331         }
332     }
333
334     $trans->screen_msg("Hold Cancelled.");
335     $trans->patron($patron);
336     $trans->item($item);
337
338     return $trans;
339 }
340
341
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;
350
351     $trans = new ILS::Transaction::Hold;
352
353     # BEGIN TRANSACTION
354     $patron = new ILS::Patron $patron_id;
355     if (!$patron) {
356         $trans->screen_msg("Invalid patron barcode.");
357
358         return $trans;
359     }
360
361     foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
362         $hold = $patron->{hold_items}[$i];
363
364         if ($hold->{item_id} eq $item_id) {
365             # Found it.  So fix it.
366             $hold->{expiration_date} = $expiry_date if $expiry_date;
367             $hold->{pickup_location} = $pickup_location if $pickup_location;
368             $hold->{hold_type} = $hold_type if $hold_type;
369
370             $trans->ok(1);
371             $trans->screen_msg("Hold updated.");
372             $trans->patron($patron);
373             $trans->item(new ILS::Item $hold->{item_id});
374             last;
375         }
376     }
377
378     # The same hold structure is linked into both the patron's
379     # list of hold items and into the queue of outstanding holds
380     # for the item, so we don't need to search the hold queue for
381     # the item, since it's already been updated by the patron code.
382
383     if (!$trans->ok) {
384         $trans->screen_msg("No such outstanding hold.");
385     }
386
387     return $trans;
388 }
389
390 sub renew {
391     my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
392         $no_block, $nb_due_date, $third_party,
393         $item_props, $fee_ack) = @_;
394     my ($patron, $item);
395     my $trans;
396
397     $trans = new ILS::Transaction::Renew;
398     $trans->patron($patron = new ILS::Patron $patron_id);
399
400     if (!$patron) {
401                 $trans->screen_msg("Invalid patron barcode.");
402
403                 return $trans;
404     } elsif (!$patron->renew_ok) {
405                 $trans->screen_msg("Renewals not allowed.");
406
407                 return $trans;
408     }
409
410     if (defined($title_id)) {
411         # renewing a title, rather than an item (sort of)
412         # This is gross, but in a real ILS it would be better
413                 foreach my $i (@{$patron->{items}}) {
414                         $item = new ILS::Item $i;
415                         last if ($title_id eq $item->title_id);
416                         $item = undef;
417                 }
418     } else {
419                 foreach my $i (@{$patron->{items}}) {
420                         if ($i == $item_id) {
421                                 # We have it checked out
422                                 $item = new ILS::Item $item_id;
423                                 last;
424                         }
425                 }
426     }
427
428     $trans->item($item);
429
430     if (!defined($item)) {
431         # It's not checked out to $patron_id
432                 $trans->screen_msg("Item not checked out to " . $patron->name);
433     } elsif (!$item->available($patron_id)) {
434                 $trans->screen_msg("Item has outstanding holds");
435     } else {
436                 $trans->renewal_ok(1);
437
438                 $trans->desensitize(0); # It's already checked out
439                 $trans->do_renew();
440 #               warn "done renew $trans->renewal_ok(1);";
441
442 #               if ($no_block eq 'Y') {
443 #                       $item->{due_date} = $nb_due_date;
444 #               } else {
445 #                       $item->{due_date} = time + (14*24*60*60); # two weeks
446 #               }
447 #               if ($item_props) {
448 #                       $item->{sip_item_properties} = $item_props;
449 #               }
450 #               $trans->ok(1);
451 #               return $trans;
452     }
453
454     return $trans;
455 }
456
457 sub renew_all {
458     my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
459     my ($patron, $item_id);
460     my $trans;
461
462     $trans = new ILS::Transaction::RenewAll;
463
464     $trans->patron($patron = new ILS::Patron $patron_id);
465     if (defined $patron) {
466         syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s",
467                $patron->name, $patron->renew_ok);
468     } else {
469         syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'",
470                $patron_id);
471     }
472
473     if (!defined($patron)) {
474         $trans->screen_msg("Invalid patron barcode.");
475         return $trans;
476     } elsif (!$patron->renew_ok) {
477         $trans->screen_msg("Renewals not allowed.");
478         return $trans;
479     } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
480         $trans->screen_msg("Invalid patron password.");
481         return $trans;
482     }
483
484     foreach $item_id (@{$patron->{items}}) {
485         my $item = new ILS::Item $item_id;
486
487         if (!defined($item)) {
488             syslog("LOG_WARNING",
489                    "renew_all: Invalid item id associated with patron '%s'",
490                    $patron->id);
491             next;
492         }
493
494         if (@{$item->hold_queue}) {
495             # Can't renew if there are outstanding holds
496             push @{$trans->unrenewed}, $item_id;
497         } else {
498             $item->{due_date} = time + (14*24*60*60); # two weeks hence
499             push @{$trans->renewed}, $item_id;
500         }
501     }
502
503     $trans->ok(1);
504
505     return $trans;
506 }
507
508 1;