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

(-)a/Koha/RestrictionType.pm (-1 / +23 lines)
Lines 42-48 sub delete { Link Here
42
    my ( $self ) = @_;
42
    my ( $self ) = @_;
43
43
44
    # Find out what the default is
44
    # Find out what the default is
45
    my $default = Koha::RestrictionTypes->find({ default_value => 1 })->code;
45
    my $default = Koha::RestrictionTypes->find({ is_default => 1 })->code;
46
    # Ensure we're not trying to delete a is_system type (this includes
46
    # Ensure we're not trying to delete a is_system type (this includes
47
    # the default type)
47
    # the default type)
48
    return 0 if $self->is_system == 1;
48
    return 0 if $self->is_system == 1;
Lines 63-68 sub delete { Link Here
63
    return 0;
63
    return 0;
64
}
64
}
65
65
66
=head3 make_default
67
68
Set the current restriction type as the default for manual restrictions
69
70
=cut
71
72
sub make_default {
73
    my ( $self ) = @_;
74
75
    $self->_result->result_source->schema->txn_do(
76
        sub {
77
            my $types =
78
              Koha::RestrictionTypes->search( { code => { '!=' => $self->code } } );
79
            $types->update( { is_default => 0 } );
80
            $self->set( { is_default => 1 } );
81
            $self->SUPER::store;
82
        }
83
    );
84
85
    return $self;
86
}
87
66
=head2 Internal methods
88
=head2 Internal methods
67
89
68
=head3 type
90
=head3 type
(-)a/admin/restrictions.pl (-6 / +5 lines)
Lines 53-59 if ( $op eq 'add_form') { Link Here
53
} elsif ( $op eq 'add_validate' ) {
53
} elsif ( $op eq 'add_validate' ) {
54
54
55
    my $display_text = $input->param('display_text');
55
    my $display_text = $input->param('display_text');
56
    my $can_be_added_manually = $input->param('can_be_added_manually') || 0;
57
    my $is_a_modif = $input->param("is_a_modif");
56
    my $is_a_modif = $input->param("is_a_modif");
58
57
59
    if ($is_a_modif) {
58
    if ($is_a_modif) {
Lines 68-76 if ( $op eq 'add_form') { Link Here
68
        } else {
67
        } else {
69
            my $restriction = Koha::RestrictionTypes->find($code);
68
            my $restriction = Koha::RestrictionTypes->find($code);
70
            $restriction->display_text($display_text);
69
            $restriction->display_text($display_text);
71
            unless ($restriction->is_system) {
72
                $restriction->can_be_added_manually($can_be_added_manually);
73
            }
74
            $restriction->store;
70
            $restriction->store;
75
        }
71
        }
76
    } else {
72
    } else {
Lines 83-95 if ( $op eq 'add_form') { Link Here
83
        } else {
79
        } else {
84
            my $restriction = Koha::RestrictionType->new({
80
            my $restriction = Koha::RestrictionType->new({
85
                code => $code,
81
                code => $code,
86
                display_text => $display_text,
82
                display_text => $display_text
87
                can_be_added_manually => $can_be_added_manually
88
            });
83
            });
89
            $restriction->store;
84
            $restriction->store;
90
        }
85
        }
91
    }
86
    }
92
    $op = 'list';
87
    $op = 'list';
88
} elsif ( $op eq 'make_default' ) {
89
    my $restriction = Koha::RestrictionTypes->find($code);
90
    $restriction->make_default;
91
    $op = 'list';
93
} elsif ( $op eq 'delete_confirm' ) {
92
} elsif ( $op eq 'delete_confirm' ) {
94
    $template->param(
93
    $template->param(
95
        restriction => scalar Koha::RestrictionTypes->find($code)
94
        restriction => scalar Koha::RestrictionTypes->find($code)
(-)a/installer/data/mysql/atomicupdate/bug_23681.pl (-7 / +6 lines)
Lines 12-29 return { Link Here
12
                code varchar(50) NOT NULL PRIMARY KEY,
12
                code varchar(50) NOT NULL PRIMARY KEY,
13
                display_text text NOT NULL,
13
                display_text text NOT NULL,
14
                is_system tinyint(1) NOT NULL DEFAULT 0,
14
                is_system tinyint(1) NOT NULL DEFAULT 0,
15
                default_value tinyint(1) NOT NULL DEFAULT 0,
15
                is_default tinyint(1) NOT NULL DEFAULT 0
16
                can_be_added_manually tinyint(1) NOT NULL DEFAULT 0
17
            ) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
16
            ) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
18
        });
17
        });
19
        say $out "Added debarment_types table";
18
        say $out "Added debarment_types table";
20
19
21
        $dbh->do(q{
20
        $dbh->do(q{
22
            INSERT IGNORE INTO debarment_types (code, display_text, is_system, default_value, can_be_added_manually) VALUES
21
            INSERT IGNORE INTO debarment_types (code, display_text, is_system, is_default) VALUES
23
            ('MANUAL', 'Manual', 1, 1, 0),
22
            ('MANUAL', 'Manual', 0, 1),
24
            ('OVERDUES', 'Overdues', 1, 0, 0),
23
            ('OVERDUES', 'Overdues', 1, 0),
25
            ('SUSPENSION', 'Suspension', 1, 0, 0),
24
            ('SUSPENSION', 'Suspension', 1, 0),
26
            ('DISCHARGE', 'Discharge', 1, 0, 0);
25
            ('DISCHARGE', 'Discharge', 1, 0);
27
        });
26
        });
28
        say $out "Added system debarment_types";
27
        say $out "Added system debarment_types";
29
28
(-)a/installer/data/mysql/en/mandatory/patron_restrictions.yml (-8 / +4 lines)
Lines 28-50 tables: Link Here
28
        - code: "MANUAL"
28
        - code: "MANUAL"
29
          display_text: "Manual"
29
          display_text: "Manual"
30
          is_system: 1
30
          is_system: 1
31
          default: 1
31
          is_default: 1
32
          can_be_added_manually: 0
33
32
34
        - code: "OVERDUES"
33
        - code: "OVERDUES"
35
          display_text: "Overdues"
34
          display_text: "Overdues"
36
          is_system: 1
35
          is_system: 1
37
          default: 0
36
          is_default: 0
38
          can_be_added_manually: 0
39
37
40
        - code: "SUSPENSION"
38
        - code: "SUSPENSION"
41
          display_text: "Suspension"
39
          display_text: "Suspension"
42
          is_system: 1
40
          is_system: 1
43
          default: 0
41
          is_default: 0
44
          can_be_added_manually: 0
45
42
46
        - code: "DISCHARGE"
43
        - code: "DISCHARGE"
47
          display_text: "Discharge"
44
          display_text: "Discharge"
48
          is_system: 1
45
          is_system: 1
49
          default: 0
46
          is_default: 0
50
          can_be_added_manually: 0
(-)a/installer/data/mysql/kohastructure.sql (-2 / +1 lines)
Lines 2033-2040 CREATE TABLE debarment_types ( Link Here
2033
    code varchar(50) NOT NULL PRIMARY KEY,
2033
    code varchar(50) NOT NULL PRIMARY KEY,
2034
    display_text text NOT NULL,
2034
    display_text text NOT NULL,
2035
    is_system tinyint(1) NOT NULL DEFAULT 0,
2035
    is_system tinyint(1) NOT NULL DEFAULT 0,
2036
    default_value tinyint(1) NOT NULL DEFAULT 0,
2036
    is_default tinyint(1) NOT NULL DEFAULT 0
2037
    can_be_added_manually tinyint(1) NOT NULL DEFAULT 0
2038
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
2037
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
2039
2038
2040
--
2039
--
(-)a/koha-tmpl/intranet-tmpl/prog/en/includes/borrower_debarments.inc (-2 / +6 lines)
Lines 58-65 Link Here
58
                        <label for="debarred_type">Type:</label>
58
                        <label for="debarred_type">Type:</label>
59
                        <select name="debarred_type">
59
                        <select name="debarred_type">
60
                            [% FOREACH code IN restriction_types.keys %]
60
                            [% FOREACH code IN restriction_types.keys %]
61
                                [% IF restriction_types.$code.can_be_added_manually %]
61
                                [% IF !restriction_types.$code.is_system %]
62
                                    <option value="[% code | html %]">[% PROCESS restriction_type_description restriction=restriction_types.$code %]</option>
62
                                   [% IF restriction_types.$code.is_default %]
63
                                   <option value="[% code | html %]" selected>[% PROCESS restriction_type_description restriction=restriction_types.$code %]</option>
64
                                   [% ELSE %]
65
                                   <option value="[% code | html %]">[% PROCESS restriction_type_description restriction=restriction_types.$code %]</option>
66
                                   [% END %]
63
                                [% END %]
67
                                [% END %]
64
                            [% END %]
68
                            [% END %]
65
                        </select>
69
                        </select>
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/restrictions.tt (-26 / +5 lines)
Lines 103-118 Link Here
103
                        <input type="text" value="[% restriction.display_text | html %]" name="display_text" id="display_text" size="50" maxlength="50" class="required" required="required" />
103
                        <input type="text" value="[% restriction.display_text | html %]" name="display_text" id="display_text" size="50" maxlength="50" class="required" required="required" />
104
                        <span class="required">Required</span>
104
                        <span class="required">Required</span>
105
                    </li>
105
                    </li>
106
                    <li>
107
                        <label for="can_be_added_manually">Can be manually added?</label>
108
                        [% IF restriction && restriction.is_system %]
109
                            [% IF restriction.can_be_added_manually %]Yes[% ELSE %]No[% END %]
110
                        [% ELSIF restriction.can_be_added_manually %]
111
                            <input type="checkbox" name="can_be_added_manually" id="can_be_added_manually" checked="checked" value="1" />
112
                        [% ELSE %]
113
                            <input type="checkbox" name="can_be_added_manually" id="can_be_added_manually" value="1" />
114
                        [% END %]
115
                    </li>
116
                [% ELSE %]
106
                [% ELSE %]
117
                    <li>
107
                    <li>
118
                        <label for="code" class="required">Code: </label>
108
                        <label for="code" class="required">Code: </label>
Lines 124-139 Link Here
124
                        <input type="text" name="display_text" id="display_text" size="50" maxlength="50" class="required" required="required" />
114
                        <input type="text" name="display_text" id="display_text" size="50" maxlength="50" class="required" required="required" />
125
                        <span class="required">Required</span>
115
                        <span class="required">Required</span>
126
                    </li>
116
                    </li>
127
                    <li>
128
                        <label for="can_be_added_manually">Can be manually added?</label>
129
                        [% IF restriction && restriction.is_system %]
130
                            [% IF restriction.can_be_added_manually %]Yes[% ELSE %]No[% END %]
131
                        [% ELSIF restriction.can_be_added_manually %]
132
                            <input type="checkbox" name="can_be_added_manually" id="can_be_added_manually" checked="checked" value="1" />
133
                        [% ELSE %]
134
                            <input type="checkbox" name="can_be_added_manually" id="can_be_added_manually" value="1" />
135
                        [% END %]
136
                    </li>
137
                [% END %]
117
                [% END %]
138
            </ol>
118
            </ol>
139
        </fieldset>
119
        </fieldset>
Lines 180-186 Link Here
180
                <tr>
160
                <tr>
181
                    <th scope="col">Code</th>
161
                    <th scope="col">Code</th>
182
                    <th scope="col">Label</th>
162
                    <th scope="col">Label</th>
183
                    <th scope="col">Manual</th>
184
                    <th scope="col">Default</th>
163
                    <th scope="col">Default</th>
185
                    <th scope="col">Actions</th>
164
                    <th scope="col">Actions</th>
186
                </tr>
165
                </tr>
Lines 195-210 Link Here
195
                            [% PROCESS restriction_type_description %]
174
                            [% PROCESS restriction_type_description %]
196
                        </td>
175
                        </td>
197
                        <td>
176
                        <td>
198
                            [% IF restriction.can_be_added_manually %]Yes[% END %]
177
                            [% IF restriction.is_default %]Yes[% END %]
199
                        </td>
200
                        <td>
201
                            [% IF restriction.default_value %]Yes[% END %]
202
                        </td>
178
                        </td>
203
                        <td class="actions">
179
                        <td class="actions">
204
                            <a class="btn btn-default btn-xs" href="/cgi-bin/koha/admin/restrictions.pl?op=add_form&amp;code=[% restriction.code | uri %]"><i class="fa fa-pencil"></i> Edit</a>
180
                            <a class="btn btn-default btn-xs" href="/cgi-bin/koha/admin/restrictions.pl?op=add_form&amp;code=[% restriction.code | uri %]"><i class="fa fa-pencil"></i> Edit</a>
205
                            [% IF !restriction.is_system %]
181
                            [% IF !restriction.is_system && !restriction.is_default %]
206
                                <a class="btn btn-default btn-xs" href="/cgi-bin/koha/admin/restrictions.pl?op=delete_confirm&amp;code=[% restriction.code | uri %]"><i class="fa fa-trash"></i> Delete</a>
182
                                <a class="btn btn-default btn-xs" href="/cgi-bin/koha/admin/restrictions.pl?op=delete_confirm&amp;code=[% restriction.code | uri %]"><i class="fa fa-trash"></i> Delete</a>
207
                            [% END %]
183
                            [% END %]
184
                            [% IF !restriction.is_system && !restriction.is_default %]
185
                                <a class="btn btn-default btn-xs" href="/cgi-bin/koha/admin/restrictions.pl?op=make_default&amp;code=[% restriction.code | uri %]"><i class="fa fa-archive"></i> Make default</a>
186
                            [% END %]
208
                        </td>
187
                        </td>
209
                    </tr>
188
                    </tr>
210
                [% END %]
189
                [% END %]
(-)a/t/db_dependent/RestrictionType.t (-24 / +28 lines)
Lines 10-60 use Test::More tests => 3; Link Here
10
10
11
my $schema = Koha::Database->new->schema;
11
my $schema = Koha::Database->new->schema;
12
$schema->storage->txn_begin;
12
$schema->storage->txn_begin;
13
my $dbh = C4::Context->dbh;
13
my $dbh     = C4::Context->dbh;
14
my $builder = t::lib::TestBuilder->new;
14
my $builder = t::lib::TestBuilder->new;
15
15
16
use_ok('Koha::RestrictionType');
16
use_ok('Koha::RestrictionType');
17
use_ok('Koha::RestrictionTypes');
17
use_ok('Koha::RestrictionTypes');
18
18
19
$dbh->do(q|DELETE FROM borrower_debarments|);
19
$dbh->do(q|DELETE FROM borrower_debarments|);
20
Koha::RestrictionTypes->search->delete;
20
$dbh->do(q|DELETE FROM debarment_types|);
21
21
22
$builder->build({
22
$builder->build(
23
    source => 'DebarmentType',
23
    {
24
    value  => {
24
        source => 'DebarmentType',
25
        code         => 'ONE',
25
        value  => {
26
        display_text => 'One',
26
            code         => 'ONE',
27
        readonly     => 1,
27
            display_text => 'One',
28
        is_system    => 0
28
            is_system    => 1,
29
            is_default   => 0
30
        }
29
    }
31
    }
30
});
32
);
31
$builder->build({
33
$builder->build(
32
    source => 'DebarmentType',
34
    {
33
    value  => {
35
        source => 'DebarmentType',
34
        code         => 'TWO',
36
        value  => {
35
        display_text => 'Two',
37
            code         => 'TWO',
36
        readonly     => 1,
38
            display_text => 'Two',
37
        is_system    => 1
39
            is_system    => 1,
40
            is_default   => 1
41
        }
38
    }
42
    }
39
});
43
);
40
44
41
# keyed_on_code
45
# keyed_on_code
42
my $keyed = Koha::RestrictionTypes->keyed_on_code;
46
my $keyed     = Koha::RestrictionTypes->keyed_on_code;
43
my $expecting = {
47
my $expecting = {
44
    ONE => {
48
    ONE => {
45
        code         => 'ONE',
49
        code         => 'ONE',
46
        display_text => 'One',
50
        display_text => 'One',
47
        readonly     => 1,
51
        is_system    => 1,
48
        is_system    => 0
52
        is_default   => 0
49
    },
53
    },
50
    TWO => {
54
    TWO => {
51
        code         => 'TWO',
55
        code         => 'TWO',
52
        display_text => 'Two',
56
        display_text => 'Two',
53
        readonly     => 1,
57
        is_system    => 1,
54
        is_system    => 1
58
        is_default   => 1
55
    }
59
    }
56
};
60
};
57
61
58
is_deeply($keyed, $expecting, 'keyed_on_code returns correctly');
62
is_deeply( $keyed, $expecting, 'keyed_on_code returns correctly' );
59
63
60
$schema->storage->txn_rollback;
64
$schema->storage->txn_rollback;
(-)a/t/db_dependent/RestrictionTypes.t (-71 / +81 lines)
Lines 10-16 use Test::More tests => 6; Link Here
10
10
11
my $schema = Koha::Database->new->schema;
11
my $schema = Koha::Database->new->schema;
12
$schema->storage->txn_begin;
12
$schema->storage->txn_begin;
13
my $dbh = C4::Context->dbh;
13
my $dbh     = C4::Context->dbh;
14
my $builder = t::lib::TestBuilder->new;
14
my $builder = t::lib::TestBuilder->new;
15
15
16
use_ok('Koha::RestrictionType');
16
use_ok('Koha::RestrictionType');
Lines 19-109 use_ok('Koha::RestrictionTypes'); Link Here
19
$dbh->do(q|DELETE FROM borrower_debarments|);
19
$dbh->do(q|DELETE FROM borrower_debarments|);
20
$dbh->do(q|DELETE FROM debarment_types|);
20
$dbh->do(q|DELETE FROM debarment_types|);
21
21
22
$builder->build({
22
$builder->build(
23
    source => 'DebarmentType',
23
    {
24
    value  => {
24
        source => 'DebarmentType',
25
        code         => 'ONE',
25
        value  => {
26
        display_text => 'One',
26
            code         => 'ONE',
27
        is_system     => 1,
27
            display_text => 'One',
28
        default_value    => 0,
28
            is_system    => 1,
29
        can_be_added_manually => 0
29
            is_default   => 0,
30
        }
30
    }
31
    }
31
});
32
);
32
$builder->build({
33
$builder->build(
33
    source => 'DebarmentType',
34
    {
34
    value  => {
35
        source => 'DebarmentType',
35
        code         => 'TWO',
36
        value  => {
36
        display_text => 'Two',
37
            code         => 'TWO',
37
        is_system     => 1,
38
            display_text => 'Two',
38
        default_value    => 1,
39
            is_system    => 1,
39
        can_be_added_manually => 0
40
            is_default   => 1,
41
        }
40
    }
42
    }
41
});
43
);
42
$builder->build({
44
$builder->build(
43
    source => 'DebarmentType',
45
    {
44
    value  => {
46
        source => 'DebarmentType',
45
        code         => 'THREE',
47
        value  => {
46
        display_text => 'Three',
48
            code         => 'THREE',
47
        is_system     => 1,
49
            display_text => 'Three',
48
        default_value    => 0,
50
            is_system    => 1,
49
        can_be_added_manually => 0
51
            is_default   => 0,
52
        }
50
    }
53
    }
51
});
54
);
52
$builder->build({
55
$builder->build(
53
    source => 'DebarmentType',
56
    {
54
    value  => {
57
        source => 'DebarmentType',
55
        code         => 'FOUR',
58
        value  => {
56
        display_text => 'Four',
59
            code         => 'FOUR',
57
        is_system     => 0,
60
            display_text => 'Four',
58
        default_value    => 0,
61
            is_system    => 0,
59
        can_be_added_manually => 0
62
            is_default   => 0,
63
        }
60
    }
64
    }
61
});
65
);
62
$builder->build({
66
$builder->build(
63
    source => 'DebarmentType',
67
    {
64
    value  => {
68
        source => 'DebarmentType',
65
        code         => 'FIVE',
69
        value  => {
66
        display_text => 'Five',
70
            code         => 'FIVE',
67
        is_system     => 0,
71
            display_text => 'Five',
68
        default_value    => 0,
72
            is_system    => 0,
69
        can_be_added_manually => 0
73
            is_default   => 0,
74
        }
70
    }
75
    }
71
});
76
);
72
77
73
# Can we create RestrictionTypes
78
# Can we create RestrictionTypes
74
my $created = Koha::RestrictionTypes->find({ code => 'ONE' });
79
my $created = Koha::RestrictionTypes->find( { code => 'ONE' } );
75
ok( $created->display_text eq 'One', 'Restrictions created');
80
ok( $created->display_text eq 'One', 'Restrictions created' );
76
81
77
# Can we delete RestrictionTypes, when appropriate
82
# Can we delete RestrictionTypes, when appropriate
78
my $deleted = Koha::RestrictionTypes->find({ code => 'FOUR' })->delete;
83
my $deleted = Koha::RestrictionTypes->find( { code => 'FOUR' } )->delete;
79
ok( $deleted, 'Restriction deleted');
84
ok( $deleted, 'Restriction deleted' );
80
my $not_deleted = Koha::RestrictionTypes->find({ code => 'TWO' })->delete;
85
my $not_deleted = Koha::RestrictionTypes->find( { code => 'TWO' } )->delete;
81
ok( !$not_deleted, 'Read only restriction not deleted');
86
ok( !$not_deleted, 'Read only restriction not deleted' );
82
87
83
# Add a patron with a debarment
88
# Add a patron with a debarment
84
my $library = $builder->build({ source => 'Branch' });
89
my $library = $builder->build( { source => 'Branch' } );
85
90
86
my $patron_category = $builder->build({ source => 'Category' });
91
my $patron_category = $builder->build( { source => 'Category' } );
87
my $borrowernumber = Koha::Patron->new({
92
my $borrowernumber  = Koha::Patron->new(
88
    firstname =>  'my firstname',
93
    {
89
    surname => 'my surname',
94
        firstname    => 'my firstname',
90
    categorycode => $patron_category->{categorycode},
95
        surname      => 'my surname',
91
    branchcode => $library->{branchcode},
96
        categorycode => $patron_category->{categorycode},
92
})->store->borrowernumber;
97
        branchcode   => $library->{branchcode},
98
    }
99
)->store->borrowernumber;
93
100
94
Koha::Patron::Debarments::AddDebarment({
101
Koha::Patron::Debarments::AddDebarment(
95
    borrowernumber => $borrowernumber,
102
    {
96
    expiration => '9999-06-10',
103
        borrowernumber => $borrowernumber,
97
    type => 'FIVE',
104
        expiration     => '9999-06-10',
98
    comment => 'Test 1',
105
        type           => 'FIVE',
99
});
106
        comment        => 'Test 1',
107
    }
108
);
100
109
101
# Now delete a code and check debarments using that code switch to
110
# Now delete a code and check debarments using that code switch to
102
# using the default
111
# using the default
103
my $to_delete = Koha::RestrictionTypes->find({ code => 'FIVE' })->delete;
112
my $to_delete  = Koha::RestrictionTypes->find( { code => 'FIVE' } )->delete;
104
my $debarments = Koha::Patron::Debarments::GetDebarments({
113
my $debarments = Koha::Patron::Debarments::GetDebarments(
105
    borrowernumber => $borrowernumber
114
    {
106
});
115
        borrowernumber => $borrowernumber
116
    }
117
);
107
is( $debarments->[0]->{type}, 'TWO', 'Debarments update with restrictions' );
118
is( $debarments->[0]->{type}, 'TWO', 'Debarments update with restrictions' );
108
119
109
$schema->storage->txn_rollback;
120
$schema->storage->txn_rollback;
110
- 

Return to bug 23681