Bug 26172: (follow-up) Summary by payment_type
[koha-ffzg.git] / koha-tmpl / intranet-tmpl / prog / en / modules / pos / pay.tt
1 [% USE raw %]
2 [% USE Asset %]
3 [% USE Koha %]
4 [% USE Price %]
5 [% USE TablesSettings %]
6 [% SET footerjs = 1 %]
7 [% PROCESS 'payments.inc' %]
8 [% INCLUDE 'doc-head-open.inc' %]
9 <title>Koha &rsaquo; Payments</title>
10 [% INCLUDE 'doc-head-close.inc' %]
11 </head>
12
13 <body id="payments" class="pos">
14 [% INCLUDE 'header.inc' %]
15 [% INCLUDE 'circ-search.inc' %]
16
17 <div id="breadcrumbs"><a href="/cgi-bin/koha/mainpage.pl">Home</a> &rsaquo; Point of sale</div>
18
19 <div class="main container-fluid">
20     <div class="row">
21         <div class="col-sm-10 col-sm-push-2">
22
23         [% IF ( error_registers ) %]
24             <div id="error_message" class="dialog message">
25                 <p>
26                     You must have at least one cash register associated with the library before you can record payments.
27                 </p>
28                 [% IF ( CAN_user_parameters_manage_cash_registers ) %]
29                     <form action="/cgi-bin/koha/admin/cash_registers.pl" method="get">
30                         <input type="hidden" name="op" value="add_form" />
31                         <button class="new" type="submit"><i class="fa fa-plus"></i> Create a new cash register</button>
32                     </form>
33                 [% END %]
34             </div>
35         [% ELSE %]
36
37         [% IF payment_id && !Koha.Preference('FinePaymentAutoPopup') %]
38         <div class="dialog alert audio-alert-action">
39             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>
40         </div>
41         [% END %]
42
43         <form name="payForm" id="payForm" method="post" action="/cgi-bin/koha/pos/pay.pl">
44             <div class="row">
45
46                 <div class="col-sm-6">
47                     <fieldset class="rows">
48                         <legend>Items for purchase</legend>
49                             Please select items from below to add to this transaction:
50                             [% IF invoice_types %]
51                             <table id="invoices">
52                             <thead>
53                                 <tr>
54                                     <th>Code</th>
55                                     <th>Description</th>
56                                     <th class="NoSort">Cost</th>
57                                     <th class="NoSort">Action</th>
58                                 </tr>
59                             </thead>
60                             <tbody>
61                             [% FOREACH invoice IN invoice_types %]
62                                 <tr>
63                                     <td>[% invoice.code | html %]</td>
64                                     <td>[% invoice.description | html %]</td>
65                                     <td>[% invoice.default_amount | $Price %]</td>
66                                     <td>
67                                         <button type="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>
68                                     </td>
69                                 </tr>
70                             [% END %]
71                             </table>
72                             [% ELSE %]
73                             You have no manual invoice types defined
74                             [% END %]
75                     </fieldset>
76                 </div>
77
78                 <div class="col-sm-6">
79
80                     <fieldset class="rows">
81                         <legend>This sale</legend>
82                         <p>Click to edit item cost or quantities</p>
83                         <table id="sale" class="table_sale">
84                             <thead>
85                                 <tr>
86                                     <th>Item</th>
87                                     <th>Cost</th>
88                                     <th>Quantity</th>
89                                     <th>Total</th>
90                                     <th>Action</th>
91                                     <th>CODE</th>
92                                 </tr>
93                             </thead>
94                             <tbody>
95                             </tbody>
96                             <tfoot>
97                                 <tr>
98                                     <td colspan="3">Total payable:</td>
99                                     <td></td>
100                                     <td></td>
101                                     <td></td>
102                                 </tr>
103                             </tfoot>
104                         </table>
105                     </fieldset>
106
107                     <fieldset class="rows">
108                         <legend>Collect payment</legend>
109                         <ol>
110                             <li>
111                                 <label for="paid">Amount being paid: </label>
112                                 <input type="number" min="0.00" max="10000.00" step="0.01" name="paid" id="paid" value="[% amountoutstanding | $Price on_editing => 1 %]" readonly/>
113                             </li>
114                             <li>
115                                 <label for="collected">Amount tendered: </label>
116                                 <input type="number" min="0.00" max="10000.00" step="0.01" name="collected" id="collected" value=""/>
117                             </li>
118                             <li>
119                                 <label>Change to give: </label>
120                                 <span id="change">[% 0 | $Price %]</span>
121                                 <input type="hidden" name="change" value="[% 0 | $Price %]"/>
122                             </li>
123
124                             [% PROCESS account_payment_types %]
125
126                             <li>
127                                 <label for="registerid">Cash register: </label>
128                                 <select name="registerid" id="registerid">
129                                     [% FOREACH register IN registers %]
130                                       [% IF register.id == default_register %]
131                                     <option value="[% register.id | html %]" selected="selected">[% register.name | html %]</option>
132                                       [% ELSE %]
133                                     <option value="[% register.id | html %]">[% register.name | html %]</option>
134                                       [% END %]
135                                     [% END %]
136                                 </select>
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="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             change.innerHTML = zero_formatted;
259         } else {
260             change.value = change.innerHTML;
261             moneyFormat(change);
262             change.innerHTML = change.value;
263         }
264
265         $(':input[name="change"]').val(change.value);
266         $('#modal_change').html(change.innerHTML);
267     }
268
269     $(document).ready(function() {
270         var sale_table = $("#sale").dataTable($.extend(true, {}, dataTablesDefaults, {
271             "bPaginate": false,
272             "bFilter": false,
273             "bInfo": false,
274             "aoColumnDefs": [{
275                 "aTargets": [-3],
276                 "bSortable": false,
277                 "bSearchable": false,
278             }, {
279                 "aTargets": [-3],
280                 "mRender": function ( data, type, full ) {
281                     var price = Number.parseFloat(data);
282                     return price.format_price();
283                 }
284             }, {
285                 "aTargets": [-5],
286                 "sClass" : "editable",
287             }, {
288                 "aTargets": [-4],
289                 "sClass" : "editable_int",
290             }, {
291                 "targets": [-1],
292                 "visible": false,
293                 "searchable": false
294             }],
295             "aaSorting": [
296                 [1, "asc"]
297             ],
298             "fnDrawCallback": function (oSettings) {
299                 var local = this;
300                 local.$('.editable').editable( function(value, settings) {
301                     var aPos = local.fnGetPosition( this );
302                     local.fnUpdate( value, aPos[0], aPos[1], true, false );
303                     return value;
304                 },{
305                     type    : 'number',
306                     step    : '0.01',
307                     min     : '0',
308                     onblur  : 'submit'
309                 });
310                 local.$('.editable_int').editable( function(value, settings) {
311                     var aPos = local.fnGetPosition( this );
312                     local.fnUpdate( value, aPos[0], aPos[1], true, false );
313                     return value;
314                 },{
315                     type    : 'number',
316                     step    : '1',
317                     min     : '0',
318                     onblur  : 'submit'
319                 });
320             },
321             "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
322                 var iTotal = aData[1] * aData[2];
323                 this.fnUpdate( iTotal, nRow, 3, false, false );
324             },
325             "fnFooterCallback": function(nFoot, aData, iStart, iEnd, aiDisplay) {
326                 var iTotalPrice = 0;
327                 for ( var i=0 ; i<aData.length ; i++ )
328                 {
329                     iTotalPrice += aData[i][3]*1;
330                 }
331                 nFoot.getElementsByTagName('td')[1].innerHTML = iTotalPrice.format_price();
332                 $('#paid').val(iTotalPrice);
333                 $('#paid').trigger('change');
334             }
335         }));
336
337         $("#sale").on("click", "button.drop", function(){
338                 sale_table.DataTable().row($(this).parents('tr')).remove().draw(false);
339         });
340
341         var items_columns_settings = [% TablesSettings.GetColumns('pos', 'pay', 'invoices', 'json') | $raw %];
342         var items_table = KohaTable("invoices", {
343                "sPaginationType": "full",
344                "aaSorting": [[ 0, "asc" ]],
345         }, items_columns_settings, false);
346
347         $(".add_button").on("click", function(e) {
348             e.preventDefault();
349             fnClickAddRow(sale_table, $( this ).data('invoiceCode'), $( this ).data('invoiceTitle'), $( this ).data('invoicePrice') );
350             items_table.fnFilter( '' );
351         });
352
353         // Change calculation and modal
354         var change = $('#change')[0];
355         $("#paid, #collected").on("change",function() {
356             moneyFormat( this );
357             if (change != undefined) {
358                 updateChangeValues();
359             }
360         });
361
362         var checked = false;
363         $('#modal_submit').click(function() {
364             checked = true;
365             $('#payForm').submit();
366         });
367
368         $('#payForm').submit(function(e){
369             if (change != undefined && change.innerHTML > 0.00 && !checked) {
370                 e.preventDefault();
371                 $("#confirm_change_form").modal("show");
372             } else {
373                 var rows = sale_table.fnGetData();
374                 rows.forEach(function (row, index) {
375                     var sale = {
376                         code: row[5],
377                         price: row[1],
378                         quantity: row[2]
379                     };
380                     $('<input>').attr({
381                         type: 'hidden',
382                         name: 'sales',
383                         value: JSON.stringify(sale)
384                     }).appendTo('#payForm');
385                 });
386                 return true;
387             }
388         });
389
390         [% IF payment_id && Koha.Preference('FinePaymentAutoPopup') %]
391             $("#printReceipt").click(function() {
392                 var win = window.open('/cgi-bin/koha/pos/printreceipt.pl?action=print&accountlines_id=[% payment_id | uri %]&collected=[% collected | uri %]&change=[% change | uri %]', '_blank');
393                 win.focus();
394             });
395             $("#printReceipt").click();
396         [% END %]
397     });
398     </script>
399 [% END %]
400
401 [% INCLUDE 'intranet-bottom.inc' %]