Bug 23843: Add mapping to Koha::Account::Line
[koha-ffzg.git] / Koha / Account / Line.pm
index a93cb81..ae7bbc1 100644 (file)
@@ -41,6 +41,19 @@ Koha::Account::Line - Koha accountline Object class
 
 =cut
 
+=head3 patron
+
+Return the patron linked to this account line
+
+=cut
+
+sub patron {
+    my ( $self ) = @_;
+    my $rs = $self->_result->borrowernumber;
+    return unless $rs;
+    return Koha::Patron->_new_from_dbic( $rs );
+}
+
 =head3 item
 
 Return the item linked to this account line if exists
@@ -54,9 +67,28 @@ sub item {
     return Koha::Item->_new_from_dbic( $rs );
 }
 
+=head3 checkout
+
+Return the checkout linked to this account line if exists
+
+=cut
+
+sub checkout {
+    my ( $self ) = @_;
+    return unless $self->issue_id ;
+
+    $self->{_checkout} ||= Koha::Checkouts->find( $self->issue_id );
+    $self->{_checkout} ||= Koha::Old::Checkouts->find( $self->issue_id );
+    return $self->{_checkout};
+}
+
 =head3 void
 
-$payment_accountline->void();
+  $payment_accountline->void();
+
+Used to 'void' (or reverse) a payment/credit. It will roll back any offsets
+created by the application of this credit upon any debits and mark the credit
+as 'void' by updating it's status to "VOID".
 
 =cut
 
@@ -118,7 +150,7 @@ sub void {
 
             $self->set(
                 {
-                    accounttype       => 'VOID',
+                    status            => 'VOID',
                     amountoutstanding => 0,
                     amount            => 0,
                 }
@@ -134,7 +166,7 @@ sub void {
     my $debits = $account->outstanding_debits;
     my $outstanding_amount = $credit->apply( { debits => $debits, [ offset_type => $offset_type ] } );
 
-Applies the credit to a given debits set.
+Applies the credit to a given debits array reference.
 
 =head4 arguments hashref
 
@@ -172,7 +204,7 @@ sub apply {
     my $schema = Koha::Database->new->schema;
 
     $schema->txn_do( sub {
-        while ( my $debit = $debits->next ) {
+        for my $debit ( @{$debits} ) {
 
             unless ( $debit->is_debit ) {
                 Koha::Exceptions::Account::IsNotDebit->throw(
@@ -202,6 +234,16 @@ sub apply {
 
             $self->amountoutstanding( $available_credit * -1 )->store;
             $debit->amountoutstanding( $owed - $amount_to_cancel )->store;
+
+            # Same logic exists in Koha::Account::pay
+            if (   $debit->amountoutstanding == 0
+                && $debit->itemnumber
+                && $debit->accounttype
+                && $debit->accounttype eq 'LOST' )
+            {
+                C4::Circulation::ReturnLostItem( $self->borrowernumber, $debit->itemnumber );
+            }
+
         }
     });
 
@@ -214,13 +256,14 @@ This method allows updating a debit or credit on a patron's account
 
     $account_line->adjust(
         {
-            amount => $amount,
-            type   => $update_type,
+            amount    => $amount,
+            type      => $update_type,
+            interface => $interface
         }
     );
 
 $update_type can be any of:
-  - fine_update
+  - overdue_update
 
 Authors Note: The intention here is that this method is only used
 to adjust accountlines where the final amount is not yet known/fixed.
@@ -235,6 +278,7 @@ sub adjust {
 
     my $amount       = $params->{amount};
     my $update_type  = $params->{type};
+    my $interface    = $params->{interface};
 
     unless ( exists($Koha::Account::Line::allowed_update->{$update_type}) ) {
         Koha::Exceptions::Account::UnrecognisedType->throw(
@@ -242,11 +286,21 @@ sub adjust {
         );
     }
 
-    my $account_type = $self->accounttype;
-    unless ( $Koha::Account::Line::allowed_update->{$update_type} eq $account_type ) {
+    my $account_type   = $self->accounttype;
+    my $account_status = $self->status;
+    unless (
+        (
+            exists(
+                $Koha::Account::Line::allowed_update->{$update_type}
+                  ->{$account_type}
+            )
+            && ( $Koha::Account::Line::allowed_update->{$update_type}
+                ->{$account_type} eq $account_status )
+        )
+      )
+    {
         Koha::Exceptions::Account::UnrecognisedType->throw(
-            error => 'Update type not allowed on this accounttype'
-        );
+            error => 'Update type not allowed on this accounttype' );
     }
 
     my $schema = Koha::Database->new->schema;
@@ -259,8 +313,8 @@ sub adjust {
             my $difference                = $amount - $amount_before;
             my $new_outstanding           = $amount_outstanding_before + $difference;
 
-            my $offset_type = substr( $update_type, 0, index( $update_type, '_' ) );
-            $offset_type .= ( $difference > 0 ) ? "_increase" : "_decrease";
+            my $offset_type = $account_type;
+            $offset_type .= ( $difference > 0 ) ? "_INCREASE" : "_DECREASE";
 
             # Catch cases that require patron refunds
             if ( $new_outstanding < 0 ) {
@@ -271,7 +325,8 @@ sub adjust {
                         amount      => $new_outstanding * -1,
                         description => 'Overpayment refund',
                         type        => 'credit',
-                        ( $update_type eq 'fine_update' ? ( item_id => $self->itemnumber ) : ()),
+                        interface   => $interface,
+                        ( $update_type eq 'overdue_update' ? ( item_id => $self->itemnumber ) : ()),
                     }
                 );
                 $new_outstanding = 0;
@@ -283,7 +338,6 @@ sub adjust {
                     date              => \'NOW()',
                     amount            => $amount,
                     amountoutstanding => $new_outstanding,
-                    ( $update_type eq 'fine_update' ? ( lastincrement => $difference ) : ()),
                 }
             )->store();
 
@@ -303,7 +357,6 @@ sub adjust {
                     Dumper(
                         {   action            => $update_type,
                             borrowernumber    => $self->borrowernumber,
-                            accountno         => $self->accountno,
                             amount            => $amount,
                             description       => undef,
                             amountoutstanding => $new_outstanding,
@@ -313,7 +366,7 @@ sub adjust {
                             manager_id        => undef,
                         }
                     )
-                ) if ( $update_type eq 'fine_update' );
+                ) if ( $update_type eq 'overdue_update' );
             }
         }
     );
@@ -345,6 +398,27 @@ sub is_debit {
     return !$self->is_credit;
 }
 
+=head3 to_api_mapping
+
+This method returns the mapping for representing a Koha::Account::Line object
+on the API.
+
+=cut
+
+sub to_api_mapping {
+    return {
+        accountlines_id   => 'account_line_id',
+        accounttype       => 'account_type',
+        amountoutstanding => 'amount_outstanding',
+        borrowernumber    => 'patron_id',
+        branchcode        => 'library_id',
+        issue_id          => 'checkout_id',
+        itemnumber        => 'item_id',
+        manager_id        => 'user_id',
+        note              => 'internal_note',
+    };
+}
+
 =head2 Internal methods
 
 =cut
@@ -365,7 +439,7 @@ sub _type {
 
 =cut
 
-our $allowed_update = { 'fine_update' => 'FU', };
+our $allowed_update = { 'overdue_update' => { 'OVERDUE' => 'UNRETURNED' } };
 
 =head1 AUTHORS