From d5868bfbd565c49e50418b4c2bb974d16ff7dc59 Mon Sep 17 00:00:00 2001 From: Thibaud Guillot Date: Tue, 21 Feb 2023 17:05:45 +0100 Subject: [PATCH] Bug 33028: Fix calculations around cronjob fines.pl When currency format is set on FR commas are decimals separators but when cron like fines.pl try to calculate fines it's fails due to this format. I changed this behavior by targetted 'fine' and 'overduefinescap' in circulation_rules.rule_name to unformat them when we save them. This also fix the display in smart_rules table (before with commas price was not good displayed - without decimals) Test Plan : 1) Set your currency format on 'FR' and 'fine' OR/AND 'overduefinescap' with commas 2) Be sure to have some patron overdues 3) Run ~/misc/cronjobs/fines.pl with args to find overdues 4) See an error like 'isn't numeric in substraction[..] or gt > [...]' 5) Run updatedatabase script (it will replace commas in your rules changed in step 1) ) 6) Repeat step 3 and see that everything was going "fine" (:tada:) Signed-off-by: Martin Renvoize Signed-off-by: Katrin Fischer --- Koha/CirculationRules.pm | 7 +++ ...ix_calc_fines_fr-currency_21-11-MT39815.pl | 51 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100755 installer/data/mysql/atomicupdate/fix_calc_fines_fr-currency_21-11-MT39815.pl diff --git a/Koha/CirculationRules.pm b/Koha/CirculationRules.pm index f96df496fb..d002937237 100644 --- a/Koha/CirculationRules.pm +++ b/Koha/CirculationRules.pm @@ -24,6 +24,7 @@ use Koha::Exceptions; use Koha::CirculationRule; use Koha::Caches; use Koha::Cache::Memory::Lite; +use Koha::Number::Price; use base qw(Koha::Objects); @@ -388,6 +389,12 @@ sub set_rule { } )->next(); + if ( $rule + && ( $rule->rule_name eq 'overduefinescap' || $rule->rule_name eq 'fine' ) ) + { + $rule_value = Koha::Number::Price->new($rule_value)->unformat; + } + if ($rule) { if ( defined $rule_value ) { $rule->rule_value($rule_value); diff --git a/installer/data/mysql/atomicupdate/fix_calc_fines_fr-currency_21-11-MT39815.pl b/installer/data/mysql/atomicupdate/fix_calc_fines_fr-currency_21-11-MT39815.pl new file mode 100755 index 0000000000..8502dc00a6 --- /dev/null +++ b/installer/data/mysql/atomicupdate/fix_calc_fines_fr-currency_21-11-MT39815.pl @@ -0,0 +1,51 @@ +use Modern::Perl; + +return { + bug_number => "BUG_33028", + description => +"Fix calculations around fines and values with comma as decimal separator", + up => sub { + my ($args) = @_; + my ( $dbh, $out ) = @$args{qw(dbh out)}; + + my $rules = $dbh->selectall_arrayref( +q|select * from circulation_rules where rule_name IN ('fine', 'overduefinescap')|, + { Slice => {} } + ); + + my $query = $dbh->prepare( + "UPDATE circulation_rules SET rule_value = ? where id = ?"); + + foreach my $rule ( @{$rules} ) { + my $rule_id = $rule->{'id'}; + my $rule_value = $rule->{'rule_value'}; + if ( $rule_value =~ /[a-zA-Z]/ ) { + die( + sprintf( + 'No only numbers in rule id %s ("%s") - fix it before restart this update', + $rule_id, $rule_value + ) + ); + } + else { + if ( $rule_value =~ /,/ ) { + if ( $rule_value !~ /,.*?,/ ) { + $rule_value =~ s/,/./; + $query->execute( $rule_value, $rule_id ); + } + else { + die( + sprintf( + 'Many commas in rule id %s ("%s") - fix it before restart this update', + $rule_id, $rule_value + ) + ); + } + } + } + + } + say $out + "BUG_33028 - Patch applied"; + }, + } -- 2.30.2