From 879256ddb57932ab0aff58595d91c45ca6eb6cc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joonas=20Kylm=C3=A4l=C3=A4?= Date: Tue, 28 Jan 2020 13:46:55 +0000 Subject: [PATCH] Bug 18936: (follow-up) Add cloning of circulation rules back to Koha MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The "Clone these rules" feature in admin/smart-rules.pl was dropped. This re-implements the cloning using Koha objects. Signed-off-by: Minna Kivinen Signed-off-by: Joonas Kylmälä --- Koha/CirculationRule.pm | 15 +++++ Koha/CirculationRules.pm | 14 +++++ admin/clone-rules.pl | 51 ++++------------- admin/smart-rules.pl | 7 ++- t/db_dependent/Koha/IssuingRules.t | 111 ++++++++++++++++++++++++++++++++++++- 5 files changed, 155 insertions(+), 43 deletions(-) diff --git a/Koha/CirculationRule.pm b/Koha/CirculationRule.pm index 5206c7d9aa..d5c9b864c9 100644 --- a/Koha/CirculationRule.pm +++ b/Koha/CirculationRule.pm @@ -71,6 +71,21 @@ sub item_type { return $self->{item_type}; } +=head3 clone + +Clone a circulation rule to another branch + +=cut + +sub clone { + my ($self, $to_branch) = @_; + + my $cloned_rule = $self->unblessed; + $cloned_rule->{branchcode} = $to_branch; + delete $cloned_rule->{id}; + return Koha::CirculationRule->new( $cloned_rule )->store; +} + =head3 _type =cut diff --git a/Koha/CirculationRules.pm b/Koha/CirculationRules.pm index 2aaf056aaa..1e50251fa7 100644 --- a/Koha/CirculationRules.pm +++ b/Koha/CirculationRules.pm @@ -358,6 +358,20 @@ sub delete { } } +=head3 clone + +Clone a set of circulation rules to another branch + +=cut + +sub clone { + my ( $self, $to_branch ) = @_; + + while ( my $rule = $self->next ){ + $rule->clone($to_branch); + } +} + =head3 get_opacitemholds_policy my $can_place_a_hold_at_item_level = Koha::CirculationRules->get_opacitemholds_policy( { patron => $patron, item => $item } ); diff --git a/admin/clone-rules.pl b/admin/clone-rules.pl index dd071252d9..cefc11ede8 100755 --- a/admin/clone-rules.pl +++ b/admin/clone-rules.pl @@ -32,9 +32,9 @@ use C4::Output; use C4::Auth; use C4::Koha; use C4::Debug; +use Koha::CirculationRules; my $input = new CGI; -my $dbh = C4::Context->dbh; my ($template, $loggedinuser, $cookie) = get_template_and_user({template_name => "admin/clone-rules.tt", @@ -51,50 +51,19 @@ my $tobranch = $input->param("tobranch"); $template->param(frombranch => $frombranch) if ($frombranch); $template->param(tobranch => $tobranch) if ($tobranch); -if ($frombranch && $tobranch) { +if ($frombranch && $tobranch && $frombranch ne $tobranch) { + $frombranch = ( $frombranch ne '*' ? $frombranch : undef ); + $tobranch = ( $tobranch ne '*' ? $tobranch : undef ); - my $error; + Koha::CirculationRules->search({branchcode => $tobranch})->delete; - # First, we create a temporary table with the rules we want to clone - my $query = "CREATE TEMPORARY TABLE tmpissuingrules ENGINE=memory SELECT * FROM issuingrules WHERE branchcode=?"; - my $sth = $dbh->prepare($query); - my $res = $sth->execute($frombranch); - $error = 1 unless ($res); - - if (!$error) { - # We modify these rules according to the new branchcode - $query = "UPDATE tmpissuingrules SET branchcode=? WHERE branchcode=?"; - $sth = $dbh->prepare($query); - $res = $sth->execute($tobranch, $frombranch); - $error = 1 unless ($res); - } - - if (!$error) { - # We delete the rules for the existing branchode - $query = "DELETE FROM issuingrules WHERE branchcode=?"; - $sth = $dbh->prepare($query); - $res = $sth->execute($tobranch); - $error = 1 unless ($res); - } - - - if (!$error) { - # We insert the new rules from our temporary table - $query = "INSERT INTO issuingrules SELECT * FROM tmpissuingrules WHERE branchcode=?"; - $sth = $dbh->prepare($query); - $res = $sth->execute($tobranch); - $error = 1 unless ($res); - } - - # Finally, we delete our temporary table - $query = "DROP TABLE tmpissuingrules"; - $sth = $dbh->prepare($query); - $res = $sth->execute(); - - $template->param(result => "1"); - $template->param(error => $error); + my $rules = Koha::CirculationRules->search({ branchcode => $frombranch }); + $rules->clone($tobranch); +} else { + $template->param(error => 1); } +$template->param(result => 1); output_html_with_http_headers $input, $cookie, $template->output; diff --git a/admin/smart-rules.pl b/admin/smart-rules.pl index 4d4ec5a6b3..ceb4a9be5b 100755 --- a/admin/smart-rules.pl +++ b/admin/smart-rules.pl @@ -574,13 +574,18 @@ my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['des my $itemtypes = Koha::ItemTypes->search_with_localization; +my $humanbranch = ( $branch ne '*' ? $branch : undef ); + +my $definedbranch = Koha::CirculationRules->search({ branchcode => $humanbranch })->count ? 1 : 0; + $template->param(show_branch_cat_rule_form => 1); $template->param( patron_categories => $patron_categories, itemtypeloop => $itemtypes, - humanbranch => ( $branch ne '*' ? $branch : undef ), + humanbranch => $humanbranch, current_branch => $branch, + definedbranch => $definedbranch, ); output_html_with_http_headers $input, $cookie, $template->output; diff --git a/t/db_dependent/Koha/IssuingRules.t b/t/db_dependent/Koha/IssuingRules.t index 2cdb143a93..4d37fcfebc 100644 --- a/t/db_dependent/Koha/IssuingRules.t +++ b/t/db_dependent/Koha/IssuingRules.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 2; +use Test::More tests => 3; use Test::Deep qw( cmp_methods ); use Test::Exception; @@ -566,6 +566,115 @@ subtest 'set_rule' => sub { }; }; +subtest 'clone' => sub { + plan tests => 2; + + my $branchcode = $builder->build({ source => 'Branch' })->{'branchcode'}; + my $categorycode = $builder->build({ source => 'Category' })->{'categorycode'}; + my $itemtype = $builder->build({ source => 'Itemtype' })->{'itemtype'}; + + subtest 'Clone multiple rules' => sub { + plan tests => 4; + + Koha::CirculationRules->delete; + + Koha::CirculationRule->new({ + branchcode => undef, + categorycode => $categorycode, + itemtype => $itemtype, + rule_name => 'fine', + rule_value => 5, + })->store; + + Koha::CirculationRule->new({ + branchcode => undef, + categorycode => $categorycode, + itemtype => $itemtype, + rule_name => 'lengthunit', + rule_value => 'days', + })->store; + + Koha::CirculationRules->search({ branchcode => undef })->clone($branchcode); + + my $rule_fine = Koha::CirculationRules->get_effective_rule({ + branchcode => $branchcode, + categorycode => $categorycode, + itemtype => $itemtype, + rule_name => 'fine', + }); + my $rule_lengthunit = Koha::CirculationRules->get_effective_rule({ + branchcode => $branchcode, + categorycode => $categorycode, + itemtype => $itemtype, + rule_name => 'lengthunit', + }); + + _is_row_match( + $rule_fine, + { + branchcode => $branchcode, + categorycode => $categorycode, + itemtype => $itemtype, + rule_name => 'fine', + rule_value => 5, + }, + 'When I attempt to get cloned fine rule,' + .' then the above one is returned.' + ); + _is_row_match( + $rule_lengthunit, + { + branchcode => $branchcode, + categorycode => $categorycode, + itemtype => $itemtype, + rule_name => 'lengthunit', + rule_value => 'days', + }, + 'When I attempt to get cloned lengthunit rule,' + .' then the above one is returned.' + ); + + }; + + subtest 'Clone one rule' => sub { + plan tests => 2; + + Koha::CirculationRules->delete; + + Koha::CirculationRule->new({ + branchcode => undef, + categorycode => $categorycode, + itemtype => $itemtype, + rule_name => 'fine', + rule_value => 5, + })->store; + + my $rule = Koha::CirculationRules->search({ branchcode => undef })->next; + $rule->clone($branchcode); + + my $cloned_rule = Koha::CirculationRules->get_effective_rule({ + branchcode => $branchcode, + categorycode => $categorycode, + itemtype => $itemtype, + rule_name => 'fine', + }); + + _is_row_match( + $cloned_rule, + { + branchcode => $branchcode, + categorycode => $categorycode, + itemtype => $itemtype, + rule_name => 'fine', + rule_value => '5', + }, + 'When I attempt to get cloned fine rule,' + .' then the above one is returned.' + ); + + }; +}; + sub _is_row_match { my ( $rule, $expected, $message ) = @_; -- 2.11.0