Bug 23384: Fix use statement order for ArticleRequest::Status
[srvgit] / Koha / Account.pm
index 67988f2..81f7a34 100644 (file)
@@ -24,14 +24,16 @@ use Data::Dumper;
 use List::MoreUtils qw( uniq );
 use Try::Tiny;
 
-use C4::Circulation qw( ReturnLostItem );
+use C4::Circulation qw( ReturnLostItem CanBookBeRenewed AddRenewal );
 use C4::Letters;
 use C4::Log qw( logaction );
 use C4::Stats qw( UpdateStats );
+use C4::Overdues qw(GetFine);
 
 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;
@@ -79,7 +81,7 @@ sub pay {
     my $type          = $params->{type} || 'PAYMENT';
     my $payment_type  = $params->{payment_type} || undef;
     my $credit_type   = $params->{credit_type};
-    my $offset_type   = $params->{offset_type} || $type eq 'writeoff' ? 'Writeoff' : 'Payment';
+    my $offset_type   = $params->{offset_type} || $type eq 'WRITEOFF' ? 'Writeoff' : 'Payment';
     my $cash_register = $params->{cash_register};
 
     my $userenv = C4::Context->userenv;
@@ -96,6 +98,10 @@ sub pay {
 
     my @fines_paid; # List of account lines paid on with this payment
 
+    # The outcome of any attempted item renewals as a result of fines being
+    # paid off
+    my $renew_outcomes = [];
+
     my $balance_remaining = $amount; # Set it now so we can adjust the amount if necessary
     $balance_remaining ||= 0;
 
@@ -113,6 +119,16 @@ sub pay {
         $fine->amountoutstanding($new_amountoutstanding)->store();
         $balance_remaining = $balance_remaining - $amount_to_pay;
 
+        # Attempt to renew the item associated with this debit if
+        # appropriate
+        if ($fine->renewable) {
+            # We're ignoring the definition of $interface above, by all
+            # accounts we can't rely on C4::Context::interface, so here
+            # we're only using what we've been explicitly passed
+            my $outcome = $fine->renew_item({ interface => $interface });
+            push @{$renew_outcomes}, $outcome if $outcome;
+        }
+
         # Same logic exists in Koha::Account::Line::apply
         if (   $new_amountoutstanding == 0
             && $fine->itemnumber
@@ -173,6 +189,14 @@ sub pay {
         $fine->amountoutstanding( $old_amountoutstanding - $amount_to_pay );
         $fine->store();
 
+        # If we need to make a note of the item associated with this line,
+        # in order that we can potentially renew it, do so.
+        my $amt = $old_amountoutstanding - $amount_to_pay;
+        if ($fine->renewable) {
+            my $outcome = $fine->renew_item;
+            push @{$renew_outcomes}, $outcome;
+        }
+
         if (   $fine->amountoutstanding == 0
             && $fine->itemnumber
             && $fine->debit_type_code
@@ -216,11 +240,11 @@ sub pay {
     }
 
     $credit_type ||=
-      $type eq 'writeoff'
-      ? 'W'
+      $type eq 'WRITEOFF'
+      ? 'WRITEOFF'
       : 'PAYMENT';
 
-    $description ||= $type eq 'writeoff' ? 'Writeoff' : q{};
+    $description ||= $type eq 'WRITEOFF' ? 'Writeoff' : q{};
 
     my $payment = Koha::Account::Line->new(
         {
@@ -247,7 +271,7 @@ sub pay {
     UpdateStats(
         {
             branch         => $library_id,
-            type           => $type,
+            type           => lc($type),
             amount         => $amount,
             borrowernumber => $self->{patron_id},
         }
@@ -281,7 +305,7 @@ sub pay {
                 lang    => $patron->lang,
                 tables => {
                     borrowers       => $self->{patron_id},
-                    branches        => $self->{library_id},
+                    branches        => $library_id,
                 },
                 substitute => {
                     credit => $payment,
@@ -300,7 +324,7 @@ sub pay {
         }
     }
 
-    return $payment->id;
+    return { payment_id => $payment->id, renew_result => $renew_outcomes };
 }
 
 =head3 add_credit
@@ -325,8 +349,8 @@ $credit_type can be any of:
   - 'CREDIT'
   - 'PAYMENT'
   - 'FORGIVEN'
-  - 'LOST_RETURN'
-  - 'writeoff'
+  - 'LOST_FOUND'
+  - 'WRITEOFF'
 
 =cut
 
@@ -334,8 +358,22 @@ sub add_credit {
 
     my ( $self, $params ) = @_;
 
-    # amount is passed as a positive value, but we store credit as negative values
-    my $amount        = $params->{amount} * -1;
+    # check for mandatory params
+    my @mandatory = ( 'interface', 'amount' );
+    for my $param (@mandatory) {
+        unless ( defined( $params->{$param} ) ) {
+            Koha::Exceptions::MissingParameter->throw(
+                error => "The $param parameter is mandatory" );
+        }
+    }
+
+    # amount should always be passed as a positive value
+    my $amount = $params->{amount} * -1;
+    unless ( $amount < 0 ) {
+        Koha::Exceptions::Account::AmountNotPositive->throw(
+            error => 'Debit amount passed is not positive' );
+    }
+
     my $description   = $params->{description} // q{};
     my $note          = $params->{note} // q{};
     my $user_id       = $params->{user_id};
@@ -343,85 +381,93 @@ sub add_credit {
     my $library_id    = $params->{library_id};
     my $cash_register = $params->{cash_register};
     my $payment_type  = $params->{payment_type};
-    my $type          = $params->{type} || 'PAYMENT';
+    my $credit_type   = $params->{type} || 'PAYMENT';
     my $item_id       = $params->{item_id};
 
-    unless ( $interface ) {
-        Koha::Exceptions::MissingParameter->throw(
-            error => 'The interface parameter is mandatory'
-        );
-    }
-
     Koha::Exceptions::Account::RegisterRequired->throw()
       if ( C4::Context->preference("UseCashRegisters")
         && defined($payment_type)
         && ( $payment_type eq 'CASH' )
         && !defined($cash_register) );
 
-    my $schema = Koha::Database->new->schema;
-
-    my $credit_type = $Koha::Account::account_type_credit->{$type};
     my $line;
+    my $schema = Koha::Database->new->schema;
+    try {
+        $schema->txn_do(
+            sub {
 
-    $schema->txn_do(
-        sub {
+                # Insert the account line
+                $line = Koha::Account::Line->new(
+                    {
+                        borrowernumber    => $self->{patron_id},
+                        date              => \'NOW()',
+                        amount            => $amount,
+                        description       => $description,
+                        credit_type_code  => $credit_type,
+                        amountoutstanding => $amount,
+                        payment_type      => $payment_type,
+                        note              => $note,
+                        manager_id        => $user_id,
+                        interface         => $interface,
+                        branchcode        => $library_id,
+                        register_id       => $cash_register,
+                        itemnumber        => $item_id,
+                    }
+                )->store();
 
-            # Insert the account line
-            $line = Koha::Account::Line->new(
-                {   borrowernumber    => $self->{patron_id},
-                    date              => \'NOW()',
-                    amount            => $amount,
-                    description       => $description,
-                    credit_type_code  => $credit_type,
-                    amountoutstanding => $amount,
-                    payment_type      => $payment_type,
-                    note              => $note,
-                    manager_id        => $user_id,
-                    interface         => $interface,
-                    branchcode        => $library_id,
-                    register_id       => $cash_register,
-                    itemnumber        => $item_id,
-                }
-            )->store();
+                # Record the account offset
+                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
+                    }
+                )->store();
 
-            # Record the account offset
-            my $account_offset = Koha::Account::Offset->new(
-                {   credit_id => $line->id,
-                    type      => $Koha::Account::offset_type->{$type},
-                    amount    => $amount
-                }
-            )->store();
+                UpdateStats(
+                    {
+                        branch         => $library_id,
+                        type           => lc($credit_type),
+                        amount         => $amount,
+                        borrowernumber => $self->{patron_id},
+                    }
+                ) if grep { $credit_type eq $_ } ( 'PAYMENT', 'WRITEOFF' );
 
-            UpdateStats(
-                {   branch         => $library_id,
-                    type           => $type,
-                    amount         => $amount,
-                    borrowernumber => $self->{patron_id},
+                if ( C4::Context->preference("FinesLog") ) {
+                    logaction(
+                        "FINES", 'CREATE',
+                        $self->{patron_id},
+                        Dumper(
+                            {
+                                action            => "create_$credit_type",
+                                borrowernumber    => $self->{patron_id},
+                                amount            => $amount,
+                                description       => $description,
+                                amountoutstanding => $amount,
+                                credit_type_code  => $credit_type,
+                                note              => $note,
+                                itemnumber        => $item_id,
+                                manager_id        => $user_id,
+                                branchcode        => $library_id,
+                            }
+                        ),
+                        $interface
+                    );
                 }
-            ) if grep { $type eq $_ } ('PAYMENT', 'writeoff') ;
-
-            if ( C4::Context->preference("FinesLog") ) {
-                logaction(
-                    "FINES", 'CREATE',
-                    $self->{patron_id},
-                    Dumper(
-                        {   action            => "create_$type",
-                            borrowernumber    => $self->{patron_id},
-                            amount            => $amount,
-                            description       => $description,
-                            amountoutstanding => $amount,
-                            credit_type_code  => $credit_type,
-                            note              => $note,
-                            itemnumber        => $item_id,
-                            manager_id        => $user_id,
-                            branchcode        => $library_id,
-                        }
-                    ),
-                    $interface
-                );
+            }
+        );
+    }
+    catch {
+        if ( ref($_) eq 'Koha::Exceptions::Object::FKConstraint' ) {
+            if ( $_->broken_fk eq 'credit_type_code' ) {
+                Koha::Exceptions::Account::UnrecognisedType->throw(
+                    error => 'Type of credit not recognised' );
+            }
+            else {
+                $_->rethrow;
             }
         }
-    );
+    };
 
     return $line;
 }
@@ -718,9 +764,9 @@ sub reconcile_balance {
 our $offset_type = {
     'CREDIT'           => 'Manual Credit',
     'FORGIVEN'         => 'Writeoff',
-    'LOST_RETURN'      => 'Lost Item',
+    'LOST_FOUND'       => 'Lost Item Found',
     'PAYMENT'          => 'Payment',
-    'writeoff'         => 'Writeoff',
+    'WRITEOFF'         => 'Writeoff',
     'ACCOUNT'          => 'Account Fee',
     'ACCOUNT_RENEW'    => 'Account Fee',
     'RESERVE'          => 'Reserve Fee',
@@ -734,18 +780,6 @@ our $offset_type = {
     'RESERVE_EXPIRED'  => 'Hold Expired'
 };
 
-=head3 $account_type_credit
-
-=cut
-
-our $account_type_credit = {
-    'CREDIT'           => 'CREDIT',
-    'FORGIVEN'         => 'FORGIVEN',
-    'LOST_RETURN'      => 'LOST_RETURN',
-    'PAYMENT'          => 'PAYMENT',
-    'writeoff'         => 'W'
-};
-
 =head1 AUTHORS
 
 =encoding utf8