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

$db = new db_safeguard();
var_dump($_POST);
$order_id = $_POST['record_id'];
$job_card_account_id = (isset($_POST['job_card_account_id']) && strlen($_POST['job_card_account_id']) > 0) ? $_POST['job_card_account_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']) && strlen($_POST['supplier_id']) > 0) ? $_POST['supplier_id'] : 0;
$warrenty = $_POST['warrenty'];
$ordered_for = $_POST['user_id'];
$status = $_POST['status'];
$order_for = $_POST['user_id'];
$order_by = $_POST['user_created'];

$update = "UPDATE `orders` SET `po_number`='$po_number',`jc_number`='$jc_number',`date_time_created`='$order_date',`date_time_recieved`='',`supplier_id`='$supplier_id',`status`='$status',`user_id`=$ordered_for,`job_card_account_id`='$job_card_account_id',`warrenty`='$warrenty',`user_created`=$order_by,`include_vat`='{$_POST['include_vat']}' WHERE record_id = $order_id";
$db->query("orders", $update);

$index = 0;
$db->query("order_items", "DELETE FROM order_items WHERE order_id = $order_id");
while ($index < $_POST['counter']) {
    if (strlen($_POST['item_description' . $index]) >= 1) {
        if (strpos($_POST['item_description' . $index], "|")) {
            echo "bracket";
            $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]})");
            echo "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];
        $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 (strlen($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($_POST['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 = 'edit_order.php?record_id=$order_id';</script>";