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