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 .' 00:00:00' if $type eq 'due';
127 #$date = $year.'-'.$mon.'-'.$day if $type eq 'due';
129 syslog('LOG_DEBUG', "OILS: formatted date [type=$type]: $date");
136 my( $self, $username, $password ) = @_;
137 syslog('LOG_DEBUG', "OILS: Logging in with username $username");
139 my $seed = $U->simplereq(
141 'open-ils.auth.authenticate.init', $username );
143 my $response = $U->simplereq(
145 'open-ils.auth.authenticate.complete',
147 username => $username,
148 password => md5_hex($seed . md5_hex($password)),
153 if( my $code = $U->event_code($response) ) {
154 my $txt = $response->{textcode};
155 syslog('LOG_WARNING', "OILS: Login failed for $username. $txt:$code");
159 my $key = $response->{payload}->{authtoken};
160 syslog('LOG_INFO', "OILS: Login succeeded for $username : authkey = $key");
161 return $self->{authtoken} = $key;
167 return OpenILS::SIP::Patron->new(@_);
173 return OpenILS::SIP::Item->new(@_);
179 return $self->{institution}->{id};
183 my ($self, $op) = @_;
184 my ($i) = grep { $_->{name} eq $op }
185 @{$config->{implementation_config}->{supports}->{item}};
186 return to_bool($i->{value});
190 my ($self, $id, $whence) = @_;
191 if ($id ne $self->{institution}->{id}) {
192 syslog("LOG_WARNING",
193 "OILS: %s: received institution '%s', expected '%s'",
194 $whence, $id, $self->{institution}->{id});
199 return to_bool($config->{policy}->{checkout});
203 return to_bool($config->{policy}->{checkin});
208 return to_bool($config->{policy}->{renew});
211 sub status_update_ok {
212 return to_bool($config->{policy}->{status_update});
216 return to_bool($config->{policy}->{offline});
222 ## Checkout(patron_id, item_id, sc_renew):
223 ## patron_id & item_id are the identifiers send by the terminal
224 ## sc_renew is the renewal policy configured on the terminal
225 ## returns a status opject that can be queried for the various bits
226 ## of information that the protocol (SIP or NCIP) needs to generate
231 my ($self, $patron_id, $item_id, $sc_renew) = @_;
233 $self->verify_session;
235 syslog('LOG_DEBUG', "OILS: OpenILS::Checkout attempt: patron=$patron_id, item=$item_id");
237 my $xact = OpenILS::SIP::Transaction::Checkout->new( authtoken => $self->{authtoken} );
238 my $patron = $self->find_patron($patron_id);
239 my $item = $self->find_item($item_id);
241 $xact->patron($patron);
245 $xact->screen_msg("Invalid Patron");
249 if (!$patron->charge_ok) {
250 $xact->screen_msg("Patron Blocked");
255 $xact->screen_msg("Invalid Item");
259 syslog('LOG_DEBUG', "OILS: OpenILS::Checkout data loaded OK, checking out...");
260 $xact->do_checkout();
262 if ($item->{patron} && ($item->{patron} ne $patron_id)) {
263 # I can't deal with this right now
264 # XXX check in then check out?
265 $xact->screen_msg("Item checked out to another patron");
269 $xact->desensitize(!$item->magnetic);
274 syslog("LOG_DEBUG", "OILS: OpenILS::Checkout: " .
275 "patron %s checkout %s succeeded", $patron_id, $item_id);
279 #editor()->xact_rollback;
280 syslog("LOG_DEBUG", "OILS: OpenILS::Checkout: " .
281 "patron %s checkout %s FAILED, rolling back xact...", $patron_id, $item_id);
289 my ($self, $item_id, $trans_date, $return_date,
290 $current_loc, $item_props, $cancel) = @_;
292 $self->verify_session;
294 syslog('LOG_DEBUG', "OILS: OpenILS::Checkin on item=$item_id");
297 my $xact = OpenILS::SIP::Transaction::Checkin->new(authtoken => $self->{authtoken});
298 my $item = $self->find_item($item_id);
303 $xact->screen_msg("Invalid item barcode: $item_id");
308 $xact->do_checkin( $trans_date, $return_date, $current_loc, $item_props );
312 $xact->patron($patron = $self->find_patron($item->{patron}));
313 delete $item->{patron};
314 delete $item->{due_date};
315 syslog('LOG_INFO', "OILS: Checkin succeeded");
320 #editor()->xact_rollback;
321 syslog('LOG_WARNING', "OILS: Checkin failed");
328 ## If the ILS caches patron information, this lets it free it up
329 sub end_patron_session {
330 my ($self, $patron_id) = @_;
331 return (1, 'Thank you for using OpenILS!', '');
336 # my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
337 # $pay_type, $fee_id, $trans_id, $currency) = @_;
341 # $trans = new ILS::Transaction::FeePayment;
343 # $patron = new ILS::Patron $patron_id;
345 # $trans->transaction_id($trans_id);
346 # $trans->patron($patron);
353 # my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
354 # $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
355 # my ($patron, $item);
360 # $trans = new ILS::Transaction::Hold;
362 # # BEGIN TRANSACTION
363 # $patron = new ILS::Patron $patron_id;
365 # || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
366 # $trans->screen_msg("Invalid Patron.");
371 # $item = new ILS::Item ($item_id || $title_id);
373 # $trans->screen_msg("No such item.");
375 # # END TRANSACTION (conditionally)
377 # } elsif ($item->fee && ($fee_ack ne 'Y')) {
378 # $trans->screen_msg = "Fee required to place hold.";
380 # # END TRANSACTION (conditionally)
385 # item_id => $item->id,
386 # patron_id => $patron->id,
387 # expiration_date => $expiry_date,
388 # pickup_location => $pickup_location,
389 # hold_type => $hold_type,
393 # $trans->patron($patron);
394 # $trans->item($item);
395 # $trans->pickup_location($pickup_location);
397 # push(@{$item->hold_queue}, $hold);
398 # push(@{$patron->{hold_items}}, $hold);
406 # my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
407 # my ($patron, $item, $hold);
410 # $trans = new ILS::Transaction::Hold;
412 # # BEGIN TRANSACTION
413 # $patron = new ILS::Patron $patron_id;
415 # $trans->screen_msg("Invalid patron barcode.");
418 # } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
419 # $trans->screen_msg('Invalid patron password.');
424 # $item = new ILS::Item ($item_id || $title_id);
426 # $trans->screen_msg("No such item.");
428 # # END TRANSACTION (conditionally)
432 # # Remove the hold from the patron's record first
433 # $trans->ok($patron->drop_hold($item_id));
436 # # We didn't find it on the patron record
437 # $trans->screen_msg("No such hold on patron record.");
439 # # END TRANSACTION (conditionally)
443 # # Now, remove it from the item record. If it was on the patron
444 # # record but not on the item record, we'll treat that as success.
445 # foreach my $i (0 .. scalar @{$item->hold_queue}) {
446 # $hold = $item->hold_queue->[$i];
448 # if ($hold->{patron_id} eq $patron->id) {
449 # # found it: delete it.
450 # splice @{$item->hold_queue}, $i, 1;
455 # $trans->screen_msg("Hold Cancelled.");
456 # $trans->patron($patron);
457 # $trans->item($item);
463 ## The patron and item id's can't be altered, but the
464 ## date, location, and type can.
466 # my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
467 # $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
468 # my ($patron, $item);
472 # $trans = new ILS::Transaction::Hold;
474 # # BEGIN TRANSACTION
475 # $patron = new ILS::Patron $patron_id;
477 # $trans->screen_msg("Invalid patron barcode.");
482 # foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
483 # $hold = $patron->{hold_items}[$i];
485 # if ($hold->{item_id} eq $item_id) {
486 # # Found it. So fix it.
487 # $hold->{expiration_date} = $expiry_date if $expiry_date;
488 # $hold->{pickup_location} = $pickup_location if $pickup_location;
489 # $hold->{hold_type} = $hold_type if $hold_type;
492 # $trans->screen_msg("Hold updated.");
493 # $trans->patron($patron);
494 # $trans->item(new ILS::Item $hold->{item_id});
499 # # The same hold structure is linked into both the patron's
500 # # list of hold items and into the queue of outstanding holds
501 # # for the item, so we don't need to search the hold queue for
502 # # the item, since it's already been updated by the patron code.
505 # $trans->screen_msg("No such outstanding hold.");
513 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
514 $no_block, $nb_due_date, $third_party, $item_props, $fee_ack) = @_;
516 $self->verify_session;
518 my $trans = OpenILS::SIP::Transaction::Renew->new( authtoken => $self->{authtoken} );
519 $trans->patron($self->find_patron($patron_id));
520 $trans->item($self->find_item($item_id));
522 if(!$trans->patron) {
523 $trans->screen_msg("Invalid patron barcode.");
528 if(!$trans->patron->renew_ok) {
529 $trans->screen_msg("Renewals not allowed.");
536 $trans->screen_msg("Item Id renewal not supported.");
538 $trans->screen_msg("Invalid item barcode.");
544 if(!$trans->item->{patron} or
545 $trans->item->{patron} ne $patron_id) {
546 $trans->screen_msg("Item not checked out to " . $trans->patron->name);
551 # Perform the renewal
554 $trans->desensitize(0); # It's already checked out
555 $trans->item->{due_date} = $nb_due_date if $no_block eq 'Y';
556 $trans->item->{sip_item_properties} = $item_props if $item_props;
567 # my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
568 # my ($patron, $item_id);
571 # $trans = new ILS::Transaction::RenewAll;
573 # $trans->patron($patron = new ILS::Patron $patron_id);
574 # if (defined $patron) {
575 # syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s",
576 # $patron->name, $patron->renew_ok);
578 # syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'",
582 # if (!defined($patron)) {
583 # $trans->screen_msg("Invalid patron barcode.");
585 # } elsif (!$patron->renew_ok) {
586 # $trans->screen_msg("Renewals not allowed.");
588 # } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
589 # $trans->screen_msg("Invalid patron password.");
593 # foreach $item_id (@{$patron->{items}}) {
594 # my $item = new ILS::Item $item_id;
596 # if (!defined($item)) {
597 # syslog("LOG_WARNING",
598 # "renew_all: Invalid item id associated with patron '%s'",
603 # if (@{$item->hold_queue}) {
604 # # Can't renew if there are outstanding holds
605 # push @{$trans->unrenewed}, $item_id;
607 # $item->{due_date} = time + (14*24*60*60); # two weeks hence
608 # push @{$trans->renewed}, $item_id;