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