Bug 22564: Remove references to 'Rep' accounttype
[koha-equinox.git] / Koha / Account.pm
index ef8a20f..9d66e70 100644 (file)
@@ -32,6 +32,7 @@ use Koha::Patrons;
 use Koha::Account::Lines;
 use Koha::Account::Offsets;
 use Koha::DateUtils qw( dt_from_string );
+use Koha::Exceptions;
 use Koha::Exceptions::Account;
 
 =head1 NAME
@@ -85,13 +86,8 @@ sub pay {
 
     my $patron = Koha::Patrons->find( $self->{patron_id} );
 
-    # We should remove accountno, it is no longer needed
-    my $last = $self->lines->search(
-        {},
-        { order_by => 'accountno' } )->next();
-    my $accountno = $last ? $last->accountno + 1 : 1;
-
     my $manager_id = $userenv ? $userenv->{number} : 0;
+    my $interface = $params ? ( $params->{interface} || C4::Context->interface ) : C4::Context->interface;
 
     my @fines_paid; # List of account lines paid on with this payment
 
@@ -112,7 +108,7 @@ sub pay {
         $fine->amountoutstanding($new_amountoutstanding)->store();
         $balance_remaining = $balance_remaining - $amount_to_pay;
 
-        if ( $fine->itemnumber && $fine->accounttype && ( $fine->accounttype eq 'Rep' || $fine->accounttype eq 'L' ) )
+        if ( $fine->itemnumber && $fine->accounttype && ( $fine->accounttype eq 'L' ) )
         {
             C4::Circulation::ReturnLostItem( $self->{patron_id}, $fine->itemnumber );
         }
@@ -138,11 +134,11 @@ sub pay {
                         new_amountoutstanding => 0,
                         amount_paid           => $old_amountoutstanding,
                         accountlines_id       => $fine->id,
-                        accountno             => $fine->accountno,
                         manager_id            => $manager_id,
                         note                  => $note,
                     }
-                )
+                ),
+                $interface
             );
             push( @fines_paid, $fine->id );
         }
@@ -189,11 +185,11 @@ sub pay {
                         new_amountoutstanding => $fine->amountoutstanding,
                         amount_paid           => $amount_to_pay,
                         accountlines_id       => $fine->id,
-                        accountno             => $fine->accountno,
                         manager_id            => $manager_id,
                         note                  => $note,
                     }
-                )
+                ),
+                $interface
             );
             push( @fines_paid, $fine->id );
         }
@@ -212,7 +208,6 @@ sub pay {
     my $payment = Koha::Account::Line->new(
         {
             borrowernumber    => $self->{patron_id},
-            accountno         => $accountno,
             date              => dt_from_string(),
             amount            => 0 - $amount,
             description       => $description,
@@ -220,6 +215,7 @@ sub pay {
             payment_type      => $payment_type,
             amountoutstanding => 0 - $balance_remaining,
             manager_id        => $manager_id,
+            interface         => $interface,
             branchcode        => $library_id,
             note              => $note,
         }
@@ -236,7 +232,6 @@ sub pay {
             type           => $type,
             amount         => $amount,
             borrowernumber => $self->{patron_id},
-            accountno      => $accountno,
         }
     );
 
@@ -248,14 +243,14 @@ sub pay {
                 {
                     action            => "create_$type",
                     borrowernumber    => $self->{patron_id},
-                    accountno         => $accountno,
                     amount            => 0 - $amount,
                     amountoutstanding => 0 - $balance_remaining,
                     accounttype       => $account_type,
                     accountlines_paid => \@fines_paid,
                     manager_id        => $manager_id,
                 }
-            )
+            ),
+            $interface
         );
     }
 
@@ -300,6 +295,7 @@ my $credit_line = Koha::Account->new({ patron_id => $patron_id })->add_credit(
         description  => $description,
         note         => $note,
         user_id      => $user_id,
+        interface    => $interface,
         library_id   => $library_id,
         sip          => $sip,
         payment_type => $payment_type,
@@ -326,12 +322,19 @@ sub add_credit {
     my $description  = $params->{description} // q{};
     my $note         = $params->{note} // q{};
     my $user_id      = $params->{user_id};
+    my $interface    = $params->{interface};
     my $library_id   = $params->{library_id};
     my $sip          = $params->{sip};
     my $payment_type = $params->{payment_type};
     my $type         = $params->{type} || 'payment';
     my $item_id      = $params->{item_id};
 
+    unless ( $interface ) {
+        Koha::Exceptions::MissingParameter->throw(
+            error => 'The interface parameter is mandatory'
+        );
+    }
+
     my $schema = Koha::Database->new->schema;
 
     my $account_type = $Koha::Account::account_type_credit->{$type};
@@ -343,16 +346,10 @@ sub add_credit {
 
     $schema->txn_do(
         sub {
-            # We should remove accountno, it is no longer needed
-            my $last = $self->lines->search(
-                {},
-                { order_by => 'accountno' } )->next();
-            my $accountno = $last ? $last->accountno + 1 : 1;
 
             # Insert the account line
             $line = Koha::Account::Line->new(
                 {   borrowernumber    => $self->{patron_id},
-                    accountno         => $accountno,
                     date              => \'NOW()',
                     amount            => $amount,
                     description       => $description,
@@ -361,9 +358,9 @@ sub add_credit {
                     payment_type      => $payment_type,
                     note              => $note,
                     manager_id        => $user_id,
+                    interface         => $interface,
                     branchcode        => $library_id,
                     itemnumber        => $item_id,
-                    lastincrement     => undef,
                 }
             )->store();
 
@@ -380,7 +377,6 @@ sub add_credit {
                     type           => $type,
                     amount         => $amount,
                     borrowernumber => $self->{patron_id},
-                    accountno      => $accountno,
                 }
             ) if grep { $type eq $_ } ('payment', 'writeoff') ;
 
@@ -391,7 +387,6 @@ sub add_credit {
                     Dumper(
                         {   action            => "create_$type",
                             borrowernumber    => $self->{patron_id},
-                            accountno         => $accountno,
                             amount            => $amount,
                             description       => $description,
                             amountoutstanding => $amount,
@@ -401,7 +396,8 @@ sub add_credit {
                             manager_id        => $user_id,
                             branchcode        => $library_id,
                         }
-                    )
+                    ),
+                    $interface
                 );
             }
         }
@@ -420,6 +416,7 @@ my $debit_line = Koha::Account->new({ patron_id => $patron_id })->add_debit(
         description  => $description,
         note         => $note,
         user_id      => $user_id,
+        interface    => $interface,
         library_id   => $library_id,
         type         => $debit_type,
         item_id      => $item_id,
@@ -428,7 +425,7 @@ my $debit_line = Koha::Account->new({ patron_id => $patron_id })->add_debit(
 );
 
 $debit_type can be any of:
-  - fine
+  - overdue
   - lost_item
   - new_card
   - account
@@ -436,7 +433,6 @@ $debit_type can be any of:
   - processing
   - rent
   - reserve
-  - overdue
   - manual
 
 =cut
@@ -457,11 +453,18 @@ sub add_debit {
     my $description  = $params->{description} // q{};
     my $note         = $params->{note} // q{};
     my $user_id      = $params->{user_id};
+    my $interface    = $params->{interface};
     my $library_id   = $params->{library_id};
     my $type         = $params->{type};
     my $item_id      = $params->{item_id};
     my $issue_id     = $params->{issue_id};
 
+    unless ( $interface ) {
+        Koha::Exceptions::MissingParameter->throw(
+            error => 'The interface parameter is mandatory'
+        );
+    }
+
     my $schema = Koha::Database->new->schema;
 
     unless ( exists($Koha::Account::account_type_debit->{$type}) ) {
@@ -476,10 +479,6 @@ sub add_debit {
 
     $schema->txn_do(
         sub {
-            # We should remove accountno, it is no longer needed
-            my $last = Koha::Account::Lines->search( { borrowernumber => $self->{patron_id} },
-                { order_by => 'accountno' } )->next();
-            my $accountno = $last ? $last->accountno + 1 : 1;
 
             # Insert the account line
             $line = Koha::Account::Line->new(
@@ -492,10 +491,11 @@ sub add_debit {
                     payment_type      => undef,
                     note              => $note,
                     manager_id        => $user_id,
+                    interface         => $interface,
                     itemnumber        => $item_id,
                     issue_id          => $issue_id,
                     branchcode        => $library_id,
-                    ( $type eq 'fine' ? ( lastincrement => $amount ) : ()),
+                    ( $type eq 'overdue' ? ( status => 'UNRETURNED' ) : ()),
                 }
             )->store();
 
@@ -514,7 +514,6 @@ sub add_debit {
                     Dumper(
                         {   action            => "create_$type",
                             borrowernumber    => $self->{patron_id},
-                            accountno         => $accountno,
                             amount            => $amount,
                             description       => $description,
                             amountoutstanding => $amount,
@@ -523,7 +522,8 @@ sub add_debit {
                             itemnumber        => $item_id,
                             manager_id        => $user_id,
                         }
-                    )
+                    ),
+                    $interface
                 );
             }
         }
@@ -697,7 +697,7 @@ our $offset_type = {
     'processing'       => 'Processing Fee',
     'lost_item'        => 'Lost Item',
     'rent'             => 'Rental Fee',
-    'fine'             => 'Fine',
+    'overdue'          => 'OVERDUE',
     'manual_debit'     => 'Manual Debit',
     'hold_expired'     => 'Hold Expired'
 };
@@ -720,14 +720,13 @@ our $account_type_credit = {
 
 our $account_type_debit = {
     'account'       => 'A',
-    'fine'          => 'FU',
+    'overdue'       => 'OVERDUE',
     'lost_item'     => 'L',
     'new_card'      => 'N',
     'sundry'        => 'M',
     'processing'    => 'PF',
     'rent'          => 'Rent',
     'reserve'       => 'Res',
-    'overdue'       => 'O',
     'manual_debit'  => 'M',
     'hold_expired'  => 'HE'
 };