Bug 19493: Remove few warnings from circulation.pl
[koha.git] / circ / returns.pl
index ddced56..80f228a 100755 (executable)
@@ -30,6 +30,8 @@ script to execute returns of books
 use strict;
 use warnings;
 
+# FIXME There are weird things going on with $patron and $borrowernumber in this script
+
 use Carp 'verbose';
 $SIG{ __DIE__ } = sub { Carp::confess( @_ ) };
 
@@ -52,6 +54,7 @@ use Koha::DateUtils;
 use Koha::Calendar;
 use Koha::BiblioFrameworks;
 use Koha::Checkouts;
+use Koha::Holds;
 use Koha::Items;
 use Koha::Patrons;
 
@@ -157,7 +160,10 @@ if ( $query->param('reserve_id') ) {
     my $biblio = $item->biblio;
 
     if ( $cancel_reserve ) {
-        CancelReserve({ reserve_id => $reserve_id, charge_cancel_fee => !$forgivemanualholdsexpire });
+        my $hold = Koha::Holds->find( $reserve_id );
+        if ( $hold ) {
+            $hold->cancel( { charge_cancel_fee => !$forgivemanualholdsexpire } );
+        } # FIXME else?
     } else {
         my $diffBranchSend = ($userenv_branch ne $diffBranchReturned) ? $diffBranchReturned : undef;
         # diffBranchSend tells ModReserveAffect whether document is expected in this library or not,
@@ -167,8 +173,8 @@ if ( $query->param('reserve_id') ) {
 #   check if we have other reserves for this document, if we have a return send the message of transfer
     my ( $messages, $nextreservinfo ) = GetOtherReserves($itemnumber);
 
-    my $borr = GetMember( borrowernumber => $nextreservinfo );
-    my $name   = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
+    my $patron = Koha::Patrons->find( $nextreservinfo );
+    my $name   = $patron ? $patron->surname . ", " . $patron->title . " " . $patron->firstname : '';
     if ( $messages->{'transfert'} ) {
         $template->param(
             itemtitle      => $biblio->title,
@@ -177,10 +183,10 @@ if ( $query->param('reserve_id') ) {
             iteminfo       => $biblio->author,
             name           => $name,
             borrowernumber => $borrowernumber,
-            borcnum        => $borr->{'cardnumber'},
-            borfirstname   => $borr->{'firstname'},
-            borsurname     => $borr->{'surname'},
-            borcategory    => $borr->{'description'},
+            borcnum        => $patron->cardnumber,
+            borfirstname   => $patron->firstname,
+            borsurname     => $patron->surname,
+            borcategory    => $patron->category->description,
             diffbranch     => 1,
         );
     }
@@ -259,42 +265,44 @@ if ($barcode) {
     $barcode = barcodedecode($barcode) if C4::Context->preference('itemBarcodeInputFilter');
     my $item = Koha::Items->find({ barcode => $barcode });
 
-    # Check if we should display a checkin message, based on the the item
-    # type of the checked in item
-    my $itemtype = Koha::ItemTypes->find( $item->effective_itemtype );
-    if ( $itemtype && $itemtype->checkinmsg ) {
-        $template->param(
-            checkinmsg     => $itemtype->checkinmsg,
-            checkinmsgtype => $itemtype->checkinmsgtype,
-        );
-    }
+    if ( $item ) {
+        # Check if we should display a checkin message, based on the the item
+        # type of the checked in item
+        my $itemtype = Koha::ItemTypes->find( $item->effective_itemtype );
+        if ( $itemtype && $itemtype->checkinmsg ) {
+            $template->param(
+                checkinmsg     => $itemtype->checkinmsg,
+                checkinmsgtype => $itemtype->checkinmsgtype,
+            );
+        }
 
-    # make sure return branch respects home branch circulation rules, default to homebranch
-    my $hbr = GetBranchItemRule($item->homebranch, $itemtype ? $itemtype->itemtype : undef )->{'returnbranch'} || "homebranch";
-    $returnbranch = $item->$hbr;
+        # make sure return branch respects home branch circulation rules, default to homebranch
+        my $hbr = GetBranchItemRule($item->homebranch, $itemtype ? $itemtype->itemtype : undef )->{'returnbranch'} || "homebranch";
+        $returnbranch = $item->$hbr;
 
-    my $materials = $item->materials;
-    my $descriptions = Koha::AuthorisedValues->get_description_by_koha_field({frameworkcode => '', kohafield =>'items.materials', authorised_value => $materials });
-    $materials = $descriptions->{lib} // $materials;
+        my $materials = $item->materials;
+        my $descriptions = Koha::AuthorisedValues->get_description_by_koha_field({frameworkcode => '', kohafield =>'items.materials', authorised_value => $materials });
+        $materials = $descriptions->{lib} // $materials;
 
-    my $issue = Koha::Checkouts->find( { itemnumber => $itemnumber } );
+        my $issue = Koha::Checkouts->find( { itemnumber => $itemnumber } );
 
-    my $biblio = $item->biblio;
-    $template->param(
-        title            => $biblio->title,
-        homebranch       => $item->homebranch,
-        holdingbranch    => $item->holdingbranch,
-        returnbranch     => $returnbranch,
-        author           => $biblio->author,
-        itembarcode      => $item->barcode,
-        itemtype         => $item->effective_itemtype,
-        ccode            => $item->ccode,
-        itembiblionumber => $biblio->biblionumber,
-        biblionumber     => $biblio->biblionumber,
-        borrower         => $borrower,
-        additional_materials => $materials,
-        issue            => $issue,
-    );
+        my $biblio = $item->biblio;
+        $template->param(
+            title            => $biblio->title,
+            homebranch       => $item->homebranch,
+            holdingbranch    => $item->holdingbranch,
+            returnbranch     => $returnbranch,
+            author           => $biblio->author,
+            itembarcode      => $item->barcode,
+            itemtype         => $item->effective_itemtype,
+            ccode            => $item->ccode,
+            itembiblionumber => $biblio->biblionumber,
+            biblionumber     => $biblio->biblionumber,
+            borrower         => $borrower,
+            additional_materials => $materials,
+            issue            => $issue,
+        );
+    } # FIXME else we should not call AddReturn but set BadBarcode directly instead
 
     my %input = (
         counter => 0,
@@ -302,6 +310,7 @@ if ($barcode) {
         barcode => $barcode,
     );
 
+
     # do the return
     ( $returned, $messages, $issue, $borrower ) =
       AddReturn( $barcode, $userenv_branch, $exemptfine, $dropboxmode, $return_date_override, $dropboxdate );
@@ -396,27 +405,31 @@ if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
     );
 
     my $reserve    = $messages->{'ResFound'};
-    my $borr = C4::Members::GetMember( borrowernumber => $reserve->{'borrowernumber'} );
-    my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
+    if ( $reserve ) {
+        my $patron = Koha::Patrons->find( $reserve->{'borrowernumber'} );
+        my $name = $patron->surname . ", " . $patron->title . " " . $patron->firstname;
+        $template->param(
+            # FIXME The full patron object should be passed to the template
+                wname           => $name,
+                wborfirstname   => $patron->firstname,
+                wborsurname     => $patron->surname,
+                wborcategory    => $patron->category->description,
+                wbortitle       => $patron->title,
+                wborphone       => $patron->phone,
+                wboremail       => $patron->email,
+                streetnumber    => $patron->streetnumber,
+                address         => $patron->address,
+                address2        => $patron->address2,
+                city            => $patron->city,
+                zipcode         => $patron->zipcode,
+                state           => $patron->state,
+                country         => $patron->country,
+                wborrowernumber => $reserve->{'borrowernumber'},
+                wborcnum        => $patron->cardnumber,
+        );
+    }
     $template->param(
-            wname           => $name,
-            wborfirstname   => $borr->{'firstname'},
-            wborsurname     => $borr->{'surname'},
-            wborcategory    => $borr->{'description'},
-            wbortitle       => $borr->{'title'},
-            wborphone       => $borr->{'phone'},
-            wboremail       => $borr->{'email'},
-            streetnumber    => $borr->{streetnumber},
-            streettype      => $borr->{streettype},
-            address         => $borr->{'address'},
-            address2        => $borr->{'address2'},
-            city            => $borr->{'city'},
-            zipcode         => $borr->{'zipcode'},
-            state           => $borr->{'state'},
-            country         => $borr->{'country'},
-            wborrowernumber => $reserve->{'borrowernumber'},
-            wborcnum        => $borr->{'cardnumber'},
-            wtransfertFrom  => $userenv_branch,
+        wtransfertFrom  => $userenv_branch,
     );
 }
 
@@ -425,7 +438,7 @@ if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
 #
 if ( $messages->{'ResFound'}) {
     my $reserve    = $messages->{'ResFound'};
-    my $borr = C4::Members::GetMember( borrowernumber => $reserve->{'borrowernumber'} );
+    my $patron = Koha::Patrons->find( $reserve->{borrowernumber} );
     my $holdmsgpreferences =  C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $reserve->{'borrowernumber'}, message_name   => 'Hold_Filled' } );
     if ( $reserve->{'ResFound'} eq "Waiting" or $reserve->{'ResFound'} eq "Reserved" ) {
         if ( $reserve->{'ResFound'} eq "Waiting" ) {
@@ -443,25 +456,25 @@ if ( $messages->{'ResFound'}) {
 
         # same params for Waiting or Reserved
         $template->param(
+            # FIXME The full patron object should be passed to the template
             found          => 1,
-            name           => $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'},
-            borfirstname   => $borr->{'firstname'},
-            borsurname     => $borr->{'surname'},
-            borcategory    => $borr->{'description'},
-            bortitle       => $borr->{'title'},
-            borphone       => $borr->{'phone'},
-            boremail       => $borr->{'email'},
-            streetnumber   => $borr->{streetnumber},
-            streettype     => $borr->{streettype},
-            address        => $borr->{'address'},
-            address2       => $borr->{'address2'},
-            city           => $borr->{'city'},
-            zipcode        => $borr->{'zipcode'},
-            state          => $borr->{'state'},
-            country        => $borr->{'country'},
-            borcnum        => $borr->{'cardnumber'},
-            debarred       => $borr->{'debarred'},
-            gonenoaddress  => $borr->{'gonenoaddress'},
+            name           => $patron->surname . ", " . $patron->title . " " . $patron->firstname,
+            borfirstname   => $patron->firstname,
+            borsurname     => $patron->surname,
+            borcategory    => $patron->category->description,
+            bortitle       => $patron->title,
+            borphone       => $patron->phone,
+            boremail       => $patron->email,
+            boraddress     => $patron->address,
+            boraddress2    => $patron->address2,
+            streetnumber   => $patron->streetnumber,
+            city           => $patron->city,
+            zipcode        => $patron->zipcode,
+            state          => $patron->state,
+            country        => $patron->country,
+            borcnum        => $patron->cardnumber,
+            debarred       => $patron->debarred,
+            gonenoaddress  => $patron->gonenoaddress,
             barcode        => $barcode,
             destbranch     => $reserve->{'branchcode'},
             borrowernumber => $reserve->{'borrowernumber'},
@@ -540,6 +553,9 @@ foreach my $code ( keys %$messages ) {
     elsif ( $code eq 'NotForLoanStatusUpdated' ) {
         $err{NotForLoanStatusUpdated} = $messages->{NotForLoanStatusUpdated};
     }
+    elsif ( $code eq 'DataCorrupted' ) {
+        $err{data_corrupted} = 1;
+    }
     else {
         die "Unknown error code $code";    # note we need all the (empty) elsif's above, or we die.
         # This forces the issue of staying in sync w/ Circulation.pm
@@ -569,19 +585,19 @@ foreach ( sort { $a <=> $b } keys %returneditems ) {
             $ri{hour}   = $duedate->hour();
             $ri{minute}   = $duedate->minute();
             $ri{duedate} = output_pref($duedate);
-            my $b      = C4::Members::GetMember( borrowernumber => $riborrowernumber{$_} );
+            my $patron = Koha::Patrons->find( $riborrowernumber{$_} );
             unless ( $dropboxmode ) {
                 $ri{return_overdue} = 1 if (DateTime->compare($duedate, DateTime->now()) == -1);
             } else {
                 $ri{return_overdue} = 1 if (DateTime->compare($duedate, $dropboxdate) == -1);
             }
-            $ri{borrowernumber} = $b->{'borrowernumber'};
-            $ri{borcnum}        = $b->{'cardnumber'};
-            $ri{borfirstname}   = $b->{'firstname'};
-            $ri{borsurname}     = $b->{'surname'};
-            $ri{bortitle}       = $b->{'title'};
-            $ri{bornote}        = $b->{'borrowernotes'};
-            $ri{borcategorycode}= $b->{'categorycode'};
+            $ri{borrowernumber} = $patron->borrowernumber;
+            $ri{borcnum}        = $patron->cardnumber;
+            $ri{borfirstname}   = $patron->firstname;
+            $ri{borsurname}     = $patron->surname;
+            $ri{bortitle}       = $patron->title;
+            $ri{bornote}        = $patron->borrowernotes;
+            $ri{borcategorycode}= $patron->categorycode;
             $ri{borissuescount} = Koha::Checkouts->count( { borrowernumber => $b->{'borrowernumber'} } );
         }
         else {