]> git.evergreen-ils.org Git - working/SIPServer.git/blob - ILS.pm
Avoid undef warning, provide lanuage failover to 000 (unknown/default)
[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     my $trans;
225     my $patron;
226
227     $trans = new ILS::Transaction::FeePayment;
228
229     $patron = new ILS::Patron $patron_id;
230
231     $trans->transaction_id($trans_id);
232     $trans->patron($patron);
233     $trans->ok(1);
234
235     return $trans;
236 }
237
238 sub add_hold {
239     my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
240         $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
241     my ($patron, $item);
242     my $hold;
243     my $trans;
244
245
246     $trans = new ILS::Transaction::Hold;
247
248     # BEGIN TRANSACTION
249     $patron = new ILS::Patron $patron_id;
250     if (!$patron
251         || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
252         $trans->screen_msg("Invalid Patron.");
253
254         return $trans;
255     }
256
257     $item = new ILS::Item ($item_id || $title_id);
258     if (!$item) {
259         $trans->screen_msg("No such item.");
260
261         # END TRANSACTION (conditionally)
262         return $trans;
263     } elsif ($item->fee && ($fee_ack ne 'Y')) {
264         $trans->screen_msg = "Fee required to place hold.";
265
266         # END TRANSACTION (conditionally)
267         return $trans;
268     }
269
270     $hold = {
271         item_id         => $item->id,
272         patron_id       => $patron->id,
273         expiration_date => $expiry_date,
274         pickup_location => $pickup_location,
275         hold_type       => $hold_type,
276     };
277
278     $trans->ok(1);
279     $trans->patron($patron);
280     $trans->item($item);
281     $trans->pickup_location($pickup_location);
282
283     push(@{$item->hold_queue}, $hold);
284     push(@{$patron->{hold_items}}, $hold);
285
286
287     # END TRANSACTION
288     return $trans;
289 }
290
291 sub cancel_hold {
292     my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
293     my ($patron, $item, $hold);
294     my $trans;
295
296     $trans = new ILS::Transaction::Hold;
297
298     # BEGIN TRANSACTION
299     $patron = new ILS::Patron $patron_id;
300     if (!$patron) {
301         $trans->screen_msg("Invalid patron barcode.");
302
303         return $trans;
304     } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
305         $trans->screen_msg('Invalid patron password.');
306
307         return $trans;
308     }
309
310     $item = new ILS::Item ($item_id || $title_id);
311     if (!$item) {
312         $trans->screen_msg("No such item.");
313
314         # END TRANSACTION (conditionally)
315         return $trans;
316     }
317
318     # Remove the hold from the patron's record first
319     $trans->ok($patron->drop_hold($item_id));
320
321     if (!$trans->ok) {
322         # We didn't find it on the patron record
323         $trans->screen_msg("No such hold on patron record.");
324
325         # END TRANSACTION (conditionally)
326         return $trans;
327     }
328
329     # Now, remove it from the item record.  If it was on the patron
330     # record but not on the item record, we'll treat that as success.
331     foreach my $i (0 .. scalar @{$item->hold_queue}) {
332         $hold = $item->hold_queue->[$i];
333
334         if ($hold->{patron_id} eq $patron->id) {
335             # found it: delete it.
336             splice @{$item->hold_queue}, $i, 1;
337             last;
338         }
339     }
340
341     $trans->screen_msg("Hold Cancelled.");
342     $trans->patron($patron);
343     $trans->item($item);
344
345     return $trans;
346 }
347
348
349 # The patron and item id's can't be altered, but the
350 # date, location, and type can.
351 sub alter_hold {
352     my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
353         $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
354     my ($patron, $item);
355     my $hold;
356     my $trans;
357
358     $trans = new ILS::Transaction::Hold;
359
360     # BEGIN TRANSACTION
361     $patron = new ILS::Patron $patron_id;
362     if (!$patron) {
363         $trans->screen_msg("Invalid patron barcode.");
364
365         return $trans;
366     }
367
368     foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
369         $hold = $patron->{hold_items}[$i];
370
371         if ($hold->{item_id} eq $item_id) {
372             # Found it.  So fix it.
373             $hold->{expiration_date} = $expiry_date if $expiry_date;
374             $hold->{pickup_location} = $pickup_location if $pickup_location;
375             $hold->{hold_type} = $hold_type if $hold_type;
376
377             $trans->ok(1);
378             $trans->screen_msg("Hold updated.");
379             $trans->patron($patron);
380             $trans->item(new ILS::Item $hold->{item_id});
381             last;
382         }
383     }
384
385     # The same hold structure is linked into both the patron's
386     # list of hold items and into the queue of outstanding holds
387     # for the item, so we don't need to search the hold queue for
388     # the item, since it's already been updated by the patron code.
389
390     if (!$trans->ok) {
391         $trans->screen_msg("No such outstanding hold.");
392     }
393
394     return $trans;
395 }
396
397 sub renew {
398     my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
399         $no_block, $nb_due_date, $third_party,
400         $item_props, $fee_ack) = @_;
401     my ($patron, $item);
402     my $trans;
403
404     $trans = new ILS::Transaction::Renew;
405
406     $trans->patron($patron = new ILS::Patron $patron_id);
407
408     if (!$patron) {
409         $trans->screen_msg("Invalid patron barcode.");
410
411         return $trans;
412     } elsif (!$patron->renew_ok) {
413         $trans->screen_msg("Renewals not allowed.");
414
415         return $trans;
416     }
417
418     if (defined($title_id)) {
419         # renewing a title, rather than an item (sort of)
420         # This is gross, but in a real ILS it would be better
421         foreach my $i (@{$patron->{items}}) {
422             $item = new ILS::Item $i;
423             last if ($title_id eq $item->title_id);
424             $item = undef;
425         }
426     } else {
427         foreach my $i (@{$patron->{items}}) {
428             if ($i == $item_id) {
429                 # We have it checked out
430                 $item = new ILS::Item $item_id;
431                 last;
432             }
433         }
434     }
435
436     $trans->item($item);
437
438     if (!defined($item)) {
439         # It's not checked out to $patron_id
440         $trans->screen_msg("Item not checked out to " . $patron->name);
441     } elsif (!$item->available($patron_id)) {
442          $trans->screen_msg("Item has outstanding holds");
443     } else {
444         $trans->renewal_ok(1);
445
446         $trans->desensitize(0); # It's already checked out
447
448         if ($no_block eq 'Y') {
449             $item->{due_date} = $nb_due_date;
450         } else {
451             $item->{due_date} = time + (14*24*60*60); # two weeks
452         }
453         if ($item_props) {
454             $item->{sip_item_properties} = $item_props;
455         }
456         $trans->ok(1);
457         $trans->renewal_ok(1);
458
459         return $trans;
460     }
461
462     return $trans;
463 }
464
465 sub renew_all {
466     my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
467     my ($patron, $item_id);
468     my $trans;
469
470     $trans = new ILS::Transaction::RenewAll;
471
472     $trans->patron($patron = new ILS::Patron $patron_id);
473     if (defined $patron) {
474         syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s",
475                $patron->name, $patron->renew_ok);
476     } else {
477         syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'",
478                $patron_id);
479     }
480
481     if (!defined($patron)) {
482         $trans->screen_msg("Invalid patron barcode.");
483         return $trans;
484     } elsif (!$patron->renew_ok) {
485         $trans->screen_msg("Renewals not allowed.");
486         return $trans;
487     } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
488         $trans->screen_msg("Invalid patron password.");
489         return $trans;
490     }
491
492     foreach $item_id (@{$patron->{items}}) {
493         my $item = new ILS::Item $item_id;
494
495         if (!defined($item)) {
496             syslog("LOG_WARNING",
497                    "renew_all: Invalid item id associated with patron '%s'",
498                    $patron->id);
499             next;
500         }
501
502         if (@{$item->hold_queue}) {
503             # Can't renew if there are outstanding holds
504             push @{$trans->unrenewed}, $item_id;
505         } else {
506             $item->{due_date} = time + (14*24*60*60); # two weeks hence
507             push @{$trans->renewed}, $item_id;
508         }
509     }
510
511     $trans->ok(1);
512
513     return $trans;
514 }
515
516 1;