View | Details | Raw Unified | Return to bug 18936
Collapse All | Expand All

(-)a/Koha/CirculationRule.pm (+15 lines)
Lines 71-76 sub item_type { Link Here
71
    return $self->{item_type};
71
    return $self->{item_type};
72
}
72
}
73
73
74
=head3 clone
75
76
Clone a circulation rule to another branch
77
78
=cut
79
80
sub clone {
81
    my ($self, $to_branch) = @_;
82
83
    my $cloned_rule = $self->unblessed;
84
    $cloned_rule->{branchcode} = $to_branch;
85
    delete $cloned_rule->{id};
86
    return Koha::CirculationRule->new( $cloned_rule )->store;
87
}
88
74
=head3 _type
89
=head3 _type
75
90
76
=cut
91
=cut
(-)a/Koha/CirculationRules.pm (+14 lines)
Lines 358-363 sub delete { Link Here
358
    }
358
    }
359
}
359
}
360
360
361
=head3 clone
362
363
Clone a set of circulation rules to another branch
364
365
=cut
366
367
sub clone {
368
    my ( $self, $to_branch ) = @_;
369
370
    while ( my $rule = $self->next ){
371
        $rule->clone($to_branch);
372
    }
373
}
374
361
=head3 get_opacitemholds_policy
375
=head3 get_opacitemholds_policy
362
376
363
my $can_place_a_hold_at_item_level = Koha::CirculationRules->get_opacitemholds_policy( { patron => $patron, item => $item } );
377
my $can_place_a_hold_at_item_level = Koha::CirculationRules->get_opacitemholds_policy( { patron => $patron, item => $item } );
(-)a/admin/clone-rules.pl (-41 / +10 lines)
Lines 32-40 use C4::Output; Link Here
32
use C4::Auth;
32
use C4::Auth;
33
use C4::Koha;
33
use C4::Koha;
34
use C4::Debug;
34
use C4::Debug;
35
use Koha::CirculationRules;
35
36
36
my $input = new CGI;
37
my $input = new CGI;
37
my $dbh = C4::Context->dbh;
38
38
39
my ($template, $loggedinuser, $cookie)
39
my ($template, $loggedinuser, $cookie)
40
    = get_template_and_user({template_name => "admin/clone-rules.tt",
40
    = get_template_and_user({template_name => "admin/clone-rules.tt",
Lines 51-100 my $tobranch = $input->param("tobranch"); Link Here
51
$template->param(frombranch     => $frombranch)                if ($frombranch);
51
$template->param(frombranch     => $frombranch)                if ($frombranch);
52
$template->param(tobranch       => $tobranch)                  if ($tobranch);
52
$template->param(tobranch       => $tobranch)                  if ($tobranch);
53
53
54
if ($frombranch && $tobranch) {
54
if ($frombranch && $tobranch && $frombranch ne $tobranch) {
55
    $frombranch = ( $frombranch ne '*' ? $frombranch : undef );
56
    $tobranch = ( $tobranch ne '*' ? $tobranch : undef );
55
57
56
    my $error;	
58
    Koha::CirculationRules->search({branchcode => $tobranch})->delete;
57
59
58
    # First, we create a temporary table with the rules we want to clone
60
    my $rules = Koha::CirculationRules->search({ branchcode => $frombranch });
59
    my $query = "CREATE TEMPORARY TABLE tmpissuingrules ENGINE=memory SELECT * FROM issuingrules WHERE branchcode=?";
61
    $rules->clone($tobranch);
60
    my $sth = $dbh->prepare($query);
62
} else {
61
    my $res = $sth->execute($frombranch);
63
    $template->param(error => 1);
62
    $error = 1 unless ($res);
63
64
    if (!$error) {
65
	# We modify these rules according to the new branchcode
66
	$query = "UPDATE tmpissuingrules SET branchcode=? WHERE branchcode=?";
67
	$sth = $dbh->prepare($query);
68
	$res = $sth->execute($tobranch, $frombranch);
69
	$error = 1 unless ($res);
70
    }
71
72
    if (!$error) {
73
	# We delete the rules for the existing branchode
74
	$query = "DELETE FROM issuingrules WHERE branchcode=?";
75
	$sth = $dbh->prepare($query);
76
	$res = $sth->execute($tobranch);
77
	$error = 1 unless ($res);
78
    }
79
80
81
    if (!$error) {
82
	# We insert the new rules from our temporary table
83
	$query = "INSERT INTO issuingrules SELECT * FROM tmpissuingrules WHERE branchcode=?";
84
	$sth = $dbh->prepare($query);
85
	$res = $sth->execute($tobranch);
86
	$error = 1 unless ($res);
87
    }
88
89
    # Finally, we delete our temporary table
90
    $query = "DROP TABLE tmpissuingrules";
91
    $sth = $dbh->prepare($query);
92
    $res = $sth->execute();
93
94
    $template->param(result => "1");
95
    $template->param(error  => $error);
96
}
64
}
97
65
66
$template->param(result => 1);
98
67
99
68
100
output_html_with_http_headers $input, $cookie, $template->output;
69
output_html_with_http_headers $input, $cookie, $template->output;
(-)a/admin/smart-rules.pl (-1 / +6 lines)
Lines 574-586 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['des Link Here
574
574
575
my $itemtypes = Koha::ItemTypes->search_with_localization;
575
my $itemtypes = Koha::ItemTypes->search_with_localization;
576
576
577
my $humanbranch = ( $branch ne '*' ? $branch : undef );
578
579
my $definedbranch = Koha::CirculationRules->search({ branchcode => $humanbranch })->count ? 1 : 0;
580
577
$template->param(show_branch_cat_rule_form => 1);
581
$template->param(show_branch_cat_rule_form => 1);
578
582
579
$template->param(
583
$template->param(
580
    patron_categories => $patron_categories,
584
    patron_categories => $patron_categories,
581
    itemtypeloop      => $itemtypes,
585
    itemtypeloop      => $itemtypes,
582
    humanbranch       => ( $branch ne '*' ? $branch : undef ),
586
    humanbranch       => $humanbranch,
583
    current_branch    => $branch,
587
    current_branch    => $branch,
588
    definedbranch     => $definedbranch,
584
);
589
);
585
output_html_with_http_headers $input, $cookie, $template->output;
590
output_html_with_http_headers $input, $cookie, $template->output;
586
591
(-)a/t/db_dependent/Koha/IssuingRules.t (-2 / +110 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 2;
22
use Test::More tests => 3;
23
use Test::Deep qw( cmp_methods );
23
use Test::Deep qw( cmp_methods );
24
use Test::Exception;
24
use Test::Exception;
25
25
Lines 566-571 subtest 'set_rule' => sub { Link Here
566
    };
566
    };
567
};
567
};
568
568
569
subtest 'clone' => sub {
570
    plan tests => 2;
571
572
    my $branchcode   = $builder->build({ source => 'Branch' })->{'branchcode'};
573
    my $categorycode = $builder->build({ source => 'Category' })->{'categorycode'};
574
    my $itemtype     = $builder->build({ source => 'Itemtype' })->{'itemtype'};
575
576
    subtest 'Clone multiple rules' => sub {
577
        plan tests => 4;
578
579
        Koha::CirculationRules->delete;
580
581
        Koha::CirculationRule->new({
582
            branchcode   => undef,
583
            categorycode => $categorycode,
584
            itemtype     => $itemtype,
585
            rule_name    => 'fine',
586
            rule_value   => 5,
587
        })->store;
588
589
        Koha::CirculationRule->new({
590
            branchcode   => undef,
591
            categorycode => $categorycode,
592
            itemtype     => $itemtype,
593
            rule_name    => 'lengthunit',
594
            rule_value   => 'days',
595
        })->store;
596
597
        Koha::CirculationRules->search({ branchcode => undef })->clone($branchcode);
598
599
        my $rule_fine = Koha::CirculationRules->get_effective_rule({
600
            branchcode   => $branchcode,
601
            categorycode => $categorycode,
602
            itemtype     => $itemtype,
603
            rule_name    => 'fine',
604
        });
605
        my $rule_lengthunit = Koha::CirculationRules->get_effective_rule({
606
            branchcode   => $branchcode,
607
            categorycode => $categorycode,
608
            itemtype     => $itemtype,
609
            rule_name    => 'lengthunit',
610
        });
611
612
        _is_row_match(
613
            $rule_fine,
614
            {
615
                branchcode   => $branchcode,
616
                categorycode => $categorycode,
617
                itemtype     => $itemtype,
618
                rule_name    => 'fine',
619
                rule_value   => 5,
620
            },
621
            'When I attempt to get cloned fine rule,'
622
           .' then the above one is returned.'
623
        );
624
        _is_row_match(
625
            $rule_lengthunit,
626
            {
627
                branchcode   => $branchcode,
628
                categorycode => $categorycode,
629
                itemtype     => $itemtype,
630
                rule_name    => 'lengthunit',
631
                rule_value   => 'days',
632
            },
633
            'When I attempt to get cloned lengthunit rule,'
634
           .' then the above one is returned.'
635
        );
636
637
    };
638
639
    subtest 'Clone one rule' => sub {
640
        plan tests => 2;
641
642
        Koha::CirculationRules->delete;
643
644
        Koha::CirculationRule->new({
645
            branchcode   => undef,
646
            categorycode => $categorycode,
647
            itemtype     => $itemtype,
648
            rule_name    => 'fine',
649
            rule_value   => 5,
650
        })->store;
651
652
        my $rule = Koha::CirculationRules->search({ branchcode => undef })->next;
653
        $rule->clone($branchcode);
654
655
        my $cloned_rule = Koha::CirculationRules->get_effective_rule({
656
            branchcode   => $branchcode,
657
            categorycode => $categorycode,
658
            itemtype     => $itemtype,
659
            rule_name    => 'fine',
660
        });
661
662
        _is_row_match(
663
            $cloned_rule,
664
            {
665
                branchcode   => $branchcode,
666
                categorycode => $categorycode,
667
                itemtype     => $itemtype,
668
                rule_name    => 'fine',
669
                rule_value   => '5',
670
            },
671
            'When I attempt to get cloned fine rule,'
672
           .' then the above one is returned.'
673
        );
674
675
    };
676
};
677
569
sub _is_row_match {
678
sub _is_row_match {
570
    my ( $rule, $expected, $message ) = @_;
679
    my ( $rule, $expected, $message ) = @_;
571
680
572
- 

Return to bug 18936