Bug 21206: (QA follow-up) Rebase problem and leftover mocked GetItem
[koha.git] / C4 / Reserves.pm
index bc5963f..92c86b6 100644 (file)
@@ -49,6 +49,7 @@ use Koha::Items;
 use Koha::ItemTypes;
 use Koha::Patrons;
 use Koha::CirculationRules;
+use Koha::Account::Lines;
 
 use List::MoreUtils qw( firstidx any );
 use Carp;
@@ -283,7 +284,7 @@ sub CanBookBeReserved{
         push (@itemnumbers, @hostitems);
     }
 
-    my $canReserve;
+    my $canReserve = { status => '' };
     foreach my $itemnumber (@itemnumbers) {
         $canReserve = CanItemBeReserved( $borrowernumber, $itemnumber, $pickup_branchcode );
         return { status => 'OK' } if $canReserve->{status} eq 'OK';
@@ -304,6 +305,7 @@ sub CanBookBeReserved{
          { 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
 
@@ -314,17 +316,18 @@ sub CanItemBeReserved {
     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 $biblio     = Koha::Biblios->find( $item->{biblionumber} );
+    my $item       = Koha::Items->find($itemnumber);
+    my $biblio     = $item->biblio;
     my $patron = Koha::Patrons->find( $borrowernumber );
     my $borrower = $patron->unblessed;
 
     # If an item is damaged and we don't allow holds on damaged items, we can stop right here
     return { status =>'damaged' }
-      if ( $item->{damaged}
+      if ( $item->damaged
         && !C4::Context->preference('AllowHoldsOnDamagedItems') );
 
     # Check for the age restriction
@@ -352,7 +355,7 @@ sub CanItemBeReserved {
 
     if ( $controlbranch eq "ItemHomeLibrary" ) {
         $branchfield = "items.homebranch";
-        $branchcode  = $item->{homebranch};
+        $branchcode  = $item->homebranch;
     }
     elsif ( $controlbranch eq "PatronLibrary" ) {
         $branchfield = "borrowers.branchcode";
@@ -360,16 +363,16 @@ sub CanItemBeReserved {
     }
 
     # we retrieve rights
-    if ( my $rights = GetHoldRule( $borrower->{'categorycode'}, $item->{'itype'}, $branchcode ) ) {
+    if ( my $rights = GetHoldRule( $borrower->{'categorycode'}, $item->effective_itemtype, $branchcode ) ) {
         $ruleitemtype     = $rights->{itemtype};
         $allowedreserves  = $rights->{reservesallowed};
         $holds_per_record = $rights->{holds_per_record};
+        $holds_per_day    = $rights->{holds_per_day};
     }
     else {
         $ruleitemtype = '*';
     }
 
-    $item = Koha::Items->find( $itemnumber );
     my $holds = Koha::Holds->search(
         {
             borrowernumber => $borrowernumber,
@@ -381,6 +384,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 = ?";
@@ -413,23 +428,14 @@ sub CanItemBeReserved {
     }
 
     # Now we need to check hold limits by patron category
-    my $rule = Koha::CirculationRules->find(
+    my $rule = Koha::CirculationRules->get_effective_rule(
         {
             categorycode => $borrower->{categorycode},
             branchcode   => $branchcode,
-            itemtype     => undef,
-            rule_name    => 'max_holds',
-        }
-    );
-    $rule ||= Koha::CirculationRules->find(
-        {
-            categorycode => $borrower->{categorycode},
-            branchcode   => undef,,
-            itemtype     => undef,
             rule_name    => 'max_holds',
         }
     );
-    if ( $rule ) {
+    if ( $rule && defined( $rule->rule_value ) && $rule->rule_value ne '' ) {
         my $total_holds_count = Koha::Holds->search(
             {
                 borrowernumber => $borrower->{borrowernumber}
@@ -442,7 +448,7 @@ sub CanItemBeReserved {
     my $circ_control_branch =
       C4::Circulation::_GetCircControlBranch( $item->unblessed(), $borrower );
     my $branchitemrule =
-      C4::Circulation::GetBranchItemRule( $circ_control_branch, $item->itype );
+      C4::Circulation::GetBranchItemRule( $circ_control_branch, $item->itype ); # FIXME Should not be item->effective_itemtype?
 
     if ( $branchitemrule->{holdallowed} == 0 ) {
         return { status => 'notReservable' };
@@ -459,8 +465,7 @@ sub CanItemBeReserved {
     if ( C4::Context->preference('IndependentBranches')
         and !C4::Context->preference('canreservefromotherbranches') )
     {
-        my $itembranch = $item->homebranch;
-        if ( $itembranch ne $borrower->{branchcode} ) {
+        if ( $item->homebranch ne $borrower->{branchcode} ) {
             return { status => 'cannotReserveFromOtherBranches' };
         }
     }
@@ -469,12 +474,16 @@ sub CanItemBeReserved {
         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' };
@@ -517,8 +526,8 @@ sub GetOtherReserves {
     my $nextreservinfo;
     my ( undef, $checkreserves, undef ) = CheckReserves($itemnumber);
     if ($checkreserves) {
-        my $iteminfo = GetItem($itemnumber);
-        if ( $iteminfo->{'holdingbranch'} ne $checkreserves->{'branchcode'} ) {
+        my $item = Koha::Items->find($itemnumber);
+        if ( $item->holdingbranch ne $checkreserves->{'branchcode'} ) {
             $messages->{'transfert'} = $checkreserves->{'branchcode'};
             #minus priorities of others reservs
             ModReserveMinusPriority(
@@ -529,7 +538,7 @@ sub GetOtherReserves {
             #launch the subroutine dotransfer
             C4::Items::ModItemTransfer(
                 $itemnumber,
-                $iteminfo->{'holdingbranch'},
+                $item->holdingbranch,
                 $checkreserves->{'branchcode'}
               ),
               ;
@@ -561,13 +570,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
@@ -754,15 +774,15 @@ sub CheckReserves {
                 }
             } else {
                 my $patron;
-                my $iteminfo;
+                my $item;
                 my $local_hold_match;
 
                 if ($LocalHoldsPriority) {
                     $patron = Koha::Patrons->find( $res->{borrowernumber} );
-                    $iteminfo = C4::Items::GetItem($itemnumber);
+                    $item = Koha::Items->find($itemnumber);
 
                     my $local_holds_priority_item_branchcode =
-                      $iteminfo->{$LocalHoldsPriorityItemControl};
+                      $item->$LocalHoldsPriorityItemControl;
                     my $local_holds_priority_patron_branchcode =
                       ( $LocalHoldsPriorityPatronControl eq 'PickupLibrary' )
                       ? $res->{branchcode}
@@ -776,14 +796,15 @@ sub CheckReserves {
 
                 # See if this item is more important than what we've got so far
                 if ( ( $res->{'priority'} && $res->{'priority'} < $priority ) || $local_hold_match ) {
-                    $iteminfo ||= C4::Items::GetItem($itemnumber);
-                    next if $res->{itemtype} && $res->{itemtype} ne _get_itype( $iteminfo );
+                    $item ||= Koha::Items->find($itemnumber);
+                    next if $res->{itemtype} && $res->{itemtype} ne $item->effective_itemtype;
                     $patron ||= Koha::Patrons->find( $res->{borrowernumber} );
-                    my $branch = GetReservesControlBranch( $iteminfo, $patron->unblessed );
-                    my $branchitemrule = C4::Circulation::GetBranchItemRule($branch,$iteminfo->{'itype'});
+                    my $branch = GetReservesControlBranch( $item->unblessed, $patron->unblessed );
+                    my $branchitemrule = C4::Circulation::GetBranchItemRule($branch,$item->effective_itemtype);
                     next if ($branchitemrule->{'holdallowed'} == 0);
                     next if (($branchitemrule->{'holdallowed'} == 1) && ($branch ne $patron->branchcode));
-                    next if ( ($branchitemrule->{hold_fulfillment_policy} ne 'any') && ($res->{branchcode} ne $iteminfo->{ $branchitemrule->{hold_fulfillment_policy} }) );
+                    my $hold_fulfillment_policy = $branchitemrule->{hold_fulfillment_policy};
+                    next if ( ($branchitemrule->{hold_fulfillment_policy} ne 'any') && ($res->{branchcode} ne $item->$hold_fulfillment_policy) );
                     $priority = $res->{'priority'};
                     $highest  = $res;
                     last if $local_hold_match;
@@ -2131,7 +2152,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 (?,'*'))