Bug 24252: Add relations to Koha::Account::Line
authorMartin Renvoize <martin.renvoize@ptfs-europe.com>
Fri, 13 Sep 2019 13:03:11 +0000 (14:03 +0100)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Wed, 8 Jan 2020 14:48:33 +0000 (14:48 +0000)
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 <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Koha/Account/Line.pm
t/db_dependent/Koha/Account/Lines.t

index 6f1e5b6..f84d460 100644 (file)
@@ -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();
index 735b91f..6e99bb4 100755 (executable)
@@ -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;