updating ILS::Patron for Koha.
[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'        => 0,
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
58     return ILS::Patron->new(@_);
59 }
60
61 sub find_item {
62     my $self = shift;
63
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
139     # BEGIN TRANSACTION
140     $circ->patron($patron = new ILS::Patron $patron_id);
141     $circ->item($item = new ILS::Item $item_id);
142
143     if (!$patron) {
144         $circ->screen_msg("Invalid Patron");
145     } elsif (!$patron->charge_ok) {
146         $circ->screen_msg("Patron Blocked");
147     } elsif (!$item) {
148         $circ->screen_msg("Invalid Item");
149     } elsif (@{$item->hold_queue} && ($patron_id ne $item->hold_queue->[0])) {
150         $circ->screen_msg("Item on Hold for Another User");
151     } elsif ($item->{patron} && ($item->{patron} ne $patron_id)) {
152         # I can't deal with this right now
153         $circ->screen_msg("Item checked out to another patron");
154     } else {
155         $circ->ok(1);
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));
159         $item->{patron} = $patron_id;
160         $item->{due_date} = time + (14*24*60*60); # two weeks
161         push(@{$patron->{items}}, $item_id);
162         $circ->desensitize(!$item->magnetic);
163
164         syslog("LOG_DEBUG", "ILS::Checkout: patron %s has checked out %s",
165                $patron_id, join(', ', @{$patron->{items}}));
166     }
167
168     # END TRANSACTION
169
170     return $circ;
171 }
172
173 sub checkin {
174     my ($self, $item_id, $trans_date, $return_date,
175         $current_loc, $item_props, $cancel) = @_;
176     my ($patron, $item, $circ);
177
178     $circ = new ILS::Transaction::Checkin;
179     # BEGIN TRANSACTION
180     $circ->item($item = new ILS::Item $item_id);
181
182     # It's ok to check it in if it exists, and if it was checked out
183     $circ->ok($item && $item->{patron});
184
185     if ($circ->ok) {
186         $circ->patron($patron = new ILS::Patron $item->{patron});
187         delete $item->{patron};
188         delete $item->{due_date};
189         $patron->{items} = [ grep {$_ ne $item_id} @{$patron->{items}} ];
190     }
191     # END TRANSACTION
192
193     return $circ;
194 }
195
196 # If the ILS caches patron information, this lets it free
197 # it up
198 sub end_patron_session {
199     my ($self, $patron_id) = @_;
200
201     # success?, screen_msg, print_line
202     return (1, 'Thank you for using Evergreen!', '');
203 }
204
205 sub pay_fee {
206     my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
207         $pay_type, $fee_id, $trans_id, $currency) = @_;
208     my $trans;
209     my $patron;
210
211     $trans = new ILS::Transaction::FeePayment;
212
213     $patron = new ILS::Patron $patron_id;
214
215     $trans->transaction_id($trans_id);
216     $trans->patron($patron);
217     $trans->ok(1);
218
219     return $trans;
220 }
221
222 sub add_hold {
223     my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
224         $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
225     my ($patron, $item);
226     my $hold;
227     my $trans;
228
229
230     $trans = new ILS::Transaction::Hold;
231
232     # BEGIN TRANSACTION
233     $patron = new ILS::Patron $patron_id;
234     if (!$patron
235         || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
236         $trans->screen_msg("Invalid Patron.");
237
238         return $trans;
239     }
240
241     $item = new ILS::Item ($item_id || $title_id);
242     if (!$item) {
243         $trans->screen_msg("No such item.");
244
245         # END TRANSACTION (conditionally)
246         return $trans;
247     } elsif ($item->fee && ($fee_ack ne 'Y')) {
248         $trans->screen_msg = "Fee required to place hold.";
249
250         # END TRANSACTION (conditionally)
251         return $trans;
252     }
253
254     $hold = {
255         item_id         => $item->id,
256         patron_id       => $patron->id,
257         expiration_date => $expiry_date,
258         pickup_location => $pickup_location,
259         hold_type       => $hold_type,
260     };
261
262     $trans->ok(1);
263     $trans->patron($patron);
264     $trans->item($item);
265     $trans->pickup_location($pickup_location);
266
267     push(@{$item->hold_queue}, $hold);
268     push(@{$patron->{hold_items}}, $hold);
269
270
271     # END TRANSACTION
272     return $trans;
273 }
274
275 sub cancel_hold {
276     my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
277     my ($patron, $item, $hold);
278     my $trans;
279
280     $trans = new ILS::Transaction::Hold;
281
282     # BEGIN TRANSACTION
283     $patron = new ILS::Patron $patron_id;
284     if (!$patron) {
285         $trans->screen_msg("Invalid patron barcode.");
286
287         return $trans;
288     } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
289         $trans->screen_msg('Invalid patron password.');
290
291         return $trans;
292     }
293
294     $item = new ILS::Item ($item_id || $title_id);
295     if (!$item) {
296         $trans->screen_msg("No such item.");
297
298         # END TRANSACTION (conditionally)
299         return $trans;
300     }
301
302     # Remove the hold from the patron's record first
303     $trans->ok($patron->drop_hold($item_id));
304
305     if (!$trans->ok) {
306         # We didn't find it on the patron record
307         $trans->screen_msg("No such hold on patron record.");
308
309         # END TRANSACTION (conditionally)
310         return $trans;
311     }
312
313     # Now, remove it from the item record.  If it was on the patron
314     # record but not on the item record, we'll treat that as success.
315     foreach my $i (0 .. scalar @{$item->hold_queue}) {
316         $hold = $item->hold_queue->[$i];
317
318         if ($hold->{patron_id} eq $patron->id) {
319             # found it: delete it.
320             splice @{$item->hold_queue}, $i, 1;
321             last;
322         }
323     }
324
325     $trans->screen_msg("Hold Cancelled.");
326     $trans->patron($patron);
327     $trans->item($item);
328
329     return $trans;
330 }
331
332
333 # The patron and item id's can't be altered, but the
334 # date, location, and type can.
335 sub alter_hold {
336     my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
337         $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
338     my ($patron, $item);
339     my $hold;
340     my $trans;
341
342     $trans = new ILS::Transaction::Hold;
343
344     # BEGIN TRANSACTION
345     $patron = new ILS::Patron $patron_id;
346     if (!$patron) {
347         $trans->screen_msg("Invalid patron barcode.");
348
349         return $trans;
350     }
351
352     foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
353         $hold = $patron->{hold_items}[$i];
354
355         if ($hold->{item_id} eq $item_id) {
356             # Found it.  So fix it.
357             $hold->{expiration_date} = $expiry_date if $expiry_date;
358             $hold->{pickup_location} = $pickup_location if $pickup_location;
359             $hold->{hold_type} = $hold_type if $hold_type;
360
361             $trans->ok(1);
362             $trans->screen_msg("Hold updated.");
363             $trans->patron($patron);
364             $trans->item(new ILS::Item $hold->{item_id});
365             last;
366         }
367     }
368
369     # The same hold structure is linked into both the patron's
370     # list of hold items and into the queue of outstanding holds
371     # for the item, so we don't need to search the hold queue for
372     # the item, since it's already been updated by the patron code.
373
374     if (!$trans->ok) {
375         $trans->screen_msg("No such outstanding hold.");
376     }
377
378     return $trans;
379 }
380
381 sub renew {
382     my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
383         $no_block, $nb_due_date, $third_party,
384         $item_props, $fee_ack) = @_;
385     my ($patron, $item);
386     my $trans;
387
388     $trans = new ILS::Transaction::Renew;
389
390     $trans->patron($patron = new ILS::Patron $patron_id);
391
392     if (!$patron) {
393         $trans->screen_msg("Invalid patron barcode.");
394
395         return $trans;
396     } elsif (!$patron->renew_ok) {
397         $trans->screen_msg("Renewals not allowed.");
398
399         return $trans;
400     }
401
402     if (defined($title_id)) {
403         # renewing a title, rather than an item (sort of)
404         # This is gross, but in a real ILS it would be better
405         foreach my $i (@{$patron->{items}}) {
406             $item = new ILS::Item $i;
407             last if ($title_id eq $item->title_id);
408             $item = undef;
409         }
410     } else {
411         foreach my $i (@{$patron->{items}}) {
412             if ($i == $item_id) {
413                 # We have it checked out
414                 $item = new ILS::Item $item_id;
415                 last;
416             }
417         }
418     }
419
420     $trans->item($item);
421
422     if (!defined($item)) {
423         # It's not checked out to $patron_id
424         $trans->screen_msg("Item not checked out to " . $patron->name);
425     } elsif (!$item->available($patron_id)) {
426          $trans->screen_msg("Item has outstanding holds");
427     } else {
428         $trans->renewal_ok(1);
429
430         $trans->desensitize(0); # It's already checked out
431
432         if ($no_block eq 'Y') {
433             $item->{due_date} = $nb_due_date;
434         } else {
435             $item->{due_date} = time + (14*24*60*60); # two weeks
436         }
437         if ($item_props) {
438             $item->{sip_item_properties} = $item_props;
439         }
440         $trans->ok(1);
441         $trans->renewal_ok(1);
442
443         return $trans;
444     }
445
446     return $trans;
447 }
448
449 sub renew_all {
450     my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
451     my ($patron, $item_id);
452     my $trans;
453
454     $trans = new ILS::Transaction::RenewAll;
455
456     $trans->patron($patron = new ILS::Patron $patron_id);
457     if (defined $patron) {
458         syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s",
459                $patron->name, $patron->renew_ok);
460     } else {
461         syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'",
462                $patron_id);
463     }
464
465     if (!defined($patron)) {
466         $trans->screen_msg("Invalid patron barcode.");
467         return $trans;
468     } elsif (!$patron->renew_ok) {
469         $trans->screen_msg("Renewals not allowed.");
470         return $trans;
471     } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
472         $trans->screen_msg("Invalid patron password.");
473         return $trans;
474     }
475
476     foreach $item_id (@{$patron->{items}}) {
477         my $item = new ILS::Item $item_id;
478
479         if (!defined($item)) {
480             syslog("LOG_WARNING",
481                    "renew_all: Invalid item id associated with patron '%s'",
482                    $patron->id);
483             next;
484         }
485
486         if (@{$item->hold_queue}) {
487             # Can't renew if there are outstanding holds
488             push @{$trans->unrenewed}, $item_id;
489         } else {
490             $item->{due_date} = time + (14*24*60*60); # two weeks hence
491             push @{$trans->renewed}, $item_id;
492         }
493     }
494
495     $trans->ok(1);
496
497     return $trans;
498 }
499
500 1;