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", "new ILS '%s'", $institution->{id});
35 $self->{institution} = $institution;
37 my $bsconfig = $institution->{implementation_config}->{bootstrap};
39 syslog('LOG_DEBUG', "loading bootstrap config: $bsconfig");
42 OpenSRF::System->bootstrap_client(config_file => $bsconfig);
43 syslog('LOG_DEBUG', "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;
72 # Creates the global editor object
74 require OpenILS::Utils::CStoreEditor;
75 my $e = OpenILS::Utils::CStoreEditor->new(xact => 1);
76 # gnarly cstore hack to re-gen autogen methods after IDL is loaded
77 if(!UNIVERSAL::can($e, 'search_actor_card')) {
78 syslog("LOG_WARNING", "Reloading CStoreEditor...");
79 delete $INC{'OpenILS/Utils/CStoreEditor.pm'};
80 require OpenILS::Utils::CStoreEditor;
81 $e = OpenILS::Utils::CStoreEditor->new(xact =>1);
89 my( $self, $username, $password ) = @_;
90 syslog('LOG_DEBUG', "OpenILS: Logging in with username $username");
92 my $seed = $U->simplereq(
94 'open-ils.auth.authenticate.init', $username );
96 my $response = $U->simplereq(
98 'open-ils.auth.authenticate.complete',
100 username => $username,
101 password => md5_hex($seed . md5_hex($password)),
106 if( my $code = $U->event_code($response) ) {
107 my $txt = $response->{textcode};
108 syslog('LOG_WARNING', "OpenILS: Login failed for $username. $txt:$code");
112 my $key = $response->{payload}->{authtoken};
113 syslog('LOG_INFO', "OpenILS: Login succeeded for $username : authkey = $key");
114 return $self->{authtoken} = $key;
120 return OpenILS::SIP::Patron->new(@_);
126 return OpenILS::SIP::Item->new(@_);
132 return $self->{institution}->{id};
136 my ($self, $op) = @_;
137 my ($i) = grep { $_->{name} eq $op }
138 @{$config->{implementation_config}->{supports}->{item}};
139 return to_bool($i->{value});
143 my ($self, $id, $whence) = @_;
144 if ($id ne $self->{institution}->{id}) {
145 syslog("LOG_WARNING",
146 "%s: received institution '%s', expected '%s'",
147 $whence, $id, $self->{institution}->{id});
152 return to_bool($config->{policy}->{checkout});
156 return to_bool($config->{policy}->{checkin});
161 return to_bool($config->{policy}->{renew});
164 sub status_update_ok {
165 return to_bool($config->{policy}->{status_update});
169 return to_bool($config->{policy}->{offline});
175 ## Checkout(patron_id, item_id, sc_renew):
176 ## patron_id & item_id are the identifiers send by the terminal
177 ## sc_renew is the renewal policy configured on the terminal
178 ## returns a status opject that can be queried for the various bits
179 ## of information that the protocol (SIP or NCIP) needs to generate
184 my ($self, $patron_id, $item_id, $sc_renew) = @_;
186 syslog('LOG_DEBUG', "OpenILS::Checkout attempt: patron=$patron_id, item=$item_id");
188 my $xact = OpenILS::SIP::Transaction::Checkout->new( authtoken => $self->{authtoken} );
189 my $patron = $self->find_patron($patron_id);
190 my $item = $self->find_item($item_id);
192 $xact->patron($patron);
196 $xact->screen_msg("Invalid Patron");
200 if (!$patron->charge_ok) {
201 $xact->screen_msg("Patron Blocked");
206 $xact->screen_msg("Invalid Item");
210 syslog('LOG_DEBUG', "OpenILS::Checkout data loaded OK, checking out...");
211 $xact->do_checkout();
213 if ($item->{patron} && ($item->{patron} ne $patron_id)) {
214 # I can't deal with this right now
215 # XXX check in then check out?
216 $xact->screen_msg("Item checked out to another patron");
220 $xact->desensitize(!$item->magnetic);
225 syslog("LOG_DEBUG", "OpenILS::Checkout: " .
226 "patron %s checkout %s succeeded", $patron_id, $item_id);
230 #editor()->xact_rollback;
231 syslog("LOG_DEBUG", "OpenILS::Checkout: " .
232 "patron %s checkout %s FAILED, rolling back xact...", $patron_id, $item_id);
240 my ($self, $item_id, $trans_date, $return_date,
241 $current_loc, $item_props, $cancel) = @_;
243 syslog('LOG_DEBUG', "OpenILS::Checkin on item=$item_id");
246 my $xact = OpenILS::SIP::Transaction::Checkin->new(authtoken => $self->{authtoken});
247 my $item = $self->find_item($item_id);
252 $xact->screen_msg("Invalid item barcode: $item_id");
257 $xact->do_checkin( $trans_date, $return_date, $current_loc, $item_props );
261 $xact->patron($patron = $self->find_patron($item->{patron}));
262 delete $item->{patron};
263 delete $item->{due_date};
264 syslog('LOG_INFO', "OpenILS: Checkin succeeded");
269 #editor()->xact_rollback;
270 syslog('LOG_WARNING', "OpenILS: Checkin failed");
277 ## If the ILS caches patron information, this lets it free it up
278 sub end_patron_session {
279 my ($self, $patron_id) = @_;
280 return (1, 'Thank you for using OpenILS!', '');
285 # my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
286 # $pay_type, $fee_id, $trans_id, $currency) = @_;
290 # $trans = new ILS::Transaction::FeePayment;
292 # $patron = new ILS::Patron $patron_id;
294 # $trans->transaction_id($trans_id);
295 # $trans->patron($patron);
302 # my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
303 # $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
304 # my ($patron, $item);
309 # $trans = new ILS::Transaction::Hold;
311 # # BEGIN TRANSACTION
312 # $patron = new ILS::Patron $patron_id;
314 # || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
315 # $trans->screen_msg("Invalid Patron.");
320 # $item = new ILS::Item ($item_id || $title_id);
322 # $trans->screen_msg("No such item.");
324 # # END TRANSACTION (conditionally)
326 # } elsif ($item->fee && ($fee_ack ne 'Y')) {
327 # $trans->screen_msg = "Fee required to place hold.";
329 # # END TRANSACTION (conditionally)
334 # item_id => $item->id,
335 # patron_id => $patron->id,
336 # expiration_date => $expiry_date,
337 # pickup_location => $pickup_location,
338 # hold_type => $hold_type,
342 # $trans->patron($patron);
343 # $trans->item($item);
344 # $trans->pickup_location($pickup_location);
346 # push(@{$item->hold_queue}, $hold);
347 # push(@{$patron->{hold_items}}, $hold);
355 # my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
356 # my ($patron, $item, $hold);
359 # $trans = new ILS::Transaction::Hold;
361 # # BEGIN TRANSACTION
362 # $patron = new ILS::Patron $patron_id;
364 # $trans->screen_msg("Invalid patron barcode.");
367 # } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
368 # $trans->screen_msg('Invalid patron password.');
373 # $item = new ILS::Item ($item_id || $title_id);
375 # $trans->screen_msg("No such item.");
377 # # END TRANSACTION (conditionally)
381 # # Remove the hold from the patron's record first
382 # $trans->ok($patron->drop_hold($item_id));
385 # # We didn't find it on the patron record
386 # $trans->screen_msg("No such hold on patron record.");
388 # # END TRANSACTION (conditionally)
392 # # Now, remove it from the item record. If it was on the patron
393 # # record but not on the item record, we'll treat that as success.
394 # foreach my $i (0 .. scalar @{$item->hold_queue}) {
395 # $hold = $item->hold_queue->[$i];
397 # if ($hold->{patron_id} eq $patron->id) {
398 # # found it: delete it.
399 # splice @{$item->hold_queue}, $i, 1;
404 # $trans->screen_msg("Hold Cancelled.");
405 # $trans->patron($patron);
406 # $trans->item($item);
412 ## The patron and item id's can't be altered, but the
413 ## date, location, and type can.
415 # my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
416 # $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
417 # my ($patron, $item);
421 # $trans = new ILS::Transaction::Hold;
423 # # BEGIN TRANSACTION
424 # $patron = new ILS::Patron $patron_id;
426 # $trans->screen_msg("Invalid patron barcode.");
431 # foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
432 # $hold = $patron->{hold_items}[$i];
434 # if ($hold->{item_id} eq $item_id) {
435 # # Found it. So fix it.
436 # $hold->{expiration_date} = $expiry_date if $expiry_date;
437 # $hold->{pickup_location} = $pickup_location if $pickup_location;
438 # $hold->{hold_type} = $hold_type if $hold_type;
441 # $trans->screen_msg("Hold updated.");
442 # $trans->patron($patron);
443 # $trans->item(new ILS::Item $hold->{item_id});
448 # # The same hold structure is linked into both the patron's
449 # # list of hold items and into the queue of outstanding holds
450 # # for the item, so we don't need to search the hold queue for
451 # # the item, since it's already been updated by the patron code.
454 # $trans->screen_msg("No such outstanding hold.");
462 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
463 $no_block, $nb_due_date, $third_party, $item_props, $fee_ack) = @_;
465 my $trans = OpenILS::SIP::Transaction::Renew->new( authtoken => $self->{authtoken} );
466 $trans->patron($self->find_patron($patron_id));
467 $trans->item($self->find_item($item_id));
469 if(!$trans->patron) {
470 $trans->screen_msg("Invalid patron barcode.");
475 if(!$trans->patron->renew_ok) {
476 $trans->screen_msg("Renewals not allowed.");
483 $trans->screen_msg("Item Id renewal not supported.");
485 $trans->screen_msg("Invalid item barcode.");
491 if(!$trans->item->{patron} or
492 $trans->item->{patron} ne $patron_id) {
493 $trans->screen_msg("Item not checked out to " . $trans->patron->name);
498 # Perform the renewal
501 $trans->desensitize(0); # It's already checked out
502 $trans->item->{due_date} = $nb_due_date if $no_block eq 'Y';
503 $trans->item->{sip_item_properties} = $item_props if $item_props;
514 # my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
515 # my ($patron, $item_id);
518 # $trans = new ILS::Transaction::RenewAll;
520 # $trans->patron($patron = new ILS::Patron $patron_id);
521 # if (defined $patron) {
522 # syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s",
523 # $patron->name, $patron->renew_ok);
525 # syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'",
529 # if (!defined($patron)) {
530 # $trans->screen_msg("Invalid patron barcode.");
532 # } elsif (!$patron->renew_ok) {
533 # $trans->screen_msg("Renewals not allowed.");
535 # } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
536 # $trans->screen_msg("Invalid patron password.");
540 # foreach $item_id (@{$patron->{items}}) {
541 # my $item = new ILS::Item $item_id;
543 # if (!defined($item)) {
544 # syslog("LOG_WARNING",
545 # "renew_all: Invalid item id associated with patron '%s'",
550 # if (@{$item->hold_queue}) {
551 # # Can't renew if there are outstanding holds
552 # push @{$trans->unrenewed}, $item_id;
554 # $item->{due_date} = time + (14*24*60*60); # two weeks hence
555 # push @{$trans->renewed}, $item_id;