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 my $U = 'OpenILS::Application::AppUtils';
26 use Digest::MD5 qw(md5_hex);
29 my ($class, $institution, $login) = @_;
30 my $type = ref($class) || $class;
33 $config = $institution;
34 syslog("LOG_DEBUG", "OILS: new ILS '%s'", $institution->{id});
35 $self->{institution} = $institution;
37 my $bsconfig = $institution->{implementation_config}->{bootstrap};
39 syslog('LOG_DEBUG', "OILS: loading bootstrap config: $bsconfig");
42 OpenSRF::System->bootstrap_client(config_file => $bsconfig);
43 syslog('LOG_DEBUG', "OILS: bootstrap loaded..");
45 $self->{osrf_config} = OpenSRF::Utils::SettingsClient->new;
47 Fieldmapper->import($self->{osrf_config}->config_value('IDL'));
49 bless( $self, $type );
52 $self->login( $login->{id}, $login->{password} );
59 return ($val and $val =~ /true/io);
63 $editor = make_editor() unless $editor;
76 # Creates the global editor object
78 require OpenILS::Utils::CStoreEditor;
79 my $e = OpenILS::Utils::CStoreEditor->new(xact => 1);
80 # gnarly cstore hack to re-gen autogen methods after IDL is loaded
81 if(!UNIVERSAL::can($e, 'search_actor_card')) {
82 syslog("LOG_WARNING", "OILS: Reloading CStoreEditor...");
83 delete $INC{'OpenILS/Utils/CStoreEditor.pm'};
84 require OpenILS::Utils::CStoreEditor;
85 $e = OpenILS::Utils::CStoreEditor->new(xact =>1);
93 my( $self, $username, $password ) = @_;
94 syslog('LOG_DEBUG', "OILS: Logging in with username $username");
96 my $seed = $U->simplereq(
98 'open-ils.auth.authenticate.init', $username );
100 my $response = $U->simplereq(
102 'open-ils.auth.authenticate.complete',
104 username => $username,
105 password => md5_hex($seed . md5_hex($password)),
110 if( my $code = $U->event_code($response) ) {
111 my $txt = $response->{textcode};
112 syslog('LOG_WARNING', "OILS: Login failed for $username. $txt:$code");
116 my $key = $response->{payload}->{authtoken};
117 syslog('LOG_INFO', "OILS: Login succeeded for $username : authkey = $key");
118 return $self->{authtoken} = $key;
124 return OpenILS::SIP::Patron->new(@_);
130 return OpenILS::SIP::Item->new(@_);
136 return $self->{institution}->{id};
140 my ($self, $op) = @_;
141 my ($i) = grep { $_->{name} eq $op }
142 @{$config->{implementation_config}->{supports}->{item}};
143 return to_bool($i->{value});
147 my ($self, $id, $whence) = @_;
148 if ($id ne $self->{institution}->{id}) {
149 syslog("LOG_WARNING",
150 "OILS: %s: received institution '%s', expected '%s'",
151 $whence, $id, $self->{institution}->{id});
156 return to_bool($config->{policy}->{checkout});
160 return to_bool($config->{policy}->{checkin});
165 return to_bool($config->{policy}->{renew});
168 sub status_update_ok {
169 return to_bool($config->{policy}->{status_update});
173 return to_bool($config->{policy}->{offline});
179 ## Checkout(patron_id, item_id, sc_renew):
180 ## patron_id & item_id are the identifiers send by the terminal
181 ## sc_renew is the renewal policy configured on the terminal
182 ## returns a status opject that can be queried for the various bits
183 ## of information that the protocol (SIP or NCIP) needs to generate
188 my ($self, $patron_id, $item_id, $sc_renew) = @_;
190 syslog('LOG_DEBUG', "OILS: OpenILS::Checkout attempt: patron=$patron_id, item=$item_id");
192 my $xact = OpenILS::SIP::Transaction::Checkout->new( authtoken => $self->{authtoken} );
193 my $patron = $self->find_patron($patron_id);
194 my $item = $self->find_item($item_id);
196 $xact->patron($patron);
200 $xact->screen_msg("Invalid Patron");
204 if (!$patron->charge_ok) {
205 $xact->screen_msg("Patron Blocked");
210 $xact->screen_msg("Invalid Item");
214 syslog('LOG_DEBUG', "OILS: OpenILS::Checkout data loaded OK, checking out...");
215 $xact->do_checkout();
217 if ($item->{patron} && ($item->{patron} ne $patron_id)) {
218 # I can't deal with this right now
219 # XXX check in then check out?
220 $xact->screen_msg("Item checked out to another patron");
224 $xact->desensitize(!$item->magnetic);
229 syslog("LOG_DEBUG", "OILS: OpenILS::Checkout: " .
230 "patron %s checkout %s succeeded", $patron_id, $item_id);
234 #editor()->xact_rollback;
235 syslog("LOG_DEBUG", "OILS: OpenILS::Checkout: " .
236 "patron %s checkout %s FAILED, rolling back xact...", $patron_id, $item_id);
244 my ($self, $item_id, $trans_date, $return_date,
245 $current_loc, $item_props, $cancel) = @_;
247 syslog('LOG_DEBUG', "OILS: OpenILS::Checkin on item=$item_id");
250 my $xact = OpenILS::SIP::Transaction::Checkin->new(authtoken => $self->{authtoken});
251 my $item = $self->find_item($item_id);
256 $xact->screen_msg("Invalid item barcode: $item_id");
261 $xact->do_checkin( $trans_date, $return_date, $current_loc, $item_props );
265 $xact->patron($patron = $self->find_patron($item->{patron}));
266 delete $item->{patron};
267 delete $item->{due_date};
268 syslog('LOG_INFO', "OILS: Checkin succeeded");
273 #editor()->xact_rollback;
274 syslog('LOG_WARNING', "OILS: Checkin failed");
281 ## If the ILS caches patron information, this lets it free it up
282 sub end_patron_session {
283 my ($self, $patron_id) = @_;
284 return (1, 'Thank you for using OpenILS!', '');
289 # my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
290 # $pay_type, $fee_id, $trans_id, $currency) = @_;
294 # $trans = new ILS::Transaction::FeePayment;
296 # $patron = new ILS::Patron $patron_id;
298 # $trans->transaction_id($trans_id);
299 # $trans->patron($patron);
306 # my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
307 # $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
308 # my ($patron, $item);
313 # $trans = new ILS::Transaction::Hold;
315 # # BEGIN TRANSACTION
316 # $patron = new ILS::Patron $patron_id;
318 # || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
319 # $trans->screen_msg("Invalid Patron.");
324 # $item = new ILS::Item ($item_id || $title_id);
326 # $trans->screen_msg("No such item.");
328 # # END TRANSACTION (conditionally)
330 # } elsif ($item->fee && ($fee_ack ne 'Y')) {
331 # $trans->screen_msg = "Fee required to place hold.";
333 # # END TRANSACTION (conditionally)
338 # item_id => $item->id,
339 # patron_id => $patron->id,
340 # expiration_date => $expiry_date,
341 # pickup_location => $pickup_location,
342 # hold_type => $hold_type,
346 # $trans->patron($patron);
347 # $trans->item($item);
348 # $trans->pickup_location($pickup_location);
350 # push(@{$item->hold_queue}, $hold);
351 # push(@{$patron->{hold_items}}, $hold);
359 # my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
360 # my ($patron, $item, $hold);
363 # $trans = new ILS::Transaction::Hold;
365 # # BEGIN TRANSACTION
366 # $patron = new ILS::Patron $patron_id;
368 # $trans->screen_msg("Invalid patron barcode.");
371 # } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
372 # $trans->screen_msg('Invalid patron password.');
377 # $item = new ILS::Item ($item_id || $title_id);
379 # $trans->screen_msg("No such item.");
381 # # END TRANSACTION (conditionally)
385 # # Remove the hold from the patron's record first
386 # $trans->ok($patron->drop_hold($item_id));
389 # # We didn't find it on the patron record
390 # $trans->screen_msg("No such hold on patron record.");
392 # # END TRANSACTION (conditionally)
396 # # Now, remove it from the item record. If it was on the patron
397 # # record but not on the item record, we'll treat that as success.
398 # foreach my $i (0 .. scalar @{$item->hold_queue}) {
399 # $hold = $item->hold_queue->[$i];
401 # if ($hold->{patron_id} eq $patron->id) {
402 # # found it: delete it.
403 # splice @{$item->hold_queue}, $i, 1;
408 # $trans->screen_msg("Hold Cancelled.");
409 # $trans->patron($patron);
410 # $trans->item($item);
416 ## The patron and item id's can't be altered, but the
417 ## date, location, and type can.
419 # my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
420 # $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
421 # my ($patron, $item);
425 # $trans = new ILS::Transaction::Hold;
427 # # BEGIN TRANSACTION
428 # $patron = new ILS::Patron $patron_id;
430 # $trans->screen_msg("Invalid patron barcode.");
435 # foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
436 # $hold = $patron->{hold_items}[$i];
438 # if ($hold->{item_id} eq $item_id) {
439 # # Found it. So fix it.
440 # $hold->{expiration_date} = $expiry_date if $expiry_date;
441 # $hold->{pickup_location} = $pickup_location if $pickup_location;
442 # $hold->{hold_type} = $hold_type if $hold_type;
445 # $trans->screen_msg("Hold updated.");
446 # $trans->patron($patron);
447 # $trans->item(new ILS::Item $hold->{item_id});
452 # # The same hold structure is linked into both the patron's
453 # # list of hold items and into the queue of outstanding holds
454 # # for the item, so we don't need to search the hold queue for
455 # # the item, since it's already been updated by the patron code.
458 # $trans->screen_msg("No such outstanding hold.");
466 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
467 $no_block, $nb_due_date, $third_party, $item_props, $fee_ack) = @_;
469 my $trans = OpenILS::SIP::Transaction::Renew->new( authtoken => $self->{authtoken} );
470 $trans->patron($self->find_patron($patron_id));
471 $trans->item($self->find_item($item_id));
473 if(!$trans->patron) {
474 $trans->screen_msg("Invalid patron barcode.");
479 if(!$trans->patron->renew_ok) {
480 $trans->screen_msg("Renewals not allowed.");
487 $trans->screen_msg("Item Id renewal not supported.");
489 $trans->screen_msg("Invalid item barcode.");
495 if(!$trans->item->{patron} or
496 $trans->item->{patron} ne $patron_id) {
497 $trans->screen_msg("Item not checked out to " . $trans->patron->name);
502 # Perform the renewal
505 $trans->desensitize(0); # It's already checked out
506 $trans->item->{due_date} = $nb_due_date if $no_block eq 'Y';
507 $trans->item->{sip_item_properties} = $item_props if $item_props;
518 # my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
519 # my ($patron, $item_id);
522 # $trans = new ILS::Transaction::RenewAll;
524 # $trans->patron($patron = new ILS::Patron $patron_id);
525 # if (defined $patron) {
526 # syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s",
527 # $patron->name, $patron->renew_ok);
529 # syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'",
533 # if (!defined($patron)) {
534 # $trans->screen_msg("Invalid patron barcode.");
536 # } elsif (!$patron->renew_ok) {
537 # $trans->screen_msg("Renewals not allowed.");
539 # } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
540 # $trans->screen_msg("Invalid patron password.");
544 # foreach $item_id (@{$patron->{items}}) {
545 # my $item = new ILS::Item $item_id;
547 # if (!defined($item)) {
548 # syslog("LOG_WARNING",
549 # "renew_all: Invalid item id associated with patron '%s'",
554 # if (@{$item->hold_queue}) {
555 # # Can't renew if there are outstanding holds
556 # push @{$trans->unrenewed}, $item_id;
558 # $item->{due_date} = time + (14*24*60*60); # two weeks hence
559 # push @{$trans->renewed}, $item_id;