From 253e409aa6226d7f5853c9473d4cc3cd0d14117f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9rick?= <frederick.capovilla@libeo.com>
Date: Tue, 10 Dec 2013 16:51:39 -0500
Subject: [PATCH] Bug 11373: Add a "cash register" feature to the fine payment form.

If the amount received is greater than the amount to pay, a message is
shown to tell the employee how much money to give back.

Signed-off-by: Joel Aloi <aloi54@live.fr>
---
 C4/Accounts.pm                                     |   12 ++-
 .../prog/en/modules/members/paycollect.tt          |   88 ++++++++++++++++----
 members/paycollect.pl                              |   34 ++++++--
 3 files changed, 104 insertions(+), 30 deletions(-)

diff --git a/C4/Accounts.pm b/C4/Accounts.pm
index 3fbb2ad..e474568 100644
--- a/C4/Accounts.pm
+++ b/C4/Accounts.pm
@@ -696,11 +696,12 @@ sub makepartialpayment {
     }
 
     # create new line
+    my $payment = 0 - $amount;
     my $insert = 'INSERT INTO accountlines (borrowernumber, accountno, date, amount, '
     .  'description, accounttype, amountoutstanding, itemnumber, manager_id, note) '
     . ' VALUES (?, ?, now(), ?, ?, ?, 0, ?, ?, ?)';
 
-    $dbh->do(  $insert, undef, $borrowernumber, $nextaccntno, $amount,
+    $dbh->do(  $insert, undef, $borrowernumber, $nextaccntno, $payment,
         "Payment, thanks - $user", 'Pay', $data->{'itemnumber'}, $manager_id, $payment_note);
 
     UpdateStats( $user, 'payment', $amount, '', '', '', $borrowernumber, $accountno );
@@ -710,7 +711,7 @@ sub makepartialpayment {
             action            => 'create_payment',
             borrowernumber    => $user,
             accountno         => $nextaccntno,
-            amount            => 0 - $amount,
+            amount            => $payment,
             accounttype       => 'Pay',
             itemnumber        => $data->{'itemnumber'},
             accountlines_paid => [ $data->{'accountlines_id'} ],
@@ -772,14 +773,15 @@ sub WriteOffFee {
     ";
     $sth = $dbh->prepare( $query );
     my $acct = getnextacctno($borrowernumber);
-    $sth->execute( $borrowernumber, $acct, $itemnum, $amount, $manager_id, $payment_note );
+    my $payment = 0 - $amount;
+    $sth->execute( $borrowernumber, $acct, $itemnum, $payment, $manager_id, $payment_note );
 
     if ( C4::Context->preference("FinesLog") ) {
         logaction("FINES", 'CREATE',$borrowernumber,Dumper({
             action            => 'create_writeoff',
             borrowernumber    => $borrowernumber,
             accountno         => $acct,
-            amount            => 0 - $amount,
+            amount            => $payment,
             accounttype       => 'W',
             itemnumber        => $itemnum,
             accountlines_paid => [ $accountlines_id ],
@@ -787,7 +789,7 @@ sub WriteOffFee {
         }));
     }
 
-    UpdateStats( $branch, 'writeoff', $amount, q{}, q{}, q{}, $borrowernumber );
+    UpdateStats( $branch, 'writeoff', $payment, q{}, q{}, q{}, $borrowernumber );
 
 }
 
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/paycollect.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/paycollect.tt
index 9cec275..8b897ee 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/paycollect.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/paycollect.tt
@@ -3,13 +3,6 @@
 [% INCLUDE 'doc-head-close.inc' %]
 <script type= "text/javascript">
 //<![CDATA[
-$(document).ready(function() {
-    $('#payindivfine, #woindivfine, #payfine').preventDoubleFormSubmit();
-});
-//]]>
-</script>
-<script type= "text/javascript">
-//<![CDATA[
 function moneyFormat(textObj) {
     var newValue = textObj.value;
     var decAmount = "";
@@ -58,6 +51,10 @@ function moneyFormat(textObj) {
 
     textObj.value = dolAmount + "." + decAmount;
 }
+
+$(document).ready(function() {
+    $('#payform, #woindivfine').preventDoubleFormSubmit();
+});
 //]]>
 </script>
 </head>
@@ -91,14 +88,32 @@ function moneyFormat(textObj) {
     </li>
 </ul>
 <div class="tabs-container">
-[% IF ( error_over ) %]
+[% IF (error_negative) %]
     <div id="error_message" class="dialog alert">
-    You must pay a value less than or equal to [% total_due | format('%.2f') %].
+        <span>The amount paid can't be negative!</span>
     </div>
 [% END %]
 
+[% IF (error_collected_less) %]
+    <div id="error_message" class="dialog alert">
+        <span>The amount collected can't be lower than the amount paid!</span>
+    </div>
+[% END %]
+
+[% UNLESS (writeoff_individual) %]
+    <form name="payform" id="payform" action="/cgi-bin/koha/members/paycollect.pl" method="post">
+[% END %]
+
+[% IF (give_change) %]
+    <div id="change_message" class="dialog message">
+        <span>The amount collected is greater than the total amount paid.</span><br />
+        <strong>Change to give back: [% give_change %]</strong><br /><br />
+        <input type="submit" name="submitbutton" value="Confirm payment" />
+        <a class="cancel" href="/cgi-bin/koha/members/pay.pl?borrowernumber=[% borrower.borrowernumber %]">Cancel</a>
+     </div>
+[% END %]
+
 [% IF ( pay_individual ) %]
-    <form name="payindivfine" id="payindivfine" onsubmit="return validatePayment(this);" method="post" action="/cgi-bin/koha/members/paycollect.pl">
     <input type="hidden" name="borrowernumber" id="borrowernumber" value="[% borrower.borrowernumber %]" />
     <input type="hidden" name="pay_individual" id="pay_individual" value="[% pay_individual %]" />
     <input type="hidden" name="itemnumber" id="itemnumber" value="[% itemnumber %]" />
@@ -140,12 +155,32 @@ function moneyFormat(textObj) {
 </table>
 
 <ol>
-
+    [% IF ( give_change ) %]
     <li>
-        <label for="paid">Collect from patron: </label>
-            <!-- default to paying all -->
-        <input name="paid" id="paid" value="[% amountoutstanding | format('%.2f') %]" onchange="moneyFormat(document.payindivfine.paid)"/>
+        <label for="paid">Amount paid:</label>
+        <input type="hidden" name="paid" id="paid" value="[% amount_paid %]" />
+        <input type="hidden" name="collected" id="collected" value="[% amount_paid %]" />
+        [% amount_paid %]
     </li>
+    <li>
+        <label>Collect from patron:</label>
+        [% amount_collected %]
+    </li>
+    <li>
+        <label>Change to give back:</label>
+        [% give_change %]
+    </li>
+    [% ELSE %]
+    <li>
+        <label for="paid">Amount paid :</label>
+        <input name="paid" id="paid" value="[% amountoutstanding | format('%.2f') %]" onchange="moneyFormat(document.payform.paid)"/>
+        [% amount_paid %]
+    </li>
+    <li>
+        <label for="collected">Collect from patron:</label>
+        <input name="collected" id="collected" value="[% amountoutstanding | format('%.2f') %]" onchange="moneyFormat(document.payform.collected)" />
+    </li>
+    [% END %]
 </ol>
 </fieldset>
 
@@ -194,7 +229,6 @@ function moneyFormat(textObj) {
     </form>
 [% ELSE %]
 
-    <form name="payfine" id="payfine" onsubmit="return validatePayment(this);" method="post" action="/cgi-bin/koha/members/paycollect.pl">
     <input type="hidden" name="borrowernumber" id="borrowernumber" value="[% borrower.borrowernumber %]" />
     <input type="hidden" name="selected_accts" id="selected_accts" value="[% selected_accts %]" />
     <input type="hidden" name="total" id="total" value="[% total %]" />
@@ -206,11 +240,31 @@ function moneyFormat(textObj) {
             <span class="label">Total amount outstanding: </span>
             <span class="debit">[% total | format('%.2f') %]</span>
         </li>
+    [% IF ( give_change ) %]
+    <li>
+        <label for="paid">Amount paid:</label>
+        <!-- default to paying all -->
+        <input name="paid" id="paid" value="[% total | format('%.2f') %]" onchange="moneyFormat(document.payform.paid)"/>
+    </li>
+    <li>
+        <label>Collect from patron:</label>
+        [% amount_collected %]
+    </li>
+    <li>
+        <label>Change to give back:</label>
+        [% give_change %]
+    </li>
+    [% ELSE %]
+    <li>
+        <label for="paid">Amount paid :</label>
+        <input name="paid" id="paid" value="[% total | format('%.2f') %]" onchange="moneyFormat(document.payform.paid)" />
+    </li>
     <li>
-        <label for="paid">Collect from patron: </label>
+        <label for="collected">Collect from patron:</label>
         <!-- default to paying all -->
-        <input name="paid" id="paid" value="[% total | format('%.2f') %]" onchange="moneyFormat(document.payfine.paid)"/>
+        <input name="collected" id="collected" value="[% total | format('%.2f') %]" onchange="moneyFormat(document.payform.collected)"/>
     </li>
+    [% END %]
     <li>
         <label for="selected_accts_notes">Note: </label>
         <textarea name="selected_accts_notes" id="selected_accts_notes">[% selected_accts_notes %]</textarea>
diff --git a/members/paycollect.pl b/members/paycollect.pl
index 96ca0fb..18a6c5d 100755
--- a/members/paycollect.pl
+++ b/members/paycollect.pl
@@ -51,11 +51,11 @@ my $branch = GetBranch( $input, GetBranches() );
 
 my ( $total_due, $accts, $numaccts ) = GetMemberAccountRecords($borrowernumber);
 my $total_paid = $input->param('paid');
+my $total_collected = $input->param('collected');
 
 my $individual   = $input->param('pay_individual');
 my $writeoff     = $input->param('writeoff_individual');
-my $select_lines = $input->param('selected');
-my $select       = $input->param('selected_accts');
+my $select       = ($input->param('selected') || $input->param('selected_accts'));
 my $payment_note = uri_unescape $input->param('payment_note');
 my $accountno;
 my $accountlines_id;
@@ -89,20 +89,38 @@ if ( $individual || $writeoff ) {
         notify_level      => $notify_level,
         payment_note    => $payment_note,
     );
-} elsif ($select_lines) {
-    $total_due = $input->param('amt');
+} elsif ($select) {
+    $total_due = ($input->param('amt') || $input->param('total'));
     $template->param(
-        selected_accts => $select_lines,
+        selected_accts => $select,
         amt            => $total_due,
         selected_accts_notes => $input->param('notes'),
     );
 }
 
 if ( $total_paid and $total_paid ne '0.00' ) {
-    if ( $total_paid < 0 or $total_paid > $total_due ) {
+    if ( $total_paid < 0 ) {
+        $template->param( 
+            error_negative => 1,
+        );
+    }
+    elsif (($total_collected - $total_paid) < 0) {
+        $template->param( 
+            error_collected_less => 1,
+        );
+    }
+    elsif ($total_paid > $total_due) {
         $template->param(
-            error_over => 1,
-            total_due => $total_due
+            amount_paid => sprintf('%.2f', $total_due),
+            amount_collected => sprintf('%.2f', $total_collected),
+            give_change => sprintf('%.2f',($total_collected-$total_due))
+        );
+    }
+    elsif ($total_paid < $total_due and $total_collected ne $total_paid) {
+         $template->param(
+            amount_paid => sprintf('%.2f', $total_paid),
+            amount_collected => sprintf('%.2f', $total_collected),
+            give_change => sprintf('%.2f',($total_collected-$total_paid))
         );
     } else {
         if ($individual) {
-- 
1.7.2.5