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