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 $config = $institution;
36 syslog("LOG_DEBUG", "OILS: new ILS '%s'", $institution->{id});
37 $self->{institution} = $institution;
39 my $bsconfig = $institution->{implementation_config}->{bootstrap};
41 syslog('LOG_DEBUG', "OILS: loading bootstrap config: $bsconfig");
44 OpenSRF::System->bootstrap_client(config_file => $bsconfig);
45 syslog('LOG_DEBUG', "OILS: bootstrap loaded..");
47 $self->{osrf_config} = OpenSRF::Utils::SettingsClient->new;
49 Fieldmapper->import($self->{osrf_config}->config_value('IDL'));
51 bless( $self, $type );
54 $self->login( $login->{id}, $login->{password} );
61 return ($val and $val =~ /true/io);
65 $editor = make_editor() unless $editor;
78 # Creates the global editor object
80 require OpenILS::Utils::CStoreEditor;
81 my $e = OpenILS::Utils::CStoreEditor->new(xact => 1);
82 # gnarly cstore hack to re-gen autogen methods after IDL is loaded
83 if(!UNIVERSAL::can($e, 'search_actor_card')) {
84 syslog("LOG_WARNING", "OILS: Reloading CStoreEditor...");
85 delete $INC{'OpenILS/Utils/CStoreEditor.pm'};
86 require OpenILS::Utils::CStoreEditor;
87 $e = OpenILS::Utils::CStoreEditor->new(xact =>1);
95 return "" unless $date;
97 $date = DateTime::Format::ISO8601->new->
98 parse_datetime(OpenSRF::Utils::clense_ISO8601($date));
99 my @time = localtime($date->epoch);
101 my $year = $time[5]+1900;
102 my $mon = $time[4]+1;
105 $mon =~ s/^(\d)$/0$1/;
106 $day =~ s/^(\d)$/0$1/;
107 $date = "$year$mon$day";
108 syslog('LOG_DEBUG', "OILS: formatted date: $date");
115 my( $self, $username, $password ) = @_;
116 syslog('LOG_DEBUG', "OILS: Logging in with username $username");
118 my $seed = $U->simplereq(
120 'open-ils.auth.authenticate.init', $username );
122 my $response = $U->simplereq(
124 'open-ils.auth.authenticate.complete',
126 username => $username,
127 password => md5_hex($seed . md5_hex($password)),
132 if( my $code = $U->event_code($response) ) {
133 my $txt = $response->{textcode};
134 syslog('LOG_WARNING', "OILS: Login failed for $username. $txt:$code");
138 my $key = $response->{payload}->{authtoken};
139 syslog('LOG_INFO', "OILS: Login succeeded for $username : authkey = $key");
140 return $self->{authtoken} = $key;
146 return OpenILS::SIP::Patron->new(@_);
152 return OpenILS::SIP::Item->new(@_);
158 return $self->{institution}->{id};
162 my ($self, $op) = @_;
163 my ($i) = grep { $_->{name} eq $op }
164 @{$config->{implementation_config}->{supports}->{item}};
165 return to_bool($i->{value});
169 my ($self, $id, $whence) = @_;
170 if ($id ne $self->{institution}->{id}) {
171 syslog("LOG_WARNING",
172 "OILS: %s: received institution '%s', expected '%s'",
173 $whence, $id, $self->{institution}->{id});
178 return to_bool($config->{policy}->{checkout});
182 return to_bool($config->{policy}->{checkin});
187 return to_bool($config->{policy}->{renew});
190 sub status_update_ok {
191 return to_bool($config->{policy}->{status_update});
195 return to_bool($config->{policy}->{offline});
201 ## Checkout(patron_id, item_id, sc_renew):
202 ## patron_id & item_id are the identifiers send by the terminal
203 ## sc_renew is the renewal policy configured on the terminal
204 ## returns a status opject that can be queried for the various bits
205 ## of information that the protocol (SIP or NCIP) needs to generate
210 my ($self, $patron_id, $item_id, $sc_renew) = @_;
212 syslog('LOG_DEBUG', "OILS: OpenILS::Checkout attempt: patron=$patron_id, item=$item_id");
214 my $xact = OpenILS::SIP::Transaction::Checkout->new( authtoken => $self->{authtoken} );
215 my $patron = $self->find_patron($patron_id);
216 my $item = $self->find_item($item_id);
218 $xact->patron($patron);
222 $xact->screen_msg("Invalid Patron");
226 if (!$patron->charge_ok) {
227 $xact->screen_msg("Patron Blocked");
232 $xact->screen_msg("Invalid Item");
236 syslog('LOG_DEBUG', "OILS: OpenILS::Checkout data loaded OK, checking out...");
237 $xact->do_checkout();
239 if ($item->{patron} && ($item->{patron} ne $patron_id)) {
240 # I can't deal with this right now
241 # XXX check in then check out?
242 $xact->screen_msg("Item checked out to another patron");
246 $xact->desensitize(!$item->magnetic);
251 syslog("LOG_DEBUG", "OILS: OpenILS::Checkout: " .
252 "patron %s checkout %s succeeded", $patron_id, $item_id);
256 #editor()->xact_rollback;
257 syslog("LOG_DEBUG", "OILS: OpenILS::Checkout: " .
258 "patron %s checkout %s FAILED, rolling back xact...", $patron_id, $item_id);
266 my ($self, $item_id, $trans_date, $return_date,
267 $current_loc, $item_props, $cancel) = @_;
269 syslog('LOG_DEBUG', "OILS: OpenILS::Checkin on item=$item_id");
272 my $xact = OpenILS::SIP::Transaction::Checkin->new(authtoken => $self->{authtoken});
273 my $item = $self->find_item($item_id);
278 $xact->screen_msg("Invalid item barcode: $item_id");
283 $xact->do_checkin( $trans_date, $return_date, $current_loc, $item_props );
287 $xact->patron($patron = $self->find_patron($item->{patron}));
288 delete $item->{patron};
289 delete $item->{due_date};
290 syslog('LOG_INFO', "OILS: Checkin succeeded");
295 #editor()->xact_rollback;
296 syslog('LOG_WARNING', "OILS: Checkin failed");
303 ## If the ILS caches patron information, this lets it free it up
304 sub end_patron_session {
305 my ($self, $patron_id) = @_;
306 return (1, 'Thank you for using OpenILS!', '');
311 # my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
312 # $pay_type, $fee_id, $trans_id, $currency) = @_;
316 # $trans = new ILS::Transaction::FeePayment;
318 # $patron = new ILS::Patron $patron_id;
320 # $trans->transaction_id($trans_id);
321 # $trans->patron($patron);
328 # my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
329 # $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
330 # my ($patron, $item);
335 # $trans = new ILS::Transaction::Hold;
337 # # BEGIN TRANSACTION
338 # $patron = new ILS::Patron $patron_id;
340 # || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
341 # $trans->screen_msg("Invalid Patron.");
346 # $item = new ILS::Item ($item_id || $title_id);
348 # $trans->screen_msg("No such item.");
350 # # END TRANSACTION (conditionally)
352 # } elsif ($item->fee && ($fee_ack ne 'Y')) {
353 # $trans->screen_msg = "Fee required to place hold.";
355 # # END TRANSACTION (conditionally)
360 # item_id => $item->id,
361 # patron_id => $patron->id,
362 # expiration_date => $expiry_date,
363 # pickup_location => $pickup_location,
364 # hold_type => $hold_type,
368 # $trans->patron($patron);
369 # $trans->item($item);
370 # $trans->pickup_location($pickup_location);
372 # push(@{$item->hold_queue}, $hold);
373 # push(@{$patron->{hold_items}}, $hold);
381 # my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
382 # my ($patron, $item, $hold);
385 # $trans = new ILS::Transaction::Hold;
387 # # BEGIN TRANSACTION
388 # $patron = new ILS::Patron $patron_id;
390 # $trans->screen_msg("Invalid patron barcode.");
393 # } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
394 # $trans->screen_msg('Invalid patron password.');
399 # $item = new ILS::Item ($item_id || $title_id);
401 # $trans->screen_msg("No such item.");
403 # # END TRANSACTION (conditionally)
407 # # Remove the hold from the patron's record first
408 # $trans->ok($patron->drop_hold($item_id));
411 # # We didn't find it on the patron record
412 # $trans->screen_msg("No such hold on patron record.");
414 # # END TRANSACTION (conditionally)
418 # # Now, remove it from the item record. If it was on the patron
419 # # record but not on the item record, we'll treat that as success.
420 # foreach my $i (0 .. scalar @{$item->hold_queue}) {
421 # $hold = $item->hold_queue->[$i];
423 # if ($hold->{patron_id} eq $patron->id) {
424 # # found it: delete it.
425 # splice @{$item->hold_queue}, $i, 1;
430 # $trans->screen_msg("Hold Cancelled.");
431 # $trans->patron($patron);
432 # $trans->item($item);
438 ## The patron and item id's can't be altered, but the
439 ## date, location, and type can.
441 # my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
442 # $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
443 # my ($patron, $item);
447 # $trans = new ILS::Transaction::Hold;
449 # # BEGIN TRANSACTION
450 # $patron = new ILS::Patron $patron_id;
452 # $trans->screen_msg("Invalid patron barcode.");
457 # foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
458 # $hold = $patron->{hold_items}[$i];
460 # if ($hold->{item_id} eq $item_id) {
461 # # Found it. So fix it.
462 # $hold->{expiration_date} = $expiry_date if $expiry_date;
463 # $hold->{pickup_location} = $pickup_location if $pickup_location;
464 # $hold->{hold_type} = $hold_type if $hold_type;
467 # $trans->screen_msg("Hold updated.");
468 # $trans->patron($patron);
469 # $trans->item(new ILS::Item $hold->{item_id});
474 # # The same hold structure is linked into both the patron's
475 # # list of hold items and into the queue of outstanding holds
476 # # for the item, so we don't need to search the hold queue for
477 # # the item, since it's already been updated by the patron code.
480 # $trans->screen_msg("No such outstanding hold.");
488 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
489 $no_block, $nb_due_date, $third_party, $item_props, $fee_ack) = @_;
491 my $trans = OpenILS::SIP::Transaction::Renew->new( authtoken => $self->{authtoken} );
492 $trans->patron($self->find_patron($patron_id));
493 $trans->item($self->find_item($item_id));
495 if(!$trans->patron) {
496 $trans->screen_msg("Invalid patron barcode.");
501 if(!$trans->patron->renew_ok) {
502 $trans->screen_msg("Renewals not allowed.");
509 $trans->screen_msg("Item Id renewal not supported.");
511 $trans->screen_msg("Invalid item barcode.");
517 if(!$trans->item->{patron} or
518 $trans->item->{patron} ne $patron_id) {
519 $trans->screen_msg("Item not checked out to " . $trans->patron->name);
524 # Perform the renewal
527 $trans->desensitize(0); # It's already checked out
528 $trans->item->{due_date} = $nb_due_date if $no_block eq 'Y';
529 $trans->item->{sip_item_properties} = $item_props if $item_props;
540 # my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
541 # my ($patron, $item_id);
544 # $trans = new ILS::Transaction::RenewAll;
546 # $trans->patron($patron = new ILS::Patron $patron_id);
547 # if (defined $patron) {
548 # syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s",
549 # $patron->name, $patron->renew_ok);
551 # syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'",
555 # if (!defined($patron)) {
556 # $trans->screen_msg("Invalid patron barcode.");
558 # } elsif (!$patron->renew_ok) {
559 # $trans->screen_msg("Renewals not allowed.");
561 # } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
562 # $trans->screen_msg("Invalid patron password.");
566 # foreach $item_id (@{$patron->{items}}) {
567 # my $item = new ILS::Item $item_id;
569 # if (!defined($item)) {
570 # syslog("LOG_WARNING",
571 # "renew_all: Invalid item id associated with patron '%s'",
576 # if (@{$item->hold_queue}) {
577 # # Can't renew if there are outstanding holds
578 # push @{$trans->unrenewed}, $item_id;
580 # $item->{due_date} = time + (14*24*60*60); # two weeks hence
581 # push @{$trans->renewed}, $item_id;