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
}
}
+=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 } );
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",
$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;
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;
use Modern::Perl;
-use Test::More tests => 2;
+use Test::More tests => 3;
use Test::Deep qw( cmp_methods );
use Test::Exception;
};
};
+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 ) = @_;