Bug 30717: Format dates when editing items
[srvgit] / Koha / Account.pm
index 7c01fba..c7cf9e5 100644 (file)
@@ -20,9 +20,8 @@ package Koha::Account;
 use Modern::Perl;
 
 use Carp;
-use Data::Dumper;
-use List::MoreUtils qw( uniq );
-use Try::Tiny;
+use Data::Dumper qw( Dumper );
+use Try::Tiny qw( catch try );
 
 use C4::Circulation qw( ReturnLostItem CanBookBeRenewed AddRenewal );
 use C4::Letters;
@@ -34,7 +33,6 @@ use Koha::Patrons;
 use Koha::Account::Lines;
 use Koha::Account::Offsets;
 use Koha::Account::DebitTypes;
-use Koha::DateUtils qw( dt_from_string );
 use Koha::Exceptions;
 use Koha::Exceptions::Account;
 
@@ -64,7 +62,6 @@ Koha::Account->new( { patron_id => $borrowernumber } )->pay(
         library_id  => $branchcode,
         lines       => $lines, # Arrayref of Koha::Account::Line objects to pay
         credit_type => $type,  # credit_type_code code
-        offset_type => $offset_type,    # offset type code
         item_id     => $itemnumber,     # pass the itemnumber if this is a credit pertianing to a specific item (i.e LOST_FOUND)
     }
 );
@@ -81,13 +78,11 @@ sub pay {
     my $lines         = $params->{lines};
     my $type          = $params->{type} || 'PAYMENT';
     my $payment_type  = $params->{payment_type} || undef;
-    my $offset_type   = $params->{offset_type} || $type eq 'WRITEOFF' ? 'Writeoff' : 'Payment';
     my $cash_register = $params->{cash_register};
     my $item_id       = $params->{item_id};
 
     my $userenv = C4::Context->userenv;
 
-
     my $manager_id = $userenv ? $userenv->{number} : undef;
     my $interface = $params ? ( $params->{interface} || C4::Context->interface ) : C4::Context->interface;
     my $payment = $self->payin_amount(
@@ -106,8 +101,16 @@ sub pay {
         }
     );
 
+    # NOTE: Pay historically always applied as much credit as it could to all
+    # existing outstanding debits, whether passed specific debits or otherwise.
+    if ( $payment->amountoutstanding ) {
+        $payment =
+          $payment->apply(
+            { debits => [ $self->outstanding_debits->as_list ] } );
+    }
+
     my $patron = Koha::Patrons->find( $self->{patron_id} );
-    my @account_offsets = $payment->debit_offsets;
+    my @account_offsets = $payment->credit_offsets({ type => 'APPLY' })->as_list;
     if ( C4::Context->preference('UseEmailReceipts') ) {
         if (
             my $letter = C4::Letters::GetPreparedLetter(
@@ -137,7 +140,7 @@ sub pay {
     }
 
     my $renew_outcomes = [];
-    for my $message ( @{$payment->messages} ) {
+    for my $message ( @{$payment->object_messages} ) {
         push @{$renew_outcomes}, $message->payload;
     }
 
@@ -238,8 +241,8 @@ sub add_credit {
                 my $account_offset = Koha::Account::Offset->new(
                     {
                         credit_id => $line->id,
-                        type   => $Koha::Account::offset_type->{$credit_type} // $Koha::Account::offset_type->{CREDIT},
-                        amount => $amount
+                        type      => 'CREATE',
+                        amount    => $amount * -1
                     }
                 )->store();
 
@@ -357,8 +360,7 @@ sub payin_amount {
             if ( exists( $params->{debits} ) ) {
                 $credit = $credit->apply(
                     {
-                        debits      => $params->{debits},
-                        offset_type => $params->{type}
+                        debits => $params->{debits}
                     }
                 );
             }
@@ -369,8 +371,7 @@ sub payin_amount {
             {
                 $credit = $credit->apply(
                     {
-                        debits      => [ $self->outstanding_debits->as_list ],
-                        offset_type => $params->{type}
+                        debits => [ $self->outstanding_debits->as_list ]
                     }
                 );
             }
@@ -455,7 +456,6 @@ sub add_debit {
     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;
@@ -492,7 +492,7 @@ sub add_debit {
                 my $account_offset = Koha::Account::Offset->new(
                     {
                         debit_id => $line->id,
-                        type     => $offset_type,
+                        type     => 'CREATE',
                         amount   => $amount
                     }
                 )->store();
@@ -615,8 +615,7 @@ sub payout_amount {
 
             # Offset against credits
             for my $credit ( @{$outstanding_credits} ) {
-                $credit->apply(
-                    { debits => [$payout], offset_type => 'PAYOUT' } );
+                $credit->apply( { debits => [$payout] } );
                 $payout->discard_changes;
                 last if $payout->amountoutstanding == 0;
             }
@@ -648,8 +647,7 @@ my $lines = Koha::Account->new({ patron_id => $patron_id })->outstanding_debits;
 
 It returns the debit lines with outstanding amounts for the patron.
 
-In scalar context, it returns a Koha::Account::Lines iterator. In list context, it will
-return a list of Koha::Account::Line objects.
+It returns a Koha::Account::Lines iterator.
 
 =cut
 
@@ -670,8 +668,7 @@ my $lines = Koha::Account->new({ patron_id => $patron_id })->outstanding_credits
 
 It returns the credit lines with outstanding amounts for the patron.
 
-In scalar context, it returns a Koha::Account::Lines iterator. In list context, it will
-return a list of Koha::Account::Line objects.
+It returns a Koha::Account::Lines iterator.
 
 =cut
 
@@ -770,33 +767,6 @@ sub reconcile_balance {
 
 1;
 
-=head2 Name mappings
-
-=head3 $offset_type
-
-=cut
-
-our $offset_type = {
-    'CREDIT'           => 'Manual Credit',
-    'FORGIVEN'         => 'Writeoff',
-    'LOST_FOUND'       => 'Lost Item Found',
-    'OVERPAYMENT'      => 'Overpayment',
-    'PAYMENT'          => 'Payment',
-    'WRITEOFF'         => 'Writeoff',
-    'ACCOUNT'          => 'Account Fee',
-    'ACCOUNT_RENEW'    => 'Account Fee',
-    'RESERVE'          => 'Reserve Fee',
-    'PROCESSING'       => 'Processing Fee',
-    'LOST'             => 'Lost Item',
-    'RENT'             => 'Rental Fee',
-    'RENT_DAILY'       => 'Rental Fee',
-    'RENT_RENEW'       => 'Rental Fee',
-    'RENT_DAILY_RENEW' => 'Rental Fee',
-    'OVERDUE'          => 'OVERDUE',
-    'RESERVE_EXPIRED'  => 'Hold Expired',
-    'PAYOUT'           => 'PAYOUT',
-};
-
 =head1 AUTHORS
 
 =encoding utf8