Bug 24081: Add discount action to boraccount
authorMartin Renvoize <martin.renvoize@ptfs-europe.com>
Tue, 3 Mar 2020 19:22:49 +0000 (19:22 +0000)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Wed, 4 Mar 2020 14:46:22 +0000 (14:46 +0000)
This patch adds the ability to apply discounts to existing patron
charges from the boraccount page.

Test plan
1/ Carry out some accounting actions that end up with debts on the
patron record. (Add a manual invoice for example)
2/ Note a new 'Apply discount' action button on the unpaid debt
3/ Paid debts do not display the action button
4/ Click the button
5/ Enter an amount into the resulting modal and click confirm
6/ Confirm that the outstanding debt has been reduced by the amount you
specified.
7/ Confirm that a new 'Discount' line appears on the patrons account
8/ Confirm that in the 'details' view of the debt that the discount line
was used against the debt to create the reduction.
9/ Signoff

Signed-off-by: Michal Denar <black23@gmail.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

koha-tmpl/intranet-tmpl/prog/en/modules/members/boraccount.tt
members/boraccount.pl

index c93eb6f..7301032 100644 (file)
@@ -90,6 +90,9 @@
         [% IF CAN_user_updatecharges_refund && account.is_debit && ( account.amountoutstanding != account.amount ) && !(account.status == 'REFUNDED') && !(account.debit_type_code == 'PAYOUT') %]
           <button type="button" data-toggle="modal" data-target="#issueRefundModal" data-item="[%- PROCESS account_type_description account=account -%]" data-accountline="[% account.accountlines_id | html %]" data-amount="[% account.amount | $Price %]" data-amountoutstanding="[% account.amountoutstanding | $Price %]" class="btn btn-default btn-xs"><i class="fa fa-money"></i> Issue refund</button>
         [% END %]
+        [% IF CAN_user_updatecharges_discount && account.is_debit && ( account.amountoutstanding == account.amount ) && !(account.debit_type_code == 'PAYOUT') %]
+          <button type="button" data-toggle="modal" data-target="#applyDiscountModal" data-item="[%- PROCESS account_type_description account=account -%]" data-accountline="[% account.accountlines_id | html %]" data-amount="[% account.amount | $Price %]" data-amountoutstanding="[% account.amountoutstanding | $Price %]" class="btn btn-default btn-xs"><i class="fa fa-percent"></i> Apply discount</button>
+        [% END %]
       </td>
     </tr>
 
                                 [% IF payment_types > 3 %]
                                 <li>
                                     <label for="transaction_type">Transaction type: </label>
-                                    <select name="transaction_type" id="transaction_type">
+                                    <select name="transaction_type" id="payout_transaction_type">
                                         [% FOREACH pt IN payment_types %]
                                             [% UNLESS excluded.grep("^$pt.authorised_value\$").size %]
                                             <option value="[% pt.authorised_value | html %]">[% pt.lib | html %]</option>
                                 [% SET payment_types = AuthorisedValues.GetAuthValueDropbox('PAYMENT_TYPE') %]
                                 <li>
                                     <label for="transaction_type">Transaction type: </label>
-                                    <select name="transaction_type" id="transaction_type">
+                                    <select name="transaction_type" id="refund_transaction_type">
                                         <option value="AC">Account credit</option>
                                         [% IF payment_types %]
                                         [% FOREACH pt IN payment_types %]
         </form> <!-- /#refund_form -->
     </div> <!-- /#issueRefundModal -->
 
+    <!-- Apply discount modal -->
+    <div class="modal" id="applyDiscountModal" tabindex="-1" role="dialog" aria-labelledby="applyDiscountLabel">
+        <form  id="discount_form" action="/cgi-bin/koha/members/boraccount.pl" method="get" enctype="multipart/form-data" class="validated">
+            <input type="hidden" name="accountlines_id" value="" id="discountline">
+            <input type="hidden" name="action" value="discount">
+            <input type="hidden" name="borrowernumber" value="[% account.borrowernumber | html %]">
+            <div class="modal-dialog" role="document">
+                <div class="modal-content">
+                    <div class="modal-header">
+                        <button type="button" class="closebtn" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
+                        <h4 class="modal-title" id="applyDiscountLabel">Apply discount</h4>
+                    </div>
+                    <div class="modal-body">
+                        <fieldset class="rows">
+                            <ol>
+                                <li>
+                                    <span id="item" class="label">Account type: </span><span></span>
+                                </li>
+                                <li>
+                                    <span id="charged" class="label">Amount charged: </span><span></span>
+                                </li>
+                                <li>
+                                    <label class="required" for="amount">Discount to apply: </label>
+                                    <input type="number" step="0.01" id="discount" name="amount" min="0.00" required="required">
+                                    <span class="required">Required</span>
+                                </li>
+                            </ol>
+                        </fieldset> <!-- /.rows -->
+                    </div> <!-- /.modal-body -->
+                    <div class="modal-footer">
+                        <input type="hidden" name="op" value="discount">
+                        <button type="submit" class="btn btn-default">Confirm</button>
+                        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
+                    </div> <!-- /.modal-footer -->
+                </div> <!-- /.modal-content -->
+            </div> <!-- /.modal-dialog -->
+        </form> <!-- /#discount_form -->
+    </div> <!-- /#applyDiscountModal -->
+
 [% MACRO jsinclude BLOCK %]
     [% INCLUDE 'datatables.inc' %]
     [% INCLUDE 'columns_settings.inc' %]
                 var amount = button.data('amount') * -1;
                 $("#paid + span").replaceWith(amount);
                 $("#amount").attr({ "value": amount, "max": amount });
-                $("#amount, #transaction_type").focus();
+                $("#amount, #payout_transaction_type").focus();
             });
 
             $("#issueRefundModal").on("shown.bs.modal", function(e){
                 var paid = amount - amountoutstanding;
                 $("#paid + span").replaceWith(paid);
                 $("#returned").attr({ "value": paid, "max": paid });
-                $("#returned, #transaction_type").focus();
+                $("#returned, #refund_transaction_type").focus();
+            });
+
+            $("#applyDiscountModal").on("shown.bs.modal", function(e){
+                var button = $(e.relatedTarget);
+                var item = button.data('item');
+                $("#item + span").replaceWith(item);
+                var accountline = button.data('accountline');
+                $('#discountline').val(accountline);
+                var amount = button.data('amount');
+                $("#charged + span").replaceWith(amount);
+                $("#discount").attr({ "max": amount });
+                $("#discount").focus();
             });
         });
     </script>
index 8dfe35d..dae6a5a 100755 (executable)
@@ -151,6 +151,26 @@ if ( $action eq 'refund' ) {
     );
 }
 
+if ( $action eq 'discount' ) {
+    my $charge_id        = scalar $input->param('accountlines_id');
+    my $charge           = Koha::Account::Lines->find($charge_id);
+    my $amount           = scalar $input->param('amount');
+    $schema->txn_do(
+        sub {
+
+            my $discount = $charge->reduce(
+                {
+                    reduction_type => 'DISCOUNT',
+                    branch         => $library_id,
+                    staff_id       => $logged_in_user->id,
+                    interface      => 'intranet',
+                    amount         => $amount
+                }
+            );
+        }
+    );
+}
+
 #get account details
 my $total = $patron->account->balance;