<?php
include_once 'shikanisha.kts.php';

// Get the data from the AJAX request
$product_name = $_POST['product_name'];
$company = $_POST['company'];
$buying_price = $_POST['buying_price'];
$selling_price = $_POST['selling_price'];
$store_quantity = $_POST['store_quantity'];
$shop_quantity = $_POST['shop_quantity'];
$id = $_POST['id'];
$date = date('Y-m-d');
$quantity = ($store_quantity + $shop_quantity);
$subtotal = ($quantity * $buying_price);
$transaction_type = "Debit";
$category = $_POST['categoryz'];

// Initialize the response array
$response = array();

// Insert the data into the database
$sql = "INSERT INTO products (product_name, company, buying_price, selling_price, store_quantity, shop_quantity,  category)
VALUES ('$product_name', '$company', '$buying_price', '$selling_price', '$store_quantity', '$shop_quantity',  '$category')";

if ($conn->query($sql) === TRUE) {
    $response['success'] = true;
    $response['message'] = "Product added successfully";
    
    // Process vendor transaction
    $sql = "SELECT amount_owed FROM vendor_debts WHERE vendor_id = '$id'";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        $current_amount_owed = $row['amount_owed'];
        $new_amount_owed = $current_amount_owed + $subtotal;
        $sql = "UPDATE vendor_debts SET amount_owed = '$new_amount_owed' WHERE vendor_id = '$id'";
        if ($conn->query($sql) === TRUE) {
            $sql = "INSERT INTO vendor_transactions (vendor_id, amount_transacted, transaction_type, date) VALUES ('$id', '$subtotal', '$transaction_type', '$date')";
            if ($conn->query($sql) === TRUE) {
                // Add product to purchase_orders
                $purchase_date = date('Y-m-d');
                $purchase_sql = "INSERT INTO purchase_orders (productname, price, quantity, subtotal) VALUES (?, ?, ?, ?)";
                $purchase_stmt = $conn->prepare($purchase_sql);
                $purchase_stmt->bind_param("sdid",$product_name, $buying_price, $quantity, $subtotal);
                $purchase_result = $purchase_stmt->execute();

                if ($purchase_result) {
                    $response['purchase_success'] = true;
                    $response['purchase_message'] = "Product added to purchases successfully";
                } else {
                    $response['purchase_success'] = false;
                    $response['purchase_error'] = "Record not added to purchases successfully";
                }
            } else {
                $response['transaction_error'] = "Error inserting transaction record: " . $conn->error;
            }
        } else {
            $response['debt_error'] = "Error updating vendor debt record: " . $conn->error;
        }
    } else {
        $response['debt_error'] = "Vendor debt not found";
    }
} else {
    $response['success'] = false;
    $response['error'] = "Error inserting product record: " . $conn->error;
}

// Close the connection
$conn->close();

// Send JSON response
header('Content-Type: application/json;charset=utf-8');
echo json_encode($response);
?>
