]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/SIP.pm
bddd83df8c0ca9d67a788db7534320b18c5a0e09
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / SIP.pm
1 #
2 # ILS.pm: Test ILS interface module
3 #
4
5 package OpenILS::SIP;
6 use warnings; use strict;
7
8 use Sys::Syslog qw(syslog);
9
10 use OpenILS::SIP::Item;
11 use OpenILS::SIP::Patron;
12 use OpenILS::SIP::Transaction;
13 use OpenILS::SIP::Transaction::Checkout;
14 use OpenILS::SIP::Transaction::Checkin;
15 use OpenILS::SIP::Transaction::Renew;
16
17 use OpenSRF::System;
18 use OpenILS::Utils::Fieldmapper;
19 use OpenSRF::Utils::SettingsClient;
20 use OpenILS::Application::AppUtils;
21 my $U = 'OpenILS::Application::AppUtils';
22
23 my $editor;
24 my $config;
25
26 use Digest::MD5 qw(md5_hex);
27
28 sub new {
29         my ($class, $institution, $login) = @_;
30         my $type = ref($class) || $class;
31         my $self = {};
32
33         $config = $institution;
34         syslog("LOG_DEBUG", "OILS: new ILS '%s'", $institution->{id});
35         $self->{institution} = $institution;
36
37         my $bsconfig = $institution->{implementation_config}->{bootstrap};
38
39         syslog('LOG_DEBUG', "OILS: loading bootstrap config: $bsconfig");
40         
41         local $/ = "\n";
42         OpenSRF::System->bootstrap_client(config_file => $bsconfig);
43         syslog('LOG_DEBUG', "OILS: bootstrap loaded..");
44
45         $self->{osrf_config} = OpenSRF::Utils::SettingsClient->new;
46
47         Fieldmapper->import($self->{osrf_config}->config_value('IDL'));
48
49         bless( $self, $type );
50
51         return undef unless 
52                 $self->login( $login->{id}, $login->{password} );
53
54         return $self;
55 }
56
57 sub to_bool {
58         my $val = shift;
59         return ($val and $val =~ /true/io);
60 }
61
62 sub editor {
63         $editor = make_editor() unless $editor;
64         return $editor;
65 }
66 sub reset_editor {
67         $editor = undef;
68         return editor();
69 }
70
71 sub config {
72         return $config;
73 }
74
75
76 # Creates the global editor object
77 sub make_editor {
78         require OpenILS::Utils::CStoreEditor;
79         my $e = OpenILS::Utils::CStoreEditor->new(xact => 1);
80         # gnarly cstore hack to re-gen autogen methods after IDL is loaded
81         if(!UNIVERSAL::can($e, 'search_actor_card')) {
82                 syslog("LOG_WARNING", "OILS: Reloading CStoreEditor...");
83                 delete $INC{'OpenILS/Utils/CStoreEditor.pm'};
84                 require OpenILS::Utils::CStoreEditor;
85                 $e = OpenILS::Utils::CStoreEditor->new(xact =>1);
86         }
87         return $e;
88 }
89
90
91
92 sub login {
93         my( $self, $username, $password ) = @_;
94         syslog('LOG_DEBUG', "OILS: Logging in with username $username");
95
96         my $seed = $U->simplereq( 
97                 'open-ils.auth',
98                 'open-ils.auth.authenticate.init', $username );
99
100         my $response = $U->simplereq(
101                 'open-ils.auth', 
102                 'open-ils.auth.authenticate.complete', 
103                 {       
104                         username => $username, 
105                         password => md5_hex($seed . md5_hex($password)), 
106                         type            => 'opac',
107                 }
108         );
109
110         if( my $code = $U->event_code($response) ) {
111                 my $txt = $response->{textcode};
112                 syslog('LOG_WARNING', "OILS: Login failed for $username.  $txt:$code");
113                 return undef;
114         }
115
116         my $key = $response->{payload}->{authtoken};
117         syslog('LOG_INFO', "OILS: Login succeeded for $username : authkey = $key");
118         return $self->{authtoken} = $key;
119 }
120
121
122 sub find_patron {
123         my $self = shift;
124         return OpenILS::SIP::Patron->new(@_);
125 }
126
127
128 sub find_item {
129         my $self = shift;
130         return OpenILS::SIP::Item->new(@_);
131 }
132
133
134 sub institution {
135     my $self = shift;
136     return $self->{institution}->{id};
137 }
138
139 sub supports {
140         my ($self, $op) = @_;
141         my ($i) = grep { $_->{name} eq $op }  
142                 @{$config->{implementation_config}->{supports}->{item}};
143         return to_bool($i->{value});
144 }
145
146 sub check_inst_id {
147         my ($self, $id, $whence) = @_;
148         if ($id ne $self->{institution}->{id}) {
149                 syslog("LOG_WARNING", 
150                         "OILS: %s: received institution '%s', expected '%s'",
151                         $whence, $id, $self->{institution}->{id});
152         }
153 }
154
155 sub checkout_ok {
156         return to_bool($config->{policy}->{checkout});
157 }
158
159 sub checkin_ok {
160         return to_bool($config->{policy}->{checkin});
161     return 0;
162 }
163
164 sub renew_ok {
165         return to_bool($config->{policy}->{renew});
166 }
167
168 sub status_update_ok {
169         return to_bool($config->{policy}->{status_update});
170 }
171
172 sub offline_ok {
173         return to_bool($config->{policy}->{offline});
174 }
175
176
177
178 ##
179 ## Checkout(patron_id, item_id, sc_renew):
180 ##    patron_id & item_id are the identifiers send by the terminal
181 ##    sc_renew is the renewal policy configured on the terminal
182 ## returns a status opject that can be queried for the various bits
183 ## of information that the protocol (SIP or NCIP) needs to generate
184 ## the response.
185 ##
186
187 sub checkout {
188         my ($self, $patron_id, $item_id, $sc_renew) = @_;
189         
190         syslog('LOG_DEBUG', "OILS: OpenILS::Checkout attempt: patron=$patron_id, item=$item_id");
191         
192         my $xact                = OpenILS::SIP::Transaction::Checkout->new( authtoken => $self->{authtoken} );
193         my $patron      = $self->find_patron($patron_id);
194         my $item                = $self->find_item($item_id);
195         
196         $xact->patron($patron);
197         $xact->item($item);
198
199         if (!$patron) {
200                 $xact->screen_msg("Invalid Patron");
201                 return $xact;
202         }
203
204         if (!$patron->charge_ok) {
205                 $xact->screen_msg("Patron Blocked");
206                 return $xact;
207         }
208
209         if( !$item ) {
210                 $xact->screen_msg("Invalid Item");
211                 return $xact;
212         }
213
214         syslog('LOG_DEBUG', "OILS: OpenILS::Checkout data loaded OK, checking out...");
215         $xact->do_checkout();
216
217         if ($item->{patron} && ($item->{patron} ne $patron_id)) {
218                 # I can't deal with this right now
219                 # XXX check in then check out?
220                 $xact->screen_msg("Item checked out to another patron");
221                 $xact->ok(0);
222         } 
223
224         $xact->desensitize(!$item->magnetic);
225
226         if( $xact->ok ) {
227
228                 #editor()->commit;
229                 syslog("LOG_DEBUG", "OILS: OpenILS::Checkout: " .
230                         "patron %s checkout %s succeeded", $patron_id, $item_id);
231
232         } else {
233
234                 #editor()->xact_rollback;
235                 syslog("LOG_DEBUG", "OILS: OpenILS::Checkout: " .
236                         "patron %s checkout %s FAILED, rolling back xact...", $patron_id, $item_id);
237         }
238
239         return $xact;
240 }
241
242
243 sub checkin {
244         my ($self, $item_id, $trans_date, $return_date,
245         $current_loc, $item_props, $cancel) = @_;
246
247         syslog('LOG_DEBUG', "OILS: OpenILS::Checkin on item=$item_id");
248         
249         my $patron;
250         my $xact                = OpenILS::SIP::Transaction::Checkin->new(authtoken => $self->{authtoken});
251         my $item                = $self->find_item($item_id);
252
253         $xact->item($item);
254
255         if(!$xact->item) {
256                 $xact->screen_msg("Invalid item barcode: $item_id");
257                 $xact->ok(0);
258                 return $xact;
259         }
260
261         $xact->do_checkin( $trans_date, $return_date, $current_loc, $item_props );
262         
263         if ($xact->ok) {
264
265                 $xact->patron($patron = $self->find_patron($item->{patron}));
266                 delete $item->{patron};
267                 delete $item->{due_date};
268                 syslog('LOG_INFO', "OILS: Checkin succeeded");
269                 #editor()->commit;
270
271         } else {
272
273                 #editor()->xact_rollback;
274                 syslog('LOG_WARNING', "OILS: Checkin failed");
275         }
276         # END TRANSACTION
277
278         return $xact;
279 }
280
281 ## If the ILS caches patron information, this lets it free it up
282 sub end_patron_session {
283     my ($self, $patron_id) = @_;
284     return (1, 'Thank you for using OpenILS!', '');
285 }
286
287
288 #sub pay_fee {
289 #    my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
290 #       $pay_type, $fee_id, $trans_id, $currency) = @_;
291 #    my $trans;
292 #    my $patron;
293 #
294 #    $trans = new ILS::Transaction::FeePayment;
295 #
296 #    $patron = new ILS::Patron $patron_id;
297 #
298 #    $trans->transaction_id($trans_id);
299 #    $trans->patron($patron);
300 #    $trans->ok(1);
301 #
302 #    return $trans;
303 #}
304 #
305 #sub add_hold {
306 #    my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
307 #       $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
308 #    my ($patron, $item);
309 #    my $hold;
310 #    my $trans;
311 #
312 #
313 #    $trans = new ILS::Transaction::Hold;
314 #
315 #    # BEGIN TRANSACTION
316 #    $patron = new ILS::Patron $patron_id;
317 #    if (!$patron
318 #       || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
319 #       $trans->screen_msg("Invalid Patron.");
320 #
321 #       return $trans;
322 #    }
323 #
324 #    $item = new ILS::Item ($item_id || $title_id);
325 #    if (!$item) {
326 #       $trans->screen_msg("No such item.");
327 #
328 #       # END TRANSACTION (conditionally)
329 #       return $trans;
330 #    } elsif ($item->fee && ($fee_ack ne 'Y')) {
331 #       $trans->screen_msg = "Fee required to place hold.";
332 #
333 #       # END TRANSACTION (conditionally)
334 #       return $trans;
335 #    }
336 #
337 #    $hold = {
338 #       item_id         => $item->id,
339 #       patron_id       => $patron->id,
340 #       expiration_date => $expiry_date,
341 #       pickup_location => $pickup_location,
342 #       hold_type       => $hold_type,
343 #    };
344 #
345 #    $trans->ok(1);
346 #    $trans->patron($patron);
347 #    $trans->item($item);
348 #    $trans->pickup_location($pickup_location);
349 #
350 #    push(@{$item->hold_queue}, $hold);
351 #    push(@{$patron->{hold_items}}, $hold);
352 #
353 #
354 #    # END TRANSACTION
355 #    return $trans;
356 #}
357 #
358 #sub cancel_hold {
359 #    my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
360 #    my ($patron, $item, $hold);
361 #    my $trans;
362 #
363 #    $trans = new ILS::Transaction::Hold;
364 #
365 #    # BEGIN TRANSACTION
366 #    $patron = new ILS::Patron $patron_id;
367 #    if (!$patron) {
368 #       $trans->screen_msg("Invalid patron barcode.");
369 #
370 #       return $trans;
371 #    } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
372 #       $trans->screen_msg('Invalid patron password.');
373 #
374 #       return $trans;
375 #    }
376 #
377 #    $item = new ILS::Item ($item_id || $title_id);
378 #    if (!$item) {
379 #       $trans->screen_msg("No such item.");
380 #
381 #       # END TRANSACTION (conditionally)
382 #       return $trans;
383 #    }
384 #
385 #    # Remove the hold from the patron's record first
386 #    $trans->ok($patron->drop_hold($item_id));
387 #
388 #    if (!$trans->ok) {
389 #       # We didn't find it on the patron record
390 #       $trans->screen_msg("No such hold on patron record.");
391 #
392 #       # END TRANSACTION (conditionally)
393 #       return $trans;
394 #    }
395 #
396 #    # Now, remove it from the item record.  If it was on the patron
397 #    # record but not on the item record, we'll treat that as success.
398 #    foreach my $i (0 .. scalar @{$item->hold_queue}) {
399 #       $hold = $item->hold_queue->[$i];
400 #
401 #       if ($hold->{patron_id} eq $patron->id) {
402 #           # found it: delete it.
403 #           splice @{$item->hold_queue}, $i, 1;
404 #           last;
405 #       }
406 #    }
407 #
408 #    $trans->screen_msg("Hold Cancelled.");
409 #    $trans->patron($patron);
410 #    $trans->item($item);
411 #
412 #    return $trans;
413 #}
414 #
415 #
416 ## The patron and item id's can't be altered, but the
417 ## date, location, and type can.
418 #sub alter_hold {
419 #    my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
420 #       $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
421 #    my ($patron, $item);
422 #    my $hold;
423 #    my $trans;
424 #
425 #    $trans = new ILS::Transaction::Hold;
426 #
427 #    # BEGIN TRANSACTION
428 #    $patron = new ILS::Patron $patron_id;
429 #    if (!$patron) {
430 #       $trans->screen_msg("Invalid patron barcode.");
431 #
432 #       return $trans;
433 #    }
434 #
435 #    foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
436 #       $hold = $patron->{hold_items}[$i];
437 #
438 #       if ($hold->{item_id} eq $item_id) {
439 #           # Found it.  So fix it.
440 #           $hold->{expiration_date} = $expiry_date if $expiry_date;
441 #           $hold->{pickup_location} = $pickup_location if $pickup_location;
442 #           $hold->{hold_type} = $hold_type if $hold_type;
443 #
444 #           $trans->ok(1);
445 #           $trans->screen_msg("Hold updated.");
446 #           $trans->patron($patron);
447 #           $trans->item(new ILS::Item $hold->{item_id});
448 #           last;
449 #       }
450 #    }
451 #
452 #    # The same hold structure is linked into both the patron's
453 #    # list of hold items and into the queue of outstanding holds
454 #    # for the item, so we don't need to search the hold queue for
455 #    # the item, since it's already been updated by the patron code.
456 #
457 #    if (!$trans->ok) {
458 #       $trans->screen_msg("No such outstanding hold.");
459 #    }
460 #
461 #    return $trans;
462 #}
463
464
465 sub renew {
466         my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
467                 $no_block, $nb_due_date, $third_party, $item_props, $fee_ack) = @_;
468
469         my $trans = OpenILS::SIP::Transaction::Renew->new( authtoken => $self->{authtoken} );
470         $trans->patron($self->find_patron($patron_id));
471         $trans->item($self->find_item($item_id));
472
473         if(!$trans->patron) {
474                 $trans->screen_msg("Invalid patron barcode.");
475                 $trans->ok(0);
476                 return $trans;
477         }
478
479         if(!$trans->patron->renew_ok) {
480                 $trans->screen_msg("Renewals not allowed.");
481                 $trans->ok(0);
482                 return $trans;
483         }
484
485         if(!$trans->item) {
486                 if( $title_id ) {
487                         $trans->screen_msg("Item Id renewal not supported.");
488                 } else {
489                         $trans->screen_msg("Invalid item barcode.");
490                 }
491                 $trans->ok(0);
492                 return $trans;
493         }
494
495         if(!$trans->item->{patron} or 
496                         $trans->item->{patron} ne $patron_id) {
497                 $trans->screen_msg("Item not checked out to " . $trans->patron->name);
498                 $trans->ok(0);
499                 return $trans;
500         }
501
502         # Perform the renewal
503         $trans->do_renew();
504
505         $trans->desensitize(0); # It's already checked out
506         $trans->item->{due_date} = $nb_due_date if $no_block eq 'Y';
507         $trans->item->{sip_item_properties} = $item_props if $item_props;
508
509         return $trans;
510 }
511
512
513
514
515
516 #
517 #sub renew_all {
518 #    my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
519 #    my ($patron, $item_id);
520 #    my $trans;
521 #
522 #    $trans = new ILS::Transaction::RenewAll;
523 #
524 #    $trans->patron($patron = new ILS::Patron $patron_id);
525 #    if (defined $patron) {
526 #       syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s",
527 #              $patron->name, $patron->renew_ok);
528 #    } else {
529 #       syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'",
530 #              $patron_id);
531 #    }
532 #
533 #    if (!defined($patron)) {
534 #       $trans->screen_msg("Invalid patron barcode.");
535 #       return $trans;
536 #    } elsif (!$patron->renew_ok) {
537 #       $trans->screen_msg("Renewals not allowed.");
538 #       return $trans;
539 #    } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
540 #       $trans->screen_msg("Invalid patron password.");
541 #       return $trans;
542 #    }
543 #
544 #    foreach $item_id (@{$patron->{items}}) {
545 #       my $item = new ILS::Item $item_id;
546 #
547 #       if (!defined($item)) {
548 #           syslog("LOG_WARNING",
549 #                  "renew_all: Invalid item id associated with patron '%s'",
550 #                  $patron->id);
551 #           next;
552 #       }
553 #
554 #       if (@{$item->hold_queue}) {
555 #           # Can't renew if there are outstanding holds
556 #           push @{$trans->unrenewed}, $item_id;
557 #       } else {
558 #           $item->{due_date} = time + (14*24*60*60); # two weeks hence
559 #           push @{$trans->renewed}, $item_id;
560 #       }
561 #    }
562 #
563 #    $trans->ok(1);
564 #
565 #    return $trans;
566 #}
567
568 1;