<?php
date_default_timezone_set('Asia/Kolkata');
require_once("config/configDB.php"); 
require "userPermission.php";

$productcode   = stripslashes($_POST['productcode']);
$productname   = stripslashes($_POST['productname']);

$qty_type   = stripslashes($_POST['qty_type']);

$purchase_rate   = stripslashes($_POST['purchase_rate']);
$gst   = stripslashes($_POST['gst']);

$description   = stripslashes($_POST['description']);

$hsn=$_POST['hsn_code'];
//$create_date  	        = date("Y-m-d H:i:s"); 

$createdate  	= date("Y-m-d");
    $createdby      = $_SESSION['username'];
    $userID         = $_SESSION['user_id'];

$autoinc=mysqli_query($conn,"ALTER TABLE purchase_product_master CHANGE id id INT(11) NOT NULL AUTO_INCREMENT ");
$autoinc1=mysqli_query($conn,"ALTER TABLE product_mapping_supplier CHANGE id id INT(11) NOT NULL AUTO_INCREMENT ");

		$count=0;
$check_query	= mysqli_query($conn,"SELECT count(productcode) as counts FROM purchase_product_master WHERE productcode = '$productcode' "); // Check the database


while($row = mysqli_fetch_array($check_query)){
$count=$row['counts'];
}
if($count!=0){
// Already Exsist


		echo "<script> alert ('Data Inserted Success.');</script>";
		?>
<script language="javascript">window.location.href = "purchase_productMasterDashboard.php";</script>
<?php
		

}		
else
{


$sql  = "INSERT INTO purchase_product_master (productcode, productname,qty_type, purchase_rate, gst,hsn, prodct_description, createddate,createdby,userId,isdelete) VALUES ('$productcode','$productname','$qty_type','$purchase_rate','$gst','$hsn','$description','$createdate', '$createdby','0','1')";


mysqli_query($conn,$sql);

$last_id = mysqli_insert_id($conn);



$sql_customer = "SELECT id FROM supplier_master";
$result = $conn->query($sql_customer);

/*if ($result->num_rows > 0) {
  
    while($row1 = $result->fetch_assoc()) {

        $supplierID  = $row1['id'];

        $sql_mapping  = "INSERT INTO product_mapping_supplier (productID, supplierID, productcode, productname, prodct_description,qty_type, quantity,adjustment_qty,  purchase_rate, sale_rate) VALUES ('$last_id','$supplierID','$productcode','$productname','$description','$qty_type','$quantity','$adjustment_qty','$purchase_rate',0)";
        
        mysqli_query($conn,$sql_mapping);
        
    }
} 
*/


 

$conn->close();
}

header("Location:purchase_productMasterDashboard.php");


?>