From ef66f7e05099450b2dcd8bfed00b94a8531e0408 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Thu, 2 Nov 2017 13:22:31 -0300
Subject: [PATCH] Bug 18357: Handle unlimited on-site checkouts

If on-site checkouts are set to unlimited (i.e. NULL/undef), they are
currently blocked.

Test plan:
1/ Set All/All rule with Unlimited/unlimited for normal/onsite checkouts
2/ Will be able to perform onsite checkout
3/ Edit rule to be 15/Unlimited normal/onsite
4/ Will be able to perform onsite checkout
=> Without this patch it was blocked
5/ Set rule to 15/15
6/ Onsite checkouts work again
---
 C4/Circulation.pm                    |  4 +--
 t/db_dependent/Circulation/TooMany.t | 55 +++++++++++++++++++++++++++++++++++-
 2 files changed, 56 insertions(+), 3 deletions(-)

diff --git a/C4/Circulation.pm b/C4/Circulation.pm
index 8bd8d85946..67969f6b93 100644
--- a/C4/Circulation.pm
+++ b/C4/Circulation.pm
@@ -474,7 +474,7 @@ sub TooMany {
         my $max_checkouts_allowed = $issuing_rule->maxissueqty;
         my $max_onsite_checkouts_allowed = $issuing_rule->maxonsiteissueqty;
 
-        if ( $onsite_checkout ) {
+        if ( $onsite_checkout and defined $max_onsite_checkouts_allowed ) {
             if ( $onsite_checkout_count >= $max_onsite_checkouts_allowed )  {
                 return {
                     reason => 'TOO_MANY_ONSITE_CHECKOUTS',
@@ -528,7 +528,7 @@ sub TooMany {
         my $max_checkouts_allowed = $branch_borrower_circ_rule->{maxissueqty};
         my $max_onsite_checkouts_allowed = $branch_borrower_circ_rule->{maxonsiteissueqty};
 
-        if ( $onsite_checkout ) {
+        if ( $onsite_checkout and defined $max_onsite_checkouts_allowed ) {
             if ( $onsite_checkout_count >= $max_onsite_checkouts_allowed )  {
                 return {
                     reason => 'TOO_MANY_ONSITE_CHECKOUTS',
diff --git a/t/db_dependent/Circulation/TooMany.t b/t/db_dependent/Circulation/TooMany.t
index 7ce7e8e5fb..193752ff26 100644
--- a/t/db_dependent/Circulation/TooMany.t
+++ b/t/db_dependent/Circulation/TooMany.t
@@ -15,7 +15,7 @@
 # with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
-use Test::More tests => 6;
+use Test::More tests => 7;
 use C4::Context;
 
 use C4::Biblio;
@@ -159,6 +159,59 @@ subtest '1 Issuingrule exist 0 0: no issue allowed' => sub {
     teardown();
 };
 
+subtest '1 Issuingrule exist with onsiteissueqty=unlimited' => sub {
+    plan tests => 4;
+    my $issuingrule = $builder->build({
+        source => 'Issuingrule',
+        value => {
+            branchcode         => $branch->{branchcode},
+            categorycode       => $category->{categorycode},
+            itemtype           => '*',
+            maxissueqty        => 1,
+            maxonsiteissueqty  => undef,
+        },
+    });
+    my $issue = C4::Circulation::AddIssue( $patron, $item->{barcode}, dt_from_string() );
+    t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 0);
+    is_deeply(
+        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
+        {
+            reason => 'TOO_MANY_CHECKOUTS',
+            count => 1,
+            max_allowed => 1,
+        },
+        'CO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
+    );
+    is(
+        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
+        undef,
+        'OSCO should be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
+    );
+
+    t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 1);
+    is_deeply(
+        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
+        {
+            reason => 'TOO_MANY_CHECKOUTS',
+            count => 1,
+            max_allowed => 1,
+        },
+        'CO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
+    );
+    is_deeply(
+        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
+        {
+            reason => 'TOO_MANY_CHECKOUTS',
+            count => 1,
+            max_allowed => 1,
+        },
+        'OSCO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
+    );
+
+    teardown();
+};
+
+
 subtest '1 Issuingrule exist 1 1: issue is allowed' => sub {
     plan tests => 4;
     my $issuingrule = $builder->build({
-- 
2.11.0