<?php

include $_SERVER['DOCUMENT_ROOT'] . "/classes/autoload.php";
try {
    $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>";
    }
}