Bug 17642: Add and use get_descriptions_by_koha_field
[koha.git] / circ / returns.pl
index 7442206..5927759 100755 (executable)
@@ -44,9 +44,10 @@ use C4::Reserves;
 use C4::Biblio;
 use C4::Items;
 use C4::Members;
-use C4::Branch; # GetBranches GetBranchName
+use C4::Members::Messaging;
 use C4::Koha;   # FIXME : is it still useful ?
 use C4::RotatingCollections;
+use Koha::AuthorisedValues;
 use Koha::DateUtils;
 use Koha::Calendar;
 
@@ -82,7 +83,6 @@ if ( $query->param('print_slip') ) {
 
 #####################
 #Global vars
-my $branches = GetBranches();
 my $printers = GetPrinters();
 my $userenv = C4::Context->userenv;
 my $userenv_branch = $userenv->{'branch'} // '';
@@ -142,10 +142,10 @@ if ($query->param('WT-itemNumber')){
        updateWrongTransfer ($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
 }
 
-if ( $query->param('resbarcode') ) {
+if ( $query->param('reserve_id') ) {
     my $item           = $query->param('itemnumber');
     my $borrowernumber = $query->param('borrowernumber');
-    my $resbarcode     = $query->param('resbarcode');
+    my $reserve_id     = $query->param('reserve_id');
     my $diffBranchReturned = $query->param('diffBranch');
     my $iteminfo   = GetBiblioFromItemNumber($item);
     my $cancel_reserve = $query->param('cancel_reserve');
@@ -153,12 +153,12 @@ if ( $query->param('resbarcode') ) {
     $iteminfo->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $iteminfo->{'itype'} : $iteminfo->{'itemtype'};
 
     if ( $cancel_reserve ) {
-        CancelReserve({ borrowernumber => $borrowernumber, itemnumber => $item, charge_cancel_fee => !$forgivemanualholdsexpire });
+        CancelReserve({ reserve_id => $reserve_id, charge_cancel_fee => !$forgivemanualholdsexpire });
     } else {
         my $diffBranchSend = ($userenv_branch ne $diffBranchReturned) ? $diffBranchReturned : undef;
         # diffBranchSend tells ModReserveAffect whether document is expected in this library or not,
         # i.e., whether to apply waiting status
-        ModReserveAffect( $item, $borrowernumber, $diffBranchSend);
+        ModReserveAffect( $item, $borrowernumber, $diffBranchSend, $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($item);
@@ -171,12 +171,12 @@ if ( $query->param('resbarcode') ) {
             itemnumber     => $iteminfo->{'itemnumber'},
             itembiblionumber => $iteminfo->{'biblionumber'},
             iteminfo       => $iteminfo->{'author'},
-            tobranchname   => GetBranchName($messages->{'transfert'}),
             name           => $name,
             borrowernumber => $borrowernumber,
             borcnum        => $borr->{'cardnumber'},
             borfirstname   => $borr->{'firstname'},
             borsurname     => $borr->{'surname'},
+            borcategory    => $borr->{'description'},
             diffbranch     => 1,
         );
     }
@@ -249,6 +249,7 @@ if ($canceltransfer){
 }
 
 # actually return book and prepare item table.....
+my $returnbranch;
 if ($barcode) {
     $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
     $barcode = barcodedecode($barcode) if C4::Context->preference('itemBarcodeInputFilter');
@@ -275,13 +276,11 @@ if ($barcode) {
 
     # make sure return branch respects home branch circulation rules, default to homebranch
     my $hbr = GetBranchItemRule($biblio->{'homebranch'}, $itemtype ? $itemtype->itemtype : undef )->{'returnbranch'} || "homebranch";
-    my $returnbranch = $biblio->{$hbr} ;
+    $returnbranch = $biblio->{$hbr};
 
     my $materials = $biblio->{'materials'};
-    my $avcode = GetAuthValCode('items.materials');
-    if ($avcode) {
-        $materials = GetKohaAuthorisedValueLib($avcode, $materials);
-    }
+    my $descriptions = Koha::AuthorisedValues->get_description_by_koha_field({frameworkcode => '', kohafield =>'items.materials', authorised_value => $materials });
+    $materials = $descriptions->{lib} // '';
 
     $template->param(
         title            => $biblio->{'title'},
@@ -400,13 +399,13 @@ if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
     );
 
     my $reserve    = $messages->{'ResFound'};
-    my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
     my $borr = C4::Members::GetMember( borrowernumber => $reserve->{'borrowernumber'} );
     my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
     $template->param(
             wname           => $name,
             wborfirstname   => $borr->{'firstname'},
             wborsurname     => $borr->{'surname'},
+            wborcategory    => $borr->{'description'},
             wbortitle       => $borr->{'title'},
             wborphone       => $borr->{'phone'},
             wboremail       => $borr->{'email'},
@@ -426,9 +425,8 @@ if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
 #
 if ( $messages->{'ResFound'}) {
     my $reserve    = $messages->{'ResFound'};
-    my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
     my $borr = C4::Members::GetMember( borrowernumber => $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" ) {
             $template->param(
@@ -438,7 +436,7 @@ if ( $messages->{'ResFound'}) {
             $template->param(
                 intransit    => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
                 transfertodo => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
-                resbarcode   => $barcode,
+                reserve_id   => $reserve->{reserve_id},
                 reserved     => 1,
             );
         }
@@ -446,11 +444,10 @@ if ( $messages->{'ResFound'}) {
         # same params for Waiting or Reserved
         $template->param(
             found          => 1,
-            currentbranch  => $branches->{$userenv_branch}->{'branchname'},
-            destbranchname => $branches->{ $reserve->{'branchcode'} }->{'branchname'},
             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'},
@@ -467,6 +464,7 @@ if ( $messages->{'ResFound'}) {
             borrowernumber => $reserve->{'borrowernumber'},
             itemnumber     => $reserve->{'itemnumber'},
             reservenotes   => $reserve->{'reservenotes'},
+            bormessagepref => $holdmsgpreferences->{'transports'},
         );
     } # else { ; }  # error?
 }
@@ -483,7 +481,7 @@ foreach my $code ( keys %$messages ) {
     elsif ( $code eq 'NotIssued' ) {
         $err{notissued} = 1;
         $err{msg} = '';
-        $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'} if $messages->{'IsPermanent'};
+        $err{msg} = $messages->{'IsPermanent'} if $messages->{'IsPermanent'};
     }
     elsif ( $code eq 'LocalUse' ) {
         $err{localuse} = 1;
@@ -510,8 +508,7 @@ foreach my $code ( keys %$messages ) {
     elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
         if ( $messages->{'IsPermanent'} ne $userenv_branch ) {
             $err{ispermanent} = 1;
-            $err{msg}         =
-              $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
+            $err{msg}         = $messages->{'IsPermanent'};
         }
     }
     elsif ( $code eq 'WrongTransfer' ) {
@@ -554,7 +551,8 @@ $template->param( errmsgloop => \@errmsgloop );
 my $returned_counter = ( C4::Context->preference('numReturnedItemsToShow') ) ? C4::Context->preference('numReturnedItemsToShow') : 8;
 my $count = 0;
 my @riloop;
-my $shelflocations = GetKohaAuthorisedValues('items.location','');
+my $shelflocations =
+  { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' } ) };
 foreach ( sort { $a <=> $b } keys %returneditems ) {
     my %ri;
     if ( $count++ < $returned_counter ) {
@@ -599,6 +597,7 @@ foreach ( sort { $a <=> $b } keys %returneditems ) {
         $ri{itemnote}            = $biblio->{'itemnotes'};
         $ri{itemnotes_nonpublic} = $item->{'itemnotes_nonpublic'};
         $ri{ccode}               = $biblio->{'ccode'};
+        $ri{enumchron}           = $biblio->{'enumchron'};
         $ri{itemnumber}          = $biblio->{'itemnumber'};
         $ri{barcode}             = $bar_code;
         $ri{homebranch}          = $item->{'homebranch'};
@@ -614,18 +613,9 @@ foreach ( sort { $a <=> $b } keys %returneditems ) {
     }
     push @riloop, \%ri;
 }
-my ($genbrname, $genprname);
-if (my $b = $branches->{$userenv_branch}) {
-    $genbrname = $b->{'branchname'};
-}
-if (my $p = $printers->{$printer}) {
-    $genprname = $p->{'printername'};
-}
+
 $template->param(
     riloop         => \@riloop,
-    genbrname      => $genbrname,
-    genprname      => $genprname,
-    branchname     => $genbrname,
     printer        => $printer,
     errmsgloop     => \@errmsgloop,
     exemptfine     => $exemptfine,
@@ -639,14 +629,17 @@ $template->param(
 
 $itemnumber = GetItemnumberFromBarcode( $barcode );
 if ( $itemnumber ) {
-   my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
-    $holdingBranch //= '';
-    $collectionBranch //= '';
-    if ( ! ( $holdingBranch eq $collectionBranch ) ) {
-        $template->param(
-          collectionItemNeedsTransferred => 1,
-          collectionBranch => GetBranchName($collectionBranch),
-        );
+    my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
+    if ( $holdingBranch and $collectionBranch ) {
+        $holdingBranch //= '';
+        $collectionBranch //= $returnbranch;
+        if ( ! ( $holdingBranch eq $collectionBranch ) ) {
+            $template->param(
+              collectionItemNeedsTransferred => 1,
+              collectionBranch => $collectionBranch,
+              itemnumber => $itemnumber,
+            );
+        }
     }
 }