Bug 30952: Fix toolbar positioning and spacing
[koha-ffzg.git] / opac / opac-reserve.pl
index 724a2ba..ec3d6ad 100755 (executable)
@@ -24,7 +24,7 @@ use Modern::Perl;
 use CGI qw ( -utf8 );
 use C4::Auth qw( get_template_and_user );
 use C4::Koha qw( getitemtypeimagelocation getitemtypeimagesrc );
-use C4::Circulation qw( GetBranchItemRule GetTransfers );
+use C4::Circulation qw( GetBranchItemRule );
 use C4::Reserves qw( CanItemBeReserved CanBookBeReserved AddReserve GetReservesControlBranch ItemsAnyAvailableAndNotRestricted IsAvailableForItemLevelRequest );
 use C4::Biblio qw( GetBiblioData GetFrameworkCode );
 use C4::Output qw( output_html_with_http_headers );
@@ -60,12 +60,6 @@ my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
     }
 );
 
-my ($show_holds_count, $show_priority);
-for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
-    m/holds/o and $show_holds_count = 1;
-    m/priority/ and $show_priority = 1;
-}
-
 my $patron = Koha::Patrons->find( $borrowernumber, { prefetch => ['categorycode'] } );
 my $category = $patron->category;
 
@@ -78,18 +72,6 @@ unless ( $can_place_hold_if_available_at_pickup ) {
     }
 }
 
-# check if this user can place a reserve, -1 means use sys pref, 0 means dont block, 1 means block
-if ( $category->effective_BlockExpiredPatronOpacActions ) {
-
-    if ( $patron->is_expired ) {
-
-        # cannot reserve, their card has expired and the rules set mean this is not allowed
-        $template->param( message => 1, expired_patron => 1 );
-        output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
-        exit;
-    }
-}
-
 # Pass through any reserve charge
 my $reservefee = $category->reservefee;
 if ( $reservefee > 0){
@@ -129,62 +111,77 @@ if (($#biblionumbers < 0) && (! $query->param('place_reserve'))) {
     exit;
 }
 
-
-# pass the pickup branch along....
-my $branch = $query->param('branch') || $patron->branchcode || C4::Context->userenv->{branch} || '' ;
-$template->param( branch => $branch );
-
 #
 #
-# Build hashes of the requested biblio(item)s and items.
+# Here we check that the borrower can actually make reserves Stage 1.
 #
 #
+my $noreserves     = 0;
+if ( $category->effective_BlockExpiredPatronOpacActions ) {
+    if ( $patron->is_expired ) {
+        # cannot reserve, their card has expired and the rules set mean this is not allowed
+        $noreserves = 1;
+        $template->param( message => 1, expired_patron => 1 );
+    }
+}
 
-my %biblioDataHash; # Hash of biblionumber to biblio/biblioitems record.
-foreach my $biblioNumber (@biblionumbers) {
-
-    my $biblioData = GetBiblioData($biblioNumber);
-    $biblioDataHash{$biblioNumber} = $biblioData;
+my $maxoutstanding = C4::Context->preference("maxoutstanding");
+my $amountoutstanding = $patron->account->balance;
+if ( $amountoutstanding && ($amountoutstanding > $maxoutstanding) ) {
+    my $amount = sprintf "%.02f", $amountoutstanding;
+    $template->param( message => 1 );
+    $noreserves = 1;
+    $template->param( too_much_oweing => $amount );
+}
 
-    my $biblio = Koha::Biblios->find( $biblioNumber );
-    next unless $biblio;
+if ( $patron->gonenoaddress && ($patron->gonenoaddress == 1) ) {
+    $noreserves = 1;
+    $template->param(
+        message => 1,
+        GNA     => 1
+    );
+}
 
-    my $marcrecord = $biblio->metadata->record;
+if ( $patron->lost && ($patron->lost == 1) ) {
+    $noreserves = 1;
+    $template->param(
+        message => 1,
+        lost    => 1
+    );
+}
 
-    my $items = Koha::Items->search_ordered(
-        [
-            biblionumber => $biblioNumber,
-            'me.itemnumber' => {
-                -in => [
-                    $biblio->host_items->get_column('itemnumber')
-                ]
-            }
-        ],
-        { prefetch => [ 'issue', 'homebranch', 'holdingbranch' ] }
-    )->filter_by_visible_in_opac({ patron => $patron });
+if ( $patron->is_debarred ) {
+    $noreserves = 1;
+    $template->param(
+        message          => 1,
+        debarred         => 1,
+        debarred_comment => $patron->debarredcomment,
+        debarred_date    => $patron->debarred,
+    );
+}
 
-    $biblioData->{items} = [$items->as_list]; # FIXME Potentially a lot in memory here!
+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 => $holds->count );
+}
 
-    # Compute the priority rank.
-    $biblioData->{object} = $biblio;
-    my $holds = $biblio->holds;
-    my $rank = $holds->count;
-    $biblioData->{reservecount} = 1;    # new reserve
-    while ( my $hold = $holds->next ) {
-        if ( $hold->is_waiting ) {
-            $rank--;
-        }
-        else {
-            $biblioData->{reservecount}++;
-        }
-    }
-    $biblioData->{rank} = $rank + 1;
+if( $noreserves ){
+    output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
+    exit;
 }
 
+
+# pass the pickup branch along....
+my $branch = $query->param('branch') || $patron->branchcode || C4::Context->userenv->{branch} || '' ;
+$template->param( branch => $branch );
+
 #
 #
-# If this is the second time through this script, it
-# means we are carrying out the hold request, possibly
+# Here we are carrying out the hold request, possibly
 # with a specific item for each biblionumber.
 #
 #
@@ -252,17 +249,11 @@ if ( $query->param('place_reserve') ) {
             }
         }
 
-#item may belong to a host biblio, if yes change biblioNum to hosts bilbionumber
+        # if we have an item, we are placing the hold on the item's bib, in case of analytics
         if ( $item ) {
-            my $hostbiblioNum = $item->biblio->biblionumber;
-            if ( $hostbiblioNum ne $biblioNum ) {
-                $biblioNum = $hostbiblioNum;
-            }
+            $biblioNum = $item->biblionumber;
         }
 
-        my $biblioData = $biblioDataHash{$biblioNum};
-        my $found;
-
         # Check for user supplied reserve date
         my $startdate;
         if (   C4::Context->preference('AllowHoldDateInFuture')
@@ -276,8 +267,8 @@ if ( $query->param('place_reserve') ) {
         my $itemtype = $query->param('itemtype') || undef;
         $itemtype = undef if $itemNum;
 
-        my $rank = $biblioData->{rank};
-        my $patron = Koha::Patrons->find( $borrowernumber );
+        my $biblio = Koha::Biblios->find($biblioNum);
+        my $rank = $biblio->holds->search( { found => [ { "!=" => "W" }, undef ] } )->count + 1;
         if ( $item ) {
             $canreserve = 1 if CanItemBeReserved( $patron, $item, $branch )->{status} eq 'OK';
         }
@@ -316,9 +307,9 @@ if ( $query->param('place_reserve') ) {
                     reservation_date => $startdate,
                     expiration_date  => $patron_expiration_date,
                     notes            => $notes,
-                    title            => $biblioData->{title},
+                    title            => $biblio->title,
                     itemnumber       => $itemNum,
-                    found            => $found,
+                    found            => undef,
                     itemtype         => $itemtype,
                 }
             );
@@ -333,66 +324,48 @@ if ( $query->param('place_reserve') ) {
 
 #
 #
-# Here we check that the borrower can actually make reserves Stage 1.
+# Build hashes of the requested biblio(item)s and items.
 #
 #
-my $noreserves     = 0;
-my $maxoutstanding = C4::Context->preference("maxoutstanding");
-$template->param( noreserve => 1 ) unless $maxoutstanding;
-my $amountoutstanding = $patron->account->balance;
-if ( $amountoutstanding && ($amountoutstanding > $maxoutstanding) ) {
-    my $amount = sprintf "%.02f", $amountoutstanding;
-    $template->param( message => 1 );
-    $noreserves = 1;
-    $template->param( too_much_oweing => $amount );
-}
 
-if ( $patron->gonenoaddress && ($patron->gonenoaddress == 1) ) {
-    $noreserves = 1;
-    $template->param(
-        message => 1,
-        GNA     => 1
-    );
-}
+my %biblioDataHash; # Hash of biblionumber to biblio/biblioitems record.
+foreach my $biblioNumber (@biblionumbers) {
 
-if ( $patron->lost && ($patron->lost == 1) ) {
-    $noreserves = 1;
-    $template->param(
-        message => 1,
-        lost    => 1
-    );
-}
+    my $biblioData = GetBiblioData($biblioNumber);
+    $biblioDataHash{$biblioNumber} = $biblioData;
 
-if ( $patron->is_debarred ) {
-    $noreserves = 1;
-    $template->param(
-        message          => 1,
-        debarred         => 1,
-        debarred_comment => $patron->debarredcomment,
-        debarred_date    => $patron->debarred,
-    );
-}
+    my $biblio = Koha::Biblios->find( $biblioNumber );
+    next unless $biblio;
 
-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 => $holds->count );
-}
+    my $items = Koha::Items->search_ordered(
+        [
+            biblionumber => $biblioNumber,
+            'me.itemnumber' => {
+                -in => [
+                    $biblio->host_items->get_column('itemnumber')
+                ]
+            }
+        ],
+        { prefetch => [ 'issue', 'homebranch', 'holdingbranch' ] }
+    )->filter_by_visible_in_opac({ patron => $patron });
 
-unless ( $noreserves ) {
-    my $requested_reserves_count = scalar( @biblionumbers );
-    if ( $maxreserves && ( $reserves_count + $requested_reserves_count > $maxreserves ) ) {
-        $template->param( new_reserves_allowed => $maxreserves - $reserves_count );
-    }
+    $biblioData->{items} = [$items->as_list]; # FIXME Potentially a lot in memory here!
+
+    # Compute the priority rank.
+    $biblioData->{object} = $biblio;
+    my $reservecount = $biblio->holds->search({ found => [ {"!=" => "W"},undef] })->count;
+    $biblioData->{reservecount} = $reservecount;
+    $biblioData->{rank} = $reservecount + 1;
 }
 
-unless ($noreserves) {
-    $template->param( select_item_types => 1 );
+
+my $requested_reserves_count = scalar( @biblionumbers );
+if ( $maxreserves && ( $reserves_count + $requested_reserves_count > $maxreserves ) ) {
+    $template->param( new_reserves_allowed => $maxreserves - $reserves_count );
 }
 
+$template->param( select_item_types => 1 );
+
 
 #
 #
@@ -441,7 +414,6 @@ foreach my $biblioNum (@biblionumbers) {
     $biblioLoopIter{rank} = $biblioData->{rank};
     $biblioLoopIter{reservecount} = $biblioData->{reservecount};
     $biblioLoopIter{already_reserved} = $biblioData->{already_reserved};
-    $biblioLoopIter{reqholdnotes}=0; #TODO: For future use
 
     if (!$itemLevelTypes && $biblioData->{itemtype}) {
         $biblioLoopIter{translated_description} = $itemtypes->{$biblioData->{itemtype}}{translated_description};
@@ -481,12 +453,11 @@ foreach my $biblioNum (@biblionumbers) {
         $item_info->{checkout} = $item->checkout;
 
         # Check of the transferred documents
-        my ( $transfertwhen, $transfertfrom, $transfertto ) =
-          GetTransfers($item->itemnumber);
-        if ( $transfertwhen && ($transfertwhen ne '') ) {
-            $item_info->{transfertwhen} = $transfertwhen;
-            $item_info->{transfertfrom} = $transfertfrom;
-            $item_info->{transfertto} = $transfertto;
+        my $transfer = $item->get_transfer;
+        if ( $transfer && $transfer->in_transit ) {
+            $item_info->{transfertwhen} = $transfer->datesent;
+            $item_info->{transfertfrom} = $transfer->frombranch;
+            $item_info->{transfertto} = $transfer->tobranch;
             $item_info->{nocancel} = 1;
         }