-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditorder.php
504 lines (417 loc) · 19.6 KB
/
editorder.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
<?php
include_once'connectdb.php';
session_start();
if($_SESSION['useremail']=="" OR $_SESSION['role']==""){
header('location:index.php');
}
function fill_product($pdo, $pid){
$output='';
$select=$pdo->prepare("select * from tbl_product order by pname asc");
$select->execute();
$result=$select->fetchAll();
foreach($result as $row){
$output.='<option value="'.$row['pid'].'"';
if($pid==$row['pid']){
$output.='selected';
}
$output.='>'.$row["pname"].'</option>';
}return $output;
}
$id = $_GET['id'];
$select=$pdo->prepare("select * from tbl_invoice where invoice_id = $id");
$select->execute();
$row=$select->fetch(PDO::FETCH_ASSOC);
$customer_name=$row['customer_name'];
$order_date=date('Y-m-d',strtotime($row['order_date']));
$subtotal=$row['subtotal'];
$tax=$row['tax'];
$discount=$row['discount'];
$total=$row['total'];
$paid=$row['paid'];
$due=$row['due'];
$payment_type=$row['payment_type'];
$select=$pdo->prepare("select * from tbl_invoice_details where invoice_id = $id");
$select->execute();
$row_invoice_details=$select->fetchAll(PDO::FETCH_ASSOC);
if(isset($_POST['btnupdateorder'])){
// steps for btnupdateorder button
// 1. get value from text feilds and from array in variable
$txt_customer_name=$_POST['txtcustomer'];
$txt_order_date=date('Y-m-d',strtotime($_POST['orderdate']));
$txt_subtotal=$_POST['txtsubtotal'];
$txt_tax=$_POST['txttax'];
$txt_discount=$_POST['txtdiscount'];
$txt_total=$_POST['txttotal'];
$txt_paid=$_POST['txtpaid'];
$txt_due=$_POST['txtdue'];
$txt_payment_type=$_POST['rd'];
$arr_productid=$_POST['productid'];
$arr_productname=$_POST['productname'];
$arr_stock=$_POST['stock'];
$arr_qty=$_POST['qty'];
$arr_price=$_POST['price'];
$arr_total=$_POST['total'];
// 2. write update query from tbl_product stock
foreach ($row_invoice_details as $item_invoice_details){
$updateproduct = $pdo->prepare("update tbl_product set pstock = pstock+".$item_invoice_details['qty']."
where pid = '".$item_invoice_details['product_id']."'");
$updateproduct->execute();
}
// 3. write delete query for tbl_invoice_details
$delete_invoice_details=$pdo->prepare("delete from tbl_invoice_details where invoice_id=$id");
$delete_invoice_details->execute();
// 4. write update query for tbl_invoice table data
$update_invoice=$pdo->prepare("update tbl_invoice set customer_name=:cust, order_date=:orderdate,
subtotal=:stotal, tax=:tax, discount=:disc, total=:total, paid=:paid, due=:due, payment_type=:ptype where invoice_id = $id");
$update_invoice->bindParam(':cust',$txt_customer_name);
$update_invoice->bindParam(':orderdate',$txt_order_date);
$update_invoice->bindParam(':stotal',$txt_subtotal);
$update_invoice->bindParam(':tax',$txt_tax);
$update_invoice->bindParam(':disc',$txt_discount);
$update_invoice->bindParam(':total',$txt_total);
$update_invoice->bindParam(':paid',$txt_paid);
$update_invoice->bindParam(':due',$txt_due);
$update_invoice->bindParam(':ptype',$txt_payment_type);
$update_invoice->execute();
$invoice_id=$pdo->lastInsertId();
if($invoice_id!=null){
for($i=0; $i<count($arr_productid); $i++){
// 5. write select query for tbl_product table to get out stock value
$selectpdt=$pdo->prepare("select * from tbl_product where pid='".$arr_productid[$i]."'");
$selectpdt->execute();
while($rowpdt=$selectpdt->fetch(PDO::FETCH_OBJ)){
$db_stock[$i]=$rowpdt->pstock;
$rem_qty = $db_stock[$i] - $arr_qty[$i];
if($rem_qty < 0){
return "Order is not complete";
}else{
// 6. write update query for tbl_product table to update stock values
$update=$pdo->prepare("update tbl_product set pstock='$rem_qty' where pid='".$arr_productid[$i]."'");
$update->execute();
}
}
// 7. write insert query for tbl_invoice_details for insert new record
$insert=$pdo->prepare("insert into tbl_invoice_details(invoice_id, product_id, product_name, qty, price, order_date)
values(:invid,:pid,:pname,:qty,:price,:orderdate)");
$insert->bindParam(':invid',$id);
$insert->bindParam(':pid',$arr_productid[$i]);
$insert->bindParam(':pname',$arr_productname[$i]);
$insert->bindParam(':qty',$arr_qty[$i]);
$insert->bindParam(':price',$arr_price[$i]);
$insert->bindParam(':orderdate',$txt_order_date);
$insert->execute();
}
// echo 'successfull created order';
header('location:orderlist.php');
}
}
if($_SESSION['role']=="Admin"){
include_once'header.php';
}else{
include_once'headeruser.php';
}
?>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
Create Order
<small></small>
</h1>
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-dashboard"></i> Level</a></li>
<li class="active">Here</li>
</ol>
</section>
<!-- Main content -->
<section class="content container-fluid">
<!--------------------------
| Your Page Content Here |
-------------------------->
<div class="box box-warning">
<form action="" method="post" name="">
<div class="box-header with-border">
<h3 class="box-title">Edit Order</h3>
</div>
<!-- /.box-header -->
<!-- form start -->
<!-- this is for customer and date -->
<div class="box-body">
<div class="col-md-6">
<div class="form-group">
<label>Customer Name</label>
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-user"></i>
</div>
<input type="text" class="form-control" name="txtcustomer" value="<?php echo $customer_name; ?>" required>
</div>
</div>
</div>
<div class="col-md-6">
<!-- Date -->
<div class="form-group">
<label>Date:</label>
<div class="input-group date">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
<input type="text" class="form-control pull-right" id="datepicker" name="orderdate"
value="<?php echo $order_date;?>">
</div>
<!-- /.input group -->
</div>
</div>
</div>
<!-- this is for table -->
<div class="box-body">
<div class="col-md-12">
<div style="overflow-x:auto">
<table id="producttable" class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Search Product</th>
<th>Stock</th>
<th>Price</th>
<th>Enter Quantity</th>
<th>Total</th>
<th>
<center>
<button type="button" name="add" class="btn btn-info btn-sm btnadd">
<span class="glyphicon glyphicon-plus"></span>
</button>
</center>
</th>
</tr>
</thead>
<?php
foreach($row_invoice_details as $item_invoice_details){
$select=$pdo->prepare("select * from tbl_product where pid = '{$item_invoice_details['product_id']}'");
$select->execute();
$row_product=$select->fetch(PDO::FETCH_ASSOC);
?>
<tr>
<?php
echo '<td><input type="hidden" class="form-control pname" name="productname[]" value="'.$row_product['pname'].'" readonly></td>';
echo '<td><select class="form-control productidedit" name="productid[]" style="width: 250px";>
<option value="">Select Option</option>'.fill_product($pdo, $item_invoice_details['product_id']).'</select></td>';
echo '<td><input type="text" class="form-control stock" name="stock[]" value="'.$row_product['pstock'].'" readonly></td>';
echo '<td><input type="text" class="form-control price" name="price[]" value="'.$row_product['saleprice'].'" readonly></td>';
echo '<td><input type="number" min="1" class="form-control qty" name="qty[]" value="'.$item_invoice_details['qty'].'" required></td>';
echo '<td><input type="text" class="form-control total" name="total[]" value="'.$row_product['saleprice'] * $item_invoice_details['qty'].'" readonly></td>';
echo '<td><center><button type="button" name="remove" class="btn btn-danger btn-sm btnremove"><span class="glyphicon glyphicon-remove"></span></button></center></td>';
?>
</tr>
<?php } ?>
</table>
</div>
</div>
</div>
<!-- tax dis. etc -->
<div class="box-body">
<div class="col-md-6">
<div class="form-group">
<label>SubTotal</label>
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-usd"></i>
</div>
<input type="text" class="form-control" value="<?php echo $subtotal;?>" name="txtsubtotal" id="txtsubtotal" required readonly>
</div>
</div>
<div class="form-group">
<label>Tax (5%)</label>
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-usd"></i>
</div>
<input type="text" class="form-control" value="<?php echo $tax;?>" name="txttax" id="txttax" required readonly>
</div>
</div>
<div class="form-group">
<label>Discount</label>
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-usd"></i>
</div>
<input type="text" class="form-control" value="<?php echo $discount;?>" name="txtdiscount" id="txtdiscount" required>
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Total</label>
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-usd"></i>
</div>
<input type="text" class="form-control" value="<?php echo $total;?>" name="txttotal" id="txttotal" required readonly>
</div>
</div>
<div class="form-group">
<label>Paid</label>
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-usd"></i>
</div>
<input type="text" class="form-control" value="<?php echo $paid;?>" name="txtpaid" id="txtpaid" required>
</div>
</div>
<div class="form-group">
<label>Due</label>
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-usd"></i>
</div>
<input type="text" class="form-control" value="<?php echo $due;?>" name="txtdue" id="txtdue" required readonly>
</div>
</div>
<!-- radio -->
<label>Payment Method</label>
<div class="form-group">
<label>
<input type="radio" name="rd" class="minimal-red" value="Cash"
<?php echo ($payment_type=='Cash')? 'checked':''?>> CASH
</label>
<label>
<input type="radio" name="rd" class="minimal-red" value="Card"
<?php echo ($payment_type=='Card')? 'checked':''?>> CARD
</label>
<label>
<input type="radio" name="rd" class="minimal-red" value="Check"
<?php echo ($payment_type=='Check')? 'checked':''?>> CHECK
</label>
</div>
</div>
</div>
<hr>
<div align="center">
<input type="submit" name="btnupdateorder" value="Update Order" class="btn btn-warning">
</div>
<hr>
</form>
</div>
</section>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
<script>
//Date picker
$('#datepicker').datepicker({
autoclose: true
});
//Red color scheme for iCheck
$('input[type="checkbox"].minimal-red, input[type="radio"].minimal-red').iCheck({
checkboxClass: 'icheckbox_minimal-red',
radioClass : 'iradio_minimal-red'
});
$(document).ready(function(){
//Initialize Select2 Elements
$('.productidedit').select2()
$(".productidedit").on('change', function(e){
var productid = this.value;
var tr = $(this).parent().parent();
$.ajax({
url: "getproduct.php",
method: "get",
data:{id:productid},
success:function(data){
// console.log(data);
tr.find(".pname").val(data["pname"]);
tr.find(".stock").val(data["pstock"]);
tr.find(".price").val(data["saleprice"]);
tr.find(".qty").val(1);
tr.find(".total").val(tr.find(".qty").val() * tr.find(".price").val());
calculate(0,0);
$("#txtpaid").val("");
}
})
})
$(document).on('click','.btnadd',function(){
var html = '';
html+='<tr>';
html+='<td><input type="hidden" class="form-control pname" name="productname[]" readonly></td>';
html+='<td><select class="form-control productid" name="productid[]" style="width: 250px";><option value="">Select Option</option><?php echo fill_product($pdo,''); ?></select></td>';
html+='<td><input type="text" class="form-control stock" name="stock[]" readonly></td>';
html+='<td><input type="text" class="form-control price" name="price[]" readonly></td>';
html+='<td><input type="number" min="1" class="form-control qty" name="qty[]" required></td>';
html+='<td><input type="text" class="form-control total" name="total[]" readonly></td>';
html+='<td><center><button type="button" name="remove" class="btn btn-danger btn-sm btnremove"><span class="glyphicon glyphicon-remove"></span></button></center></td>';
$('#producttable').append(html);
//Initialize Select2 Elements
$('.productid').select2()
$(".productid").on('change', function(e){
var productid = this.value;
var tr = $(this).parent().parent();
$.ajax({
url: "getproduct.php",
method: "get",
data:{id:productid},
success:function(data){
// console.log(data);
tr.find(".pname").val(data["pname"]);
tr.find(".stock").val(data["pstock"]);
tr.find(".price").val(data["saleprice"]);
tr.find(".qty").val(1);
tr.find(".total").val(tr.find(".qty").val() * tr.find(".price").val());
calculate(0,0);
$("#txtpaid").val("");
}
})
})
});
$(document).on('click','.btnremove',function(){
$(this).closest('tr').remove();
calculate(0,0);
$("#txtpaid").val("");
});
$("#producttable").delegate(".qty","keyup change", function(){
var quantity = $(this);
var tr = $(this).parent().parent();
$("#txtpaid").val("");
if((quantity.val()-0)>(tr.find(".stock").val()-0)){
swal("WARNING","SORRY! This much of quantity is not available","warning");
quantity.val(1);
tr.find(".total").val(quantity.val() * tr.find(".price").val());
calculate(0,0);
}else{
tr.find(".total").val(quantity.val() * tr.find(".price").val());
calculate(0,0);
}
})
function calculate(dis,paid){
var subtotal=0;
var tax=0;
var discount=dis;
var net_total=0;
var paid_amt=paid;
var due=0;
$(".total").each(function(){
subtotal=subtotal+($(this).val()*1);
})
tax=0.05*subtotal;
net_total=tax+subtotal;
net_total=net_total-discount;
due=net_total-paid_amt;
$("#txtsubtotal").val(subtotal.toFixed(2));
$("#txttax").val(tax.toFixed(2));
$("#txttotal").val(net_total.toFixed(2));
$("#txtdiscount").val(discount);
$("#txtdue").val(due.toFixed(2));
}
$("#txtdiscount").keyup(function(){
var discount = $(this).val();
calculate(discount,0);
})
$("#txtpaid").keyup(function(){
var paid = $(this).val();
var discount = $("#txtdiscount").val();
calculate(discount,paid)
})
});
</script>
<?php
include_once'footer.php';
?>