4 # A Class for handing SIP messages
7 package C4::SIP::Sip::MsgType;
13 use C4::SIP::Sip qw(:all);
14 use C4::SIP::Sip::Constants qw(:all);
15 use C4::SIP::Sip::Checksum qw(verify_cksum);
20 use C4::Auth qw(&check_api_auth);
21 use C4::Items qw(ModDateLastSeen);
24 use Koha::Patron::Attributes;
27 use Koha::DateUtils qw( output_pref );
31 use vars qw(@ISA @EXPORT_OK);
33 use constant INVALID_CARD => 'Invalid cardnumber';
34 use constant INVALID_PW => 'Invalid password';
38 @EXPORT_OK = qw(handle login_core);
41 # Predeclare handler subroutines
42 use subs qw(handle_patron_status handle_checkout handle_checkin
43 handle_block_patron handle_sc_status handle_request_acs_resend
44 handle_login handle_patron_info handle_end_patron_session
45 handle_fee_paid handle_item_information handle_item_status_update
46 handle_patron_enable handle_hold handle_renew handle_renew_all);
49 # For the most part, Version 2.00 of the protocol just adds new
50 # variable fields, but sometimes it changes the fixed header.
52 # In general, if there's no '2.00' protocol entry for a handler, that's
53 # because 2.00 didn't extend the 1.00 version of the protocol. This will
54 # be handled by the module initialization code following the declaration,
55 # which goes through the handlers table and creates a '2.00' entry that
56 # points to the same place as the '1.00' entry. If there's a 2.00 entry
57 # but no 1.00 entry, then that means that it's a completely new service
58 # in 2.00, so 1.00 shouldn't recognize it.
61 (PATRON_STATUS_REQ) => {
62 name => "Patron Status Request",
63 handler => \&handle_patron_status,
68 fields => [ (FID_INST_ID), (FID_PATRON_ID), (FID_TERMINAL_PWD), (FID_PATRON_PWD) ],
74 handler => \&handle_checkout,
77 template => "A1A1A18A18",
79 fields => [ (FID_INST_ID), (FID_PATRON_ID), (FID_ITEM_ID), (FID_TERMINAL_PWD) ],
82 template => "A1A1A18A18",
84 fields => [ (FID_INST_ID), (FID_PATRON_ID), (FID_ITEM_ID), (FID_TERMINAL_PWD), (FID_ITEM_PROPS), (FID_PATRON_PWD), (FID_FEE_ACK), (FID_CANCEL) ],
90 handler => \&handle_checkin,
93 template => "A1A18A18",
95 fields => [ (FID_CURRENT_LOCN), (FID_INST_ID), (FID_ITEM_ID), (FID_TERMINAL_PWD) ],
98 template => "A1A18A18",
100 fields => [ (FID_CURRENT_LOCN), (FID_INST_ID), (FID_ITEM_ID), (FID_TERMINAL_PWD), (FID_ITEM_PROPS), (FID_CANCEL) ],
105 name => "Block Patron",
106 handler => \&handle_block_patron,
111 fields => [ (FID_INST_ID), (FID_BLOCKED_CARD_MSG), (FID_PATRON_ID), (FID_TERMINAL_PWD) ],
117 handler => \&handle_sc_status,
120 template => "A1A3A4",
126 (REQUEST_ACS_RESEND) => {
127 name => "Request ACS Resend",
128 handler => \&handle_request_acs_resend,
139 handler => \&handle_login,
144 fields => [ (FID_LOGIN_UID), (FID_LOGIN_PWD), (FID_LOCATION_CODE) ],
149 name => "Patron Info",
150 handler => \&handle_patron_info,
153 template => "A3A18A10",
155 fields => [ (FID_INST_ID), (FID_PATRON_ID), (FID_TERMINAL_PWD), (FID_PATRON_PWD), (FID_START_ITEM), (FID_END_ITEM) ],
159 (END_PATRON_SESSION) => {
160 name => "End Patron Session",
161 handler => \&handle_end_patron_session,
166 fields => [ (FID_INST_ID), (FID_PATRON_ID), (FID_TERMINAL_PWD), (FID_PATRON_PWD) ],
172 handler => \&handle_fee_paid,
175 template => "A18A2A2A3",
177 fields => [ (FID_FEE_AMT), (FID_INST_ID), (FID_PATRON_ID), (FID_TERMINAL_PWD), (FID_PATRON_PWD), (FID_FEE_ID), (FID_TRANSACTION_ID) ],
181 (ITEM_INFORMATION) => {
182 name => "Item Information",
183 handler => \&handle_item_information,
188 fields => [ (FID_INST_ID), (FID_ITEM_ID), (FID_TERMINAL_PWD) ],
192 (ITEM_STATUS_UPDATE) => {
193 name => "Item Status Update",
194 handler => \&handle_item_status_update,
199 fields => [ (FID_INST_ID), (FID_PATRON_ID), (FID_ITEM_ID), (FID_TERMINAL_PWD), (FID_ITEM_PROPS) ],
204 name => "Patron Enable",
205 handler => \&handle_patron_enable,
210 fields => [ (FID_INST_ID), (FID_PATRON_ID), (FID_TERMINAL_PWD), (FID_PATRON_PWD) ],
216 handler => \&handle_hold,
222 (FID_EXPIRATION), (FID_PICKUP_LOCN), (FID_HOLD_TYPE), (FID_INST_ID), (FID_PATRON_ID), (FID_PATRON_PWD),
223 (FID_ITEM_ID), (FID_TITLE_ID), (FID_TERMINAL_PWD), (FID_FEE_ACK)
230 handler => \&handle_renew,
233 template => "A1A1A18A18",
235 fields => [ (FID_INST_ID), (FID_PATRON_ID), (FID_PATRON_PWD), (FID_ITEM_ID), (FID_TITLE_ID), (FID_TERMINAL_PWD), (FID_ITEM_PROPS), (FID_FEE_ACK) ],
241 handler => \&handle_renew_all,
246 fields => [ (FID_INST_ID), (FID_PATRON_ID), (FID_PATRON_PWD), (FID_TERMINAL_PWD), (FID_FEE_ACK) ],
253 # Now, initialize some of the missing bits of %handlers
255 foreach my $i ( keys(%handlers) ) {
256 if ( !exists( $handlers{$i}->{protocol}->{2} ) ) {
257 $handlers{$i}->{protocol}->{2} = $handlers{$i}->{protocol}->{1};
262 my ( $class, $msg, $seqno ) = @_;
264 my $msgtag = substr( $msg, 0, 2 );
266 if ( $msgtag eq LOGIN ) {
268 # If the client is using the 2.00-style "Login" message
269 # to authenticate to the server, then we get the Login message
270 # _before_ the client has indicated that it supports 2.00, but
271 # it's using the 2.00 login process, so it must support 2.00.
272 $protocol_version = 2;
274 siplog( "LOG_DEBUG", "Sip::MsgType::new('%s', '%s...', '%s'): seq.no '%s', protocol %s", $class, substr( $msg, 0, 10 ), $msgtag, $seqno, $protocol_version );
276 # warn "SIP PROTOCOL: $protocol_version";
277 if ( !exists( $handlers{$msgtag} ) ) {
278 siplog( "LOG_WARNING", "new Sip::MsgType: Skipping message of unknown type '%s' in '%s'", $msgtag, $msg );
280 } elsif ( !exists( $handlers{$msgtag}->{protocol}->{$protocol_version} ) ) {
281 siplog( "LOG_WARNING", "new Sip::MsgType: Skipping message '%s' unsupported by protocol rev. '%d'", $msgtag, $protocol_version );
287 $self->{seqno} = $seqno;
288 $self->_initialize( substr( $msg, 2 ), $handlers{$msgtag} );
294 my ( $self, $msg, $control_block ) = @_;
296 my $proto = $control_block->{protocol}->{$protocol_version};
298 $self->{name} = $control_block->{name};
299 $self->{handler} = $control_block->{handler};
301 $self->{fields} = {};
302 $self->{fixed_fields} = [];
304 chomp($msg); # These four are probably unnecessary now.
309 foreach my $field ( @{ $proto->{fields} } ) {
310 $self->{fields}->{$field} = undef;
313 siplog( "LOG_DEBUG", "Sip::MsgType::_initialize('%s', '%s', '%s', '%s', ...)", $self->{name}, $msg, $proto->{template}, $proto->{template_len} );
315 $self->{fixed_fields} = [ unpack( $proto->{template}, $msg ) ]; # see http://perldoc.perl.org/5.8.8/functions/unpack.html
317 # Skip over the fixed fields and the split the rest of
318 # the message into fields based on the delimiter and parse them
319 foreach my $field ( split( quotemeta($field_delimiter), substr( $msg, $proto->{template_len} ) ) ) {
320 $fn = substr( $field, 0, 2 );
322 if ( !exists( $self->{fields}->{$fn} ) ) {
323 siplog( "LOG_WARNING", "Unsupported field '%s' in %s message '%s'", $fn, $self->{name}, $msg );
324 } elsif ( defined( $self->{fields}->{$fn} ) ) {
325 siplog( "LOG_WARNING", "Duplicate field '%s' (previous value '%s') in %s message '%s'", $fn, $self->{fields}->{$fn}, $self->{name}, $msg );
327 $self->{fields}->{$fn} = substr( $field, 2 );
335 my ( $msg, $server, $req ) = @_;
336 my $config = $server->{config};
339 # Set system preference overrides, first global, then account level
340 # Clear overrides from previous message handling first
341 foreach my $key ( keys %ENV ) {
342 delete $ENV{$key} if index($key, 'OVERRIDE_SYSPREF_') > 0;
344 foreach my $key ( keys %{ $config->{'syspref_overrides'} } ) {
345 $ENV{"OVERRIDE_SYSPREF_$key"} = $config->{'syspref_overrides'}->{$key};
347 foreach my $key ( keys %{ $server->{account}->{'syspref_overrides'} } ) {
348 $ENV{"OVERRIDE_SYSPREF_$key"} =
349 $server->{account}->{'syspref_overrides'}->{$key};
353 # What's the field delimiter for variable length fields?
354 # This can't be based on the account, since we need to know
355 # the field delimiter to parse a SIP login message
357 if ( defined( $server->{config}->{delimiter} ) ) {
358 $field_delimiter = $server->{config}->{delimiter};
361 # error detection is active if this is a REQUEST_ACS_RESEND
362 # message with a checksum, or if the message is long enough
363 # and the last nine characters begin with a sequence number
365 if ( $msg eq REQUEST_ACS_RESEND_CKSUM ) {
368 $error_detection = 1;
369 $self = C4::SIP::Sip::MsgType->new( (REQUEST_ACS_RESEND), 0 );
370 } elsif ( ( length($msg) > 11 ) && ( substr( $msg, -9, 2 ) eq "AY" ) ) {
371 $error_detection = 1;
373 if ( !verify_cksum($msg) ) {
374 siplog( "LOG_WARNING", "Checksum failed on message '%s'", $msg );
376 # REQUEST_SC_RESEND with error detection
377 $last_response = REQUEST_SC_RESEND_CKSUM;
378 print("$last_response\r");
379 return REQUEST_ACS_RESEND;
382 # Save the sequence number, then strip off the
383 # error detection data to process the message
384 $self = C4::SIP::Sip::MsgType->new( substr( $msg, 0, -9 ), substr( $msg, -7, 1 ) );
386 } elsif ($error_detection) {
388 # We received a non-ED message when ED is supposed to be active.
389 # Warn about this problem, then process the message anyway.
390 siplog( "LOG_WARNING", "Received message without error detection: '%s'", $msg );
391 $error_detection = 0;
392 $self = C4::SIP::Sip::MsgType->new( $msg, 0 );
394 $self = C4::SIP::Sip::MsgType->new( $msg, 0 );
397 if ( ( substr( $msg, 0, 2 ) ne REQUEST_ACS_RESEND )
399 && ( substr( $msg, 0, 2 ) ne $req ) ) {
400 return substr( $msg, 0, 2 );
402 unless ( $self->{handler} ) {
403 siplog( "LOG_WARNING", "No handler defined for '%s'", $msg );
404 $last_response = REQUEST_SC_RESEND;
405 print("$last_response\r");
406 return REQUEST_ACS_RESEND;
408 return ( $self->{handler}->( $self, $server ) ); # FIXME
409 # FIXME: Use of uninitialized value in subroutine entry
410 # Can't use string ("") as a subroutine ref while "strict refs" in use
418 # Patron status messages are produced in response to both
419 # "Request Patron Status" and "Block Patron"
421 # Request Patron Status requires a patron password, but
422 # Block Patron doesn't (since the patron may never have
423 # provided one before attempting some illegal action).
425 # ASSUMPTION: If the patron password field is present in the
426 # message, then it must match, otherwise incomplete patron status
427 # information will be returned to the terminal.
429 sub build_patron_status {
430 my ( $patron, $lang, $fields, $server ) = @_;
432 my $patron_pwd = $fields->{ (FID_PATRON_PWD) };
433 my $resp = (PATRON_STATUS_RESP);
438 $password_rc = $patron->check_password($patron_pwd);
441 $resp .= patron_status_string( $patron, $server );
442 $resp .= $lang . timestamp();
443 if ( defined $server->{account}->{ae_field_template} ) {
444 $resp .= add_field( FID_PERSONAL_NAME, $patron->format( $server->{account}->{ae_field_template}, $server ) );
446 $resp .= add_field( FID_PERSONAL_NAME, $patron->name, $server );
450 # while the patron ID we got from the SC is valid, let's
451 # use the one returned from the ILS, just in case...
452 $resp .= add_field( FID_PATRON_ID, $patron->id, $server );
454 if ( $protocol_version >= 2 ) {
455 $resp .= add_field( FID_VALID_PATRON, 'Y', $server );
457 # Patron password is a required field.
458 $resp .= add_field( FID_VALID_PATRON_PWD, sipbool($password_rc), $server );
459 $resp .= maybe_add( FID_CURRENCY, $patron->currency, $server );
460 $resp .= maybe_add( FID_FEE_AMT, $patron->fee_amount, $server );
463 my $msg = $patron->screen_msg;
464 $msg .= ' -- '. INVALID_PW if $patron_pwd && !$password_rc;
465 $resp .= maybe_add( FID_SCREEN_MSG, $msg, $server );
467 $resp .= maybe_add( FID_SCREEN_MSG, $patron->{branchcode}, $server )
468 if ( $server->{account}->{send_patron_home_library_in_af} );
469 $resp .= maybe_add( FID_PRINT_LINE, $patron->print_line, $server );
471 $resp .= $patron->build_custom_field_string( $server );
472 $resp .= $patron->build_patron_attributes_string( $server );
475 # Invalid patron (cardnumber)
476 # Report that the user has no privs.
478 # no personal name, and is invalid (if we're using 2.00)
479 $resp .= 'YYYY' . ( ' ' x 10 ) . $lang . timestamp();
480 $resp .= add_field( FID_PERSONAL_NAME, '', $server );
482 # the patron ID is invalid, but it's a required field, so
484 $resp .= add_field( FID_PATRON_ID, $fields->{ (FID_PATRON_ID) }, $server );
486 ( $protocol_version >= 2 )
487 and $resp .= add_field( FID_VALID_PATRON, 'N', $server );
489 $resp .= maybe_add( FID_SCREEN_MSG, INVALID_CARD, $server );
492 $resp .= add_field( FID_INST_ID, $fields->{ (FID_INST_ID) }, $server );
496 sub handle_patron_status {
497 my ( $self, $server ) = @_;
498 my $ils = $server->{ils};
500 my $resp = (PATRON_STATUS_RESP);
501 my $account = $server->{account};
502 my ( $lang, $date ) = @{ $self->{fixed_fields} };
503 my $fields = $self->{fields};
505 $ils->check_inst_id( $fields->{ (FID_INST_ID) }, "handle_patron_status" );
506 $patron = $ils->find_patron( $fields->{ (FID_PATRON_ID) } );
507 if ( C4::Context->preference('TrackLastPatronActivityTriggers') ) {
508 my $koha_patron = Koha::Patrons->find($patron->{borrowernumber});
509 $koha_patron->update_lastseen('connection');
511 $resp = build_patron_status( $patron, $lang, $fields, $server );
512 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
513 return (PATRON_STATUS_REQ);
516 sub handle_checkout {
517 my ( $self, $server ) = @_;
518 my $account = $server->{account};
519 my $ils = $server->{ils};
520 my $inst = $ils->institution;
521 my ( $sc_renewal_policy, $no_block, $trans_date, $nb_due_date );
523 my ( $patron_id, $item_id, $status );
524 my ( $item, $patron );
527 ( $sc_renewal_policy, $no_block, $trans_date, $nb_due_date ) = @{ $self->{fixed_fields} };
528 $fields = $self->{fields};
530 $patron_id = $fields->{ (FID_PATRON_ID) };
531 Koha::Plugins->call('patron_barcode_transform', \$patron_id );
532 $item_id = $fields->{ (FID_ITEM_ID) };
533 my $fee_ack = $fields->{ (FID_FEE_ACK) };
535 if ( $no_block eq 'Y' ) {
537 # Off-line transactions need to be recorded, but there's
538 # not a lot we can do about it
539 siplog( "LOG_WARNING", "received no-block checkout from terminal '%s'", $account->{id} );
541 $status = $ils->checkout( $patron_id, $item_id, $sc_renewal_policy, $fee_ack, $account, $nb_due_date );
544 # Does the transaction date really matter for items that are
545 # checkout out while the terminal is online? I'm guessing 'no'
546 $status = $ils->checkout( $patron_id, $item_id, $sc_renewal_policy, $fee_ack, $account );
549 $item = $status->item;
550 $patron = $status->patron;
554 # Item successfully checked out
556 $resp = CHECKOUT_RESP . '1';
557 $resp .= sipbool( $status->renew_ok );
558 if ( $ils->supports('magnetic media') ) {
559 $resp .= sipbool( $item->magnetic_media );
564 # We never return the obsolete 'U' value for 'desensitize'
577 # Now for the variable fields
578 $resp .= add_field( FID_INST_ID, $inst, $server );
579 $resp .= add_field( FID_PATRON_ID, $patron_id, $server );
580 $resp .= add_field( FID_ITEM_ID, $item_id, $server );
581 $resp .= add_field( FID_TITLE_ID, $item->title_id, $server );
582 if ( $item->due_date ) {
584 $account->{format_due_date}
585 ? output_pref( { str => $item->due_date, as_due_date => 1 } )
586 : timestamp( $item->due_date );
587 $resp .= add_field( FID_DUE_DATE, $due_date, $server );
589 $resp .= add_field( FID_DUE_DATE, q{}, $server );
592 $resp .= maybe_add( FID_SCREEN_MSG, $status->screen_msg, $server );
593 $resp .= maybe_add( FID_PRINT_LINE, $status->print_line, $server );
595 if ( $protocol_version >= 2 ) {
596 if ( $ils->supports('security inhibit') ) {
597 $resp .= add_field( FID_SECURITY_INHIBIT, $status->security_inhibit, $server );
599 $resp .= maybe_add( FID_MEDIA_TYPE, $item->sip_media_type, $server );
600 $resp .= maybe_add( FID_ITEM_PROPS, $item->sip_item_properties, $server );
608 # Checkout Response: not ok, no renewal, don't know mag. media,
610 $resp = sprintf( "120NUN%s", timestamp );
611 $resp .= add_field( FID_INST_ID, $inst, $server );
612 $resp .= add_field( FID_PATRON_ID, $patron_id, $server );
613 $resp .= add_field( FID_ITEM_ID, $item_id, $server );
615 # If the item is valid, provide the title, otherwise
617 $resp .= add_field( FID_TITLE_ID, $item ? $item->title_id : '', $server );
619 # Due date is required. Since it didn't get checked out,
620 # it's not due, so leave the date blank
621 $resp .= add_field( FID_DUE_DATE, '', $server );
623 $resp .= maybe_add( FID_SCREEN_MSG, $status->screen_msg, $server );
624 $resp .= maybe_add( FID_PRINT_LINE, $status->print_line, $server );
626 if ( $protocol_version >= 2 ) {
628 # Is the patron ID valid?
629 $resp .= add_field( FID_VALID_PATRON, sipbool($patron), $server );
631 if ( $patron && exists( $fields->{FID_PATRON_PWD} ) ) {
633 # Password provided, so we can tell if it was valid or not
634 $resp .= add_field( FID_VALID_PATRON_PWD, sipbool( $patron->check_password( $fields->{ (FID_PATRON_PWD) } ) ), $server );
639 $resp .= $item->build_additional_item_fields_string( $server ) if $item;
641 if ( $protocol_version >= 2 ) {
643 # Financials : return irrespective of ok status
644 if ( $status->fee_amount ) {
645 $resp .= add_field( FID_FEE_AMT, $status->fee_amount, $server );
646 $resp .= maybe_add( FID_CURRENCY, $status->sip_currency, $server );
647 $resp .= maybe_add( FID_FEE_TYPE, $status->sip_fee_type, $server );
648 $resp .= maybe_add( FID_TRANSACTION_ID, $status->transaction_id, $server );
652 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
657 my ( $self, $server ) = @_;
658 my $account = $server->{account};
659 my $ils = $server->{ils};
660 my $my_branch = $ils->institution;
661 my ( $current_loc, $inst_id, $item_id, $terminal_pwd, $item_props, $cancel );
662 my ( $patron, $item, $status );
663 my $resp = CHECKIN_RESP;
664 my ( $no_block, $trans_date, $return_date ) = @{ $self->{fixed_fields} };
665 my $fields = $self->{fields};
667 $current_loc = $fields->{ (FID_CURRENT_LOCN) };
668 $inst_id = $fields->{ (FID_INST_ID) };
669 $item_id = $fields->{ (FID_ITEM_ID) };
670 $item_props = $fields->{ (FID_ITEM_PROPS) };
671 $cancel = $fields->{ (FID_CANCEL) };
673 $my_branch = $current_loc; # most scm do not set $current_loc
676 $ils->check_inst_id( $inst_id, "handle_checkin" );
678 if ( $no_block eq 'Y' ) {
680 # Off-line transactions, ick.
681 siplog( "LOG_WARNING", "received no-block checkin from terminal '%s' - no-block checkin not supported", $account->{id} );
682 #FIXME We need to write the routine called below
683 #$status = $ils->checkin_no_block( $item_id, $trans_date, $return_date, $item_props, $cancel );
684 #Until we do, lets just checkin the item
685 $status = $ils->checkin( $item_id, $trans_date, $return_date, $my_branch, $item_props, $cancel, $account );
687 $status = $ils->checkin( $item_id, $trans_date, $return_date, $my_branch, $item_props, $cancel, $account );
690 $patron = $status->patron;
691 $item = $status->item;
693 $resp .= $status->ok ? '1' : '0';
694 $resp .= $status->resensitize ? 'Y' : 'N';
695 if ( $item && $ils->supports('magnetic media') ) {
696 $resp .= sipbool( $item->magnetic_media );
699 # item barcode is invalid or system doesn't support 'magnetic media' indicator
703 $resp .= $status->alert ? 'Y' : 'N';
705 $resp .= add_field( FID_INST_ID, $inst_id, $server );
706 $resp .= add_field( FID_ITEM_ID, $item_id, $server );
709 $resp .= add_field( FID_PERM_LOCN, $item->permanent_location, $server );
710 $resp .= maybe_add( FID_TITLE_ID, $item->title_id, $server );
711 $resp .= $item->build_additional_item_fields_string( $server );
713 $resp .= add_field( FID_PERM_LOCN, "", $server );
716 if ( $protocol_version >= 2 ) {
717 $resp .= maybe_add( FID_SORT_BIN, $status->sort_bin, $server );
719 $resp .= add_field( FID_PATRON_ID, $patron->id, $server );
722 $resp .= maybe_add( FID_MEDIA_TYPE, $item->sip_media_type, $server );
723 $resp .= maybe_add( FID_ITEM_PROPS, $item->sip_item_properties, $server );
724 $resp .= maybe_add( FID_CALL_NUMBER, $item->call_number, $server );
725 $resp .= maybe_add( FID_HOLD_PATRON_ID, $item->hold_patron_bcode, $server );
726 $resp .= add_field( FID_DESTINATION_LOCATION, $item->destination_loc, $server ) if ( $item->destination_loc || $server->{account}->{ct_always_send} );
727 $resp .= maybe_add( FID_HOLD_PATRON_NAME, $item->hold_patron_name( $server->{account}->{da_field_template} ), $server );
729 if ( my $CR = $server->{account}->{cr_item_field} ) {
730 $resp .= maybe_add( FID_COLLECTION_CODE, $item->{$CR}, $server );
732 $resp .= maybe_add( FID_COLLECTION_CODE, $item->collection_code, $server );
735 if ( $status->hold and $status->hold->{branchcode} ne $item->destination_loc ) {
736 warn 'SIP hold mismatch: $status->hold->{branchcode}=' . $status->hold->{branchcode} . '; $item->destination_loc=' . $item->destination_loc;
738 # just me being paranoid.
743 if ( $status->alert && $status->alert_type ) {
744 $resp .= maybe_add( FID_ALERT_TYPE, $status->alert_type, $server );
745 } elsif ( $server->{account}->{cv_send_00_on_success} ) {
746 $resp .= add_field( FID_ALERT_TYPE, '00', $server );
748 $resp .= maybe_add( FID_SCREEN_MSG, $status->screen_msg, $server );
749 $resp .= maybe_add( FID_PRINT_LINE, $status->print_line, $server );
751 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
756 sub handle_block_patron {
757 my ( $self, $server ) = @_;
758 my $account = $server->{account};
759 my $ils = $server->{ils};
760 my ( $card_retained, $trans_date );
761 my ( $inst_id, $blocked_card_msg, $patron_id, $terminal_pwd );
762 my ( $fields, $resp, $patron );
764 ( $card_retained, $trans_date ) = @{ $self->{fixed_fields} };
765 $fields = $self->{fields};
766 $inst_id = $fields->{ (FID_INST_ID) };
767 $blocked_card_msg = $fields->{ (FID_BLOCKED_CARD_MSG) };
768 $patron_id = $fields->{ (FID_PATRON_ID) };
769 $terminal_pwd = $fields->{ (FID_TERMINAL_PWD) };
771 Koha::Plugins->call('patron_barcode_transform', \$patron_id );
773 # Terminal passwords are different from account login
774 # passwords, but I have no idea what to do with them. So,
775 # I'll just ignore them for now.
779 $ils->check_inst_id( $inst_id, "block_patron" );
780 $patron = $ils->find_patron($patron_id);
782 # The correct response for a "Block Patron" message is a
783 # "Patron Status Response", so use that handler to generate
784 # the message, but then return the correct code from here.
786 # Normally, the language is provided by the "Patron Status"
787 # fixed field, but since we're not responding to one of those
788 # we'll just say, "Unspecified", as per the spec. Let the
789 # terminal default to something that, one hopes, will be
794 $patron->block( $card_retained, $blocked_card_msg );
797 $resp = build_patron_status( $patron, $patron->language, $fields, $server );
798 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
799 return (BLOCK_PATRON);
802 sub handle_sc_status {
803 my ( $self, $server ) = @_;
804 ($server) or warn "handle_sc_status error: no \$server argument received.";
805 my ( $status, $print_width, $sc_protocol_version ) = @{ $self->{fixed_fields} };
808 if ( $sc_protocol_version =~ /^1\./ ) {
810 } elsif ( $sc_protocol_version =~ /^2\./ ) {
813 siplog( "LOG_WARNING", "Unrecognized protocol revision '%s', falling back to '1'", $sc_protocol_version );
817 if ( $new_proto != $protocol_version ) {
818 siplog( "LOG_INFO", "Setting protocol level to $new_proto" );
819 $protocol_version = $new_proto;
822 if ( $status == SC_STATUS_PAPER ) {
823 siplog( "LOG_WARNING", "Self-Check unit '%s@%s' out of paper", $self->{account}->{id}, $self->{account}->{institution} );
824 } elsif ( $status == SC_STATUS_SHUTDOWN ) {
825 siplog( "LOG_WARNING", "Self-Check unit '%s@%s' shutting down", $self->{account}->{id}, $self->{account}->{institution} );
828 $self->{account}->{print_width} = $print_width;
829 return ( send_acs_status( $self, $server ) ? SC_STATUS : '' );
832 sub handle_request_acs_resend {
833 my ( $self, $server ) = @_;
835 if ( !$last_response ) {
837 # We haven't sent anything yet, so respond with a
838 # REQUEST_SC_RESEND msg (p. 16)
839 $self->write_msg( REQUEST_SC_RESEND, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
840 } elsif ( ( length($last_response) < 9 )
841 || substr( $last_response, -9, 2 ) ne 'AY' ) {
843 # When resending a message, we aren't supposed to include
844 # a sequence number, even if the original had one (p. 4).
845 # If the last message didn't have a sequence number, then
846 # we can just send it.
847 print("$last_response\r"); # not write_msg?
850 # Cut out the sequence number and checksum, since the old
851 # checksum is wrong for the resent message.
852 my $rebuilt = substr( $last_response, 0, -9 );
853 $self->write_msg( $rebuilt, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
856 return REQUEST_ACS_RESEND;
860 my $server = shift or return;
863 my $status = 1; # Assume it all works
864 if ( !exists( $server->{config}->{accounts}->{$uid} ) ) {
865 siplog( "LOG_WARNING", "MsgType::login_core: Unknown login '$uid'" );
867 } elsif ( $server->{config}->{accounts}->{$uid}->{password} ne $pwd ) {
868 siplog( "LOG_WARNING", "MsgType::login_core: Invalid password for login '$uid'" );
872 # Store the active account someplace handy for everybody else to find.
873 $server->{account} = $server->{config}->{accounts}->{$uid};
874 my $inst = $server->{account}->{institution};
875 $server->{institution} = $server->{config}->{institutions}->{$inst};
876 $server->{policy} = $server->{institution}->{policy};
877 $server->{sip_username} = $uid;
878 $server->{sip_password} = $pwd;
880 my $auth_status = api_auth( $uid, $pwd, $inst );
881 if ( !$auth_status or $auth_status !~ /^ok$/i ) {
882 siplog( "LOG_WARNING", "api_auth failed for SIP terminal '%s' of '%s': %s", $uid, $inst, ( $auth_status || 'unknown' ) );
885 siplog( "LOG_INFO", "Successful login/auth for '%s' of '%s'", $server->{account}->{id}, $inst );
888 # initialize connection to ILS
890 my $module = $server->{config}->{institutions}->{$inst}->{implementation};
891 siplog( "LOG_DEBUG", 'login_core: ' . Dumper($module) );
893 # Suspect this is always ILS but so we don't break any eccentic install (for now)
894 if ( $module eq 'ILS' ) {
895 $module = 'C4::SIP::ILS';
899 siplog( "LOG_ERR", "%s: Loading ILS implementation '%s' for institution '%s' failed", $server->{service}, $module, $inst );
900 die("Failed to load ILS implementation '$module' for $inst");
903 # like ILS->new(), I think.
904 $server->{ils} = $module->new( $server->{institution}, $server->{account} );
905 if ( !$server->{ils} ) {
906 siplog( "LOG_ERR", "%s: ILS connection to '%s' failed", $server->{service}, $inst );
907 die("Unable to connect to ILS '$inst'");
915 my ( $self, $server ) = @_;
916 my ( $uid_algorithm, $pwd_algorithm );
920 my $status = 1; # Assume it all works
922 $fields = $self->{fields};
923 ( $uid_algorithm, $pwd_algorithm ) = @{ $self->{fixed_fields} };
925 $uid = $fields->{ (FID_LOGIN_UID) }; # Terminal ID, not patron ID.
926 $pwd = $fields->{ (FID_LOGIN_PWD) }; # Terminal PWD, not patron PWD.
928 if ( $uid_algorithm || $pwd_algorithm ) {
929 siplog( "LOG_ERR", "LOGIN: Unsupported non-zero encryption method(s): uid = $uid_algorithm, pwd = $pwd_algorithm" );
932 $status = login_core( $server, $uid, $pwd );
935 $self->write_msg( LOGIN_RESP . $status, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
936 return $status ? LOGIN : '';
940 # Build the detailed summary information for the Patron
941 # Information Response message based on the first 'Y' that appears
942 # in the 'summary' field of the Patron Information request. The
943 # specification says that only one 'Y' can appear in that field,
944 # and we're going to believe it.
947 my ( $ils, $patron, $summary, $start, $end, $server ) = @_;
951 # Map from offsets in the "summary" field of the Patron Information
952 # message to the corresponding field and handler
955 { func => $patron->can("hold_items"), fid => FID_HOLD_ITEMS },
956 { func => $patron->can("overdue_items"), fid => FID_OVERDUE_ITEMS },
957 { func => $patron->can("charged_items"), fid => FID_CHARGED_ITEMS },
958 { func => $patron->can("fine_items"), fid => FID_FINE_ITEMS },
959 { func => $patron->can("recall_items"), fid => FID_RECALL_ITEMS },
960 { func => $patron->can("unavail_holds"), fid => FID_UNAVAILABLE_HOLD_ITEMS },
963 my $summary_type = index( $summary, 'Y' );
964 return q{} if $summary_type == -1; # No detailed information required.
965 return q{} if $summary_type > 5; # Positions 6-9 are not defined in the sip spec,
966 # and we have no extensions to handle them.
968 siplog( "LOG_DEBUG", "Summary_info: index == '%d', field '%s'", $summary_type, $summary_map[$summary_type]->{fid} );
970 my $func = $summary_map[$summary_type]->{func};
971 my $fid = $summary_map[$summary_type]->{fid};
972 my $itemlist = &$func( $patron, $start, $end, $server );
974 siplog( "LOG_DEBUG", "summary_info: list = (%s)", join( ", ", map{ $_->{barcode} } @{$itemlist} ) );
975 foreach my $i ( @{$itemlist} ) {
976 $resp .= add_field( $fid, $i->{barcode}, $server );
982 sub handle_patron_info {
983 my ( $self, $server ) = @_;
984 my $ils = $server->{ils};
985 my ( $lang, $trans_date, $summary ) = @{ $self->{fixed_fields} };
986 my $fields = $self->{fields};
987 my ( $inst_id, $patron_id, $terminal_pwd, $patron_pwd, $start, $end );
988 my ( $resp, $patron );
990 $inst_id = $fields->{ (FID_INST_ID) };
991 $patron_id = $fields->{ (FID_PATRON_ID) };
992 $terminal_pwd = $fields->{ (FID_TERMINAL_PWD) };
993 $patron_pwd = $fields->{ (FID_PATRON_PWD) };
994 $start = $fields->{ (FID_START_ITEM) };
995 $end = $fields->{ (FID_END_ITEM) };
997 Koha::Plugins->call('patron_barcode_transform', \$patron_id );
999 $patron = $ils->find_patron($patron_id);
1001 $resp = (PATRON_INFO_RESP);
1003 if ( C4::Context->preference('TrackLastPatronActivityTriggers') ) {
1004 my $koha_patron = Koha::Patrons->find($patron->{borrowernumber});
1005 $koha_patron->update_lastseen('connection');
1007 $resp .= patron_status_string( $patron, $server );
1008 $resp .= ( defined($lang) and length($lang) == 3 ) ? $lang : $patron->language;
1009 $resp .= timestamp();
1011 $resp .= add_count( 'patron_info/hold_items', scalar @{ $patron->hold_items } );
1012 $resp .= add_count( 'patron_info/overdue_items', scalar @{ $patron->overdue_items } );
1013 $resp .= add_count( 'patron_info/charged_items', scalar @{ $patron->charged_items } );
1014 $resp .= add_count( 'patron_info/fine_items', scalar @{ $patron->fine_items } );
1015 $resp .= add_count( 'patron_info/recall_items', scalar @{ $patron->recall_items } );
1016 $resp .= add_count( 'patron_info/unavail_holds', scalar @{ $patron->unavail_holds } );
1018 $resp .= add_field( FID_INST_ID, ( $ils->institution_id || 'SIP2' ), $server );
1020 # while the patron ID we got from the SC is valid, let's
1021 # use the one returned from the ILS, just in case...
1022 $resp .= add_field( FID_PATRON_ID, $patron->id, $server );
1023 if ( defined $server->{account}->{ae_field_template} ) {
1024 $resp .= add_field( FID_PERSONAL_NAME, $patron->format( $server->{account}->{ae_field_template} ), $server );
1026 $resp .= add_field( FID_PERSONAL_NAME, $patron->name, $server );
1029 # TODO: add code for the fields
1031 # overdue items limit
1032 # charged items limit
1034 $resp .= add_field( FID_VALID_PATRON, 'Y', $server );
1036 if ( defined($patron_pwd) ) {
1038 # If patron password was provided, report whether it was right or not.
1039 if ( $patron_pwd eq q{} && $server->{account}->{allow_empty_passwords} ) {
1042 $password_rc = $patron->check_password($patron_pwd);
1044 $resp .= add_field( FID_VALID_PATRON_PWD, sipbool( $password_rc ), $server );
1047 $resp .= maybe_add( FID_CURRENCY, $patron->currency, $server );
1048 $resp .= maybe_add( FID_FEE_AMT, $patron->fee_amount, $server );
1049 $resp .= add_field( FID_FEE_LMT, $patron->fee_limit, $server );
1051 # TODO: zero or more item details for 2.0 can go here:
1058 $resp .= summary_info( $ils, $patron, $summary, $start, $end, $server );
1060 $resp .= maybe_add( FID_HOME_ADDR, $patron->address, $server );
1061 $resp .= maybe_add( FID_EMAIL, $patron->email_addr, $server );
1062 $resp .= maybe_add( FID_HOME_PHONE, $patron->home_phone, $server );
1064 # SIP 2.0 extensions used by Envisionware
1065 # Other terminals will ignore unrecognized fields (unrecognized field identifiers)
1066 $resp .= maybe_add( FID_PATRON_BIRTHDATE, $patron->birthdate, $server );
1067 $resp .= maybe_add( FID_PATRON_CLASS, $patron->ptype, $server );
1069 # Custom protocol extension to report patron internet privileges
1070 $resp .= maybe_add( FID_INET_PROFILE, $patron->inet_privileges, $server );
1072 my $msg = $patron->screen_msg;
1073 if( defined( $patron_pwd ) && !$password_rc ) {
1074 $msg .= ' -- ' . INVALID_PW;
1076 $resp .= maybe_add( FID_SCREEN_MSG, $msg, $server );
1077 if ( $server->{account}->{send_patron_home_library_in_af} ) {
1078 $resp .= maybe_add( FID_SCREEN_MSG, $patron->{branchcode}, $server);
1080 $resp .= maybe_add( FID_PRINT_LINE, $patron->print_line, $server );
1082 $resp .= $patron->build_custom_field_string( $server );
1083 $resp .= $patron->build_patron_attributes_string( $server );
1086 # Invalid patron ID:
1087 # no privileges, no items associated,
1088 # no personal name, and is invalid (if we're using 2.00)
1089 $resp .= 'YYYY' . ( ' ' x 10 ) . $lang . timestamp();
1090 $resp .= '0000' x 6;
1092 $resp .= add_field( FID_INST_ID, ( $ils->institution_id || 'SIP2' ), $server );
1094 # patron ID is invalid, but field is required, so just echo it back
1095 $resp .= add_field( FID_PATRON_ID, $fields->{ (FID_PATRON_ID) }, $server );
1096 $resp .= add_field( FID_PERSONAL_NAME, '', $server );
1098 if ( $protocol_version >= 2 ) {
1099 $resp .= add_field( FID_VALID_PATRON, 'N', $server );
1101 $resp .= maybe_add( FID_SCREEN_MSG, INVALID_CARD, $server );
1104 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1105 return (PATRON_INFO);
1108 sub handle_end_patron_session {
1109 my ( $self, $server ) = @_;
1110 my $ils = $server->{ils};
1112 my $fields = $self->{fields};
1113 my $resp = END_SESSION_RESP;
1114 my ( $status, $screen_msg, $print_line );
1116 ($trans_date) = @{ $self->{fixed_fields} };
1118 $ils->check_inst_id( $fields->{ (FID_INST_ID) }, 'handle_end_patron_session' );
1120 ( $status, $screen_msg, $print_line ) = $ils->end_patron_session( $fields->{ (FID_PATRON_ID) } );
1122 $resp .= $status ? 'Y' : 'N';
1123 $resp .= timestamp();
1125 $resp .= add_field( FID_INST_ID, $server->{ils}->institution, $server );
1126 $resp .= add_field( FID_PATRON_ID, $fields->{ (FID_PATRON_ID) }, $server );
1128 $resp .= maybe_add( FID_SCREEN_MSG, $screen_msg, $server );
1129 $resp .= maybe_add( FID_PRINT_LINE, $print_line, $server );
1131 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1133 return (END_PATRON_SESSION);
1136 sub handle_fee_paid {
1137 my ( $self, $server ) = @_;
1138 my $ils = $server->{ils};
1139 my ( $trans_date, $fee_type, $pay_type, $currency ) = @{ $self->{fixed_fields} };
1140 my $fields = $self->{fields};
1141 my ( $fee_amt, $inst_id, $patron_id, $terminal_pwd, $patron_pwd );
1142 my ( $fee_id, $trans_id );
1144 my $resp = FEE_PAID_RESP;
1146 my $disallow_overpayment = $server->{account}->{disallow_overpayment};
1147 my $payment_type_writeoff = $server->{account}->{payment_type_writeoff} || q{};
1148 my $register_id = $server->{account}->{register_id};
1150 my $is_writeoff = $pay_type eq $payment_type_writeoff;
1152 $fee_amt = $fields->{ (FID_FEE_AMT) };
1153 $inst_id = $fields->{ (FID_INST_ID) };
1154 $patron_id = $fields->{ (FID_PATRON_ID) };
1155 $patron_pwd = $fields->{ (FID_PATRON_PWD) };
1156 $fee_id = $fields->{ (FID_FEE_ID) };
1157 $trans_id = $fields->{ (FID_TRANSACTION_ID) };
1159 Koha::Plugins->call('patron_barcode_transform', \$patron_id );
1161 $ils->check_inst_id( $inst_id, "handle_fee_paid" );
1163 my $pay_result = $ils->pay_fee( $patron_id, $patron_pwd, $fee_amt, $fee_type, $pay_type, $fee_id, $trans_id, $currency, $is_writeoff, $disallow_overpayment, $register_id );
1164 $status = $pay_result->{status};
1165 my $pay_response = $pay_result->{pay_response};
1168 "no_item" => "No matching item could be found",
1169 "no_checkout" => "Item is not checked out",
1170 "too_soon" => "Cannot yet be renewed",
1171 "too_many" => "Renewed the maximum number of times",
1172 "auto_too_soon" => "Scheduled for automatic renewal and cannot yet be renewed",
1173 "auto_too_late" => "Scheduled for automatic renewal and cannot yet be any more",
1174 "auto_account_expired" => "Scheduled for automatic renewal and cannot be renewed because the patron's account has expired",
1175 "auto_renew" => "Scheduled for automatic renewal",
1176 "auto_too_much_oweing" => "Scheduled for automatic renewal",
1177 "on_reserve" => "On hold for another patron",
1178 "patron_restricted" => "Patron is currently restricted",
1179 "item_denied_renewal" => "Item is not allowed renewal",
1180 "onsite_checkout" => "Item is an onsite checkout",
1181 "item_issued_to_other_patron" => "Item already issued to other borrower"
1185 foreach my $result( @{$pay_response->{renew_result}} ) {
1186 my $item = Koha::Items->find({ itemnumber => $result->{itemnumber} });
1187 if ($result->{success}) {
1188 push @success, '"' . $item->biblio->title . '"';
1190 push @fail, '"' . $item->biblio->title . '" : ' . $failmap->{$result->{error}};
1195 if (scalar @success > 0) {
1196 $msg.="The following items were renewed: " . join(", ", @success) . ". ";
1198 if (scalar @fail > 0) {
1199 $msg.="The following items were not renewed: " . join(", ", @fail) . ".";
1201 if (length $msg > 0) {
1202 $status->screen_msg($status->screen_msg . " $msg");
1205 $resp .= ( $status->ok ? 'Y' : 'N' ) . timestamp;
1206 $resp .= add_field( FID_INST_ID, $inst_id, $server );
1207 $resp .= add_field( FID_PATRON_ID, $patron_id, $server );
1208 $resp .= maybe_add( FID_TRANSACTION_ID, $status->transaction_id, $server );
1209 $resp .= maybe_add( FID_SCREEN_MSG, $pay_result->{error}, $server );
1210 $resp .= maybe_add( FID_SCREEN_MSG, $status->screen_msg, $server );
1211 $resp .= maybe_add( FID_PRINT_LINE, $status->print_line, $server );
1213 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1218 sub handle_item_information {
1219 my ( $self, $server ) = @_;
1220 my $account = $server->{account};
1221 my $ils = $server->{ils};
1222 my $fields = $self->{fields};
1223 my $resp = ITEM_INFO_RESP;
1228 ($trans_date) = @{ $self->{fixed_fields} };
1230 $ils->check_inst_id( $fields->{ (FID_INST_ID) }, "handle_item_information" );
1232 $item = $ils->find_item( $fields->{ (FID_ITEM_ID) } );
1234 if ( !defined($item) ) {
1237 # "Other" circ stat, "Other" security marker, "Unknown" fee type
1241 # Just echo back the invalid item id
1242 $resp .= add_field( FID_ITEM_ID, $fields->{ (FID_ITEM_ID) }, $server );
1244 # title id is required, but we don't have one
1245 $resp .= add_field( FID_TITLE_ID, '', $server );
1247 my $seen = $account->{seen_on_item_information};
1248 ModDateLastSeen( $item->itemnumber, $seen eq 'keep_lost' ) if $seen;
1250 # Valid Item ID, send the good stuff
1251 my $circulation_status = $item->sip_circulation_status($server);
1252 $resp .= $circulation_status;
1253 $resp .= $item->sip_security_marker;
1254 $resp .= $item->sip_fee_type;
1257 if ( $circulation_status eq '01' ) {
1258 $resp .= maybe_add( FID_SCREEN_MSG, "Item is damaged", $server );
1261 $resp .= add_field( FID_ITEM_ID, $item->id, $server );
1262 $resp .= add_field( FID_TITLE_ID, $item->title_id, $server );
1264 $resp .= maybe_add( FID_MEDIA_TYPE, $item->sip_media_type, $server );
1265 $resp .= maybe_add( FID_PERM_LOCN, $item->permanent_location, $server );
1266 $resp .= maybe_add( FID_CURRENT_LOCN, $item->current_location, $server );
1267 $resp .= maybe_add( FID_ITEM_PROPS, $item->sip_item_properties, $server );
1270 if ( my $CR = $server->{account}->{cr_item_field} ) {
1271 $resp .= maybe_add( FID_COLLECTION_CODE, $item->{$CR}, $server );
1273 $resp .= maybe_add( FID_COLLECTION_CODE, $item->collection_code, $server );
1276 if ( ( $i = $item->fee ) != 0 ) {
1277 $resp .= add_field( FID_CURRENCY, $item->fee_currency, $server );
1278 $resp .= add_field( FID_FEE_AMT, $i, $server );
1280 $resp .= maybe_add( FID_OWNER, $item->owner, $server );
1282 if ( ( $i = scalar @{ $item->hold_queue } ) > 0 ) {
1283 $resp .= add_field( FID_HOLD_QUEUE_LEN, $i, $server );
1285 if ( $item->due_date ) {
1287 $account->{format_due_date}
1288 ? output_pref( { str => $item->due_date, as_due_date => 1 } )
1289 : timestamp( $item->due_date );
1290 $resp .= add_field( FID_DUE_DATE, $due_date, $server );
1292 if ( ( $i = $item->recall_date ) != 0 ) {
1293 $resp .= add_field( FID_RECALL_DATE, timestamp($i), $server );
1295 if ( ( $i = $item->hold_pickup_date ) != 0 ) {
1296 $resp .= add_field( FID_HOLD_PICKUP_DATE, timestamp($i), $server );
1299 $resp .= maybe_add( FID_SCREEN_MSG, $item->screen_msg, $server );
1300 $resp .= maybe_add( FID_PRINT_LINE, $item->print_line, $server );
1302 $resp .= $item->build_additional_item_fields_string( $server );
1303 $resp .= $item->build_custom_field_string( $server );
1306 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1308 return (ITEM_INFORMATION);
1311 sub handle_item_status_update {
1312 my ( $self, $server ) = @_;
1313 my $ils = $server->{ils};
1314 my ( $trans_date, $item_id, $terminal_pwd, $item_props );
1315 my $fields = $self->{fields};
1318 my $resp = ITEM_STATUS_UPDATE_RESP;
1320 ($trans_date) = @{ $self->{fixed_fields} };
1322 $ils->check_inst_id( $fields->{ (FID_INST_ID) } );
1324 $item_id = $fields->{ (FID_ITEM_ID) };
1325 $item_props = $fields->{ (FID_ITEM_PROPS) };
1327 if ( !defined($item_id) ) {
1328 siplog( "LOG_WARNING", "handle_item_status: received message without Item ID field" );
1330 $item = $ils->find_item($item_id);
1338 $resp .= add_field( FID_ITEM_ID, $item_id, $server );
1343 $status = $item->status_update($item_props);
1345 $resp .= $status->ok ? '1' : '0';
1348 $resp .= add_field( FID_ITEM_ID, $item->id, $server );
1349 $resp .= add_field( FID_TITLE_ID, $item->title_id, $server );
1350 $resp .= maybe_add( FID_ITEM_PROPS, $item->sip_item_properties, $server );
1353 $resp .= maybe_add( FID_SCREEN_MSG, $status->screen_msg, $server );
1354 $resp .= maybe_add( FID_PRINT_LINE, $status->print_line, $server );
1356 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1358 return (ITEM_STATUS_UPDATE);
1361 sub handle_patron_enable {
1362 my ( $self, $server ) = @_;
1363 my $ils = $server->{ils};
1364 my $fields = $self->{fields};
1365 my ( $trans_date, $patron_id, $terminal_pwd, $patron_pwd );
1366 my ( $status, $patron );
1367 my $resp = PATRON_ENABLE_RESP;
1369 ($trans_date) = @{ $self->{fixed_fields} };
1370 $patron_id = $fields->{ (FID_PATRON_ID) };
1371 $patron_pwd = $fields->{ (FID_PATRON_PWD) };
1373 Koha::Plugins->call('patron_barcode_transform', \$patron_id );
1375 siplog( "LOG_DEBUG", "handle_patron_enable: patron_id: '%s', patron_pwd: '%s'", $patron_id, $patron_pwd );
1377 $patron = $ils->find_patron($patron_id);
1379 if ( !defined($patron) ) {
1382 $resp .= 'YYYY' . ( ' ' x 10 ) . '000' . timestamp();
1383 $resp .= add_field( FID_PATRON_ID, $patron_id, $server );
1384 $resp .= add_field( FID_PERSONAL_NAME, '', $server );
1385 $resp .= add_field( FID_VALID_PATRON, 'N', $server );
1386 $resp .= add_field( FID_VALID_PATRON_PWD, 'N', $server );
1390 if ( !defined($patron_pwd) || $patron->check_password($patron_pwd) ) {
1392 # Don't enable the patron if there was an invalid password
1393 $status = $patron->enable;
1395 $resp .= patron_status_string( $patron, $server );
1396 $resp .= $patron->language . timestamp();
1398 $resp .= add_field( FID_PATRON_ID, $patron->id, $server );
1399 $resp .= add_field( FID_PERSONAL_NAME, $patron->format( $server->{account}->{ae_field_template} ), $server );
1400 if ( defined($patron_pwd) ) {
1401 $resp .= add_field( FID_VALID_PATRON_PWD, sipbool( $patron->check_password($patron_pwd) ), $server );
1403 $resp .= add_field( FID_VALID_PATRON, 'Y', $server );
1404 $resp .= maybe_add( FID_SCREEN_MSG, $patron->screen_msg, $server );
1405 $resp .= maybe_add( FID_PRINT_LINE, $patron->print_line, $server );
1408 $resp .= add_field( FID_INST_ID, $ils->institution, $server );
1410 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1412 return (PATRON_ENABLE);
1416 my ( $self, $server ) = @_;
1417 my $ils = $server->{ils};
1418 my ( $hold_mode, $trans_date );
1419 my ( $expiry_date, $pickup_locn, $hold_type, $patron_id, $patron_pwd );
1420 my ( $item_id, $title_id, $fee_ack );
1421 my $fields = $self->{fields};
1423 my $resp = HOLD_RESP;
1425 ( $hold_mode, $trans_date ) = @{ $self->{fixed_fields} };
1427 $ils->check_inst_id( $fields->{ (FID_INST_ID) }, "handle_hold" );
1429 $patron_id = $fields->{ (FID_PATRON_ID) };
1430 $expiry_date = $fields->{ (FID_EXPIRATION) } || '';
1431 $pickup_locn = $fields->{ (FID_PICKUP_LOCN) } || '';
1432 $hold_type = $fields->{ (FID_HOLD_TYPE) } || '2'; # Any copy of title
1433 $patron_pwd = $fields->{ (FID_PATRON_PWD) };
1434 $item_id = $fields->{ (FID_ITEM_ID) } || '';
1435 $title_id = $fields->{ (FID_TITLE_ID) } || '';
1436 $fee_ack = $fields->{ (FID_FEE_ACK) } || 'N';
1438 Koha::Plugins->call('patron_barcode_transform', \$patron_id );
1440 if ( $hold_mode eq '+' ) {
1441 $status = $ils->add_hold( $patron_id, $patron_pwd, $item_id, $title_id, $expiry_date, $pickup_locn, $hold_type, $fee_ack );
1442 } elsif ( $hold_mode eq '-' ) {
1443 $status = $ils->cancel_hold( $patron_id, $patron_pwd, $item_id, $title_id );
1444 } elsif ( $hold_mode eq '*' ) {
1445 $status = $ils->alter_hold( $patron_id, $patron_pwd, $item_id, $title_id, $expiry_date, $pickup_locn, $hold_type, $fee_ack );
1447 siplog( "LOG_WARNING", "handle_hold: Unrecognized hold mode '%s' from terminal '%s'", $hold_mode, $server->{account}->{id} );
1448 $status = $ils->Transaction::Hold; # new?
1449 $status->screen_msg("System error. Please contact library staff.");
1452 $resp .= $status->ok;
1453 $resp .= sipbool( $status->item && $status->item->available($patron_id) );
1456 if ( $status->ok ) {
1457 $resp .= add_field( FID_PATRON_ID, $status->patron->id, $server );
1459 ( $status->expiration_date )
1460 and $resp .= maybe_add( FID_EXPIRATION, timestamp( $status->expiration_date ), $server );
1461 $resp .= maybe_add( FID_QUEUE_POS, $status->queue_position, $server );
1462 $resp .= maybe_add( FID_PICKUP_LOCN, $status->pickup_location, $server );
1463 $resp .= maybe_add( FID_ITEM_ID, $status->item->id, $server );
1464 $resp .= maybe_add( FID_TITLE_ID, $status->item->title_id, $server );
1467 # Not ok. still need required fields
1468 $resp .= add_field( FID_PATRON_ID, $patron_id, $server );
1471 $resp .= add_field( FID_INST_ID, $ils->institution, $server );
1472 $resp .= maybe_add( FID_SCREEN_MSG, $status->screen_msg, $server );
1473 $resp .= maybe_add( FID_PRINT_LINE, $status->print_line, $server );
1475 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1481 my ( $self, $server ) = @_;
1482 my $ils = $server->{ils};
1483 my ( $third_party, $no_block, $trans_date, $nb_due_date );
1484 my ( $patron_id, $patron_pwd, $item_id, $title_id, $item_props, $fee_ack );
1485 my $fields = $self->{fields};
1487 my ( $patron, $item );
1488 my $resp = RENEW_RESP;
1490 ( $third_party, $no_block, $trans_date, $nb_due_date ) = @{ $self->{fixed_fields} };
1492 $ils->check_inst_id( $fields->{ (FID_INST_ID) }, "handle_renew" );
1494 if ( $no_block eq 'Y' ) {
1495 siplog( "LOG_WARNING", "handle_renew: received 'no block' renewal from terminal '%s'", $server->{account}->{id} );
1498 $patron_id = $fields->{ (FID_PATRON_ID) };
1499 $patron_pwd = $fields->{ (FID_PATRON_PWD) };
1500 $item_id = $fields->{ (FID_ITEM_ID) };
1501 $title_id = $fields->{ (FID_TITLE_ID) };
1502 $item_props = $fields->{ (FID_ITEM_PROPS) };
1503 $fee_ack = $fields->{ (FID_FEE_ACK) };
1505 Koha::Plugins->call('patron_barcode_transform', \$patron_id );
1507 $status = $ils->renew( $patron_id, $patron_pwd, $item_id, $title_id, $no_block, $nb_due_date, $third_party, $item_props, $fee_ack );
1509 $patron = $status->patron;
1510 $item = $status->item;
1512 if ( $status->renewal_ok ) {
1514 $resp .= $status->renewal_ok ? 'Y' : 'N';
1515 if ( $ils->supports('magnetic media') ) {
1516 $resp .= sipbool( $item->magnetic_media );
1520 $resp .= sipbool( desensitize( { status => $status, patron => $patron, server => $server } ) );
1522 $resp .= add_field( FID_PATRON_ID, $patron->id, $server );
1523 $resp .= add_field( FID_ITEM_ID, $item->id, $server );
1524 $resp .= add_field( FID_TITLE_ID, $item->title_id, $server );
1525 if ( $item->due_date ) {
1526 $resp .= add_field( FID_DUE_DATE, timestamp( $item->due_date ), $server );
1528 $resp .= add_field( FID_DUE_DATE, q{}, $server );
1530 if ( $ils->supports('security inhibit') ) {
1531 $resp .= add_field( FID_SECURITY_INHIBIT, $status->security_inhibit, $server );
1533 $resp .= add_field( FID_MEDIA_TYPE, $item->sip_media_type, $server );
1534 $resp .= maybe_add( FID_ITEM_PROPS, $item->sip_item_properties, $server );
1537 # renew failed for some reason
1538 # not OK, renewal not OK, Unknown media type (why bother checking?)
1542 # If we found the patron or the item, the return the ILS
1543 # information, otherwise echo back the information we received
1545 $resp .= add_field( FID_PATRON_ID, $patron ? $patron->id : $patron_id, $server );
1546 $resp .= add_field( FID_ITEM_ID, $item ? $item->id : $item_id, $server );
1547 $resp .= add_field( FID_TITLE_ID, $item ? $item->title_id : $title_id, $server );
1548 $resp .= add_field( FID_DUE_DATE, '', $server );
1551 if ( $status->fee_amount ) {
1552 $resp .= add_field( FID_FEE_AMT, $status->fee_amount, $server );
1553 $resp .= maybe_add( FID_CURRENCY, $status->sip_currency, $server );
1554 $resp .= maybe_add( FID_FEE_TYPE, $status->sip_fee_type, $server );
1555 $resp .= maybe_add( FID_TRANSACTION_ID, $status->transaction_id, $server );
1558 $resp .= add_field( FID_INST_ID, $ils->institution, $server );
1559 $resp .= maybe_add( FID_SCREEN_MSG, $status->screen_msg, $server );
1560 $resp .= maybe_add( FID_PRINT_LINE, $status->print_line, $server );
1562 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1567 sub handle_renew_all {
1569 # my ($third_party, $no_block, $nb_due_date, $fee_ack, $patron);
1571 my ( $self, $server ) = @_;
1572 my $ils = $server->{ils};
1573 my ( $trans_date, $patron_id, $patron_pwd, $terminal_pwd, $fee_ack );
1574 my $fields = $self->{fields};
1575 my $resp = RENEW_ALL_RESP;
1577 my ( @renewed, @unrenewed );
1579 $ils->check_inst_id( $fields->{ (FID_INST_ID) }, "handle_renew_all" );
1581 ($trans_date) = @{ $self->{fixed_fields} };
1583 $patron_id = $fields->{ (FID_PATRON_ID) };
1584 $patron_pwd = $fields->{ (FID_PATRON_PWD) };
1585 $terminal_pwd = $fields->{ (FID_TERMINAL_PWD) };
1586 $fee_ack = $fields->{ (FID_FEE_ACK) };
1588 Koha::Plugins->call('patron_barcode_transform', \$patron_id );
1590 $status = $ils->renew_all( $patron_id, $patron_pwd, $fee_ack );
1592 $resp .= $status->ok ? '1' : '0';
1594 if ( !$status->ok ) {
1595 $resp .= add_count( "renew_all/renewed_count", 0 );
1596 $resp .= add_count( "renew_all/unrenewed_count", 0 );
1600 @renewed = ( @{ $status->renewed } );
1601 @unrenewed = ( @{ $status->unrenewed } );
1602 $resp .= add_count( "renew_all/renewed_count", scalar @renewed );
1603 $resp .= add_count( "renew_all/unrenewed_count", scalar @unrenewed );
1607 $resp .= add_field( FID_INST_ID, $ils->institution, $server );
1609 $resp .= join( '', map( add_field( FID_RENEWED_ITEMS, $_, $server ), @renewed ) );
1610 $resp .= join( '', map( add_field( FID_UNRENEWED_ITEMS, $_, $server ), @unrenewed ) );
1612 $resp .= maybe_add( FID_SCREEN_MSG, $status->screen_msg, $server );
1613 $resp .= maybe_add( FID_PRINT_LINE, $status->print_line, $server );
1615 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1621 # send_acs_status($self, $server)
1623 # Send an ACS Status message, which is contains lots of little fields
1624 # of information gleaned from all sorts of places.
1627 my @message_type_names = (
1628 "patron status request",
1633 "request sc/acs resend",
1635 "patron information",
1636 "end patron session",
1639 "item status update",
1646 sub send_acs_status {
1647 my ( $self, $server, $screen_msg, $print_line ) = @_;
1649 my $msg = ACS_STATUS;
1650 ($server) or die "send_acs_status error: no \$server argument received";
1651 my $account = $server->{account} or die "send_acs_status error: no 'account' in \$server object:\n" . Dumper($server);
1652 my $policy = $server->{policy} or die "send_acs_status error: no 'policy' in \$server object:\n" . Dumper($server);
1653 my $ils = $server->{ils} or die "send_acs_status error: no 'ils' in \$server object:\n" . Dumper($server);
1654 my $sip_username = $server->{sip_username} or die "send_acs_status error: no 'sip_username' in \$server object:\n" . Dumper($server);
1655 my ( $online_status, $checkin_ok, $checkout_ok, $ACS_renewal_policy );
1656 my ( $status_update_ok, $offline_ok, $timeout, $retries );
1657 my $sip_user = Koha::Patrons->find({ userid => $sip_username });
1658 die "send_acs_status error: sip_username cannot be found in DB or DB cannot be reached" unless $sip_user;
1660 $online_status = 'Y';
1661 $checkout_ok = sipbool( $ils->checkout_ok );
1662 $checkin_ok = sipbool( $ils->checkin_ok );
1663 $ACS_renewal_policy = sipbool( $policy->{renewal} );
1664 $status_update_ok = sipbool( $ils->status_update_ok );
1665 $offline_ok = sipbool( $ils->offline_ok );
1666 $timeout = $server->get_timeout({ policy => 1 });
1667 $retries = sprintf( "%03d", $policy->{retries} );
1669 if ( length($retries) != 3 ) {
1670 siplog( "LOG_ERR", "handle_acs_status: retries field wrong size: '%s'", $retries );
1674 $msg .= "$online_status$checkin_ok$checkout_ok$ACS_renewal_policy";
1675 $msg .= "$status_update_ok$offline_ok$timeout$retries";
1676 $msg .= timestamp();
1678 if ( $protocol_version == 1 ) {
1680 } elsif ( $protocol_version == 2 ) {
1683 siplog( "LOG_ERR", 'Bad setting for $protocol_version, "%s" in send_acs_status', $protocol_version );
1688 $msg .= add_field( FID_INST_ID, $account->{institution}, $server );
1690 if ( $protocol_version >= 2 ) {
1692 # Supported messages: we do it all
1693 my $supported_msgs = '';
1695 foreach my $msg_name (@message_type_names) {
1696 if ( $msg_name eq 'request sc/acs resend' ) {
1697 $supported_msgs .= sipbool(1);
1699 $supported_msgs .= sipbool( $ils->supports($msg_name) );
1702 if ( length($supported_msgs) < 16 ) {
1703 siplog( "LOG_ERR", 'send_acs_status: supported messages "%s" too short', $supported_msgs );
1705 $msg .= add_field( FID_SUPPORTED_MSGS, $supported_msgs, $server );
1708 $msg .= maybe_add( FID_SCREEN_MSG, $screen_msg, $server );
1710 if ( defined( $account->{print_width} )
1711 && defined($print_line)
1712 && $account->{print_width} < length($print_line) ) {
1713 siplog( "LOG_WARNING", "send_acs_status: print line '%s' too long. Truncating", $print_line );
1714 $print_line = substr( $print_line, 0, $account->{print_width} );
1717 $msg .= maybe_add( FID_PRINT_LINE, $print_line, $server );
1719 # Do we want to tell the terminal its location?
1721 $self->write_msg( $msg, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1726 # build_patron_status: create the 14-char patron status
1727 # string for the Patron Status message
1729 sub patron_status_string {
1735 siplog( "LOG_DEBUG", "patron_status_string: %s charge_ok: %s", $patron->id, $patron->charge_ok );
1736 $patron_status = sprintf(
1737 '%s%s%s%s%s%s%s%s%s%s%s%s%s%s',
1738 denied( $patron->charge_ok ),
1739 denied( $patron->renew_ok ),
1740 denied( $patron->recall_ok ),
1741 denied( $patron->hold_ok ),
1742 boolspace( $patron->card_lost ),
1743 boolspace( $patron->too_many_charged ),
1744 $server->{account}->{overdues_block_checkout} ? boolspace( $patron->too_many_overdue ) : q{ },
1745 boolspace( $patron->too_many_renewal ),
1746 boolspace( $patron->too_many_claim_return ),
1747 boolspace( $patron->too_many_lost( $server ) ),
1748 boolspace( $patron->excessive_fines ),
1749 boolspace( $patron->excessive_fees ),
1750 boolspace( $patron->recall_overdue ),
1751 boolspace( $patron->too_many_billed )
1753 return $patron_status;
1757 my ( $username, $password, $branch ) = @_;
1758 $ENV{REMOTE_USER} = $username;
1759 my $query = CGI->new();
1760 $query->param( userid => $username );
1761 $query->param( password => $password );
1763 $query->param( branch => $branch );
1765 my ( $status, $cookie, $sessionID ) = check_api_auth( $query, { circulate => 1 }, 'intranet' );
1772 my $status = $params->{status};
1773 my $desensitize = $status->desensitize();
1775 # If desenstize is already false, no need to do anything
1776 return unless $desensitize;
1778 my $patron = $params->{patron};
1779 my $item = $params->{item};
1780 my $server = $params->{server};
1782 my $patron_categories = $server->{account}->{inhouse_patron_categories} // q{};
1783 my $item_types = $server->{account}->{inhouse_item_types} // q{};
1785 # If no patron categories or item types are set for never desensitize, no need to do anything
1786 return $desensitize unless $patron_categories || $item_types;
1788 my $patron_category = $patron->ptype();
1789 my @patron_categories = split( /,/, $patron_categories );
1790 my $found_patron_category = grep( /^$patron_category$/, @patron_categories );
1791 return 0 if $found_patron_category;
1793 my $item_type = $item->itemtype;
1794 my @item_types = split( /,/, $item_types );
1795 my $found_item_type = grep( /^$item_type$/, @item_types );
1796 return 0 if $found_item_type;