Bug 25711: Add ExpireReservesMaxPickUpDelayCharge to circulation rules

Add a feature that allows to define ExpireReservesMaxPickUpDelayCharge
in circulation rules per specific items/patron categories.
When value not found in circulation rules it uses global
ExpireReservesMaxPickUpDelayCharge, as it was before this smart-rules
feature was introduced.

To check the feature after applying the patch:
    1) Go to /cgi-bin/koha/admin/preferences.pl and set
ExpireReservesMaxPickUpDelay to allow.
    1) Go to /cgi-bin/koha/admin/smart-rules.pl circulation rules table
and notice the newly added "Expire reserve charge" row.
    2) In that row add numerical values to type/patron categories
that you will be using later to test this feature.
    3) Prepare an item for our next step, pick a biblio that has item
with the same item type you altered in smart rules and check it out.
    3) Place a hold on that item and set the expiration date as the
next day.
    4) Check in it after that.
    5) Do SQL request to change expiration date manually to "yesterday":
in "reserves" table, find the id of the hold that you placed and
"UPDATE reserves SET `expirationdate`='2020-07-19' WHERE
`reserve_id`=329;"
 Or alternatively:
    5) Wait for tomorrow, change server time or invent a time machine.
    6) Run cancel_expired_holds.pl cron job.
    7) Check that patron got charged in "accounting" left tab and
"transactions" upper tab: your patron should have debt of that amount
of fee you assigned in that rule
(/cgi-bin/koha/members/boraccount.pl?borrowernumber=...YourPatronID...)
    8) You can repeat this with other rules and different
numbers/biblio/item types to check that different fees apply according
to smart rules.
    9) If there's no matching smart rule or smart rule have an empty
string as a fee - global ExpireReservesMaxPickUpDelayCharge variable
value will be used.
   10) But in case if matching rule has zero ("0") as a value - that
allows to have no-fee smart-rule results, as a feature.

IMPORTANT:
Smart-rules filters only applied to item-level holds when item-type
for items is known. If there's biblio-level hold, in that case global
ExpireReservesMaxPickUpDelayCharge value will be used).

Mentored-by: Andrew Nugged <nugged@gmail.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
This commit is contained in:
Petro Vashchuk 2024-09-05 14:11:25 +03:00 committed by Katrin Fischer
parent e72ee54eb5
commit 6f28ffbcc5
No known key found for this signature in database
GPG Key ID: 0EF6E2C03357A834
5 changed files with 296 additions and 3 deletions

View File

@ -96,6 +96,9 @@ our $RULE_KINDS = {
cap_fine_to_replacement_price => {
scope => [ 'branchcode', 'categorycode', 'itemtype' ],
},
expire_reserves_charge => {
scope => [ 'branchcode', 'categorycode', 'itemtype' ],
},
chargeperiod => {
scope => [ 'branchcode', 'categorycode', 'itemtype' ],
},

View File

@ -785,7 +785,24 @@ sub cancel {
C4::Reserves::_FixPriority({ biblionumber => $self->biblionumber });
# and, if desired, charge a cancel fee
my $charge = C4::Context->preference("ExpireReservesMaxPickUpDelayCharge");
my $charge;
if (my $item = $self->item) {
my $branchcode = C4::Reserves::GetReservesControlBranch($item->unblessed, $self->borrower->unblessed);
my $rule = Koha::CirculationRules->get_effective_rule(
{
categorycode => $self->borrower->categorycode,
itemtype => $item->effective_itemtype,
branchcode => $branchcode,
rule_name => 'expire_reserves_charge',
}
);
my $rule_value = $rule && $rule->rule_value // '';
$charge = $rule_value ne '' ? $rule_value : C4::Context->preference("ExpireReservesMaxPickUpDelayCharge");
} else {
$charge = C4::Context->preference("ExpireReservesMaxPickUpDelayCharge");
}
if ( $charge && $params->{'charge_cancel_fee'} ) {
my $account =
Koha::Account->new( { patron_id => $self->borrowernumber } );

View File

@ -107,6 +107,7 @@ if ($op eq 'cud-delete') {
opacitemholds => undef,
overduefinescap => undef,
cap_fine_to_replacement_price => undef,
expire_reserves_charge => undef,
article_requests => undef,
note => undef,
recalls_allowed => undef,
@ -305,6 +306,7 @@ elsif ( $op eq 'cud-add' ) {
my $overduefinescap = $input->param('overduefinescap')
&& ( $input->param('overduefinescap') + 0 ) > 0 ? sprintf( "%.02f", $input->param('overduefinescap') ) : q{};
my $cap_fine_to_replacement_price = ( $input->param('cap_fine_to_replacement_price') || q{} ) eq 'on';
my $expire_reserves_charge = $input->param('expire_reserves_charge') // q{};
my $note = $input->param('note');
my $decreaseloanholds = $input->param('decreaseloanholds') || q{};
my $recalls_allowed = $input->param('recalls_allowed');
@ -346,6 +348,7 @@ elsif ( $op eq 'cud-add' ) {
opacitemholds => $opacitemholds,
overduefinescap => $overduefinescap,
cap_fine_to_replacement_price => $cap_fine_to_replacement_price,
expire_reserves_charge => $expire_reserves_charge,
article_requests => $article_requests,
note => $note,
decreaseloanholds => $decreaseloanholds,

View File

@ -124,6 +124,7 @@
<th>Fine/suspension grace period</th>
<th>Overdue fines cap (amount)</th>
<th>Cap fine at replacement price</th>
<th>Expire reserve charge</th>
<th>Suspension in days (day)</th>
<th>Max. suspension duration (day)</th>
<th>Suspension charging interval</th>
@ -178,6 +179,7 @@
[% SET firstremind = all_rules.$c.$i.firstremind %]
[% SET overduefinescap = all_rules.$c.$i.overduefinescap %]
[% SET cap_fine_to_replacement_price = all_rules.$c.$i.cap_fine_to_replacement_price %]
[% SET expire_reserves_charge = all_rules.$c.$i.expire_reserves_charge %]
[% SET finedays = all_rules.$c.$i.finedays %]
[% SET maxsuspensiondays = all_rules.$c.$i.maxsuspensiondays %]
[% SET suspension_chargeperiod = all_rules.$c.$i.suspension_chargeperiod %]
@ -205,7 +207,7 @@
[% SET recall_shelf_time = all_rules.$c.$i.recall_shelf_time %]
[% SET holds_pickup_period = all_rules.$c.$i.holds_pickup_period %]
[% SET show_rule = note || maxissueqty || maxonsiteissueqty || issuelength || daysmode || lengthunit || hardduedate || hardduedatecompare || fine || chargeperiod || chargeperiod_charge_at || firstremind || overduefinescap || cap_fine_to_replacement_price || finedays || maxsuspensiondays || suspension_chargeperiod || renewalsallowed || unseenrenewalsallowed || renewalperiod || norenewalbefore || noautorenewalbefore || auto_renew || no_auto_renewal_after || no_auto_renewal_after_hard_limit || reservesallowed || holds_per_day || holds_per_record || onshelfholds || opacitemholds || article_requests || rentaldiscount || decreaseloanholds || recalls_allowed || recalls_per_record || on_shelf_recalls || recall_due_date_interval || recall_overdue_fine || recall_shelf_time || holds_pickup_period %]
[% SET show_rule = note || maxissueqty || maxonsiteissueqty || issuelength || daysmode || lengthunit || hardduedate || hardduedatecompare || fine || chargeperiod || chargeperiod_charge_at || firstremind || overduefinescap || cap_fine_to_replacement_price || expire_reserves_charge || finedays || maxsuspensiondays || suspension_chargeperiod || renewalsallowed || unseenrenewalsallowed || renewalperiod || norenewalbefore || noautorenewalbefore || auto_renew || no_auto_renewal_after || no_auto_renewal_after_hard_limit || reservesallowed || holds_per_day || holds_per_record || onshelfholds || opacitemholds || article_requests || rentaldiscount || decreaseloanholds || recalls_allowed || recalls_per_record || on_shelf_recalls || recall_due_date_interval || recall_overdue_fine || recall_shelf_time || holds_pickup_period %]
[% IF show_rule %]
[% SET row_count = row_count + 1 %]
<tr row_countd="row_[% row_count | html %]">
@ -307,6 +309,13 @@
<input type="checkbox" disabled="disabled" />
[% END %]
</td>
<td>
[% IF expire_reserves_charge.defined && expire_reserves_charge != '' %]
[% expire_reserves_charge | html %]
[% ELSE %]
<a href="/cgi-bin/koha/admin/preferences.pl?op=search&amp;searchfield=ExpireReservesMaxPickUpDelayCharge">Use global</a>
[% END %]
</td>
<td>[% finedays | html %]</td>
<td>[% maxsuspensiondays | html %]</td>
<td>[% suspension_chargeperiod | html %]</td>
@ -489,6 +498,7 @@
<td><input type="text" name="firstremind" id="firstremind" size="2" /> </td>
<td><input type="text" name="overduefinescap" id="overduefinescap" size="6" inputmode="decimal" pattern="^\d+(\.\d{2})?$" /> </td>
<td><input type="checkbox" name="cap_fine_to_replacement_price" id="cap_fine_to_replacement_price" /></td>
<td><input type="text" name="expire_reserves_charge" id="expire_reserves_charge" size="6"/></td>
<td><input type="text" name="finedays" id="fined" size="3" /> </td>
<td><input type="text" name="maxsuspensiondays" id="maxsuspensiondays" size="3" /> </td>
<td><input type="text" name="suspension_chargeperiod" id="suspension_chargeperiod" size="3" /> </td>
@ -580,6 +590,7 @@
<th>Fine/suspension grace period</th>
<th>Overdue fines cap (amount)</th>
<th>Cap fine at replacement price</th>
<th>Expire reserve charge</th>
<th>Suspension in days (day)</th>
<th>Max. suspension duration (day)</th>
<th>Suspension charging interval</th>
@ -1597,10 +1608,11 @@
// Remove potential previous input added
$(current_column).find("input").remove();
$(current_column).append("<input type='hidden' name='"+name+"' value='"+val+"' />");
} else if ( current_input_id === "maxissueqty" || current_input_id === "maxonsiteissueqty" || current_input_id === "reservesallowed" || current_input_id === "holds_per_day" || current_input_id === "holds_per_record" || current_input_id === "holds_pickup_period" ) {
} else if ( current_input_id === "maxissueqty" || current_input_id === "maxonsiteissueqty" || current_input_id === "reservesallowed" || current_input_id === "expire_reserves_charge" || current_input_id === "holds_per_day" || current_input_id === "holds_per_record" || current_input_id === "holds_pickup_period" ) {
// If the value is not an integer for
// - "Current checkouts allowed"
// - "Current on-site checkouts allowed"
// - "Expire reserve charge"
// - "Holds allowed (total)"
// - "Holds allowed (daily)"
// - "Holds per record (count)"

258
t/db_dependent/Holds/Holdfine.t Executable file
View File

@ -0,0 +1,258 @@
#!/usr/bin/perl
# Copyright The National Library of Finland, University of Helsinki 2020
# Copyright Petro Vashchuk <stalkernoid@gmail.com> 2020
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use C4::Context;
use Koha::CirculationRules;
use Test::More tests => 13;
use t::lib::TestBuilder;
use t::lib::Mocks;
use Koha::Holds;
use Koha::Account;
use Koha::Account::DebitTypes;
BEGIN {
use_ok('C4::Reserves');
}
my $schema = Koha::Database->schema;
$schema->storage->txn_begin;
my $dbh = C4::Context->dbh;
my $builder = t::lib::TestBuilder->new;
my $library1 = $builder->build({
source => 'Branch',
});
my $library2 = $builder->build({
source => 'Branch',
});
my $bib_title = "Test Title";
my $borrower = $builder->build({
source => 'Borrower',
value => {
branchcode => $library1->{branchcode},
}
});
my $itemtype1 = $builder->build({
source => 'Itemtype',
value => {}
});
my $itemtype2 = $builder->build({
source => 'Itemtype',
value => {}
});
my $itemtype3 = $builder->build({
source => 'Itemtype',
value => {}
});
my $itemtype4 = $builder->build({
source => 'Itemtype',
value => {}
});
my $borrowernumber = $borrower->{borrowernumber};
my $library_A_code = $library1->{branchcode};
my $biblio = $builder->build_sample_biblio({itemtype => $itemtype1->{itemtype}});
my $biblionumber = $biblio->biblionumber;
my $item1 = $builder->build_sample_item({
biblionumber => $biblionumber,
itype => $itemtype1->{itemtype},
homebranch => $library_A_code,
holdingbranch => $library_A_code
});
my $item2 = $builder->build_sample_item({
biblionumber => $biblionumber,
itype => $itemtype2->{itemtype},
homebranch => $library_A_code,
holdingbranch => $library_A_code
});
my $item3 = $builder->build_sample_item({
biblionumber => $biblionumber,
itype => $itemtype3->{itemtype},
homebranch => $library_A_code,
holdingbranch => $library_A_code
});
my $library_B_code = $library2->{branchcode};
my $biblio2 = $builder->build_sample_biblio({itemtype => $itemtype4->{itemtype}});
my $biblionumber2 = $biblio2->biblionumber;
my $item4 = $builder->build_sample_item({
biblionumber => $biblionumber2,
itype => $itemtype4->{itemtype},
homebranch => $library_B_code,
holdingbranch => $library_B_code
});
$dbh->do("DELETE FROM circulation_rules");
Koha::CirculationRules->set_rules(
{
itemtype => $itemtype1->{itemtype},
categorycode => undef,
branchcode => undef,
rules => {
expire_reserves_charge => '111'
}
}
);
Koha::CirculationRules->set_rules(
{
itemtype => $itemtype2->{itemtype},
categorycode => undef,
branchcode => undef,
rules => {
expire_reserves_charge => undef
}
}
);
Koha::CirculationRules->set_rules(
{
itemtype => undef,
categorycode => undef,
branchcode => $library_B_code,
rules => {
expire_reserves_charge => '444'
}
}
);
t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
my $reserve_id;
my $account;
my $status;
my $start_balance;
# TEST: Hold itemtype1 item
$reserve_id = AddReserve(
{
branchcode => $library_A_code,
borrowernumber => $borrowernumber,
biblionumber => $biblionumber,
priority => 1,
itemnumber => $item1->itemnumber,
}
);
$account = Koha::Account->new({ patron_id => $borrowernumber });
( $status ) = CheckReserves($item1->id);
is( $status, 'Reserved', "Hold for the itemtype1 created" );
$start_balance = $account->balance();
Koha::Holds->find( $reserve_id )->cancel({ charge_cancel_fee => 1 });
( $status ) = CheckReserves($item1->id);
is( $status, '', "Hold for the itemtype1 cancelled" );
is( $account->balance() - $start_balance, 111, "Used circulation rule for itemtype1" );
# TEST: circulation rule for itemtype2 has 'expire_reserves_charge' set undef, so it should use ExpireReservesMaxPickUpDelayCharge preference
t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 222);
$reserve_id = AddReserve(
{
branchcode => $library_A_code,
borrowernumber => $borrowernumber,
biblionumber => $biblionumber,
priority => 1,
itemnumber => $item2->itemnumber,
}
);
$account = Koha::Account->new({ patron_id => $borrowernumber });
( $status ) = CheckReserves($item2->id);
is( $status, 'Reserved', "Hold for the itemtype2 created" );
$start_balance = $account->balance();
Koha::Holds->find( $reserve_id )->cancel({ charge_cancel_fee => 1 });
( $status ) = CheckReserves($item2->id);
is( $status, '', "Hold for the itemtype2 cancelled" );
is( $account->balance() - $start_balance, 222, "Used ExpireReservesMaxPickUpDelayCharge preference as expire_reserves_charge set to undef" );
# TEST: no circulation rules for itemtype3, it should use ExpireReservesMaxPickUpDelayCharge preference
t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 333);
$reserve_id = AddReserve(
{
branchcode => $library_A_code,
borrowernumber => $borrowernumber,
biblionumber => $biblionumber,
priority => 1,
itemnumber => $item3->itemnumber,
}
);
$account = Koha::Account->new({ patron_id => $borrowernumber });
( $status ) = CheckReserves($item3->id);
is( $status, 'Reserved', "Hold for the itemtype3 created" );
$start_balance = $account->balance();
Koha::Holds->find( $reserve_id )->cancel({ charge_cancel_fee => 1 });
( $status ) = CheckReserves($item3->id);
is( $status, '', "Hold for the itemtype3 cancelled" );
is( $account->balance() - $start_balance, 333, "Used ExpireReservesMaxPickUpDelayCharge preference as there's no circulation rules for itemtype3" );
# TEST: circulation rule for itemtype4 with library_B_code
t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 555);
$reserve_id = AddReserve(
{
branchcode => $library_B_code,
borrowernumber => $borrowernumber,
biblionumber => $biblionumber2,
priority => 1,
itemnumber => $item4->itemnumber,
}
);
$account = Koha::Account->new({ patron_id => $borrowernumber });
( $status ) = CheckReserves($item4->id);
is( $status, 'Reserved', "Hold for the itemtype4 created" );
$start_balance = $account->balance();
Koha::Holds->find( $reserve_id )->cancel({ charge_cancel_fee => 1 });
( $status ) = CheckReserves($item4->id);
is( $status, '', "Hold for the itemtype4 cancelled" );
is( $account->balance() - $start_balance, 444, "Used circulation rule for itemtype4 with library_B_code" );
$schema->storage->txn_rollback;