forked from ronilcajan/point-of-sale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert_sales.php
executable file
·76 lines (64 loc) · 2.39 KB
/
insert_sales.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
<?php include 'server/connection.php';
if(isset($_POST['product'])){
$user = $_SESSION['username'];
$discount = $_POST['discount'];
$total = $_POST['totalvalue'];
$price = $_POST['price'];
$product = $_POST['product'];
$customer = $_POST['customer'];
$quantity = $_POST['quantity'];
$reciept = array();
$query = '';
$customer_id = mysqli_query($db, "SELECT customer_id FROM customer WHERE CONCAT(firstname,' ',lastname) LIKE '$customer'");
if(mysqli_num_rows($customer_id) == 0){
echo "failure";
}else{
$cust_id = mysqli_fetch_array($customer_id);
$cust_id_new = $cust_id['customer_id'];
$sql = "INSERT INTO sales(customer_id,username,discount,total) VALUES($cust_id_new,'$user',$discount, $total)";
$result = mysqli_query($db,$sql);
if($result == true){
$select = "SELECT reciept_no FROM sales ORDER BY reciept_no DESC LIMIT 1";
$res = mysqli_query($db,$select);
$id = mysqli_fetch_array($res);
for($i = 0; $i < count($product); $i++){
$reciept[] = $id[0];
}
for($num=0; $num<count($product); $num++){
$product_id = mysqli_real_escape_string($db, $product[$num]);
$qtyold = mysqli_real_escape_string($db, $quantity[$num]);
$sql1 = "SELECT quantity FROM products WHERE product_no='$product_id'";
$result1 = mysqli_query($db, $sql1);
$qty = mysqli_fetch_array($result1);
$newqty = $qty['quantity'] - $qtyold;
$sql2 = "UPDATE products SET quantity=$newqty WHERE product_no='$product_id'";
$result2 = mysqli_query($db, $sql2);
}
$query1 = "INSERT INTO logs (username,purpose) VALUES('$user','Product sold')";
$insert = mysqli_query($db,$query1);
for($count = 0; $count < count($product); $count++){
$price_clean = mysqli_real_escape_string($db, $price[$count]);
$reciept_clean = mysqli_real_escape_string($db, $reciept[$count]);
$product_clean = mysqli_real_escape_string($db, $product[$count]);
$quantity_clean = mysqli_real_escape_string($db, $quantity[$count]);
if($product_clean != '' && $quantity_clean != '' && $price_clean != '' && $reciept_clean != ''){
$query .= "
INSERT INTO sales_product(reciept_no,product_id,price,qty)
VALUES($reciept_clean,$product_clean,$price_clean,$quantity_clean);
";
}
}
}else{
echo "failure";
}
if ($query != ''){
if(mysqli_multi_query($db,$query)){
echo "success";
}else{
echo "failure";
}
}else{
echo 'failure';
}
}
}