From b87dae9ecd3f8576040b908ed4bb5b1f31236d47 Mon Sep 17 00:00:00 2001 From: Alex Arnaud Date: Wed, 28 Jun 2017 13:50:20 +0000 Subject: [PATCH] Bug 8137 - Add Default checkout limit by patron category with priority. This patch add the ability to make a 'Default checkout limit by patron category' rule has priority. When a rule has priority, it overrides all others for the same categorycode. For "normal" rule, the behavior is the same than before (like a default rule if no one is specified at branch level). Rule with priority can only be created on the circulation and fine rules page for *all libraries*. The goal is to allow librarian to limit the total number of checkouts by patron category for all libraries. test plan: - Create a rule with 4 checkouts allowed in default checkout limit table for a patron category and make it has priority, - create more permissive rules for all other branches (at least 5 checkouts allowed), - Checkout items of different branches for a patron with the same category, - Patron should be limited to 4 checkouts https://bugs.koha-community.org/show_bug.cgi?id=8137 --- C4/Circulation.pm | 28 +++++++++++++++------- admin/smart-rules.pl | 16 ++++++++----- .../Bug_8137-add-column-has_priority.sql | 5 ++++ .../prog/en/modules/admin/smart-rules.tt | 15 ++++++++++++ t/db_dependent/Circulation/Branch.t | 26 +++++++++++++++++++- 5 files changed, 74 insertions(+), 16 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/Bug_8137-add-column-has_priority.sql diff --git a/C4/Circulation.pm b/C4/Circulation.pm index 1cfa69b..a3625ab 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -427,7 +427,7 @@ sub TooMany { AND (categorycode = ? OR categorycode = ?) AND itemtype <> '*' ) "; - } else { + } else { $count_query .= " JOIN biblioitems USING (biblionumber) WHERE biblioitems.itemtype NOT IN ( SELECT itemtype FROM issuingrules @@ -512,14 +512,17 @@ sub TooMany { |; push @bind_params, $borrower->{borrowernumber}; - if (C4::Context->preference('CircControl') eq 'PickupLibrary') { - $branch_count_query .= " AND issues.branchcode = ? "; - push @bind_params, $branch; - } elsif (C4::Context->preference('CircControl') eq 'PatronLibrary') { - ; # if branch is the patron's home branch, then count all loans by patron - } else { - $branch_count_query .= " AND items.homebranch = ? "; - push @bind_params, $branch; + # Only for specific or default branches. + unless ($branch_borrower_circ_rule->{has_priority}) { + if (C4::Context->preference('CircControl') eq 'PickupLibrary') { + $branch_count_query .= " AND issues.branchcode = ? "; + push @bind_params, $branch; + } elsif (C4::Context->preference('CircControl') eq 'PatronLibrary') { + ; # if branch is the patron's home branch, then count all loans by patron + } else { + $branch_count_query .= " AND items.homebranch = ? "; + push @bind_params, $branch; + } } my ( $checkout_count, $onsite_checkout_count ) = $dbh->selectrow_array( $branch_count_query, {}, @bind_params ); my $max_checkouts_allowed = $branch_borrower_circ_rule->{maxissueqty}; @@ -1616,6 +1619,13 @@ sub GetBranchBorrowerCircRule { my $rules; my $dbh = C4::Context->dbh(); $rules = $dbh->selectrow_hashref( q| + SELECT maxissueqty, maxonsiteissueqty, has_priority + FROM default_borrower_circ_rules + WHERE categorycode = ? AND has_priority = 1 + |, {}, $categorycode ) ; + return $rules if $rules; + + $rules = $dbh->selectrow_hashref( q| SELECT maxissueqty, maxonsiteissueqty FROM branch_borrower_circ_rules WHERE branchcode = ? diff --git a/admin/smart-rules.pl b/admin/smart-rules.pl index 6a55b7b..d779e5c 100755 --- a/admin/smart-rules.pl +++ b/admin/smart-rules.pl @@ -255,10 +255,12 @@ elsif ($op eq "add-branch-cat") { my $categorycode = $input->param('categorycode'); my $maxissueqty = $input->param('maxissueqty'); my $maxonsiteissueqty = $input->param('maxonsiteissueqty'); + my $has_priority = $input->param('has_priority'); $maxissueqty =~ s/\s//g; $maxissueqty = undef if $maxissueqty !~ /^\d+/; $maxonsiteissueqty =~ s/\s//g; $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/; + $has_priority = $has_priority ? 1 : 0; if ($branch eq "*") { if ($categorycode eq "*") { @@ -285,24 +287,26 @@ elsif ($op eq "add-branch-cat") { } else { my $sth_search = $dbh->prepare("SELECT count(*) AS total FROM default_borrower_circ_rules - WHERE categorycode = ?"); + WHERE categorycode = ? + AND has_priority = ?"); my $sth_insert = $dbh->prepare(q| INSERT INTO default_borrower_circ_rules - (categorycode, maxissueqty, maxonsiteissueqty) - VALUES (?, ?, ?) + (categorycode, maxissueqty, maxonsiteissueqty, has_priority) + VALUES (?, ?, ?, ?) |); my $sth_update = $dbh->prepare(q| UPDATE default_borrower_circ_rules SET maxissueqty = ?, maxonsiteissueqty = ? WHERE categorycode = ? + AND has_priority = ? |); - $sth_search->execute($branch); + $sth_search->execute($categorycode, $has_priority); my $res = $sth_search->fetchrow_hashref(); if ($res->{total}) { - $sth_update->execute($maxissueqty, $maxonsiteissueqty, $categorycode); + $sth_update->execute($maxissueqty, $maxonsiteissueqty, $categorycode, $has_priority) or die $sth_update->errstr;; } else { - $sth_insert->execute($categorycode, $maxissueqty, $maxonsiteissueqty); + $sth_insert->execute($categorycode, $maxissueqty, $maxonsiteissueqty, $has_priority); } } } elsif ($categorycode eq "*") { diff --git a/installer/data/mysql/atomicupdate/Bug_8137-add-column-has_priority.sql b/installer/data/mysql/atomicupdate/Bug_8137-add-column-has_priority.sql new file mode 100644 index 0000000..fc7c42f --- /dev/null +++ b/installer/data/mysql/atomicupdate/Bug_8137-add-column-has_priority.sql @@ -0,0 +1,5 @@ +ALTER TABLE default_borrower_circ_rules ADD has_priority INT(1) DEFAULT 0; +ALTER TABLE default_borrower_circ_rules DROP foreign key borrower_borrower_circ_rules_ibfk_1; +ALTER TABLE default_borrower_circ_rules DROP primary key; +ALTER TABLE default_borrower_circ_rules ADD primary key(categorycode, has_priority); +ALTER TABLE default_borrower_circ_rules ADD CONSTRAINT borrower_borrower_circ_rules_ibfk_1 FOREIGN KEY (categorycode) REFERENCES categories (categorycode) ON DELETE CASCADE ON UPDATE CASCADE; \ No newline at end of file diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tt index 0283d80..d2ec909 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tt @@ -547,6 +547,10 @@ $(document).ready(function() {

If the total amount loanable for a given patron category is left blank, no limit applies, except possibly for a limit you define for a specific item type.

+

+ Has priority: If checked, the rule will override those for all branches. Else + it behaves like a default one: used if no rule existe for the coresponding branch. +

@@ -555,6 +559,7 @@ $(document).ready(function() { Patron category Total current checkouts allowed Total current on-site checkouts allowed + [% UNLESS humanbranch %]Has priority[% END %]   [% FOREACH branch_cat_rule_loo IN branch_cat_rule_loop %] @@ -581,6 +586,11 @@ $(document).ready(function() { [% branch_cat_rule_loo.maxonsiteissueqty %] [% END %] + [% UNLESS humanbranch %] + + + + [% END %] Delete @@ -597,6 +607,11 @@ $(document).ready(function() { + [% UNLESS humanbranch %] + + + + [% END %]