<?php
include "../../root.class.php";

$db = new db_safeguard();
var_dump($_POST);

$job_card_account_id = isset($_POST['jobcard_acount_id']) && !empty($_POST['jobcard_acount_id']) ? $_POST['jobcard_acount_id'] : 0;

$po_number = $_POST['po_number'];
$jc_number = $_POST['jc_number'];
$order_date = str_replace("T", " ", $_POST['order_date']);
$supplier_id = isset($_POST['supplier_id']) && !empty($_POST['supplier_id']) ? $_POST['supplier_id'] : 0;
$warrenty = $_POST['warrenty'];
$ordered_for = (isset($_POST['ordered_for']) ? $_POST['ordered_for'] : 0);
$user_created = isset($_POST['user_created']) && !empty($_POST['user_created']) ? $_POST['user_created'] : $_SESSION['user_id'];
if (isset($_POST['close_order']) && $_POST['close_order'] == "on") {
    $status = 1;
} else {
    $status = 0;
}

$order_id = $db->query("orders", "INSERT INTO `orders`(`po_number`, `jc_number`, `date_time_created`, `date_time_recieved`, `supplier_id`, `status`, `user_id`, `job_card_account_id`, `warrenty`, `user_created`,`include_vat`) VALUES ('$po_number','$jc_number','$order_date','','$supplier_id','$status',$ordered_for,'$job_card_account_id','$warrenty','$user_created','{$_POST['include_vat']}')");

$index = 0;
$order_items_array = [];
while ($index < $_POST['counter']) {
    if (strlen($_POST['item_description' . $index]) >= 1) {
        if (strpos($_POST['item_description' . $index], "|")) {
            $description = explode(" | ", $_POST['item_description' . $index])[1];
            $description = str_replace(array("\r", "\n"), '', $description);
            $item_id = explode(" | ", $_POST['item_description' . $index])[0];

        } else {

            $description = $_POST['item_description' . $index];
            $description = str_replace(array("\r", "\n"), '', $description);
            $item_id = $db->query("stock", "INSERT INTO `stock`(`category_id`, `supplier_id`, `description`, `packaging_size`, `cost_price`, `max`, `min`, `unit_type_id`, `barcode`, `stock_type`) VALUES (-1,'$supplier_id','$description',0,0,0,0,0,0,{$_POST['stock_type_' . $index]})");
        }
        $quantity = $_POST['quantity' . $index];
        $cost_price = $_POST['cost_price' . $index];
       // echo "<br>" . "INSERT INTO `order_items`(`name`, `stock_id`, `quantity`, `purchase_price_ex`, `order_id`, `user_id_created`, `user_id_received`,`stock_type`) VALUES ('$description','$item_id','$quantity','$cost_price','$order_id','{$_SESSION['user_id']}','0',{$_POST['stock_type_' . $index]})";

        $db->query("order_items", "INSERT INTO `order_items`(`name`, `stock_id`, `quantity`, `purchase_price_ex`, `order_id`, `user_id_created`, `user_id_received`,`stock_type`) VALUES ('$description','$item_id','$quantity','$cost_price','$order_id','{$_SESSION['user_id']}','0',{$_POST['stock_type_' . $index]})");

        $order_items_array[] = "<tr><td> " . $description . " </td><td> " . $quantity . " </td><td> " . $cost_price . " </td></tr>";
    }
    $index++;

}


if ($job_card_account_id != 0) {
    $call = new call_functions();
    $tool_res = $db->query("job_card_accounts", "SELECT * FROM `job_card_accounts` WHERE `record_id` = '$job_card_account_id'");
    $tool = $tool_res->fetch_assoc();
    if ((strpos($tool['name'], "ST-") !== false || strpos($tool['name'], "TA-") !== false) && count($order_items_array) > 0 && strlen($tool['name']) > 0) {
       $call->send_for_email(['chanay@midrandac.co.za'], ['Chanay'], "Tool Account Order : " . $tool['name'], "
        <html>
        <body>
        <h1>Tool Account Order : " . $tool['name'] . "</h1>
        <p>PO Number : " . $_POST['po_number'] . "</p>
        <p>JC Number : " . $_POST['jc_number'] . "</p>
        <p>Supplier : " . $call->get_supplier_name($supplier_id ) . "</p>
        <p>Order Date : " . $_POST['order_date'] . "</p>
        <p>Ordered For : " . $_POST['ordered_for'] . "</p><table style='width:100%'><th>Item</th><th>Quantity</th><th>Cost Price</th>" . implode("", $order_items_array) . "</table>
        </body>
        </html>
        ");
    }
}

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