]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/SIP.pm
updated due-dates format to conform with vendors - still need to decide if the hour...
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / SIP.pm
1 #
2 # ILS.pm: Test ILS interface module
3 #
4
5 package OpenILS::SIP;
6 use warnings; use strict;
7
8 use Sys::Syslog qw(syslog);
9
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;
16
17 use OpenSRF::System;
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';
24
25 my $editor;
26 my $config;
27
28 use Digest::MD5 qw(md5_hex);
29
30 sub new {
31         my ($class, $institution, $login) = @_;
32         my $type = ref($class) || $class;
33         my $self = {};
34
35         $self->{login} = $login;
36
37         $config = $institution;
38         syslog("LOG_DEBUG", "OILS: new ILS '%s'", $institution->{id});
39         $self->{institution} = $institution;
40
41         my $bsconfig = $institution->{implementation_config}->{bootstrap};
42
43         syslog('LOG_DEBUG', "OILS: loading bootstrap config: $bsconfig");
44         
45         local $/ = "\n";
46         OpenSRF::System->bootstrap_client(config_file => $bsconfig);
47         syslog('LOG_DEBUG', "OILS: bootstrap loaded..");
48
49         $self->{osrf_config} = OpenSRF::Utils::SettingsClient->new;
50
51         Fieldmapper->import($self->{osrf_config}->config_value('IDL'));
52
53         bless( $self, $type );
54
55         return undef unless 
56                 $self->login( $login->{id}, $login->{password} );
57
58         return $self;
59 }
60
61 sub verify_session {
62         my $self = shift;
63         my $ses = $U->simplereq( 
64                 'open-ils.auth',
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} );
69 }
70
71 sub to_bool {
72         my $val = shift;
73         return ($val and $val =~ /true/io);
74 }
75
76 sub editor {
77         return $editor 
78                 if $editor and $editor->{session}
79                 and $editor->session->connected;
80         return $editor = make_editor();
81 }
82
83 sub reset_editor {
84         $editor = undef;
85         return editor();
86 }
87
88 sub config {
89         return $config;
90 }
91
92
93 # Creates the global editor object
94 sub make_editor {
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);
103         }
104         return $e;
105 }
106
107 sub format_date {
108         my $class = shift;
109         my $date = shift;
110         my $type = shift || 'dob';
111
112         return "" unless $date;
113
114         $date = DateTime::Format::ISO8601->new->
115                 parse_datetime(OpenSRF::Utils::clense_ISO8601($date));
116         my @time = localtime($date->epoch);
117
118         my $year = $time[5]+1900;
119         my $mon = $time[4]+1;
120         my $day = $time[3];
121
122         $mon =~ s/^(\d)$/0$1/;
123         $day =~ s/^(\d)$/0$1/;
124         $date = "$year$mon$day";
125
126         $date = $year.'-'.$mon.'-'.$day .' 0:00:00' if $type eq 'due';
127
128         syslog('LOG_DEBUG', "OILS: formatted date [type=$type]: $date");
129         return $date;
130 }
131
132
133
134 sub login {
135         my( $self, $username, $password ) = @_;
136         syslog('LOG_DEBUG', "OILS: Logging in with username $username");
137
138         my $seed = $U->simplereq( 
139                 'open-ils.auth',
140                 'open-ils.auth.authenticate.init', $username );
141
142         my $response = $U->simplereq(
143                 'open-ils.auth', 
144                 'open-ils.auth.authenticate.complete', 
145                 {       
146                         username => $username, 
147                         password => md5_hex($seed . md5_hex($password)), 
148                         type            => 'opac',
149                 }
150         );
151
152         if( my $code = $U->event_code($response) ) {
153                 my $txt = $response->{textcode};
154                 syslog('LOG_WARNING', "OILS: Login failed for $username.  $txt:$code");
155                 return undef;
156         }
157
158         my $key = $response->{payload}->{authtoken};
159         syslog('LOG_INFO', "OILS: Login succeeded for $username : authkey = $key");
160         return $self->{authtoken} = $key;
161 }
162
163
164 sub find_patron {
165         my $self = shift;
166         return OpenILS::SIP::Patron->new(@_);
167 }
168
169
170 sub find_item {
171         my $self = shift;
172         return OpenILS::SIP::Item->new(@_);
173 }
174
175
176 sub institution {
177     my $self = shift;
178     return $self->{institution}->{id};
179 }
180
181 sub supports {
182         my ($self, $op) = @_;
183         my ($i) = grep { $_->{name} eq $op }  
184                 @{$config->{implementation_config}->{supports}->{item}};
185         return to_bool($i->{value});
186 }
187
188 sub check_inst_id {
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});
194         }
195 }
196
197 sub checkout_ok {
198         return to_bool($config->{policy}->{checkout});
199 }
200
201 sub checkin_ok {
202         return to_bool($config->{policy}->{checkin});
203     return 0;
204 }
205
206 sub renew_ok {
207         return to_bool($config->{policy}->{renew});
208 }
209
210 sub status_update_ok {
211         return to_bool($config->{policy}->{status_update});
212 }
213
214 sub offline_ok {
215         return to_bool($config->{policy}->{offline});
216 }
217
218
219
220 ##
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
226 ## the response.
227 ##
228
229 sub checkout {
230         my ($self, $patron_id, $item_id, $sc_renew) = @_;
231
232         $self->verify_session;
233         
234         syslog('LOG_DEBUG', "OILS: OpenILS::Checkout attempt: patron=$patron_id, item=$item_id");
235         
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);
239         
240         $xact->patron($patron);
241         $xact->item($item);
242
243         if (!$patron) {
244                 $xact->screen_msg("Invalid Patron");
245                 return $xact;
246         }
247
248         if (!$patron->charge_ok) {
249                 $xact->screen_msg("Patron Blocked");
250                 return $xact;
251         }
252
253         if( !$item ) {
254                 $xact->screen_msg("Invalid Item");
255                 return $xact;
256         }
257
258         syslog('LOG_DEBUG', "OILS: OpenILS::Checkout data loaded OK, checking out...");
259         $xact->do_checkout();
260
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");
265                 $xact->ok(0);
266         } 
267
268         $xact->desensitize(!$item->magnetic);
269
270         if( $xact->ok ) {
271
272                 #editor()->commit;
273                 syslog("LOG_DEBUG", "OILS: OpenILS::Checkout: " .
274                         "patron %s checkout %s succeeded", $patron_id, $item_id);
275
276         } else {
277
278                 #editor()->xact_rollback;
279                 syslog("LOG_DEBUG", "OILS: OpenILS::Checkout: " .
280                         "patron %s checkout %s FAILED, rolling back xact...", $patron_id, $item_id);
281         }
282
283         return $xact;
284 }
285
286
287 sub checkin {
288         my ($self, $item_id, $trans_date, $return_date,
289         $current_loc, $item_props, $cancel) = @_;
290
291         $self->verify_session;
292
293         syslog('LOG_DEBUG', "OILS: OpenILS::Checkin on item=$item_id");
294         
295         my $patron;
296         my $xact                = OpenILS::SIP::Transaction::Checkin->new(authtoken => $self->{authtoken});
297         my $item                = $self->find_item($item_id);
298
299         $xact->item($item);
300
301         if(!$xact->item) {
302                 $xact->screen_msg("Invalid item barcode: $item_id");
303                 $xact->ok(0);
304                 return $xact;
305         }
306
307         $xact->do_checkin( $trans_date, $return_date, $current_loc, $item_props );
308         
309         if ($xact->ok) {
310
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");
315                 #editor()->commit;
316
317         } else {
318
319                 #editor()->xact_rollback;
320                 syslog('LOG_WARNING', "OILS: Checkin failed");
321         }
322         # END TRANSACTION
323
324         return $xact;
325 }
326
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!', '');
331 }
332
333
334 #sub pay_fee {
335 #    my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
336 #       $pay_type, $fee_id, $trans_id, $currency) = @_;
337 #    my $trans;
338 #    my $patron;
339 #
340 #    $trans = new ILS::Transaction::FeePayment;
341 #
342 #    $patron = new ILS::Patron $patron_id;
343 #
344 #    $trans->transaction_id($trans_id);
345 #    $trans->patron($patron);
346 #    $trans->ok(1);
347 #
348 #    return $trans;
349 #}
350 #
351 #sub add_hold {
352 #    my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
353 #       $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
354 #    my ($patron, $item);
355 #    my $hold;
356 #    my $trans;
357 #
358 #
359 #    $trans = new ILS::Transaction::Hold;
360 #
361 #    # BEGIN TRANSACTION
362 #    $patron = new ILS::Patron $patron_id;
363 #    if (!$patron
364 #       || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
365 #       $trans->screen_msg("Invalid Patron.");
366 #
367 #       return $trans;
368 #    }
369 #
370 #    $item = new ILS::Item ($item_id || $title_id);
371 #    if (!$item) {
372 #       $trans->screen_msg("No such item.");
373 #
374 #       # END TRANSACTION (conditionally)
375 #       return $trans;
376 #    } elsif ($item->fee && ($fee_ack ne 'Y')) {
377 #       $trans->screen_msg = "Fee required to place hold.";
378 #
379 #       # END TRANSACTION (conditionally)
380 #       return $trans;
381 #    }
382 #
383 #    $hold = {
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,
389 #    };
390 #
391 #    $trans->ok(1);
392 #    $trans->patron($patron);
393 #    $trans->item($item);
394 #    $trans->pickup_location($pickup_location);
395 #
396 #    push(@{$item->hold_queue}, $hold);
397 #    push(@{$patron->{hold_items}}, $hold);
398 #
399 #
400 #    # END TRANSACTION
401 #    return $trans;
402 #}
403 #
404 #sub cancel_hold {
405 #    my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
406 #    my ($patron, $item, $hold);
407 #    my $trans;
408 #
409 #    $trans = new ILS::Transaction::Hold;
410 #
411 #    # BEGIN TRANSACTION
412 #    $patron = new ILS::Patron $patron_id;
413 #    if (!$patron) {
414 #       $trans->screen_msg("Invalid patron barcode.");
415 #
416 #       return $trans;
417 #    } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
418 #       $trans->screen_msg('Invalid patron password.');
419 #
420 #       return $trans;
421 #    }
422 #
423 #    $item = new ILS::Item ($item_id || $title_id);
424 #    if (!$item) {
425 #       $trans->screen_msg("No such item.");
426 #
427 #       # END TRANSACTION (conditionally)
428 #       return $trans;
429 #    }
430 #
431 #    # Remove the hold from the patron's record first
432 #    $trans->ok($patron->drop_hold($item_id));
433 #
434 #    if (!$trans->ok) {
435 #       # We didn't find it on the patron record
436 #       $trans->screen_msg("No such hold on patron record.");
437 #
438 #       # END TRANSACTION (conditionally)
439 #       return $trans;
440 #    }
441 #
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];
446 #
447 #       if ($hold->{patron_id} eq $patron->id) {
448 #           # found it: delete it.
449 #           splice @{$item->hold_queue}, $i, 1;
450 #           last;
451 #       }
452 #    }
453 #
454 #    $trans->screen_msg("Hold Cancelled.");
455 #    $trans->patron($patron);
456 #    $trans->item($item);
457 #
458 #    return $trans;
459 #}
460 #
461 #
462 ## The patron and item id's can't be altered, but the
463 ## date, location, and type can.
464 #sub alter_hold {
465 #    my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
466 #       $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
467 #    my ($patron, $item);
468 #    my $hold;
469 #    my $trans;
470 #
471 #    $trans = new ILS::Transaction::Hold;
472 #
473 #    # BEGIN TRANSACTION
474 #    $patron = new ILS::Patron $patron_id;
475 #    if (!$patron) {
476 #       $trans->screen_msg("Invalid patron barcode.");
477 #
478 #       return $trans;
479 #    }
480 #
481 #    foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
482 #       $hold = $patron->{hold_items}[$i];
483 #
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;
489 #
490 #           $trans->ok(1);
491 #           $trans->screen_msg("Hold updated.");
492 #           $trans->patron($patron);
493 #           $trans->item(new ILS::Item $hold->{item_id});
494 #           last;
495 #       }
496 #    }
497 #
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.
502 #
503 #    if (!$trans->ok) {
504 #       $trans->screen_msg("No such outstanding hold.");
505 #    }
506 #
507 #    return $trans;
508 #}
509
510
511 sub renew {
512         my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
513                 $no_block, $nb_due_date, $third_party, $item_props, $fee_ack) = @_;
514
515         $self->verify_session;
516
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));
520
521         if(!$trans->patron) {
522                 $trans->screen_msg("Invalid patron barcode.");
523                 $trans->ok(0);
524                 return $trans;
525         }
526
527         if(!$trans->patron->renew_ok) {
528                 $trans->screen_msg("Renewals not allowed.");
529                 $trans->ok(0);
530                 return $trans;
531         }
532
533         if(!$trans->item) {
534                 if( $title_id ) {
535                         $trans->screen_msg("Item Id renewal not supported.");
536                 } else {
537                         $trans->screen_msg("Invalid item barcode.");
538                 }
539                 $trans->ok(0);
540                 return $trans;
541         }
542
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);
546                 $trans->ok(0);
547                 return $trans;
548         }
549
550         # Perform the renewal
551         $trans->do_renew();
552
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;
556
557         return $trans;
558 }
559
560
561
562
563
564 #
565 #sub renew_all {
566 #    my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
567 #    my ($patron, $item_id);
568 #    my $trans;
569 #
570 #    $trans = new ILS::Transaction::RenewAll;
571 #
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);
576 #    } else {
577 #       syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'",
578 #              $patron_id);
579 #    }
580 #
581 #    if (!defined($patron)) {
582 #       $trans->screen_msg("Invalid patron barcode.");
583 #       return $trans;
584 #    } elsif (!$patron->renew_ok) {
585 #       $trans->screen_msg("Renewals not allowed.");
586 #       return $trans;
587 #    } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
588 #       $trans->screen_msg("Invalid patron password.");
589 #       return $trans;
590 #    }
591 #
592 #    foreach $item_id (@{$patron->{items}}) {
593 #       my $item = new ILS::Item $item_id;
594 #
595 #       if (!defined($item)) {
596 #           syslog("LOG_WARNING",
597 #                  "renew_all: Invalid item id associated with patron '%s'",
598 #                  $patron->id);
599 #           next;
600 #       }
601 #
602 #       if (@{$item->hold_queue}) {
603 #           # Can't renew if there are outstanding holds
604 #           push @{$trans->unrenewed}, $item_id;
605 #       } else {
606 #           $item->{due_date} = time + (14*24*60*60); # two weeks hence
607 #           push @{$trans->renewed}, $item_id;
608 #       }
609 #    }
610 #
611 #    $trans->ok(1);
612 #
613 #    return $trans;
614 #}
615
616 1;