Bug 23354: Add a Point Of Sale 'pay' screen
[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
32                     <fieldset class="rows">
33                         <legend>This sale</legend>
34                         <p>Click to edit item cost or quantities</p>
35                         <table id="sale" class="table_sale">
36                             <thead>
37                                 <tr>
38                                     <th>Item</th>
39                                     <th>Cost</th>
40                                     <th>Quantity</th>
41                                     <th>Total</th>
42                                 </tr>
43                             </thead>
44                             <tbody>
45                             </tbody>
46                             <tfoot>
47                                 <tr>
48                                     <td colspan="3">Total payable:</td>
49                                     <td></td>
50                                 </tr>
51                             </tfoot>
52                         </table>
53                     </fieldset>
54
55                     <fieldset class="rows">
56                         <legend>Collect payment</legend>
57                         <ol>
58                             <li>
59                                 <label for="paid">Amount being paid: </label>
60                                 <input name="paid" id="paid" value="[% amountoutstanding | $Price on_editing => 1 %]"/>
61                             </li>
62                             <li>
63                                 <label for="collected">Collected from patron: </label>
64                                 <input id="collected" value="[% amountoutstanding | $Price on_editing => 1 %]"/>
65                             </li>
66                             <li>
67                                 <label>Change to give: </label>
68                                 <span id="change">0.00</span>
69                             </li>
70
71                             [% SET payment_types = AuthorisedValues.GetAuthValueDropbox('PAYMENT_TYPE') %]
72                             [% IF payment_types %]
73                             <li>
74                                 <label for="payment_type">Payment type: </label>
75                                 <select name="payment_type" id="payment_type">
76                                     [% FOREACH pt IN payment_types %]
77                                         <option value="[% pt.authorised_value | html %]">[% pt.lib | html %]</option>
78                                     [% END %]
79                                 </select>
80                             </li>
81                             [% END %]
82
83                             [% IF Koha.Preference('UseCashRegisters') %]
84                             <li>
85                                 <label for="cash_register">Cash register: </label>
86                                 <select name="cash_register" id="cash_register">
87                                     [% FOREACH register IN registers %]
88                                       [% IF register.id == registerid %]
89                                     <option value="[% register.id %]" selected="selected">[% register.name | html %]</option>
90                                       [% ELSE %]
91                                     <option value="[% register.id %]">[% register.name | html %]</option>
92                                       [% END %]
93                                     [% END %]
94                                 </select>
95                             </li>
96                             [% END %]
97                         </ol>
98
99                     </fieldset>
100                 </div>
101
102                 <div class="col-sm-6">
103                     <fieldset class="rows">
104                         <legend>Items for purchase</legend>
105                             [% SET invoice_types = AuthorisedValues.GetAuthValueDropbox('MANUAL_INV') %]
106                             [% IF invoice_types %]
107                             <table id="invoices">
108                             <thead>
109                                 <tr>
110                                     <th>Code</th>
111                                     <th>Description</th>
112                                     <th>Cost</th>
113                                     <th>Action</th>
114                                 </tr>
115                             </thead>
116                             <tbody>
117                             [% FOREACH invoice IN invoice_types %]
118                                 <tr>
119                                     <td>[% invoice.authorised_value | html %]</td>
120                                     <td>[% invoice.lib_opac | html %]</td>
121                                     <td>[% invoice.lib | html %]</td>
122                                     <td>
123                                         <button class="add_button" data-invoice-code="[% invoice.lib_opac %]" data-invoice-title="[% invoice.authorised_value | html %]" data-invoice-price="[% invoice.lib | html %]"><i class="fa fa-plus"></i> Add</button>
124                                     </td>
125                                 </tr>
126                             [% END %]
127                             </table>
128                             [% ELSE %]
129                             You have no manual invoice types defined
130                             [% END %]
131                     </fieldset>
132                 </div>
133
134                 <div class="action">
135                     <input type="submit" name="submitbutton" value="Confirm" />
136                     <a class="cancel" href="/cgi-bin/koha/pos/pay.pl">Cancel</a>
137                 </div>
138             </div>
139         </form>
140         [% END %]
141     </div>
142
143     <div class="col-sm-2 col-sm-pull-10">
144         <aside>
145             [% INCLUDE 'pos-menu.inc' %]
146         </aside>
147     </div>
148
149 </div> <!-- /.row -->
150
151 [% MACRO jsinclude BLOCK %]
152     [% Asset.js("js/admin-menu.js") | $raw %]
153     [% INCLUDE 'datatables.inc' %]
154     [% Asset.js("lib/jquery/plugins/jquery.jeditable.mini.js") | $raw %]
155     <script>
156     function fnClickAddRow( table, invoiceTitle, invoicePrice ) {
157       table.fnAddData( [
158         invoiceTitle,
159         invoicePrice,
160         1,
161         null
162          ]
163       );
164     }
165
166     function moneyFormat(textObj) {
167         var newValue = textObj.value;
168         var decAmount = "";
169         var dolAmount = "";
170         var decFlag   = false;
171         var aChar     = "";
172
173         for(i=0; i < newValue.length; i++) {
174             aChar = newValue.substring(i, i+1);
175             if (aChar >= "0" && aChar <= "9") {
176                 if(decFlag) {
177                     decAmount = "" + decAmount + aChar;
178                 }
179                 else {
180                     dolAmount = "" + dolAmount + aChar;
181                 }
182             }
183             if (aChar == ".") {
184                 if (decFlag) {
185                     dolAmount = "";
186                     break;
187                 }
188                 decFlag = true;
189             }
190         }
191
192         if (dolAmount == "") {
193             dolAmount = "0";
194         }
195     // Strip leading 0s
196         if (dolAmount.length > 1) {
197             while(dolAmount.length > 1 && dolAmount.substring(0,1) == "0") {
198                 dolAmount = dolAmount.substring(1,dolAmount.length);
199             }
200         }
201         if (decAmount.length > 2) {
202             decAmount = decAmount.substring(0,2);
203         }
204     // Pad right side
205         if (decAmount.length == 1) {
206            decAmount = decAmount + "0";
207         }
208         if (decAmount.length == 0) {
209            decAmount = decAmount + "00";
210         }
211
212         textObj.value = dolAmount + "." + decAmount;
213     }
214
215     function updateChangeValues() {
216         var change = $('#change')[0];
217         change.innerHTML = Math.round(($('#collected')[0].value - $('#paid')[0].value) * 100) / 100;
218         if (change.innerHTML <= 0) {
219             change.innerHTML = "0.00";
220         } else {
221             change.value = change.innerHTML;
222             moneyFormat(change);
223             change.innerHTML = change.value;
224         }
225         $('#modal_change').html(change.innerHTML);
226     }
227
228     $(document).ready(function() {
229         var sale_table = $("#sale").dataTable($.extend(true, {}, dataTablesDefaults, {
230             "bPaginate": false,
231             "bFilter": false,
232             "bInfo": false,
233             "bAutoWidth": false,
234             "aoColumnDefs": [{
235                 "aTargets": [-2],
236                 "bSortable": false,
237                 "bSearchable": false,
238             }, {
239                 "aTargets": [-1],
240                 "mRender": function ( data, type, full ) {
241                     var price = Number.parseFloat(data).toFixed(2);
242                     return '£'+price;
243                 }
244             }, {
245                 "aTargets": [-2, -3],
246                 "sClass" : "editable",
247             }],
248             "aaSorting": [
249                 [1, "asc"]
250             ],
251             "fnDrawCallback": function (oSettings) {
252                 var local = this;
253                 local.$('.editable').editable( function(value, settings) {
254                     var aPos = local.fnGetPosition( this );
255                     local.fnUpdate( value, aPos[0], aPos[1], true, false );
256                     return value;
257                 },{
258                     type    : 'text'
259                 })
260             },
261             "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
262                 var iTotal = aData[1] * aData[2];
263                 this.fnUpdate( iTotal, nRow, 3, false, false );
264             },
265             "fnFooterCallback": function(nFoot, aData, iStart, iEnd, aiDisplay) {
266                 var iTotalPrice = 0;
267                 for ( var i=0 ; i<aData.length ; i++ )
268                             {
269                                     iTotalPrice += aData[i][3]*1;
270                             }
271
272                 iTotalPrice = Number.parseFloat(iTotalPrice).toFixed(2);
273                 nFoot.getElementsByTagName('td')[1].innerHTML = iTotalPrice;
274                 $('#paid').val(iTotalPrice);
275             }
276         }));
277
278         var items_table = $("#invoices").dataTable($.extend(true,{}, dataTablesDefaults, {
279                "aoColumnDefs": [
280                   { "aTargets": [ -1, -2 ], "bSortable": false, "bSearchable":false },
281                ],
282                "aaSorting": [[ 0, "asc" ]],
283                "paginationType": "full",
284         }));
285
286         $(".add_button").on("click", function(ev) {
287             ev.preventDefault();
288             fnClickAddRow(sale_table, $( this ).data('invoiceTitle'), $( this ).data('invoicePrice') );
289             items_table.fnFilter( '' );
290         });
291
292         $("#paid, #collected").on("change",function() {
293             moneyFormat( this );
294             if (change != undefined) {
295                 updateChangeValues();
296             }
297         });
298
299         $("#payForm").submit(function(e){
300             var rows = sale_table.fnGetData();
301             rows.forEach(function (row, index) {
302                 var sale = {
303                     code: row[0],
304                     price: row[1],
305                     quantity: row[2]
306                 };
307                 $('<input>').attr({
308                     type: 'hidden',
309                     name: 'sales',
310                     value: JSON.stringify(sale)
311                 }).appendTo('#payForm');
312             });
313             return true;
314         });
315     });
316     </script>
317 [% END %]
318
319 [% INCLUDE 'intranet-bottom.inc' %]