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);
78 if $editor and $editor->{session}
79 and $editor->session->connected;
80 return $editor = make_editor();
93 # Creates the global editor object
95 require OpenILS::Utils::CStoreEditor;
96 my $e = OpenILS::Utils::CStoreEditor->new(xact => 1);
97 # gnarly cstore hack to re-gen autogen methods after IDL is loaded
98 if(!UNIVERSAL::can($e, 'search_actor_card')) {
99 syslog("LOG_WARNING", "OILS: Reloading CStoreEditor...");
100 delete $INC{'OpenILS/Utils/CStoreEditor.pm'};
101 require OpenILS::Utils::CStoreEditor;
102 $e = OpenILS::Utils::CStoreEditor->new(xact =>1);
110 my $type = shift || 'dob';
112 return "" unless $date;
114 $date = DateTime::Format::ISO8601->new->
115 parse_datetime(OpenSRF::Utils::clense_ISO8601($date));
116 my @time = localtime($date->epoch);
118 my $year = $time[5]+1900;
119 my $mon = $time[4]+1;
122 $mon =~ s/^(\d)$/0$1/;
123 $day =~ s/^(\d)$/0$1/;
124 $date = "$year$mon$day";
126 $date = $year.'-'.$mon.'-'.$day .' 0:00:00' if $type eq 'due';
128 syslog('LOG_DEBUG', "OILS: formatted date [type=$type]: $date");
135 my( $self, $username, $password ) = @_;
136 syslog('LOG_DEBUG', "OILS: Logging in with username $username");
138 my $seed = $U->simplereq(
140 'open-ils.auth.authenticate.init', $username );
142 my $response = $U->simplereq(
144 'open-ils.auth.authenticate.complete',
146 username => $username,
147 password => md5_hex($seed . md5_hex($password)),
152 if( my $code = $U->event_code($response) ) {
153 my $txt = $response->{textcode};
154 syslog('LOG_WARNING', "OILS: Login failed for $username. $txt:$code");
158 my $key = $response->{payload}->{authtoken};
159 syslog('LOG_INFO', "OILS: Login succeeded for $username : authkey = $key");
160 return $self->{authtoken} = $key;
166 return OpenILS::SIP::Patron->new(@_);
172 return OpenILS::SIP::Item->new(@_);
178 return $self->{institution}->{id};
182 my ($self, $op) = @_;
183 my ($i) = grep { $_->{name} eq $op }
184 @{$config->{implementation_config}->{supports}->{item}};
185 return to_bool($i->{value});
189 my ($self, $id, $whence) = @_;
190 if ($id ne $self->{institution}->{id}) {
191 syslog("LOG_WARNING",
192 "OILS: %s: received institution '%s', expected '%s'",
193 $whence, $id, $self->{institution}->{id});
198 return to_bool($config->{policy}->{checkout});
202 return to_bool($config->{policy}->{checkin});
207 return to_bool($config->{policy}->{renew});
210 sub status_update_ok {
211 return to_bool($config->{policy}->{status_update});
215 return to_bool($config->{policy}->{offline});
221 ## Checkout(patron_id, item_id, sc_renew):
222 ## patron_id & item_id are the identifiers send by the terminal
223 ## sc_renew is the renewal policy configured on the terminal
224 ## returns a status opject that can be queried for the various bits
225 ## of information that the protocol (SIP or NCIP) needs to generate
230 my ($self, $patron_id, $item_id, $sc_renew) = @_;
232 $self->verify_session;
234 syslog('LOG_DEBUG', "OILS: OpenILS::Checkout attempt: patron=$patron_id, item=$item_id");
236 my $xact = OpenILS::SIP::Transaction::Checkout->new( authtoken => $self->{authtoken} );
237 my $patron = $self->find_patron($patron_id);
238 my $item = $self->find_item($item_id);
240 $xact->patron($patron);
244 $xact->screen_msg("Invalid Patron");
248 if (!$patron->charge_ok) {
249 $xact->screen_msg("Patron Blocked");
254 $xact->screen_msg("Invalid Item");
258 syslog('LOG_DEBUG', "OILS: OpenILS::Checkout data loaded OK, checking out...");
259 $xact->do_checkout();
261 if ($item->{patron} && ($item->{patron} ne $patron_id)) {
262 # I can't deal with this right now
263 # XXX check in then check out?
264 $xact->screen_msg("Item checked out to another patron");
268 $xact->desensitize(!$item->magnetic);
273 syslog("LOG_DEBUG", "OILS: OpenILS::Checkout: " .
274 "patron %s checkout %s succeeded", $patron_id, $item_id);
278 #editor()->xact_rollback;
279 syslog("LOG_DEBUG", "OILS: OpenILS::Checkout: " .
280 "patron %s checkout %s FAILED, rolling back xact...", $patron_id, $item_id);
288 my ($self, $item_id, $trans_date, $return_date,
289 $current_loc, $item_props, $cancel) = @_;
291 $self->verify_session;
293 syslog('LOG_DEBUG', "OILS: OpenILS::Checkin on item=$item_id");
296 my $xact = OpenILS::SIP::Transaction::Checkin->new(authtoken => $self->{authtoken});
297 my $item = $self->find_item($item_id);
302 $xact->screen_msg("Invalid item barcode: $item_id");
307 $xact->do_checkin( $trans_date, $return_date, $current_loc, $item_props );
311 $xact->patron($patron = $self->find_patron($item->{patron}));
312 delete $item->{patron};
313 delete $item->{due_date};
314 syslog('LOG_INFO', "OILS: Checkin succeeded");
319 #editor()->xact_rollback;
320 syslog('LOG_WARNING', "OILS: Checkin failed");
327 ## If the ILS caches patron information, this lets it free it up
328 sub end_patron_session {
329 my ($self, $patron_id) = @_;
330 return (1, 'Thank you for using OpenILS!', '');
335 # my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
336 # $pay_type, $fee_id, $trans_id, $currency) = @_;
340 # $trans = new ILS::Transaction::FeePayment;
342 # $patron = new ILS::Patron $patron_id;
344 # $trans->transaction_id($trans_id);
345 # $trans->patron($patron);
352 # my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
353 # $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
354 # my ($patron, $item);
359 # $trans = new ILS::Transaction::Hold;
361 # # BEGIN TRANSACTION
362 # $patron = new ILS::Patron $patron_id;
364 # || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
365 # $trans->screen_msg("Invalid Patron.");
370 # $item = new ILS::Item ($item_id || $title_id);
372 # $trans->screen_msg("No such item.");
374 # # END TRANSACTION (conditionally)
376 # } elsif ($item->fee && ($fee_ack ne 'Y')) {
377 # $trans->screen_msg = "Fee required to place hold.";
379 # # END TRANSACTION (conditionally)
384 # item_id => $item->id,
385 # patron_id => $patron->id,
386 # expiration_date => $expiry_date,
387 # pickup_location => $pickup_location,
388 # hold_type => $hold_type,
392 # $trans->patron($patron);
393 # $trans->item($item);
394 # $trans->pickup_location($pickup_location);
396 # push(@{$item->hold_queue}, $hold);
397 # push(@{$patron->{hold_items}}, $hold);
405 # my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
406 # my ($patron, $item, $hold);
409 # $trans = new ILS::Transaction::Hold;
411 # # BEGIN TRANSACTION
412 # $patron = new ILS::Patron $patron_id;
414 # $trans->screen_msg("Invalid patron barcode.");
417 # } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
418 # $trans->screen_msg('Invalid patron password.');
423 # $item = new ILS::Item ($item_id || $title_id);
425 # $trans->screen_msg("No such item.");
427 # # END TRANSACTION (conditionally)
431 # # Remove the hold from the patron's record first
432 # $trans->ok($patron->drop_hold($item_id));
435 # # We didn't find it on the patron record
436 # $trans->screen_msg("No such hold on patron record.");
438 # # END TRANSACTION (conditionally)
442 # # Now, remove it from the item record. If it was on the patron
443 # # record but not on the item record, we'll treat that as success.
444 # foreach my $i (0 .. scalar @{$item->hold_queue}) {
445 # $hold = $item->hold_queue->[$i];
447 # if ($hold->{patron_id} eq $patron->id) {
448 # # found it: delete it.
449 # splice @{$item->hold_queue}, $i, 1;
454 # $trans->screen_msg("Hold Cancelled.");
455 # $trans->patron($patron);
456 # $trans->item($item);
462 ## The patron and item id's can't be altered, but the
463 ## date, location, and type can.
465 # my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
466 # $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
467 # my ($patron, $item);
471 # $trans = new ILS::Transaction::Hold;
473 # # BEGIN TRANSACTION
474 # $patron = new ILS::Patron $patron_id;
476 # $trans->screen_msg("Invalid patron barcode.");
481 # foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
482 # $hold = $patron->{hold_items}[$i];
484 # if ($hold->{item_id} eq $item_id) {
485 # # Found it. So fix it.
486 # $hold->{expiration_date} = $expiry_date if $expiry_date;
487 # $hold->{pickup_location} = $pickup_location if $pickup_location;
488 # $hold->{hold_type} = $hold_type if $hold_type;
491 # $trans->screen_msg("Hold updated.");
492 # $trans->patron($patron);
493 # $trans->item(new ILS::Item $hold->{item_id});
498 # # The same hold structure is linked into both the patron's
499 # # list of hold items and into the queue of outstanding holds
500 # # for the item, so we don't need to search the hold queue for
501 # # the item, since it's already been updated by the patron code.
504 # $trans->screen_msg("No such outstanding hold.");
512 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
513 $no_block, $nb_due_date, $third_party, $item_props, $fee_ack) = @_;
515 $self->verify_session;
517 my $trans = OpenILS::SIP::Transaction::Renew->new( authtoken => $self->{authtoken} );
518 $trans->patron($self->find_patron($patron_id));
519 $trans->item($self->find_item($item_id));
521 if(!$trans->patron) {
522 $trans->screen_msg("Invalid patron barcode.");
527 if(!$trans->patron->renew_ok) {
528 $trans->screen_msg("Renewals not allowed.");
535 $trans->screen_msg("Item Id renewal not supported.");
537 $trans->screen_msg("Invalid item barcode.");
543 if(!$trans->item->{patron} or
544 $trans->item->{patron} ne $patron_id) {
545 $trans->screen_msg("Item not checked out to " . $trans->patron->name);
550 # Perform the renewal
553 $trans->desensitize(0); # It's already checked out
554 $trans->item->{due_date} = $nb_due_date if $no_block eq 'Y';
555 $trans->item->{sip_item_properties} = $item_props if $item_props;
566 # my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
567 # my ($patron, $item_id);
570 # $trans = new ILS::Transaction::RenewAll;
572 # $trans->patron($patron = new ILS::Patron $patron_id);
573 # if (defined $patron) {
574 # syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s",
575 # $patron->name, $patron->renew_ok);
577 # syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'",
581 # if (!defined($patron)) {
582 # $trans->screen_msg("Invalid patron barcode.");
584 # } elsif (!$patron->renew_ok) {
585 # $trans->screen_msg("Renewals not allowed.");
587 # } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
588 # $trans->screen_msg("Invalid patron password.");
592 # foreach $item_id (@{$patron->{items}}) {
593 # my $item = new ILS::Item $item_id;
595 # if (!defined($item)) {
596 # syslog("LOG_WARNING",
597 # "renew_all: Invalid item id associated with patron '%s'",
602 # if (@{$item->hold_queue}) {
603 # # Can't renew if there are outstanding holds
604 # push @{$trans->unrenewed}, $item_id;
606 # $item->{due_date} = time + (14*24*60*60); # two weeks hence
607 # push @{$trans->renewed}, $item_id;