Bug 21738: check items count in C4:ILSDI::HoldTitle
[koha.git] / C4 / Reserves.pm
index 38f9953..77880dc 100644 (file)
@@ -48,6 +48,8 @@ use Koha::IssuingRules;
 use Koha::Items;
 use Koha::ItemTypes;
 use Koha::Patrons;
+use Koha::CirculationRules;
+use Koha::Account::Lines;
 
 use List::MoreUtils qw( firstidx any );
 use Carp;
@@ -265,7 +267,7 @@ sub AddReserve {
 
 =head2 CanBookBeReserved
 
-  $canReserve = &CanBookBeReserved($borrowernumber, $biblionumber)
+  $canReserve = &CanBookBeReserved($borrowernumber, $biblionumber, $branchcode)
   if ($canReserve eq 'OK') { #We can reserve this Item! }
 
 See CanItemBeReserved() for possible return values.
@@ -273,48 +275,52 @@ See CanItemBeReserved() for possible return values.
 =cut
 
 sub CanBookBeReserved{
-    my ($borrowernumber, $biblionumber) = @_;
+    my ($borrowernumber, $biblionumber, $pickup_branchcode) = @_;
 
-    my $items = GetItemnumbersForBiblio($biblionumber);
+    my @itemnumbers = Koha::Items->search({ biblionumber => $biblionumber})->get_column("itemnumber");
     #get items linked via host records
     my @hostitems = get_hostitemnumbers_of($biblionumber);
     if (@hostitems){
-    push (@$items,@hostitems);
+        push (@itemnumbers, @hostitems);
     }
 
     my $canReserve;
-    foreach my $item (@$items) {
-        $canReserve = CanItemBeReserved( $borrowernumber, $item );
-        return $canReserve if $canReserve->{status} eq 'OK';
+    foreach my $itemnumber (@itemnumbers) {
+        $canReserve = CanItemBeReserved( $borrowernumber, $itemnumber, $pickup_branchcode );
+        return { status => 'OK' } if $canReserve->{status} eq 'OK';
     }
     return $canReserve;
 }
 
 =head2 CanItemBeReserved
 
-  $canReserve = &CanItemBeReserved($borrowernumber, $itemnumber)
+  $canReserve = &CanItemBeReserved($borrowernumber, $itemnumber, $branchcode)
   if ($canReserve->{status} eq 'OK') { #We can reserve this Item! }
 
 @RETURNS { status => OK },              if the Item can be reserved.
          { status => ageRestricted },   if the Item is age restricted for this borrower.
          { status => damaged },         if the Item is damaged.
          { status => cannotReserveFromOtherBranches }, if syspref 'canreservefromotherbranches' is OK.
-         { status => tooManyReserves, limit => $limit }, if the borrower has exceeded his maximum reserve amount.
+         { status => tooManyReserves, limit => $limit }, if the borrower has exceeded their maximum reserve amount.
          { status => notReservable },   if holds on this item are not allowed
+         { status => libraryNotFound },   if given branchcode is not an existing library
+         { status => libraryNotPickupLocation },   if given branchcode is not configured to be a pickup location
+         { status => cannotBeTransferred }, if branch transfer limit applies on given item and branchcode
 
 =cut
 
 sub CanItemBeReserved {
-    my ( $borrowernumber, $itemnumber ) = @_;
+    my ( $borrowernumber, $itemnumber, $pickup_branchcode ) = @_;
 
     my $dbh = C4::Context->dbh;
     my $ruleitemtype;    # itemtype of the matching issuing rule
     my $allowedreserves  = 0; # Total number of holds allowed across all records
     my $holds_per_record = 1; # Total number of holds allowed for this one given record
+    my $holds_per_day;        # Default to unlimited
 
     # we retrieve borrowers and items informations #
     # item->{itype} will come for biblioitems if necessery
-    my $item       = GetItem($itemnumber);
+    my $item       = C4::Items::GetItem($itemnumber);
     my $biblio     = Koha::Biblios->find( $item->{biblionumber} );
     my $patron = Koha::Patrons->find( $borrowernumber );
     my $borrower = $patron->unblessed;
@@ -361,6 +367,7 @@ sub CanItemBeReserved {
         $ruleitemtype     = $rights->{itemtype};
         $allowedreserves  = $rights->{reservesallowed};
         $holds_per_record = $rights->{holds_per_record};
+        $holds_per_day    = $rights->{holds_per_day};
     }
     else {
         $ruleitemtype = '*';
@@ -378,6 +385,18 @@ sub CanItemBeReserved {
         return { status => "tooManyHoldsForThisRecord", limit => $holds_per_record };
     }
 
+    my $today_holds = Koha::Holds->search({
+        borrowernumber => $borrowernumber,
+        reservedate    => dt_from_string->date
+    });
+
+    if ( defined $holds_per_day &&
+          (   ( $holds_per_day > 0 && $today_holds->count() >= $holds_per_day )
+           or ( $holds_per_day == 0 ) )
+        )  {
+        return { status => 'tooManyReservesToday', limit => $holds_per_day };
+    }
+
     # we retrieve count
 
     $querycount .= "AND $branchfield = ?";
@@ -410,26 +429,21 @@ sub CanItemBeReserved {
     }
 
     # Now we need to check hold limits by patron category
-    my $schema = Koha::Database->new()->schema();
-    my $rule = $schema->resultset('BranchBorrowerCircRule')->find(
+    my $rule = Koha::CirculationRules->get_effective_rule(
         {
-            branchcode   => $branchcode,
             categorycode => $borrower->{categorycode},
+            branchcode   => $branchcode,
+            rule_name    => 'max_holds',
         }
     );
-    $rule ||= $schema->resultset('DefaultBorrowerCircRule')->find(
-        {
-            categorycode => $borrower->{categorycode}
-        }
-    );
-    if ( $rule && defined $rule->max_holds ) {
+    if ( $rule && defined( $rule->rule_value ) && $rule->rule_value ne '' ) {
         my $total_holds_count = Koha::Holds->search(
             {
                 borrowernumber => $borrower->{borrowernumber}
             }
         )->count();
 
-        return { status => 'tooManyReserves', limit => $rule->max_holds } if $total_holds_count >= $rule->max_holds;
+        return { status => 'tooManyReserves', limit => $rule->rule_value} if $total_holds_count >= $rule->rule_value;
     }
 
     my $circ_control_branch =
@@ -458,6 +472,22 @@ sub CanItemBeReserved {
         }
     }
 
+    if ($pickup_branchcode) {
+        my $destination = Koha::Libraries->find({
+            branchcode => $pickup_branchcode,
+        });
+
+        unless ($destination) {
+            return { status => 'libraryNotFound' };
+        }
+        unless ($destination->pickup_location) {
+            return { status => 'libraryNotPickupLocation' };
+        }
+        unless ($item->can_be_transferred({ to => $destination })) {
+            return 'cannotBeTransferred';
+        }
+    }
+
     return { status => 'OK' };
 }
 
@@ -542,13 +572,24 @@ sub GetOtherReserves {
 
 sub ChargeReserveFee {
     my ( $borrowernumber, $fee, $title ) = @_;
-    return if !$fee || $fee==0; # the last test is needed to include 0.00
-    my $accquery = qq{
-INSERT INTO accountlines ( borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding ) VALUES (?, ?, NOW(), ?, ?, 'Res', ?)
-    };
-    my $dbh = C4::Context->dbh;
-    my $nextacctno = &getnextacctno( $borrowernumber );
-    $dbh->do( $accquery, undef, ( $borrowernumber, $nextacctno, $fee, "Reserve Charge - $title", $fee ) );
+
+    return if !$fee || $fee == 0;    # the last test is needed to include 0.00
+
+    my $branchcode = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
+    my $nextacctno = C4::Accounts::getnextacctno($borrowernumber);
+
+    Koha::Account::Line->new(
+        {
+            borrowernumber    => $borrowernumber,
+            accountno         => $nextacctno,
+            date              => dt_from_string(),
+            amount            => $fee,
+            description       => "Reserve Charge - $title",
+            accounttype       => 'Res',
+            amountoutstanding => $fee,
+            branchcode        => $branchcode
+        }
+    )->store();
 }
 
 =head2 GetReserveFee
@@ -1211,7 +1252,7 @@ sub _get_itype {
 
 =head2 AlterPriority
 
-  AlterPriority( $where, $reserve_id );
+  AlterPriority( $where, $reserve_id, $prev_priority, $next_priority, $first_priority, $last_priority );
 
 This function changes a reserve's priority up, down, to the top, or to the bottom.
 Input: $where is 'up', 'down', 'top' or 'bottom'. Biblionumber, Date reserve was placed
@@ -1219,7 +1260,7 @@ Input: $where is 'up', 'down', 'top' or 'bottom'. Biblionumber, Date reserve was
 =cut
 
 sub AlterPriority {
-    my ( $where, $reserve_id ) = @_;
+    my ( $where, $reserve_id, $prev_priority, $next_priority, $first_priority, $last_priority ) = @_;
 
     my $hold = Koha::Holds->find( $reserve_id );
     return unless $hold;
@@ -1229,21 +1270,18 @@ sub AlterPriority {
         return;
     }
 
-    if ( $where eq 'up' || $where eq 'down' ) {
-
-      my $priority = $hold->priority;
-      $priority = $where eq 'up' ? $priority - 1 : $priority + 1;
-      _FixPriority({ reserve_id => $reserve_id, rank => $priority })
-
+    if ( $where eq 'up' ) {
+      return unless $prev_priority;
+      _FixPriority({ reserve_id => $reserve_id, rank => $prev_priority })
+    } elsif ( $where eq 'down' ) {
+      return unless $next_priority;
+      _FixPriority({ reserve_id => $reserve_id, rank => $next_priority })
     } elsif ( $where eq 'top' ) {
-
-      _FixPriority({ reserve_id => $reserve_id, rank => '1' })
-
+      _FixPriority({ reserve_id => $reserve_id, rank => $first_priority })
     } elsif ( $where eq 'bottom' ) {
-
-      _FixPriority({ reserve_id => $reserve_id, rank => '999999' });
-
+      _FixPriority({ reserve_id => $reserve_id, rank => $last_priority });
     }
+
     # FIXME Should return the new priority
 }
 
@@ -2115,7 +2153,7 @@ sub GetHoldRule {
 
     my $sth = $dbh->prepare(
         q{
-         SELECT categorycode, itemtype, branchcode, reservesallowed, holds_per_record
+         SELECT categorycode, itemtype, branchcode, reservesallowed, holds_per_record, holds_per_day
            FROM issuingrules
           WHERE (categorycode in (?,'*') )
             AND (itemtype IN (?,'*'))