<?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;

$name = $_POST['name'];
$jc_number = $_POST['jc_number'];
$order_date = str_replace("T", " ", $_POST['order_date']);
$user_created = isset($_POST['user_created']) && !empty($_POST['user_created']) ? $_POST['user_created'] : $_SESSION['user_id'];


$cost_id = $db->query("costs", "INSERT INTO `costs`(`name`, `jc_number`, `date_time_created`, `status`, `user_id`, `job_card_account_id`,`include_vat`) VALUES ('$name','$jc_number','$order_date','1',{$_SESSION['user_id']},'$job_card_account_id','{$_POST['include_vat']}')");

$index = 0;
$order_items_array = [];
while ($index < $_POST['counter']) {
    if (strlen($_POST['item_description' . $index]) >= 1) {
        $description = $_POST['item_description' . $index];
        $quantity = $_POST['quantity' . $index];
        $cost_price = $_POST['cost_price' . $index];
        $db->query("cost_items", "INSERT INTO `cost_items`(`name`, `quantity`, `purchase_price_ex`, `cost_id`, `user_id_created`) VALUES ('$description','$quantity','$cost_price','$cost_id','{$_SESSION['user_id']}')");

    }
    $index++;

}

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

