(bug #4334) create needed sample datas
[koha.git] / reserve / request.pl
index 874b8b2..acb813c 100755 (executable)
@@ -26,6 +26,7 @@ script to place reserves/requests
 =cut
 
 use strict;
+use warnings;
 use C4::Branch; # GetBranches get_branchinfos_of
 use CGI;
 use List::MoreUtils qw/uniq/;
@@ -39,6 +40,7 @@ use C4::Koha;
 use C4::Circulation;
 use C4::Dates qw/format_date/;
 use C4::Members;
+use C4::Search;                # enabled_staff_search_views
 
 my $dbh = C4::Context->dbh;
 my $sth;
@@ -104,7 +106,7 @@ if ($cardnumber) {
     my $number_reserves =
       GetReserveCount( $borrowerinfo->{'borrowernumber'} );
 
-    if ( $number_reserves > C4::Context->preference('maxreserves') ) {
+    if ( not CanBookBeReserved($borrowerinfo->{borrowernumber}, $biblionumber)) {
                $warnings = 1;
         $maxreserves = 1;
     }
@@ -113,7 +115,7 @@ if ($cardnumber) {
     my $expiry_date = $borrowerinfo->{dateexpiry};
     my $expiry = 0; # flag set if patron account has expired
     if ($expiry_date and $expiry_date ne '0000-00-00' and
-            Date_to_Days(split /-/,$date) > Date_to_Days(split /-/,$expiry_date)) {
+            Date_to_Days(split (/-/,$date)) > Date_to_Days(split (/-/,$expiry_date))) {
                $messages = $expiry = 1;
     }else{
         $expiry = 0;
@@ -169,7 +171,7 @@ if ($borrowerslist) {
             '%s, %s ... (%s - %s) ... %s',
             $borrower->{surname},    $borrower->{firstname},
             $borrower->{cardnumber}, $borrower->{categorycode},
-            $borrower->{streetaddress},
+            $borrower->{address},
         );
     }
 
@@ -248,18 +250,25 @@ my @bibitemloop;
 
 foreach my $biblioitemnumber (@biblioitemnumbers) {
     my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
-
+    
+    my $num_available;
+    my $num_override;
+    $biblioitem->{itemloop} = [];
+    
     $biblioitem->{description} =
       $itemtypes->{ $biblioitem->{itemtype} }{description};
 
-    foreach
-      my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } )
-    {
+    foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ){
         my $item = $iteminfos_of->{$itemnumber};
-    $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
-    $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
-        $item->{homebranchname} =
-          $branches->{ $item->{homebranch} }{branchname};
+        
+        
+        unless (C4::Context->preference('item-level_itypes')) {
+            $item->{itype} = $biblioitem->{itemtype};
+        }
+
+        $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
+        $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
+        $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
 
         # if the holdingbranch is different than the homebranch, we show the
         # holdingbranch of the document too
@@ -268,8 +277,8 @@ foreach my $biblioitemnumber (@biblioitemnumbers) {
               $branches->{ $item->{holdingbranch} }{branchname};
         }
         
-#   add information
-    $item->{itemcallnumber} = $item->{itemcallnumber};
+        #   add information
+        $item->{itemcallnumber} = $item->{itemcallnumber};
     
         # if the item is currently on loan, we display its return date and
         # change the background color
@@ -326,31 +335,57 @@ foreach my $biblioitemnumber (@biblioitemnumbers) {
         # If there is no loan, return and transfer, we show a checkbox.
         $item->{notforloan} = $item->{notforloan} || 0;
     
-    # if independent branches is on we need to check if the person can reserve
-    # for branches they arent logged in to
-    if ( C4::Context->preference("IndependantBranches") ) { 
-        if (! C4::Context->preference("canreservefromotherbranches")){
-        # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
-        my $userenv = C4::Context->userenv; 
-        if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
-            $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
-        } 
+        # if independent branches is on we need to check if the person can reserve
+        # for branches they arent logged in to
+        if ( C4::Context->preference("IndependantBranches") ) { 
+            if (! C4::Context->preference("canreservefromotherbranches")){
+                # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
+                my $userenv = C4::Context->userenv; 
+                if ( ($userenv) && ( $userenv->{flags} %2 != 1 ) ) {
+                    $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
+                } 
+            }
         }
-    }
 
-    if (IsAvailableForItemLevelRequest($itemnumber) and not $item->{cantreserve}) {
-        $item->{available} = 1;
-    }
+        my $branch = C4::Circulation::_GetCircControlBranch($item, $borrowerinfo);
 
-    # FIXME: move this to a pm
-    my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
-    $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
-    while (my $wait_hashref = $sth2->fetchrow_hashref) {
-        $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
-    }
+        my $branchitemrule = GetBranchItemRule( $branch, $item->{'itype'} );
+        my $policy_holdallowed = 1;
+
+        $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
+
+        if ( $branchitemrule->{'holdallowed'} == 0 ||
+                ( $branchitemrule->{'holdallowed'} == 1 && $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
+            $policy_holdallowed = 0;
+        }
+
+        if (IsAvailableForItemLevelRequest($itemnumber) and not $item->{cantreserve} and CanItemBeReserved($borrowerinfo->{borrowernumber}, $itemnumber)) {
+            if ( not $policy_holdallowed and C4::Context->preference( 'AllowHoldPolicyOverride' ) ) {
+                $item->{override} = 1;
+                $num_override++;
+            } elsif ( $policy_holdallowed ) {
+                $item->{available} = 1;
+                $num_available++;
+            }
+        }
+        # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
+
+        # FIXME: move this to a pm
+        my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
+        $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
+        while (my $wait_hashref = $sth2->fetchrow_hashref) {
+            $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
+        }
         push @{ $biblioitem->{itemloop} }, $item;
     }
 
+    if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
+        $template->param( override_required => 1 );
+    } elsif ( $num_available == 0 ) {
+        $template->param( none_available => 1 );
+        $template->param( warnings => 1 );
+    }
+
     push @bibitemloop, $biblioitem;
 }