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