From 7e1acb03651bbdfc75c25611dc87a8f126f6881a Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 18 Mar 2021 14:00:43 +0000 Subject: [PATCH] Bug 24300: (QA follow-up) Enhance test and catch failures The tests did not include testing for offset lines. This patch adds such tests and updates the function to pass them Signed-off-by: Tomas Cohen Arazi --- Koha/Account.pm | 69 +++++++++++++++++++++-------------- t/db_dependent/Koha/Account.t | 32 +++++++++++++++- 2 files changed, 72 insertions(+), 29 deletions(-) diff --git a/Koha/Account.pm b/Koha/Account.pm index da1b562af8..182796efef 100644 --- a/Koha/Account.pm +++ b/Koha/Account.pm @@ -489,19 +489,21 @@ sub add_credit { This method allows adding debits to a patron's account -my $debit_line = Koha::Account->new({ patron_id => $patron_id })->add_debit( - { - amount => $amount, - description => $description, - note => $note, - user_id => $user_id, - interface => $interface, - library_id => $library_id, - type => $debit_type, - item_id => $item_id, - issue_id => $issue_id - } -); + my $debit_line = Koha::Account->new({ patron_id => $patron_id })->add_debit( + { + amount => $amount, + description => $description, + note => $note, + user_id => $user_id, + interface => $interface, + library_id => $library_id, + type => $debit_type, + transaction_type => $transaction_type, + cash_register => $register_id, + item_id => $item_id, + issue_id => $issue_id + } + ); $debit_type can be any of: - ACCOUNT @@ -517,6 +519,7 @@ $debit_type can be any of: - RENT_RENEW - RENT_DAILY_RENEW - RESERVE + - PAYOUT =cut @@ -533,6 +536,13 @@ sub add_debit { } } + # check for cash register if using cash + Koha::Exceptions::Account::RegisterRequired->throw() + if ( C4::Context->preference("UseCashRegisters") + && defined( $params->{transaction_type} ) + && ( $params->{transaction_type} eq 'CASH' ) + && !defined( $params->{cash_register} ) ); + # amount should always be a positive value my $amount = $params->{amount}; unless ( $amount > 0 ) { @@ -540,15 +550,17 @@ sub add_debit { error => 'Debit amount passed is not positive' ); } - my $description = $params->{description} // q{}; - my $note = $params->{note} // q{}; - my $user_id = $params->{user_id}; - my $interface = $params->{interface}; - my $library_id = $params->{library_id}; - my $debit_type = $params->{type}; - my $item_id = $params->{item_id}; - my $issue_id = $params->{issue_id}; - my $offset_type = $Koha::Account::offset_type->{$debit_type} // 'Manual Debit'; + my $description = $params->{description} // q{}; + my $note = $params->{note} // q{}; + my $user_id = $params->{user_id}; + my $interface = $params->{interface}; + my $library_id = $params->{library_id}; + my $cash_register = $params->{cash_register}; + my $debit_type = $params->{type}; + my $transaction_type = $params->{transaction_type}; + my $item_id = $params->{item_id}; + my $issue_id = $params->{issue_id}; + my $offset_type = $Koha::Account::offset_type->{$debit_type} // 'Manual Debit'; my $line; my $schema = Koha::Database->new->schema; @@ -565,13 +577,14 @@ sub add_debit { description => $description, debit_type_code => $debit_type, amountoutstanding => $amount, - payment_type => undef, + payment_type => $transaction_type, note => $note, manager_id => $user_id, interface => $interface, itemnumber => $item_id, issue_id => $issue_id, branchcode => $library_id, + register_id => $cash_register, ( $debit_type eq 'OVERDUE' ? ( status => 'UNRETURNED' ) @@ -669,7 +682,7 @@ sub payout_amount { my $amount = $params->{amount}; unless ( $amount > 0 ) { Koha::Exceptions::Account::AmountNotPositive->throw( - error => 'Debit amount passed is not positive' ); + error => 'Payout amount passed is not positive' ); } # Amount should always be less than or equal to outstanding credit @@ -696,12 +709,12 @@ sub payout_amount { { amount => $params->{amount}, type => 'PAYOUT', - payment_type => $params->{payout_type}, + transaction_type => $params->{payout_type}, amountoutstanding => $params->{amount}, manager_id => $params->{staff_id}, interface => $params->{interface}, branchcode => $params->{branch}, - register_id => $params->{cash_register} + cash_register => $params->{cash_register} } ); @@ -710,6 +723,7 @@ sub payout_amount { $credit->apply( { debits => [$payout], offset_type => 'PAYOUT' } ); $payout->discard_changes; + last if $payout->amountoutstanding == 0; } # Set payout as paid @@ -884,7 +898,8 @@ our $offset_type = { 'RENT_RENEW' => 'Rental Fee', 'RENT_DAILY_RENEW' => 'Rental Fee', 'OVERDUE' => 'OVERDUE', - 'RESERVE_EXPIRED' => 'Hold Expired' + 'RESERVE_EXPIRED' => 'Hold Expired', + 'PAYOUT' => 'PAYOUT', }; =head1 AUTHORS diff --git a/t/db_dependent/Koha/Account.t b/t/db_dependent/Koha/Account.t index b6c9222c23..1b01602228 100755 --- a/t/db_dependent/Koha/Account.t +++ b/t/db_dependent/Koha/Account.t @@ -1177,7 +1177,7 @@ subtest 'Koha::Account::pay() generates credit number (Koha::Account::Line->stor }; subtest 'Koha::Account::payout_amount() tests' => sub { - plan tests => 21; + plan tests => 39; $schema->storage->txn_begin; @@ -1258,12 +1258,30 @@ subtest 'Koha::Account::payout_amount() tests' => sub { is($credits->count, 1, "Payout was applied against oldest outstanding credits first"); is($credits->total_outstanding + 0, -10, "Total of 10 outstanding credit remaining"); + my $offsets = Koha::Account::Offsets->search( { debit_id => $payout->id } ); + is( $offsets->count, 4, 'Four offsets generated' ); + my $offset = $offsets->next; + is( $offset->type, 'PAYOUT', 'PAYOUT offset added for payout line' ); + is( $offset->amount * 1, 10, 'Correct offset amount recorded' ); + $offset = $offsets->next; + is( $offset->credit_id, $credit_1->id, "Offset added against credit_1"); + is( $offset->type, 'PAYOUT', "PAYOUT used for offset_type" ); + is( $offset->amount * 1, -2, 'Correct amount offset against credit_1' ); + $offset = $offsets->next; + is( $offset->credit_id, $credit_2->id, "Offset added against credit_2"); + is( $offset->type, 'PAYOUT', "PAYOUT used for offset_type" ); + is( $offset->amount * 1, -3, 'Correct amount offset against credit_2' ); + $offset = $offsets->next; + is( $offset->credit_id, $credit_3->id, "Offset added against credit_3"); + is( $offset->type, 'PAYOUT', "PAYOUT used for offset_type" ); + is( $offset->amount * 1, -5, 'Correct amount offset against credit_3' ); + my $credit_5 = $account->add_credit( { amount => 5, interface => 'commandline' } ); $credits = $account->outstanding_credits(); is($credits->count, 2, "New credit added"); $payout_params->{amount} = 2.50; $payout_params->{credits} = [$credit_5]; - $account->payout_amount($payout_params); + $payout = $account->payout_amount($payout_params); $credits = $account->outstanding_credits(); is($credits->count, 2, "Second credit not fully paid off"); @@ -1273,5 +1291,15 @@ subtest 'Koha::Account::payout_amount() tests' => sub { is($credit_4->amountoutstanding + 0, -10, "Credit 4 unaffected when credit_5 was passed to payout_amount"); is($credit_5->amountoutstanding + 0, -2.50, "Credit 5 correctly reduced when payout_amount called with credit_5 passed"); + $offsets = Koha::Account::Offsets->search( { debit_id => $payout->id } ); + is( $offsets->count, 2, 'Two offsets generated' ); + $offset = $offsets->next; + is( $offset->type, 'PAYOUT', 'PAYOUT offset added for payout line' ); + is( $offset->amount * 1, 2.50, 'Correct offset amount recorded' ); + $offset = $offsets->next; + is( $offset->credit_id, $credit_5->id, "Offset added against credit_5"); + is( $offset->type, 'PAYOUT', "PAYOUT used for offset_type" ); + is( $offset->amount * 1, -2.50, 'Correct amount offset against credit_5' ); + $schema->storage->txn_rollback; }; -- 2.20.1