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

(-)a/C4/Circulation.pm (-23 / +1 lines)
Lines 2877-2883 sub CanBookBeRenewed { Link Here
2877
    my $item      = Koha::Items->find($itemnumber)      or return ( 0, 'no_item' );
2877
    my $item      = Koha::Items->find($itemnumber)      or return ( 0, 'no_item' );
2878
    my $issue = $item->checkout or return ( 0, 'no_checkout' );
2878
    my $issue = $item->checkout or return ( 0, 'no_checkout' );
2879
    return ( 0, 'onsite_checkout' ) if $issue->onsite_checkout;
2879
    return ( 0, 'onsite_checkout' ) if $issue->onsite_checkout;
2880
    return ( 0, 'item_denied_renewal') if _item_denied_renewal({ item => $item });
2880
    return ( 0, 'item_denied_renewal') if $item->is_denied_renewal;
2881
2881
2882
    my $patron = $issue->patron or return;
2882
    my $patron = $issue->patron or return;
2883
2883
Lines 4448-4475 sub _CanBookBeAutoRenewed { Link Here
4448
    return "ok";
4448
    return "ok";
4449
}
4449
}
4450
4450
4451
sub _item_denied_renewal {
4452
    my ($params) = @_;
4453
4454
    my $item = $params->{item};
4455
    return unless $item;
4456
4457
    my $denyingrules = Koha::Config::SysPrefs->find('ItemsDeniedRenewal')->get_yaml_pref_hash();
4458
    return unless $denyingrules;
4459
    foreach my $field (keys %$denyingrules) {
4460
        my $val = $item->$field;
4461
        if( !defined $val) {
4462
            if ( any { !defined $_ }  @{$denyingrules->{$field}} ){
4463
                return 1;
4464
            }
4465
        } elsif (any { defined($_) && $val eq $_ } @{$denyingrules->{$field}}) {
4466
           # If the results matches the values in the syspref
4467
           # We return true if match found
4468
            return 1;
4469
        }
4470
    }
4471
    return 0;
4472
}
4473
4451
4474
1;
4452
1;
4475
4453
(-)a/Koha/Item.pm (+29 lines)
Lines 1928-1933 sub is_notforloan { Link Here
1928
    return $is_notforloan;
1928
    return $is_notforloan;
1929
}
1929
}
1930
1930
1931
=head3 is_denied_renewal
1932
1933
    my $is_denied_renewal = $item->is_denied_renewal;
1934
1935
Determine whether or not this item can be renewed based on the
1936
rules set in the ItemsDeniedRenewal system preference.
1937
1938
=cut
1939
1940
sub is_denied_renewal {
1941
    my ( $self ) = @_;
1942
1943
    my $denyingrules = Koha::Config::SysPrefs->find('ItemsDeniedRenewal')->get_yaml_pref_hash();
1944
    return 0 unless $denyingrules;
1945
    foreach my $field (keys %$denyingrules) {
1946
        my $val = $self->$field;
1947
        if( !defined $val) {
1948
            if ( any { !defined $_ }  @{$denyingrules->{$field}} ){
1949
                return 1;
1950
            }
1951
        } elsif (any { defined($_) && $val eq $_ } @{$denyingrules->{$field}}) {
1952
           # If the results matches the values in the syspref
1953
           # We return true if match found
1954
            return 1;
1955
        }
1956
    }
1957
    return 0;
1958
}
1959
1931
=head3 _type
1960
=head3 _type
1932
1961
1933
=cut
1962
=cut
(-)a/t/db_dependent/Circulation.t (-92 / +18 lines)
Lines 4107-4116 subtest 'CanBookBeIssued | is_overdue' => sub { Link Here
4107
    is( $needsconfirmation->{TOO_MANY}, undef, "Not too many, is a renewal");
4107
    is( $needsconfirmation->{TOO_MANY}, undef, "Not too many, is a renewal");
4108
};
4108
};
4109
4109
4110
subtest 'ItemsDeniedRenewal preference' => sub {
4110
subtest 'ItemsDeniedRenewal rules are checked' => sub {
4111
    plan tests => 18;
4111
    plan tests => 4;
4112
4113
    C4::Context->set_preference('ItemsDeniedRenewal','');
4114
4112
4115
    my $idr_lib = $builder->build_object({ class => 'Koha::Libraries'});
4113
    my $idr_lib = $builder->build_object({ class => 'Koha::Libraries'});
4116
    Koha::CirculationRules->set_rules(
4114
    Koha::CirculationRules->set_rules(
Lines 4132-4146 subtest 'ItemsDeniedRenewal preference' => sub { Link Here
4132
        }
4130
        }
4133
    );
4131
    );
4134
4132
4135
    my $deny_book = $builder->build_object({ class => 'Koha::Items', value => {
4136
        homebranch => $idr_lib->branchcode,
4137
        withdrawn => 1,
4138
        itype => 'HIDE',
4139
        location => 'PROC',
4140
        itemcallnumber => undef,
4141
        itemnotes => "",
4142
        }
4143
    });
4144
    my $allow_book = $builder->build_object({ class => 'Koha::Items', value => {
4133
    my $allow_book = $builder->build_object({ class => 'Koha::Items', value => {
4145
        homebranch => $idr_lib->branchcode,
4134
        homebranch => $idr_lib->branchcode,
4146
        withdrawn => 0,
4135
        withdrawn => 0,
Lines 4154-4174 subtest 'ItemsDeniedRenewal preference' => sub { Link Here
4154
        }
4143
        }
4155
    });
4144
    });
4156
    my $future = dt_from_string->add( days => 1 );
4145
    my $future = dt_from_string->add( days => 1 );
4157
    my $deny_issue = $builder->build_object(
4146
    my $issue = $builder->build_object(
4158
        {
4159
            class => 'Koha::Checkouts',
4160
            value => {
4161
                returndate      => undef,
4162
                renewals_count  => 0,
4163
                auto_renew      => 0,
4164
                borrowernumber  => $idr_borrower->borrowernumber,
4165
                itemnumber      => $deny_book->itemnumber,
4166
                onsite_checkout => 0,
4167
                date_due        => $future,
4168
            }
4169
        }
4170
    );
4171
    my $allow_issue = $builder->build_object(
4172
        {
4147
        {
4173
            class => 'Koha::Checkouts',
4148
            class => 'Koha::Checkouts',
4174
            value => {
4149
            value => {
Lines 4183-4252 subtest 'ItemsDeniedRenewal preference' => sub { Link Here
4183
        }
4158
        }
4184
    );
4159
    );
4185
4160
4186
    my $idr_rules;
4161
    my $mock_item_class = Test::MockModule->new("Koha::Item");
4187
4162
    $mock_item_class->mock( 'is_denied_renewal', sub { return 1; } );
4188
    my ( $idr_mayrenew, $idr_error ) =
4163
4189
    CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
4164
    my ( $mayrenew, $error ) = CanBookBeRenewed( $idr_borrower->borrowernumber, $issue->itemnumber );
4190
    is( $idr_mayrenew, 1, 'Renewal allowed when no rules' );
4165
    is( $mayrenew, 0, 'Renewal blocked when $item->is_denied_renewal returns true' );
4191
    is( $idr_error, undef, 'Renewal allowed when no rules' );
4166
    is( $error, 'item_denied_renewal', 'Renewal blocked when $item->is_denied_renewal returns true' );
4192
4167
4193
    $idr_rules="withdrawn: [1]";
4168
    $mock_item_class->unmock( 'is_denied_renewal' );
4194
4169
    $mock_item_class->mock( 'is_denied_renewal', sub { return 0; } );
4195
    C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
4170
4196
    ( $idr_mayrenew, $idr_error ) =
4171
    ( $mayrenew, $error ) = CanBookBeRenewed( $idr_borrower->borrowernumber, $issue->itemnumber );
4197
    CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
4172
    is( $mayrenew, 1, 'Renewal allowed when $item->is_denied_renewal returns false' );
4198
    is( $idr_mayrenew, 0, 'Renewal blocked when 1 rules (withdrawn)' );
4173
    is( $error, undef, 'Renewal allowed when $item->is_denied_renewal returns false' );
4199
    is( $idr_error, 'item_denied_renewal', 'Renewal blocked when 1 rule (withdrawn)' );
4174
4200
    ( $idr_mayrenew, $idr_error ) =
4175
    $mock_item_class->unmock( 'is_denied_renewal' );
4201
    CanBookBeRenewed( $idr_borrower->borrowernumber, $allow_issue->itemnumber );
4202
    is( $idr_mayrenew, 1, 'Renewal allowed when 1 rules not matched (withdrawn)' );
4203
    is( $idr_error, undef, 'Renewal allowed when 1 rules not matched (withdrawn)' );
4204
4205
    $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]";
4206
4207
    C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
4208
    ( $idr_mayrenew, $idr_error ) =
4209
    CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
4210
    is( $idr_mayrenew, 0, 'Renewal blocked when 2 rules matched (withdrawn, itype)' );
4211
    is( $idr_error, 'item_denied_renewal', 'Renewal blocked when 2 rules matched (withdrawn,itype)' );
4212
    ( $idr_mayrenew, $idr_error ) =
4213
    CanBookBeRenewed( $idr_borrower->borrowernumber, $allow_issue->itemnumber );
4214
    is( $idr_mayrenew, 1, 'Renewal allowed when 2 rules not matched (withdrawn, itype)' );
4215
    is( $idr_error, undef, 'Renewal allowed when 2 rules not matched (withdrawn, itype)' );
4216
4217
    $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]\nlocation: [PROC]";
4218
4219
    C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
4220
    ( $idr_mayrenew, $idr_error ) =
4221
    CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
4222
    is( $idr_mayrenew, 0, 'Renewal blocked when 3 rules matched (withdrawn, itype, location)' );
4223
    is( $idr_error, 'item_denied_renewal', 'Renewal blocked when 3 rules matched (withdrawn,itype, location)' );
4224
    ( $idr_mayrenew, $idr_error ) =
4225
    CanBookBeRenewed( $idr_borrower->borrowernumber, $allow_issue->itemnumber );
4226
    is( $idr_mayrenew, 1, 'Renewal allowed when 3 rules not matched (withdrawn, itype, location)' );
4227
    is( $idr_error, undef, 'Renewal allowed when 3 rules not matched (withdrawn, itype, location)' );
4228
4229
    $idr_rules="itemcallnumber: [NULL]";
4230
    C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
4231
    ( $idr_mayrenew, $idr_error ) =
4232
    CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
4233
    is( $idr_mayrenew, 0, 'Renewal blocked for undef when NULL in pref' );
4234
    $idr_rules="itemcallnumber: ['']";
4235
    C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
4236
    ( $idr_mayrenew, $idr_error ) =
4237
    CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
4238
    is( $idr_mayrenew, 1, 'Renewal not blocked for undef when "" in pref' );
4239
4240
    $idr_rules="itemnotes: [NULL]";
4241
    C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
4242
    ( $idr_mayrenew, $idr_error ) =
4243
    CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
4244
    is( $idr_mayrenew, 1, 'Renewal not blocked for "" when NULL in pref' );
4245
    $idr_rules="itemnotes: ['']";
4246
    C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
4247
    ( $idr_mayrenew, $idr_error ) =
4248
    CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
4249
    is( $idr_mayrenew, 0, 'Renewal blocked for empty string when "" in pref' );
4250
};
4176
};
4251
4177
4252
subtest 'CanBookBeIssued | item-level_itypes=biblio' => sub {
4178
subtest 'CanBookBeIssued | item-level_itypes=biblio' => sub {
(-)a/t/db_dependent/Koha/Item.t (-2 / +63 lines)
Lines 20-26 Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
use utf8;
21
use utf8;
22
22
23
use Test::More tests => 26;
23
use Test::More tests => 27;
24
use Test::Exception;
24
use Test::Exception;
25
use Test::MockModule;
25
use Test::MockModule;
26
26
Lines 1749-1751 subtest 'has_pending_recall() tests' => sub { Link Here
1749
1749
1750
    $schema->storage->txn_rollback;
1750
    $schema->storage->txn_rollback;
1751
};
1751
};
1752
- 
1752
1753
subtest 'is_denied_renewal' => sub {
1754
    plan tests => 11;
1755
1756
    $schema->storage->txn_begin;
1757
1758
    my $library = $builder->build_object({ class => 'Koha::Libraries'});
1759
1760
    my $deny_book = $builder->build_object({ class => 'Koha::Items', value => {
1761
        homebranch => $library->branchcode,
1762
        withdrawn => 1,
1763
        itype => 'HIDE',
1764
        location => 'PROC',
1765
        itemcallnumber => undef,
1766
        itemnotes => "",
1767
        }
1768
    });
1769
1770
    my $allow_book = $builder->build_object({ class => 'Koha::Items', value => {
1771
        homebranch => $library->branchcode,
1772
        withdrawn => 0,
1773
        itype => 'NOHIDE',
1774
        location => 'NOPROC'
1775
        }
1776
    });
1777
1778
    my $idr_rules = "";
1779
    C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
1780
    is( $deny_book->is_denied_renewal, 0, 'Renewal allowed when no rules' );
1781
1782
    $idr_rules="withdrawn: [1]";
1783
    C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
1784
    is( $deny_book->is_denied_renewal, 1, 'Renewal blocked when 1 rules (withdrawn)' );
1785
    is( $allow_book->is_denied_renewal, 0, 'Renewal allowed when 1 rules not matched (withdrawn)' );
1786
1787
    $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]";
1788
    is( $deny_book->is_denied_renewal, 1, 'Renewal blocked when 2 rules matched (withdrawn, itype)' );
1789
    is( $allow_book->is_denied_renewal, 0, 'Renewal allowed when 2 rules not matched (withdrawn, itype)' );
1790
1791
    $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]\nlocation: [PROC]";
1792
    C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
1793
    is( $deny_book->is_denied_renewal, 1, 'Renewal blocked when 3 rules matched (withdrawn, itype, location)' );
1794
    is( $allow_book->is_denied_renewal, 0, 'Renewal allowed when 3 rules not matched (withdrawn, itype, location)' );
1795
1796
    $idr_rules="itemcallnumber: [NULL]";
1797
    C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
1798
    is( $deny_book->is_denied_renewal, 1, 'Renewal blocked for undef when NULL in pref' );
1799
1800
    $idr_rules="itemcallnumber: ['']";
1801
    C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
1802
    is( $deny_book->is_denied_renewal, 0, 'Renewal not blocked for undef when "" in pref' );
1803
1804
    $idr_rules="itemnotes: [NULL]";
1805
    C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
1806
    is( $deny_book->is_denied_renewal, 0, 'Renewal not blocked for "" when NULL in pref' );
1807
1808
    $idr_rules="itemnotes: ['']";
1809
    C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
1810
    is( $deny_book->is_denied_renewal, 1, 'Renewal blocked for empty string when "" in pref' );
1811
1812
    $schema->storage->txn_rollback;
1813
};

Return to bug 31485