-
Notifications
You must be signed in to change notification settings - Fork 0
/
pos.php
331 lines (317 loc) · 11 KB
/
pos.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php include 'db_connect.php';
if(isset($_GET['id'])){
$qry = $conn->query("SELECT * FROM sales_list where id=".$_GET['id'])->fetch_array();
foreach($qry as $k => $val){
$$k = $val;
}
$inv = $conn->query("SELECT * FROM inventory where type=2 and form_id=".$_GET['id']);
}
?>
<div class="container-fluid">
<div class="col-lg-12">
<div class="card">
<div class="card-header">
<h4>Sales</h4>
</div>
<div class="card-body">
<form action="" id="manage-sales">
<input type="hidden" name="id" value="<?php echo isset($_GET['id']) ? $_GET['id'] : '' ?>">
<input type="hidden" name="ref_no" value="<?php echo isset($ref_no) ? $ref_no : '' ?>">
<div class="col-md-12">
<div class="row">
<div class="form-group col-md-5">
<label class="control-label">Customer</label>
<select name="customer_id" id="" class="custom-select browser-default select2">
<option value="0" selected="">Guest</option>
<?php
$customer = $conn->query("SELECT * FROM customer_list order by name asc");
while($row=$customer->fetch_assoc()):
?>
<option value="<?php echo $row['id'] ?>"><?php echo $row['name'] ?></option>
<?php endwhile; ?>
</select>
</div>
</div>
<hr>
<div class="row mb-3">
<div class="col-md-4">
<label class="control-label">Product</label>
<select name="" id="product" class="custom-select browser-default select2">
<option value=""></option>
<?php
$cat = $conn->query("SELECT * FROM category_list order by name asc");
while($row=$cat->fetch_assoc()):
$cat_arr[$row['id']] = $row['name'];
endwhile;
$product = $conn->query("SELECT * FROM product_list order by name asc");
while($row=$product->fetch_assoc()):
$prod[$row['id']] = $row;
?>
<option value="<?php echo $row['id'] ?>" data-name="<?php echo $row['name'] ?>" data-description="<?php echo $row['description'] ?>"><?php echo $row['name'] . ' | ' . $row['product_id'] ?></option>
<?php endwhile; ?>
</select>
</div>
<div class="col-md-2">
<label class="control-label">Qty</label>
<input type="number" class="form-control text-right" step="any" id="qty" >
</div>
<div class="col-md-3">
<label class="control-label"> </label>
<button class="btn btn-block btn-sm btn-primary" type="button" id="add_list"><i class="fa fa-plus"></i> Add to List</button>
</div>
</div>
<div class="row">
<table class="table table-bordered" id="list">
<colgroup>
<col width="30%">
<col width="10%">
<col width="25%">
<col width="25%">
<col width="10%">
</colgroup>
<thead>
<tr>
<th class="text-center">Product</th>
<th class="text-center">Qty</th>
<th class="text-center">Price</th>
<th class="text-center">Amount</th>
<th class="text-center">Action</th>
</tr>
</thead>
<tbody>
<?php
if(isset($id)):
while($row = $inv->fetch_assoc()):
foreach(json_decode($row['other_details']) as $k=>$v){
$row[$k] = $v;
}
?>
<tr class="item-row">
<td>
<input type="hidden" name="inv_id[]" value="<?php echo $row['id'] ?>">
<input type="hidden" name="product_id[]" value="<?php echo $row['product_id']?>">
<p class="pname">Name: <b><?php echo $prod[$row['product_id']]['name']?></b></p>
<p class="pdesc"><small><i>Description: <b><?php echo $prod[$row['product_id']]['description'] ?></b></i></small></p>
</td>
<td>
<input type="number" min="1" step="any" name="qty[]" value="<?php echo $row['qty'] ?>" class="text-right">
</td>
<td>
<input type="hidden" min="1" step="any" name="price[]" value="<?php echo $row['price'] ?>" class="text-right">
<p class="text-right"><?php echo $row['price'] ?></p>
</td>
<td>
<p class="amount text-right"></p>
</td>
<td class="text-center">
<button class="btn btn-sm btn-danger" onclick = "rem_list($(this))"><i class="fa fa-trash"></i></button>
</td>
</tr>
<?php endwhile; ?>
<?php endif; ?>
</tbody>
<tfoot>
<tr>
<th class="text-right" colspan="3">Total</th>
<th class="text-right tamount"></th>
<th></th>
</tr>
</tfoot>
</table>
</div>
<div class="row">
<button class="btn btn-primary btn-sm btn-block float-right " type="button" id="pay">Pay</button>
</div>
</div>
<div class="modal fade" id="pay_modal" role='dialog'>
<div class="modal-dialog modal-md" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title"></h5>
</div>
<div class="modal-body">
<div class="container-fluid">
<div class="form-group">
<label for="" class="control-label">Amount</label>
<input type="text" name="tamount" value="" class="form-control text-right" readonly="">
</div>
<div class="form-group">
<label for="" class="control-label">Discount</label>
<input type="number" name="amount_discount" value="0" min="0" class="form-control text-right" >
</div>
<div class="form-group">
<label for="" class="control-label">Total Amount</label>
<input type="number" name="totalamount" value="0" min="0" class="form-control text-right" readonly="">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" id='submit' onclick="$('#manage-sales').submit()">Pay</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<div id="tr_clone">
<table>
<tr class="item-row">
<td>
<input type="hidden" name="inv_id[]" value="">
<input type="hidden" name="product_id[]" value="">
<p class="pname">Name: <b>product</b></p>
<p class="pdesc"><small><i>Description: <b>Description</b></i></small></p>
</td>
<td>
<input type="number" min="1" step="any" name="qty[]" value="" class="text-right">
</td>
<td>
<input type="hidden" min="1" step="any" name="price[]" value="" class="text-right" readonly="">
<p class="price text-right">0</p>
</td>
<td>
<p class="amount text-right"></p>
</td>
<td class="text-center">
<buttob class="btn btn-sm btn-danger" onclick = "rem_list($(this))"><i class="fa fa-trash"></i></buttob>
</td>
</tr>
</table>
</div>
<style type="text/css">
#tr_clone{
display: none;
}
td{
vertical-align: middle;
}
td p {
margin: unset;
}
td input[type='number']{
height: calc(100%);
width: calc(100%);
}
input[type=number]::-webkit-inner-spin-button,
input[type=number]::-webkit-outer-spin-button {
-webkit-appearance: none;
margin: 0;
}
</style>
<script>
$('.select2').select2({
placeholder:"Please select here",
width:"100%"
})
$('#pay').click(function(){
if($("#list .item-row").length <= 0){
alert_toast("Please insert atleast 1 item first.",'danger');
end_load();
return false;
}
$('#pay_modal').modal('show')
})
$(document).ready(function(){
if('<?php echo isset($id) ?>' == 1){
$('[name="supplier_id"]').val('<?php echo isset($supplier_id) ? $supplier_id :'' ?>').select2({
placeholder:"Please select here",
width:"100%"
})
calculate_total()
}
})
function rem_list(_this){
_this.closest('tr').remove()
}
$('#add_list').click(function(){
// alert("TEST");
// return false;
var tr = $('#tr_clone tr.item-row').clone();
var product = $('#product').val(),
qty = $('#qty').val(),
price = $('#price').val();
if($('#list').find('tr[data-id="'+product+'"]').length > 0){
alert_toast("Product already on the list",'danger')
return false;
}
if(product == '' || qty == ''){
alert_toast("Please complete the fields first",'danger')
return false;
}
$.ajax({
url:'ajax.php?action=chk_prod_availability',
method:'POST',
data:{id:product},
success:function(resp){
resp = JSON.parse(resp);
if(resp.available >= qty){
tr.attr('data-id',product)
tr.find('.pname b').html($("#product option[value='"+product+"']").attr('data-name'))
tr.find('.pdesc b').html($("#product option[value='"+product+"']").attr('data-description'))
tr.find('.price').html(resp.price)
tr.find('[name="product_id[]"]').val(product)
tr.find('[name="qty[]"]').val(qty)
tr.find('[name="price[]"]').val(resp.price)
var amount = parseFloat(price) * parseFloat(qty);
tr.find('.amount').html(parseFloat(amount).toLocaleString('en-US',{style:'decimal',maximumFractionDigits:2,minimumFractionDigits:2}))
$('#list tbody').append(tr)
calculate_total()
$('[name="qty[]"],[name="price[]"]').keyup(function(){
calculate_total()
})
$('#product').val('').select2({
placeholder:"Please select here",
width:"100%"
})
$('#qty').val('')
$('#price').val('')
}else{
alert_toast("Product quantity is greater than available stock.",'danger')
}
}
})
})
function calculate_total(){
var total = 0;
$('#list tbody').find('.item-row').each(function(){
var _this = $(this).closest('tr')
var amount = parseFloat(_this.find('[name="qty[]"]').val()) * parseFloat(_this.find('[name="price[]"]').val());
amount = amount > 0 ? amount :0;
_this.find('p.amount').html(parseFloat(amount).toLocaleString('en-US',{style:'decimal',maximumFractionDigits:2,minimumFractionDigits:2}))
total+=parseFloat(amount);
})
$('[name="tamount"]').val(total)
$('#list .tamount').html(parseFloat(total).toLocaleString('en-US',{style:'decimal',maximumFractionDigits:2,minimumFractionDigits:2}))
}
$('[name="amount_discount"]').keyup(function(){
var discount = $(this).val();
var tamount = $('[name="tamount"]').val();
$('[name="totalamount"]').val(parseFloat(tamount) - parseFloat(discount))
})
$('#manage-sales').submit(function(e){
e.preventDefault()
start_load()
if($("#list .item-row").length <= 0){
alert_toast("Please insert atleast 1 item first.",'danger');
end_load();
return false;
}
$.ajax({
url:'ajax.php?action=save_sales',
method: 'POST',
data: $(this).serialize(),
success:function(resp){
if(resp > 0){
end_load()
alert_toast("Data successfully submitted",'success')
uni_modal('Print',"print_sales.php?id="+resp)
$('#uni_modal').modal({backdrop:'static',keyboard:false})
}
}
})
})
</script>