From 73e34907586bc8b66495d9439804d1e7bad694f0 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Fri, 29 Jan 2021 08:47:37 +0000 Subject: [PATCH] Bug 27574: Update \NOW() to dt_from_string for Accounts This patch replaces all instances of setting the date/timestamp fields for accounts related tables from the SQL literal \'NOW()' to Koha::DateUtils dt_from_string so we get properly localised dates. We also update the unit tests --- Koha/Account.pm | 4 ++-- Koha/Account/Line.pm | 25 ++++++++++++++++--------- t/db_dependent/Koha/Account/Line.t | 24 +++++++++++++++++++++--- 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/Koha/Account.pm b/Koha/Account.pm index b4838bcdf2..4a79adf9e7 100644 --- a/Koha/Account.pm +++ b/Koha/Account.pm @@ -413,7 +413,7 @@ sub add_credit { $line = Koha::Account::Line->new( { borrowernumber => $self->{patron_id}, - date => \'NOW()', + date => dt_from_string, amount => $amount, description => $description, credit_type_code => $credit_type, @@ -560,7 +560,7 @@ sub add_debit { $line = Koha::Account::Line->new( { borrowernumber => $self->{patron_id}, - date => \'NOW()', + date => dt_from_string, amount => $amount, description => $description, debit_type_code => $debit_type, diff --git a/Koha/Account/Line.pm b/Koha/Account/Line.pm index 2cc81993ef..209c199612 100644 --- a/Koha/Account/Line.pm +++ b/Koha/Account/Line.pm @@ -247,6 +247,7 @@ sub void { debit_id => $fee_paid->id, amount => $amount_paid, type => 'Void Payment', + created_on => dt_from_string } )->store(); } @@ -341,7 +342,7 @@ sub cancel { # A 'cancellation' is a 'credit' $cancellation = Koha::Account::Line->new( { - date => \'NOW()', + date => dt_from_string, amount => 0 - $self->amount, credit_type_code => 'CANCELLATION', status => 'ADDED', @@ -357,7 +358,8 @@ sub cancel { { credit_id => $cancellation->accountlines_id, type => 'CANCELLATION', - amount => $self->amount + amount => $self->amount, + created_on => dt_from_string } )->store(); @@ -465,7 +467,7 @@ sub reduce { # A 'reduction' is a 'credit' $reduction = Koha::Account::Line->new( { - date => \'NOW()', + date => dt_from_string, amount => 0 - $params->{amount}, credit_type_code => $params->{reduction_type}, status => 'ADDED', @@ -481,7 +483,8 @@ sub reduce { { credit_id => $reduction->accountlines_id, type => uc( $params->{reduction_type} ), - amount => $params->{amount} + amount => $params->{amount}, + created_on => dt_from_string } )->store(); @@ -506,7 +509,8 @@ sub reduce { credit_id => $reduction->accountlines_id, debit_id => $self->accountlines_id, type => uc( $params->{reduction_type} ), - amount => 0 + amount => 0, + created_on => dt_from_string } )->store(); } @@ -586,6 +590,7 @@ sub apply { debit_id => $debit->id, amount => $amount_to_cancel * -1, type => $offset_type, + created_on => dt_from_string } )->store(); @@ -687,7 +692,7 @@ sub payout { # A 'payout' is a 'debit' $payout = Koha::Account::Line->new( { - date => \'NOW()', + date => dt_from_string, amount => $amount, debit_type_code => 'PAYOUT', payment_type => $params->{payout_type}, @@ -704,7 +709,8 @@ sub payout { { debit_id => $payout->accountlines_id, type => 'PAYOUT', - amount => $amount + amount => $amount, + created_on => dt_from_string } )->store(); @@ -801,7 +807,7 @@ sub adjust { # Update the account line $self->set( { - date => \'NOW()', + date => dt_from_string, amount => $amount, amountoutstanding => $new_outstanding, } @@ -812,7 +818,8 @@ sub adjust { { debit_id => $self->id, type => $offset_type, - amount => $difference + amount => $difference, + created_on => dt_from_string } )->store(); diff --git a/t/db_dependent/Koha/Account/Line.t b/t/db_dependent/Koha/Account/Line.t index 4a18526dac..31d1810f02 100755 --- a/t/db_dependent/Koha/Account/Line.t +++ b/t/db_dependent/Koha/Account/Line.t @@ -22,6 +22,7 @@ use Modern::Perl; use Test::More tests => 14; use Test::Exception; use Test::MockModule; +use Time::Fake; use DateTime; @@ -466,6 +467,8 @@ subtest 'adjust() tests' => sub { plan tests => 29; $schema->storage->txn_begin; + my $faketime = DateTime->now->subtract( hours => 4 ); + Time::Fake->offset($faketime->epoch); # count logs before any actions my $action_logs = $schema->resultset('ActionLog')->search()->count; @@ -510,13 +513,14 @@ subtest 'adjust() tests' => sub { is( $debit_2->amount * 1, 150, 'Fine amount was updated in full' ); is( $debit_2->amountoutstanding * 1, 150, 'Fine amountoutstanding was update in full' ); - isnt( $debit_2->date, undef, 'Date has been set' ); + is( $debit_2->date, $faketime->strftime('%F %T'), "Instance time was used to set date"); my $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } ); is( $offsets->count, 1, 'An offset is generated for the increment' ); my $THIS_offset = $offsets->next; is( $THIS_offset->amount * 1, 50, 'Amount was calculated correctly (increment by 50)' ); is( $THIS_offset->type, 'OVERDUE_INCREASE', 'Adjust type stored correctly' ); + is( $THIS_offset->created_on, $faketime->strftime('%F %T'), "Instance time was used to set created_on date"); is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No log was added' ); @@ -777,6 +781,8 @@ subtest "payout() tests" => sub { plan tests => 18; $schema->storage->txn_begin; + my $faketime = DateTime->now->subtract( hours => 4 ); + Time::Fake->offset($faketime->epoch); # Create a borrower my $categorycode = @@ -906,15 +912,19 @@ subtest "payout() tests" => sub { is( $credit1->amountoutstanding + 0, -10, 'Credit has an new amount outstanding of -10' ); is( $credit1->status(), 'PAID', "Credit has a new status of PAID" ); + is( $payout->date, $faketime->strftime('%F %T'), "Instance time was used to set payout date"); + Time::Fake->reset; $schema->storage->txn_rollback; }; subtest "reduce() tests" => sub { - plan tests => 29; + plan tests => 31; $schema->storage->txn_begin; + my $faketime = DateTime->now->subtract( hours => 4 ); + Time::Fake->offset($faketime->epoch); # Create a borrower my $categorycode = @@ -1033,6 +1043,7 @@ subtest "reduce() tests" => sub { is( $debit1->status(), 'DISCOUNTED', "Debit status updated to DISCOUNTED"); is( $account->balance() * 1, -5, "Account balance is -5" ); is( $reduction->status(), 'APPLIED', "Reduction status is 'APPLIED'" ); + is( $reduction->date(), $faketime->strftime('%F %T'), "Instance time was used to set reduction date"); my $offsets = Koha::Account::Offsets->search( { credit_id => $reduction->id, debit_id => $debit1->id } ); @@ -1041,6 +1052,7 @@ subtest "reduce() tests" => sub { is( $THE_offset->amount * 1, -5, 'Correct amount was applied against debit' ); is( $THE_offset->type, 'DISCOUNT', "Offset type set to 'DISCOUNT'" ); + is( $THE_offset->created_on, $faketime->strftime('%F %T'), "Instance time was used to set created_on date"); # Zero offset created when zero outstanding # (Refund another 5 on paid debt of 20) @@ -1087,13 +1099,16 @@ subtest "reduce() tests" => sub { 'Koha::Exceptions::Account::IsNotDebit', '->reduce() cannot be used on a payout debit'; + Time::Fake->reset; $schema->storage->txn_rollback; }; subtest "cancel() tests" => sub { - plan tests => 16; + plan tests => 18; $schema->storage->txn_begin; + my $faketime = DateTime->now->subtract( hours => 4 ); + Time::Fake->offset($faketime->epoch); my $library = $builder->build_object( { class => 'Koha::Libraries' }); my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library->branchcode } }); @@ -1175,6 +1190,7 @@ subtest "cancel() tests" => sub { 0, "Debit amountoutstanding reduced to 0" ); is( $debit1->status(), 'CANCELLED', "Debit status updated to CANCELLED" ); is( $account->balance() * 1, 15, "Account balance is 15" ); + is( $cancellation->date, $faketime->strftime('%F %T'), "Instance time was used to set cancellation date"); my $offsets = Koha::Account::Offsets->search( { credit_id => $cancellation->id, debit_id => $debit1->id } ); @@ -1184,7 +1200,9 @@ subtest "cancel() tests" => sub { -10, 'Correct amount was applied against debit' ); is( $THE_offset->type, 'CANCELLATION', "Offset type set to 'CANCELLATION'" ); + is( $THE_offset->created_on, $faketime->strftime('%F %T'), "Instance time was used to set created_on date"); + Time::Fake->reset; $schema->storage->txn_rollback; }; -- 2.20.1