Bug 18936: (follow-up) Add cloning of circulation rules back to Koha
authorJoonas Kylmälä <joonas.kylmala@helsinki.fi>
Tue, 28 Jan 2020 13:46:55 +0000 (13:46 +0000)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Tue, 4 Feb 2020 09:56:29 +0000 (09:56 +0000)
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 <minna.kivinen@hamk.fi>
Signed-off-by: Joonas Kylmälä <joonas.kylmala@helsinki.fi>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Koha/CirculationRule.pm
Koha/CirculationRules.pm
admin/clone-rules.pl
admin/smart-rules.pl
t/db_dependent/Koha/IssuingRules.t

index 5206c7d..d5c9b86 100644 (file)
@@ -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
index 2aaf056..1e50251 100644 (file)
@@ -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 } );
index dd07125..cefc11e 100755 (executable)
@@ -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;
index 4d4ec5a..ceb4a9b 100755 (executable)
@@ -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;
 
index 2cdb143..4d37fcf 100644 (file)
@@ -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 ) = @_;