Bug 17894 - Update pay() and use it internally for WriteOffFee
authorKyle M Hall <kyle@bywatersolutions.com>
Thu, 12 Jan 2017 14:24:02 +0000 (14:24 +0000)
committerKyle M Hall <kyle@bywatersolutions.com>
Thu, 19 Jan 2017 11:15:25 +0000 (11:15 +0000)
Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Jonathan Druart <jonathan.druart@biblibre.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
C4/Accounts.pm
Koha/Account.pm

index f20ea2f..17f7e69 100644 (file)
@@ -26,6 +26,7 @@ use C4::Members;
 use C4::Circulation qw(ReturnLostItem);
 use C4::Log qw(logaction);
 use Koha::Account;
+use Koha::Account::Lines;
 
 use Data::Dumper qw(Dumper);
 
@@ -366,62 +367,17 @@ C<$payment_note> is the note to attach to this payment
 
 sub WriteOffFee {
     my ( $borrowernumber, $accountlines_id, $itemnum, $accounttype, $amount, $branch, $payment_note ) = @_;
-    $payment_note //= "";
-    $branch ||= C4::Context->userenv->{branch};
-    my $manager_id = 0;
-    $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
-
-    # if no item is attached to fine, make sure to store it as a NULL
-    $itemnum ||= undef;
-
-    my ( $sth, $query );
-    my $dbh = C4::Context->dbh();
-
-    $query = "
-        UPDATE accountlines SET amountoutstanding = 0
-        WHERE accountlines_id = ? AND borrowernumber = ?
-    ";
-    $sth = $dbh->prepare( $query );
-    $sth->execute( $accountlines_id, $borrowernumber );
-
-    if ( C4::Context->preference("FinesLog") ) {
-        logaction("FINES", 'MODIFY', $borrowernumber, Dumper({
-            action                => 'fee_writeoff',
-            borrowernumber        => $borrowernumber,
-            accountlines_id       => $accountlines_id,
-            manager_id            => $manager_id,
-        }));
-    }
 
-    $query ="
-        INSERT INTO accountlines
-        ( borrowernumber, accountno, itemnumber, date, amount, description, accounttype, manager_id, note )
-        VALUES ( ?, ?, ?, NOW(), ?, 'Writeoff', 'W', ?, ? )
-    ";
-    $sth = $dbh->prepare( $query );
-    my $acct = getnextacctno($borrowernumber);
-    $sth->execute( $borrowernumber, $acct, $itemnum, $amount, $manager_id, $payment_note );
-
-    if ( C4::Context->preference("FinesLog") ) {
-        logaction("FINES", 'CREATE',$borrowernumber,Dumper({
-            action            => 'create_writeoff',
-            borrowernumber    => $borrowernumber,
-            accountno         => $acct,
-            amount            => 0 - $amount,
-            accounttype       => 'W',
-            itemnumber        => $itemnum,
-            accountlines_paid => [ $accountlines_id ],
-            manager_id        => $manager_id,
-        }));
-    }
-
-    UpdateStats({
-                branch => $branch,
-                type => 'writeoff',
-                amount => $amount,
-                borrowernumber => $borrowernumber}
+    my $line = Koha::Account::Lines->find($accountlines_id);
+    return Koha::Account->new( { patron_id => $borrowernumber } )->pay(
+        {
+            amount     => $amount,
+            lines      => [$line],
+            type       => 'writeoff',
+            note       => $payment_note,
+            library_id => $branch,
+        }
     );
-
 }
 
 =head2 purge_zero_balance_fees
index 2572fc4..28b789f 100644 (file)
@@ -67,6 +67,7 @@ sub pay {
     my $note            = $params->{note} || q{};
     my $library_id      = $params->{library_id};
     my $lines           = $params->{lines};
+    my $type            = $params->{type} || 'payment';
 
     my $userenv = C4::Context->userenv;
 
@@ -154,7 +155,7 @@ sub pay {
                 $self->{patron_id},
                 Dumper(
                     {
-                        action                => 'fee_payment',
+                        action                => "fee_$type",
                         borrowernumber        => $fine->borrowernumber,
                         old_amountoutstanding => $old_amountoutstanding,
                         new_amountoutstanding => $fine->amountoutstanding,
@@ -173,7 +174,12 @@ sub pay {
         last unless $balance_remaining > 0;
     }
 
-    my $account_type = defined($sip) ? "Pay$sip" : 'Pay';
+    my $account_type =
+        $type eq 'writeoff' ? 'W'
+      : defined($sip)       ? "Pay$sip"
+      :                       'Pay';
+
+    my $description = $type eq 'writeoff' ? 'Writeoff' : q{};
 
     my $payment = Koha::Account::Line->new(
         {
@@ -181,7 +187,7 @@ sub pay {
             accountno         => $accountno,
             date              => dt_from_string(),
             amount            => 0 - $amount,
-            description       => q{},
+            description       => $description,
             accounttype       => $account_type,
             amountoutstanding => 0 - $balance_remaining,
             manager_id        => $manager_id,
@@ -194,7 +200,7 @@ sub pay {
     UpdateStats(
         {
             branch         => $library_id,
-            type           => 'payment',
+            type           => $type,
             amount         => $amount,
             borrowernumber => $self->{patron_id},
             accountno      => $accountno,
@@ -207,12 +213,12 @@ sub pay {
             $self->{patron_id},
             Dumper(
                 {
-                    action            => 'create_payment',
+                    action            => "create_$type",
                     borrowernumber    => $self->{patron_id},
                     accountno         => $accountno,
                     amount            => 0 - $amount,
                     amountoutstanding => 0 - $balance_remaining,
-                    accounttype       => 'Pay',
+                    accounttype       => $account_type,
                     accountlines_paid => \@fines_paid,
                     manager_id        => $manager_id,
                 }