Bug 3638 : Captured Holds may need to generate a transfer
[koha.git] / C4 / SIP / Sip / MsgType.pm
index 16b1506..c3914c0 100644 (file)
@@ -176,13 +176,13 @@ my %handlers = (
                    handler => \&handle_fee_paid,
                    protocol => {
                        2 => {
-                           template => "A18A2A3",
-                           template_len => 0,
+                           template => "A18A2A2A3",
+                           template_len => 25,
                            fields => [(FID_FEE_AMT), (FID_INST_ID),
                                       (FID_PATRON_ID), (FID_TERMINAL_PWD),
                                       (FID_PATRON_PWD), (FID_FEE_ID),
-                                      (FID_TRANSACTION_ID)],
-                       }
+                       (FID_TRANSACTION_ID)],
+               }
                    }
                },
                (ITEM_INFORMATION) => {
@@ -531,7 +531,7 @@ sub handle_checkout {
        $resp = CHECKOUT_RESP . '1';
        $resp .= sipbool($status->renew_ok);
        if ($ils->supports('magnetic media')) {
-           $resp .= sipbool($item->magnetic);
+           $resp .= sipbool($item->magnetic_media);
        } else {
            $resp .= 'U';
        }
@@ -544,7 +544,11 @@ sub handle_checkout {
        $resp .= add_field(FID_PATRON_ID, $patron_id);
        $resp .= add_field(FID_ITEM_ID, $item_id);
        $resp .= add_field(FID_TITLE_ID, $item->title_id);
-       $resp .= add_field(FID_DUE_DATE, $item->due_date);
+    if ($item->due_date) {
+        $resp .= add_field(FID_DUE_DATE, Sip::timestamp($item->due_date));
+    } else {
+        $resp .= add_field(FID_DUE_DATE, q{});
+    }
 
        $resp .= maybe_add(FID_SCREEN_MSG, $status->screen_msg);
        $resp .= maybe_add(FID_PRINT_LINE, $status->print_line);
@@ -606,6 +610,7 @@ sub handle_checkin {
     my ($self, $server) = @_;
     my $account = $server->{account};
     my $ils     = $server->{ils};
+    my $my_branch = $ils->institution;
     my ($current_loc, $inst_id, $item_id, $terminal_pwd, $item_props, $cancel);
     my ($patron, $item, $status);
     my $resp = CHECKIN_RESP;
@@ -617,6 +622,9 @@ sub handle_checkin {
        $item_id     = $fields->{(FID_ITEM_ID)};
        $item_props  = $fields->{(FID_ITEM_PROPS)};
        $cancel      = $fields->{(FID_CANCEL)};
+    if ($current_loc) {
+        $my_branch = $current_loc;# most scm do not set $current_loc
+    }
 
     $ils->check_inst_id($inst_id, "handle_checkin");
 
@@ -625,7 +633,7 @@ sub handle_checkin {
         syslog("LOG_WARNING", "received no-block checkin from terminal '%s'", $account->{id});
         $status = $ils->checkin_no_block($item_id, $trans_date, $return_date, $item_props, $cancel);
     } else {
-        $status = $ils->checkin($item_id, $trans_date, $return_date, $current_loc, $item_props, $cancel);
+        $status = $ils->checkin($item_id, $trans_date, $return_date, $my_branch, $item_props, $cancel);
     }
 
     $patron = $status->patron;
@@ -634,12 +642,20 @@ sub handle_checkin {
     $resp .= $status->ok ? '1' : '0';
     $resp .= $status->resensitize ? 'Y' : 'N';
     if ($item && $ils->supports('magnetic media')) {
-               $resp .= sipbool($item->magnetic);
+               $resp .= sipbool($item->magnetic_media);
     } else {
-       # The item barcode was invalid or the system doesn't support
-       # the 'magnetic media' indicator
+        # item barcode is invalid or system doesn't support 'magnetic media' indicator
                $resp .= 'U';
     }
+
+    # apparently we can't trust the returns from Checkin yet (because C4::Circulation::AddReturn is faulty)
+    # So we reproduce the alert logic here.
+    if (not $status->alert) {
+        if ($item->destination_loc and $item->destination_loc ne $my_branch) {
+            $status->alert(1);
+            $status->alert_type('04');  # no hold, just send it
+        }
+    }
     $resp .= $status->alert ? 'Y' : 'N';
     $resp .= Sip::timestamp;
     $resp .= add_field(FID_INST_ID, $inst_id);
@@ -656,17 +672,21 @@ sub handle_checkin {
             $resp .= add_field(FID_PATRON_ID, $patron->id);
         }
         if ($item) {
-            $resp .= maybe_add(FID_MEDIA_TYPE,        $item->sip_media_type     );
-            $resp .= maybe_add(FID_ITEM_PROPS,        $item->sip_item_properties);
-            # $resp .= maybe_add(FID_COLLECTION_CODE, $item->collection_code    );
-            # $resp .= maybe_add(FID_CALL_NUMBER,     $item->call_number        );
-            # $resp .= maybe_add(FID_DESTINATION,     $item->destination_loc    );
-            # $resp .= maybe_add(FID_ALERT_TYPE,      $item->alert_type         );
-            # $resp .= maybe_add(FID_PATRON_ID,       $item->hold_patron_id     );
-            # $resp .= maybe_add(FID_PATRON_NAME,     $item->hold_patron_name   );
+            $resp .= maybe_add(FID_MEDIA_TYPE,           $item->sip_media_type     );
+            $resp .= maybe_add(FID_ITEM_PROPS,           $item->sip_item_properties);
+            $resp .= maybe_add(FID_COLLECTION_CODE,      $item->collection_code    );
+            $resp .= maybe_add(FID_CALL_NUMBER,          $item->call_number        );
+            $resp .= maybe_add(FID_DESTINATION_LOCATION, $item->destination_loc    );
+            $resp .= maybe_add(FID_HOLD_PATRON_ID,       $item->hold_patron_bcode     );
+            $resp .= maybe_add(FID_HOLD_PATRON_NAME,     $item->hold_patron_name   );
+            if ($status->hold and $status->hold->{branchcode} ne $item->destination_loc) {
+                warn 'SIP hold mismatch: $status->hold->{branchcode}=' . $status->hold->{branchcode} . '; $item->destination_loc=' . $item->destination_loc;
+                # just me being paranoid.
+            }
         }
     }
 
+    $resp .= maybe_add(FID_ALERT_TYPE, $status->alert_type) if $status->alert;
     $resp .= maybe_add(FID_SCREEN_MSG, $status->screen_msg);
     $resp .= maybe_add(FID_PRINT_LINE, $status->print_line);
 
@@ -794,7 +814,7 @@ sub login_core ($$$) {
                $server->{sip_username} = $uid;
                $server->{sip_password} = $pwd;
 
-               my $auth_status = api_auth($uid,$pwd);
+        my $auth_status = api_auth($uid,$pwd,$inst);
                if (!$auth_status or $auth_status !~ /^ok$/i) {
                        syslog("LOG_WARNING", "api_auth failed for SIP terminal '%s' of '%s': %s",
                                                $uid, $inst, ($auth_status||'unknown'));
@@ -885,7 +905,7 @@ sub summary_info {
 
     syslog("LOG_DEBUG", "summary_info: list = (%s)", join(", ", @{$itemlist}));
     foreach my $i (@{$itemlist}) {
-        $resp .= add_field($fid, $i);
+        $resp .= add_field($fid, $i->{barcode});
     }
 
     return $resp;
@@ -910,69 +930,69 @@ sub handle_patron_info {
 
     $resp = (PATRON_INFO_RESP);
     if ($patron) {
-       $resp .= patron_status_string($patron);
-       $resp .= (defined($lang) and length($lang) ==3) ? $lang : $patron->language;
-       $resp .= Sip::timestamp();
-
-       $resp .= add_count('patron_info/hold_items',
-                          scalar @{$patron->hold_items});
-       $resp .= add_count('patron_info/overdue_items',
-                          scalar @{$patron->overdue_items});
-       $resp .= add_count('patron_info/charged_items',
-                          scalar @{$patron->charged_items});
-       $resp .= add_count('patron_info/fine_items',
-                          scalar @{$patron->fine_items});
-       $resp .= add_count('patron_info/recall_items',
-                          scalar @{$patron->recall_items});
-       $resp .= add_count('patron_info/unavail_holds',
-                          scalar @{$patron->unavail_holds});
-
-    # FID_INST_ID added last (order irrelevant for fields w/ identifiers)
-
-       # while the patron ID we got from the SC is valid, let's
-       # use the one returned from the ILS, just in case...
-       $resp .= add_field(FID_PATRON_ID,     $patron->id);
-       $resp .= add_field(FID_PERSONAL_NAME, $patron->name);
-
-       # TODO: add code for the fields
-       #   hold items limit
-       #   overdue items limit
-       #   charged items limit
-
-       $resp .= add_field(FID_VALID_PATRON, 'Y');
-       if (defined($patron_pwd)) {
-           # If patron password was provided, report whether it was right or not.
-           $resp .= add_field(FID_VALID_PATRON_PWD,
-                              sipbool($patron->check_password($patron_pwd)));
-       }
+        $resp .= patron_status_string($patron);
+        $resp .= (defined($lang) and length($lang) ==3) ? $lang : $patron->language;
+        $resp .= Sip::timestamp();
+
+        $resp .= add_count('patron_info/hold_items',
+            scalar @{$patron->hold_items});
+        $resp .= add_count('patron_info/overdue_items',
+            scalar @{$patron->overdue_items});
+        $resp .= add_count('patron_info/charged_items',
+            scalar @{$patron->charged_items});
+        $resp .= add_count('patron_info/fine_items',
+            scalar @{$patron->fine_items});
+        $resp .= add_count('patron_info/recall_items',
+            scalar @{$patron->recall_items});
+        $resp .= add_count('patron_info/unavail_holds',
+            scalar @{$patron->unavail_holds});
+
+        $resp .= add_field(FID_INST_ID,       ($ils->institution_id || 'SIP2'));
+
+        # while the patron ID we got from the SC is valid, let's
+        # use the one returned from the ILS, just in case...
+        $resp .= add_field(FID_PATRON_ID,     $patron->id);
+        $resp .= add_field(FID_PERSONAL_NAME, $patron->name);
+
+        # TODO: add code for the fields
+        #   hold items limit
+        #   overdue items limit
+        #   charged items limit
+
+        $resp .= add_field(FID_VALID_PATRON, 'Y');
+        if (defined($patron_pwd)) {
+            # If patron password was provided, report whether it was right or not.
+            $resp .= add_field(FID_VALID_PATRON_PWD,
+                sipbool($patron->check_password($patron_pwd)));
+        }
 
-       $resp .= maybe_add(FID_CURRENCY,   $patron->currency);
-       $resp .= maybe_add(FID_FEE_AMT,    $patron->fee_amount);
-       $resp .= add_field(FID_FEE_LMT,    $patron->fee_limit);
+        $resp .= maybe_add(FID_CURRENCY,   $patron->currency);
+        $resp .= maybe_add(FID_FEE_AMT,    $patron->fee_amount);
+        $resp .= add_field(FID_FEE_LMT,    $patron->fee_limit);
 
-    # TODO: zero or more item details for 2.0 can go here:
-    #          hold_items
-    #       overdue_items
-    #       charged_items
-    #          fine_items
-    #        recall_items
+        # TODO: zero or more item details for 2.0 can go here:
+        #          hold_items
+        #       overdue_items
+        #       charged_items
+        #          fine_items
+        #        recall_items
 
-       $resp .= summary_info($ils, $patron, $summary, $start, $end);
+        $resp .= summary_info($ils, $patron, $summary, $start, $end);
 
-       $resp .= maybe_add(FID_HOME_ADDR,  $patron->address);
-       $resp .= maybe_add(FID_EMAIL,      $patron->email_addr);
-       $resp .= maybe_add(FID_HOME_PHONE, $patron->home_phone);
+        $resp .= maybe_add(FID_HOME_ADDR,  $patron->address);
+        $resp .= maybe_add(FID_EMAIL,      $patron->email_addr);
+        $resp .= maybe_add(FID_HOME_PHONE, $patron->home_phone);
 
-       # SIP 2.0 extensions used by Envisionware
-       # Other terminals will ignore unrecognized fields (unrecognized field identifiers)
-       $resp .= maybe_add(FID_PATRON_BIRTHDATE, $patron->birthdate);
-       $resp .= maybe_add(FID_PATRON_CLASS,     $patron->ptype);
+        # SIP 2.0 extensions used by Envisionware
+        # Other terminals will ignore unrecognized fields (unrecognized field identifiers)
+        $resp .= maybe_add(FID_PATRON_BIRTHDATE, $patron->birthdate);
+        $resp .= maybe_add(FID_PATRON_CLASS,     $patron->ptype);
 
-       # Custom protocol extension to report patron internet privileges
-       $resp .= maybe_add(FID_INET_PROFILE,     $patron->inet_privileges);
+        # Custom protocol extension to report patron internet privileges
+        $resp .= maybe_add(FID_INET_PROFILE,     $patron->inet_privileges);
 
-       $resp .= maybe_add(FID_SCREEN_MSG,       $patron->screen_msg);
-       $resp .= maybe_add(FID_PRINT_LINE,       $patron->print_line);
+        $resp .= maybe_add(FID_SCREEN_MSG,       $patron->screen_msg);
+        $resp .= maybe_add(FID_PRINT_LINE,       $patron->print_line);
     } else {
         # Invalid patron ID:
         # no privileges, no items associated,
@@ -980,6 +1000,7 @@ sub handle_patron_info {
         $resp .= 'YYYY' . (' ' x 10) . $lang . Sip::timestamp();
         $resp .= '0000' x 6;
 
+        $resp .= add_field(FID_INST_ID,       ($ils->institution_id || 'SIP2'));
         # patron ID is invalid, but field is required, so just echo it back
         $resp .= add_field(FID_PATRON_ID,     $fields->{(FID_PATRON_ID)});
         $resp .= add_field(FID_PERSONAL_NAME, '');
@@ -989,7 +1010,6 @@ sub handle_patron_info {
         }
     }
 
-    $resp .= add_field(FID_INST_ID,       ($ils->institution_id || 'SIP2'));
     $self->write_msg($resp);
     return(PATRON_INFO);
 }
@@ -1004,7 +1024,7 @@ sub handle_end_patron_session {
 
     ($trans_date) = @{$self->{fixed_fields}};
 
-    $ils->check_inst_id($fields->{FID_INST_ID}, "handle_end_patron_session");
+    $ils->check_inst_id($fields->{(FID_INST_ID)}, 'handle_end_patron_session');
 
     ($status, $screen_msg, $print_line) = $ils->end_patron_session($fields->{(FID_PATRON_ID)});
 
@@ -1104,8 +1124,8 @@ sub handle_item_information {
        if (($i = scalar @{$item->hold_queue}) > 0) {
            $resp .= add_field(FID_HOLD_QUEUE_LEN, $i);
        }
-       if (($i = $item->due_date) != 0) {
-           $resp .= add_field(FID_DUE_DATE, Sip::timestamp($i));
+       if ($item->due_date) {
+           $resp .= add_field(FID_DUE_DATE, Sip::timestamp($item->due_date));
        }
        if (($i = $item->recall_date) != 0) {
            $resp .= add_field(FID_RECALL_DATE, Sip::timestamp($i));
@@ -1328,7 +1348,7 @@ sub handle_renew {
        $resp .= '1';
        $resp .= $status->renewal_ok ? 'Y' : 'N';
        if ($ils->supports('magnetic media')) {
-           $resp .= sipbool($item->magnetic);
+           $resp .= sipbool($item->magnetic_media);
        } else {
            $resp .= 'U';
        }
@@ -1562,17 +1582,17 @@ sub patron_status_string {
     return $patron_status;
 }
 
-sub api_auth($$) {
-       # AUTH
-       my ($username,$password) = (shift,shift);
-       $ENV{REMOTE_USER} = $username;
-       my $query = CGI->new();
-       $query->param(userid   => $username);
-       $query->param(password => $password);
-       my ($status, $cookie, $sessionID) = check_api_auth($query, {circulate=>1}, "intranet");
-       print STDERR "check_api_auth returns " . ($status || 'undef') . "\n";
-       # print "api_auth userenv = " . &dump_userenv;
-       return $status;
+sub api_auth {
+    my ($username,$password, $branch) = @_;
+    $ENV{REMOTE_USER} = $username;
+    my $query = CGI->new();
+    $query->param(userid   => $username);
+    $query->param(password => $password);
+    if ($branch) {
+        $query->param(branch => $branch);
+    }
+    my ($status, $cookie, $sessionID) = check_api_auth($query, {circulate=>1}, 'intranet');
+    return $status;
 }
 
 1;