From 90e2bbb66750132d0c3a292912b04799aa8e59bb Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 1 Nov 2018 17:12:14 +0000 Subject: [PATCH] Bug 21748: Add ->adjust to Koha::Account::Line --- Koha/Account/Line.pm | 111 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/Koha/Account/Line.pm b/Koha/Account/Line.pm index 3573e604e6..c1ccdb681b 100644 --- a/Koha/Account/Line.pm +++ b/Koha/Account/Line.pm @@ -205,6 +205,95 @@ sub apply { return $available_credit; } +=head3 adjust + +This method allows updating a debit or credit on a patron's account + +my $account_line = Koha::Account->find({ accountlines_id => $line_id })->update( + { + amount => $amount, + type => $update_type, + } +); + +$update_type can be any of: + - fine_increment + +=cut + +sub adjust { + my ( $self, $params ) = @_; + + my $amount = $params->{amount}; + my $update_type = $params->{type}; + + unless ( exists($Koha::Account::Line::offset_type->{$update_type}) ) { + Koha::Exceptions::Account::UnrecognisedType->throw( + error => 'Update type not recognised' + ); + } + + my $account_type = $self->accounttype; + unless ( $Koha::Account::Line::allowed_update->{$update_type} eq $account_type ) { + Koha::Exceptions::Account::UnrecognisedType->throw( + error => 'Update type not allowed on this accounttype' + ); + } + + my $schema = Koha::Database->new->schema; + + $schema->txn_do( + sub { + + my $amount_before = $self->amount; + my $amount_outstanding_before = $self->amountoutstanding; + my $difference = $amount - $amount_before; + my $new_outstanding = $amount_outstanding_before + $difference; + + # Update the account line + $self->set( + { + date => \'NOW()', + amount => $amount, + amountoutstanding => $new_outstanding, + ( $update_type eq 'fine_increment' ? ( lastincrement => $difference ) : ()), + } + )->store(); + + # Record the account offset + my $account_offset = Koha::Account::Offset->new( + { + debit_id => $self->id, + type => $Koha::Account::Line::offset_type->{$update_type}, + amount => $difference + } + )->store(); + + if ( C4::Context->preference("FinesLog") ) { + logaction( + "FINES", 'UPDATE', #undef becomes UPDATE in UpdateFine + $self->borrowernumber, + Dumper( + { action => $update_type, + borrowernumber => $self->borrowernumber, + accountno => $self->accountno, + amount => $amount, + description => undef, + amountoutstanding => $new_outstanding, + accounttype => $self->accounttype, + note => undef, + itemnumber => $self->itemnumber, + manager_id => undef, + } + ) + ) if ( $update_type eq 'fine_increment' ); + } + } + ); + + return $self; +} + =head3 is_credit my $bool = $line->is_credit; @@ -242,3 +331,25 @@ sub _type { } 1; + +=head2 Name mappings + +=head3 $offset_type + +=cut + +our $offset_type = { 'fine_increment' => 'Fine Update', }; + +=head3 $allowed_update + +=cut + +our $allowed_update = { 'fine_increment' => 'FU', }; + +=head1 AUTHORS + +Kyle M Hall +Tomás Cohen Arazi +Martin Renvoize + +=cut -- 2.19.1