Bugzilla – Attachment 179582 Details for
Bug 39405
Add plugin hook `overwrite_calc_fine` to override fine calculation
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 39405: Add hook overwrite_calc_fine to override CalcFine in Overdues.pm
Bug-39405-Add-hook-overwritecalcfine-to-override-C.patch (text/plain), 5.91 KB, created by
Raphael Straub
on 2025-03-21 10:41:43 UTC
(
hide
)
Description:
Bug 39405: Add hook overwrite_calc_fine to override CalcFine in Overdues.pm
Filename:
MIME Type:
Creator:
Raphael Straub
Created:
2025-03-21 10:41:43 UTC
Size:
5.91 KB
patch
obsolete
>From 7b9611ed837f9eb47fcde61cfe0e4099a2e456cd Mon Sep 17 00:00:00 2001 >From: Raphael Straub <raphael.straub@kit.edu> >Date: Fri, 21 Mar 2025 10:35:00 +0000 >Subject: [PATCH] Bug 39405: Add hook overwrite_calc_fine to override CalcFine > in Overdues.pm > >This new hook allows to overwrite the koha standard calculation of fines. >This can be useful if your library has a fine policy with graduated fines >per overdue letter. > >To test: >1) Apply patch. >2) Run prove -v t/db_dependent/Circulation/CalcFine.t >3) Run prove -v t/db_dependent/Koha/Plugins/Overdues.t >4) Sign off. > >Sponsored-by: Karlsruhe Institute of Technology (KIT) >--- > C4/Overdues.pm | 16 ++++ > t/db_dependent/Koha/Plugins/Overdues.t | 111 +++++++++++++++++++++++++ > t/lib/plugins/Koha/Plugin/Test.pm | 10 +++ > 3 files changed, 137 insertions(+) > create mode 100755 t/db_dependent/Koha/Plugins/Overdues.t > >diff --git a/C4/Overdues.pm b/C4/Overdues.pm >index 4d6e50f27a..46becbcead 100644 >--- a/C4/Overdues.pm >+++ b/C4/Overdues.pm >@@ -222,6 +222,22 @@ sub CalcFine { > # Skip calculations if item is not overdue > return ( 0, 0, 0 ) unless ( DateTime->compare( $due_dt, $end_date ) == -1 ); > >+ # Call the plugin hook overwrite_calc_fine of all plugins and return >+ # the first defined fine. >+ my @fines = Koha::Plugins->call( >+ 'overwrite_calc_fine', >+ { >+ item => $item, >+ categorycode => $bortype, >+ branchcode => $branchcode, >+ due_date => $due_dt, >+ end_date => $end_date, >+ } >+ ); >+ foreach my $fine (@fines) { >+ return @$fine if ( defined $fine ); >+ } >+ > my $start_date = $due_dt->clone(); > > # get issuingrules (fines part will be used) >diff --git a/t/db_dependent/Koha/Plugins/Overdues.t b/t/db_dependent/Koha/Plugins/Overdues.t >new file mode 100755 >index 0000000000..74860518d6 >--- /dev/null >+++ b/t/db_dependent/Koha/Plugins/Overdues.t >@@ -0,0 +1,111 @@ >+#!/usr/bin/perl >+ >+# 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 Test::NoWarnings; >+use Test::More tests => 6; >+ >+use C4::Overdues qw(CalcFine); >+ >+use File::Basename; >+ >+use t::lib::Mocks; >+use t::lib::TestBuilder; >+ >+BEGIN { >+ # Mock pluginsdir before loading Plugins module >+ my $path = dirname(__FILE__) . '/../../../lib/plugins'; >+ t::lib::Mocks::mock_config( 'pluginsdir', $path ); >+ >+ use_ok('C4::Overdues'); >+ use_ok('Koha::Plugins'); >+ use_ok('Koha::Plugins::Handler'); >+ use_ok('Koha::Plugin::Test'); >+} >+ >+t::lib::Mocks::mock_config( 'enable_plugins', 1 ); >+ >+my $builder = t::lib::TestBuilder->new; >+ >+my $branch = $builder->build( >+ { >+ source => 'Branch', >+ } >+); >+ >+my $category = $builder->build( >+ { >+ source => 'Category', >+ } >+); >+ >+my $item = $builder->build_sample_item; >+ >+subtest 'overwrite_calc_fine hook tests' => sub { >+ plan tests => 6; >+ >+ my $schema = Koha::Database->new->schema; >+ $schema->storage->txn_begin; >+ >+ my $plugins = Koha::Plugins->new; >+ $plugins->InstallPlugins; >+ Koha::Plugin::Test->new->enable; >+ >+ Koha::CirculationRules->set_rules( >+ { >+ branchcode => undef, >+ categorycode => undef, >+ itemtype => undef, >+ rules => { >+ fine => '2.00', >+ lengthunit => 'days', >+ finedays => 0, >+ firstremind => 0, >+ chargeperiod => 1, >+ overduefinescap => '0', >+ cap_fine_to_replacement_price => 0, >+ } >+ }, >+ ); >+ >+ my $due_date = DateTime->new( >+ year => 2000, >+ month => 1, >+ day => 1, >+ ); >+ my $end_date = DateTime->new( >+ year => 2000, >+ month => 1, >+ day => 30, >+ ); >+ >+ my ( $amount, $units, $chargable_units ) = CalcFine( $item->unblessed, undef, undef, $due_date, $end_date ); >+ >+ is( $amount, 87, 'Amount is calculated correctly with custom function' ); >+ is( $units, 29, 'Units are calculated correctly with custom function' ); >+ is( $chargable_units, 27, 'Chargable units are calculated correctly with custom function' ); >+ >+ ( $amount, $units, $chargable_units ) = >+ CalcFine( $item->unblessed, $category->{categorycode}, $branch->{branchcode}, $due_date, $end_date ); >+ >+ is( $amount, 58, 'Amount is calculated correctly with original function' ); >+ is( $units, 29, 'Units are calculated correctly with original function' ); >+ is( $chargable_units, 29, 'Chargable units are calculated correctly with original function' ); >+ >+ Koha::Plugins->RemovePlugins; >+ $schema->storage->txn_rollback; >+}; >diff --git a/t/lib/plugins/Koha/Plugin/Test.pm b/t/lib/plugins/Koha/Plugin/Test.pm >index f2f10330d3..4d9904da86 100644 >--- a/t/lib/plugins/Koha/Plugin/Test.pm >+++ b/t/lib/plugins/Koha/Plugin/Test.pm >@@ -448,6 +448,16 @@ sub auth_client_get_user { > return; > } > >+sub overwrite_calc_fine { >+ my ( $self, $params ) = @_; >+ >+ my $days = $params->{end_date}->delta_days( $params->{due_date} )->delta_days; >+ >+ return $params->{categorycode} && $params->{branchcode} >+ ? undef >+ : [ 3 * $days, $days, $days - 2 ]; >+} >+ > sub _private_sub { > return ""; > } >-- >2.39.5
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 39405
:
179582
|
180095
|
180111
|
180127
|
180342
|
180343
|
180344
|
180433
|
180439
|
180440
|
180441
|
180442
|
180486
|
180505
|
180506
|
180507
|
180508