Bug 18925: Replace logic in GetBranchBorrowerCircRule with get_effective_rule
[koha.git] / C4 / Circulation.pm
index 658286e..58b8efb 100644 (file)
@@ -505,7 +505,7 @@ sub TooMany {
 
     # Now count total loans against the limit for the branch
     my $branch_borrower_circ_rule = GetBranchBorrowerCircRule($branch, $cat_borrower);
-    if (defined($branch_borrower_circ_rule->{patron_maxissueqty})) {
+    if (defined($branch_borrower_circ_rule->{patron_maxissueqty}) and $branch_borrower_circ_rule->{patron_maxissueqty} ne '') {
         my @bind_params = ();
         my $branch_count_query = q|
             SELECT COUNT(*) AS total, COALESCE(SUM(onsite_checkout), 0) AS onsite_checkouts
@@ -528,7 +528,7 @@ sub TooMany {
         my $max_checkouts_allowed = $branch_borrower_circ_rule->{patron_maxissueqty};
         my $max_onsite_checkouts_allowed = $branch_borrower_circ_rule->{patron_maxonsiteissueqty};
 
-        if ( $onsite_checkout and defined $max_onsite_checkouts_allowed ) {
+        if ( $onsite_checkout and $max_onsite_checkouts_allowed ne '' ) {
             if ( $onsite_checkout_count >= $max_onsite_checkouts_allowed )  {
                 return {
                     reason => 'TOO_MANY_ONSITE_CHECKOUTS',
@@ -1617,30 +1617,6 @@ wildcards.
 sub GetBranchBorrowerCircRule {
     my ( $branchcode, $categorycode ) = @_;
 
-    # Set search prededences
-    my @params = (
-        {
-            branchcode   => $branchcode,
-            categorycode => $categorycode,
-            itemtype     => undef,
-        },
-        {
-            branchcode   => $branchcode,
-            categorycode => undef,
-            itemtype     => undef,
-        },
-        {
-            branchcode   => undef,
-            categorycode => $categorycode,
-            itemtype     => undef,
-        },
-        {
-            branchcode   => undef,
-            categorycode => undef,
-            itemtype     => undef,
-        },
-    );
-
     # Initialize default values
     my $rules = {
         patron_maxissueqty       => undef,
@@ -1649,19 +1625,16 @@ sub GetBranchBorrowerCircRule {
 
     # Search for rules!
     foreach my $rule_name (qw( patron_maxissueqty patron_maxonsiteissueqty )) {
-        foreach my $params (@params) {
-            my $rule = Koha::CirculationRules->search(
-                {
-                    rule_name => $rule_name,
-                    %$params,
-                }
-            )->next();
-
-            if ( $rule ) {
-                $rules->{$rule_name} = $rule->rule_value;
-                last;
+        my $rule = Koha::CirculationRules->get_effective_rule(
+            {
+                categorycode => $categorycode,
+                itemtype     => undef,
+                branchcode   => $branchcode,
+                rule_name    => $rule_name,
             }
-        }
+        );
+
+        $rules->{$rule_name} = $rule->rule_value if defined $rule;
     }
 
     return $rules;