Bugzilla – Attachment 122621 Details for
Bug 28656
Cancellations should be allowed on paid/partially paid debts
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 28656: Improve the transaction history
Bug-28656-Improve-the-transaction-history.patch (text/plain), 9.79 KB, created by
Martin Renvoize (ashimema)
on 2021-07-06 13:11:23 UTC
(
hide
)
Description:
Bug 28656: Improve the transaction history
Filename:
MIME Type:
Creator:
Martin Renvoize (ashimema)
Created:
2021-07-06 13:11:23 UTC
Size:
9.79 KB
patch
obsolete
>From b74a2f94c33d3ed6242de31a9c8d192ee55135d3 Mon Sep 17 00:00:00 2001 >From: Martin Renvoize <martin.renvoize@ptfs-europe.com> >Date: Mon, 5 Jul 2021 13:46:13 +0100 >Subject: [PATCH] Bug 28656: Improve the transaction history > >This patch improves the transaction history for a cancellation action. > >Test plan >1/ Set up a complex transaction that takes in various actions against an >accountline; for example >1a/ Add a manual debt for 25.00 >1b/ Discount that debt by 5.00 >1c/ Writeoff 2.50 from that debt >1d/ Writeoff 7.50 from that debt >1e/ Void the 7.40 Writeoff >1f/ Pay 3.50 of that debt >2/ Cancel the debt >3/ Looks at the 'Details' view for the debt >3a/ Confirm that all the original actions appear followed by the >cancellation amount of the original debt amount minus any reductions >(discounts, writeoffs) >3b/ After the cancellation line you should see some reversals for >non-reduction type offsets (i.e. payments) >--- > Koha/Account/Line.pm | 120 +++++++++++++++++++++++++++------------- > Koha/Account/Offsets.pm | 43 ++++++++++++++ > 2 files changed, 126 insertions(+), 37 deletions(-) > >diff --git a/Koha/Account/Line.pm b/Koha/Account/Line.pm >index 34bab520fc..23d48eb1cd 100644 >--- a/Koha/Account/Line.pm >+++ b/Koha/Account/Line.pm >@@ -172,7 +172,7 @@ sub debit_offsets { > > Return the credits linked to this account line if some exist. > Search conditions and attributes may be passed if you wish to filter >-the resultant resultant resultset. >+the resultant resultset. > > =cut > >@@ -200,7 +200,7 @@ sub credits { > > Return the debits linked to this account line if some exist. > Search conditions and attributes may be passed if you wish to filter >-the resultant resultant resultset. >+the resultant resultset. > > =cut > >@@ -362,13 +362,13 @@ sub void { > > =head3 cancel > >- $debit_accountline->cancel({ cancellation_type => $type }); >+ $debit_accountline->cancel(); > > Cancel a charge. It will mark the debit as 'cancelled' by updating its > status to 'CANCELLED'. > >-Charges that have been fully or partially paid will be refunded upto >-the amount paid excluding discounts, writeoffs and refunds. >+Charges that have been fully or partially offset will be refunded upto >+the amount paid excluding discounts, writeoffs and prior refunds. > > Returns the cancellation accountline. > >@@ -393,22 +393,18 @@ sub cancel { > error => 'Account line ' . $self->id . 'is already cancelled' ); > } > >- my $cancellation_type = $params->{cancellation_type} // 'CANCELLATION'; >- >- my $cancellation_amount = $self->amountoutstanding; >+ my $cancellation_amount = $self->amount; >+ my @applied_offsets; > if ( $self->amount != $self->amountoutstanding ) { >- my $credit_offsets = Koha::Account::Offsets->search( >- { >- debit_id => $self->id, >- credit_id => { '!=' => undef }, # not the debt itself >- type => { '!=' => [ 'Writeoff', 'REFUND', 'DISCOUNT' ] } >- , # Don't refund writeoffs, refunds or discounts >- amount => { '<' => 0 } # credits are negative on the DB >- } >- ); >- >+ # Reductions are 'Writeoff', 'Refund' or 'Discount' lines applied against the debt already. >+ # We can ignore 'VOID' status as it will be accounted for in the offsets total >+ my $reduction_offsets = $self->debit_offsets->filter_by_non_reversable(); > $cancellation_amount += >- $credit_offsets->count > 0 ? $credit_offsets->total * -1 : 0; >+ $reduction_offsets->count > 0 ? $reduction_offsets->total : 0; >+ >+ # Payments are any credits applied (and not subsequently voided) that are >+ # not 'Writeoff', 'Refund' or 'Discount' >+ @applied_offsets = $self->debit_offsets->filter_by_reversable(); > } > > my $cancellation; >@@ -416,15 +412,53 @@ sub cancel { > sub { > > # A 'cancellation' is a 'credit' >- $cancellation = $self->reduce( >+ $cancellation = Koha::Account::Line->new( > { >- reduction_type => $cancellation_type, >- amount => $cancellation_amount, >- staff_id => $params->{staff_id}, >- interface => $params->{interface}, >- branch => $params->{branch} >+ date => \'NOW()', >+ amount => 0 - $cancellation_amount, >+ credit_type_code => 'CANCELLATION', >+ status => 'ADDED', >+ amountoutstanding => 0 - $cancellation_amount, >+ manager_id => $params->{staff_id}, >+ borrowernumber => $self->borrowernumber, >+ interface => 'intranet', >+ branchcode => $params->{branch}, >+ } >+ )->store(); >+ >+ Koha::Account::Offset->new( >+ { >+ credit_id => $cancellation->accountlines_id, >+ type => 'CREATE', >+ amount => $cancellation_amount > } >- ); >+ )->store(); >+ >+ # Link cancellation to charge >+ $self->set( >+ { >+ amountoutstanding => $cancellation_amount, >+ status => 'CANCELLED' >+ } >+ )->store(); >+ $cancellation->apply( { debits => [$self] } ); >+ >+ # Reverse any applied payments >+ for my $applied_offset ( @applied_offsets ) { >+ my $credit = $applied_offset->credit; >+ $credit->amountoutstanding( >+ $credit->amountoutstanding - $applied_offset->amount ) >+ ->store(); >+ >+ Koha::Account::Offset->new( >+ { >+ credit_id => $applied_offset->credit_id, >+ debit_id => $self->id, >+ amount => $applied_offset->amount * -1, >+ type => 'VOID', >+ } >+ )->store(); >+ } > } > ); > >@@ -454,7 +488,6 @@ Reduction type may be one of: > > * REFUND > * DISCOUNT >-* CANCELLATION > > Returns the reduction accountline (which will be a credit) > >@@ -482,6 +515,11 @@ sub reduce { > } > } > >+ # Amount should always be passed as positive >+ Koha::Exceptions::Account::AmountNotPositive->throw( >+ error => 'Reduce amount passed is not positive' ) >+ unless ( $params->{amount} > 0 ); >+ > # More mandatory parameters > if ( $params->{interface} eq 'intranet' ) { > my @optional = ( 'staff_id', 'branch' ); >@@ -496,25 +534,33 @@ sub reduce { > > # Make sure the reduction isn't more than the original > my $original = $self->amount; >- Koha::Exceptions::Account::AmountNotPositive->throw( >- error => 'Reduce amount passed is not positive' ) >- unless ( $params->{amount} > 0 ); > Koha::Exceptions::ParameterTooHigh->throw( error => > "Amount to reduce ($params->{amount}) is higher than original amount ($original)" > ) unless ( $original >= $params->{amount} ); >- my $reduced = >- $self->credits( { credit_type_code => [ 'WRITEOFF', 'DISCOUNT', 'REFUND' ] } )->total; >+ >+ # Make sure combined reduction isn't more than the original >+ my $reduced = 0; >+ if ( $self->amount != $self->amountoutstanding ) { >+ my $reduction_offsets = Koha::Account::Offsets->search( >+ { >+ debit_id => $self->id, >+ credit_id => { '!=' => undef }, # not the debt itself >+ type => { '!=' => [ 'Writeoff', 'REFUND', 'DISCOUNT' ] } >+ , # Don't refund writeoffs, refunds or discounts >+ amount => { '<' => 0 } # credits are negative on the DB >+ } >+ ); >+ >+ $reduced = $reduction_offsets->count > 0 ? $reduction_offsets->total * -1 : 0; >+ } >+ > Koha::Exceptions::ParameterTooHigh->throw( error => > "Combined reduction ($params->{amount} + $reduced) is higher than original amount (" > . abs($original) > . ")" ) > unless ( $original >= ( $params->{amount} + abs($reduced) ) ); > >- my $status = { >- 'REFUND' => 'REFUNDED', >- 'DISCOUNT' => 'DISCOUNTED', >- 'CANCELLATION' => 'CANCELLED' >- }; >+ my $status = { 'REFUND' => 'REFUNDED', 'DISCOUNT' => 'DISCOUNTED' }; > > my $reduction; > $self->_result->result_source->schema->txn_do( >diff --git a/Koha/Account/Offsets.pm b/Koha/Account/Offsets.pm >index 0e6971ff57..2fcee1c0a4 100644 >--- a/Koha/Account/Offsets.pm >+++ b/Koha/Account/Offsets.pm >@@ -61,6 +61,49 @@ sub total { > : 0; > } > >+ >+=head3 >+ >+=cut >+ >+sub filter_by_non_reversable { >+ my ($self) = @_; >+ >+ my $me = $self->_resultset()->current_source_alias . "."; >+ my $where = { >+ debit_id => { '!=' => undef }, >+ credit_id => { '!=' => undef }, >+ 'credit.credit_type_code' => [ 'WRITEOFF', 'REFUND', 'DISCOUNT' ], >+ 'credit.status' => [ { '!=' => 'VOID' }, undef ], >+ $me.amount => { '<' => 0 } >+ }; >+ my $attr = { join => 'credit' }; >+ >+ return $self->search( $where, $attr ); >+} >+ >+=head3 >+ >+=cut >+ >+sub filter_by_reversable { >+ my ($self) = @_; >+ >+ my $me = $self->_resultset()->current_source_alias . "."; >+ my $where = { >+ debit_id => { '!=' => undef }, >+ credit_id => { '!=' => undef }, >+ 'credit.credit_type_code' => { >+ '-not_in' => [ 'WRITEOFF', 'REFUND', 'DISCOUNT' ] >+ }, >+ 'credit.status' => [ { '!=' => 'VOID' }, undef ], >+ $me . amount => { '<' => 0 } >+ }; >+ my $attr = { join => 'credit' }; >+ >+ return $self->search( $where, $attr ); >+} >+ > =head2 Internal methods > > =head3 _type >-- >2.20.1
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 28656
:
122551
|
122585
|
122604
|
122605
|
122606
|
122620
| 122621 |
122622