Bug 31552: SIP2 option format_due_date not honored for AH field in item information...
[koha-ffzg.git] / C4 / SIP / Sip / MsgType.pm
index 62d3d5f..cad82e0 100644 (file)
@@ -18,8 +18,11 @@ use Data::Dumper;
 use CGI qw ( -utf8 );
 use C4::Auth qw(&check_api_auth);
 
+use Koha::Patrons;
 use Koha::Patron::Attributes;
+use Koha::Plugins;
 use Koha::Items;
+use Koha::DateUtils qw( output_pref );
 
 use UNIVERSAL::can;
 
@@ -69,12 +72,12 @@ my %handlers = (
         handler  => \&handle_checkout,
         protocol => {
             1 => {
-                template     => "CCA18A18",
+                template     => "A1A1A18A18",
                 template_len => 38,
                 fields       => [ (FID_INST_ID), (FID_PATRON_ID), (FID_ITEM_ID), (FID_TERMINAL_PWD) ],
             },
             2 => {
-                template     => "CCA18A18",
+                template     => "A1A1A18A18",
                 template_len => 38,
                 fields       => [ (FID_INST_ID), (FID_PATRON_ID), (FID_ITEM_ID), (FID_TERMINAL_PWD), (FID_ITEM_PROPS), (FID_PATRON_PWD), (FID_FEE_ACK), (FID_CANCEL) ],
             },
@@ -85,12 +88,12 @@ my %handlers = (
         handler  => \&handle_checkin,
         protocol => {
             1 => {
-                template     => "CA18A18",
+                template     => "A1A18A18",
                 template_len => 37,
                 fields       => [ (FID_CURRENT_LOCN), (FID_INST_ID), (FID_ITEM_ID), (FID_TERMINAL_PWD) ],
             },
             2 => {
-                template     => "CA18A18",
+                template     => "A1A18A18",
                 template_len => 37,
                 fields       => [ (FID_CURRENT_LOCN), (FID_INST_ID), (FID_ITEM_ID), (FID_TERMINAL_PWD), (FID_ITEM_PROPS), (FID_CANCEL) ],
             }
@@ -101,7 +104,7 @@ my %handlers = (
         handler  => \&handle_block_patron,
         protocol => {
             1 => {
-                template     => "CA18",
+                template     => "A1A18",
                 template_len => 19,
                 fields       => [ (FID_INST_ID), (FID_BLOCKED_CARD_MSG), (FID_PATRON_ID), (FID_TERMINAL_PWD) ],
             },
@@ -112,7 +115,7 @@ my %handlers = (
         handler  => \&handle_sc_status,
         protocol => {
             1 => {
-                template     => "CA3A4",
+                template     => "A1A3A4",
                 template_len => 8,
                 fields       => [],
             }
@@ -123,7 +126,7 @@ my %handlers = (
         handler  => \&handle_request_acs_resend,
         protocol => {
             1 => {
-                template     => "",
+                template     => q{},
                 template_len => 0,
                 fields       => [],
             }
@@ -225,7 +228,7 @@ my %handlers = (
         handler  => \&handle_renew,
         protocol => {
             2 => {
-                template     => "CCA18A18",
+                template     => "A1A1A18A18",
                 template_len => 38,
                 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) ],
             }
@@ -333,7 +336,7 @@ sub handle {
 
     # Set system preference overrides, first global, then account level
     # Clear overrides from previous message handling first
-    foreach my $key ( %ENV ) {
+    foreach my $key ( keys %ENV ) {
         delete $ENV{$key} if index($key, 'OVERRIDE_SYSPREF_') > 0;
     }
     foreach my $key ( keys %{ $config->{'syspref_overrides'} } ) {
@@ -519,6 +522,7 @@ sub handle_checkout {
     $fields = $self->{fields};
 
     $patron_id = $fields->{ (FID_PATRON_ID) };
+    Koha::Plugins->call('patron_barcode_transform', \$patron_id );
     $item_id   = $fields->{ (FID_ITEM_ID) };
     my $fee_ack = $fields->{ (FID_FEE_ACK) };
 
@@ -528,7 +532,7 @@ sub handle_checkout {
         # not a lot we can do about it
         siplog( "LOG_WARNING", "received no-block checkout from terminal '%s'", $account->{id} );
 
-        $status = $ils->checkout_no_block( $patron_id, $item_id, $sc_renewal_policy, $trans_date, $nb_due_date );
+        $status = $ils->checkout( $patron_id, $item_id, $sc_renewal_policy, $fee_ack, $account, $nb_due_date );
     } else {
 
         # Does the transaction date really matter for items that are
@@ -552,7 +556,16 @@ sub handle_checkout {
         }
 
         # We never return the obsolete 'U' value for 'desensitize'
-        $resp .= sipbool( $status->desensitize );
+        $resp .= sipbool(
+            desensitize(
+                {
+                    item   => $item,
+                    patron => $patron,
+                    server => $server,
+                    status => $status,
+                }
+            )
+        );
         $resp .= timestamp;
 
         # Now for the variable fields
@@ -561,7 +574,11 @@ sub handle_checkout {
         $resp .= add_field( FID_ITEM_ID,   $item_id, $server );
         $resp .= add_field( FID_TITLE_ID,  $item->title_id, $server );
         if ( $item->due_date ) {
-            $resp .= add_field( FID_DUE_DATE, timestamp( $item->due_date ), $server );
+            my $due_date =
+              $account->{format_due_date}
+              ? output_pref( { str => $item->due_date, as_due_date => 1 } )
+              : timestamp( $item->due_date );
+            $resp .= add_field( FID_DUE_DATE, $due_date, $server );
         } else {
             $resp .= add_field( FID_DUE_DATE, q{}, $server );
         }
@@ -740,6 +757,8 @@ sub handle_block_patron {
     $patron_id        = $fields->{ (FID_PATRON_ID) };
     $terminal_pwd     = $fields->{ (FID_TERMINAL_PWD) };
 
+    Koha::Plugins->call('patron_barcode_transform', \$patron_id );
+
     # Terminal passwords are different from account login
     # passwords, but I have no idea what to do with them.  So,
     # I'll just ignore them for now.
@@ -964,6 +983,8 @@ sub handle_patron_info {
     $start        = $fields->{ (FID_START_ITEM) };
     $end          = $fields->{ (FID_END_ITEM) };
 
+    Koha::Plugins->call('patron_barcode_transform', \$patron_id );
+
     $patron = $ils->find_patron($patron_id);
 
     $resp = (PATRON_INFO_RESP);
@@ -1121,6 +1142,8 @@ sub handle_fee_paid {
     $fee_id     = $fields->{ (FID_FEE_ID) };
     $trans_id   = $fields->{ (FID_TRANSACTION_ID) };
 
+    Koha::Plugins->call('patron_barcode_transform', \$patron_id );
+
     $ils->check_inst_id( $inst_id, "handle_fee_paid" );
 
     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 );
@@ -1178,10 +1201,11 @@ sub handle_fee_paid {
 
 sub handle_item_information {
     my ( $self, $server ) = @_;
-    my $ils = $server->{ils};
+    my $account = $server->{account};
+    my $ils     = $server->{ils};
+    my $fields  = $self->{fields};
+    my $resp    = ITEM_INFO_RESP;
     my $trans_date;
-    my $fields = $self->{fields};
-    my $resp   = ITEM_INFO_RESP;
     my $item;
     my $i;
 
@@ -1206,11 +1230,16 @@ sub handle_item_information {
     } else {
 
         # Valid Item ID, send the good stuff
-        $resp .= $item->sip_circulation_status;
+        my $circulation_status = $item->sip_circulation_status;
+        $resp .= $circulation_status;
         $resp .= $item->sip_security_marker;
         $resp .= $item->sip_fee_type;
         $resp .= timestamp;
 
+        if ( $circulation_status eq '01' ) {
+            $resp .= maybe_add( FID_SCREEN_MSG, "Item is damaged", $server );
+        }
+
         $resp .= add_field( FID_ITEM_ID,  $item->id, $server );
         $resp .= add_field( FID_TITLE_ID, $item->title_id, $server );
 
@@ -1219,6 +1248,7 @@ sub handle_item_information {
         $resp .= maybe_add( FID_CURRENT_LOCN, $item->current_location, $server );
         $resp .= maybe_add( FID_ITEM_PROPS,   $item->sip_item_properties, $server );
 
+
         if ( my $CR = $server->{account}->{cr_item_field} ) {
                 $resp .= maybe_add( FID_COLLECTION_CODE, $item->{$CR}, $server );
         } else {
@@ -1235,7 +1265,11 @@ sub handle_item_information {
             $resp .= add_field( FID_HOLD_QUEUE_LEN, $i, $server );
         }
         if ( $item->due_date ) {
-            $resp .= add_field( FID_DUE_DATE, timestamp( $item->due_date ), $server );
+            my $due_date =
+              $account->{format_due_date}
+              ? output_pref( { str => $item->due_date, as_due_date => 1 } )
+              : timestamp( $item->due_date );
+            $resp .= add_field( FID_DUE_DATE, $due_date, $server );
         }
         if ( ( $i = $item->recall_date ) != 0 ) {
             $resp .= add_field( FID_RECALL_DATE, timestamp($i), $server );
@@ -1248,6 +1282,7 @@ sub handle_item_information {
         $resp .= maybe_add( FID_PRINT_LINE, $item->print_line, $server );
 
         $resp .= $item->build_additional_item_fields_string( $server );
+        $resp .= $item->build_custom_field_string( $server );
     }
 
     $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
@@ -1317,6 +1352,8 @@ sub handle_patron_enable {
     $patron_id  = $fields->{ (FID_PATRON_ID) };
     $patron_pwd = $fields->{ (FID_PATRON_PWD) };
 
+    Koha::Plugins->call('patron_barcode_transform', \$patron_id );
+
     siplog( "LOG_DEBUG", "handle_patron_enable: patron_id: '%s', patron_pwd: '%s'", $patron_id, $patron_pwd );
 
     $patron = $ils->find_patron($patron_id);
@@ -1380,6 +1417,8 @@ sub handle_hold {
     $title_id    = $fields->{ (FID_TITLE_ID) } || '';
     $fee_ack     = $fields->{ (FID_FEE_ACK) } || 'N';
 
+    Koha::Plugins->call('patron_barcode_transform', \$patron_id );
+
     if ( $hold_mode eq '+' ) {
         $status = $ils->add_hold( $patron_id, $patron_pwd, $item_id, $title_id, $expiry_date, $pickup_locn, $hold_type, $fee_ack );
     } elsif ( $hold_mode eq '-' ) {
@@ -1445,6 +1484,8 @@ sub handle_renew {
     $item_props = $fields->{ (FID_ITEM_PROPS) };
     $fee_ack    = $fields->{ (FID_FEE_ACK) };
 
+    Koha::Plugins->call('patron_barcode_transform', \$patron_id );
+
     $status = $ils->renew( $patron_id, $patron_pwd, $item_id, $title_id, $no_block, $nb_due_date, $third_party, $item_props, $fee_ack );
 
     $patron = $status->patron;
@@ -1458,7 +1499,7 @@ sub handle_renew {
         } else {
             $resp .= 'U';
         }
-        $resp .= sipbool( $status->desensitize );
+        $resp .= sipbool( desensitize( { status => $status, patron => $patron, server => $server } ) );
         $resp .= timestamp;
         $resp .= add_field( FID_PATRON_ID, $patron->id, $server );
         $resp .= add_field( FID_ITEM_ID, $item->id, $server );
@@ -1526,6 +1567,8 @@ sub handle_renew_all {
     $terminal_pwd = $fields->{ (FID_TERMINAL_PWD) };
     $fee_ack      = $fields->{ (FID_FEE_ACK) };
 
+    Koha::Plugins->call('patron_barcode_transform', \$patron_id );
+
     $status = $ils->renew_all( $patron_id, $patron_pwd, $fee_ack );
 
     $resp .= $status->ok ? '1' : '0';
@@ -1584,13 +1627,17 @@ my @message_type_names = (
 
 sub send_acs_status {
     my ( $self, $server, $screen_msg, $print_line ) = @_;
+
     my $msg = ACS_STATUS;
     ($server) or die "send_acs_status error: no \$server argument received";
     my $account = $server->{account} or die "send_acs_status error: no 'account' in \$server object:\n" . Dumper($server);
     my $policy  = $server->{policy}  or die "send_acs_status error: no 'policy' in \$server object:\n" . Dumper($server);
     my $ils     = $server->{ils}     or die "send_acs_status error: no 'ils' in \$server object:\n" . Dumper($server);
+    my $sip_username = $server->{sip_username} or die "send_acs_status error: no 'sip_username' in \$server object:\n" . Dumper($server);
     my ( $online_status,    $checkin_ok, $checkout_ok, $ACS_renewal_policy );
     my ( $status_update_ok, $offline_ok, $timeout,     $retries );
+    my $sip_user = Koha::Patrons->find({ userid => $sip_username });
+    die "send_acs_status error: sip_username cannot be found in DB or DB cannot be reached" unless $sip_user;
 
     $online_status      = 'Y';
     $checkout_ok        = sipbool( $ils->checkout_ok );
@@ -1701,6 +1748,38 @@ sub api_auth {
     return $status;
 }
 
+sub desensitize {
+    my ($params) = @_;
+
+    my $status      = $params->{status};
+    my $desensitize = $status->desensitize();
+
+    # If desenstize is already false, no need to do anything
+    return unless $desensitize;
+
+    my $patron = $params->{patron};
+    my $item   = $params->{item};
+    my $server = $params->{server};
+
+    my $patron_categories = $server->{account}->{inhouse_patron_categories} // q{};
+    my $item_types = $server->{account}->{inhouse_item_types} // q{};
+
+    # If no patron categories or item types are set for never desensitize, no need to do anything
+    return $desensitize unless $patron_categories || $item_types;
+
+    my $patron_category = $patron->ptype();
+    my @patron_categories = split( /,/, $patron_categories );
+    my $found_patron_category = grep( /^$patron_category$/, @patron_categories );
+    return 0 if $found_patron_category;
+
+    my $item_type = $item->itemtype;
+    my @item_types = split( /,/, $item_types );
+    my $found_item_type = grep( /^$item_type$/, @item_types );
+    return 0 if $found_item_type;
+
+    return 1;
+}
+
 1;
 __END__