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