Bug 14591: Update other calls to AddReturn
[koha.git] / C4 / SIP / ILS.pm
index 2ea451e..b115dcd 100644 (file)
@@ -126,56 +126,78 @@ sub offline_ok {
 # the response.
 #
 sub checkout {
-    my ($self, $patron_id, $item_id, $sc_renew, $fee_ack) = @_;
-    my ($patron, $item, $circ);
+    my ( $self, $patron_id, $item_id, $sc_renew, $fee_ack ) = @_;
+    my ( $patron, $item, $circ );
 
     $circ = C4::SIP::ILS::Transaction::Checkout->new();
+
     # BEGIN TRANSACTION
-    $circ->patron($patron = C4::SIP::ILS::Patron->new( $patron_id));
-    $circ->item($item = C4::SIP::ILS::Item->new( $item_id));
+    $circ->patron( $patron = C4::SIP::ILS::Patron->new($patron_id) );
+    $circ->item( $item     = C4::SIP::ILS::Item->new($item_id) );
     if ($fee_ack) {
         $circ->fee_ack($fee_ack);
     }
 
-    if (!$patron) {
-               $circ->screen_msg("Invalid Patron");
-    } elsif (!$patron->charge_ok) {
-               $circ->screen_msg("Patron Blocked");
-    } elsif (!$item) {
-               $circ->screen_msg("Invalid Item");
-    # holds checked inside do_checkout
-    # } elsif ($item->hold_queue && @{$item->hold_queue} && ! $item->barcode_is_borrowernumber($patron_id, $item->hold_queue->[0]->{borrowernumber})) {
-       #       $circ->screen_msg("Item on Hold for Another User");
-    } elsif ($item->{patron} && ($item->{patron} ne $patron_id)) {
-       # I can't deal with this right now
-               $circ->screen_msg("Item checked out to another patron");
-    } else {
-               $circ->do_checkout();
-               if ($circ->ok){
-                       $debug and warn "circ is ok";
-                       # If the item is already associated with this patron, then
-                       # we're renewing it.
-                       $circ->renew_ok($item->{patron} && ($item->{patron} eq $patron_id));
-               
-                       $item->{patron} = $patron_id;
-                       $item->{due_date} = $circ->{due};
-                       push(@{$patron->{items}}, $item_id);
-                       $circ->desensitize(!$item->magnetic_media);
-
-                       syslog("LOG_DEBUG", "ILS::Checkout: patron %s has checked out %s",
-                               $patron_id, join(', ', @{$patron->{items}}));
-               }
-               else {
-                       syslog("LOG_ERR", "ILS::Checkout Issue failed");
-               }
+    if ( !$patron ) {
+        $circ->screen_msg("Invalid Patron");
+    }
+    elsif ( !$patron->charge_ok ) {
+        $circ->screen_msg("Patron Blocked");
+    }
+    elsif ( !$item ) {
+        $circ->screen_msg("Invalid Item");
     }
+    elsif ( $item->{patron}
+        && !_ci_cardnumber_cmp( $item->{patron}, $patron_id ) )
+    {
+        $circ->screen_msg("Item checked out to another patron");
+    }
+    else {
+        $circ->do_checkout();
+        if ( $circ->ok ) {
+            $debug and warn "circ is ok";
+
+            # If the item is already associated with this patron, then
+            # we're renewing it.
+            $circ->renew_ok( $item->{patron}
+                  && _ci_cardnumber_cmp( $item->{patron}, $patron_id ) );
+
+            $item->{patron}   = $patron_id;
+            $item->{due_date} = $circ->{due};
+            push( @{ $patron->{items} }, $item_id );
+            $circ->desensitize( !$item->magnetic_media );
+
+            syslog(
+                "LOG_DEBUG", "ILS::Checkout: patron %s has checked out %s",
+                $patron_id, join( ', ', @{ $patron->{items} } )
+            );
+        }
+        else {
+            syslog( "LOG_ERR", "ILS::Checkout Issue failed" );
+        }
+    }
+
     # END TRANSACTION
 
     return $circ;
 }
 
+sub _ci_cardnumber_cmp {
+    my ( $s1, $s2) = @_;
+    # As the database is case insensitive we need to normalize two strings
+    # before comparing them
+    return ( uc($s1) eq uc($s2) );
+}
+
+# wrapper which allows above to be called for testing
+
+sub test_cardnumber_compare {
+    my ($self, $str1, $str2) = @_;
+    return _ci_cardnumber_cmp($str1, $str2);
+}
+
 sub checkin {
-    my ( $self, $item_id, $trans_date, $return_date, $current_loc, $item_props, $cancel, $checked_in_ok ) = @_;
+    my ( $self, $item_id, $trans_date, $return_date, $current_loc, $item_props, $cancel, $checked_in_ok, $cv_triggers_alert ) = @_;
     my ( $patron, $item, $circ );
 
     $circ = C4::SIP::ILS::Transaction::Checkin->new();
@@ -184,32 +206,40 @@ sub checkin {
     $circ->item( $item = C4::SIP::ILS::Item->new($item_id) );
 
     if ($item) {
-        $circ->do_checkin( $current_loc, $return_date );
+        $circ->do_checkin( $current_loc, $return_date, $cv_triggers_alert );
     }
     else {
         $circ->alert(1);
         $circ->alert_type(99);
+        $circ->ok( 0 );
         $circ->screen_msg('Invalid Item');
+        return $circ;
     }
 
-    # It's ok to check it in if it exists, and if it was checked out
-    # or it was not checked out but the checked_in_ok flag was set
-    $circ->ok( ( $checked_in_ok && $item ) || ( $item && $item->{patron} ) );
-
-    if ( !defined( $item->{patron} ) ) {
-        $circ->screen_msg("Item not checked out") unless $checked_in_ok;
-    }
-    else {
-        if ( $circ->ok ) {
-            $circ->patron( $patron = C4::SIP::ILS::Patron->new( $item->{patron} ) );
-            delete $item->{patron};
-            delete $item->{due_date};
-            $patron->{items} = [ grep { $_ ne $item_id } @{ $patron->{items} } ];
+    if( !$circ->ok && $circ->alert_type && $circ->alert_type == 98 ) { # data corruption
+        $circ->screen_msg("Checkin failed: data problem");
+        syslog( "LOG_WARNING", "Problem with issue_id in issues and old_issues; check the about page" );
+    } elsif( !$item->{patron} ) {
+        if( $checked_in_ok ) { # Mark checkin ok although book not checked out
+            $circ->ok( 1 );
+            syslog("LOG_DEBUG", "C4::SIP::ILS::Checkin - using checked_in_ok");
+        } else {
+            $circ->screen_msg("Item not checked out");
+            syslog("LOG_DEBUG", "C4::SIP::ILS::Checkin - item not checked out");
         }
+    } elsif( $circ->ok ) {
+        $circ->patron( $patron = C4::SIP::ILS::Patron->new( $item->{patron} ) );
+        delete $item->{patron};
+        delete $item->{due_date};
+        $patron->{items} = [ grep { $_ ne $item_id } @{ $patron->{items} } ];
+    } else {
+        # Checkin failed: Wrongbranch or withdrawn?
+        # Bug 10748 with pref BlockReturnOfLostItems adds another case to come
+        # here: returning a lost item when the pref is set.
+        $circ->screen_msg("Checkin failed");
+        syslog( "LOG_WARNING", "Checkin failed: probably for Wrongbranch or withdrawn" );
     }
 
-    # END TRANSACTION
-
     return $circ;
 }
 
@@ -223,12 +253,9 @@ sub end_patron_session {
 }
 
 sub pay_fee {
-    my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
-       $pay_type, $fee_id, $trans_id, $currency) = @_;
-    my $trans;
-
-    $trans = C4::SIP::ILS::Transaction::FeePayment->new();
+    my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type, $pay_type, $fee_id, $trans_id, $currency, $is_writeoff, $disallow_overpayment ) = @_;
 
+    my $trans = C4::SIP::ILS::Transaction::FeePayment->new();
 
     $trans->transaction_id($trans_id);
     my $patron;
@@ -237,8 +264,8 @@ sub pay_fee {
         $trans->screen_msg('Invalid patron barcode.');
         return $trans;
     }
-    $trans->pay($patron->{borrowernumber},$fee_amt, $pay_type);
-    $trans->ok(1);
+    my $ok = $trans->pay( $patron->{borrowernumber}, $fee_amt, $pay_type, $fee_id, $is_writeoff, $disallow_overpayment );
+    $trans->ok($ok);
 
     return $trans;
 }