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