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" (🎉)

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Thibaud Guillot 2023-02-21 17:05:45 +01:00 committed by Tomas Cohen Arazi
parent 0397251ece
commit 1f9e161b41
Signed by: tomascohen
GPG key ID: 0A272EA1B2F3C15F
2 changed files with 58 additions and 0 deletions

View file

@ -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);

View file

@ -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";
},
}