Bug 15548: Move new patron related code to Patron*
[koha.git] / opac / opac-reserve.pl
index c1b5387..0f753d8 100755 (executable)
@@ -28,27 +28,32 @@ use C4::Reserves;
 use C4::Biblio;
 use C4::Items;
 use C4::Output;
-use C4::Dates qw/format_date/;
 use C4::Context;
 use C4::Members;
 use C4::Branch; # GetBranches
 use C4::Overdues;
 use C4::Debug;
 use Koha::DateUtils;
-use Koha::Borrower::Debarments qw(IsDebarred);
+use Koha::Libraries;
+use Koha::Patron::Debarments qw(IsDebarred);
 use Date::Calc qw/Today Date_to_Days/;
-# use Data::Dumper;
 
 my $maxreserves = C4::Context->preference("maxreserves");
 
 my $query = new CGI;
+
+# if RequestOnOpac (for placing holds) is disabled, leave immediately
+if ( ! C4::Context->preference('RequestOnOpac') ) {
+    print $query->redirect("/cgi-bin/koha/errors/404.pl");
+    exit;
+}
+
 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
     {
         template_name   => "opac-reserve.tt",
         query           => $query,
         type            => "opac",
         authnotrequired => 0,
-        flagsrequired   => { borrow => 1 },
         debug           => 1,
     }
 );
@@ -215,6 +220,7 @@ if ( $query->param('place_reserve') ) {
         &get_out($query, $cookie, $template->output);
     }
 
+    my $failed_holds = 0;
     while (@selectedItems) {
         my $biblioNum = shift(@selectedItems);
         my $itemNum   = shift(@selectedItems);
@@ -222,7 +228,7 @@ if ( $query->param('place_reserve') ) {
 
         my $canreserve = 0;
 
-        my $singleBranchMode = C4::Context->preference("singleBranchMode");
+        my $singleBranchMode = Koha::Libraries->search->count == 1;
         if ( $singleBranchMode || !$OPACChooseBranch )
         {    # single branch mode or disabled user choosing
             $branch = $borr->{'branchcode'};
@@ -277,19 +283,20 @@ if ( $query->param('place_reserve') ) {
 
         # Here we actually do the reserveration. Stage 3.
         if ($canreserve) {
-            AddReserve(
+            my $reserve_id = AddReserve(
                 $branch,      $borrowernumber,
-                $biblioNum,   'a',
+                $biblioNum,
                 [$biblioNum], $rank,
                 $startdate,   $expiration_date,
                 $notes,       $biblioData->{title},
                 $itemNum,     $found
             );
+            $failed_holds++ unless $reserve_id;
             ++$reserve_cnt;
         }
     }
 
-    print $query->redirect("/cgi-bin/koha/opac-user.pl#opac-user-holds");
+    print $query->redirect("/cgi-bin/koha/opac-user.pl?" . ( $failed_holds ? "failed_holds=$failed_holds" : q|| ) . "#opac-user-holds");
     exit;
 }
 
@@ -307,6 +314,7 @@ if ( $borr->{'amountoutstanding'} && ($borr->{'amountoutstanding'} > $maxoutstan
     $noreserves = 1;
     $template->param( too_much_oweing => $amount );
 }
+
 if ( $borr->{gonenoaddress} && ($borr->{gonenoaddress} == 1) ) {
     $noreserves = 1;
     $template->param(
@@ -314,6 +322,7 @@ if ( $borr->{gonenoaddress} && ($borr->{gonenoaddress} == 1) ) {
         GNA     => 1
     );
 }
+
 if ( $borr->{lost} && ($borr->{lost} == 1) ) {
     $noreserves = 1;
     $template->param(
@@ -321,11 +330,14 @@ if ( $borr->{lost} && ($borr->{lost} == 1) ) {
         lost    => 1
     );
 }
+
 if ( IsDebarred($borrowernumber) ) {
     $noreserves = 1;
     $template->param(
-        message  => 1,
-        debarred => 1
+        message          => 1,
+        debarred         => 1,
+        debarred_comment => $borr->{debarredcomment},
+        debarred_date    => $borr->{debarred},
     );
 }
 
@@ -399,20 +411,13 @@ foreach my $biblioNum (@biblionumbers) {
     $biblioLoopIter{mandatorynotes}=0; #FIXME: For future use
 
     if (!$itemLevelTypes && $biblioData->{itemtype}) {
-        $biblioLoopIter{description} = $itemTypes->{$biblioData->{itemtype}}{description};
+        $biblioLoopIter{translated_description} = $itemTypes->{$biblioData->{itemtype}}{translated_description};
         $biblioLoopIter{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$biblioData->{itemtype}}{imageurl};
     }
 
     foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
-        $debug and warn $itemInfo->{'notforloan'};
-
-        # Get reserve fee.
-        my $fee = GetReserveFee(undef, $borrowernumber, $itemInfo->{'biblionumber'}, 'a',
-                                ( $itemInfo->{'biblioitemnumber'} ) );
-        $itemInfo->{'reservefee'} = sprintf "%.02f", ($fee ? $fee : 0.0);
-
         if ($itemLevelTypes && $itemInfo->{itype}) {
-            $itemInfo->{description} = $itemTypes->{$itemInfo->{itype}}{description};
+            $itemInfo->{translated_description} = $itemTypes->{$itemInfo->{itype}}{translated_description};
             $itemInfo->{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$itemInfo->{itype}}{imageurl};
         }
 
@@ -435,7 +440,7 @@ foreach my $biblioNum (@biblionumbers) {
         $itemLoopIter->{enumchron} = $itemInfo->{enumchron};
         $itemLoopIter->{copynumber} = $itemInfo->{copynumber};
         if ($itemLevelTypes) {
-            $itemLoopIter->{description} = $itemInfo->{description};
+            $itemLoopIter->{translated_description} = $itemInfo->{translated_description};
             $itemLoopIter->{imageurl} = $itemInfo->{imageurl};
         }
 
@@ -459,13 +464,14 @@ foreach my $biblioNum (@biblionumbers) {
         my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
 
         # the item could be reserved for this borrower vi a host record, flag this
+        $reservedfor //= '';
         if ($reservedfor eq $borrowernumber){
             $itemLoopIter->{already_reserved} = 1;
         }
 
         if ( defined $reservedate ) {
             $itemLoopIter->{backgroundcolor} = 'reserved';
-            $itemLoopIter->{reservedate}     = format_date($reservedate);
+            $itemLoopIter->{reservedate}     = output_pref({ dt => dt_from_string($reservedate), dateonly => 1 });
             $itemLoopIter->{ReservedForBorrowernumber} = $reservedfor;
             $itemLoopIter->{ReservedForSurname}        = $ItemBorrowerReserveInfo->{'surname'};
             $itemLoopIter->{ReservedForFirstname}      = $ItemBorrowerReserveInfo->{'firstname'};
@@ -499,7 +505,7 @@ foreach my $biblioNum (@biblionumbers) {
         my ( $transfertwhen, $transfertfrom, $transfertto ) =
           GetTransfers($itemNum);
         if ( $transfertwhen && ($transfertwhen ne '') ) {
-            $itemLoopIter->{transfertwhen} = format_date($transfertwhen);
+            $itemLoopIter->{transfertwhen} = output_pref({ dt => dt_from_string($transfertwhen), dateonly => 1 });
             $itemLoopIter->{transfertfrom} =
               $branches->{$transfertfrom}{branchname};
             $itemLoopIter->{transfertto} = $branches->{$transfertto}{branchname};
@@ -519,16 +525,6 @@ foreach my $biblioNum (@biblionumbers) {
         my $branch = GetReservesControlBranch( $itemInfo, $borr );
 
         my $policy_holdallowed = !$itemLoopIter->{already_reserved};
-        if ($policy_holdallowed) {
-            if (my $branchitemrule = GetBranchItemRule( $branch, $itemInfo->{'itype'} )) {
-                $policy_holdallowed =
-                  ($branchitemrule->{'holdallowed'} == 2) ||
-                  ($branchitemrule->{'holdallowed'} == 1
-                      && $borr->{'branchcode'} eq $itemInfo->{'homebranch'});
-            } else {
-                $policy_holdallowed = 0; # No rule - not allowed
-            }
-        }
         $policy_holdallowed &&=
             IsAvailableForItemLevelRequest($itemInfo,$borr) &&
             CanItemBeReserved($borrowernumber,$itemNum) eq 'OK';
@@ -594,5 +590,4 @@ if (
     );
 }
 
-output_html_with_http_headers $query, $cookie, $template->output;
-
+output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };