Bug 12001: Move GetMemberAccountRecords to the Koha namespace
[koha.git] / opac / opac-reserve.pl
index 740f405..2be6991 100755 (executable)
@@ -32,9 +32,14 @@ use C4::Context;
 use C4::Members;
 use C4::Overdues;
 use C4::Debug;
+
 use Koha::AuthorisedValues;
+use Koha::Biblios;
 use Koha::DateUtils;
+use Koha::IssuingRules;
 use Koha::Items;
+use Koha::ItemTypes;
+use Koha::Checkouts;
 use Koha::Libraries;
 use Koha::Patrons;
 use Date::Calc qw/Today Date_to_Days/;
@@ -71,15 +76,14 @@ sub get_out {
        exit;
 }
 
-# get borrower information ....
-my ( $borr ) = GetMember( borrowernumber => $borrowernumber );
 my $patron = Koha::Patrons->find( $borrowernumber );
 
 my $can_place_hold_if_available_at_pickup = C4::Context->preference('OPACHoldsIfAvailableAtPickup');
 unless ( $can_place_hold_if_available_at_pickup ) {
     my @patron_categories = split '\|', C4::Context->preference('OPACHoldsIfAvailableAtPickupExceptions');
     if ( @patron_categories ) {
-        $can_place_hold_if_available_at_pickup = grep /$borr->{categorycode}/, @patron_categories;
+        my $categorycode = $patron->categorycode;
+        $can_place_hold_if_available_at_pickup = grep /^$categorycode$/, @patron_categories;
     }
 }
 
@@ -100,7 +104,7 @@ if ( $reservefee > 0){
     $template->param( RESERVE_CHARGE => sprintf("%.2f",$reservefee));
 }
 
-my $itemTypes = GetItemTypes();
+my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
 
 # There are two ways of calling this script, with a single biblio num
 # or multiple biblio nums.
@@ -133,7 +137,7 @@ if (($#biblionumbers < 0) && (! $query->param('place_reserve'))) {
 
 
 # pass the pickup branch along....
-my $branch = $query->param('branch') || $borr->{'branchcode'} || C4::Context->userenv->{branch} || '' ;
+my $branch = $query->param('branch') || $patron->branchcode || C4::Context->userenv->{branch} || '' ;
 $template->param( branch => $branch );
 
 # Is the person allowed to choose their branch
@@ -156,7 +160,7 @@ foreach my $biblioNumber (@biblionumbers) {
 
     my @itemInfos = GetItemsInfo($biblioNumber);
 
-    my $marcrecord= GetMarcBiblio($biblioNumber);
+    my $marcrecord= GetMarcBiblio({ biblionumber => $biblioNumber });
 
     # flag indicating existence of at least one item linked via a host record
     my $hostitemsflag;
@@ -198,7 +202,7 @@ foreach my $biblioNumber (@biblionumbers) {
 if ( $query->param('place_reserve') ) {
     my $reserve_cnt = 0;
     if ($maxreserves) {
-        $reserve_cnt = GetReservesFromBorrowernumber( $borrowernumber );
+        $reserve_cnt = $patron->holds->count;
     }
 
     # List is composed of alternating biblio/item/branch
@@ -238,12 +242,13 @@ if ( $query->param('place_reserve') ) {
         my $singleBranchMode = Koha::Libraries->search->count == 1;
         if ( $singleBranchMode || !$OPACChooseBranch )
         {    # single branch mode or disabled user choosing
-            $branch = $borr->{'branchcode'};
+            $branch = $patron->branchcode;
         }
 
 #item may belong to a host biblio, if yes change biblioNum to hosts bilbionumber
         if ( $itemNum ne '' ) {
-            my $hostbiblioNum = GetBiblionumberFromItemnumber($itemNum);
+            my $item = Koha::Items->find( $itemNum );
+            my $hostbiblioNum = $item->biblio->biblionumber;
             if ( $hostbiblioNum ne $biblioNum ) {
                 $biblioNum = $hostbiblioNum;
             }
@@ -325,7 +330,7 @@ if ( $query->param('place_reserve') ) {
 my $noreserves     = 0;
 my $maxoutstanding = C4::Context->preference("maxoutstanding");
 $template->param( noreserve => 1 ) unless $maxoutstanding;
-my ( $amountoutstanding ) = GetMemberAccountRecords($borrowernumber);
+my $amountoutstanding = $patron->account->balance;
 if ( $amountoutstanding && ($amountoutstanding > $maxoutstanding) ) {
     my $amount = sprintf "%.02f", $amountoutstanding;
     $template->param( message => 1 );
@@ -333,7 +338,7 @@ if ( $amountoutstanding && ($amountoutstanding > $maxoutstanding) ) {
     $template->param( too_much_oweing => $amount );
 }
 
-if ( $borr->{gonenoaddress} && ($borr->{gonenoaddress} == 1) ) {
+if ( $patron->gonenoaddress && ($patron->gonenoaddress == 1) ) {
     $noreserves = 1;
     $template->param(
         message => 1,
@@ -341,7 +346,7 @@ if ( $borr->{gonenoaddress} && ($borr->{gonenoaddress} == 1) ) {
     );
 }
 
-if ( $borr->{lost} && ($borr->{lost} == 1) ) {
+if ( $patron->lost && ($patron->lost == 1) ) {
     $noreserves = 1;
     $template->param(
         message => 1,
@@ -349,23 +354,23 @@ if ( $borr->{lost} && ($borr->{lost} == 1) ) {
     );
 }
 
-if ( Koha::Patrons->find( $borrowernumber )->is_debarred ) {
+if ( $patron->is_debarred ) {
     $noreserves = 1;
     $template->param(
         message          => 1,
         debarred         => 1,
-        debarred_comment => $borr->{debarredcomment},
-        debarred_date    => $borr->{debarred},
+        debarred_comment => $patron->debarredcomment,
+        debarred_date    => $patron->debarred,
     );
 }
 
-my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
-my $reserves_count = scalar(@reserves);
-$template->param( RESERVES => \@reserves );
+my $holds = $patron->holds;
+my $reserves_count = $holds->count;
+$template->param( RESERVES => $holds->unblessed );
 if ( $maxreserves && ( $reserves_count >= $maxreserves ) ) {
     $template->param( message => 1 );
     $noreserves = 1;
-    $template->param( too_many_reserves => scalar(@reserves));
+    $template->param( too_many_reserves => $holds->count );
 }
 
 unless ( $noreserves ) {
@@ -397,7 +402,7 @@ $template->param('item_level_itypes' => $itemLevelTypes);
 foreach my $biblioNum (@biblionumbers) {
 
     my @not_available_at = ();
-    my $record = GetMarcBiblio($biblioNum);
+    my $record = GetMarcBiblio({ biblionumber => $biblioNum });
     # Init the bib item with the choices for branch pickup
     my %biblioLoopIter;
 
@@ -419,14 +424,14 @@ foreach my $biblioNum (@biblionumbers) {
     $biblioLoopIter{reqholdnotes}=0; #TODO: For future use
 
     if (!$itemLevelTypes && $biblioData->{itemtype}) {
-        $biblioLoopIter{translated_description} = $itemTypes->{$biblioData->{itemtype}}{translated_description};
-        $biblioLoopIter{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$biblioData->{itemtype}}{imageurl};
+        $biblioLoopIter{translated_description} = $itemtypes->{$biblioData->{itemtype}}{translated_description};
+        $biblioLoopIter{imageurl} = getitemtypeimagesrc() . "/". $itemtypes->{$biblioData->{itemtype}}{imageurl};
     }
 
     foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
         if ($itemLevelTypes && $itemInfo->{itype}) {
-            $itemInfo->{translated_description} = $itemTypes->{$itemInfo->{itype}}{translated_description};
-            $itemInfo->{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$itemInfo->{itype}}{imageurl};
+            $itemInfo->{translated_description} = $itemtypes->{$itemInfo->{itype}}{translated_description};
+            $itemInfo->{imageurl} = getitemtypeimagesrc() . "/". $itemtypes->{$itemInfo->{itype}}{imageurl};
         }
 
         if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
@@ -442,6 +447,7 @@ foreach my $biblioNum (@biblionumbers) {
     my $numCopiesOPACAvailable = 0;
     foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
         my $itemNum = $itemInfo->{itemnumber};
+        my $item = Koha::Items->find( $itemNum );
         my $itemLoopIter = {};
 
         $itemLoopIter->{itemnumber} = $itemNum;
@@ -464,26 +470,18 @@ foreach my $biblioNum (@biblionumbers) {
 
         # If the item is currently on loan, we display its return date and
         # change the background color.
-        my $issues= GetItemIssue($itemNum);
-        if ( $issues->{'date_due'} ) {
-            $itemLoopIter->{dateDue} = output_pref({ dt => dt_from_string($issues->{date_due}, 'sql'), as_due_date => 1 });
+        my $issue = Koha::Checkouts->find( { itemnumber => $itemNum } );
+        if ( $issue ) {
+            $itemLoopIter->{dateDue} = output_pref({ dt => dt_from_string($issue->date_due, 'sql'), as_due_date => 1 });
             $itemLoopIter->{backgroundcolor} = 'onloan';
         }
 
         # checking reserve
-        my $item = Koha::Items->find( $itemNum );
-        my $holds = $item->holds_placed_before_today;
-
-        # the item could be reserved for this borrower vi a host record, flag this
-        my $reservedfor = q||;
+        my $holds = $item->current_holds;
 
         if ( my $first_hold = $holds->next ) {
-            my $ItemBorrowerReserveInfo = GetMember( borrowernumber => $first_hold->borrowernumber );
             $itemLoopIter->{backgroundcolor} = 'reserved';
             $itemLoopIter->{reservedate}     = output_pref({ dt => dt_from_string($first_hold->reservedate), dateonly => 1 }); # FIXME Should be formatted in the template
-            $itemLoopIter->{ReservedForBorrowernumber} = $first_hold->borrowernumber;
-            $itemLoopIter->{ReservedForSurname}        = $ItemBorrowerReserveInfo->{'surname'};
-            $itemLoopIter->{ReservedForFirstname}      = $ItemBorrowerReserveInfo->{'firstname'};
             $itemLoopIter->{ExpectedAtLibrary}         = $first_hold->branchcode;
             $itemLoopIter->{waitingdate} = $first_hold->waitingdate;
         }
@@ -523,24 +521,26 @@ foreach my $biblioNum (@biblionumbers) {
         if ( $itemInfo->{biblionumber} ne $biblioNum ) {
             $biblioLoopIter{hostitemsflag}    = 1;
             $itemLoopIter->{hostbiblionumber} = $itemInfo->{biblionumber};
-            $itemLoopIter->{hosttitle}        = GetBiblioData( $itemInfo->{biblionumber} )->{title};
+            $itemLoopIter->{hosttitle}        = Koha::Biblios->find( $itemInfo->{biblionumber} )->title;
         }
 
         # If there is no loan, return and transfer, we show a checkbox.
         $itemLoopIter->{notforloan} = $itemLoopIter->{notforloan} || 0;
 
-        my $branch = GetReservesControlBranch( $itemInfo, $borr );
+        my $patron_unblessed = $patron->unblessed;
+        my $branch = GetReservesControlBranch( $itemInfo, $patron_unblessed );
 
         my $policy_holdallowed = !$itemLoopIter->{already_reserved};
         $policy_holdallowed &&=
-            IsAvailableForItemLevelRequest($itemInfo,$borr) &&
+            IsAvailableForItemLevelRequest($itemInfo,$patron_unblessed) &&
             CanItemBeReserved($borrowernumber,$itemNum) eq 'OK';
 
         if ($policy_holdallowed) {
-            if ( my $hold_allowed = OPACItemHoldsAllowed( $itemInfo, $borr ) ) {
+            my $opac_hold_policy = Koha::IssuingRules->get_opacitemholds_policy( { item => $item, patron => $patron } );
+            if ( $opac_hold_policy ne 'N' ) { # If Y or F
                 $itemLoopIter->{available} = 1;
                 $numCopiesOPACAvailable++;
-                $biblioLoopIter{force_hold} = 1 if $hold_allowed eq 'F';
+                $biblioLoopIter{force_hold} = 1 if $opac_hold_policy eq 'F';
             }
             $numCopiesAvailable++;
 
@@ -553,7 +553,7 @@ foreach my $biblioNum (@biblionumbers) {
             }
         }
 
-        $itemLoopIter->{imageurl} = getitemtypeimagelocation( 'opac', $itemTypes->{ $itemInfo->{itype} }{imageurl} );
+        $itemLoopIter->{imageurl} = getitemtypeimagelocation( 'opac', $itemtypes->{ $itemInfo->{itype} }{imageurl} );
 
     # Show serial enumeration when needed
         if ($itemLoopIter->{enumchron}) {