From 9e6dadcac61c1b8041c6db848a086f4167dc11a1 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Mon, 4 Feb 2019 14:29:08 +0000 Subject: [PATCH] Bug 21747: (follow-up) Intelligent rename of offsets This patch intelligently renames the account_offset types for updateing fines from `Fine Update` to `fine_increment` and `fine_decrement` depending on the sign of the calculated difference of the adjustment. Signed-off-by: Martin Renvoize --- C4/Overdues.pm | 5 +--- Koha/Account/Line.pm | 23 ++++++++----------- .../data/mysql/atomicupdate/bug_21747.perl | 21 +++++++++++++++++ 3 files changed, 32 insertions(+), 17 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/bug_21747.perl diff --git a/C4/Overdues.pm b/C4/Overdues.pm index 7f0394f720..de23dd906d 100644 --- a/C4/Overdues.pm +++ b/C4/Overdues.pm @@ -573,12 +573,9 @@ sub UpdateFine { } if ( $data ) { - # we're updating an existing fine. Only modify if amount changed - # Note that in the current implementation, you cannot pay against an accruing fine - # (i.e. , of accounttype 'FU'). Doing so will break accrual. if ( $data->{'amount'} != $amount ) { my $accountline = Koha::Account::Lines->find( $data->{accountlines_id} ); - $accountline->adjust({ amount => $amount, type => 'fine_increment' }); + $accountline->adjust({ amount => $amount, type => 'fine_update' }); } } else { if ( $amount ) { # Don't add new fines with an amount of 0 diff --git a/Koha/Account/Line.pm b/Koha/Account/Line.pm index df5d98b016..3bfc2012f0 100644 --- a/Koha/Account/Line.pm +++ b/Koha/Account/Line.pm @@ -220,7 +220,7 @@ This method allows updating a debit or credit on a patron's account ); $update_type can be any of: - - fine_increment + - fine_update Authors Note: The intention here is that this method is only used to adjust accountlines where the final amount is not yet known/fixed. @@ -236,7 +236,7 @@ sub adjust { my $amount = $params->{amount}; my $update_type = $params->{type}; - unless ( exists($Koha::Account::Line::offset_type->{$update_type}) ) { + unless ( exists($Koha::Account::Line::allowed_update->{$update_type}) ) { Koha::Exceptions::Account::UnrecognisedType->throw( error => 'Update type not recognised' ); @@ -259,6 +259,9 @@ sub adjust { my $difference = $amount - $amount_before; my $new_outstanding = $amount_outstanding_before + $difference; + my $offset_type = substr( $update_type, 0, index( $update_type, '_' ) ); + $offset_type .= ( $difference > 0 ) ? "_increment" : "_decrement"; + # Catch cases that require patron refunds if ( $new_outstanding < 0 ) { my $account = @@ -268,7 +271,7 @@ sub adjust { amount => $new_outstanding * -1, description => 'Overpayment refund', type => 'credit', - ( $update_type eq 'fine_increment' ? ( item_id => $self->itemnumber ) : ()), + ( $update_type eq 'fine_update' ? ( item_id => $self->itemnumber ) : ()), } ); $new_outstanding = 0; @@ -280,7 +283,7 @@ sub adjust { date => \'NOW()', amount => $amount, amountoutstanding => $new_outstanding, - ( $update_type eq 'fine_increment' ? ( lastincrement => $difference ) : ()), + ( $update_type eq 'fine_update' ? ( lastincrement => $difference ) : ()), } )->store(); @@ -288,7 +291,7 @@ sub adjust { my $account_offset = Koha::Account::Offset->new( { debit_id => $self->id, - type => $Koha::Account::Line::offset_type->{$update_type}, + type => $offset_type, amount => $difference } )->store(); @@ -310,7 +313,7 @@ sub adjust { manager_id => undef, } ) - ) if ( $update_type eq 'fine_increment' ); + ) if ( $update_type eq 'fine_update' ); } } ); @@ -358,17 +361,11 @@ sub _type { =head2 Name mappings -=head3 $offset_type - -=cut - -our $offset_type = { 'fine_increment' => 'Fine Update', }; - =head3 $allowed_update =cut -our $allowed_update = { 'fine_increment' => 'FU', }; +our $allowed_update = { 'fine_update' => 'FU', }; =head1 AUTHORS diff --git a/installer/data/mysql/atomicupdate/bug_21747.perl b/installer/data/mysql/atomicupdate/bug_21747.perl new file mode 100644 index 0000000000..1a92d5b5a5 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_21747.perl @@ -0,0 +1,21 @@ +$DBversion = 'XXX'; # will be replaced by the RM +if( CheckVersion( $DBversion ) ) { + $dbh->do(q{ + INSERT IGNORE INTO account_offset_types ( type ) VALUES ( 'fine_increment' ), ( 'fine_decrement' ); + }); + + $dbh->do(q{ + UPDATE account_offsets SET type = 'fine_increment' WHERE type = 'Fine Update' AND amount > 0; + }); + + $dbh->do(q{ + UPDATE account_offsets SET type = 'fine_decrement' WHERE type = 'Fine Update' AND amount < 0; + }); + + $dbh->do(q{ + DELETE FROM account_offset_types WHERE type = 'Fine Update'; + }); + + SetVersion( $DBversion ); + print "Upgrade to $DBversion done (Bug 21747 - Update account_offset_types to include 'fine_increment' and 'fine_decrement')\n"; +} -- 2.20.1