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