Bug 29139: Add exceptions to relation accessors
authorMartin Renvoize <martin.renvoize@ptfs-europe.com>
Thu, 30 Sep 2021 08:28:29 +0000 (09:28 +0100)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Fri, 1 Oct 2021 14:28:55 +0000 (16:28 +0200)
We already had exceptions on the many-to-many links, but we didn't have
them for the middle table. The underlying dbic relations make it clear
which id's are being used for linking.  A 'credit' has 'credit_offsets',
a 'debit' has 'debit_offsets'.

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Andrew Fuerste-Henry <andrew@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Koha/Account/Line.pm

index c0964fc..9b402a1 100644 (file)
@@ -145,6 +145,13 @@ Return the credit_offsets linked to this account line if some exist
 
 sub credit_offsets {
     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', $cond, $attr);
     return unless $rs;
     return Koha::Account::Offsets->_new_from_dbic($rs);
@@ -158,12 +165,18 @@ Return the debit_offsets linked to this account line if some exist
 
 sub debit_offsets {
     my ( $self, $cond, $attr ) = @_;
+
+    unless ( $self->is_debit ) {
+        Koha::Exceptions::Account::IsNotDebit->throw(
+            error => 'Account line ' . $self->id . ' is not a debit'
+        );
+    }
+
     my $rs = $self->_result->search_related( 'account_offsets_debits', $cond, $attr);
     return unless $rs;
     return Koha::Account::Offsets->_new_from_dbic($rs);
 }
 
-
 =head3 credits
 
   my $credits = $accountline->credits;