Bug 18925: Move maxissueqty and maxonsiteissueqty to circulation_rules
authorKyle M Hall <kyle@bywatersolutions.com>
Mon, 10 Jul 2017 15:00:44 +0000 (11:00 -0400)
committerNick Clemens <nick@bywatersolutions.com>
Tue, 5 Mar 2019 20:41:42 +0000 (20:41 +0000)
This patch set moves maxissueqty and maxonsiteissueqty to the
circulation_rules table.

Test Plan:
1) Apply this patch
2) Run updatedatabase
3) prove t/db_dependent/Circulation.t
4) prove t/db_dependent/Circulation/Branch.t
5) prove t/db_dependent/Circulation/GetHardDueDate.t
6) prove t/db_dependent/Circulation/Returns.t
7) prove t/db_dependent/Circulation/SwitchOnSiteCheckouts.t
8) prove t/db_dependent/Circulation/TooMany.t
9) prove t/db_dependent/Holds/DisallowHoldIfItemsAvailable.t
10) prove t/db_dependent/Reserves.t
11) Note no changes in circulation behavior related to check out limis
    both on and off site

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
14 files changed:
C4/Circulation.pm
Koha/CirculationRules.pm
admin/smart-rules.pl
installer/data/mysql/atomicupdate/bug_18925.perl [new file with mode: 0644]
installer/data/mysql/kohastructure.sql
koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tt
t/db_dependent/Circulation.t
t/db_dependent/Circulation/Branch.t
t/db_dependent/Circulation/GetHardDueDate.t
t/db_dependent/Circulation/Returns.t
t/db_dependent/Circulation/SwitchOnSiteCheckouts.t
t/db_dependent/Circulation/TooMany.t
t/db_dependent/Holds/DisallowHoldIfItemsAvailable.t
t/db_dependent/Reserves.t

index 9291cb7..66c208a 100644 (file)
@@ -389,10 +389,20 @@ sub TooMany {
  
     # given branch, patron category, and item type, determine
     # applicable issuing rule
-    my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule(
-        {   categorycode => $cat_borrower,
+    my $maxissueqty_rule = Koha::CirculationRules->get_effective_rule(
+        {
+            categorycode => $cat_borrower,
+            itemtype     => $type,
+            branchcode   => $branch,
+            rule_name    => 'maxissueqty',
+        }
+    );
+    my $maxonsiteissueqty_rule = Koha::CirculationRules->get_effective_rule(
+        {
+            categorycode => $cat_borrower,
             itemtype     => $type,
-            branchcode   => $branch
+            branchcode   => $branch,
+            rule_name    => 'maxonsiteissueqty',
         }
     );
 
@@ -400,7 +410,7 @@ sub TooMany {
     # if a rule is found and has a loan limit set, count
     # how many loans the patron already has that meet that
     # rule
-    if (defined($issuing_rule) and defined($issuing_rule->maxissueqty)) {
+    if (defined($maxissueqty_rule) and defined($maxissueqty_rule->rule_value)) {
         my @bind_params;
         my $count_query = q|
             SELECT COUNT(*) AS total, COALESCE(SUM(onsite_checkout), 0) AS onsite_checkouts
@@ -408,7 +418,7 @@ sub TooMany {
             JOIN items USING (itemnumber)
         |;
 
-        my $rule_itemtype = $issuing_rule->itemtype;
+        my $rule_itemtype = $maxissueqty_rule->itemtype;
         if ($rule_itemtype eq "*") {
             # matching rule has the default item type, so count only
             # those existing loans that don't fall under a more
@@ -429,8 +439,8 @@ sub TooMany {
                                     AND   itemtype <> '*'
                                   ) ";
             }
-            push @bind_params, $issuing_rule->branchcode;
-            push @bind_params, $issuing_rule->categorycode;
+            push @bind_params, $maxissueqty_rule->branchcode;
+            push @bind_params, $maxissueqty_rule->categorycode;
             push @bind_params, $cat_borrower;
         } else {
             # rule has specific item type, so count loans of that
@@ -446,7 +456,7 @@ sub TooMany {
 
         $count_query .= " AND borrowernumber = ? ";
         push @bind_params, $borrower->{'borrowernumber'};
-        my $rule_branch = $issuing_rule->branchcode;
+        my $rule_branch = $maxissueqty_rule->branchcode;
         if ($rule_branch ne "*") {
             if (C4::Context->preference('CircControl') eq 'PickupLibrary') {
                 $count_query .= " AND issues.branchcode = ? ";
@@ -461,8 +471,8 @@ sub TooMany {
 
         my ( $checkout_count, $onsite_checkout_count ) = $dbh->selectrow_array( $count_query, {}, @bind_params );
 
-        my $max_checkouts_allowed = $issuing_rule->maxissueqty;
-        my $max_onsite_checkouts_allowed = $issuing_rule->maxonsiteissueqty;
+        my $max_checkouts_allowed = $maxissueqty_rule ? $maxissueqty_rule->rule_value : 0;
+        my $max_onsite_checkouts_allowed = $maxonsiteissueqty_rule ? $maxonsiteissueqty_rule->rule_value : 0;
 
         if ( $onsite_checkout and defined $max_onsite_checkouts_allowed ) {
             if ( $onsite_checkout_count >= $max_onsite_checkouts_allowed )  {
@@ -547,7 +557,7 @@ sub TooMany {
         }
     }
 
-    if ( not defined( $issuing_rule ) and not defined($branch_borrower_circ_rule->{maxissueqty}) ) {
+    if ( not defined( $maxissueqty_rule ) and not defined($branch_borrower_circ_rule->{maxissueqty}) ) {
         return { reason => 'NO_RULE_DEFINED', max_allowed => 0 };
     }
 
@@ -1586,14 +1596,11 @@ maxonsiteissueqty - maximum of on-site checkouts that a
 patron of the given category can have at the given
 branch.  If the value is undef, no limit.
 
-This will first check for a specific branch and
-category match from branch_borrower_circ_rules. 
-
-If no rule is found, it will then check default_branch_circ_rules
-(same branch, default category).  If no rule is found,
-it will then check default_borrower_circ_rules (default 
-branch, same category), then failing that, default_circ_rules
-(default branch, default category).
+This will check for different branch/category combinations in the following order:
+branch and category
+branch only
+category only
+default branch and category
 
 If no rule has been found in the database, it will default to
 the buillt in rule:
@@ -1610,44 +1617,54 @@ wildcards.
 sub GetBranchBorrowerCircRule {
     my ( $branchcode, $categorycode ) = @_;
 
-    my $rules;
-    my $dbh = C4::Context->dbh();
-    $rules = $dbh->selectrow_hashref( q|
-        SELECT maxissueqty, maxonsiteissueqty
-        FROM branch_borrower_circ_rules
-        WHERE branchcode = ?
-        AND   categorycode = ?
-    |, {}, $branchcode, $categorycode ) ;
-    return $rules if $rules;
-
-    # try same branch, default borrower category
-    $rules = $dbh->selectrow_hashref( q|
-        SELECT maxissueqty, maxonsiteissueqty
-        FROM default_branch_circ_rules
-        WHERE branchcode = ?
-    |, {}, $branchcode ) ;
-    return $rules if $rules;
-
-    # try default branch, same borrower category
-    $rules = $dbh->selectrow_hashref( q|
-        SELECT maxissueqty, maxonsiteissueqty
-        FROM default_borrower_circ_rules
-        WHERE categorycode = ?
-    |, {}, $categorycode ) ;
-    return $rules if $rules;
-
-    # try default branch, default borrower category
-    $rules = $dbh->selectrow_hashref( q|
-        SELECT maxissueqty, maxonsiteissueqty
-        FROM default_circ_rules
-    |, {} );
-    return $rules if $rules;
+    # 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,
+        },
+    );
 
-    # built-in default circulation rule
-    return {
-        maxissueqty => undef,
+    # Initialize default values
+    my $rules = {
+        maxissueqty       => undef,
         maxonsiteissueqty => undef,
     };
+
+    # Search for rules!
+    foreach my $rule_name (qw( maxissueqty 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;
+            }
+        }
+    }
+
+    return $rules;
 }
 
 =head2 GetBranchItemRule
index c800673..82becb1 100644 (file)
@@ -145,17 +145,21 @@ sub set_rules {
     my $itemtype     = $params->{itemtype};
     my $rules        = $params->{rules};
 
-    foreach my $rule (@$rules) {
-        Koha::CirculationRules->set_rule(
+    my $rule_objects = [];
+    while ( my ( $rule_name, $rule_value ) = each %$rules ) {
+        my $rule_object = Koha::CirculationRules->set_rule(
             {
                 branchcode   => $branchcode,
                 categorycode => $categorycode,
                 itemtype     => $itemtype,
-                rule_name    => $rule->{rule_name},
-                rule_value   => $rule->{rule_value},
+                rule_name    => $rule_name,
+                rule_value   => $rule_value,
             }
         );
+        push( @$rule_objects, $rule_object );
     }
+
+    return $rule_objects;
 }
 
 =head3 type
index fcb5b7e..76fed0a 100755 (executable)
@@ -91,28 +91,22 @@ elsif ($op eq 'delete-branch-cat') {
         if ($categorycode eq "*") {
             my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
             $sth_delete->execute();
-        } else {
-            my $sth_delete = $dbh->prepare("DELETE FROM default_borrower_circ_rules
-                                            WHERE categorycode = ?");
-            $sth_delete->execute($categorycode);
         }
     } elsif ($categorycode eq "*") {
         my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
                                         WHERE branchcode = ?");
         $sth_delete->execute($branch);
-    } else {
-        my $sth_delete = $dbh->prepare("DELETE FROM branch_borrower_circ_rules
-                                        WHERE branchcode = ?
-                                        AND categorycode = ?");
-        $sth_delete->execute($branch, $categorycode);
     }
-    Koha::CirculationRules->set_rule(
+    Koha::CirculationRules->set_rules(
         {
-            branchcode   => $branch,
-            categorycode => $categorycode,
+            categorycode => $categorycode eq '*' ? undef : $categorycode,
+            branchcode   => $branch eq '*'       ? undef : $branch,
             itemtype     => undef,
-            rule_name    => 'max_holds',
-            rule_value   => undef,
+            rules        => {
+                max_holds         => undef,
+                maxissueqty       => undef,
+                maxonsiteissueqty => undef,
+            }
         }
     );
 }
@@ -199,8 +193,6 @@ elsif ($op eq 'add') {
         firstremind                   => $firstremind,
         chargeperiod                  => $chargeperiod,
         chargeperiod_charge_at        => $chargeperiod_charge_at,
-        maxissueqty                   => $maxissueqty,
-        maxonsiteissueqty             => $maxonsiteissueqty,
         renewalsallowed               => $renewalsallowed,
         renewalperiod                 => $renewalperiod,
         norenewalbefore               => $norenewalbefore,
@@ -230,6 +222,18 @@ elsif ($op eq 'add') {
         Koha::IssuingRule->new()->set($params)->store();
     }
 
+    Koha::CirculationRules->set_rules(
+        {
+            categorycode => $bor,
+            itemtype     => $itemtype,
+            branchcode   => $br,
+            rules        => {
+                maxissueqty       => $maxissueqty,
+                maxonsiteissueqty => $maxonsiteissueqty,
+            }
+        }
+    );
+
 }
 elsif ($op eq "set-branch-defaults") {
     my $categorycode  = $input->param('categorycode');
@@ -252,35 +256,59 @@ elsif ($op eq "set-branch-defaults") {
         my $sth_search = $dbh->prepare("SELECT count(*) AS total
                                         FROM default_circ_rules");
         my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
-                                        (maxissueqty, maxonsiteissueqty, holdallowed, hold_fulfillment_policy, returnbranch)
-                                        VALUES (?, ?, ?, ?, ?)");
+                                        (holdallowed, hold_fulfillment_policy, returnbranch)
+                                        VALUES (?, ?, ?)");
         my $sth_update = $dbh->prepare("UPDATE default_circ_rules
-                                        SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?");
+                                        SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?");
 
         $sth_search->execute();
         my $res = $sth_search->fetchrow_hashref();
         if ($res->{total}) {
-            $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch);
+            $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch);
         } else {
-            $sth_insert->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch);
+            $sth_insert->execute($holdallowed, $hold_fulfillment_policy, $returnbranch);
         }
+
+        Koha::CirculationRules->set_rules(
+            {
+                categorycode => undef,
+                itemtype     => undef,
+                branchcode   => undef,
+                rules        => {
+                    maxissueqty       => $maxissueqty,
+                    maxonsiteissueqty => $maxonsiteissueqty,
+                }
+            }
+        );
     } else {
         my $sth_search = $dbh->prepare("SELECT count(*) AS total
                                         FROM default_branch_circ_rules
                                         WHERE branchcode = ?");
         my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
-                                        (branchcode, maxissueqty, maxonsiteissueqty, holdallowed, hold_fulfillment_policy, returnbranch)
-                                        VALUES (?, ?, ?, ?, ?, ?)");
+                                        (branchcode, holdallowed, hold_fulfillment_policy, returnbranch)
+                                        VALUES (?, ?, ?, ?)");
         my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
-                                        SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
+                                        SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
                                         WHERE branchcode = ?");
         $sth_search->execute($branch);
         my $res = $sth_search->fetchrow_hashref();
         if ($res->{total}) {
-            $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch, $branch);
+            $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $branch);
         } else {
-            $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch);
+            $sth_insert->execute($branch, $holdallowed, $hold_fulfillment_policy, $returnbranch);
         }
+
+        Koha::CirculationRules->set_rules(
+            {
+                categorycode => undef,
+                itemtype     => undef,
+                branchcode   => $branch,
+                rules        => {
+                    maxissueqty       => $maxissueqty,
+                    maxonsiteissueqty => $maxonsiteissueqty,
+                }
+            }
+        );
     }
     Koha::CirculationRules->set_rule(
         {
@@ -306,125 +334,56 @@ elsif ($op eq "add-branch-cat") {
 
     if ($branch eq "*") {
         if ($categorycode eq "*") {
-            #FIXME This block is will probably be never used
-            my $sth_search = $dbh->prepare("SELECT count(*) AS total
-                                            FROM default_circ_rules");
-            my $sth_insert = $dbh->prepare(q|
-                INSERT INTO default_circ_rules
-                    (maxissueqty, maxonsiteissueqty)
-                    VALUES (?, ?)
-            |);
-            my $sth_update = $dbh->prepare(q|
-                UPDATE default_circ_rules
-                SET maxissueqty = ?,
-                    maxonsiteissueqty = ?,
-            |);
-
-            $sth_search->execute();
-            my $res = $sth_search->fetchrow_hashref();
-            if ($res->{total}) {
-                $sth_update->execute( $maxissueqty, $maxonsiteissueqty );
-            } else {
-                $sth_insert->execute( $maxissueqty, $maxonsiteissueqty );
-            }
-
-            Koha::CirculationRules->set_rule(
+            Koha::CirculationRules->set_rules(
                 {
-                    branchcode   => undef,
                     categorycode => undef,
                     itemtype     => undef,
-                    rule_name    => 'max_holds',
-                    rule_value   => $max_holds,
+                    branchcode   => undef,
+                    rules        => {
+                        max_holds         => $max_holds,
+                        maxissueqty       => $maxissueqty,
+                        maxonsiteissueqty => $maxonsiteissueqty,
+                    }
                 }
             );
         } else {
-            my $sth_search = $dbh->prepare("SELECT count(*) AS total
-                                            FROM default_borrower_circ_rules
-                                            WHERE categorycode = ?");
-            my $sth_insert = $dbh->prepare(q|
-                INSERT INTO default_borrower_circ_rules
-                    (categorycode, maxissueqty, maxonsiteissueqty)
-                    VALUES ( ?, ?, ?)
-            |);
-            my $sth_update = $dbh->prepare(q|
-                UPDATE default_borrower_circ_rules
-                SET maxissueqty = ?,
-                    maxonsiteissueqty = ?,
-                WHERE categorycode = ?
-            |);
-            $sth_search->execute($categorycode);
-            my $res = $sth_search->fetchrow_hashref();
-            if ($res->{total}) {
-                $sth_update->execute( $maxissueqty, $maxonsiteissueqty, $categorycode );
-            } else {
-                $sth_insert->execute( $categorycode, $maxissueqty, $maxonsiteissueqty );
-            }
-
-            Koha::CirculationRules->set_rule(
+            Koha::CirculationRules->set_rules(
                 {
                     branchcode   => undef,
                     categorycode => $categorycode,
                     itemtype     => undef,
-                    rule_name    => 'max_holds',
-                    rule_value   => $max_holds,
+                    rules        => {
+                        max_holds         => $max_holds,
+                        maxissueqty       => $maxissueqty,
+                        maxonsiteissueqty => $maxonsiteissueqty,
+                    }
                 }
             );
         }
     } elsif ($categorycode eq "*") {
-        my $sth_search = $dbh->prepare("SELECT count(*) AS total
-                                        FROM default_branch_circ_rules
-                                        WHERE branchcode = ?");
-        my $sth_insert = $dbh->prepare(q|
-            INSERT INTO default_branch_circ_rules
-            (branchcode, maxissueqty, maxonsiteissueqty)
-            VALUES (?, ?, ?)
-        |);
-        my $sth_update = $dbh->prepare(q|
-            UPDATE default_branch_circ_rules
-            SET maxissueqty = ?,
-                maxonsiteissueqty = ?
-            WHERE branchcode = ?
-        |);
-        $sth_search->execute($branch);
-        my $res = $sth_search->fetchrow_hashref();
-        if ($res->{total}) {
-            $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch);
-        } else {
-            $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty);
-        }
-    } else {
-        my $sth_search = $dbh->prepare("SELECT count(*) AS total
-                                        FROM branch_borrower_circ_rules
-                                        WHERE branchcode = ?
-                                        AND   categorycode = ?");
-        my $sth_insert = $dbh->prepare(q|
-            INSERT INTO branch_borrower_circ_rules
-            (branchcode, categorycode, maxissueqty, maxonsiteissueqty)
-            VALUES (?, ?, ?, ?)
-        |);
-        my $sth_update = $dbh->prepare(q|
-            UPDATE branch_borrower_circ_rules
-            SET maxissueqty = ?,
-                maxonsiteissueqty = ?
-            WHERE branchcode = ?
-            AND categorycode = ?
-        |);
-
-        $sth_search->execute($branch, $categorycode);
-        my $res = $sth_search->fetchrow_hashref();
-        if ($res->{total}) {
-            $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch, $categorycode);
-        } else {
-            $sth_insert->execute($branch, $categorycode, $maxissueqty, $maxonsiteissueqty);
-        }
-
-        Koha::CirculationRules->set_rule(
+        Koha::CirculationRules->set_rules(
             {
+                categorycode => undef,
+                itemtype     => undef,
                 branchcode   => $branch,
+                rules        => {
+                    max_holds         => $max_holds,
+                    maxissueqty       => $maxissueqty,
+                    maxonsiteissueqty => $maxonsiteissueqty,
+                }
+            }
+        );
+    } else {
+        Koha::CirculationRules->set_rules(
+            {
                 categorycode => $categorycode,
                 itemtype     => undef,
-                rule_name    => 'max_holds',
-                rule_value   => $max_holds,
+                branchcode   => $branch,
+                rules        => {
+                    max_holds         => $max_holds,
+                    maxissueqty       => $maxissueqty,
+                    maxonsiteissueqty => $maxonsiteissueqty,
+                }
             }
         );
     }
@@ -592,41 +551,6 @@ while (my $row = $sth2->fetchrow_hashref) {
 
 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
 
-my $sth_branch_cat;
-if ($branch eq "*") {
-    $sth_branch_cat = $dbh->prepare("
-        SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
-        FROM default_borrower_circ_rules
-        JOIN categories USING (categorycode)
-
-    ");
-    $sth_branch_cat->execute();
-} else {
-    $sth_branch_cat = $dbh->prepare("
-        SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
-        FROM branch_borrower_circ_rules
-        JOIN categories USING (categorycode)
-        WHERE branch_borrower_circ_rules.branchcode = ?
-    ");
-    $sth_branch_cat->execute($branch);
-}
-
-my @branch_cat_rules = ();
-while (my $row = $sth_branch_cat->fetchrow_hashref) {
-    push @branch_cat_rules, $row;
-}
-my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
-
-# note undef maxissueqty so that template can deal with them
-foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
-    $entry->{unlimited_maxissueqty}       = 1 unless defined($entry->{maxissueqty});
-    $entry->{unlimited_maxonsiteissueqty} = 1 unless defined($entry->{maxonsiteissueqty});
-    $entry->{unlimited_max_holds}         = 1 unless defined($entry->{max_holds});
-    $entry->{unlimited_holds_per_day}     = 1 unless defined($entry->{holds_per_day});
-}
-
-@sorted_row_loop = sort by_category_and_itemtype @row_loop;
-
 my $sth_branch_item;
 if ($branch eq "*") {
     $sth_branch_item = $dbh->prepare("
@@ -667,7 +591,6 @@ foreach my $entry (@sorted_branch_item_rules) {
 
 $template->param(show_branch_cat_rule_form => 1);
 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
-$template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
 
 my $sth_defaults;
 if ($branch eq "*") {
diff --git a/installer/data/mysql/atomicupdate/bug_18925.perl b/installer/data/mysql/atomicupdate/bug_18925.perl
new file mode 100644 (file)
index 0000000..b80dd29
--- /dev/null
@@ -0,0 +1,75 @@
+$DBversion = 'XXX';  # will be replaced by the RM
+if( CheckVersion( $DBversion ) ) {
+    if ( column_exists( 'branch_borrower_circ_rules', 'maxissueqty' ) ) {
+        $dbh->do("
+            INSERT INTO circulation_rules ( categorycode, branchcode, itemtype, rule_name, rule_value )
+            SELECT categorycode, branchcode, NULL, 'maxissueqty', maxissueqty
+            FROM branch_borrower_circ_rules
+        ");
+        $dbh->do("
+            INSERT INTO circulation_rules ( categorycode, branchcode, itemtype, rule_name, rule_value )
+            SELECT categorycode, branchcode, NULL, 'maxonsiteissueqty', maxonsiteissueqty
+            FROM branch_borrower_circ_rules
+        ");
+        $dbh->do("DROP TABLE branch_borrower_circ_rules");
+    }
+
+    if ( column_exists( 'default_borrower_circ_rules', 'maxissueqty' ) ) {
+        $dbh->do("
+            INSERT INTO circulation_rules ( categorycode, branchcode, itemtype, rule_name, rule_value )
+            SELECT categorycode, NULL, NULL, 'maxissueqty', maxissueqty
+            FROM default_borrower_circ_rules
+        ");
+        $dbh->do("
+            INSERT INTO circulation_rules ( categorycode, branchcode, itemtype, rule_name, rule_value )
+            SELECT categorycode, NULL, NULL, 'maxonsiteissueqty', maxonsiteissueqty
+            FROM default_borrower_circ_rules
+        ");
+        $dbh->do("DROP TABLE default_borrower_circ_rules");
+    }
+
+    if ( column_exists( 'default_circ_rules', 'maxissueqty' ) ) {
+        $dbh->do("
+            INSERT INTO circulation_rules ( categorycode, branchcode, itemtype, rule_name, rule_value )
+            SELECT NULL, NULL, NULL, 'maxissueqty', maxissueqty
+            FROM default_circ_rules
+        ");
+        $dbh->do("
+            INSERT INTO circulation_rules ( categorycode, branchcode, itemtype, rule_name, rule_value )
+            SELECT NULL, NULL, NULL, 'maxonsiteissueqty', maxonsiteissueqty
+            FROM default_circ_rules
+        ");
+        $dbh->do("ALTER TABLE default_circ_rules DROP COLUMN maxissueqty, DROP COLUMN maxonsiteissueqty");
+    }
+
+    if ( column_exists( 'default_branch_circ_rules', 'maxissueqty' ) ) {
+        $dbh->do("
+            INSERT INTO circulation_rules ( categorycode, branchcode, itemtype, rule_name, rule_value )
+            SELECT NULL, branchcode, NULL, 'maxissueqty', maxissueqty
+            FROM default_branch_circ_rules
+        ");
+        $dbh->do("
+            INSERT INTO circulation_rules ( categorycode, branchcode, itemtype, rule_name, rule_value )
+            SELECT NULL, NULL, NULL, 'maxonsiteissueqty', maxonsiteissueqty
+            FROM default_branch_circ_rules
+        ");
+        $dbh->do("ALTER TABLE default_branch_circ_rules DROP COLUMN maxissueqty, DROP COLUMN maxonsiteissueqty");
+    }
+
+    if ( column_exists( 'issuingrules', 'maxissueqty' ) ) {
+        $dbh->do("
+            INSERT INTO circulation_rules ( categorycode, branchcode, itemtype, rule_name, rule_value )
+            SELECT categorycode, branchcode, itemtype, 'maxissueqty', maxissueqty
+            FROM issuingrules
+        ");
+        $dbh->do("
+            INSERT INTO circulation_rules ( categorycode, branchcode, itemtype, rule_name, rule_value )
+            SELECT categorycode, branchcode, itemtype, 'maxonsiteissueqty', maxonsiteissueqty
+            FROM issuingrules
+        ");
+        $dbh->do("ALTER TABLE issuingrules DROP COLUMN maxissueqty, DROP COLUMN maxonsiteissueqty");
+    }
+
+    SetVersion( $DBversion );
+    print "Upgrade to $DBversion done (Bug 18925 - Move maxissueqty and maxonsiteissueqty to circulation_rules)\n";
+}
index 76798c3..16a1a68 100644 (file)
@@ -352,37 +352,6 @@ CREATE TABLE collections_tracking (
   PRIMARY KEY (collections_tracking_id)
 ) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
 
---
--- Table structure for table `branch_borrower_circ_rules`
---
-
-DROP TABLE IF EXISTS `branch_borrower_circ_rules`;
-CREATE TABLE `branch_borrower_circ_rules` ( -- includes default circulation rules for patron categories found under "Checkout limit by patron category"
-  `branchcode` VARCHAR(10) NOT NULL, -- the branch this rule applies to (branches.branchcode)
-  `categorycode` VARCHAR(10) NOT NULL, -- the patron category this rule applies to (categories.categorycode)
-  `maxissueqty` int(4) default NULL, -- the maximum number of checkouts this patron category can have at this branch
-  `maxonsiteissueqty` int(4) default NULL, -- the maximum number of on-site checkouts this patron category can have at this branch
-  PRIMARY KEY (`categorycode`, `branchcode`),
-  CONSTRAINT `branch_borrower_circ_rules_ibfk_1` FOREIGN KEY (`categorycode`) REFERENCES `categories` (`categorycode`)
-    ON DELETE CASCADE ON UPDATE CASCADE,
-  CONSTRAINT `branch_borrower_circ_rules_ibfk_2` FOREIGN KEY (`branchcode`) REFERENCES `branches` (`branchcode`)
-    ON DELETE CASCADE ON UPDATE CASCADE
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-
---
--- Table structure for table `default_borrower_circ_rules`
---
-
-DROP TABLE IF EXISTS `default_borrower_circ_rules`;
-CREATE TABLE `default_borrower_circ_rules` ( -- default checkout rules found under "Default checkout, hold and return policy"
-  `categorycode` VARCHAR(10) NOT NULL, -- patron category this rul
-  `maxissueqty` int(4) default NULL,
-  `maxonsiteissueqty` int(4) default NULL,
-  PRIMARY KEY (`categorycode`),
-  CONSTRAINT `borrower_borrower_circ_rules_ibfk_1` FOREIGN KEY (`categorycode`) REFERENCES `categories` (`categorycode`)
-    ON DELETE CASCADE ON UPDATE CASCADE
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-
 --
 -- Table structure for table `default_branch_circ_rules`
 --
@@ -390,8 +359,6 @@ CREATE TABLE `default_borrower_circ_rules` ( -- default checkout rules found und
 DROP TABLE IF EXISTS `default_branch_circ_rules`;
 CREATE TABLE `default_branch_circ_rules` (
   `branchcode` VARCHAR(10) NOT NULL,
-  `maxissueqty` int(4) default NULL,
-  `maxonsiteissueqty` int(4) default NULL,
   `holdallowed` tinyint(1) default NULL,
   hold_fulfillment_policy ENUM('any', 'homebranch', 'holdingbranch') NOT NULL DEFAULT 'any', -- limit trapping of holds by branchcode
   `returnbranch` varchar(15) default NULL,
@@ -407,8 +374,6 @@ CREATE TABLE `default_branch_circ_rules` (
 DROP TABLE IF EXISTS `default_circ_rules`;
 CREATE TABLE `default_circ_rules` (
     `singleton` enum('singleton') NOT NULL default 'singleton',
-    `maxissueqty` int(4) default NULL,
-    `maxonsiteissueqty` int(4) default NULL,
     `holdallowed` int(1) default NULL,
     hold_fulfillment_policy ENUM('any', 'homebranch', 'holdingbranch') NOT NULL DEFAULT 'any', -- limit trapping of holds by branchcode
     `returnbranch` varchar(15) default NULL,
index f42a63b..98d857f 100644 (file)
@@ -1,8 +1,17 @@
 [% USE raw %]
 [% USE Asset %]
 [% USE Branches %]
+[% USE Categories %]
 [% USE CirculationRules %]
 [% SET footerjs = 1 %]
+
+[% SET branchcode = humanbranch %]
+
+[% SET categorycodes = ['*'] %]
+[% FOREACH pc IN patron_categories %]
+    [% categorycodes.push( pc.id ) %]
+[% END %]
+
 [% INCLUDE 'doc-head-open.inc' %]
 <title>Koha &rsaquo; Administration &rsaquo; Circulation and fine rules</title>
 [% INCLUDE 'doc-head-close.inc' %]
                                                                 <a name="viewnote" data-toggle="popover" title="Note" data-content="[% rule.note | html %]" data-placement="top" data-trigger="hover">View note</a>
                                                             [% ELSE %]&nbsp;[% END %]
                                                         </td>
-                                                       <td>[% IF ( rule.unlimited_maxissueqty ) %]
-                                                                       <span>Unlimited</span>
-                                                               [% ELSE %]
-                                                                       [% rule.maxissueqty | html %]
-                                                               [% END %]
-                                                       </td>
-                            <td>[% IF rule.unlimited_maxonsiteissueqty %]
+                                                       <td>
+                                [% SET rule_value = CirculationRules.Get( rule.branchcode, rule.categorycode, rule.itemtype, 'maxissueqty' ) %]
+                                [% IF rule_value  %]
+                                    [% rule_value | html %]
+                                [% ELSE %]
                                     <span>Unlimited</span>
+                                [% END %]
+                                                       </td>
+                                                       <td>
+                                [% SET rule_value = CirculationRules.Get( rule.branchcode, rule.categorycode, rule.itemtype, 'maxonsiteissueqty' ) %]
+                                [% IF rule_value  %]
+                                    [% rule_value | html %]
                                 [% ELSE %]
-                                    [% rule.maxonsiteissueqty | html %]
+                                    <span>Unlimited</span>
                                 [% END %]
-                            </td>
+                                                       </td>
                                                        <td>[% rule.issuelength | html %]</td>
                                                        <td>
                                                            [% rule.lengthunit | html %]
                 </tr>
                 <tr>
                     <td><em>Defaults[% UNLESS ( default_rules ) %] (not set)[% END %]</em></td>
-                    <td><input type="text" name="maxissueqty" size="3" value="[% default_maxissueqty | html %]"/></td>
-                    <td><input type="text" name="maxonsiteissueqty" size="3" value="[% default_maxonsiteissueqty | html %]"/></td>
+                    <td>
+                        [% SET maxissueqty  = CirculationRules.Get( branchcode, undef, undef, 'maxissueqty' ) %]
+                        <input type="text" name="maxissueqty" size="3" value="[% maxissueqty | html %]"/>
+                    </td>
+                    <td>
+                        [% SET maxonsiteissueqty  = CirculationRules.Get( branchcode, undef, undef, 'maxonsiteissueqty' ) %]
+                        <input type="text" name="maxonsiteissueqty" size="3" value="[% maxonsiteissueqty | html %]"/>
+                    </td>
                     <td>
                         [% SET rule_value = CirculationRules.Get( current_branch, '*', undef, 'max_holds' ) %]
                         <input name="max_holds" size="3" value="[% rule_value | html %]" />
                     <th>Total holds allowed</th>
                     <th>&nbsp;</th>
                 </tr>
-                [% FOREACH branch_cat_rule_loo IN branch_cat_rule_loop %]
-                    [% UNLESS ( loop.odd ) %]
-                    <tr class="highlight">
-                    [% ELSE %]
+                [% FOREACH c IN categorycodes %]
+                    [% SET maxissueqty = CirculationRules.Get( branchcode, c, undef, 'maxissueqty' ) %]
+                    [% SET maxonsiteissueqty = CirculationRules.Get( branchcode, c, undef, 'maxonsiteissueqty' ) %]
+                    [% SET max_holds = CirculationRules.Get( branchcode, c, undef, 'max_holds' ) %]
+
+                    [% IF maxissueqty || maxissueqty || max_holds %]
                     <tr>
-                    [% END %]
-                        <td>[% IF ( branch_cat_rule_loo.default_humancategorycode ) %]
+                        <td>
+                            [% IF c == '*'%]
                                 <em>Default</em>
                             [% ELSE %]
-                                [% branch_cat_rule_loo.humancategorycode | html %]
+                                [% Categories.GetName(c) | html %]
                             [% END %]
                         </td>
-                        <td>[% IF ( branch_cat_rule_loo.unlimited_maxissueqty ) %]
-                                <span>Unlimited</span>
+                        <td>
+                            [% IF maxissueqty  %]
+                                [% maxissueqty | html %]
                             [% ELSE %]
-                                [% branch_cat_rule_loo.maxissueqty | html %]
+                                <span>Unlimited</span>
                             [% END %]
                         </td>
-                        <td>[% IF ( branch_cat_rule_loo.unlimited_maxonsiteissueqty ) %]
-                                <span>Unlimited</span>
+                        <td>
+                            [% IF maxonsiteissueqty  %]
+                                [% maxonsiteissueqty | html %]
                             [% ELSE %]
-                                [% branch_cat_rule_loo.maxonsiteissueqty | html %]
+                                <span>Unlimited</span>
                             [% END %]
                         </td>
                         <td>
                             [% IF rule_value.defined && rule_value != '' %]
                                 [% rule_value | html %]
                             [% ELSE %]
-                                Unlimited
+                                <span>Unlimited</span>
                             [% END %]
                         </td>
 
                         <td class="actions">
-                            <a class="btn btn-default btn-xs delete" href="/cgi-bin/koha/admin/smart-rules.pl?op=delete-branch-cat&amp;categorycode=[% branch_cat_rule_loo.categorycode | html %]&amp;branch=[% current_branch | html %]"><i class="fa fa-trash"></i> Delete</a>
+                            <a class="btn btn-default btn-xs delete" href="/cgi-bin/koha/admin/smart-rules.pl?op=delete-branch-cat&amp;categorycode=[% c | html %]&amp;branch=[% current_branch | html %]"><i class="fa fa-trash"></i> Delete</a>
                         </td>
                     </tr>
+                    [% END %]
                 [% END %]
                 <tr>
                     <td>
index a8758f1..eafc2d8 100755 (executable)
@@ -41,6 +41,7 @@ use Koha::IssuingRules;
 use Koha::Items;
 use Koha::Checkouts;
 use Koha::Patrons;
+use Koha::CirculationRules;
 use Koha::Subscriptions;
 use Koha::Account::Lines;
 use Koha::Account::Offsets;
@@ -189,14 +190,15 @@ is(
 
 # Set a simple circ policy
 $dbh->do('DELETE FROM issuingrules');
+Koha::CirculationRules->search()->delete();
 $dbh->do(
     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed,
-                                maxissueqty, issuelength, lengthunit,
+                                issuelength, lengthunit,
                                 renewalsallowed, renewalperiod,
                                 norenewalbefore, auto_renew,
                                 fine, chargeperiod)
       VALUES (?, ?, ?, ?,
-              ?, ?, ?,
+              ?, ?,
               ?, ?,
               ?, ?,
               ?, ?
@@ -204,7 +206,7 @@ $dbh->do(
     },
     {},
     '*', '*', '*', 25,
-    20, 14, 'days',
+    14, 'days',
     1, 7,
     undef, 0,
     .10, 1
@@ -1008,18 +1010,29 @@ my ( $reused_itemnumber_1, $reused_itemnumber_2 );
     $dbh->do('DELETE FROM issues');
     $dbh->do('DELETE FROM items');
     $dbh->do('DELETE FROM issuingrules');
+    Koha::CirculationRules->search()->delete();
     $dbh->do(
         q{
-        INSERT INTO issuingrules ( categorycode, branchcode, itemtype, reservesallowed, maxissueqty, issuelength, lengthunit, renewalsallowed, renewalperiod,
-                    norenewalbefore, auto_renew, fine, chargeperiod ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
+        INSERT INTO issuingrules ( categorycode, branchcode, itemtype, reservesallowed, issuelength, lengthunit, renewalsallowed, renewalperiod,
+                    norenewalbefore, auto_renew, fine, chargeperiod ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
         },
         {},
         '*', '*', '*', 25,
-        20,  14,  'days',
+        14,  'days',
         1,   7,
         undef,  0,
         .10, 1
     );
+    Koha::CirculationRules->set_rules(
+        {
+            categorycode => '*',
+            itemtype     => '*',
+            branchcode   => '*',
+            rules        => {
+                maxissueqty => 20
+            }
+        }
+    );
     my $biblio = $builder->build_sample_biblio();
 
     my $item_1 = $builder->build_sample_item(
@@ -1605,7 +1618,6 @@ subtest 'AddReturn + CumulativeRestrictionPeriods' => sub {
             categorycode => '*',
             itemtype     => '*',
             branchcode   => '*',
-            maxissueqty  => 99,
             issuelength  => 1,
             firstremind  => 1,        # 1 day of grace
             finedays     => 2,        # 2 days of fine per day of overdue
index 10bc52e..22e491d 100644 (file)
@@ -21,6 +21,7 @@ use C4::Circulation;
 use C4::Items;
 use C4::Biblio;
 use C4::Context;
+use Koha::CirculationRules;
 
 use Koha::Patrons;
 
@@ -53,7 +54,6 @@ $dbh->do(q|DELETE FROM categories|);
 $dbh->do(q|DELETE FROM accountlines|);
 $dbh->do(q|DELETE FROM itemtypes|);
 $dbh->do(q|DELETE FROM branch_item_rules|);
-$dbh->do(q|DELETE FROM branch_borrower_circ_rules|);
 $dbh->do(q|DELETE FROM default_branch_circ_rules|);
 $dbh->do(q|DELETE FROM default_circ_rules|);
 $dbh->do(q|DELETE FROM default_branch_item_rules|);
@@ -152,31 +152,53 @@ is_deeply(
 "Without parameter, GetBranchBorrower returns undef (unilimited) for maxissueqty and maxonsiteissueqty if no rules defined"
 );
 
-my $query = q|
-    INSERT INTO branch_borrower_circ_rules
-    (branchcode, categorycode, maxissueqty, maxonsiteissueqty)
-    VALUES( ?, ?, ?, ? )
-|;
-
-$dbh->do(
-    $query, {},
-    $samplebranch1->{branchcode},
-    $samplecat->{categorycode}, 5, 6
+Koha::CirculationRules->set_rules(
+    {
+        branchcode   => $samplebranch1->{branchcode},
+        categorycode => $samplecat->{categorycode},
+        itemtype     => undef,
+        rules        => {
+            maxissueqty       => 5,
+            maxonsiteissueqty => 6,
+        }
+    }
 );
 
-$query = q|
+my $query = q|
     INSERT INTO default_branch_circ_rules
-    (branchcode, maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
-    VALUES( ?, ?, ?, ?, ? )
+    (branchcode, holdallowed, returnbranch)
+    VALUES( ?, ?, ? )
 |;
-$dbh->do( $query, {}, $samplebranch2->{branchcode},
-    3, 2, 1, 'holdingbranch' );
+$dbh->do( $query, {}, $samplebranch2->{branchcode}, 1, 'holdingbranch' );
+Koha::CirculationRules->set_rules(
+    {
+        branchcode   => $samplebranch2->{branchcode},
+        categorycode => undef,
+        itemtype     => undef,
+        rules        => {
+            maxissueqty       => 3,
+            maxonsiteissueqty => 2,
+        }
+    }
+);
+
 $query = q|
     INSERT INTO default_circ_rules
-    (singleton, maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
-    VALUES( ?, ?, ?, ?, ? )
+    (singleton, holdallowed, returnbranch)
+    VALUES( ?, ?, ? )
 |;
-$dbh->do( $query, {}, 'singleton', 4, 5, 3, 'homebranch' );
+$dbh->do( $query, {}, 'singleton', 3, 'homebranch' );
+Koha::CirculationRules->set_rules(
+    {
+        branchcode   => undef,
+        categorycode => undef,
+        itemtype     => undef,
+        rules        => {
+            maxissueqty       => 4,
+            maxonsiteissueqty => 5,
+        }
+    }
+);
 
 $query =
 "INSERT INTO branch_item_rules (branchcode,itemtype,holdallowed,returnbranch) VALUES( ?,?,?,?)";
index ebf8032..06069b8 100644 (file)
@@ -116,8 +116,6 @@ my $sampleissuingrule1 = {
     reservecharge      => '0.000000',
     restrictedtype     => 0,
     accountsent        => 0,
-    maxissueqty        => 5,
-    maxonsiteissueqty  => 4,
     finedays           => 0,
     lengthunit         => 'days',
     renewalperiod      => 5,
@@ -152,9 +150,7 @@ my $sampleissuingrule2 = {
     branchcode         => $samplebranch2->{branchcode},
     categorycode       => $samplecat->{categorycode},
     itemtype           => 'BOOK',
-    maxissueqty        => 2,
-    maxonsiteissueqty  => 1,
-    renewalsallowed    => 0,
+    renewalsallowed    => 'Null',
     renewalperiod      => 2,
     norenewalbefore    => 7,
     auto_renew         => 0,
@@ -184,9 +180,7 @@ my $sampleissuingrule3 = {
     branchcode         => $samplebranch1->{branchcode},
     categorycode       => $samplecat->{categorycode},
     itemtype           => 'DVD',
-    maxissueqty        => 3,
-    maxonsiteissueqty  => 2,
-    renewalsallowed    => 0,
+    renewalsallowed    => 'Null',
     renewalperiod      => 3,
     norenewalbefore    => 8,
     auto_renew         => 0,
@@ -217,8 +211,6 @@ $query = 'INSERT INTO issuingrules (
                 branchcode,
                 categorycode,
                 itemtype,
-                maxissueqty,
-                maxonsiteissueqty,
                 renewalsallowed,
                 renewalperiod,
                 norenewalbefore,
@@ -243,14 +235,12 @@ $query = 'INSERT INTO issuingrules (
                 opacitemholds,
                 cap_fine_to_replacement_price,
                 article_requests
-                ) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
+                ) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
 my $sth = $dbh->prepare($query);
 $sth->execute(
     $sampleissuingrule1->{branchcode},
     $sampleissuingrule1->{categorycode},
     $sampleissuingrule1->{itemtype},
-    $sampleissuingrule1->{maxissueqty},
-    $sampleissuingrule1->{maxonsiteissueqty},
     $sampleissuingrule1->{renewalsallowed},
     $sampleissuingrule1->{renewalperiod},
     $sampleissuingrule1->{norenewalbefore},
@@ -280,8 +270,6 @@ $sth->execute(
     $sampleissuingrule2->{branchcode},
     $sampleissuingrule2->{categorycode},
     $sampleissuingrule2->{itemtype},
-    $sampleissuingrule2->{maxissueqty},
-    $sampleissuingrule2->{maxonsiteissueqty},
     $sampleissuingrule2->{renewalsallowed},
     $sampleissuingrule2->{renewalperiod},
     $sampleissuingrule2->{norenewalbefore},
@@ -311,8 +299,6 @@ $sth->execute(
     $sampleissuingrule3->{branchcode},
     $sampleissuingrule3->{categorycode},
     $sampleissuingrule3->{itemtype},
-    $sampleissuingrule3->{maxissueqty},
-    $sampleissuingrule3->{maxonsiteissueqty},
     $sampleissuingrule3->{renewalsallowed},
     $sampleissuingrule3->{renewalperiod},
     $sampleissuingrule3->{norenewalbefore},
index 4d1b351..47f11fa 100644 (file)
@@ -54,7 +54,6 @@ my $rule = Koha::IssuingRule->new(
         categorycode => '*',
         itemtype     => '*',
         branchcode   => '*',
-        maxissueqty  => 99,
         issuelength  => 1,
     }
 );
index 335f1c2..7ef05ee 100644 (file)
@@ -27,6 +27,7 @@ use C4::Context;
 use Koha::DateUtils qw( dt_from_string );
 use Koha::Database;
 use Koha::Checkouts;
+use Koha::CirculationRules;
 
 use t::lib::TestBuilder;
 use t::lib::Mocks;
@@ -38,7 +39,6 @@ our $dbh = C4::Context->dbh;
 
 $dbh->do(q|DELETE FROM branch_item_rules|);
 $dbh->do(q|DELETE FROM issues|);
-$dbh->do(q|DELETE FROM branch_borrower_circ_rules|);
 $dbh->do(q|DELETE FROM default_branch_circ_rules|);
 $dbh->do(q|DELETE FROM default_circ_rules|);
 $dbh->do(q|DELETE FROM default_branch_item_rules|);
@@ -91,14 +91,24 @@ my $issuingrule = $builder->build({
         branchcode         => $branch->{branchcode},
         categorycode       => '*',
         itemtype           => '*',
-        maxissueqty        => 2,
-        maxonsiteissueqty  => 1,
         lengthunit         => 'days',
         issuelength        => 5,
         hardduedate        => undef,
         hardduedatecompare => 0,
     },
 });
+Koha::CirculationRules->search()->delete();
+Koha::CirculationRules->set_rules(
+    {
+        branchcode   => $branch->{branchcode},
+        categorycode => '*',
+        itemtype     => '*',
+        rules        => {
+            maxissueqty       => 2,
+            maxonsiteissueqty => 1,
+        }
+    }
+);
 
 t::lib::Mocks::mock_userenv({ patron => $patron });
 
@@ -155,16 +165,18 @@ my $yet_another_item = $builder->build({
 ( $impossible, undef, undef, undef ) = C4::Circulation::CanBookBeIssued( $patron, $yet_another_item->{barcode} );
 is( $impossible->{TOO_MANY}, 'TOO_MANY_CHECKOUTS', 'Not a specific case, $delta should not be incremented' );
 
-$dbh->do(q|DELETE FROM issuingrules|);
-my $borrower_circ_rule = $builder->build({
-    source => 'DefaultCircRule',
-    value => {
-        branchcode         => $branch->{branchcode},
-        categorycode       => '*',
-        maxissueqty        => 2,
-        maxonsiteissueqty  => 1,
-    },
-});
+Koha::CirculationRules->search()->delete();
+Koha::CirculationRules->set_rules(
+    {
+        branchcode   => $branch->{branchcode},
+        categorycode => '*',
+        itemtype     => '*',
+        rules        => {
+            maxissueqty       => 2,
+            maxonsiteissueqty => 1,
+        }
+    }
+);
 ( $impossible, undef, undef, $messages ) = C4::Circulation::CanBookBeIssued( $patron, $another_item->{barcode} );
 is( $messages->{ONSITE_CHECKOUT_WILL_BE_SWITCHED}, 1, 'Specific case 2 - Switch is allowed' );
 is( exists $impossible->{TOO_MANY}, '', 'Specific case 2 - Switch is allowed' );
index c88b200..581a010 100644 (file)
@@ -26,6 +26,7 @@ use C4::Context;
 
 use Koha::DateUtils qw( dt_from_string );
 use Koha::Database;
+use Koha::CirculationRules;
 
 use t::lib::TestBuilder;
 use t::lib::Mocks;
@@ -43,11 +44,11 @@ $dbh->do(q|DELETE FROM categories|);
 $dbh->do(q|DELETE FROM accountlines|);
 $dbh->do(q|DELETE FROM itemtypes|);
 $dbh->do(q|DELETE FROM branch_item_rules|);
-$dbh->do(q|DELETE FROM branch_borrower_circ_rules|);
 $dbh->do(q|DELETE FROM default_branch_circ_rules|);
 $dbh->do(q|DELETE FROM default_circ_rules|);
 $dbh->do(q|DELETE FROM default_branch_item_rules|);
 $dbh->do(q|DELETE FROM issuingrules|);
+Koha::CirculationRules->search()->delete();
 
 my $builder = t::lib::TestBuilder->new();
 t::lib::Mocks::mock_preference('item-level_itypes', 1); # Assuming the item type is defined at item level
@@ -106,16 +107,17 @@ subtest 'no rules exist' => sub {
 
 subtest '1 Issuingrule exist 0 0: no issue allowed' => sub {
     plan tests => 4;
-    my $issuingrule = $builder->build({
-        source => 'Issuingrule',
-        value => {
-            branchcode         => $branch->{branchcode},
-            categorycode       => $category->{categorycode},
-            itemtype           => '*',
-            maxissueqty        => 0,
-            maxonsiteissueqty  => 0,
+    Koha::CirculationRules->set_rules(
+        {
+            branchcode   => $branch->{branchcode},
+            categorycode => $category->{categorycode},
+            itemtype     => '*',
+            rules        => {
+                maxissueqty       => 0,
+                maxonsiteissueqty => 0,
+            }
         },
-    });
+    );
     t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 0);
     is_deeply(
         C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
@@ -214,16 +216,17 @@ subtest '1 Issuingrule exist with onsiteissueqty=unlimited' => sub {
 
 subtest '1 Issuingrule exist 1 1: issue is allowed' => sub {
     plan tests => 4;
-    my $issuingrule = $builder->build({
-        source => 'Issuingrule',
-        value => {
-            branchcode         => $branch->{branchcode},
-            categorycode       => $category->{categorycode},
-            itemtype           => '*',
-            maxissueqty        => 1,
-            maxonsiteissueqty  => 1,
-        },
-    });
+    Koha::CirculationRules->set_rules(
+        {
+            branchcode   => $branch->{branchcode},
+            categorycode => $category->{categorycode},
+            itemtype     => '*',
+            rules        => {
+                maxissueqty       => 1,
+                maxonsiteissueqty => 1,
+            }
+        }
+    );
     t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 0);
     is(
         C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
@@ -253,16 +256,17 @@ subtest '1 Issuingrule exist 1 1: issue is allowed' => sub {
 
 subtest '1 Issuingrule exist: 1 CO allowed, 1 OSCO allowed. Do a CO' => sub {
     plan tests => 5;
-    my $issuingrule = $builder->build({
-        source => 'Issuingrule',
-        value => {
-            branchcode         => $branch->{branchcode},
-            categorycode       => $category->{categorycode},
-            itemtype           => '*',
-            maxissueqty        => 1,
-            maxonsiteissueqty  => 1,
-        },
-    });
+    Koha::CirculationRules->set_rules(
+        {
+            branchcode   => $branch->{branchcode},
+            categorycode => $category->{categorycode},
+            itemtype     => '*',
+            rules        => {
+                maxissueqty       => 1,
+                maxonsiteissueqty => 1,
+            }
+        }
+    );
 
     my $issue = C4::Circulation::AddIssue( $patron, $item->{barcode}, dt_from_string() );
     like( $issue->issue_id, qr|^\d+$|, 'The issue should have been inserted' );
@@ -308,16 +312,17 @@ subtest '1 Issuingrule exist: 1 CO allowed, 1 OSCO allowed. Do a CO' => sub {
 
 subtest '1 Issuingrule exist: 1 CO allowed, 1 OSCO allowed, Do a OSCO' => sub {
     plan tests => 5;
-    my $issuingrule = $builder->build({
-        source => 'Issuingrule',
-        value => {
-            branchcode         => $branch->{branchcode},
-            categorycode       => $category->{categorycode},
-            itemtype           => '*',
-            maxissueqty        => 1,
-            maxonsiteissueqty  => 1,
-        },
-    });
+    Koha::CirculationRules->set_rules(
+        {
+            branchcode   => $branch->{branchcode},
+            categorycode => $category->{categorycode},
+            itemtype     => '*',
+            rules        => {
+                maxissueqty       => 1,
+                maxonsiteissueqty => 1,
+            }
+        }
+    );
 
     my $issue = C4::Circulation::AddIssue( $patron, $item->{barcode}, dt_from_string(), undef, undef, undef, { onsite_checkout => 1 } );
     like( $issue->issue_id, qr|^\d+$|, 'The issue should have been inserted' );
@@ -366,15 +371,17 @@ subtest '1 BranchBorrowerCircRule exist: 1 CO allowed, 1 OSCO allowed' => sub {
     # DefaultBorrowerCircRule, DefaultBranchCircRule, DefaultBranchItemRule ans DefaultCircRule.pm
 
     plan tests => 10;
-    my $issuingrule = $builder->build({
-        source => 'BranchBorrowerCircRule',
-        value => {
-            branchcode         => $branch->{branchcode},
-            categorycode       => $category->{categorycode},
-            maxissueqty        => 1,
-            maxonsiteissueqty  => 1,
-        },
-    });
+    Koha::CirculationRules->set_rules(
+        {
+            branchcode   => $branch->{branchcode},
+            categorycode => $category->{categorycode},
+            itemtype     => undef,
+            rules        => {
+                maxissueqty       => 1,
+                maxonsiteissueqty => 1,
+            }
+        }
+    );
 
     my $issue = C4::Circulation::AddIssue( $patron, $item->{barcode}, dt_from_string(), undef, undef, undef );
     like( $issue->issue_id, qr|^\d+$|, 'The issue should have been inserted' );
index 34413a4..647460d 100755 (executable)
@@ -97,7 +97,6 @@ my $rule = Koha::IssuingRule->new(
         categorycode => '*',
         itemtype     => '*',
         branchcode   => '*',
-        maxissueqty  => 99,
         issuelength  => 7,
         lengthunit   => 8,
         reservesallowed => 99,
index 956c569..9b52625 100755 (executable)
@@ -189,8 +189,6 @@ $requesters{$branch_3} = Koha::Patron->new({
 
 $dbh->do('DELETE FROM issuingrules');
 $dbh->do('DELETE FROM branch_item_rules');
-$dbh->do('DELETE FROM branch_borrower_circ_rules');
-$dbh->do('DELETE FROM default_borrower_circ_rules');
 $dbh->do('DELETE FROM default_branch_item_rules');
 $dbh->do('DELETE FROM default_branch_circ_rules');
 $dbh->do('DELETE FROM default_circ_rules');
@@ -203,18 +201,18 @@ $dbh->do(
 
 # CPL allows only its own patrons to request its items
 $dbh->do(
-    q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
-      VALUES (?, ?, ?, ?)},
+    q{INSERT INTO default_branch_circ_rules (branchcode, holdallowed, returnbranch)
+      VALUES (?, ?, ?)},
     {},
-    $branch_1, 10, 1, 'homebranch',
+    $branch_1, 1, 'homebranch',
 );
 
 # ... while FPL allows anybody to request its items
 $dbh->do(
-    q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
-      VALUES (?, ?, ?, ?)},
+    q{INSERT INTO default_branch_circ_rules (branchcode, holdallowed, returnbranch)
+      VALUES (?, ?, ?)},
     {},
-    $branch_2, 10, 2, 'homebranch',
+    $branch_2, 2, 'homebranch',
 );
 
 my $bibnum2 = $builder->build_sample_biblio({frameworkcode => $frameworkcode})->biblionumber;