<?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['mbarcode'])) {

    $mbarcode = $_REQUEST['mbarcode'];
    $mprodname = $_REQUEST['mprodname'];
    $mpkg = $_REQUEST['mpkg'];

    // 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`, `barcode`, `name`, `pkg`)
                                                    VALUES ('$qvouch', '$mbarcode', '$mprodname', '$mpkg')") or die(mysqli_error($conn));

    echo "$qvouch";
}
