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::FeePayment;
20 use OpenSRF::AppSession;
21 use OpenILS::Utils::Fieldmapper;
22 use OpenSRF::Utils::SettingsClient;
23 use OpenILS::Application::AppUtils;
24 use OpenSRF::Utils qw/:datetime/;
25 use DateTime::Format::ISO8601;
27 use Unicode::Normalize;
28 my $U = 'OpenILS::Application::AppUtils';
33 my $target_encoding; # FIXME: this is configured at the institution level.
35 use Digest::MD5 qw(md5_hex);
38 my ($class, $institution, $login) = @_;
39 my $type = ref($class) || $class;
42 $self->{login} = $login_account = $login;
44 $config = $institution;
45 syslog("LOG_DEBUG", "OILS: new ILS '%s'", $institution->{id});
46 $self->{institution} = $institution;
48 my $bsconfig = $institution->{implementation_config}->{bootstrap};
49 $target_encoding = $institution->{implementation_config}->{encoding} || 'ascii';
51 syslog('LOG_DEBUG', "OILS: loading bootstrap config: $bsconfig");
53 # ingress will persist throughout
54 OpenSRF::AppSession->ingress('sip2');
56 local $/ = "\n"; # why?
57 OpenSRF::System->bootstrap_client(config_file => $bsconfig);
58 syslog('LOG_DEBUG', "OILS: bootstrap loaded..");
60 $self->{osrf_config} = OpenSRF::Utils::SettingsClient->new;
62 Fieldmapper->import($self->{osrf_config}->config_value('IDL'));
64 bless( $self, $type );
67 $self->login( $login->{id}, $login->{password} );
75 my $ses = $U->simplereq(
77 'open-ils.auth.session.retrieve', $self->{authtoken});
79 return undef if $U->event_code($ses); # auth timed out
80 return $self->{login_session} = $ses;
86 return 1 if $self->fetch_session;
88 syslog('LOG_INFO', "OILS: Logging back after session timeout as user ".$self->{login}->{id});
89 return $self->login( $self->{login}->{id}, $self->{login}->{password} );
93 return $editor = make_editor();
100 return $login_account;
103 sub get_option_value {
104 my($self, $option) = @_;
105 my $ops = $config->{implementation_config}->{options}->{option};
106 $ops = [$ops] unless ref $ops eq 'ARRAY';
107 my @vals = grep { $_->{name} eq $option } @$ops;
108 return @vals ? $vals[0]->{value} : undef;
112 # Creates the global editor object
113 my $cstore_init = 1; # call init on first use
115 OpenILS::Utils::CStoreEditor::init() if $cstore_init;
117 return OpenILS::Utils::CStoreEditor->new;
120 =head2 clean_text(scalar)
122 Evergreen uses the UTF8 encoding for everything from the database up. Perl
123 doesn't know this, however, so we have to convince it to treat our UTF8 strings
124 as UTF8 strings. This may enable OpenNCIP to correctly calculate the checksums
125 for UTF8 text for SIP clients that support such modern options.
127 The target encoding is set in the <encoding> element of the SIPServer.pm
133 my $text = shift || '';
135 # Convert our incoming UTF8 data into Perl's internal string format
137 # Also convert to Normalization Form D, as the ASCII, iso-8859-1,
138 # and latin-1 encodings (at least) require this to substitute
139 # characters rather than simply returning a string truncated
140 # after the first non-ASCII character
141 $text = NFD(decode_utf8($text));
143 if ($target_encoding eq 'ascii') {
145 # Try to maintain a reasonable version of the content by
146 # stripping diacritics from the text, given that the SIP client
147 # wants just plain ASCII. This is the base requirement according
148 # to the SIP2 specification.
150 # Stripping the combining characters converts ""béè♁ts"
151 # into "bee?ts" instead of "b???ts" - better, eh?
155 # Characters that cannot be represented in the target encoding will
156 # generally be replaced with a question mark (?) character.
157 $text = encode($target_encoding, $text);
163 sub shortname_from_id {
164 my $id = shift or return;
165 return $id->shortname if ref $id;
166 return $org_sn_cache{$id} if $org_sn_cache{$id};
167 return $org_sn_cache{$id} = editor()->retrieve_actor_org_unit($id)->shortname;
169 sub patron_barcode_from_id {
170 my $id = shift or return;
171 return editor()->search_actor_card({ usr => $id, active => 't' })->[0]->barcode;
177 my $type = shift || 'dob';
179 return "" unless $date;
181 $date = DateTime::Format::ISO8601->new->
182 parse_datetime(OpenSRF::Utils::cleanse_ISO8601($date));
183 my @time = localtime($date->epoch);
185 my $year = $time[5]+1900;
186 my $mon = $time[4]+1;
189 my $minute = $time[1];
190 my $second = $time[0];
192 $date = sprintf("%04d%02d%02d", $year, $mon, $day);
194 # Due dates need hyphen separators and time of day as well
195 if ($type eq 'due') {
196 $date = sprintf("%04d-%02d-%02d %02d:%02d:%02d", $year, $mon, $day, $hour, $minute, $second);
199 syslog('LOG_DEBUG', "OILS: formatted date [type=$type]: $date");
206 my( $self, $username, $password ) = @_;
207 syslog('LOG_DEBUG', "OILS: Logging in with username $username");
209 my $seed = $U->simplereq(
211 'open-ils.auth.authenticate.init', $username );
213 my $response = $U->simplereq(
215 'open-ils.auth.authenticate.complete',
217 username => $username,
218 password => md5_hex($seed . md5_hex($password)),
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->fetch_session; # to cache the login
234 return $self->{authtoken} = $key;
240 return OpenILS::SIP::Patron->new(@_);
246 return OpenILS::SIP::Item->new(@_);
252 return $self->{institution}->{id}; # consider making this return the whole institution
257 return $self->{institution}->{id}; # then use this for just the ID
261 my ($self, $op) = @_;
262 my ($i) = grep { $_->{name} eq $op }
263 @{$config->{implementation_config}->{supports}->{item}};
264 return to_bool($i->{value});
268 my ($self, $id, $whence) = @_;
269 if ($id ne $self->{institution}->{id}) {
270 syslog("LOG_WARNING", "OILS: %s: received institution '%s', expected '%s'", $whence, $id, $self->{institution}->{id});
271 # Just an FYI check, we don't expect the user to change location from that in SIPconfig.xml
278 # If it's defined, and matches a true sort of string, or is
279 # a non-zero number, then it's true.
280 defined($bool) or return; # false
281 ($bool =~ /true|y|yes/i) and return 1; # true
282 return ($bool =~ /^\d+$/ and $bool != 0); # true for non-zero numbers, false otherwise
286 return to_bool($config->{policy}->{checkout});
290 return to_bool($config->{policy}->{checkin});
294 return to_bool($config->{policy}->{renewal});
297 sub status_update_ok {
298 return to_bool($config->{policy}->{status_update});
302 return to_bool($config->{policy}->{offline});
308 ## Checkout(patron_id, item_id, sc_renew, fee_ack):
309 ## patron_id & item_id are the identifiers send by the terminal
310 ## sc_renew is the renewal policy configured on the terminal
311 ## returns a status opject that can be queried for the various bits
312 ## of information that the protocol (SIP or NCIP) needs to generate
314 ## fee_ack is the fee_acknowledged field (BO) sent from the sc
315 ## when doing chargeable loans.
319 my ($self, $patron_id, $item_id, $sc_renew, $fee_ack) = @_;
320 # In order to allow renewals the selfcheck AND the config have to say they are allowed
321 $sc_renew = (chr($sc_renew) eq 'Y' && $self->renew_ok());
323 $self->verify_session;
325 syslog('LOG_DEBUG', "OILS: OpenILS::Checkout attempt: patron=$patron_id, item=$item_id");
327 my $xact = OpenILS::SIP::Transaction::Checkout->new( authtoken => $self->{authtoken} );
328 my $patron = $self->find_patron($patron_id);
329 my $item = $self->find_item($item_id);
331 $xact->patron($patron);
335 $xact->screen_msg("Invalid Patron Barcode '$patron_id'");
339 if (!$patron->charge_ok) {
340 $xact->screen_msg("Patron Blocked");
345 $xact->screen_msg("Invalid Item Barcode: '$item_id'");
349 syslog('LOG_DEBUG', "OILS: OpenILS::Checkout data loaded OK, checking out...");
351 if ($item->{patron} && ($item->{patron} eq $patron_id)) {
352 $xact->renew_ok(1); # So that accept/reject responses have the correct value later
354 syslog('LOG_INFO', "OILS: OpenILS::Checkout data loaded OK, doing renew...");
356 syslog('LOG_INFO', "OILS: OpenILS::Checkout appears to be renew, but renewal disallowed...");
357 $xact->screen_msg("Renewals not permitted");
359 return $xact; # Don't attempt later
361 } elsif ($item->{patron} && ($item->{patron} ne $patron_id)) {
362 # I can't deal with this right now
363 # XXX check in then check out?
364 $xact->screen_msg("Item checked out to another patron");
366 return $xact; # Don't wipe out the screen message later
371 # Check for fee and $fee_ack. If there is a fee, and $fee_ack
372 # is 'Y', we proceed, otherwise we reject the checkout.
373 if ($item->fee > 0.0) {
374 $xact->fee_amount($item->fee);
375 $xact->sip_fee_type($item->sip_fee_type);
376 $xact->sip_currency($item->fee_currency);
377 if ($fee_ack && $fee_ack eq 'Y') {
380 $xact->screen_msg('Fee required');
386 $xact->do_checkout($sc_renew);
387 $xact->desensitize(!$item->magnetic);
391 syslog("LOG_DEBUG", "OILS: OpenILS::Checkout: " .
392 "patron %s checkout %s succeeded", $patron_id, $item_id);
394 #editor()->xact_rollback;
395 syslog("LOG_DEBUG", "OILS: OpenILS::Checkout: " .
396 "patron %s checkout %s FAILED, rolling back xact...", $patron_id, $item_id);
404 my ($self, $item_id, $inst_id, $trans_date, $return_date,
405 $current_loc, $item_props, $cancel) = @_;
407 my $start_time = time();
409 $self->verify_session;
411 syslog('LOG_DEBUG', "OILS: OpenILS::Checkin of item=$item_id (to $inst_id)");
413 my $xact = OpenILS::SIP::Transaction::Checkin->new(authtoken => $self->{authtoken});
414 my $item = OpenILS::SIP::Item->new($item_id);
416 unless ( $xact->item($item) ) {
418 # $circ->alert(1); $circ->alert_type(99);
419 $xact->screen_msg("Invalid Item Barcode: '$item_id'");
420 syslog('LOG_INFO', "OILS: Checkin failed. " . $xact->screen_msg() );
424 $xact->do_checkin( $self, $inst_id, $trans_date, $return_date, $current_loc, $item_props );
427 $xact->patron($self->find_patron(usr => $xact->{circ_user_id}, slim_user => 1)) if $xact->{circ_user_id};
428 delete $item->{patron};
429 delete $item->{due_date};
430 syslog('LOG_INFO', "OILS: Checkin succeeded");
432 syslog('LOG_WARNING', "OILS: Checkin failed");
435 syslog('LOG_INFO', "OILS: SIP Checkin request took %0.3f seconds", (time() - $start_time));
439 ## If the ILS caches patron information, this lets it free it up.
440 ## Also, this could be used for centrally logging session duration.
441 ## We don't do anything with it.
442 sub end_patron_session {
443 my ($self, $patron_id) = @_;
444 return (1, 'Thank you!', '');
449 my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
450 $pay_type, $fee_id, $trans_id, $currency) = @_;
452 $self->verify_session;
454 my $xact = OpenILS::SIP::Transaction::FeePayment->new(authtoken => $self->{authtoken});
455 my $patron = $self->find_patron($patron_id);
458 $xact->screen_msg("Invalid Patron Barcode '$patron_id'");
463 $xact->patron($patron);
464 $xact->sip_currency($currency);
465 $xact->fee_amount($fee_amt);
466 $xact->sip_fee_type($fee_type);
467 $xact->transaction_id($trans_id);
468 $xact->fee_id($fee_id);
469 # We don't presently use these, but we might in the future.
470 $xact->patron_password($patron_pwd);
471 $xact->sip_payment_type($pay_type);
473 $xact->do_fee_payment();
479 # my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
480 # $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
481 # my ($patron, $item);
486 # $trans = new ILS::Transaction::Hold;
488 # # BEGIN TRANSACTION
489 # $patron = new ILS::Patron $patron_id;
491 # || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
492 # $trans->screen_msg("Invalid Patron.");
497 # $item = new ILS::Item ($item_id || $title_id);
499 # $trans->screen_msg("No such item.");
501 # # END TRANSACTION (conditionally)
503 # } elsif ($item->fee && ($fee_ack ne 'Y')) {
504 # $trans->screen_msg = "Fee required to place hold.";
506 # # END TRANSACTION (conditionally)
511 # item_id => $item->id,
512 # patron_id => $patron->id,
513 # expiration_date => $expiry_date,
514 # pickup_location => $pickup_location,
515 # hold_type => $hold_type,
519 # $trans->patron($patron);
520 # $trans->item($item);
521 # $trans->pickup_location($pickup_location);
523 # push(@{$item->hold_queue}, $hold);
524 # push(@{$patron->{hold_items}}, $hold);
532 # my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
533 # my ($patron, $item, $hold);
536 # $trans = new ILS::Transaction::Hold;
538 # # BEGIN TRANSACTION
539 # $patron = new ILS::Patron $patron_id;
541 # $trans->screen_msg("Invalid patron barcode.");
544 # } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
545 # $trans->screen_msg('Invalid patron password.');
550 # $item = new ILS::Item ($item_id || $title_id);
552 # $trans->screen_msg("No such item.");
554 # # END TRANSACTION (conditionally)
558 # # Remove the hold from the patron's record first
559 # $trans->ok($patron->drop_hold($item_id));
562 # # We didn't find it on the patron record
563 # $trans->screen_msg("No such hold on patron record.");
565 # # END TRANSACTION (conditionally)
569 # # Now, remove it from the item record. If it was on the patron
570 # # record but not on the item record, we'll treat that as success.
571 # foreach my $i (0 .. scalar @{$item->hold_queue}) {
572 # $hold = $item->hold_queue->[$i];
574 # if ($hold->{patron_id} eq $patron->id) {
575 # # found it: delete it.
576 # splice @{$item->hold_queue}, $i, 1;
581 # $trans->screen_msg("Hold Cancelled.");
582 # $trans->patron($patron);
583 # $trans->item($item);
589 ## The patron and item id's can't be altered, but the
590 ## date, location, and type can.
592 # my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
593 # $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
594 # my ($patron, $item);
598 # $trans = new ILS::Transaction::Hold;
600 # # BEGIN TRANSACTION
601 # $patron = new ILS::Patron $patron_id;
603 # $trans->screen_msg("Invalid patron barcode.");
608 # foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
609 # $hold = $patron->{hold_items}[$i];
611 # if ($hold->{item_id} eq $item_id) {
612 # # Found it. So fix it.
613 # $hold->{expiration_date} = $expiry_date if $expiry_date;
614 # $hold->{pickup_location} = $pickup_location if $pickup_location;
615 # $hold->{hold_type} = $hold_type if $hold_type;
618 # $trans->screen_msg("Hold updated.");
619 # $trans->patron($patron);
620 # $trans->item(new ILS::Item $hold->{item_id});
625 # # The same hold structure is linked into both the patron's
626 # # list of hold items and into the queue of outstanding holds
627 # # for the item, so we don't need to search the hold queue for
628 # # the item, since it's already been updated by the patron code.
631 # $trans->screen_msg("No such outstanding hold.");
639 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
640 $no_block, $nb_due_date, $third_party, $item_props, $fee_ack) = @_;
642 $self->verify_session;
644 my $trans = OpenILS::SIP::Transaction::Renew->new( authtoken => $self->{authtoken} );
645 $trans->patron($self->find_patron($patron_id));
646 $trans->item($self->find_item($item_id));
648 if(!$trans->patron) {
649 $trans->screen_msg("Invalid patron barcode.");
654 if(!$trans->patron->renew_ok) {
655 $trans->screen_msg("Renewals not allowed.");
662 $trans->screen_msg("Title ID renewal not supported. Use item barcode.");
664 $trans->screen_msg("Invalid item barcode.");
670 if(!$trans->item->{patron} or
671 $trans->item->{patron} ne $patron_id) {
672 $trans->screen_msg("Item not checked out to " . $trans->patron->name);
677 # Perform the renewal
680 $trans->desensitize(0); # It's already checked out
681 $trans->item->{due_date} = $nb_due_date if $no_block eq 'Y';
682 $trans->item->{sip_item_properties} = $item_props if $item_props;
693 # my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
694 # my ($patron, $item_id);
697 # $trans = new ILS::Transaction::RenewAll;
699 # $trans->patron($patron = new ILS::Patron $patron_id);
700 # if (defined $patron) {
701 # syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s",
702 # $patron->name, $patron->renew_ok);
704 # syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'",
708 # if (!defined($patron)) {
709 # $trans->screen_msg("Invalid patron barcode.");
711 # } elsif (!$patron->renew_ok) {
712 # $trans->screen_msg("Renewals not allowed.");
714 # } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
715 # $trans->screen_msg("Invalid patron password.");
719 # foreach $item_id (@{$patron->{items}}) {
720 # my $item = new ILS::Item $item_id;
722 # if (!defined($item)) {
723 # syslog("LOG_WARNING",
724 # "renew_all: Invalid item id associated with patron '%s'",
729 # if (@{$item->hold_queue}) {
730 # # Can't renew if there are outstanding holds
731 # push @{$trans->unrenewed}, $item_id;
733 # $item->{due_date} = time + (14*24*60*60); # two weeks hence
734 # push @{$trans->renewed}, $item_id;