Bug 29002: Tidy
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / modules / pos / pay.tt
1 [% USE raw %]
2 [% USE AdditionalContents %]
3 [% USE Asset %]
4 [% USE Branches %]
5 [% USE Koha %]
6 [% USE Price %]
7 [% USE TablesSettings %]
8 [% USE Registers %]
9 [% PROCESS 'i18n.inc' %]
10 [% SET footerjs = 1 %]
11 [% INCLUDE 'doc-head-open.inc' %]
12 [% SET registers = Registers.all( filters => { current_branch => 1 } ) %]
13 <title>[% FILTER collapse %]
14     [% t("Point of sale") | html %] &rsaquo;
15     [% t("Koha") | html %]
16 [% END %]</title>
17 [% INCLUDE 'doc-head-close.inc' %]
18 </head>
19
20 <body id="payments" class="pos">
21 [% WRAPPER 'header.inc' %]
22     [% INCLUDE 'circ-search.inc' %]
23 [% END %]
24
25 [% WRAPPER 'sub-header.inc' %]
26     [% WRAPPER breadcrumbs %]
27         [% WRAPPER breadcrumb_item bc_active= 1 %]
28             <span>Point of sale</span>
29         [% END %]
30     [% END #/ WRAPPER breadcrumbs %]
31 [% END #/ WRAPPER sub-header.inc %]
32
33 <div class="main container-fluid">
34     <div class="row">
35         <div class="col-md-10 col-md-push-2">
36
37             <h1>Point of sale</h1>
38
39         [% IF ( registers.size == 0 ) %]
40             <div id="error_message" class="dialog message">
41                 <p>
42                     You must have at least one cash register associated with the library before you can record payments.
43                 </p>
44                 [% IF ( CAN_user_parameters_manage_cash_registers ) %]
45                     <form action="/cgi-bin/koha/admin/cash_registers.pl" method="get">
46                         <input type="hidden" name="op" value="add_form" />
47                         <button class="new" type="submit"><i class="fa fa-plus"></i> Create a new cash register</button>
48                     </form>
49                 [% END %]
50             </div>
51         [% ELSE %]
52
53         [% IF payment_id && !Koha.Preference('FinePaymentAutoPopup') %]
54         <div class="dialog alert audio-alert-action">
55             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> <a type="button" data-toggle="modal" data-target="#emailReceiptModal" class="btn btn-default btn-xs"><i class="fa-solid fa-envelope"></i> Email receipt</a>
56         </div>
57         [% END %]
58
59         <form name="payForm" id="payForm" method="post" action="/cgi-bin/koha/pos/pay.pl">
60             <div class="row">
61
62                 <div class="col-md-6">
63                     <fieldset class="rows">
64                         <legend>Items for purchase</legend>
65                             Please select items from below to add to this transaction:
66                             [% IF invoice_types %]
67                             <table id="invoices">
68                             <thead>
69                                 <tr>
70                                     <th>Code</th>
71                                     <th>Description</th>
72                                     <th class="NoSort">Cost</th>
73                                     <th class="NoSort">Action</th>
74                                 </tr>
75                             </thead>
76                             <tbody>
77                             [% FOREACH invoice IN invoice_types %]
78                                 <tr>
79                                     <td>[% invoice.code | html %]</td>
80                                     <td>[% invoice.description | html %]</td>
81                                     <td>[% invoice.default_amount | $Price %]</td>
82                                     <td>
83                                         <button type="button" class="btn btn-default btn-xs 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>
84                                     </td>
85                                 </tr>
86                             [% END %]
87                             </table>
88                             [% ELSE %]
89                             You have no manual invoice types defined
90                             [% END %]
91                     </fieldset>
92                 </div>
93
94                 <div class="col-md-6">
95
96                     <fieldset class="rows">
97                         <legend>This sale</legend>
98                         <p>Click to edit item cost or quantities</p>
99                         <table id="sale" class="table_sale">
100                             <thead>
101                                 <tr>
102                                     <th>Item</th>
103                                     <th>Cost</th>
104                                     <th>Quantity</th>
105                                     <th>Total</th>
106                                     <th>Action</th>
107                                     <th>CODE</th>
108                                 </tr>
109                             </thead>
110                             <tbody>
111                             </tbody>
112                             <tfoot>
113                                 <tr>
114                                     <td colspan="3">Total payable:</td>
115                                     <td></td>
116                                     <td></td>
117                                     <td></td>
118                                 </tr>
119                             </tfoot>
120                         </table>
121                     </fieldset>
122
123                     <fieldset class="rows">
124                         <legend>Collect payment</legend>
125                         <ol>
126                             <li>
127                                 <label for="paid">Amount being paid: </label>
128                                 <input type="text" inputmode="none" pattern="[0-9]*" name="paid" id="paid" value="" readonly/>
129                             </li>
130                             <li>
131                                 <label for="collected" class="required">Amount tendered: </label>
132                                 <input type="text" inputmode="numeric" pattern="[0-9]*" name="collected" id="collected" value="" class="required" required="required" />
133                                 <span class="required">Required</span>
134                             </li>
135                             <li>
136                                 <label>Change to give: </label>
137                                 <span id="change">[% 0 | $Price %]</span>
138                                 <input type="hidden" name="change" value="[% 0 | $Price %]"/>
139                             </li>
140
141                             [% INCLUDE 'transaction_types.inc' type="payment" %]
142
143                             <li>
144                                 <label for="registerid" class="required">Cash register: </label>
145                                 <select name="registerid" id="registerid" class="required" required="required">
146                                     <option id="noregister" disabled selected="selected" value="">-- Select an option--</option>
147                                     [% PROCESS options_for_registers %]
148                                 </select>
149                                 <span class="required">Required</span>
150                             </li>
151                         </ol>
152
153                     </fieldset>
154
155                     <div class="action">
156                         <input type="submit" id="submitbutton" name="submitbutton" class="btn btn-primary" value="Confirm" />
157                         <a class="cancel" href="/cgi-bin/koha/pos/pay.pl">Cancel</a>
158                     </div>
159                 </div>
160             </div>
161         </form>
162         [% END %]
163
164         [%- SET StaffPOSHome = AdditionalContents.get( location => "StaffPOSHome", lang => lang, library => logged_in_user.branchcode ) -%]
165         [%- FOREACH block IN StaffPOSHome.content -%]
166         <div class="page-section">
167             [%- block.content | $raw -%]
168         </div>
169         [%- END -%]
170
171     </div>
172
173     <div class="col-md-2 col-md-pull-10">
174         <aside>
175             [% INCLUDE 'pos-menu.inc' %]
176         </aside>
177     </div>
178 </div> <!-- /.row -->
179
180 <!-- Email receipt modal -->
181 <div class="modal" id="emailReceiptModal" tabindex="-1" role="dialog" aria-labelledby="emailReceiptLabel">
182     <form id="email_form" action="/cgi-bin/koha/pos/pay.pl" method="get" enctype="multipart/form-data" class="validated">
183         <input type="hidden" name="payment_id" id="payment_id" value="[% payment_id | uri %]">
184         <input type="hidden" name="collected" id="collected" value="[% collected | uri %]">
185         <input type="hidden" name="change" id="change" value="[% change | uri %]">"
186         <div class="modal-dialog" role="document">
187             <div class="modal-content">
188                 <div class="modal-header">
189                     <button type="button" class="closebtn" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
190                     <h4 class="modal-title" id="emailReceiptLabel">Email receipt</h4>
191                 </div>
192                 <div class="modal-body">
193                     <fieldset class="rows">
194                         <ol>
195                             <li>
196                                 <label class="required" for="toaddr">Email address: </label>
197                                 <input type="email" id="toaddr" name="toaddr" required="required">
198                                 <span class="required">Required</span>
199                             </li>
200                         </ol>
201                     </fieldset> <!-- /.rows -->
202                 </div> <!-- /.modal-body -->
203                 <div class="modal-footer">
204                     <input type="hidden" name="action" value="send">
205                     <button type="submit" class="btn btn-default">Confirm</button>
206                     <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
207                 </div> <!-- /.modal-footer -->
208             </div> <!-- /.modal-content -->
209         </div> <!-- /.modal-dialog -->
210     </form> <!-- /#email_form -->
211 </div> <!-- /#emailReceiptModal
212
213 <!-- Change modal -->
214 <div id="confirm_change_form" class="modal" tabindex="-1" role="dialog" aria-hidden="true">
215     <div class="modal-dialog">
216         <div class="modal-content">
217             <div class="modal-header">
218                 <h3>The amount collected is more than the outstanding charge</h3>
219             </div>
220             <div class="modal-body">
221                 <p>The amount collected from the patron is higher than the amount to be paid.</p>
222                 <p>The change to give is <strong><span id="modal_change">[% 0 | $Price %]</span></strong>.</p>
223                 <p>Confirm this payment?</p>
224             </div>
225             <div class="modal-footer">
226                 <button class="btn btn-default approve" id="modal_submit" type="button"><i class="fa fa-check"></i> Yes</button>
227                 <button class="btn btn-default deny cancel" href="#" data-dismiss="modal" aria-hidden="true" type="button"><i class="fa fa-times"></i> No</button>
228             </div>
229         </div>
230     </div>
231 </div>
232
233 [% IF payment_id && Koha.Preference('FinePaymentAutoPopup') %]
234     <!-- Automatic Print Receipt -->
235     <a id="printReceipt" style="display: none" href="#"></a>
236 [% END %]
237
238 [% MACRO jsinclude BLOCK %]
239     [% INCLUDE 'format_price.inc' %]
240     [% INCLUDE 'datatables.inc' %]
241     [% INCLUDE 'columns_settings.inc' %]
242     [% Asset.js("lib/jquery/plugins/jquery.jeditable.mini.js") | $raw %]
243     <script>
244     function moneyFormat(textObj) {
245         var newValue = textObj.value;
246         var decAmount = "";
247         var dolAmount = "";
248         var dolFlag   = false;
249         var aChar     = "";
250
251         for(var i = newValue.length; 0 < i; i--) {
252             aChar = newValue.substring(i-1, i);
253             if ("0" <= aChar && aChar <= "9") {
254                 if(dolFlag) {
255                     dolAmount = "" + aChar + dolAmount;
256                 }
257                 else {
258                     decAmount = "" + aChar + decAmount;
259                 }
260             }
261             if (aChar == "." || aChar == ",") {
262                 dolFlag = true;
263             }
264         }
265
266         if (!dolFlag) {
267             dolAmount = decAmount;
268             decAmount = "";
269         }
270
271         if (dolAmount == "") {
272             dolAmount = "0";
273         }
274     // Strip leading 0s
275         if (dolAmount.length > 1) {
276             while(dolAmount.length > 1 && dolAmount.substring(0,1) == "0") {
277                 dolAmount = dolAmount.substring(1,dolAmount.length);
278             }
279         }
280         if (decAmount.length > 2) {
281             decAmount = decAmount.substring(0,2);
282         }
283     // Pad right side
284         if (decAmount.length == 1) {
285            decAmount = decAmount + "0";
286         }
287         if (decAmount.length == 0) {
288            decAmount = decAmount + "00";
289         }
290
291         textObj.value = dolAmount + "." + decAmount;
292     }
293
294     function fnClickAddRow( table, invoiceCode, invoiceTitle, invoicePrice ) {
295       var defaultPrice = { value: invoicePrice };
296       moneyFormat(defaultPrice);
297       table.fnAddData( [
298         invoiceTitle,
299         defaultPrice.value,
300         1,
301         null,
302         '<button class="btn btn-default btn-xs drop" type="button"><i class="fa fa-trash-can"></i> ' + _("Remove") + '</button>',
303         invoiceCode
304         ]
305       );
306     }
307
308     function updateChangeValues() {
309         var change = $('#change')[0];
310         var zero_formatted = "[% 0 | $Price %]";
311         change.innerHTML = Math.round(($('#collected')[0].value - $('#paid')[0].value) * 100) / 100;
312         if (change.innerHTML <= 0) {
313             var paid = $('#paid')[0];
314             moneyFormat(paid);
315             $('#collected').rules( "add", { min: Number(paid.value) });
316             change.innerHTML = zero_formatted;
317             $(':input[name="change"]').val(zero_formatted);
318         } else {
319             change.value = change.innerHTML;
320             moneyFormat(change);
321             change.innerHTML = change.value;
322             $(':input[name="change"]').val(change.value);
323         }
324
325         $('#modal_change').html(change.innerHTML);
326     }
327
328     $(document).ready(function() {
329         var sale_table = $("#sale").dataTable($.extend(true, {}, dataTablesDefaults, {
330             "bPaginate": false,
331             "bFilter": false,
332             "bInfo": false,
333             "aoColumnDefs": [{
334                 "aTargets": [-2],
335                 "bSortable": false,
336                 "bSearchable": false,
337             }, {
338                 "aTargets": [-3],
339                 "mRender": function ( data, type, full ) {
340                     var price = Number.parseFloat(data);
341                     return price.format_price();
342                 }
343             }, {
344                 "aTargets": [-5],
345                 "sClass" : "editable",
346             }, {
347                 "aTargets": [-4],
348                 "sClass" : "editable_int",
349             }, {
350                 "targets": [-1],
351                 "visible": false,
352                 "searchable": false
353             }],
354             "aaSorting": [],
355             "fnDrawCallback": function (oSettings) {
356                 var local = this;
357                 local.$('.editable').editable( function(value, settings) {
358                     var aPos = local.fnGetPosition( this );
359                     local.fnUpdate( value, aPos[0], aPos[1], true, false );
360                     return value;
361                 },{
362                     type    : 'text',
363                     pattern : "^\\d+(\.\\d{2})?$",
364                     onblur  : 'submit',
365                     width   : "8em",
366                     tooltip : _("Click to edit")
367                 });
368                 local.$('.editable_int').editable( function(value, settings) {
369                     var aPos = local.fnGetPosition( this );
370                     local.fnUpdate( value, aPos[0], aPos[1], true, false );
371                     return value;
372                 },{
373                     type    : 'text',
374                     pattern : "[0-9]*",
375                     onblur  : 'submit',
376                     width   : "4em",
377                     tooltip : _("Click to edit")
378                 });
379             },
380             "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
381                 var iTotal = aData[1] * aData[2];
382                 this.fnUpdate( iTotal, nRow, 3, false, false );
383             },
384             "fnFooterCallback": function(nFoot, aData, iStart, iEnd, aiDisplay) {
385                 var iTotalPrice = 0;
386                 for ( var i=0 ; i<aData.length ; i++ )
387                 {
388                     iTotalPrice += aData[i][3]*1;
389                 }
390                 iTotalPrice = iTotalPrice.format_price();
391                 nFoot.getElementsByTagName('td')[1].innerHTML = iTotalPrice;
392                 $('#paid').val(iTotalPrice);
393                 $('#paid').trigger('change');
394             },
395             "autoWidth": false
396         }));
397
398         $("#sale").on("click", "button.drop", function(){
399                 sale_table.DataTable().row($(this).parents('tr')).remove().draw(false);
400         });
401
402         var items_table_settings = [% TablesSettings.GetTableSettings('pos', 'pay', 'invoices', 'json') | $raw %];
403         var items_table = KohaTable("invoices", {
404                "sPaginationType": "full",
405                "aaSorting": [[ 0, "asc" ]],
406                "autoWidth": false
407         }, items_table_settings, false);
408
409         $("#invoices").on("click", ".add_button", function(e) {
410             e.preventDefault();
411             fnClickAddRow(sale_table, $( this ).data('invoiceCode'), $( this ).data('invoiceTitle'), $( this ).data('invoicePrice') );
412             if($('#invoices_filter').find('input[type=search]').val() !== ''){
413                 items_table.fnFilter( '' );
414             }
415         });
416
417         // Change calculation and modal
418         var change = $('#change')[0];
419         $("#paid, #collected").on("change",function() {
420             moneyFormat( this );
421             if (change != undefined) {
422                 updateChangeValues();
423             }
424         });
425
426         var checked = false;
427         $('#modal_submit').click(function() {
428             checked = true;
429             $('#payForm').submit();
430         });
431
432         $('#payForm').validate({
433             rules: {
434                 paid: {
435                     required: true
436                 },
437                 collected: {
438                     required: true
439                 },
440                 payment_type: {
441                     required: true
442                 },
443                 registerid: {
444                     required: true
445                 }
446             }
447         });
448
449         $('#payForm').submit(function(e){
450             // first, clear stale sales 'rows' from the payForm
451             if($('input[name="sales"]').length > 0) {
452                 $('input[name="sales"]').each(function() {
453                     $(this).remove();
454                 });
455             }
456
457             // now, process the current & fresh contents of the sale_table
458             if (change != undefined && change.innerHTML > 0.00 && !checked) {
459                 e.preventDefault();
460                 $("#confirm_change_form").modal("show");
461             } else {
462                 var rows = sale_table.fnGetData();
463                 rows.forEach(function (row, index) {
464                     var sale = {
465                         code: row[5],
466                         price: row[1],
467                         quantity: row[2]
468                     };
469                     $('<input>').attr({
470                         type: 'hidden',
471                         name: 'sales',
472                         value: JSON.stringify(sale)
473                     }).appendTo('#payForm');
474                 });
475                 return true;
476             }
477         });
478
479         [% IF payment_id && Koha.Preference('FinePaymentAutoPopup') %]
480             $("#printReceipt").click(function() {
481                 var win = window.open('/cgi-bin/koha/pos/printreceipt.pl?action=print&accountlines_id=[% payment_id | uri %]&collected=[% collected | uri %]&change=[% change | uri %]', '_blank');
482                 win.focus();
483             });
484             $("#printReceipt").click();
485         [% END %]
486     });
487     </script>
488 [% END %]
489 [% INCLUDE 'intranet-bottom.inc' %]