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