    <?php include "../../root.class.php";
$db = new db_safeguard();

echo "<br><br>";

var_dump($_POST);

echo "<br><br>";

// echo "<br>stock_no: $stock_no";
// echo "<br>stock_type: {$_POST['stock_type']}";
// echo "<br>status: {$_POST['status']}";

$supplier_id = $_POST['supplier_id'];
// echo "<br>supplier_id: {$_POST['supplier_id']}";


// echo "<br><br>";

// echo "INSERT INTO `stock_control`(`stock_no`, `stock_type`, `supplier_id`, `status`) VALUES ('$stock_no','{$_POST['stock_type']}','{$supplier_id}','{$_POST['status']}')";

$stock_res = $db->query("stock", "SELECT * FROM stock ORDER BY stock_no DESC LIMIT 1");

if ($stock = $stock_res->fetch_assoc()) {
    //get the last 3 digits in the stock_id
    $last_three_digits = substr($stock["stock_no"], -3);
    $next_three_digits = $last_three_digits + 1;
    $stock_no = "ST" . str_pad($next_three_digits, 3, "0", STR_PAD_LEFT);

} else {
    $stock_no = "ST001";
}

// $stock_record_id = $db->query("stock_control", "INSERT INTO `stock_control`(`stock_no`, `stock_type`, `supplier_id`, `status`) VALUES ('$stock_no','{$_POST['stock_type']}','{$supplier_id}','{$_POST['status']}')");

if ($_POST['stock_type'] == "1") {

    // echo "<br><br>";

    // echo "<br><br>stock_record_id: {$stock_record_id}";
    // echo "<br>item_name: {$_POST['item_name']}";
    // echo "<br>stock_type: {$_POST['stock_type']}";
    // echo "<br>supplier_id: {$supplier_id}";
    // echo "<br>item_price: {$_POST['item_price']}";
    // echo "<br>unit_of_measure: {$_POST['unit_of_measure']}";
    // echo "<br>status: {$_POST['status']}";
    // echo "<br>quantity: {$_POST['quantity']}";
    // echo "<br>vehicle_type: {$_POST['vehicle_type']}";

    // echo "<br><br>";

    // echo "INSERT INTO `stock`(`stock_no`, `item_name`, `stock_type_id`, `supplier_id`, `item_price`, `unit_of_measure`,`vehicle_type`, `status`) VALUES ('$stock_no','{$_POST['item_name']}','{$_POST['stock_type']}','{$supplier_id}','{$_POST['item_price']}','{$_POST['unit_of_measure']}','{$_POST['vehicle_type']}','{$_POST['status']}')";

    $stock_details = $db->query("stock", "INSERT INTO `stock`(`stock_no`, `item_name`, `stock_type_id`, `supplier_id`, `item_price`, `unit_of_measure`,`vehicle_type`, `status`) VALUES ('$stock_no','{$_POST['item_name']}','{$_POST['stock_type']}','{$supplier_id}','{$_POST['item_price']}','{$_POST['unit_of_measure']}','{$_POST['vehicle_type']}','{$_POST['status']}')");

    // echo "<br><br>";

    // echo "INSERT INTO `stock_trans`(`stock_no`, `item_name`, `quantity`) VALUES ('$stock_no','{$_POST['item_name']}','{$_POST['quantity']}')";

    $stock_transactions = $db->query("stock_trans", "INSERT INTO `stock_trans`(`stock_no`, `item_name`, `quantity`) VALUES ('$stock_no','{$_POST['item_name']}','{$_POST['quantity']}')");

    // echo "<br><br>";

} else if ($_POST['stock_type'] == "2") {

    // echo "<br><br>";

    // echo "<br><br>stock_record_id: {$stock_record_id}";
    // echo "<br>item_name: {$_POST['item_name']}";
    // echo "<br>stock_type: {$_POST['stock_type']}";
    // echo "<br>supplier_id: {$supplier_id}";
    // echo "<br>item_price: {$_POST['item_price']}";
    // echo "<br>unit_of_measure: {$_POST['unit_of_measure']}";
    // echo "<br>status: {$_POST['status']}";
    // echo "<br>quantity: {$_POST['quantity']}";
    // echo "<br>pipe_size: {$_POST['pipe_size']}";
    // echo "<br>collar_size: {$_POST['collar_size']}";
    // echo "<br>type_size: {$_POST['type_size']}";

    // echo "<br><br>";

    if ($_POST['type'] == "Pipe") {
        // echo "<br>pipe_size: {$_POST['pipe_size']}";
        $item_name = $_POST['pipe_size'] . " " . $_POST['type'];

        // echo "<br><br>";

        // echo "INSERT INTO `stock`(`stock_no`, `stock_type_id`, `item_name`, `type`, `size`,`supplier_id`, `item_price`, `unit_of_measure`, `status`) VALUES ('$stock_no','{$_POST['stock_type']}','{$item_name}','{$_POST['type']}','{$_POST['pipe_size']}','{$supplier_id}','{$_POST['item_price']}','{$_POST['unit_of_measure']}','{$_POST['status']}')";

        $stock_details = $db->query("stock", "INSERT INTO `stock`(`stock_no`, `stock_type_id`, `item_name`, `type`, `size`,`supplier_id`, `item_price`, `unit_of_measure`, `status`) VALUES ('$stock_no','{$_POST['stock_type']}','{$item_name}','{$_POST['type']}','{$_POST['pipe_size']}','{$supplier_id}','{$_POST['item_price']}','{$_POST['unit_of_measure']}','{$_POST['status']}')");

        // echo "<br><br>";

        // echo "INSERT INTO `stock_trans`(`stock_no`, `item_name`, `quantity`) VALUES ('$stock_no','{$item_name}','{$_POST['quantity']}')";

        $stock_transactions = $db->query("stock_trans", "INSERT INTO `stock_trans`(`stock_no`, `item_name`, `quantity`) VALUES ('$stock_no','{$item_name}','{$_POST['quantity']}')");

    } else {
        // echo "<br>collar_size: {$_POST['collar_size']}";
        // echo "<br>type_size: {$_POST['type_size']}";
        $item_name = $_POST['collar_size'] . " " . $_POST['type_size'];
        // echo "<br><br>";

        // echo "INSERT INTO `stock`(`stock_no`, `stock_type_id`,`item_name`, `type`, `size`, `supplier_id`, `item_price`, `unit_of_measure`, `status`) VALUES ('$stock_no','{$_POST['stock_type']}', '$item_name', '{$_POST['type_size']}', '{$_POST['collar_size']}','{$supplier_id}','{$_POST['item_price']}','{$_POST['unit_of_measure']}','{$_POST['status']}')";

        $stock_details = $db->query("stock", "INSERT INTO `stock`(`stock_no`, `stock_type_id`,`item_name`, `type`, `size`, `supplier_id`, `item_price`, `unit_of_measure`, `status`) VALUES ('$stock_no','{$_POST['stock_type']}', '$item_name', '{$_POST['type_size']}', '{$_POST['collar_size']}','{$supplier_id}','{$_POST['item_price']}','{$_POST['unit_of_measure']}','{$_POST['status']}')");

        // echo "<br><br>";

        // echo "INSERT INTO `stock_trans`(`stock_no`, `item_name`, `quantity`) VALUES ('$stock_no','$item_name','{$_POST['quantity']}')";

        $stock_transactions = $db->query("stock_trans", "INSERT INTO `stock_trans`(`stock_no`, `item_name`, `quantity`) VALUES ('$stock_no','$item_name','{$_POST['quantity']}')");

    }

} else if ($_POST['stock_type'] == "3") {

    // echo "<br><br>stock_record_id: {$stock_record_id}";
    // echo "<br>item_name: {$_POST['item_name']}";
    // echo "<br>stock_type: {$_POST['stock_type']}";
    // echo "<br>supplier_id: {$supplier_id}";
    // echo "<br>status: {$_POST['status']}";

    $quantity = 1;

    // echo "<br><br>";

    // echo "INSERT INTO `stock`(`stock_no`, `item_name`, `stock_type_id`, `supplier_id`, `status`) VALUES ('$stock_no','{$_POST['item_name']}','{$_POST['stock_type']}','{$supplier_id}','{$_POST['status']}')";

    $stock_details = $db->query("stock", "INSERT INTO `stock`(`stock_no`, `item_name`, `stock_type_id`, `supplier_id`, `pump_code`, `status`) VALUES ('$stock_no','{$_POST['item_name']}','{$_POST['stock_type']}','{$supplier_id}','{$_POST['pump_code']}','{$_POST['status']}')");

    // echo "<br><br>";

    // echo "INSERT INTO `stock_trans`(`stock_no`, `item_name`, `quantity`) VALUES ('$stock_no','{$_POST['item_name']}','{$quantity}')";

    $stock_transactions = $db->query("stock_trans", "INSERT INTO `stock_trans`(`stock_no`, `item_name`, `quantity`) VALUES ('$stock_no','{$_POST['item_name']}','{$quantity}')");

} else if ($_POST['stock_type'] == "4") {

    // echo "<br><br>";

    // echo "<br><br>stock_record_id: {$stock_record_id}";
    // echo "<br>item_name: {$_POST['item_name']}";
    // echo "<br>stock_type: {$_POST['stock_type']}";
    // echo "<br>supplier_id: {$supplier_id}";
    // echo "<br>item_price: {$_POST['item_price']}";
    // echo "<br>unit_of_measure: {$_POST['unit_of_measure']}";
    // echo "<br>status: {$_POST['status']}";
    // echo "<br>quantity: {$_POST['quantity']}";
    // echo "<br>serial_number: {$_POST['serial_number']}";

    // echo "<br><br>";

    // echo "INSERT INTO `stock`(`stock_no`, `item_name`, `serial_number`,`stock_type_id`, `supplier_id`, `item_price`, `unit_of_measure`, `status`) VALUES ('$stock_no','{$_POST['item_name']}','{$_POST['serial_number']}','{$_POST['stock_type']}','{$supplier_id}','{$_POST['item_price']}','{$_POST['unit_of_measure']}','{$_POST['status']}')";

    $stock_details = $db->query("stock", "INSERT INTO `stock`(`stock_no`, `item_name`, `serial_number`,`stock_type_id`, `supplier_id`, `item_price`, `unit_of_measure`, `status`) VALUES ('$stock_no','{$_POST['item_name']}','{$_POST['serial_number']}','{$_POST['stock_type']}','{$supplier_id}','{$_POST['item_price']}','{$_POST['unit_of_measure']}','{$_POST['status']}')");

    // echo "<br><br>";

    // echo "INSERT INTO `stock_trans`(`stock_no`, `item_name`, `quantity`) VALUES ('$stock_no','{$_POST['item_name']}','{$_POST['quantity']}')";

    $stock_transactions = $db->query("stock_trans", "INSERT INTO `stock_trans`(`stock_no`, `item_name`, `quantity`) VALUES ('$stock_no','{$_POST['item_name']}','{$_POST['quantity']}')");

} else if ($_POST['stock_type'] == "5") {

    // echo "<br><br>";

    // echo "<br><br>stock_record_id: {$stock_record_id}";
    // echo "<br>item_name: {$_POST['item_name']}";
    // echo "<br>stock_type: {$_POST['stock_type']}";
    // echo "<br>supplier_id: {$supplier_id}";
    // echo "<br>item_price: {$_POST['item_price']}";
    // echo "<br>unit_of_measure: {$_POST['unit_of_measure']}";
    // echo "<br>status: {$_POST['status']}";
    // echo "<br>quantity: {$_POST['quantity']}";

    // echo "<br><br>";

    // echo "INSERT INTO `stock`(`stock_no`, `item_name`, `stock_type_id`, `supplier_id`, `item_price`, `unit_of_measure`, `status`) VALUES ('$stock_no','{$_POST['item_name']}','{$_POST['stock_type']}','{$supplier_id}','{$_POST['item_price']}','{$_POST['unit_of_measure']}','{$_POST['status']}')";

    $item_name = $_POST['bn_type'] . " X " . $_POST['bn_size'];

    $stock_details = $db->query("stock", "INSERT INTO `stock`(`stock_no`, `item_name`, `type`, `size`, `stock_type_id`, `supplier_id`, `item_price`, `unit_of_measure`, `status`) VALUES ('$stock_no','{$item_name}','{$_POST['bn_type']}','{$_POST['bn_size']}','{$_POST['stock_type']}','{$supplier_id}','{$_POST['item_price']}','{$_POST['unit_of_measure']}','{$_POST['status']}')");

    // echo "<br><br>";

    // echo "INSERT INTO `stock_trans`(`stock_no`, `item_name`, `quantity`) VALUES ('$stock_no','{$_POST['item_name']}','{$_POST['quantity']}')";

    $stock_transactions = $db->query("stock_trans", "INSERT INTO `stock_trans`(`stock_no`, `item_name`, `quantity`) VALUES ('$stock_no','{$_POST['item_name']}','{$_POST['quantity']}')");

} else if ($_POST['stock_type'] == "6") {

    // echo "<br><br>";

    // echo "<br><br>stock_record_id: {$stock_record_id}";
    // echo "<br>item_name: {$_POST['item_name']}";
    // echo "<br>stock_type: {$_POST['stock_type']}";
    // echo "<br>supplier_id: {$supplier_id}";
    // echo "<br>item_price: {$_POST['item_price']}";
    // echo "<br>unit_of_measure: {$_POST['unit_of_measure']}";
    // echo "<br>status: {$_POST['status']}";
    // echo "<br>quantity: {$_POST['quantity']}";

    // echo "<br><br>";

    // echo "INSERT INTO `stock`(`stock_no`, `item_name`, `stock_type_id`, `supplier_id`, `item_price`, `unit_of_measure`, `status`) VALUES ('$stock_no','{$_POST['item_name']}','{$_POST['stock_type']}','{$supplier_id}','{$_POST['item_price']}','{$_POST['unit_of_measure']}','{$_POST['status']}')";

    $stock_details = $db->query("stock", "INSERT INTO `stock`(`stock_no`, `item_name`, `stock_type_id`, `supplier_id`, `item_price`, `unit_of_measure`, `status`) VALUES ('$stock_no','{$_POST['item_name']}','{$_POST['stock_type']}','{$supplier_id}','{$_POST['item_price']}','{$_POST['unit_of_measure']}','{$_POST['status']}')");

    // echo "<br><br>";

    // echo "INSERT INTO `stock_trans`(`stock_no`, `item_name`, `quantity`) VALUES ('$stock_no','{$_POST['item_name']}','{$_POST['quantity']}')";

    $stock_transactions = $db->query("stock_trans", "INSERT INTO `stock_trans`(`stock_no`, `item_name`, `quantity`) VALUES ('$stock_no','{$_POST['item_name']}','{$_POST['quantity']}')");

}

?>
<script>
    alert("Stock Added Successfully");
</script>
<?php

echo "<br><br>";

echo "<script>window.location.href = '../stock_control/home.php';</script>";