2 # ILS.pm: Test ILS interface module
6 use warnings; use strict;
8 use Sys::Syslog qw(syslog);
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::FeePayment;
16 #use OpenILS::SIP::Transaction::Hold;
17 use OpenILS::SIP::Transaction::Renew;
18 #use OpenILS::SIP::Transaction::RenewAll;
22 use OpenILS::Utils::Fieldmapper;
23 use OpenSRF::Utils::SettingsClient;
24 use OpenILS::Application::AppUtils;
25 my $U = 'OpenILS::Application::AppUtils';
27 use Digest::MD5 qw(md5_hex);
29 # PUT ME IN THE CONFIG XXX
31 'magnetic media' => 1,
32 'security inhibit' => 0,
33 'offline operation' => 0
38 my ($class, $institution, $login) = @_;
39 my $type = ref($class) || $class;
42 syslog("LOG_DEBUG", "new ILS '%s'", $institution->{id});
43 $self->{institution} = $institution;
45 my $config = $institution->{implementation_config}->{bootstrap};
47 syslog('LOG_DEBUG', "loading bootstrap config: $config");
50 OpenSRF::System->bootstrap_client(config_file => $config);
51 syslog('LOG_DEBUG', "bootstrap loaded..");
53 $self->{osrf_config} = OpenSRF::Utils::SettingsClient->new;
55 Fieldmapper->import($self->{osrf_config}->config_value('IDL'));
57 bless( $self, $type );
60 $self->login( $login->{id}, $login->{password} );
66 my( $self, $username, $password ) = @_;
67 syslog('LOG_DEBUG', "OpenILS: Logging in with username $username");
69 my $seed = $U->simplereq(
71 'open-ils.auth.authenticate.init', $username );
73 my $response = $U->simplereq(
75 'open-ils.auth.authenticate.complete',
77 username => $username,
78 password => md5_hex($seed . md5_hex($password)),
84 if( ref($response) eq 'HASH' and $response->{payload}
85 and $key = $response->{payload}->{authtoken} ) {
86 syslog('LOG_INFO', "OpenILS: Login succeeded for $username : authkey = $key");
89 syslog('LOG_WARN', "OpenILS: Login failed for $username");
92 return $self->{authtoken} = $key;
98 return OpenILS::SIP::Patron->new(@_);
104 return OpenILS::SIP::Item->new(@_);
111 return $self->{institution}->{id};
115 # XXX Get me from the config?
117 my ($self, $op) = @_;
118 return exists($supports{$op}) ? $supports{$op} : 0;
122 my ($self, $id, $whence) = @_;
124 if ($id ne $self->{institution}->{id}) {
125 syslog("LOG_WARNING", "%s: received institution '%s', expected '%s'",
126 $whence, $id, $self->{institution}->{id});
131 # XXX by default, these should come from the config
140 sub status_update_ok {
150 ## Checkout(patron_id, item_id, sc_renew):
151 ## patron_id & item_id are the identifiers send by the terminal
152 ## sc_renew is the renewal policy configured on the terminal
153 ## returns a status opject that can be queried for the various bits
154 ## of information that the protocol (SIP or NCIP) needs to generate
159 my ($self, $patron_id, $item_id, $sc_renew) = @_;
161 syslog('LOG_DEBUG', "OpenILS::Checkout attempt: patron=$patron_id, item=$item_id");
163 my $xact = OpenILS::SIP::Transaction::Checkout->new( authtoken => $self->{authtoken} );
164 my $patron = OpenILS::SIP::Patron->new($patron_id);
165 my $item = OpenILS::SIP::Item->new($item_id);
167 $xact->patron($patron);
171 $xact->screen_msg("Invalid Patron");
175 if (!$patron->charge_ok) {
176 $xact->screen_msg("Patron Blocked");
181 $xact->screen_msg("Invalid Item");
185 syslog('LOG_DEBUG', "OpenILS::Checkout data loaded OK, checking out...");
186 $xact->do_checkout();
188 if ($item->{patron} && ($item->{patron} ne $patron_id)) {
189 # I can't deal with this right now
190 # XXX check in then check out?
191 $xact->screen_msg("Item checked out to another patron");
195 $xact->desensitize(!$item->magnetic);
199 $xact->editor->commit;
200 syslog("LOG_DEBUG", "OpenILS::Checkout: " .
201 "patron %s checkout %s succeeded", $patron_id, $item_id);
205 $xact->editor->xact_rollback;
206 syslog("LOG_DEBUG", "OpenILS::Checkout: " .
207 "patron %s checkout %s FAILED, rolling back xact...", $patron_id, $item_id);
215 my ($self, $item_id, $trans_date, $return_date,
216 $current_loc, $item_props, $cancel) = @_;
218 syslog('LOG_DEBUG', "OpenILS::Checkin on item=$item_id");
221 my $xact = OpenILS::SIP::Transaction::Checkin->new(authtoken => $self->{authtoken});
222 my $item = OpenILS::SIP::Item->new($item_id);
226 $xact->do_checkin( $trans_date, $return_date, $current_loc, $item_props );
230 $xact->patron($patron = OpenILS::SIP::Patron->new($item->{patron}));
231 delete $item->{patron};
232 delete $item->{due_date};
233 syslog('LOG_INFO', "OpenILS: Checkin succeeded");
234 $xact->editor->commit;
238 $xact->editor->xact_rollback;
239 syslog('LOG_WARNING', "OpenILS: Checkin failed");
249 ## If the ILS caches patron information, this lets it free it up
250 sub end_patron_session {
251 my ($self, $patron_id) = @_;
252 return (1, 'Thank you for using OpenILS!', '');
257 # my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
258 # $pay_type, $fee_id, $trans_id, $currency) = @_;
262 # $trans = new ILS::Transaction::FeePayment;
264 # $patron = new ILS::Patron $patron_id;
266 # $trans->transaction_id($trans_id);
267 # $trans->patron($patron);
274 # my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
275 # $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
276 # my ($patron, $item);
281 # $trans = new ILS::Transaction::Hold;
283 # # BEGIN TRANSACTION
284 # $patron = new ILS::Patron $patron_id;
286 # || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
287 # $trans->screen_msg("Invalid Patron.");
292 # $item = new ILS::Item ($item_id || $title_id);
294 # $trans->screen_msg("No such item.");
296 # # END TRANSACTION (conditionally)
298 # } elsif ($item->fee && ($fee_ack ne 'Y')) {
299 # $trans->screen_msg = "Fee required to place hold.";
301 # # END TRANSACTION (conditionally)
306 # item_id => $item->id,
307 # patron_id => $patron->id,
308 # expiration_date => $expiry_date,
309 # pickup_location => $pickup_location,
310 # hold_type => $hold_type,
314 # $trans->patron($patron);
315 # $trans->item($item);
316 # $trans->pickup_location($pickup_location);
318 # push(@{$item->hold_queue}, $hold);
319 # push(@{$patron->{hold_items}}, $hold);
327 # my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
328 # my ($patron, $item, $hold);
331 # $trans = new ILS::Transaction::Hold;
333 # # BEGIN TRANSACTION
334 # $patron = new ILS::Patron $patron_id;
336 # $trans->screen_msg("Invalid patron barcode.");
339 # } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
340 # $trans->screen_msg('Invalid patron password.');
345 # $item = new ILS::Item ($item_id || $title_id);
347 # $trans->screen_msg("No such item.");
349 # # END TRANSACTION (conditionally)
353 # # Remove the hold from the patron's record first
354 # $trans->ok($patron->drop_hold($item_id));
357 # # We didn't find it on the patron record
358 # $trans->screen_msg("No such hold on patron record.");
360 # # END TRANSACTION (conditionally)
364 # # Now, remove it from the item record. If it was on the patron
365 # # record but not on the item record, we'll treat that as success.
366 # foreach my $i (0 .. scalar @{$item->hold_queue}) {
367 # $hold = $item->hold_queue->[$i];
369 # if ($hold->{patron_id} eq $patron->id) {
370 # # found it: delete it.
371 # splice @{$item->hold_queue}, $i, 1;
376 # $trans->screen_msg("Hold Cancelled.");
377 # $trans->patron($patron);
378 # $trans->item($item);
384 ## The patron and item id's can't be altered, but the
385 ## date, location, and type can.
387 # my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
388 # $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
389 # my ($patron, $item);
393 # $trans = new ILS::Transaction::Hold;
395 # # BEGIN TRANSACTION
396 # $patron = new ILS::Patron $patron_id;
398 # $trans->screen_msg("Invalid patron barcode.");
403 # foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
404 # $hold = $patron->{hold_items}[$i];
406 # if ($hold->{item_id} eq $item_id) {
407 # # Found it. So fix it.
408 # $hold->{expiration_date} = $expiry_date if $expiry_date;
409 # $hold->{pickup_location} = $pickup_location if $pickup_location;
410 # $hold->{hold_type} = $hold_type if $hold_type;
413 # $trans->screen_msg("Hold updated.");
414 # $trans->patron($patron);
415 # $trans->item(new ILS::Item $hold->{item_id});
420 # # The same hold structure is linked into both the patron's
421 # # list of hold items and into the queue of outstanding holds
422 # # for the item, so we don't need to search the hold queue for
423 # # the item, since it's already been updated by the patron code.
426 # $trans->screen_msg("No such outstanding hold.");
434 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
435 $no_block, $nb_due_date, $third_party, $item_props, $fee_ack) = @_;
437 my $trans = OpenILS::SIP::Transaction::Renew->new( authtoken => $self->{authtoken} );
438 $trans->patron(OpenILS::SIP::Patron->new($patron_id));
439 $trans->item(OpenILS::SIP::Item->new($item_id));
441 if(!$trans->patron) {
442 $trans->screen_msg("Invalid patron barcode.");
447 if(!$trans->patron->renew_ok) {
448 $trans->screen_msg("Renewals not allowed.");
455 $trans->screen_msg("Item Id renewal not supported.");
457 $trans->screen_msg("Invalid item barcode.");
463 if(!$trans->item->{patron} or
464 $trans->item->{patron} ne $patron_id) {
465 $trans->screen_msg("Item not checked out to " . $trans->patron->name);
470 # Perform the renewal
473 $trans->desensitize(0); # It's already checked out
474 $trans->item->{due_date} = $nb_due_date if $no_block eq 'Y';
475 $trans->item->{sip_item_properties} = $item_props if $item_props;
486 # my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
487 # my ($patron, $item_id);
490 # $trans = new ILS::Transaction::RenewAll;
492 # $trans->patron($patron = new ILS::Patron $patron_id);
493 # if (defined $patron) {
494 # syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s",
495 # $patron->name, $patron->renew_ok);
497 # syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'",
501 # if (!defined($patron)) {
502 # $trans->screen_msg("Invalid patron barcode.");
504 # } elsif (!$patron->renew_ok) {
505 # $trans->screen_msg("Renewals not allowed.");
507 # } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
508 # $trans->screen_msg("Invalid patron password.");
512 # foreach $item_id (@{$patron->{items}}) {
513 # my $item = new ILS::Item $item_id;
515 # if (!defined($item)) {
516 # syslog("LOG_WARNING",
517 # "renew_all: Invalid item id associated with patron '%s'",
522 # if (@{$item->hold_queue}) {
523 # # Can't renew if there are outstanding holds
524 # push @{$trans->unrenewed}, $item_id;
526 # $item->{due_date} = time + (14*24*60*60); # two weeks hence
527 # push @{$trans->renewed}, $item_id;