From 9432c010fa40aa5b72e153de8b2e29a8360dba77 Mon Sep 17 00:00:00 2001
From: Kyle M Hall <kyle@bywatersolutions.com>
Date: Thu, 17 Jan 2013 08:34:27 -0500
Subject: [PATCH] Bug 9293 - action_logs not tracking payments

Add all fine actions to action_logs via FinesLog syspref

Test Plan:
1) Enable FinesLog
2) Apply patch
3) Try all the various fine related actions possible
4) Verify that each action creates an action_logs entry
---
 C4/Accounts.pm                                     |  257 ++++++++++++++++----
 .../prog/en/modules/admin/preferences/logs.pref    |    2 +-
 2 files changed, 215 insertions(+), 44 deletions(-)

diff --git a/C4/Accounts.pm b/C4/Accounts.pm
index 5aa4fef..3d2a7dd 100644
--- a/C4/Accounts.pm
+++ b/C4/Accounts.pm
@@ -24,6 +24,9 @@ use C4::Context;
 use C4::Stats;
 use C4::Members;
 use C4::Circulation qw(ReturnLostItem);
+use C4::Log qw(logaction);
+
+use Data::Dumper qw(Dumper);
 
 use vars qw($VERSION @ISA @EXPORT);
 
@@ -100,12 +103,13 @@ sub recordpayment {
     # get lines with outstanding amounts to offset
     my $sth = $dbh->prepare(
         "SELECT * FROM accountlines
-  WHERE (borrowernumber = ?) AND (amountoutstanding<>0)
-  ORDER BY date"
+         WHERE (borrowernumber = ?) AND (amountoutstanding<>0)
+         ORDER BY date"
     );
     $sth->execute($borrowernumber);
 
     # offset transactions
+    my @ids;
     while ( ( $accdata = $sth->fetchrow_hashref ) and ( $amountleft > 0 ) ) {
         if ( $accdata->{'amountoutstanding'} < $amountleft ) {
             $newamtos = 0;
@@ -118,30 +122,48 @@ sub recordpayment {
         my $thisacct = $accdata->{accountlines_id};
         my $usth     = $dbh->prepare(
             "UPDATE accountlines SET amountoutstanding= ?
-     WHERE (accountlines_id = ?)"
+             WHERE (accountlines_id = ?)"
         );
         $usth->execute( $newamtos, $thisacct );
-        $usth->finish;
-#        $usth = $dbh->prepare(
-#            "INSERT INTO accountoffsets
-#     (borrowernumber, accountno, offsetaccount,  offsetamount)
-#     VALUES (?,?,?,?)"
-#        );
-#        $usth->execute( $borrowernumber, $accdata->{'accountno'},
-#            $nextaccntno, $newamtos );
-        $usth->finish;
+
+        if ( C4::Context->preference("FinesLog") ) {
+            $accdata->{'amountoutstanding_new'} = $newamtos;
+            logaction("FINES", 'MODIFY', $borrowernumber, Dumper({  
+                action                => 'fee_payment',
+                borrowernumber        => $accdata->{'borrowernumber'},
+                old_amountoutstanding => $accdata->{'amountoutstanding'},
+                new_amountoutstanding => $newamtos,
+                amount_paid           => $accdata->{'amountoutstanding'} - $newamtos,
+                accountlines_id       => $accdata->{'accountlines_id'},
+                accountno             => $accdata->{'accountno'},
+            }));
+            push( @ids, $accdata->{'accountlines_id'} );
+        }
     }
 
     # create new line
     my $usth = $dbh->prepare(
         "INSERT INTO accountlines
-  (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding,manager_id)
-  VALUES (?,?,now(),?,'Payment,thanks','Pay',?,?)"
+         (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding,manager_id)
+         VALUES (?,?,now(),?,'Payment,thanks','Pay',?,?)"
     );
     $usth->execute( $borrowernumber, $nextaccntno, 0 - $data, 0 - $amountleft, $manager_id );
-    $usth->finish;
+
     UpdateStats( $branch, 'payment', $data, '', '', '', $borrowernumber, $nextaccntno );
-    $sth->finish;
+
+    if ( C4::Context->preference("FinesLog") ) {
+        $accdata->{'amountoutstanding_new'} = $newamtos;
+        logaction("FINES", 'CREATE',$borrowernumber,Dumper({
+            action            => 'create_payment',
+            borrowernumber    => $borrowernumber,
+            accountno         => $nextaccntno,
+            amount            => $data * -1,
+            amountoutstanding => $amountleft * -1,
+            accounttype       => 'Pay',
+            accountlines_paid => \@ids,
+        }));
+    }
+
 }
 
 =head2 makepayment
@@ -180,7 +202,8 @@ sub makepayment {
     my $data = $sth->fetchrow_hashref;
     $sth->finish;
 
-    if($data->{'accounttype'} eq "Pay"){
+    my $payment;
+    if ( $data->{'accounttype'} eq "Pay" ){
         my $udp = 		
             $dbh->prepare(
                 "UPDATE accountlines
@@ -190,7 +213,7 @@ sub makepayment {
             );
         $udp->execute($accountlines_id);
         $udp->finish;
-    }else{
+    } else {
         my $udp = 		
             $dbh->prepare(
                 "UPDATE accountlines
@@ -201,8 +224,8 @@ sub makepayment {
         $udp->execute($accountlines_id);
         $udp->finish;
 
-         # create new line
-        my $payment = 0 - $amount;
+        # create new line
+        $payment = 0 - $amount;
         
         my $ins = 
             $dbh->prepare( 
@@ -214,12 +237,35 @@ sub makepayment {
         $ins->finish;
     }
 
+    if ( C4::Context->preference("FinesLog") ) {
+        logaction("FINES", 'MODIFY', $borrowernumber, Dumper({  
+            action                => 'fine_payment',
+            borrowernumber        => $borrowernumber,
+            old_amountoutstanding => $data->{'amountoutstanding'},
+            new_amountoutstanding => 0,
+            amount_paid           => $data->{'amountoutstanding'},
+            accountlines_id       => $data->{'accountlines_id'},
+            accountno             => $data->{'accountno'},
+        }));
+
+
+        logaction("FINES", 'CREATE',$borrowernumber,Dumper({
+            action            => 'create_payment',
+            borrowernumber    => $borrowernumber,
+            accountno         => $nextaccntno,
+            amount            => $payment,
+            amountoutstanding => 0,,
+            accounttype       => 'Pay',
+            accountlines_paid => [$data->{'accountlines_id'}],
+        }));
+    }
+
+
     # FIXME - The second argument to &UpdateStats is supposed to be the
     # branch code.
     # UpdateStats is now being passed $accountno too. MTJ
     UpdateStats( $user, 'payment', $amount, '', '', '', $borrowernumber,
         $accountno );
-    #from perldoc: for SELECT only #$sth->finish;
 
     #check to see what accounttype
     if ( $data->{'accounttype'} eq 'Rep' || $data->{'accounttype'} eq 'L' ) {
@@ -314,7 +360,20 @@ sub chargelostitem{
         $sth2->execute($borrowernumber,$accountno,$amount,
         $description,$amount,$itemnumber,$manager_id);
         $sth2->finish;
-    # FIXME: Log this ?
+
+        if ( C4::Context->preference("FinesLog") ) {
+            logaction("FINES", 'CREATE', $borrowernumber, Dumper({
+                action            => 'create_fee',
+                borrowernumber    => $borrowernumber,
+                accountno         => $accountno,
+                amount            => $amount,
+                amountoutstanding => $amount,
+                description       => $description,
+                accounttype       => 'L',
+                itemnumber        => $itemnumber,
+            }));
+        }
+
     }
 }
 
@@ -396,19 +455,36 @@ sub manualinvoice {
 
     if ( $itemnum ) {
         $desc .= ' ' . $itemnum;
-        my $sth = $dbh->prepare(
-            'INSERT INTO  accountlines
-                        (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding, itemnumber,notify_id, note, manager_id)
-        VALUES (?, ?, now(), ?,?, ?,?,?,?,?,?)');
-     $sth->execute($borrowernumber, $accountno, $amount, $desc, $type, $amountleft, $itemnum,$notifyid, $note, $manager_id) || return $sth->errstr;
-  } else {
-    my $sth=$dbh->prepare("INSERT INTO  accountlines
+        my $sth = $dbh->prepare("
+            INSERT INTO  accountlines
+            (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding, itemnumber,notify_id, note, manager_id)
+            VALUES (?, ?, now(), ?,?, ?,?,?,?,?,?)
+        ");
+        $sth->execute($borrowernumber, $accountno, $amount, $desc, $type, $amountleft, $itemnum,$notifyid, $note, $manager_id) || return $sth->errstr;
+    } else {
+        my $sth=$dbh->prepare("
+            INSERT INTO  accountlines
             (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding,notify_id, note, manager_id)
-            VALUES (?, ?, now(), ?, ?, ?, ?,?,?,?)"
-        );
-        $sth->execute( $borrowernumber, $accountno, $amount, $desc, $type,
-            $amountleft, $notifyid, $note, $manager_id );
+            VALUES (?, ?, now(), ?, ?, ?, ?,?,?,?)
+        ");
+        $sth->execute( $borrowernumber, $accountno, $amount, $desc, $type, $amountleft, $notifyid, $note, $manager_id );
+    }
+
+    if ( C4::Context->preference("FinesLog") ) {
+        logaction("FINES", 'CREATE',$borrowernumber,Dumper({
+            action            => 'create_fee',
+            borrowernumber    => $borrowernumber,
+            accountno         => $accountno,
+            amount            => $amount,
+            description       => $desc,
+            accounttype       => $type,
+            amountoutstanding => $amountleft,
+            notify_id         => $notifyid,
+            note              => $note,
+            itemnumber        => $itemnum
+        }));
     }
+
     return 0;
 }
 
@@ -653,7 +729,7 @@ sub ReversePayment {
     my ( $accountlines_id ) = @_;
     my $dbh = C4::Context->dbh;
 
-    my $sth = $dbh->prepare('SELECT amountoutstanding FROM accountlines WHERE accountlines_id = ?');
+    my $sth = $dbh->prepare('SELECT * FROM accountlines WHERE accountlines_id = ?');
     $sth->execute( $accountlines_id );
     my $row = $sth->fetchrow_hashref();
     my $amount_outstanding = $row->{'amountoutstanding'};
@@ -665,6 +741,25 @@ sub ReversePayment {
         $sth = $dbh->prepare('UPDATE accountlines SET amountoutstanding = 0, description = CONCAT( description, " Reversed -" ) WHERE accountlines_id = ?');
         $sth->execute( $accountlines_id );
     }
+
+    if ( C4::Context->preference("FinesLog") ) {
+        if ( $amount_outstanding <= 0 ) {
+            $row->{'amountoutstanding'} *= -1;
+        } else {
+            $row->{'amountoutstanding'} = '0';
+        }
+        $row->{'description'} .= ' Reversed -';
+        logaction("FINES", 'MODIFY', $row->{'borrowernumber'}, Dumper({  
+            action                => 'reverse_fee_payment',
+            borrowernumber        => $row->{'borrowernumber'},
+            old_amountoutstanding => $row->{'amountoutstanding'},
+            new_amountoutstanding => 0 - $amount_outstanding,,
+            accountlines_id       => $row->{'accountlines_id'},
+            accountno             => $row->{'accountno'},
+        }));
+
+    }
+
 }
 
 =head2 recordpayment_selectaccts
@@ -707,8 +802,12 @@ sub recordpayment_selectaccts {
     my $rows = $dbh->selectall_arrayref($sql, { Slice => {} }, $borrowernumber);
 
     # offset transactions
-    my $sth     = $dbh->prepare('UPDATE accountlines SET amountoutstanding= ? ' .
-        'WHERE accountlines_id=?');
+    my $sth = $dbh->prepare("
+        UPDATE accountlines SET amountoutstanding = ?
+        WHERE accountlines_id = ?
+    ");
+
+    my @ids;
     for my $accdata ( @{$rows} ) {
         if ($amountleft == 0) {
             last;
@@ -723,14 +822,43 @@ sub recordpayment_selectaccts {
         }
         my $thisacct = $accdata->{accountlines_id};
         $sth->execute( $newamtos, $thisacct );
+        
+        if ( C4::Context->preference("FinesLog") ) {
+            logaction("FINES", 'MODIFY', $borrowernumber, Dumper({  
+                action                => 'fee_payment',
+                borrowernumber        => $borrowernumber,
+                old_amountoutstanding => $accdata->{'amountoutstanding'},
+                new_amountoutstanding => $newamtos,
+                amount_paid           => $accdata->{'amountoutstanding'} - $newamtos,
+                accountlines_id       => $accdata->{'accountlines_id'},
+                accountno             => $accdata->{'accountno'},
+            }));
+            push( @ids, $accdata->{'accountlines_id'} );
+        }
+
     }
 
     # create new line
-    $sql = 'INSERT INTO accountlines ' .
-    '(borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding,manager_id) ' .
-    q|VALUES (?,?,now(),?,'Payment,thanks','Pay',?,?)|;
+    $sql = "
+        INSERT INTO accountlines
+        (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding,manager_id)
+        VALUES (?,?,now(),?,'Payment,thanks','Pay',?,?)
+    ";
     $dbh->do($sql,{},$borrowernumber, $nextaccntno, 0 - $amount, 0 - $amountleft, $manager_id );
     UpdateStats( $branch, 'payment', $amount, '', '', '', $borrowernumber, $nextaccntno );
+
+    if ( C4::Context->preference("FinesLog") ) {
+        logaction("FINES", 'CREATE',$borrowernumber,Dumper({
+            action            => 'create_payment',
+            borrowernumber    => $borrowernumber,
+            accountno         => $nextaccntno,
+            amount            => 0 - $amount,
+            amountoutstanding => 0 - $amountleft,
+            accounttype       => 'Pay',
+            accountlines_paid => \@ids,
+        }));
+    }
+
     return;
 }
 
@@ -748,13 +876,24 @@ sub makepartialpayment {
     my $nextaccntno = getnextacctno($borrowernumber);
     my $newamtos    = 0;
 
-    my $data = $dbh->selectrow_hashref(
-        'SELECT * FROM accountlines WHERE  accountlines_id=?',undef,$accountlines_id);
+    my $data = $dbh->selectrow_hashref('SELECT * FROM accountlines WHERE  accountlines_id=?',undef,$accountlines_id);
     my $new_outstanding = $data->{amountoutstanding} - $amount;
 
     my $update = 'UPDATE  accountlines SET amountoutstanding = ?  WHERE   accountlines_id = ? ';
     $dbh->do( $update, undef, $new_outstanding, $accountlines_id);
 
+    if ( C4::Context->preference("FinesLog") ) {
+        logaction("FINES", 'MODIFY', $borrowernumber, Dumper({  
+            action                => 'fee_payment',
+            borrowernumber        => $borrowernumber,
+            old_amountoutstanding => $data->{'amountoutstanding'},
+            new_amountoutstanding => $new_outstanding,
+            amount_paid           => $data->{'amountoutstanding'} - $new_outstanding,
+            accountlines_id       => $data->{'accountlines_id'},
+            accountno             => $data->{'accountno'},
+        }));
+    }
+
     # create new line
     my $insert = 'INSERT INTO accountlines (borrowernumber, accountno, date, amount, '
     .  'description, accounttype, amountoutstanding, itemnumber, manager_id) '
@@ -765,6 +904,18 @@ sub makepartialpayment {
 
     UpdateStats( $user, 'payment', $amount, '', '', '', $borrowernumber, $accountno );
 
+    if ( C4::Context->preference("FinesLog") ) {
+        logaction("FINES", 'CREATE',$borrowernumber,Dumper({
+            action            => 'create_payment',
+            borrowernumber    => $user,
+            accountno         => $nextaccntno,
+            amount            => 0 - $amount,
+            accounttype       => 'Pay',
+            itemnumber        => $data->{'itemnumber'},
+            accountlines_paid => [ $data->{'accountlines_id'} ],
+        }));
+    }
+
     return;
 }
 
@@ -783,7 +934,7 @@ C<$branch> is the branchcode of the library where the writeoff occurred.
 =cut
 
 sub WriteOffFee {
-    my ( $borrowernumber, $accountline_id, $itemnum, $accounttype, $amount, $branch ) = @_;
+    my ( $borrowernumber, $accountlines_id, $itemnum, $accounttype, $amount, $branch ) = @_;
     $branch ||= C4::Context->userenv->{branch};
     my $manager_id = 0;
     $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
@@ -799,7 +950,15 @@ sub WriteOffFee {
         WHERE accountlines_id = ? AND borrowernumber = ?
     ";
     $sth = $dbh->prepare( $query );
-    $sth->execute( $accountline_id, $borrowernumber );
+    $sth->execute( $accountlines_id, $borrowernumber );
+
+    if ( C4::Context->preference("FinesLog") ) {
+        logaction("FINES", 'MODIFY', $borrowernumber, Dumper({  
+            action                => 'fee_writeoff',
+            borrowernumber        => $borrowernumber,
+            accountlines_id       => $accountlines_id,
+        }));
+    }
 
     $query ="
         INSERT INTO accountlines
@@ -810,6 +969,18 @@ sub WriteOffFee {
     my $acct = getnextacctno($borrowernumber);
     $sth->execute( $borrowernumber, $acct, $itemnum, $amount, $manager_id );
 
+    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 ],
+        }));
+    }
+
     UpdateStats( $branch, 'writeoff', $amount, q{}, q{}, q{}, $borrowernumber );
 
 }
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/logs.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/logs.pref
index 67cb299..b6f22d0 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/logs.pref
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/logs.pref
@@ -28,7 +28,7 @@ Logging:
           choices:
               on: Log
               off: "Don't log"
-        - when overdue fines are charged or automatically forgiven.
+        - when fines are charged, paid, or forgiven.
     -
         - pref: IssueLog
           choices:
-- 
1.7.2.5