-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_product.php
68 lines (55 loc) · 2.35 KB
/
fetch_product.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
<?php
include("./layouts/session.php"); // start session
include 'conn.php'; // Include database connection
// Establish the connection to the user's database
$conn = connectMainDB();
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Get the barcode from the request
$input = json_decode(file_get_contents("php://input"), true);
$barcode = $input['barcode'];
// Get the email from the session
$email = htmlspecialchars($_SESSION['email']);
// Query to fetch product details by barcode
$query = $conn->prepare(
"SELECT id, email, product_name, price, discount_type, discount_value, tax_value, unit, image
FROM products WHERE product_barcode = ? AND email = ?");
// Bind both the barcode and email to the query
$query->bind_param('ss', $barcode, $email);
$query->execute();
$result = $query->get_result();
if ($result->num_rows > 0) {
$product = $result->fetch_assoc();
// Calculate the discount and tax
$price = $product['price'];
$discount_type = $product['discount_type'];
$discount_value = $product['discount_value'];
$tax_value = $product['tax_value'];
// Handle discount logic
if ($discount_type == 'Percentage') {
$discounted_amount = $price * ($discount_value / 100); // Percentage discount
} else {
$discounted_amount = $discount_value; // Cash discount
}
// Calculate total cost after discount
$total_cost_after_discount = $price - $discounted_amount;
// Add tax to the total cost
$total_cost = $total_cost_after_discount + $tax_value;
echo json_encode([
'success' => true,
'product' => [
'name' => $product['product_name'],
'price' => $product['price'],
'discount_type' => $product['discount_type'],
'discount_value' => $product['discount_value'],
'tax_value' => $product['tax_value'],
'unit' => $product['unit'],
'total_cost' => $total_cost,
'image_url' => $product['image'] // assuming you have an image URL field
]
]);
} else {
echo json_encode(['success' => false, 'message' => 'Product not found']);
}
$conn->close();
}