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::Renew;
18 use OpenILS::Utils::Fieldmapper;
19 use OpenSRF::Utils::SettingsClient;
20 use OpenILS::Application::AppUtils;
21 use OpenSRF::Utils qw/:datetime/;
22 use DateTime::Format::ISO8601;
23 my $U = 'OpenILS::Application::AppUtils';
28 use Digest::MD5 qw(md5_hex);
31 my ($class, $institution, $login) = @_;
32 my $type = ref($class) || $class;
35 $self->{login} = $login;
37 $config = $institution;
38 syslog("LOG_DEBUG", "OILS: new ILS '%s'", $institution->{id});
39 $self->{institution} = $institution;
41 my $bsconfig = $institution->{implementation_config}->{bootstrap};
43 syslog('LOG_DEBUG', "OILS: loading bootstrap config: $bsconfig");
46 OpenSRF::System->bootstrap_client(config_file => $bsconfig);
47 syslog('LOG_DEBUG', "OILS: bootstrap loaded..");
49 $self->{osrf_config} = OpenSRF::Utils::SettingsClient->new;
51 Fieldmapper->import($self->{osrf_config}->config_value('IDL'));
53 bless( $self, $type );
56 $self->login( $login->{id}, $login->{password} );
63 my $ses = $U->simplereq(
65 'open-ils.auth.session.retrieve', $self->{authtoken} );
66 return 1 unless $U->event_code($ses);
67 syslog('LOG_INFO', "OILS: Logging back after session timeout as user ".$self->{login}->{id});
68 return $self->login( $self->{login}->{id}, $self->{login}->{password} );
73 return ($val and $val =~ /true/io);
77 $editor = make_editor() unless $editor;
90 # Creates the global editor object
92 require OpenILS::Utils::CStoreEditor;
93 my $e = OpenILS::Utils::CStoreEditor->new(xact => 1);
94 # gnarly cstore hack to re-gen autogen methods after IDL is loaded
95 if(!UNIVERSAL::can($e, 'search_actor_card')) {
96 syslog("LOG_WARNING", "OILS: Reloading CStoreEditor...");
97 delete $INC{'OpenILS/Utils/CStoreEditor.pm'};
98 require OpenILS::Utils::CStoreEditor;
99 $e = OpenILS::Utils::CStoreEditor->new(xact =>1);
107 return "" unless $date;
109 $date = DateTime::Format::ISO8601->new->
110 parse_datetime(OpenSRF::Utils::clense_ISO8601($date));
111 my @time = localtime($date->epoch);
113 my $year = $time[5]+1900;
114 my $mon = $time[4]+1;
117 $mon =~ s/^(\d)$/0$1/;
118 $day =~ s/^(\d)$/0$1/;
119 $date = "$year$mon$day";
120 syslog('LOG_DEBUG', "OILS: formatted date: $date");
127 my( $self, $username, $password ) = @_;
128 syslog('LOG_DEBUG', "OILS: Logging in with username $username");
130 my $seed = $U->simplereq(
132 'open-ils.auth.authenticate.init', $username );
134 my $response = $U->simplereq(
136 'open-ils.auth.authenticate.complete',
138 username => $username,
139 password => md5_hex($seed . md5_hex($password)),
144 if( my $code = $U->event_code($response) ) {
145 my $txt = $response->{textcode};
146 syslog('LOG_WARNING', "OILS: Login failed for $username. $txt:$code");
150 my $key = $response->{payload}->{authtoken};
151 syslog('LOG_INFO', "OILS: Login succeeded for $username : authkey = $key");
152 return $self->{authtoken} = $key;
158 return OpenILS::SIP::Patron->new(@_);
164 return OpenILS::SIP::Item->new(@_);
170 return $self->{institution}->{id};
174 my ($self, $op) = @_;
175 my ($i) = grep { $_->{name} eq $op }
176 @{$config->{implementation_config}->{supports}->{item}};
177 return to_bool($i->{value});
181 my ($self, $id, $whence) = @_;
182 if ($id ne $self->{institution}->{id}) {
183 syslog("LOG_WARNING",
184 "OILS: %s: received institution '%s', expected '%s'",
185 $whence, $id, $self->{institution}->{id});
190 return to_bool($config->{policy}->{checkout});
194 return to_bool($config->{policy}->{checkin});
199 return to_bool($config->{policy}->{renew});
202 sub status_update_ok {
203 return to_bool($config->{policy}->{status_update});
207 return to_bool($config->{policy}->{offline});
213 ## Checkout(patron_id, item_id, sc_renew):
214 ## patron_id & item_id are the identifiers send by the terminal
215 ## sc_renew is the renewal policy configured on the terminal
216 ## returns a status opject that can be queried for the various bits
217 ## of information that the protocol (SIP or NCIP) needs to generate
222 my ($self, $patron_id, $item_id, $sc_renew) = @_;
224 $self->verify_session;
226 syslog('LOG_DEBUG', "OILS: OpenILS::Checkout attempt: patron=$patron_id, item=$item_id");
228 my $xact = OpenILS::SIP::Transaction::Checkout->new( authtoken => $self->{authtoken} );
229 my $patron = $self->find_patron($patron_id);
230 my $item = $self->find_item($item_id);
232 $xact->patron($patron);
236 $xact->screen_msg("Invalid Patron");
240 if (!$patron->charge_ok) {
241 $xact->screen_msg("Patron Blocked");
246 $xact->screen_msg("Invalid Item");
250 syslog('LOG_DEBUG', "OILS: OpenILS::Checkout data loaded OK, checking out...");
251 $xact->do_checkout();
253 if ($item->{patron} && ($item->{patron} ne $patron_id)) {
254 # I can't deal with this right now
255 # XXX check in then check out?
256 $xact->screen_msg("Item checked out to another patron");
260 $xact->desensitize(!$item->magnetic);
265 syslog("LOG_DEBUG", "OILS: OpenILS::Checkout: " .
266 "patron %s checkout %s succeeded", $patron_id, $item_id);
270 #editor()->xact_rollback;
271 syslog("LOG_DEBUG", "OILS: OpenILS::Checkout: " .
272 "patron %s checkout %s FAILED, rolling back xact...", $patron_id, $item_id);
280 my ($self, $item_id, $trans_date, $return_date,
281 $current_loc, $item_props, $cancel) = @_;
283 $self->verify_session;
285 syslog('LOG_DEBUG', "OILS: OpenILS::Checkin on item=$item_id");
288 my $xact = OpenILS::SIP::Transaction::Checkin->new(authtoken => $self->{authtoken});
289 my $item = $self->find_item($item_id);
294 $xact->screen_msg("Invalid item barcode: $item_id");
299 $xact->do_checkin( $trans_date, $return_date, $current_loc, $item_props );
303 $xact->patron($patron = $self->find_patron($item->{patron}));
304 delete $item->{patron};
305 delete $item->{due_date};
306 syslog('LOG_INFO', "OILS: Checkin succeeded");
311 #editor()->xact_rollback;
312 syslog('LOG_WARNING', "OILS: Checkin failed");
319 ## If the ILS caches patron information, this lets it free it up
320 sub end_patron_session {
321 my ($self, $patron_id) = @_;
322 return (1, 'Thank you for using OpenILS!', '');
327 # my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
328 # $pay_type, $fee_id, $trans_id, $currency) = @_;
332 # $trans = new ILS::Transaction::FeePayment;
334 # $patron = new ILS::Patron $patron_id;
336 # $trans->transaction_id($trans_id);
337 # $trans->patron($patron);
344 # my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
345 # $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
346 # my ($patron, $item);
351 # $trans = new ILS::Transaction::Hold;
353 # # BEGIN TRANSACTION
354 # $patron = new ILS::Patron $patron_id;
356 # || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
357 # $trans->screen_msg("Invalid Patron.");
362 # $item = new ILS::Item ($item_id || $title_id);
364 # $trans->screen_msg("No such item.");
366 # # END TRANSACTION (conditionally)
368 # } elsif ($item->fee && ($fee_ack ne 'Y')) {
369 # $trans->screen_msg = "Fee required to place hold.";
371 # # END TRANSACTION (conditionally)
376 # item_id => $item->id,
377 # patron_id => $patron->id,
378 # expiration_date => $expiry_date,
379 # pickup_location => $pickup_location,
380 # hold_type => $hold_type,
384 # $trans->patron($patron);
385 # $trans->item($item);
386 # $trans->pickup_location($pickup_location);
388 # push(@{$item->hold_queue}, $hold);
389 # push(@{$patron->{hold_items}}, $hold);
397 # my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
398 # my ($patron, $item, $hold);
401 # $trans = new ILS::Transaction::Hold;
403 # # BEGIN TRANSACTION
404 # $patron = new ILS::Patron $patron_id;
406 # $trans->screen_msg("Invalid patron barcode.");
409 # } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
410 # $trans->screen_msg('Invalid patron password.');
415 # $item = new ILS::Item ($item_id || $title_id);
417 # $trans->screen_msg("No such item.");
419 # # END TRANSACTION (conditionally)
423 # # Remove the hold from the patron's record first
424 # $trans->ok($patron->drop_hold($item_id));
427 # # We didn't find it on the patron record
428 # $trans->screen_msg("No such hold on patron record.");
430 # # END TRANSACTION (conditionally)
434 # # Now, remove it from the item record. If it was on the patron
435 # # record but not on the item record, we'll treat that as success.
436 # foreach my $i (0 .. scalar @{$item->hold_queue}) {
437 # $hold = $item->hold_queue->[$i];
439 # if ($hold->{patron_id} eq $patron->id) {
440 # # found it: delete it.
441 # splice @{$item->hold_queue}, $i, 1;
446 # $trans->screen_msg("Hold Cancelled.");
447 # $trans->patron($patron);
448 # $trans->item($item);
454 ## The patron and item id's can't be altered, but the
455 ## date, location, and type can.
457 # my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
458 # $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
459 # my ($patron, $item);
463 # $trans = new ILS::Transaction::Hold;
465 # # BEGIN TRANSACTION
466 # $patron = new ILS::Patron $patron_id;
468 # $trans->screen_msg("Invalid patron barcode.");
473 # foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
474 # $hold = $patron->{hold_items}[$i];
476 # if ($hold->{item_id} eq $item_id) {
477 # # Found it. So fix it.
478 # $hold->{expiration_date} = $expiry_date if $expiry_date;
479 # $hold->{pickup_location} = $pickup_location if $pickup_location;
480 # $hold->{hold_type} = $hold_type if $hold_type;
483 # $trans->screen_msg("Hold updated.");
484 # $trans->patron($patron);
485 # $trans->item(new ILS::Item $hold->{item_id});
490 # # The same hold structure is linked into both the patron's
491 # # list of hold items and into the queue of outstanding holds
492 # # for the item, so we don't need to search the hold queue for
493 # # the item, since it's already been updated by the patron code.
496 # $trans->screen_msg("No such outstanding hold.");
504 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
505 $no_block, $nb_due_date, $third_party, $item_props, $fee_ack) = @_;
507 $self->verify_session;
509 my $trans = OpenILS::SIP::Transaction::Renew->new( authtoken => $self->{authtoken} );
510 $trans->patron($self->find_patron($patron_id));
511 $trans->item($self->find_item($item_id));
513 if(!$trans->patron) {
514 $trans->screen_msg("Invalid patron barcode.");
519 if(!$trans->patron->renew_ok) {
520 $trans->screen_msg("Renewals not allowed.");
527 $trans->screen_msg("Item Id renewal not supported.");
529 $trans->screen_msg("Invalid item barcode.");
535 if(!$trans->item->{patron} or
536 $trans->item->{patron} ne $patron_id) {
537 $trans->screen_msg("Item not checked out to " . $trans->patron->name);
542 # Perform the renewal
545 $trans->desensitize(0); # It's already checked out
546 $trans->item->{due_date} = $nb_due_date if $no_block eq 'Y';
547 $trans->item->{sip_item_properties} = $item_props if $item_props;
558 # my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
559 # my ($patron, $item_id);
562 # $trans = new ILS::Transaction::RenewAll;
564 # $trans->patron($patron = new ILS::Patron $patron_id);
565 # if (defined $patron) {
566 # syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s",
567 # $patron->name, $patron->renew_ok);
569 # syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'",
573 # if (!defined($patron)) {
574 # $trans->screen_msg("Invalid patron barcode.");
576 # } elsif (!$patron->renew_ok) {
577 # $trans->screen_msg("Renewals not allowed.");
579 # } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
580 # $trans->screen_msg("Invalid patron password.");
584 # foreach $item_id (@{$patron->{items}}) {
585 # my $item = new ILS::Item $item_id;
587 # if (!defined($item)) {
588 # syslog("LOG_WARNING",
589 # "renew_all: Invalid item id associated with patron '%s'",
594 # if (@{$item->hold_queue}) {
595 # # Can't renew if there are outstanding holds
596 # push @{$trans->unrenewed}, $item_id;
598 # $item->{due_date} = time + (14*24*60*60); # two weeks hence
599 # push @{$trans->renewed}, $item_id;