-
Notifications
You must be signed in to change notification settings - Fork 0
/
editOrder.php
90 lines (69 loc) · 3.95 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
<?php
require_once 'core.php';
$valid['success'] = array('success' => false, 'messages' => array());
if($_POST) {
$orderId = $_POST['orderId'];
$orderDate = date('Y-m-d', strtotime($_POST['orderDate']));
$clientName = $_POST['clientName'];
$clientContact = $_POST['clientContact'];
$subTotalValue = $_POST['subTotalValue'];
$vatValue = $_POST['vatValue'];
$totalAmountValue = $_POST['totalAmountValue'];
$discount = $_POST['discount'];
$grandTotalValue = $_POST['grandTotalValue'];
$paid = $_POST['paid'];
$dueValue = $_POST['dueValue'];
$paymentType = $_POST['paymentType'];
$paymentStatus = $_POST['paymentStatus'];
$paymentPlace = $_POST['paymentPlace'];
$gstn = $_POST['gstn'];
$userid = $_SESSION['userId'];
$sql = "UPDATE orders SET order_date = '$orderDate', client_name = '$clientName', client_contact = '$clientContact', sub_total = '$subTotalValue', vat = '$vatValue', total_amount = '$totalAmountValue', discount = '$discount', grand_total = '$grandTotalValue', paid = '$paid', due = '$dueValue', payment_type = '$paymentType', payment_status = '$paymentStatus', order_status = 1 ,user_id = '$userid',payment_place = '$paymentPlace' , gstn = '$gstn' WHERE order_id = {$orderId}";
$connect->query($sql);
$readyToUpdateOrderItem = false;
// add the quantity from the order item to product table
for($x = 0; $x < count($_POST['productName']); $x++) {
// product table
$updateProductQuantitySql = "SELECT product.quantity FROM product WHERE product.product_id = ".$_POST['productName'][$x]."";
$updateProductQuantityData = $connect->query($updateProductQuantitySql);
while ($updateProductQuantityResult = $updateProductQuantityData->fetch_row()) {
// order item table add product quantity
$orderItemTableSql = "SELECT order_item.quantity FROM order_item WHERE order_item.order_id = {$orderId}";
$orderItemResult = $connect->query($orderItemTableSql);
$orderItemData = $orderItemResult->fetch_row();
$editQuantity = $updateProductQuantityResult[0] + $orderItemData[0];
$updateQuantitySql = "UPDATE product SET quantity = $editQuantity WHERE product_id = ".$_POST['productName'][$x]."";
$connect->query($updateQuantitySql);
} // while
if(count($_POST['productName']) == count($_POST['productName'])) {
$readyToUpdateOrderItem = true;
}
} // /for quantity
// remove the order item data from order item table
for($x = 0; $x < count($_POST['productName']); $x++) {
$removeOrderSql = "DELETE FROM order_item WHERE order_id = {$orderId}";
$connect->query($removeOrderSql);
} // /for quantity
if($readyToUpdateOrderItem) {
// insert the order item data
for($x = 0; $x < count($_POST['productName']); $x++) {
$updateProductQuantitySql = "SELECT product.quantity FROM product WHERE product.product_id = ".$_POST['productName'][$x]."";
$updateProductQuantityData = $connect->query($updateProductQuantitySql);
while ($updateProductQuantityResult = $updateProductQuantityData->fetch_row()) {
$updateQuantity[$x] = $updateProductQuantityResult[0] - $_POST['quantity'][$x];
// update product table
$updateProductTable = "UPDATE product SET quantity = '".$updateQuantity[$x]."' WHERE product_id = ".$_POST['productName'][$x]."";
$connect->query($updateProductTable);
// add into order_item
$orderItemSql = "INSERT INTO order_item (order_id, product_id, quantity, rate, total, order_item_status)
VALUES ({$orderId}, '".$_POST['productName'][$x]."', '".$_POST['quantity'][$x]."', '".$_POST['rateValue'][$x]."', '".$_POST['totalValue'][$x]."', 1)";
$connect->query($orderItemSql);
} // while
} // /for quantity
}
$valid['success'] = true;
$valid['messages'] = "Successfully Updated";
$connect->close();
echo json_encode($valid);
} // /if $_POST
// echo json_encode($valid);