Bug 23354: Add receipting to Pay page
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / modules / pos / pay.tt
1 [% USE raw %]
2 [% USE Asset %]
3 [% USE Koha %]
4 [% USE AuthorisedValues %]
5 [% USE Price %]
6 [% SET footerjs = 1 %]
7 [% INCLUDE 'doc-head-open.inc' %]
8 <title>Koha &rsaquo; Payments</title>
9 [% INCLUDE 'doc-head-close.inc' %]
10 </head>
11
12 <body id="payments" class="pos">
13 [% INCLUDE 'header.inc' %]
14 [% INCLUDE 'circ-search.inc' %]
15
16 <div id="breadcrumbs"><a href="/cgi-bin/koha/mainpage.pl">Home</a> &rsaquo; Point of sale</div>
17
18 <div class="main container-fluid">
19     <div class="row">
20         <div class="col-sm-10 col-sm-push-2">
21
22         [% IF ( error_registers ) %]
23         <div id="error_message" class="dialog alert">
24             You must have at least one cash register associated with this branch before you can record payments.
25         </div>
26         [% ELSE %]
27         <form name="payForm" id="payForm" method="post" action="/cgi-bin/koha/pos/pay.pl">
28             <div class="row">
29
30                 <div class="col-sm-6">
31
32                     <fieldset class="rows">
33                         <legend>This sale</legend>
34                         <p>Click to edit item cost or quantities</p>
35                         <table id="sale" class="table_sale">
36                             <thead>
37                                 <tr>
38                                     <th>Item</th>
39                                     <th>Cost</th>
40                                     <th>Quantity</th>
41                                     <th>Total</th>
42                                 </tr>
43                             </thead>
44                             <tbody>
45                             </tbody>
46                             <tfoot>
47                                 <tr>
48                                     <td colspan="3">Total payable:</td>
49                                     <td></td>
50                                 </tr>
51                             </tfoot>
52                         </table>
53                     </fieldset>
54
55                     <fieldset class="rows">
56                         <legend>Collect payment</legend>
57                         <ol>
58                             <li>
59                                 <label for="paid">Amount being paid: </label>
60                                 <input type="number" min="0.00" max="10000.00" step="0.01" name="paid" id="paid" value="[% amountoutstanding | $Price on_editing => 1 %]" readonly/>
61                             </li>
62                             <li>
63                                 <label for="collected">Collected from patron: </label>
64                                 <input type="number" min="0.00" max="10000.00" step="0.01" name="collected" id="collected" value=""/>
65                             </li>
66                             <li>
67                                 <label>Change to give: </label>
68                                 <span id="change">0.00</span>
69                                 <input type="hidden" name="change" value="0.00"/>
70                             </li>
71
72                             [% SET payment_types = AuthorisedValues.GetAuthValueDropbox('PAYMENT_TYPE') %]
73                             [% IF payment_types %]
74                             <li>
75                                 <label for="payment_type">Payment type: </label>
76                                 <select name="payment_type" id="payment_type">
77                                     [% FOREACH pt IN payment_types %]
78                                         <option value="[% pt.authorised_value | html %]">[% pt.lib | html %]</option>
79                                     [% END %]
80                                 </select>
81                             </li>
82                             [% END %]
83
84                             [% IF Koha.Preference('UseCashRegisters') %]
85                             <li>
86                                 <label for="cash_register">Cash register: </label>
87                                 <select name="cash_register" id="cash_register">
88                                     [% FOREACH register IN registers %]
89                                       [% IF register.id == registerid %]
90                                     <option value="[% register.id %]" selected="selected">[% register.name | html %]</option>
91                                       [% ELSE %]
92                                     <option value="[% register.id %]">[% register.name | html %]</option>
93                                       [% END %]
94                                     [% END %]
95                                 </select>
96                             </li>
97                             [% END %]
98                         </ol>
99
100                     </fieldset>
101                 </div>
102
103                 <div class="col-sm-6">
104                     <fieldset class="rows">
105                         <legend>Items for purchase</legend>
106                             [% SET invoice_types = AuthorisedValues.GetAuthValueDropbox('MANUAL_INV') %]
107                             [% IF invoice_types %]
108                             <table id="invoices">
109                             <thead>
110                                 <tr>
111                                     <th>Code</th>
112                                     <th>Description</th>
113                                     <th>Cost</th>
114                                     <th>Action</th>
115                                 </tr>
116                             </thead>
117                             <tbody>
118                             [% FOREACH invoice IN invoice_types %]
119                                 <tr>
120                                     <td>[% invoice.authorised_value | html %]</td>
121                                     <td>[% invoice.lib_opac | html %]</td>
122                                     <td>[% invoice.lib | html %]</td>
123                                     <td>
124                                         <button class="add_button" data-invoice-code="[% invoice.lib_opac %]" data-invoice-title="[% invoice.authorised_value | html %]" data-invoice-price="[% invoice.lib | html %]"><i class="fa fa-plus"></i> Add</button>
125                                     </td>
126                                 </tr>
127                             [% END %]
128                             </table>
129                             [% ELSE %]
130                             You have no manual invoice types defined
131                             [% END %]
132                     </fieldset>
133                 </div>
134
135                 <div class="action">
136                     <input type="submit" name="submitbutton" value="Confirm" />
137                     <a class="cancel" href="/cgi-bin/koha/pos/pay.pl">Cancel</a>
138                 </div>
139             </div>
140         </form>
141         [% END %]
142     </div>
143
144     <div class="col-sm-2 col-sm-pull-10">
145         <aside>
146             [% INCLUDE 'pos-menu.inc' %]
147         </aside>
148     </div>
149 </div> <!-- /.row -->
150
151 <!-- Modal -->
152 <div id="confirm_change_form" class="modal" tabindex="-1" role="dialog" aria-hidden="true">
153     <div class="modal-dialog">
154         <div class="modal-content">
155             <div class="modal-header">
156                 <h3>The amount collected is more than the outstanding charge</h3>
157             </div>
158             <div class="modal-body">
159                 <p>The amount collected from the patron is higher than the amount to be paid.</p>
160                 <p>The change to give is <b><span id="modal_change">0.00</span></b>.</p>
161                 <p>Confirm this payment?</p>
162             </div>
163             <div class="modal-footer">
164                 <button class="btn btn-default approve" id="modal_submit" type="button"><i class="fa fa-check"></i> Yes</button>
165                 <button class="btn btn-default deny cancel" href="#" data-dismiss="modal" aria-hidden="true"><i class="fa fa-times"></i> No</button>
166             </div>
167         </div>
168     </div>
169 </div>
170
171 [% IF payment_id && Koha.Preference('FinePaymentAutoPopup') %]
172 <!-- Automatic Print Reciept -->
173       <a id="printReciept" style="display: none" href="#"></a>
174 [% END %]
175
176 [% MACRO jsinclude BLOCK %]
177     [% Asset.js("js/admin-menu.js") | $raw %]
178     [% INCLUDE 'datatables.inc' %]
179     [% Asset.js("lib/jquery/plugins/jquery.jeditable.mini.js") | $raw %]
180     <script>
181     function fnClickAddRow( table, invoiceTitle, invoicePrice ) {
182       table.fnAddData( [
183         invoiceTitle,
184         invoicePrice,
185         1,
186         null
187          ]
188       );
189     }
190
191     function moneyFormat(textObj) {
192         var newValue = textObj.value;
193         var decAmount = "";
194         var dolAmount = "";
195         var decFlag   = false;
196         var aChar     = "";
197
198         for(var i=0; i < newValue.length; i++) {
199             aChar = newValue.substring(i, i+1);
200             if (aChar >= "0" && aChar <= "9") {
201                 if(decFlag) {
202                     decAmount = "" + decAmount + aChar;
203                 }
204                 else {
205                     dolAmount = "" + dolAmount + aChar;
206                 }
207             }
208             if (aChar == ".") {
209                 if (decFlag) {
210                     dolAmount = "";
211                     break;
212                 }
213                 decFlag = true;
214             }
215         }
216
217         if (dolAmount == "") {
218             dolAmount = "0";
219         }
220     // Strip leading 0s
221         if (dolAmount.length > 1) {
222             while(dolAmount.length > 1 && dolAmount.substring(0,1) == "0") {
223                 dolAmount = dolAmount.substring(1,dolAmount.length);
224             }
225         }
226         if (decAmount.length > 2) {
227             decAmount = decAmount.substring(0,2);
228         }
229     // Pad right side
230         if (decAmount.length == 1) {
231            decAmount = decAmount + "0";
232         }
233         if (decAmount.length == 0) {
234            decAmount = decAmount + "00";
235         }
236
237         textObj.value = dolAmount + "." + decAmount;
238     }
239
240     function updateChangeValues() {
241         var change = $('#change')[0];
242         change.innerHTML = Math.round(($('#collected')[0].value - $('#paid')[0].value) * 100) / 100;
243         if (change.innerHTML <= 0) {
244             change.innerHTML = "0.00";
245         } else {
246             change.value = change.innerHTML;
247             moneyFormat(change);
248             change.innerHTML = change.value;
249         }
250
251         $(':input[name="change"]').val(change.value);
252         $('#modal_change').html(change.innerHTML);
253     }
254
255     $(document).ready(function() {
256         var sale_table = $("#sale").dataTable($.extend(true, {}, dataTablesDefaults, {
257             "bPaginate": false,
258             "bFilter": false,
259             "bInfo": false,
260             "bAutoWidth": false,
261             "aoColumnDefs": [{
262                 "aTargets": [-2],
263                 "bSortable": false,
264                 "bSearchable": false,
265             }, {
266                 "aTargets": [-1],
267                 "mRender": function ( data, type, full ) {
268                     var price = Number.parseFloat(data).toFixed(2);
269                     return '£'+price;
270                 }
271             }, {
272                 "aTargets": [-2, -3],
273                 "sClass" : "editable",
274             }],
275             "aaSorting": [
276                 [1, "asc"]
277             ],
278             "fnDrawCallback": function (oSettings) {
279                 var local = this;
280                 local.$('.editable').editable( function(value, settings) {
281                     var aPos = local.fnGetPosition( this );
282                     local.fnUpdate( value, aPos[0], aPos[1], true, false );
283                     return value;
284                 },{
285                     type    : 'text'
286                 });
287             },
288             "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
289                 var iTotal = aData[1] * aData[2];
290                 this.fnUpdate( iTotal, nRow, 3, false, false );
291             },
292             "fnFooterCallback": function(nFoot, aData, iStart, iEnd, aiDisplay) {
293                 var iTotalPrice = 0;
294                 for ( var i=0 ; i<aData.length ; i++ )
295                             {
296                                     iTotalPrice += aData[i][3]*1;
297                             }
298
299                 iTotalPrice = Number.parseFloat(iTotalPrice).toFixed(2);
300                 nFoot.getElementsByTagName('td')[1].innerHTML = iTotalPrice;
301                 $('#paid').val(iTotalPrice);
302                 $('#paid').trigger('change');
303             }
304         }));
305
306         var items_table = $("#invoices").dataTable($.extend(true,{}, dataTablesDefaults, {
307                "aoColumnDefs": [
308                   { "aTargets": [ -1, -2 ], "bSortable": false, "bSearchable":false },
309                ],
310                "aaSorting": [[ 0, "asc" ]],
311                "paginationType": "full",
312         }));
313
314         $(".add_button").on("click", function(e) {
315             e.preventDefault();
316             fnClickAddRow(sale_table, $( this ).data('invoiceTitle'), $( this ).data('invoicePrice') );
317             items_table.fnFilter( '' );
318         });
319
320         // Change calculation and modal
321         var change = $('#change')[0];
322         $("#paid, #collected").on("change",function() {
323             moneyFormat( this );
324             if (change != undefined) {
325                 updateChangeValues();
326             }
327         });
328
329         var checked = false;
330         $('#modal_submit').click(function() {
331             checked = true;
332             $('#payForm').submit();
333         });
334
335         $('#payForm').submit(function(e){
336             if (change != undefined && change.innerHTML > 0.00 && !checked) {
337                 e.preventDefault();
338                 $("#confirm_change_form").modal("show");
339             } else {
340                 var rows = sale_table.fnGetData();
341                 rows.forEach(function (row, index) {
342                     var sale = {
343                         code: row[0],
344                         price: row[1],
345                         quantity: row[2]
346                     };
347                     $('<input>').attr({
348                         type: 'hidden',
349                         name: 'sales',
350                         value: JSON.stringify(sale)
351                     }).appendTo('#payForm');
352                 });
353                 return true;
354             }
355         });
356
357         [% IF payment_id && Koha.Preference('FinePaymentAutoPopup') %]
358             $("#printReciept").click(function() {
359                 var win = window.open('/cgi-bin/koha/pos/printreceipt.pl?action=print&accountlines_id=[% payment_id | uri %]&collected=[% collected | uri %]&change=[% change | uri %]', '_blank');
360                 win.focus();
361             });
362             $("#printReciept").click();
363         [% END %]
364     });
365     </script>
366 [% END %]
367
368 [% INCLUDE 'intranet-bottom.inc' %]