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

(-)a/Koha/CirculationRules.pm (+1 lines)
Lines 84-89 our $RULE_KINDS = { Link Here
84
    },
84
    },
85
    expire_reserves_charge => {
85
    expire_reserves_charge => {
86
        scope => [ 'branchcode', 'categorycode', 'itemtype' ],
86
        scope => [ 'branchcode', 'categorycode', 'itemtype' ],
87
        can_be_blank => 0,
87
    },
88
    },
88
    chargeperiod => {
89
    chargeperiod => {
89
        scope => [ 'branchcode', 'categorycode', 'itemtype' ],
90
        scope => [ 'branchcode', 'categorycode', 'itemtype' ],
(-)a/Koha/Hold.pm (-2 / +1 lines)
Lines 565-572 sub cancel { Link Here
565
                        rule_name    => 'expire_reserves_charge',
565
                        rule_name    => 'expire_reserves_charge',
566
                    }
566
                    }
567
                );
567
                );
568
                my $rule_value = $rule && $rule->rule_value // '';
568
                $charge = $rule ? $rule->rule_value : C4::Context->preference("ExpireReservesMaxPickUpDelayCharge");
569
                $charge = $rule_value ne '' ? $rule_value : C4::Context->preference("ExpireReservesMaxPickUpDelayCharge");
570
569
571
                my $account =
570
                my $account =
572
                  Koha::Account->new( { patron_id => $self->borrowernumber } );
571
                  Koha::Account->new( { patron_id => $self->borrowernumber } );
(-)a/t/db_dependent/Holds/Holdfine.t (-258 lines)
Lines 1-258 Link Here
1
#!/usr/bin/perl
2
3
# Copyright The National Library of Finland, University of Helsinki 2020
4
# Copyright Petro Vashchuk <stalkernoid@gmail.com> 2020
5
#
6
# This file is part of Koha.
7
#
8
# Koha is free software; you can redistribute it and/or modify it under the
9
# terms of the GNU General Public License as published by the Free Software
10
# Foundation; either version 3 of the License, or (at your option) any later
11
# version.
12
#
13
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License along
18
# with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use C4::Context;
23
use Koha::CirculationRules;
24
25
use Test::More tests => 13;
26
27
use t::lib::TestBuilder;
28
use t::lib::Mocks;
29
use Koha::Holds;
30
31
use Koha::Account;
32
use Koha::Account::DebitTypes;
33
34
BEGIN {
35
    use_ok('C4::Reserves');
36
}
37
38
my $schema = Koha::Database->schema;
39
$schema->storage->txn_begin;
40
my $dbh = C4::Context->dbh;
41
42
my $builder = t::lib::TestBuilder->new;
43
44
my $library1 = $builder->build({
45
    source => 'Branch',
46
});
47
my $library2 = $builder->build({
48
    source => 'Branch',
49
});
50
51
my $bib_title = "Test Title";
52
53
my $borrower = $builder->build({
54
    source => 'Borrower',
55
    value => {
56
        branchcode => $library1->{branchcode},
57
    }
58
});
59
60
my $itemtype1 = $builder->build({
61
    source => 'Itemtype',
62
    value => {}
63
});
64
my $itemtype2 = $builder->build({
65
    source => 'Itemtype',
66
    value => {}
67
});
68
my $itemtype3 = $builder->build({
69
    source => 'Itemtype',
70
    value => {}
71
});
72
my $itemtype4 = $builder->build({
73
    source => 'Itemtype',
74
    value => {}
75
});
76
77
my $borrowernumber = $borrower->{borrowernumber};
78
79
my $library_A_code = $library1->{branchcode};
80
81
my $biblio = $builder->build_sample_biblio({itemtype => $itemtype1->{itemtype}});
82
my $biblionumber = $biblio->biblionumber;
83
my $item1 = $builder->build_sample_item({
84
    biblionumber => $biblionumber,
85
    itype => $itemtype1->{itemtype},
86
    homebranch => $library_A_code,
87
    holdingbranch => $library_A_code
88
});
89
my $item2 = $builder->build_sample_item({
90
    biblionumber => $biblionumber,
91
    itype => $itemtype2->{itemtype},
92
    homebranch => $library_A_code,
93
    holdingbranch => $library_A_code
94
});
95
my $item3 = $builder->build_sample_item({
96
    biblionumber => $biblionumber,
97
    itype => $itemtype3->{itemtype},
98
    homebranch => $library_A_code,
99
    holdingbranch => $library_A_code
100
});
101
102
my $library_B_code = $library2->{branchcode};
103
104
my $biblio2 = $builder->build_sample_biblio({itemtype => $itemtype4->{itemtype}});
105
my $biblionumber2 = $biblio2->biblionumber;
106
my $item4 = $builder->build_sample_item({
107
    biblionumber => $biblionumber2,
108
    itype => $itemtype4->{itemtype},
109
    homebranch => $library_B_code,
110
    holdingbranch => $library_B_code
111
});
112
113
$dbh->do("DELETE FROM circulation_rules");
114
Koha::CirculationRules->set_rules(
115
    {
116
        itemtype     => $itemtype1->{itemtype},
117
        categorycode => undef,
118
        branchcode   => undef,
119
        rules        => {
120
            expire_reserves_charge => '111'
121
        }
122
    }
123
);
124
Koha::CirculationRules->set_rules(
125
    {
126
        itemtype     => $itemtype2->{itemtype},
127
        categorycode => undef,
128
        branchcode   => undef,
129
        rules        => {
130
            expire_reserves_charge => undef
131
        }
132
    }
133
);
134
Koha::CirculationRules->set_rules(
135
    {
136
        itemtype     => undef,
137
        categorycode => undef,
138
        branchcode   => $library_B_code,
139
        rules        => {
140
            expire_reserves_charge => '444'
141
        }
142
    }
143
);
144
145
t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
146
147
my $reserve_id;
148
my $account;
149
my $status;
150
my $start_balance;
151
152
# TEST: Hold itemtype1 item
153
$reserve_id = AddReserve(
154
    {
155
        branchcode       => $library_A_code,
156
        borrowernumber   => $borrowernumber,
157
        biblionumber     => $biblionumber,
158
        priority         => 1,
159
        itemnumber       => $item1->itemnumber,
160
    }
161
);
162
163
$account = Koha::Account->new({ patron_id => $borrowernumber });
164
165
( $status ) = CheckReserves($item1->id);
166
is( $status, 'Reserved', "Hold for the itemtype1 created" );
167
168
$start_balance = $account->balance();
169
170
Koha::Holds->find( $reserve_id )->cancel({ charge_cancel_fee => 1 });
171
172
( $status ) = CheckReserves($item1->id);
173
is( $status, '', "Hold for the itemtype1 cancelled" );
174
175
is( $account->balance() - $start_balance, 111, "Used circulation rule for itemtype1" );
176
177
# TEST: circulation rule for itemtype2 has 'expire_reserves_charge' set undef, so it should use ExpireReservesMaxPickUpDelayCharge preference
178
t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 222);
179
180
$reserve_id = AddReserve(
181
    {
182
        branchcode       => $library_A_code,
183
        borrowernumber   => $borrowernumber,
184
        biblionumber     => $biblionumber,
185
        priority         => 1,
186
        itemnumber       => $item2->itemnumber,
187
    }
188
);
189
190
$account = Koha::Account->new({ patron_id => $borrowernumber });
191
192
( $status ) = CheckReserves($item2->id);
193
is( $status, 'Reserved', "Hold for the itemtype2 created" );
194
195
$start_balance = $account->balance();
196
197
Koha::Holds->find( $reserve_id )->cancel({ charge_cancel_fee => 1 });
198
199
( $status ) = CheckReserves($item2->id);
200
is( $status, '', "Hold for the itemtype2 cancelled" );
201
202
is( $account->balance() - $start_balance, 222, "Used ExpireReservesMaxPickUpDelayCharge preference as expire_reserves_charge set to undef" );
203
204
# TEST: no circulation rules for itemtype3, it should use ExpireReservesMaxPickUpDelayCharge preference
205
t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 333);
206
207
$reserve_id = AddReserve(
208
    {
209
        branchcode       => $library_A_code,
210
        borrowernumber   => $borrowernumber,
211
        biblionumber     => $biblionumber,
212
        priority         => 1,
213
        itemnumber       => $item3->itemnumber,
214
    }
215
);
216
217
$account = Koha::Account->new({ patron_id => $borrowernumber });
218
219
( $status ) = CheckReserves($item3->id);
220
is( $status, 'Reserved', "Hold for the itemtype3 created" );
221
222
$start_balance = $account->balance();
223
224
Koha::Holds->find( $reserve_id )->cancel({ charge_cancel_fee => 1 });
225
226
( $status ) = CheckReserves($item3->id);
227
is( $status, '', "Hold for the itemtype3 cancelled" );
228
229
is( $account->balance() - $start_balance, 333, "Used ExpireReservesMaxPickUpDelayCharge preference as there's no circulation rules for itemtype3" );
230
231
# TEST: circulation rule for itemtype4 with library_B_code
232
t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 555);
233
234
$reserve_id = AddReserve(
235
    {
236
        branchcode       => $library_B_code,
237
        borrowernumber   => $borrowernumber,
238
        biblionumber     => $biblionumber2,
239
        priority         => 1,
240
        itemnumber       => $item4->itemnumber,
241
    }
242
);
243
244
$account = Koha::Account->new({ patron_id => $borrowernumber });
245
246
( $status ) = CheckReserves($item4->id);
247
is( $status, 'Reserved', "Hold for the itemtype4 created" );
248
249
$start_balance = $account->balance();
250
251
Koha::Holds->find( $reserve_id )->cancel({ charge_cancel_fee => 1 });
252
253
( $status ) = CheckReserves($item4->id);
254
is( $status, '', "Hold for the itemtype4 cancelled" );
255
256
is( $account->balance() - $start_balance, 444, "Used circulation rule for itemtype4 with library_B_code" );
257
258
$schema->storage->txn_rollback;
(-)a/t/db_dependent/Koha/Holds.t (-29 / +243 lines)
Lines 23-29 use Test::More tests => 6; Link Here
23
use Test::Warn;
23
use Test::Warn;
24
24
25
use C4::Circulation qw( AddIssue );
25
use C4::Circulation qw( AddIssue );
26
use C4::Reserves qw( AddReserve ModReserve ModReserveCancelAll );
26
use C4::Reserves qw( AddReserve CheckReserves ModReserve ModReserveCancelAll );
27
use Koha::AuthorisedValueCategory;
27
use Koha::AuthorisedValueCategory;
28
use Koha::Database;
28
use Koha::Database;
29
use Koha::Holds;
29
use Koha::Holds;
Lines 131-168 subtest 'cancel' => sub { Link Here
131
    is( $third_hold->discard_changes->priority, 2, 'Third hold should now be second' );
131
    is( $third_hold->discard_changes->priority, 2, 'Third hold should now be second' );
132
132
133
    subtest 'charge_cancel_fee parameter' => sub {
133
    subtest 'charge_cancel_fee parameter' => sub {
134
        plan tests => 4;
134
        plan tests => 15;
135
        my $patron_category = $builder->build_object({ class => 'Koha::Patron::Categories', value => { reservefee => 0 } } );
135
        my $library1 = $builder->build({
136
        my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { categorycode => $patron_category->categorycode } });
136
            source => 'Branch',
137
        is( $patron->account->balance, 0, 'A new patron does not have any charges' );
137
        });
138
        my $library2 = $builder->build({
139
            source => 'Branch',
140
        });
141
142
        my $bib_title = "Test Title";
143
144
        my $borrower = $builder->build({
145
            source => 'Borrower',
146
            value => {
147
                branchcode => $library1->{branchcode},
148
            }
149
        });
150
151
        my $itemtype1 = $builder->build({
152
            source => 'Itemtype',
153
            value => {}
154
        });
155
        my $itemtype2 = $builder->build({
156
            source => 'Itemtype',
157
            value => {}
158
        });
159
        my $itemtype3 = $builder->build({
160
            source => 'Itemtype',
161
            value => {}
162
        });
163
        my $itemtype4 = $builder->build({
164
            source => 'Itemtype',
165
            value => {}
166
        });
167
168
        my $borrowernumber = $borrower->{borrowernumber};
169
170
        my $library_A_code = $library1->{branchcode};
171
172
        my $biblio = $builder->build_sample_biblio({itemtype => $itemtype1->{itemtype}});
173
        my $biblionumber = $biblio->biblionumber;
174
        my $item1 = $builder->build_sample_item({
175
            biblionumber => $biblionumber,
176
            itype => $itemtype1->{itemtype},
177
            homebranch => $library_A_code,
178
            holdingbranch => $library_A_code
179
        });
180
        my $item2 = $builder->build_sample_item({
181
            biblionumber => $biblionumber,
182
            itype => $itemtype2->{itemtype},
183
            homebranch => $library_A_code,
184
            holdingbranch => $library_A_code
185
        });
186
        my $item3 = $builder->build_sample_item({
187
            biblionumber => $biblionumber,
188
            itype => $itemtype3->{itemtype},
189
            homebranch => $library_A_code,
190
            holdingbranch => $library_A_code
191
        });
192
193
        my $library_B_code = $library2->{branchcode};
194
195
        my $biblio2 = $builder->build_sample_biblio({itemtype => $itemtype4->{itemtype}});
196
        my $biblionumber2 = $biblio2->biblionumber;
197
        my $item4 = $builder->build_sample_item({
198
            biblionumber => $biblionumber2,
199
            itype => $itemtype4->{itemtype},
200
            homebranch => $library_B_code,
201
            holdingbranch => $library_B_code
202
        });
203
204
        Koha::CirculationRules->set_rules(
205
            {
206
                itemtype     => undef,
207
                categorycode => undef,
208
                branchcode   => undef,
209
                rules        => {
210
                    expire_reserves_charge => undef
211
                }
212
            }
213
        );
214
        Koha::CirculationRules->set_rules(
215
            {
216
                itemtype     => $itemtype1->{itemtype},
217
                categorycode => undef,
218
                branchcode   => undef,
219
                rules        => {
220
                    expire_reserves_charge => '111'
221
                }
222
            }
223
        );
224
        Koha::CirculationRules->set_rules(
225
            {
226
                itemtype     => $itemtype2->{itemtype},
227
                categorycode => undef,
228
                branchcode   => undef,
229
                rules        => {
230
                    expire_reserves_charge => undef
231
                }
232
            }
233
        );
234
        Koha::CirculationRules->set_rules(
235
            {
236
                itemtype     => undef,
237
                categorycode => undef,
238
                branchcode   => $library_B_code,
239
                rules        => {
240
                    expire_reserves_charge => '444'
241
                }
242
            }
243
        );
138
244
139
        my $hold_info = {
245
        t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
140
            branchcode     => $library->branchcode,
246
141
            borrowernumber => $patron->borrowernumber,
247
        my $reserve_id;
142
            biblionumber   => $item->biblionumber,
248
        my $account;
143
            priority       => 1,
249
        my $status;
144
            title          => "title for fee",
250
        my $start_balance;
145
            itemnumber     => $item->itemnumber,
251
146
        };
252
# TEST: Hold itemtype1 item
253
        $reserve_id = AddReserve(
254
            {
255
                branchcode       => $library_A_code,
256
                borrowernumber   => $borrowernumber,
257
                biblionumber     => $biblionumber,
258
                priority         => 1,
259
                itemnumber       => $item1->itemnumber,
260
            }
261
        );
262
263
        $account = Koha::Account->new({ patron_id => $borrowernumber });
264
265
        ( $status ) = CheckReserves($item1->id);
266
        is( $status, 'Reserved', "Hold for the itemtype1 created" );
267
268
        $start_balance = $account->balance();
269
270
        Koha::Holds->find( $reserve_id )->cancel({ charge_cancel_fee => 1 });
271
272
        ( $status ) = CheckReserves($item1->id);
273
        is( $status, '', "Hold for the itemtype1 cancelled" );
274
275
        is( $account->balance() - $start_balance, 111, "Used circulation rule for itemtype1" );
276
277
# TEST: circulation rule for itemtype2 has 'expire_reserves_charge' set undef, so it should use ExpireReservesMaxPickUpDelayCharge preference
278
        t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 222);
279
280
        $reserve_id = AddReserve(
281
            {
282
                branchcode       => $library_A_code,
283
                borrowernumber   => $borrowernumber,
284
                biblionumber     => $biblionumber,
285
                priority         => 1,
286
                itemnumber       => $item2->itemnumber,
287
            }
288
        );
289
290
        $account = Koha::Account->new({ patron_id => $borrowernumber });
291
292
        ( $status ) = CheckReserves($item2->id);
293
        is( $status, 'Reserved', "Hold for the itemtype2 created" );
294
295
        $start_balance = $account->balance();
296
297
        Koha::Holds->find( $reserve_id )->cancel({ charge_cancel_fee => 1 });
298
299
        ( $status ) = CheckReserves($item2->id);
300
        is( $status, '', "Hold for the itemtype2 cancelled" );
301
302
        is( $account->balance() - $start_balance, 222, "Used ExpireReservesMaxPickUpDelayCharge preference as expire_reserves_charge set to undef" );
303
304
# TEST: no circulation rules for itemtype3, it should use ExpireReservesMaxPickUpDelayCharge preference
305
        t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 333);
306
307
        $reserve_id = AddReserve(
308
            {
309
                branchcode       => $library_A_code,
310
                borrowernumber   => $borrowernumber,
311
                biblionumber     => $biblionumber,
312
                priority         => 1,
313
                itemnumber       => $item3->itemnumber,
314
            }
315
        );
316
317
        $account = Koha::Account->new({ patron_id => $borrowernumber });
318
319
        ( $status ) = CheckReserves($item3->id);
320
        is( $status, 'Reserved', "Hold for the itemtype3 created" );
321
322
        $start_balance = $account->balance();
323
324
        Koha::Holds->find( $reserve_id )->cancel({ charge_cancel_fee => 1 });
325
326
        ( $status ) = CheckReserves($item3->id);
327
        is( $status, '', "Hold for the itemtype3 cancelled" );
328
329
        is( $account->balance() - $start_balance, 333, "Used ExpireReservesMaxPickUpDelayCharge preference as there's no circulation rules for itemtype3" );
330
331
# TEST: circulation rule for itemtype4 with library_B_code
332
        t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 555);
333
334
        $reserve_id = AddReserve(
335
            {
336
                branchcode       => $library_B_code,
337
                borrowernumber   => $borrowernumber,
338
                biblionumber     => $biblionumber2,
339
                priority         => 1,
340
                itemnumber       => $item4->itemnumber,
341
            }
342
        );
343
344
        $account = Koha::Account->new({ patron_id => $borrowernumber });
345
346
        ( $status ) = CheckReserves($item4->id);
347
        is( $status, 'Reserved', "Hold for the itemtype4 created" );
348
349
        $start_balance = $account->balance();
350
351
        Koha::Holds->find( $reserve_id )->cancel({ charge_cancel_fee => 1 });
352
353
        ( $status ) = CheckReserves($item4->id);
354
        is( $status, '', "Hold for the itemtype4 cancelled" );
355
356
        is( $account->balance() - $start_balance, 444, "Used circulation rule for itemtype4 with library_B_code" );
357
358
        $reserve_id = AddReserve(
359
            {
360
                branchcode       => $library_B_code,
361
                borrowernumber   => $borrowernumber,
362
                biblionumber     => $biblionumber2,
363
                priority         => 1,
364
                itemnumber       => $item4->itemnumber,
365
            }
366
        );
367
368
        $account = Koha::Account->new({ patron_id => $borrowernumber });
369
370
        ( $status ) = CheckReserves($item4->id);
371
        is( $status, 'Reserved', "Hold for the itemtype4 created" );
147
372
148
        # First, test cancelling a reserve when there's no charge configured.
373
        $start_balance = $account->balance();
149
        t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 0);
150
        my $reserve_id = C4::Reserves::AddReserve( $hold_info );
151
        Koha::Holds->find( $reserve_id )->cancel( { charge_cancel_fee => 1 } );
152
        is( $patron->account->balance, 0, 'ExpireReservesMaxPickUpDelayCharge=0 - The patron should not have been charged' );
153
374
154
        # Then, test cancelling a reserve when there's no charge desired.
375
        Koha::Holds->find( $reserve_id )->cancel({ charge_cancel_fee => 0 });
155
        t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 42);
156
        $reserve_id = C4::Reserves::AddReserve( $hold_info );
157
        Koha::Holds->find( $reserve_id )->cancel(); # charge_cancel_fee => 0
158
        is( $patron->account->balance, 0, 'ExpireReservesMaxPickUpDelayCharge=42, but charge_cancel_fee => 0, The patron should not have been charged' );
159
376
377
        ( $status ) = CheckReserves($item4->id);
378
        is( $status, '', "Hold for the itemtype4 cancelled" );
160
379
161
        # Finally, test cancelling a reserve when there's a charge desired and configured.
380
        is( $account->balance() - $start_balance, 0, "Patron not charged when charge_cancel_fee is 0" );
162
        t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 42);
163
        $reserve_id = C4::Reserves::AddReserve( $hold_info );
164
        Koha::Holds->find( $reserve_id )->cancel( { charge_cancel_fee => 1 } );
165
        is( int($patron->account->balance), 42, 'ExpireReservesMaxPickUpDelayCharge=42 and charge_cancel_fee => 1, The patron should have been charged!' );
166
    };
381
    };
167
382
168
    subtest 'waiting hold' => sub {
383
    subtest 'waiting hold' => sub {
169
- 

Return to bug 25711