<?php

require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');


ini_set('display_errors', 1);
error_reporting(E_ERROR);

$zdb = $_SESSION["zdbyear_$dbname"];
mysqli_select_db($conn, $zdb);
date_default_timezone_set("Asia/Kolkata");
$adddate = date("Y-m-d");
$addtime = date("H:i:sa");

$username = $_SESSION["username_" . $dbname];

if (isset($_REQUEST['mprodname'])) {

    $mprodname = $_REQUEST['mprodname'];
    $mhsncode = $_REQUEST['mhsncode'];
    $mcost = $_REQUEST['mcost'];
    $mrate = $_REQUEST['mrate'];
    $mgstperc = $_REQUEST['mgstperc'];

    // PRODUCT CREATION //

    $maindbf = "product";

    $gen1 = mysqli_query($conn, "SELECT MAX(code) AS maxcode FROM `$maindbf` LIMIT 0,1 ") or die(mysqli_error($conn));
    if (mysqli_num_rows($gen1) > 0) {
        $vvouch = mysqli_fetch_assoc($gen1);
        $qvouch = $vvouch["maxcode"] + 1;
    } else {
        $qvouch = '1';
    }

    mysqli_query($conn, "INSERT INTO `$maindbf`(`code`,  `name`, `hsncode`, `rate`, `cost`, `gstperc`)
                                        VALUES ('$qvouch', '$mprodname', '$mhsncode', '$mrate', '$mcost', '$mgstperc')") or die(mysqli_error($conn));

    echo trim($mprodname);
}
