<?php

include $_SERVER['DOCUMENT_ROOT'] . "/classes/autoload.php";
try {


    if ($_POST['cost'] == "") {
        $_POST['cost'] = 0.0;
    }
    if ($_POST['retail'] == "") {
        $_POST['retail'] = 0.0;
    }
    if ($_POST['contractors'] == "") {
        $_POST['contractors'] = 0.0;
    }
    if ($_POST['shops'] == "") {
        $_POST['shops'] = 0.0;
    }

    $client_id = $db->query("stock", "INSERT INTO `stock`(`name`, `cost`, `retail`, `unit_of_measure`, `status`, `supplier_id`,`shops`,`contractors`,`stock_categories`,`code`) VALUES
 ('{$_POST['name']}',
 '" . $function->number_to_save($_POST['cost']) . "',
 '" . $function->number_to_save($_POST['retail']) . "',
 '{$_POST['unit_of_measure']}',
 '{$_POST['status']}',
 '{$_POST['supplier_id']}',
 '" . $function->number_to_save($_POST['shops']) . "',
 '" . $function->number_to_save($_POST['contractors']) . "',
 '{$_POST['stock_categories']}',
 '{$_POST['code']}'
 )");
    if ($client_id > 0) {
        header("location: home.php");
    }
} catch (Exception $e) {
    if (strpos($e->getMessage(), "Duplicate") !== false) {
        echo "<script>alert('Stock already exists!');
        window.location.href = 'home.php';
        </script>";
    }
}