[HEAD] (bug #3323) allow rules for reserves in issuing rules.
[koha.git] / C4 / Reserves.pm
index da685b9..bd6bd8c 100644 (file)
@@ -112,6 +112,8 @@ BEGIN {
         &ModReserveCancelAll
         &ModReserveMinusPriority
         
+        &CanBookBeReserved
+        &CanItemBeReserved
         &CheckReserves
         &CancelReserve
 
@@ -228,40 +230,41 @@ sub GetReservesFromBiblionumber {
     while ( my $data = $sth->fetchrow_hashref ) {
 
         # FIXME - What is this doing? How do constraints work?
-        ($data->{constrainttype} eq 'o') or next;
-        $query = '
-            SELECT biblioitemnumber
-             FROM  reserveconstraints
-            WHERE  biblionumber   = ?
-             AND   borrowernumber = ?
-             AND   reservedate    = ?
-        ';
-        my $csth = $dbh->prepare($query);
-        $csth->execute( $data->{biblionumber}, $data->{borrowernumber},
-            $data->{reservedate}, );
-
-        my @bibitemno;
-        while ( my $bibitemnos = $csth->fetchrow_array ) {
-            push( @bibitemno, $bibitemnos );    # FIXME: inefficient: use fetchall_arrayref
-        }
-        my $count = scalar @bibitemno;
-
-        # if we have two or more different specific itemtypes
-        # reserved by same person on same day
-        my $bdata;
-        if ( $count > 1 ) {
-            $bdata = GetBiblioItemData( $bibitemno[$i] );
-            $i++;
-        }
-        else {
-            # Look up the book we just found.
-            $bdata = GetBiblioItemData( $bibitemno[0] );
-        }
-        # Add the results of this latest search to the current
-        # results.
-        # FIXME - An 'each' would probably be more efficient.
-        foreach my $key ( keys %$bdata ) {
-            $data->{$key} = $bdata->{$key};
+        if ($data->{constrainttype} eq 'o') {
+            $query = '
+                SELECT biblioitemnumber
+                FROM  reserveconstraints
+                WHERE  biblionumber   = ?
+                AND   borrowernumber = ?
+                AND   reservedate    = ?
+            ';
+            my $csth = $dbh->prepare($query);
+            $csth->execute( $data->{biblionumber}, $data->{borrowernumber},
+                $data->{reservedate}, );
+    
+            my @bibitemno;
+            while ( my $bibitemnos = $csth->fetchrow_array ) {
+                push( @bibitemno, $bibitemnos );    # FIXME: inefficient: use fetchall_arrayref
+            }
+            my $count = scalar @bibitemno;
+    
+            # if we have two or more different specific itemtypes
+            # reserved by same person on same day
+            my $bdata;
+            if ( $count > 1 ) {
+                $bdata = GetBiblioItemData( $bibitemno[$i] );
+                $i++;
+            }
+            else {
+                # Look up the book we just found.
+                $bdata = GetBiblioItemData( $bibitemno[0] );
+            }
+            # Add the results of this latest search to the current
+            # results.
+            # FIXME - An 'each' would probably be more efficient.
+            foreach my $key ( keys %$bdata ) {
+                $data->{$key} = $bdata->{$key};
+            }
         }
         push @results, $data;
     }
@@ -325,6 +328,180 @@ sub GetReservesFromBorrowernumber {
 }
 #-------------------------------------------------------------------------------------
 
+=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);
+    my $controlbranch = C4::Context->preference('ReservesControlBranch');
+    my $itype         = C4::Context->preference('item-level_itypes');
+    my $reservesrights= 0;
+    my $reservescount = 0;
+    
+    # we retrieve the user rights
+    my @args;
+    my $rightsquery = "SELECT categorycode, itemtype, branchcode, reservesallowed 
+                       FROM issuingrules 
+                       WHERE categorycode = ?";
+    push @args,$borrower->{categorycode};
+
+    if($controlbranch eq "ItemHomeLibrary"){
+        $rightsquery .= " AND branchcode = '*'";
+    }elsif($controlbranch eq "PatronLibrary"){
+        $rightsquery .= " AND branchcode IN (?,'*')";
+        push @args, $borrower->{branchcode};
+    }
+    
+    if(not $itype){
+        $rightsquery .= " AND itemtype IN (?,'*')";
+        push @args, $biblio->{itemtype};
+    }else{
+        $rightsquery .= " AND itemtype = '*'";
+    }
+    
+    $rightsquery .= " ORDER BY categorycode DESC, itemtype DESC, branchcode DESC";
+    
+    my $sthrights = $dbh->prepare($rightsquery);
+    $sthrights->execute(@args);
+    
+    if(my $row = $sthrights->fetchrow_hashref()){
+       $reservesrights = $row->{reservesallowed};
+    }
+    
+    @args = ();
+    # we count how many reserves the borrower have
+    my $countquery = "SELECT count(*) as count
+                      FROM reserves
+                      LEFT JOIN items USING (itemnumber)
+                      LEFT JOIN biblioitems ON (reserves.biblionumber=biblioitems.biblionumber)
+                      LEFT JOIN borrowers USING (borrowernumber)
+                      WHERE borrowernumber = ?
+                    ";
+    push @args, $borrowernumber;
+    
+    if(not $itype){
+           $countquery .= "AND itemtype = ?";
+           push @args, $biblio->{itemtype};
+    }
+    
+    if($controlbranch eq "PatronLibrary"){
+        $countquery .= " AND borrowers.branchcode = ? ";
+        push @args, $borrower->{branchcode};
+    }
+    
+    my $sthcount = $dbh->prepare($countquery);
+    $sthcount->execute(@args);
+    
+    if(my $row = $sthcount->fetchrow_hashref()){
+       $reservescount = $row->{count};
+    }
+    
+    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 $allowedreserves = 0;
+            
+    my $controlbranch = C4::Context->preference('ReservesControlBranch');
+    my $itype         = C4::Context->preference('item-level_itypes') ? "itype" : "itemtype";
+
+    # we retrieve borrowers and items informations #
+    my $item     = GetItem($itemnumber);
+    my $borrower = C4::Members::GetMember($borrowernumber);     
+    
+    # we retrieve user rights on this itemtype and branchcode
+    my $sth = $dbh->prepare("SELECT categorycode, itemtype, branchcode, reservesallowed 
+                             FROM issuingrules 
+                             WHERE (categorycode in (?,'*') ) 
+                             AND (itemtype IN (?,'*')) 
+                             AND (branchcode IN (?,'*')) 
+                             ORDER BY 
+                               categorycode DESC, 
+                               itemtype     DESC, 
+                               branchcode   DESC;"
+                           );
+                           
+    my $querycount ="SELECT 
+                            count(*) as count
+                            FROM reserves
+                                LEFT JOIN items USING (itemnumber)
+                                LEFT JOIN biblioitems ON (reserves.biblionumber=biblioitems.biblionumber)
+                                LEFT JOIN borrowers USING (borrowernumber)
+                            WHERE borrowernumber = ?
+                                ";
+    
+    
+    my $itemtype     = $item->{$itype};
+    my $categorycode = $borrower->{categorycode};
+    my $branchcode   = "";
+    my $branchfield  = "reserves.branchcode";
+    
+    if( $controlbranch eq "ItemHomeLibrary" ){
+        $branchfield = "items.homebranch";
+        $branchcode = $item->{homebranch};
+    }elsif( $controlbranch eq "PatronLibrary" ){
+        $branchfield = "borrowers.branchcode";
+        $branchcode = $borrower->{branchcode};
+    }
+    
+    # we retrieve rights 
+    $sth->execute($categorycode, $itemtype, $branchcode);
+    if(my $rights = $sth->fetchrow_hashref()){
+        $itemtype        = $rights->{itemtype};
+        $allowedreserves = $rights->{reservesallowed}; 
+    }else{
+        $itemtype = '*';
+    }
+    
+    # we retrieve count
+    
+    $querycount .= "AND $branchfield = ?";
+    
+    $querycount .= " AND $itype = ?" if ($itemtype ne "*");
+    my $sthcount = $dbh->prepare($querycount);
+    
+    if($itemtype eq "*"){
+        $sthcount->execute($borrowernumber, $branchcode);
+    }else{
+        $sthcount->execute($borrowernumber, $branchcode, $itemtype);
+    }
+    
+    my $reservecount = "0";
+    if(my $rowcount = $sthcount->fetchrow_hashref()){
+        $reservecount = $rowcount->{count};
+    }
+    
+    # we check if it's ok or not
+    if( $reservecount < $allowedreserves ){
+        return 1;
+    }else{
+        return 0;
+    }
+}
+
 =item GetReserveCount
 
 $number = &GetReserveCount($borrowernumber);
@@ -374,7 +551,7 @@ sub GetOtherReserves {
             );
 
             #launch the subroutine dotransfer
-            C4::Circulation::ModItemTransfer(
+            C4::Items::ModItemTransfer(
                 $itemnumber,
                 $iteminfo->{'holdingbranch'},
                 $checkreserves->{'branchcode'}
@@ -972,6 +1149,12 @@ sub ModReserveAffect {
     my $sth = $dbh->prepare("SELECT biblionumber FROM items WHERE itemnumber=?");
     $sth->execute($itemnumber);
     my ($biblionumber) = $sth->fetchrow;
+
+    # get request - need to find out if item is already
+    # waiting in order to not send duplicate hold filled notifications
+    my $request = GetReserveInfo($borrowernumber, $biblionumber);
+    my $already_on_shelf = ($request && $request->{found} eq 'W') ? 1 : 0;
+
     # If we affect a reserve that has to be transfered, don't set to Waiting
     my $query;
     if ($transferToDo) {
@@ -997,7 +1180,7 @@ sub ModReserveAffect {
     }
     $sth = $dbh->prepare($query);
     $sth->execute( $itemnumber, $borrowernumber,$biblionumber);
-    _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber ) if ( !$transferToDo );
+    _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber ) if ( !$transferToDo && !$already_on_shelf );
 
     return;
 }