batchRebuildBiblioTables.pl doesn't crash anymore when GetMarcBiblio fails.
[koha.git] / C4 / Reserves.pm
index e008500..19f483f 100644 (file)
@@ -35,7 +35,7 @@ use C4::Accounts;
 use C4::Members::Messaging;
 use C4::Letters;
 use C4::Branch qw( GetBranchDetail );
-use List::MoreUtils qw( firstidx );
+use List::MoreUtils qw( firstidx any );
 
 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
 
@@ -101,7 +101,8 @@ BEGIN {
         &GetReserveCount
         &GetReserveFee
                &GetReserveInfo
-    
+        &GetReserveStatus
+        
         &GetOtherReserves
         
         &ModReserveFill
@@ -112,6 +113,8 @@ BEGIN {
         &ModReserveMinusPriority
         
         &CheckReserves
+        &CanBookBeReserved
+        &CanItemBeReserved
         &CancelReserve
 
         &IsAvailableForItemLevelRequest
@@ -120,7 +123,7 @@ BEGIN {
 
 =item AddReserve
 
-    AddReserve($branch,$borrowernumber,$biblionumber,$constraint,$bibitems,$priority,$notes,$title,$checkitem,$found)
+    AddReserve($branch,$borrowernumber,$biblionumber,$constraint,$bibitems,$priority,$notes,$title,$checkitem,$found, $from)
 
 =cut
 
@@ -128,7 +131,7 @@ sub AddReserve {
     my (
         $branch,    $borrowernumber, $biblionumber,
         $constraint, $bibitems,  $priority,       $notes,
-        $title,      $checkitem, $found
+        $title,      $checkitem, $found, $from
     ) = @_;
     my $fee =
           GetReserveFee($borrowernumber, $biblionumber, $constraint,
@@ -180,7 +183,8 @@ sub AddReserve {
     if(C4::Context->preference("emailLibrarianWhenHoldIsPlaced")){
         my $borrower = GetMemberDetails($borrowernumber);
         my $biblio   = GetBiblioData($biblionumber);
-        my $letter = C4::Letters::getletter( 'reserves', 'HOLDPLACED');
+       my $lettertype = ($from eq "intranet") ? "STAFFHOLDPLACED" : "HOLDPLACED";
+        my $letter = C4::Letters::getletter( 'reserves', $lettertype);
         my $admin_email_address = C4::Context->preference('KohaAdminEmailAddress');
 
         my %keys = (%$borrower, %$biblio);
@@ -226,41 +230,24 @@ sub AddReserve {
 =cut
 
 sub GetPendingReserves {
-    my ($startdate, $enddate) = @_;
-    
-    my $sqldatewhere;
+    my ($filters, $startindex, $results) = @_;
+
+    $startindex = "0" if not $startindex;
+
     my @query_params;
     my $indepbranch  = C4::Context->preference('IndependantBranches') ? C4::Context->userenv->{'branch'} : undef;
     my $dbh          = C4::Context->dbh;
     
-    my $query = "SELECT * 
-                 FROM reserves
-                 WHERE 
-                    reserves.found IS NULL ";
-    
-    if (!defined($startdate) or $startdate eq "") {
-        $startdate = format_date($startdate);
-    }
-    if (!defined($enddate) or $enddate eq "") {
-        $enddate = format_date($enddate);
-    }
-    
-    if ($startdate) {
-        $sqldatewhere .= " AND reservedate >= ?";
-        push @query_params, format_date_in_iso($startdate);
-    }
-    if ($enddate) {
-        $sqldatewhere .= " AND reservedate <= ?";
-        push @query_params, format_date_in_iso($enddate);
-    }
-    $query .= $sqldatewhere;
+    my $query = "SELECT DISTINCT(biblionumber) AS biblionumber 
+                 FROM reserves 
+                 LEFT JOIN biblio USING(biblionumber)
+                 WHERE reserves.found IS NULL ";
     
     if ($indepbranch){
            $query .= " AND branchcode = ? ";
         push @query_params, $indepbranch;
     }
     
-    
     my $sth = $dbh->prepare($query);
     $sth->execute(@query_params);
     
@@ -274,9 +261,12 @@ sub GetPendingReserves {
             my @items  = GetItemsInfo($reserve->{biblionumber});
                     
             $line->{title}           = $biblio->{title};
-    
             foreach my $item (@items){
-                next if ($indepbranch && $indepbranch ne $item->{holdingbranch});
+                next if ( ($indepbranch && $indepbranch ne $item->{holdingbranch}) 
+                          or $item->{onloan} 
+                          or $item->{notforloan} 
+                          or $item->{itemlost} 
+                          or $item->{count_reserves} eq "Waiting" or $item->{count_reserves} eq "Transit");
                 $line->{count}++;
                 $line->{holdingbranches}->{$item->{holdingbranch}} = 1;
                 $line->{callnumbers}->{$item->{itemcallnumber}} = 1;
@@ -296,15 +286,27 @@ sub GetPendingReserves {
         foreach my $datatype (qw/holdingbranches callnumbers locations itemtypes/){
             my @newdatas = ();
             foreach my $data (keys %{$line->{$datatype}}){
-                @newdatas = { 'value' => $data}
+                push @newdatas, { 'value' => $data}
             }
             $line->{$datatype} = \@newdatas;
         }
-        
-        push @reserves, $line;
+        my $filtered = 1;
+        foreach my $key (keys %$filters){
+            my $value = $filters->{$key};
+            $filtered = 0 if not (any { $_->{value} =~ /^$value$/ } @{$line->{$key}}) and $value;
+        }
+        push @reserves, $line if $filtered; # if (any { $_->{value} =~ /^FOSPC$/ } @{$line->{holdingbranches}});
     }
     
-    return \@reserves;
+    my $count = scalar @reserves;
+    my $endindex = ($count > $startindex + $results) ? $startindex + $results : $count;
+    
+    if($count){
+        @reserves = @reserves[$startindex..$endindex];
+    }
+    
+    
+    return ($count, \@reserves);
 }
 
 =item GetReservesFromBiblionumber
@@ -437,6 +439,89 @@ sub GetReservesFromBorrowernumber {
     return @$data;
 }
 #-------------------------------------------------------------------------------------
+=item CanBookBeReserved
+
+$error = &CanBookBeReserved($borrowernumber, $biblionumber)
+
+=cut
+
+sub CanBookBeReserved{
+    my ($borrowernumber, $biblionumber) = @_;
+
+    my $dbh           = C4::Context->dbh;
+    my $biblio        = GetBiblioData($biblionumber);
+    my $borrower      = C4::Members::GetMember(borrowernumber=>$borrowernumber);
+    my $controlbranch = C4::Context->preference('ReservesControlBranch');
+    my $itype         = C4::Context->preference('item-level_itypes');
+    my $reservesrights= C4::Context->preference('maxreserves');
+    my $reservescount = 0;
+    
+    # we retrieve the user rights
+    my @args;
+    my $branchcode;
+    
+    
+    if($controlbranch eq "ItemHomeLibrary"){
+        $branchcode = '*';
+    }elsif($controlbranch eq "PatronLibrary"){
+        $branchcode = $borrower->{branchcode};
+    }
+
+    $reservescount = GetReserveCount($borrowernumber);
+
+    if($reservescount < $reservesrights){
+        return 1;
+    }else{
+        return 0;
+    }
+    
+}
+
+=item CanItemBeReserved
+
+$error = &CanItemBeReserved($borrowernumber, $itemnumber)
+
+this function return 1 if an item can be issued by this borrower.
+
+=cut
+
+sub CanItemBeReserved{
+    my ($borrowernumber, $itemnumber) = @_;
+    
+    my $dbh             = C4::Context->dbh;
+            
+    my $controlbranch   = C4::Context->preference('ReservesControlBranch') || "ItemHomeLibrary";
+    my $itype           = C4::Context->preference('item-level_itypes') ? "itype" : "itemtype";
+    my $allowedreserves = C4::Context->preference('maxreserves');
+    
+    # we retrieve borrowers and items informations #
+    my $item     = C4::Items::GetItem($itemnumber);
+    my $borrower = C4::Members::GetMember($borrowernumber, 'borrowernumber');     
+
+    my $branchcode   = "*";
+    my $branchfield  = "reserves.branchcode";
+    
+    if( $controlbranch eq "ItemHomeLibrary" ){
+        $branchcode = $item->{homebranch};
+    }elsif( $controlbranch eq "PatronLibrary" ){
+        $branchcode = $borrower->{branchcode};
+    }
+    
+    # we retrieve user rights on this itemtype and branchcode
+    my $issuingrule = C4::Circulation::GetIssuingRule($borrower->{categorycode}, $item->{$itype}, $branchcode);
+    
+    # we retrieve count
+    
+    my $reservecount = GetReserveCount($borrowernumber);
+
+    # we check if it's ok or not
+    if(( $reservecount < $allowedreserves ) and $issuingrule->{maxissueqty} ){
+        return 1;
+    }else{
+        return 0;
+    }
+}
+#-------------------------------------------------------------------------------------
 
 =item GetReserveCount
 
@@ -675,6 +760,18 @@ sub GetReservesForBranch {
     return (@transreserv);
 }
 
+sub GetReserveStatus {
+    my ($itemnumber) = @_;
+    
+    my $dbh = C4::Context->dbh;
+    
+    my $itemstatus = $dbh->prepare("SELECT found FROM reserves WHERE itemnumber = ?");
+    
+    $itemstatus->execute($itemnumber);
+    my ($found) = $itemstatus->fetchrow_array;
+    return $found;
+}
+
 =item CheckReserves
 
   ($status, $reserve) = &CheckReserves($itemnumber);
@@ -759,6 +856,9 @@ sub CheckReserves {
                 # Found it
                 return ( "Waiting", $res );
             }
+            elsif( $res->{'itemnumber'} == $item && $res->{'found'} eq 'T' ){
+                return ( "Transit", $res );
+            }
             else {
                 # See if this item is more important than what we've got
                 # so far.
@@ -1269,10 +1369,11 @@ sub IsAvailableForItemLevelRequest {
                                $item->{wthdrawn} or
                                $notforloan_per_itemtype;
 
+    
     if (C4::Context->preference('AllowOnShelfHolds')) {
         return $available_per_item;
     } else {
-        return ($available_per_item and $item->{onloan}); 
+        return ($available_per_item and ($item->{onloan} or GetReserveStatus($itemnumber) eq "W")); 
     }
 }