@@ -, +, @@ --- C4/Circulation.pm | 34 +++++++---- admin/smart-rules.pl | 63 ++++++++++--------- ...dd-returnbranch-to-branch-item-issuing-rules.pl | 20 ++++++ installer/data/mysql/kohastructure.sql | 4 + .../prog/en/modules/admin/smart-rules.tt | 44 ++++++++++++++ 5 files changed, 122 insertions(+), 43 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/bug_7144-add-returnbranch-to-branch-item-issuing-rules.pl --- a/C4/Circulation.pm +++ a/C4/Circulation.pm @@ -1351,13 +1351,17 @@ sub GetBranchBorrowerCircRule { Retrieves circulation rule attributes that apply to the given branch and item type, regardless of patron category. -The return value is a hashref containing the following key: +The return value is a hashref containing the following keys: holdallowed => Hold policy for this branch and itemtype. Possible values: 0: No holds allowed. 1: Holds allowed only by patrons that have the same homebranch as the item. 2: Holds allowed from any patron. +returnbranch => branch to which to return item. Possible values: + noreturn: do not return, let item remain where checked in (floating collections) + homebranch: return to item's home branch + This searches branchitemrules in the following order: * Same branchcode and itemtype @@ -1365,7 +1369,7 @@ This searches branchitemrules in the following order: * branchcode '*', same itemtype * branchcode and itemtype '*' -Neither C<$branchcode> nor C<$categorycode> should be '*'. +Neither C<$branchcode> nor C<$itemtype> should be '*'. =cut @@ -1375,33 +1379,36 @@ sub GetBranchItemRule { my $result = {}; my @attempts = ( - ['SELECT holdallowed + ['SELECT holdallowed, returnbranch FROM branch_item_rules WHERE branchcode = ? AND itemtype = ?', $branchcode, $itemtype], - ['SELECT holdallowed + ['SELECT holdallowed, returnbranch FROM default_branch_circ_rules WHERE branchcode = ?', $branchcode], - ['SELECT holdallowed + ['SELECT holdallowed, returnbranch FROM default_branch_item_rules WHERE itemtype = ?', $itemtype], - ['SELECT holdallowed + ['SELECT holdallowed, returnbranch FROM default_circ_rules'], ); foreach my $attempt (@attempts) { my ($query, @bind_params) = @{$attempt}; + my $search_result = $dbh->selectrow_hashref ( $query , {}, @bind_params ); # Since branch/category and branch/itemtype use the same per-branch # defaults tables, we have to check that the key we want is set, not # just that a row was returned - return $result if ( defined( $result->{'holdallowed'} = $dbh->selectrow_array( $query, {}, @bind_params ) ) ); + $result->{'holdallowed'} = $search_result->{'holdallowed'} unless ( defined $result->{'holdallowed'} ); + $result->{'returnbranch'} = $search_result->{'returnbranch'} unless ( defined $result->{'returnbranch'} ); } # built-in default circulation rule - return { - holdallowed => 2, - }; + $result->{'holdallowed'} = 2 unless ( defined $result->{'holdallowed'} ); + $result->{'returnbranch'} = 'homebranch' unless ( defined $result->{'returnbranch'} ); + + return $result; } =head2 AddReturn @@ -1518,9 +1525,10 @@ sub AddReturn { my $item = GetItem($itemnumber) or die "GetItem($itemnumber) failed"; # full item data, but no borrowernumber or checkout info (no issue) # we know GetItem should work because GetItemnumberFromBarcode worked - my $hbr = C4::Context->preference("HomeOrHoldingBranchReturn") || "homebranch"; - $hbr = $item->{$hbr} || ''; - # item must be from items table -- issues table has branchcode and issuingbranch, not homebranch nor holdingbranch + my $hbr = GetBranchItemRule($item->{'homebranch'}, $item->{'itype'})->{'returnbranch'} || "homebranch"; + # get the proper branch to which to return the item + $hbr = $item->{$hbr} || $branch ; + # if $hbr was "noreturn" or any other non-item table value, then it should 'float' (i.e. stay at this branch) my $borrowernumber = $borrower->{'borrowernumber'} || undef; # we don't know if we had a borrower or not --- a/admin/smart-rules.pl +++ a/admin/smart-rules.pl @@ -135,6 +135,7 @@ elsif ($op eq "set-branch-defaults") { my $categorycode = $input->param('categorycode'); my $maxissueqty = $input->param('maxissueqty'); my $holdallowed = $input->param('holdallowed'); + my $returnbranch = $input->param('returnbranch'); $maxissueqty =~ s/\s//g; $maxissueqty = undef if $maxissueqty !~ /^\d+/; $holdallowed =~ s/\s//g; @@ -144,34 +145,34 @@ 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, holdallowed) - VALUES (?, ?)"); + (maxissueqty, holdallowed, returnbranch) + VALUES (?, ?, ?)"); my $sth_update = $dbh->prepare("UPDATE default_circ_rules - SET maxissueqty = ?, holdallowed = ?"); + SET maxissueqty = ?, holdallowed = ?, returnbranch = ?"); $sth_search->execute(); my $res = $sth_search->fetchrow_hashref(); if ($res->{total}) { - $sth_update->execute($maxissueqty, $holdallowed); + $sth_update->execute($maxissueqty, $holdallowed, $returnbranch); } else { - $sth_insert->execute($maxissueqty, $holdallowed); + $sth_insert->execute($maxissueqty, $holdallowed, $returnbranch); } } 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, holdallowed) - VALUES (?, ?, ?)"); + (branchcode, maxissueqty, holdallowed, returnbranch) + VALUES (?, ?, ?, ?)"); my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules - SET maxissueqty = ?, holdallowed = ? + SET maxissueqty = ?, holdallowed = ?, returnbranch = ? WHERE branchcode = ?"); $sth_search->execute($branch); my $res = $sth_search->fetchrow_hashref(); if ($res->{total}) { - $sth_update->execute($maxissueqty, $holdallowed, $branch); + $sth_update->execute($maxissueqty, $holdallowed, $returnbranch, $branch); } else { - $sth_insert->execute($branch, $maxissueqty, $holdallowed); + $sth_insert->execute($branch, $maxissueqty, $holdallowed, $returnbranch); } } } @@ -258,6 +259,7 @@ elsif ($op eq "add-branch-cat") { elsif ($op eq "add-branch-item") { my $itemtype = $input->param('itemtype'); my $holdallowed = $input->param('holdallowed'); + my $returnbranch = $input->param('returnbranch'); $holdallowed =~ s/\s//g; $holdallowed = undef if $holdallowed !~ /^\d+/; @@ -266,34 +268,34 @@ elsif ($op eq "add-branch-item") { my $sth_search = $dbh->prepare("SELECT count(*) AS total FROM default_circ_rules"); my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules - (holdallowed) - VALUES (?)"); + (holdallowed, returnbranch) + VALUES (?, ?)"); my $sth_update = $dbh->prepare("UPDATE default_circ_rules - SET holdallowed = ?"); + SET holdallowed = ?, returnbranch = ?"); $sth_search->execute(); my $res = $sth_search->fetchrow_hashref(); if ($res->{total}) { - $sth_update->execute($holdallowed); + $sth_update->execute($holdallowed, $returnbranch); } else { - $sth_insert->execute($holdallowed); + $sth_insert->execute($holdallowed, $returnbranch); } } else { my $sth_search = $dbh->prepare("SELECT count(*) AS total FROM default_branch_item_rules WHERE itemtype = ?"); my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules - (itemtype, holdallowed) - VALUES (?, ?)"); + (itemtype, holdallowed, returnbranch) + VALUES (?, ?, ?)"); my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules - SET holdallowed = ? + SET holdallowed = ?, returnbranch = ? WHERE itemtype = ?"); $sth_search->execute($itemtype); my $res = $sth_search->fetchrow_hashref(); if ($res->{total}) { - $sth_update->execute($holdallowed, $itemtype); + $sth_update->execute($holdallowed, $returnbranch, $itemtype); } else { - $sth_insert->execute($itemtype, $holdallowed); + $sth_insert->execute($itemtype, $holdallowed, $returnbranch); } } } elsif ($itemtype eq "*") { @@ -301,17 +303,17 @@ elsif ($op eq "add-branch-item") { FROM default_branch_circ_rules WHERE branchcode = ?"); my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules - (branchcode, holdallowed) - VALUES (?, ?)"); + (branchcode, holdallowed, returnbranch) + VALUES (?, ?, ?)"); my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules - SET holdallowed = ? + SET holdallowed = ?, returnbranch = ? WHERE branchcode = ?"); $sth_search->execute($branch); my $res = $sth_search->fetchrow_hashref(); if ($res->{total}) { - $sth_update->execute($holdallowed, $branch); + $sth_update->execute($holdallowed, $returnbranch, $branch); } else { - $sth_insert->execute($branch, $holdallowed); + $sth_insert->execute($branch, $holdallowed, $returnbranch); } } else { my $sth_search = $dbh->prepare("SELECT count(*) AS total @@ -319,19 +321,19 @@ elsif ($op eq "add-branch-item") { WHERE branchcode = ? AND itemtype = ?"); my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules - (branchcode, itemtype, holdallowed) - VALUES (?, ?, ?)"); + (branchcode, itemtype, holdallowed, returnbranch) + VALUES (?, ?, ?, ?)"); my $sth_update = $dbh->prepare("UPDATE branch_item_rules - SET holdallowed = ? + SET holdallowed = ?, returnbranch = ? WHERE branchcode = ? AND itemtype = ?"); $sth_search->execute($branch, $itemtype); my $res = $sth_search->fetchrow_hashref(); if ($res->{total}) { - $sth_update->execute($holdallowed, $branch, $itemtype); + $sth_update->execute($holdallowed, $returnbranch, $branch, $itemtype); } else { - $sth_insert->execute($branch, $itemtype, $holdallowed); + $sth_insert->execute($branch, $itemtype, $holdallowed, $returnbranch); } } } @@ -484,6 +486,7 @@ if ($defaults) { $template->param(default_holdallowed_same => 1) if($defaults->{holdallowed} == 1); $template->param(default_holdallowed_any => 1) if($defaults->{holdallowed} == 2); $template->param(default_maxissueqty => $defaults->{maxissueqty}); + $template->param(default_returnbranch => $defaults->{returnbranch}); } $template->param(default_rules => ($defaults ? 1 : 0)); --- a/installer/data/mysql/atomicupdate/bug_7144-add-returnbranch-to-branch-item-issuing-rules.pl +++ a/installer/data/mysql/atomicupdate/bug_7144-add-returnbranch-to-branch-item-issuing-rules.pl @@ -0,0 +1,20 @@ +#! /usr/bin/perl +use strict; +use warnings; +use C4::Context; +my $dbh=C4::Context->dbh; + +$dbh->do("ALTER TABLE default_circ_rules ADD + COLUMN `returnbranch` varchar(15) default NULL AFTER `holdallowed`"); +$dbh->do("ALTER TABLE branch_item_rules ADD + COLUMN `returnbranch` varchar(15) default NULL AFTER `holdallowed`"); +$dbh->do("ALTER TABLE default_branch_circ_rules ADD + COLUMN `returnbranch` varchar(15) default NULL AFTER `holdallowed`"); +$dbh->do("ALTER TABLE default_branch_item_rules ADD + COLUMN `returnbranch` varchar(15) default NULL AFTER `holdallowed`"); + +# set the default rule to the current value of HomeOrHoldingBranchReturn (default to 'homebranch' if need be) +my $homeorholdingbranchreturn = C4::Context->prefernce('HomeOrHoldingBranchReturn') || 'homebranch'; +$dbh->do("UPDATE default_circ_rules SET returnbranch = '$homeorholdingbranchreturn'"); + +print "Upgrade done (Adding 'returnbranch' to branch/item issuing rules tables)\n"; --- a/installer/data/mysql/kohastructure.sql +++ a/installer/data/mysql/kohastructure.sql @@ -317,6 +317,7 @@ CREATE TABLE `branch_item_rules` ( `branchcode` varchar(10) NOT NULL, `itemtype` varchar(10) NOT NULL, `holdallowed` tinyint(1) default NULL, + `returnbranch` varchar(15) default NULL, PRIMARY KEY (`itemtype`,`branchcode`), KEY `branch_item_rules_ibfk_2` (`branchcode`), CONSTRAINT `branch_item_rules_ibfk_1` FOREIGN KEY (`itemtype`) REFERENCES `itemtypes` (`itemtype`) @@ -497,6 +498,7 @@ CREATE TABLE `default_branch_circ_rules` ( `branchcode` VARCHAR(10) NOT NULL, `maxissueqty` int(4) default NULL, `holdallowed` tinyint(1) default NULL, + `returnbranch` varchar(15) default NULL, PRIMARY KEY (`branchcode`), CONSTRAINT `default_branch_circ_rules_ibfk_1` FOREIGN KEY (`branchcode`) REFERENCES `branches` (`branchcode`) ON DELETE CASCADE ON UPDATE CASCADE @@ -509,6 +511,7 @@ DROP TABLE IF EXISTS `default_branch_item_rules`; CREATE TABLE `default_branch_item_rules` ( `itemtype` varchar(10) NOT NULL, `holdallowed` tinyint(1) default NULL, + `returnbranch` varchar(15) default NULL, PRIMARY KEY (`itemtype`), CONSTRAINT `default_branch_item_rules_ibfk_1` FOREIGN KEY (`itemtype`) REFERENCES `itemtypes` (`itemtype`) ON DELETE CASCADE ON UPDATE CASCADE @@ -523,6 +526,7 @@ CREATE TABLE `default_circ_rules` ( `singleton` enum('singleton') NOT NULL default 'singleton', `maxissueqty` int(4) default NULL, `holdallowed` int(1) default NULL, + `returnbranch` varchar(15) default NULL, PRIMARY KEY (`singleton`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tt +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tt @@ -192,6 +192,7 @@ for="tobranch">Clone these rules to: Clone these rules to: + [% IF ( default_returnbranch == 'homebranch' ) %] + + [% IF ( default_returnbranch == 'holdingbranch' ) %] + + [% IF ( default_returnbranch == 'noreturn' ) %] + + + Unset @@ -316,6 +342,7 @@ for="tobranch">Clone these rules to: Clone these rules to: Delete @@ -358,6 +395,13 @@ for="tobranch">Clone these rules to: No Holds Allowed + + + --