Bug 15253: Add Koha::Logger based logging for SIP2
[koha-equinox.git] / C4 / SIP / ILS / Transaction / Checkout.pm
index d277c80..1765dc4 100644 (file)
@@ -8,7 +8,7 @@ use warnings;
 use strict;
 
 use POSIX qw(strftime);
-use Sys::Syslog qw(syslog);
+use C4::SIP::Sip qw(syslog);
 use Data::Dumper;
 use CGI qw ( -utf8 );
 
@@ -19,11 +19,12 @@ use C4::Circulation;
 use C4::Members;
 use C4::Reserves qw(ModReserveFill);
 use C4::Debug;
+use Koha::DateUtils;
+
 use parent qw(C4::SIP::ILS::Transaction);
 
 our $debug;
 
-our $VERSION = 3.07.00.049;
 
 # Most fields are handled by the Transaction superclass
 my %fields = (
@@ -47,27 +48,25 @@ sub new {
 sub do_checkout {
        my $self = shift;
        syslog('LOG_DEBUG', "ILS::Transaction::Checkout performing checkout...");
-       my $pending        = $self->{item}->pending_queue;
        my $shelf          = $self->{item}->hold_shelf;
        my $barcode        = $self->{item}->id;
        my $patron_barcode = $self->{patron}->id;
         my $overridden_duedate; # usually passed as undef to AddIssue
        $debug and warn "do_checkout: patron (" . $patron_barcode . ")";
-       my $borrower = $self->{patron}->getmemberdetails_object();
+    my $patron = Koha::Patrons->find( { cardnumber => $patron_barcode } );
+    my $borrower = $patron->unblessed;
        $debug and warn "do_checkout borrower: . " . Dumper $borrower;
-       my ($issuingimpossible,$needsconfirmation) = CanBookBeIssued(
-        $borrower,
-        $barcode,
-        undef,
-        0,
+    my ($issuingimpossible, $needsconfirmation) = _can_we_issue($patron, $barcode,
         C4::Context->preference("AllowItemsOnHoldCheckout")
     );
-       my $noerror=1;
-    if (scalar keys %$issuingimpossible) {
-        foreach (keys %$issuingimpossible) {
+
+    my $noerror=1;  # If set to zero we block the issue
+    if (keys %{$issuingimpossible}) {
+        foreach (keys %{$issuingimpossible}) {
             # do something here so we pass these errors
-            $self->screen_msg($_ . ': ' . $issuingimpossible->{$_});
+            $self->screen_msg("Issue failed : $_");
             $noerror = 0;
+            last;
         }
     } else {
         foreach my $confirmation (keys %{$needsconfirmation}) {
@@ -79,20 +78,30 @@ sub do_checkout {
                     $self->screen_msg("Item was reserved for you.");
                 } else {
                     $self->screen_msg("Item is reserved for another patron upon return.");
-                    # $noerror = 0;
+                    $noerror = 0;
                 }
             } elsif ($confirmation eq 'ISSUED_TO_ANOTHER') {
                 $self->screen_msg("Item already checked out to another patron.  Please return item for check-in.");
                 $noerror = 0;
+                last;
             } elsif ($confirmation eq 'DEBT') {
                 $self->screen_msg('Outstanding Fines block issue');
                 $noerror = 0;
+                last;
             } elsif ($confirmation eq 'HIGHHOLDS') {
                 $overridden_duedate = $needsconfirmation->{$confirmation}->{returndate};
                 $self->screen_msg('Loan period reduced for high-demand item');
+            } elsif ($confirmation eq 'RENTALCHARGE') {
+                if ($self->{fee_ack} ne 'Y') {
+                    $noerror = 0;
+                    last;
+                }
             } else {
-                $self->screen_msg($needsconfirmation->{$confirmation});
+                # We've been returned a case other than those above
+                $self->screen_msg("Item cannot be issued: $confirmation");
                 $noerror = 0;
+                syslog('LOG_DEBUG', "Blocking checkout Reason:$confirmation");
+                last;
             }
         }
     }
@@ -106,35 +115,46 @@ sub do_checkout {
         $self->screen_msg("Item is on hold shelf for another patron.");
         $noerror = 0;
     }
+    my ($fee, undef) = GetIssuingCharges($itemnumber, $self->{patron}->{borrowernumber});
+    if ( $fee > 0 ) {
+        $self->{sip_fee_type} = '06';
+        $self->{fee_amount} = sprintf '%.2f', $fee;
+        if ($self->{fee_ack} eq 'N' ) {
+            $noerror = 0;
+        }
+    }
        unless ($noerror) {
                $debug and warn "cannot issue: " . Dumper($issuingimpossible) . "\n" . Dumper($needsconfirmation);
                $self->ok(0);
                return $self;
        }
-    # Fill any reserves the patron had on the item.  
-    # TODO: this logic should be pulled internal to AddIssue for all Koha. 
-    $debug and warn "pending_queue: " . (@$pending) ? Dumper($pending) : '[]';
-    foreach (grep {$_->{borrowernumber} eq $self->{patron}->{borrowernumber}} @$pending) {
-        $debug and warn "Filling reserve (borrowernumber,biblionumber,reservedate): "
-            . sprintf("(%s,%s,%s)\n",$_->{borrowernumber},$_->{biblionumber},$_->{reservedate});
-        ModReserveFill($_);
-        # TODO: adjust representation in $self->item
-    }
        # can issue
        $debug and warn "do_checkout: calling AddIssue(\$borrower,$barcode, $overridden_duedate, 0)\n"
                # . "w/ \$borrower: " . Dumper($borrower)
                . "w/ C4::Context->userenv: " . Dumper(C4::Context->userenv);
-       my $due_dt  = AddIssue($borrower, $barcode, $overridden_duedate, 0);
-    if ($due_dt) {
-        $self->{due} = $due_dt->clone();
-    } else {
-        $self->{due} = undef;
-    }
+    my $issue = AddIssue( $borrower, $barcode, $overridden_duedate, 0 );
+    $self->{due} = $self->duedatefromissue($issue, $itemnumber);
 
-    #$self->{item}->due_date($due);
        $self->ok(1);
        return $self;
 }
 
+sub _can_we_issue {
+    my ( $patron, $barcode, $pref ) = @_;
+
+    my ( $issuingimpossible, $needsconfirmation, $alerts ) =
+      CanBookBeIssued( $patron, $barcode, undef, 0, $pref );
+    for my $href ( $issuingimpossible, $needsconfirmation ) {
+
+        # some data is returned using lc keys we only
+        foreach my $key ( keys %{$href} ) {
+            if ( $key =~ m/[^A-Z_]/ ) {
+                delete $href->{$key};
+            }
+        }
+    }
+    return ( $issuingimpossible, $needsconfirmation );
+}
+
 1;
 __END__