2 # ILS.pm: Test ILS interface module
6 use warnings; use strict;
8 use Sys::Syslog qw(syslog);
9 use Time::HiRes q/time/;
11 use OpenILS::SIP::Item;
12 use OpenILS::SIP::Patron;
13 use OpenILS::SIP::Transaction;
14 use OpenILS::SIP::Transaction::Checkout;
15 use OpenILS::SIP::Transaction::Checkin;
16 use OpenILS::SIP::Transaction::Renew;
17 use OpenILS::SIP::Transaction::RenewAll;
18 use OpenILS::SIP::Transaction::FeePayment;
19 use OpenILS::SIP::Transaction::Hold;
22 use OpenSRF::AppSession;
23 use OpenILS::Utils::Fieldmapper;
24 use OpenSRF::Utils::SettingsClient;
25 use OpenILS::Application::AppUtils;
26 use OpenILS::Utils::DateTime qw/:datetime/;
27 use DateTime::Format::ISO8601;
29 my $U = 'OpenILS::Application::AppUtils';
34 my $target_encoding; # FIXME: this is configured at the institution level.
36 use Digest::MD5 qw(md5_hex);
38 # Copied from Sip::Constants
40 SIP_DATETIME => "%Y%m%d %H%M%S",
44 OpenSRF::Transport::PeerHandle->retrieve->disconnect
48 my ($class, $institution, $login, $state) = @_;
49 my $type = ref($class) || $class;
52 $self->{login} = $login_account = $login;
54 $config = $institution;
55 syslog("LOG_DEBUG", "OILS: new ILS '%s'", $institution->{id});
56 $self->{institution} = $institution;
58 my $bsconfig = $institution->{implementation_config}->{bootstrap};
59 $target_encoding = $institution->{implementation_config}->{encoding} || 'ascii';
61 syslog('LOG_DEBUG', "OILS: loading bootstrap config: $bsconfig");
63 # ingress will persist throughout
64 OpenSRF::AppSession->ingress('sip2');
66 local $/ = "\n"; # why?
67 OpenSRF::System->bootstrap_client(config_file => $bsconfig);
68 syslog('LOG_DEBUG', "OILS: bootstrap loaded..");
70 $self->{osrf_config} = OpenSRF::Utils::SettingsClient->new;
72 Fieldmapper->import($self->{osrf_config}->config_value('IDL'));
74 bless( $self, $type );
77 $self->login( $login->{id}, $login->{password}, $state );
85 my $ses = $U->simplereq(
87 'open-ils.auth.session.retrieve', $self->{authtoken});
89 return undef if $U->event_code($ses); # auth timed out
90 return $self->{login_session} = $ses;
96 return 1 if $self->fetch_session;
98 syslog('LOG_INFO', "OILS: Logging back after session timeout as user ".$self->{login}->{id});
99 return $self->login( $self->{login}->{id}, $self->{login}->{password} );
103 return $editor = make_editor();
110 return $login_account;
113 sub get_option_value {
114 my($self, $option) = @_;
115 my $ops = $config->{implementation_config}->{options}->{option};
116 $ops = [$ops] unless ref $ops eq 'ARRAY';
117 my @vals = grep { $_->{name} eq $option } @$ops;
118 return @vals ? $vals[0]->{value} : undef;
122 # Creates the global editor object
123 my $cstore_init = 1; # call init on first use
125 OpenILS::Utils::CStoreEditor::init() if $cstore_init;
127 return OpenILS::Utils::CStoreEditor->new;
131 sub shortname_from_id {
132 my $id = shift or return;
133 return $id->shortname if ref $id;
134 return $org_sn_cache{$id} if $org_sn_cache{$id};
135 return $org_sn_cache{$id} = editor()->retrieve_actor_org_unit($id)->shortname;
137 sub patron_barcode_from_id {
138 my $id = shift or return;
139 return editor()->search_actor_card({ usr => $id, active => 't' })->[0]->barcode;
145 my $type = shift || '';
147 return "" unless $date;
149 my $dt = DateTime::Format::ISO8601->new->
150 parse_datetime(clean_ISO8601($date));
152 # actor.usr.dob stores dates without time/timezone, which causes
153 # DateTime to assume the date is stored as UTC. Tell DateTime
154 # to use the local time zone, instead.
155 # Other dates will have time zones and should be parsed as-is.
156 $dt->set_time_zone('local') if $type eq 'dob';
158 my @time = localtime($dt->epoch);
160 my $year = $time[5]+1900;
161 my $mon = $time[4]+1;
164 my $minute = $time[1];
165 my $second = $time[0];
167 $date = sprintf("%04d%02d%02d", $year, $mon, $day);
169 # Due dates need hyphen separators and time of day as well
170 if ($type eq 'due') {
172 my $use_sdf = $class->get_option_value('use_sip_date_format') || '';
174 if ($use_sdf =~ /true/i) {
175 $date = $dt->strftime(SIP_DATETIME);
178 $date = sprintf("%04d-%02d-%02d %02d:%02d:%02d",
179 $year, $mon, $day, $hour, $minute, $second);
183 syslog('LOG_DEBUG', "OILS: formatted date [type=$type]: $date");
190 my( $self, $username, $password, $state ) = @_;
191 syslog('LOG_DEBUG', "OILS: Logging in with username $username");
194 if ($state and ref $state and $$state{authtoken}) {
195 $self->{authtoken} = $$state{authtoken};
196 return $self->{authtoken} if ($self->fetch_session); # fetch the session
199 my $nonce = rand($$);
201 my $seed = $U->simplereq(
203 'open-ils.auth.authenticate.init', $username, $nonce );
207 username => $username,
208 password => md5_hex($seed . md5_hex($password)),
213 if ($self->{login}->{location}) {
214 $opts->{workstation} = $self->{login}->{location};
217 my $response = $U->simplereq(
219 'open-ils.auth.authenticate.complete',
223 if( my $code = $U->event_code($response) ) {
224 my $txt = $response->{textcode};
225 syslog('LOG_WARNING', "OILS: Login failed for $username. $txt:$code");
229 my $key = $response->{payload}->{authtoken};
230 syslog('LOG_INFO', "OILS: Login succeeded for $username : authkey = $key");
232 $self->{authtoken} = $key;
234 $self->fetch_session; # to cache the login
241 return { authtoken => $self->{authtoken} };
245 # find_patron($barcode);
246 # find_patron(barcode => $barcode); # same as above
247 # find_patron(usr => $id);
251 my $key = (@_ > 1) ? shift : 'barcode'; # if we have multiple args, the first is the key index (default barcode)
252 my $patron_id = shift;
254 $self->verify_session;
255 return OpenILS::SIP::Patron->new($key => $patron_id, authtoken => $self->{authtoken}, @_);
261 $self->verify_session;
262 return OpenILS::SIP::Item->new(@_);
268 return $self->{institution}->{id}; # consider making this return the whole institution
273 return $self->{institution}->{id}; # then use this for just the ID
277 my ($self, $op) = @_;
278 my ($i) = grep { $_->{name} eq $op }
279 @{$config->{implementation_config}->{supports}->{item}};
280 return to_bool($i->{value});
284 my ($self, $id, $whence) = @_;
285 if ($id ne $self->{institution}->{id}) {
286 syslog("LOG_WARNING", "OILS: %s: received institution '%s', expected '%s'", $whence, $id, $self->{institution}->{id});
287 # Just an FYI check, we don't expect the user to change location from that in SIPconfig.xml
294 # If it's defined, and matches a true sort of string, or is
295 # a non-zero number, then it's true.
296 defined($bool) or return; # false
297 ($bool =~ /true|y|yes/i) and return 1; # true
298 return ($bool =~ /^\d+$/ and $bool != 0); # true for non-zero numbers, false otherwise
302 return to_bool($config->{policy}->{checkout});
306 return to_bool($config->{policy}->{checkin});
310 return to_bool($config->{policy}->{renewal});
313 sub status_update_ok {
314 return to_bool($config->{policy}->{status_update});
318 return to_bool($config->{policy}->{offline});
324 ## Checkout(patron_id, item_id, sc_renew, fee_ack):
325 ## patron_id & item_id are the identifiers send by the terminal
326 ## sc_renew is the renewal policy configured on the terminal
327 ## returns a status opject that can be queried for the various bits
328 ## of information that the protocol (SIP or NCIP) needs to generate
330 ## fee_ack is the fee_acknowledged field (BO) sent from the sc
331 ## when doing chargeable loans.
335 my ($self, $patron_id, $item_id, $sc_renew, $fee_ack) = @_;
336 # In order to allow renewals the selfcheck AND the config have to say they are allowed
337 $sc_renew = (chr($sc_renew) eq 'Y' && $self->renew_ok());
339 $self->verify_session;
341 syslog('LOG_DEBUG', "OILS: OpenILS::Checkout attempt: patron=$patron_id, item=$item_id");
343 my $xact = OpenILS::SIP::Transaction::Checkout->new( authtoken => $self->{authtoken} );
344 my $patron = $self->find_patron($patron_id);
345 my $item = $self->find_item($item_id);
347 $xact->patron($patron);
351 $xact->screen_msg("Invalid Patron Barcode '$patron_id'");
355 if (!$patron->charge_ok) {
356 $xact->screen_msg("Patron Blocked");
361 $xact->screen_msg("Invalid Item Barcode: '$item_id'");
365 syslog('LOG_DEBUG', "OILS: OpenILS::Checkout data loaded OK, checking out...");
367 if ($item->{patron} && ($item->{patron} eq $patron_id)) {
368 $xact->renew_ok(1); # So that accept/reject responses have the correct value later
370 syslog('LOG_INFO', "OILS: OpenILS::Checkout data loaded OK, doing renew...");
372 syslog('LOG_INFO', "OILS: OpenILS::Checkout appears to be renew, but renewal disallowed...");
373 $xact->screen_msg("Renewals not permitted");
375 return $xact; # Don't attempt later
377 } elsif ($item->{patron} && ($item->{patron} ne $patron_id)) {
378 # I can't deal with this right now
379 # XXX check in then check out?
380 $xact->screen_msg("Item checked out to another patron");
382 return $xact; # Don't wipe out the screen message later
387 # Check for fee and $fee_ack. If there is a fee, and $fee_ack
388 # is 'Y', we proceed, otherwise we reject the checkout.
389 if ($item->fee > 0.0) {
390 $xact->fee_amount($item->fee);
391 $xact->sip_fee_type($item->sip_fee_type);
392 $xact->sip_currency($item->fee_currency);
393 if ($fee_ack && $fee_ack eq 'Y') {
396 $xact->screen_msg('Fee required');
402 $xact->do_checkout($sc_renew);
403 $xact->desensitize(!$item->magnetic);
407 syslog("LOG_DEBUG", "OILS: OpenILS::Checkout: " .
408 "patron %s checkout %s succeeded", $patron_id, $item_id);
410 #editor()->xact_rollback;
411 syslog("LOG_DEBUG", "OILS: OpenILS::Checkout: " .
412 "patron %s checkout %s FAILED, rolling back xact...", $patron_id, $item_id);
420 my ($self, $item_id, $inst_id, $trans_date, $return_date,
421 $current_loc, $item_props, $cancel) = @_;
423 my $start_time = time();
425 $self->verify_session;
427 syslog('LOG_DEBUG', "OILS: OpenILS::Checkin of item=$item_id (to $inst_id)");
429 my $xact = OpenILS::SIP::Transaction::Checkin->new(authtoken => $self->{authtoken});
430 my $item = OpenILS::SIP::Item->new($item_id);
432 unless ( $xact->item($item) ) {
434 # $circ->alert(1); $circ->alert_type(99);
435 $xact->screen_msg("Invalid Item Barcode: '$item_id'");
436 syslog('LOG_INFO', "OILS: Checkin failed. " . $xact->screen_msg() );
440 $xact->do_checkin( $self, $inst_id, $trans_date, $return_date, $current_loc, $item_props );
443 $xact->patron($self->find_patron(usr => $xact->{circ_user_id}, slim_user => 1)) if $xact->{circ_user_id};
444 delete $item->{patron};
445 delete $item->{due_date};
446 syslog('LOG_INFO', "OILS: Checkin succeeded");
448 syslog('LOG_WARNING', "OILS: Checkin failed");
451 syslog('LOG_INFO', "OILS: SIP Checkin request took %0.3f seconds", (time() - $start_time));
455 ## If the ILS caches patron information, this lets it free it up.
456 ## Also, this could be used for centrally logging session duration.
457 ## We don't do anything with it.
458 sub end_patron_session {
459 my ($self, $patron_id) = @_;
460 return (1, 'Thank you!', '');
465 my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
466 $pay_type, $fee_id, $trans_id, $currency) = @_;
468 $self->verify_session;
470 my $xact = OpenILS::SIP::Transaction::FeePayment->new(authtoken => $self->{authtoken});
471 my $patron = $self->find_patron($patron_id);
474 $xact->screen_msg("Invalid Patron Barcode '$patron_id'");
479 $xact->patron($patron);
480 $xact->sip_currency($currency);
481 $xact->fee_amount($fee_amt);
482 $xact->sip_fee_type($fee_type);
483 $xact->transaction_id($trans_id);
484 $xact->fee_id($fee_id);
485 $xact->sip_payment_type($pay_type);
486 # We don't presently use this, but we might in the future.
487 $xact->patron_password($patron_pwd);
489 $xact->do_fee_payment();
495 # my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
496 # $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
497 # my ($patron, $item);
502 # $trans = new ILS::Transaction::Hold;
504 # # BEGIN TRANSACTION
505 # $patron = new ILS::Patron $patron_id;
507 # || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
508 # $trans->screen_msg("Invalid Patron.");
513 # $item = new ILS::Item ($item_id || $title_id);
515 # $trans->screen_msg("No such item.");
517 # # END TRANSACTION (conditionally)
519 # } elsif ($item->fee && ($fee_ack ne 'Y')) {
520 # $trans->screen_msg = "Fee required to place hold.";
522 # # END TRANSACTION (conditionally)
527 # item_id => $item->id,
528 # patron_id => $patron->id,
529 # expiration_date => $expiry_date,
530 # pickup_location => $pickup_location,
531 # hold_type => $hold_type,
535 # $trans->patron($patron);
536 # $trans->item($item);
537 # $trans->pickup_location($pickup_location);
539 # push(@{$item->hold_queue}, $hold);
540 # push(@{$patron->{hold_items}}, $hold);
548 # Note: item_id in this context is the hold id
550 my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
552 my $trans = OpenILS::SIP::Transaction::Hold->new(authtoken => $self->{authtoken});
553 my $patron = $self->find_patron($patron_id);
556 $trans->screen_msg("Invalid patron barcode.");
561 if (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
562 $trans->screen_msg('Invalid patron password.');
567 $trans->patron($patron);
568 my $hold = $patron->find_hold_from_copy($item_id);
571 syslog('LOG_WARNING', "OILS: No hold found from copy $item_id");
572 $trans->screen_msg("No such hold.");
577 if ($hold->usr ne $patron->{user}->id) {
578 $trans->screen_msg("No such hold on patron record.");
584 $trans->do_hold_cancel($self);
586 if ($trans->cancel_ok) {
587 $trans->screen_msg("Hold Cancelled.");
589 $trans->screen_msg("Hold was not cancelled.");
592 # if the hold had no current_copy, use the representative
593 # item as the item for the hold. Without this, the SIP
595 $trans->item($self->find_item($item_id)) unless $trans->item;
601 ## The patron and item id's can't be altered, but the
602 ## date, location, and type can.
604 # my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
605 # $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
606 # my ($patron, $item);
610 # $trans = new ILS::Transaction::Hold;
612 # # BEGIN TRANSACTION
613 # $patron = new ILS::Patron $patron_id;
615 # $trans->screen_msg("Invalid patron barcode.");
620 # foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
621 # $hold = $patron->{hold_items}[$i];
623 # if ($hold->{item_id} eq $item_id) {
624 # # Found it. So fix it.
625 # $hold->{expiration_date} = $expiry_date if $expiry_date;
626 # $hold->{pickup_location} = $pickup_location if $pickup_location;
627 # $hold->{hold_type} = $hold_type if $hold_type;
630 # $trans->screen_msg("Hold updated.");
631 # $trans->patron($patron);
632 # $trans->item(new ILS::Item $hold->{item_id});
637 # # The same hold structure is linked into both the patron's
638 # # list of hold items and into the queue of outstanding holds
639 # # for the item, so we don't need to search the hold queue for
640 # # the item, since it's already been updated by the patron code.
643 # $trans->screen_msg("No such outstanding hold.");
651 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
652 $no_block, $nb_due_date, $third_party, $item_props, $fee_ack) = @_;
654 $self->verify_session;
656 my $trans = OpenILS::SIP::Transaction::Renew->new( authtoken => $self->{authtoken} );
657 $trans->patron($self->find_patron($patron_id));
658 $trans->item($self->find_item($item_id));
660 if(!$trans->patron) {
661 $trans->screen_msg("Invalid patron barcode.");
666 if(!$trans->patron->renew_ok) {
667 $trans->screen_msg("Renewals not allowed.");
674 $trans->screen_msg("Title ID renewal not supported. Use item barcode.");
676 $trans->screen_msg("Invalid item barcode.");
682 if(!$trans->item->{patron} or
683 $trans->item->{patron} ne $patron_id) {
684 $trans->screen_msg("Item not checked out to " . $trans->patron->name);
689 # Perform the renewal
692 $trans->desensitize(0); # It's already checked out
693 $trans->item->{due_date} = $nb_due_date if $no_block eq 'Y';
694 $trans->item->{sip_item_properties} = $item_props if $item_props;
701 my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
703 $self->verify_session;
705 my $trans = OpenILS::SIP::Transaction::RenewAll->new(authtoken => $self->{authtoken});
706 $trans->patron($self->find_patron($patron_id));
708 if(!$trans->patron) {
709 $trans->screen_msg("Invalid patron barcode.");
714 if(!$trans->patron->renew_ok) {
715 $trans->screen_msg("Renewals not allowed.");
720 $trans->do_renew_all($self);
727 # my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
728 # my ($patron, $item_id);
731 # $trans = new ILS::Transaction::RenewAll;
733 # $trans->patron($patron = new ILS::Patron $patron_id);
734 # if (defined $patron) {
735 # syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s",
736 # $patron->name, $patron->renew_ok);
738 # syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'",
742 # if (!defined($patron)) {
743 # $trans->screen_msg("Invalid patron barcode.");
745 # } elsif (!$patron->renew_ok) {
746 # $trans->screen_msg("Renewals not allowed.");
748 # } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
749 # $trans->screen_msg("Invalid patron password.");
753 # foreach $item_id (@{$patron->{items}}) {
754 # my $item = new ILS::Item $item_id;
756 # if (!defined($item)) {
757 # syslog("LOG_WARNING",
758 # "renew_all: Invalid item id associated with patron '%s'",
763 # if (@{$item->hold_queue}) {
764 # # Can't renew if there are outstanding holds
765 # push @{$trans->unrenewed}, $item_id;
767 # $item->{due_date} = time + (14*24*60*60); # two weeks hence
768 # push @{$trans->renewed}, $item_id;