Bug 21719: Fix typos
[koha.git] / circ / returns.pl
index 91eb5cb..63cf75b 100755 (executable)
@@ -27,8 +27,7 @@ script to execute returns of books
 
 =cut
 
-use strict;
-use warnings;
+use Modern::Perl;
 
 # FIXME There are weird things going on with $patron and $borrowernumber in this script
 
@@ -50,7 +49,6 @@ use Koha::AuthorisedValues;
 use Koha::DateUtils;
 use Koha::Calendar;
 use Koha::BiblioFrameworks;
-use Koha::Checkouts;
 use Koha::Holds;
 use Koha::Items;
 use Koha::Patrons;
@@ -80,8 +78,9 @@ if ($session->param('branch') eq 'NO_LIBRARY_SET'){
 if ( $query->param('print_slip') ) {
     $template->param(
         print_slip     => 1,
-        borrowernumber => scalar $query->param('borrowernumber'),
+        borrowernumber => scalar $query->param('borrowernumber'), # FIXME We should send a Koha::Patron and raise an error if not exist.
         biblionumber   => scalar $query->param('biblionumber'),
+        itemnumber     => scalar $query->param('itemnumber'),
     );
 }
 
@@ -179,11 +178,7 @@ if ( $query->param('reserve_id') ) {
             itembiblionumber => $biblio->biblionumber,
             iteminfo       => $biblio->author,
             name           => $name,
-            borrowernumber => $borrowernumber,
-            borcnum        => $patron->cardnumber,
-            borfirstname   => $patron->firstname,
-            borsurname     => $patron->surname,
-            borcategory    => $patron->category->description,
+            patron         => $patron,
             diffbranch     => 1,
         );
     }
@@ -263,6 +258,7 @@ if ($barcode) {
     my $item = Koha::Items->find({ barcode => $barcode });
 
     if ( $item ) {
+        $itemnumber = $item->itemnumber;
         # 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 );
@@ -275,15 +271,14 @@ if ($barcode) {
 
         # 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;
+        $returnbranch = $hbr ne 'noreturn' ? $item->$hbr : $userenv_branch; # can be noreturn, homebranch or holdingbranch
 
         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 $biblio = $item->biblio;
+        my $checkout = $item->checkout;
+        my $biblio   = $item->biblio;
         $template->param(
             title            => $biblio->title,
             homebranch       => $item->homebranch,
@@ -295,9 +290,8 @@ if ($barcode) {
             ccode            => $item->ccode,
             itembiblionumber => $biblio->biblionumber,
             biblionumber     => $biblio->biblionumber,
-            borrower         => $borrower,
             additional_materials => $materials,
-            issue            => $issue,
+            issue            => $checkout,
         );
     } # FIXME else we should not call AddReturn but set BadBarcode directly instead
 
@@ -366,7 +360,7 @@ my $waiting  = 0;
 my $reserved = 0;
 
 # new op dev : we check if the document must be returned to his homebranch directly,
-#  if the document is transfered, we have warning message .
+#  if the document is transferred, we have warning message .
 
 if ( $messages->{'WasTransfered'} ) {
     $template->param(
@@ -391,7 +385,7 @@ if ( $messages->{'Wrongbranch'} ){
     );
 }
 
-# case of wrong transfert, if the document wasn't transfered to the right library (according to branchtransfer (tobranch) BDD)
+# case of wrong transfert, if the document wasn't transferred to the right library (according to branchtransfer (tobranch) BDD)
 
 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
     $template->param(
@@ -406,23 +400,7 @@ if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
         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,
+            patron => $patron,
         );
     }
     $template->param(
@@ -437,7 +415,36 @@ if ( $messages->{'ResFound'}) {
     my $reserve    = $messages->{'ResFound'};
     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 "Reserved" && C4::Context->preference('HoldsAutoFill') ) {
+        my $item = Koha::Items->find( $itemnumber );
+        my $biblio = $item->biblio;
+
+        my $diffBranchSend = ($userenv_branch ne $reserve->{branchcode}) ? $reserve->{branchcode} : undef;
+        ModReserveAffect( $reserve->{itemnumber}, $reserve->{borrowernumber}, $diffBranchSend, $reserve->{reserve_id} );
+        my ( $messages, $nextreservinfo ) = GetOtherReserves($reserve->{itemnumber});
+
+        my $patron = Koha::Patrons->find( $nextreservinfo );
+
+        $template->param(
+            hold_auto_filled => 1,
+            print_slip       => C4::Context->preference('HoldsAutoFillPrintSlip'),
+            patron           => $patron,
+            borrowernumber   => $patron->id,
+            biblionumber     => $biblio->id,
+        );
+
+        if ( $messages->{'transfert'} ) {
+            $template->param(
+                itemtitle      => $biblio->title,
+                itemnumber     => $item->itemnumber,
+                itembiblionumber => $biblio->biblionumber,
+                iteminfo       => $biblio->author,
+                diffbranch     => 1,
+            );
+        }
+    }
+    elsif ( $reserve->{'ResFound'} eq "Waiting" or $reserve->{'ResFound'} eq "Reserved" ) {
         if ( $reserve->{'ResFound'} eq "Waiting" ) {
             $template->param(
                 waiting      => ($userenv_branch eq $reserve->{'branchcode'} ? 1 : 0 ),
@@ -451,36 +458,19 @@ 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           => $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'},
-            itemnumber     => $reserve->{'itemnumber'},
-            reservenotes   => $reserve->{'reservenotes'},
-            reserve_id     => $reserve->{reserve_id},
-            bormessagepref => $holdmsgpreferences->{'transports'},
-        );
     } # else { ; }  # error?
+
+    # same params for Waiting or Reserved
+    $template->param(
+        found          => 1,
+        patron         => $patron,
+        barcode        => $barcode,
+        destbranch     => $reserve->{'branchcode'},
+        itemnumber     => $reserve->{'itemnumber'},
+        reservenotes   => $reserve->{'reservenotes'},
+        reserve_id     => $reserve->{reserve_id},
+        bormessagepref => $holdmsgpreferences->{'transports'},
+    );
 }
 
 # Error Messages
@@ -495,7 +485,6 @@ foreach my $code ( keys %$messages ) {
     elsif ( $code eq 'NotIssued' ) {
         $err{notissued} = 1;
         $err{msg} = '';
-        $err{msg} = $messages->{'IsPermanent'} if $messages->{'IsPermanent'};
     }
     elsif ( $code eq 'LocalUse' ) {
         $err{localuse} = 1;
@@ -520,12 +509,6 @@ foreach my $code ( keys %$messages ) {
         $err{withdrawn} = 1;
         $exit_required_p = 1 if C4::Context->preference("BlockReturnOfWithdrawnItems");
     }
-    elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
-        if ( $messages->{'IsPermanent'} ne $userenv_branch ) {
-            $err{ispermanent} = 1;
-            $err{msg}         = $messages->{'IsPermanent'};
-        }
-    }
     elsif ( $code eq 'WrongTransfer' ) {
         ;    # FIXME... anything to do here?
     }
@@ -589,20 +572,16 @@ foreach ( sort { $a <=> $b } keys %returneditems ) {
             } else {
                 $ri{return_overdue} = 1 if (DateTime->compare($duedate, $dropboxdate) == -1);
             }
-            $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'} } );
+            $ri{patron} = $patron,
+            $ri{borissuescount} = $patron->checkouts->count;
         }
         else {
             $ri{borrowernumber} = $riborrowernumber{$_};
         }
 
         my $item = Koha::Items->find({ barcode => $bar_code });
+        next unless $item; # FIXME The item has been deleted in the meantime,
+                           # we could handle that better displaying a message in the template
         my $biblio = $item->biblio;
         # FIXME pass $item to the template and we are done here...
         $ri{itembiblionumber}    = $biblio->biblionumber;
@@ -620,7 +599,7 @@ foreach ( sort { $a <=> $b } keys %returneditems ) {
         $ri{homebranch}          = $item->homebranch;
         $ri{holdingbranch}       = $item->holdingbranch;
 
-        $ri{location}         = $biblio->{'location'};
+        $ri{location} = $item->location;
         my $shelfcode = $ri{'location'};
         $ri{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
 
@@ -643,8 +622,9 @@ $template->param(
     AudioAlerts        => C4::Context->preference("AudioAlerts"),
 );
 
-$itemnumber = GetItemnumberFromBarcode( $barcode );
-if ( $itemnumber ) {
+my $item_from_barcode = Koha::Items->find({barcode => $barcode }); # How many times do we fetch this item?!?
+if ( $item_from_barcode ) {
+    $itemnumber = $item_from_barcode->itemnumber;
     my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
     if ( $holdingBranch and $collectionBranch ) {
         $holdingBranch //= '';