From 9bcf22b0d57fce9d0c38006cd204e3a1cba5b361 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Fri, 13 Sep 2019 14:03:11 +0100 Subject: [PATCH] Bug 24252: Add relations to Koha::Account::Line This patch adds two new relationships to the Koha::Account::Line object. * credit_offsets - returns all credit type Koha::Account::Offsets related to this Koha::Account::Line. * debit_offsets - returns all debit type Koha::Account::Offsets related to this Koha::Account::Line. * credits - returns all credits related to this Koha::Account::Line. * debits - returns all debits related to this Koha::Account::Line. Signed-off-by: Kyle M Hall Signed-off-by: Tomas Cohen Arazi Signed-off-by: Martin Renvoize --- Koha/Account/Line.pm | 81 +++++++++++++++++++++++++++++ t/db_dependent/Koha/Account/Lines.t | 61 +++++++++++++++++++++- 2 files changed, 141 insertions(+), 1 deletion(-) diff --git a/Koha/Account/Line.pm b/Koha/Account/Line.pm index 6f1e5b6e00..f84d460178 100644 --- a/Koha/Account/Line.pm +++ b/Koha/Account/Line.pm @@ -110,6 +110,87 @@ sub debit_type { return Koha::Account::DebitType->_new_from_dbic( $rs ); } +=head3 credit_offsets + +Return the credit_offsets linked to this account line if some exist + +=cut + +sub credit_offsets { + my ( $self ) = @_; + my $rs = $self->_result->account_offsets_credits; + return unless $rs; + return Koha::Account::Offsets->_new_from_dbic($rs); +} + +=head3 debit_offsets + +Return the debit_offsets linked to this account line if some exist + +=cut + +sub debit_offsets { + my ( $self ) = @_; + my $rs = $self->_result->account_offsets_debits; + return unless $rs; + return Koha::Account::Offsets->_new_from_dbic($rs); +} + + +=head3 credits + + my $credits = $accountline->credits; + my $credits = $accountline->credits( $cond, $attr ); + +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. + +=cut + +sub credits { + my ( $self, $cond, $attr ) = @_; + + unless ( $self->is_debit ) { + Koha::Exceptions::Account::IsNotCredit->throw( + error => 'Account line ' . $self->id . ' is not a debit' + ); + } + + my $rs = + $self->_result->search_related('account_offsets_debits') + ->search_related( 'credit', $cond, $attr ); + return unless $rs; + return Koha::Account::Lines->_new_from_dbic($rs); +} + +=head3 debits + + my $debits = $accountline->debits; + my $debits = $accountline->debits( $cond, $attr ); + +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. + +=cut + +sub debits { + my ( $self, $cond, $attr ) = @_; + + unless ( $self->is_credit ) { + Koha::Exceptions::Account::IsNotCredit->throw( + error => 'Account line ' . $self->id . ' is not a credit' + ); + } + + my $rs = + $self->_result->search_related('account_offsets_credits') + ->search_related( 'debit', $cond, $attr ); + return unless $rs; + return Koha::Account::Lines->_new_from_dbic($rs); +} + =head3 void $payment_accountline->void(); diff --git a/t/db_dependent/Koha/Account/Lines.t b/t/db_dependent/Koha/Account/Lines.t index 735b91fc15..6e99bb45eb 100755 --- a/t/db_dependent/Koha/Account/Lines.t +++ b/t/db_dependent/Koha/Account/Lines.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 10; +use Test::More tests => 11; use Test::Exception; use C4::Circulation qw/AddIssue AddReturn/; @@ -518,6 +518,65 @@ subtest 'checkout() tests' => sub { $schema->storage->txn_rollback; }; +subtest 'credits() and debits() tests' => sub { + plan tests => 10; + + $schema->storage->txn_begin; + + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + my $account = $patron->account; + + my $debit1 = $account->add_debit({ + amount => 8, + interface => 'commandline', + type => 'ACCOUNT', + }); + my $debit2 = $account->add_debit({ + amount => 12, + interface => 'commandline', + type => 'ACCOUNT', + }); + my $credit1 = $account->add_credit({ + amount => 5, + interface => 'commandline', + type => 'CREDIT', + }); + my $credit2 = $account->add_credit({ + amount => 10, + interface => 'commandline', + type => 'CREDIT', + }); + + $credit1->apply({ debits => [ $debit1 ] }); + $credit2->apply({ debits => [ $debit1, $debit2 ] }); + + my $credits = $debit1->credits; + is($credits->count, 2, '2 Credits applied to debit 1'); + my $credit = $credits->next; + is($credit->amount + 0, -5, 'Correct first credit'); + $credit = $credits->next; + is($credit->amount + 0, -10, 'Correct second credit'); + + $credits = $debit2->credits; + is($credits->count, 1, '1 Credits applied to debit 2'); + $credit = $credits->next; + is($credit->amount + 0, -10, 'Correct first credit'); + + my $debits = $credit1->debits; + is($debits->count, 1, 'Credit 1 applied to 1 debit'); + my $debit = $debits->next; + is($debit->amount + 0, 8, 'Correct first debit'); + + $debits = $credit2->debits; + is($debits->count, 2, 'Credit 2 applied to 2 debits'); + $debit = $debits->next; + is($debit->amount + 0, 8, 'Correct first debit'); + $debit = $debits->next; + is($debit->amount + 0, 12, 'Correct second debit'); + + $schema->storage->txn_rollback; +}; + subtest "void() tests" => sub { plan tests => 16; -- 2.39.5