Bug 31307: Remove 'already_reserved' flag from opac-reserve.pl
[koha-ffzg.git] / opac / opac-reserve.pl
index a7391e2..a4cacfe 100755 (executable)
@@ -24,8 +24,8 @@ 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::Reserves qw( CanItemBeReserved CanBookBeReserved AddReserve GetReservesControlBranch IsAvailableForItemLevelRequest );
-use C4::Biblio qw( GetBiblioData GetFrameworkCode GetMarcBiblio );
+use C4::Reserves qw( CanItemBeReserved CanBookBeReserved AddReserve GetReservesControlBranch ItemsAnyAvailableAndNotRestricted IsAvailableForItemLevelRequest );
+use C4::Biblio qw( GetBiblioData GetFrameworkCode );
 use C4::Items qw( GetHostItemsInfo GetItemsInfo );
 use C4::Output qw( output_html_with_http_headers );
 use C4::Context;
@@ -150,14 +150,15 @@ foreach my $biblioNumber (@biblionumbers) {
 
     my @itemInfos = GetItemsInfo($biblioNumber);
 
-    my $marcrecord= GetMarcBiblio({ biblionumber => $biblioNumber });
+    my $biblio = Koha::Biblios->find( $biblioNumber );
+    next unless $biblio;
+
+    my $marcrecord = $biblio->metadata->record;
 
     # flag indicating existence of at least one item linked via a host record
-    my $hostitemsflag;
     # adding items linked via host biblios
     my @hostitemInfos = GetHostItemsInfo($marcrecord);
     if (@hostitemInfos){
-        $hostitemsflag =1;
         push (@itemInfos,@hostitemInfos);
     }
 
@@ -167,7 +168,6 @@ foreach my $biblioNumber (@biblionumbers) {
     }
 
     # Compute the priority rank.
-    my $biblio = Koha::Biblios->find( $biblioNumber );
     $biblioData->{object} = $biblio;
     my $holds = $biblio->holds;
     my $rank = $holds->count;
@@ -237,9 +237,9 @@ if ( $query->param('place_reserve') ) {
             $branch = $patron->branchcode;
         }
 
+        my $item = $itemNum ? Koha::Items->find( $itemNum ) : undef;
         # When choosing a specific item, the default pickup library should be dictated by the default hold policy
-        if ( ! C4::Context->preference("OPACAllowUserToChooseBranch") && $itemNum ) {
-            my $item = Koha::Items->find( $itemNum );
+        if ( ! C4::Context->preference("OPACAllowUserToChooseBranch") && $item ) {
             my $type = $item->effective_itemtype;
             my $rule = GetBranchItemRule( $patron->branchcode, $type );
 
@@ -254,8 +254,7 @@ if ( $query->param('place_reserve') ) {
         }
 
 #item may belong to a host biblio, if yes change biblioNum to hosts bilbionumber
-        if ( $itemNum ne '' ) {
-            my $item = Koha::Items->find( $itemNum );
+        if ( $item ) {
             my $hostbiblioNum = $item->biblio->biblionumber;
             if ( $hostbiblioNum ne $biblioNum ) {
                 $biblioNum = $hostbiblioNum;
@@ -273,14 +272,19 @@ if ( $query->param('place_reserve') ) {
             $startdate = $query->param("reserve_date_$biblioNum");
         }
 
-        my $expiration_date = $query->param("expiration_date_$biblioNum");
+        my $patron_expiration_date = $query->param("expiration_date_$biblioNum");
+
+        my $itemtype = $query->param('itemtype') || undef;
+        $itemtype = undef if $itemNum;
 
         my $rank = $biblioData->{rank};
-        if ( $itemNum ne '' ) {
-            $canreserve = 1 if CanItemBeReserved( $borrowernumber, $itemNum, $branch )->{status} eq 'OK';
+        my $patron = Koha::Patrons->find( $borrowernumber );
+        if ( $item ) {
+            $canreserve = 1 if CanItemBeReserved( $patron, $item, $branch )->{status} eq 'OK';
         }
         else {
-            $canreserve = 1 if CanBookBeReserved( $borrowernumber, $biblioNum, $branch )->{status} eq 'OK';
+            $canreserve = 1
+              if CanBookBeReserved( $borrowernumber, $biblioNum, $branch, { itemtype => $itemtype } )->{status} eq 'OK';
 
             # Inserts a null into the 'itemnumber' field of 'reserves' table.
             $itemNum = undef;
@@ -302,9 +306,6 @@ if ( $query->param('place_reserve') ) {
             }
         }
 
-        my $itemtype = $query->param('itemtype') || undef;
-        $itemtype = undef if $itemNum;
-
         # Here we actually do the reserveration. Stage 3.
         if ($canreserve) {
             my $reserve_id = AddReserve(
@@ -314,7 +315,7 @@ if ( $query->param('place_reserve') ) {
                     biblionumber     => $biblioNum,
                     priority         => $rank,
                     reservation_date => $startdate,
-                    expiration_date  => $expiration_date,
+                    expiration_date  => $patron_expiration_date,
                     notes            => $notes,
                     title            => $biblioData->{title},
                     itemnumber       => $itemNum,
@@ -458,7 +459,7 @@ foreach my $biblioNum (@biblionumbers) {
         }
     }
 
-    my @notforloan_avs = Koha::AuthorisedValues->search_by_koha_field({ kohafield => 'items.notforloan', frameworkcode => $frameworkcode });
+    my @notforloan_avs = Koha::AuthorisedValues->search_by_koha_field({ kohafield => 'items.notforloan', frameworkcode => $frameworkcode })->as_list;
     my $notforloan_label_of = { map { $_->authorised_value => $_->opac_description } @notforloan_avs };
 
     my $visible_items = { map { $_->itemnumber => $_ } $biblio->items->filter_by_visible_in_opac( { patron => $patron } )->as_list };
@@ -470,6 +471,12 @@ foreach my $biblioNum (@biblionumbers) {
     $biblioLoopIter{itemLoop} = [];
     my $numCopiesAvailable = 0;
     my $numCopiesOPACAvailable = 0;
+    # iterating through all items first to check if any of them available
+    # to pass this value further inside down to IsAvailableForItemLevelRequest to
+    # it's complicated logic to analyse.
+    # (before this loop was inside that sub loop so it was O(n^2) )
+    my $items_any_available;
+    $items_any_available = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblioNum, patron => $patron }) if $patron;
     foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
         my $itemNum = $itemInfo->{itemnumber};
         my $item = $visible_items->{$itemNum};
@@ -482,6 +489,7 @@ foreach my $biblioNum (@biblionumbers) {
         $itemLoopIter->{enumchron} = $itemInfo->{enumchron};
         $itemLoopIter->{ccode} = $itemInfo->{ccode};
         $itemLoopIter->{copynumber} = $itemInfo->{copynumber};
+        $itemLoopIter->{itemnotes} = $itemInfo->{itemnotes};
         if ($itemLevelTypes) {
             $itemLoopIter->{translated_description} = $itemInfo->{translated_description};
             $itemLoopIter->{itype} = $itemInfo->{itype};
@@ -498,8 +506,8 @@ foreach my $biblioNum (@biblionumbers) {
         # change the background color.
         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';
+            $itemLoopIter->{dateDue} = $issue->date_due;
+            $itemLoopIter->{onloan} = 'onloan';
         }
 
         # checking reserve
@@ -507,7 +515,7 @@ foreach my $biblioNum (@biblionumbers) {
 
         if ( my $first_hold = $holds->next ) {
             $itemLoopIter->{backgroundcolor} = 'reserved';
-            $itemLoopIter->{reservedate}     = output_pref({ dt => dt_from_string($first_hold->reservedate), dateonly => 1 }); # FIXME Should be formatted in the template
+            $itemLoopIter->{reservedate}     = $first_hold->reservedate;
             $itemLoopIter->{ExpectedAtLibrary}         = $first_hold->branchcode;
             $itemLoopIter->{waitingdate} = $first_hold->waitingdate;
         }
@@ -545,7 +553,6 @@ foreach my $biblioNum (@biblionumbers) {
 
         # if the items belongs to a host record, show link to host record
         if ( $itemInfo->{biblionumber} ne $biblioNum ) {
-            $biblioLoopIter{hostitemsflag}    = 1;
             $itemLoopIter->{hostbiblionumber} = $itemInfo->{biblionumber};
             $itemLoopIter->{hosttitle}        = Koha::Biblios->find( $itemInfo->{biblionumber} )->title;
         }
@@ -556,10 +563,11 @@ foreach my $biblioNum (@biblionumbers) {
         my $patron_unblessed = $patron->unblessed;
         my $branch = GetReservesControlBranch( $itemInfo, $patron_unblessed );
 
-        my $policy_holdallowed = !$itemLoopIter->{already_reserved};
-        $policy_holdallowed &&=
-            IsAvailableForItemLevelRequest($item, $patron) &&
-            CanItemBeReserved( $borrowernumber, $itemNum )->{status} eq 'OK';
+        # items_any_available defined outside of the current loop,
+        # so we avoiding loop inside IsAvailableForItemLevelRequest:
+        my $policy_holdallowed =
+            CanItemBeReserved( $patron, $item )->{status} eq 'OK' &&
+            IsAvailableForItemLevelRequest($item, $patron, undef, $items_any_available);
 
         if ($policy_holdallowed) {
             my $opac_hold_policy = Koha::CirculationRules->get_opacitemholds_policy( { item => $item, patron => $patron } );
@@ -625,6 +633,26 @@ foreach my $biblioNum (@biblionumbers) {
     $biblioLoopIter{holdable} &&= $status eq 'OK';
     $biblioLoopIter{already_patron_possession} = $status eq 'alreadypossession';
 
+    if ( $biblioLoopIter{holdable} and C4::Context->preference('AllowHoldItemTypeSelection') ) {
+        # build the allowed item types loop
+        my $rs = $biblio->items->search(
+            undef,
+            {   select => [ { distinct => 'itype' } ],
+                as     => 'item_type'
+            }
+        );
+
+        my @item_types =
+          grep { CanBookBeReserved( $borrowernumber, $biblioNum, $branch, { itemtype => $_ } )->{status} eq 'OK' }
+          $rs->get_column('item_type');
+
+        $biblioLoopIter{allowed_item_types} = \@item_types;
+    }
+
+    if ( $status eq 'recall' ) {
+        $biblioLoopIter{recall} = 1;
+    }
+
     # For multiple holds per record, if a patron has previously placed a hold,
     # the patron can only place more holds of the same type. That is, if the
     # patron placed a record level hold, all the holds the patron places must