<?php
session_start();
require_once("config/configDB.php"); 
require "userPermission.php";
function validation_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

 $customername   = stripslashes($_POST['customername']);
    $address1       = stripslashes($_POST['address1']);
    $customer_state = stripslashes($_POST['state']);
    $pincode        = stripslashes($_POST['pincode']);
    $mobile         = stripslashes($_POST['mobile']);
    $opening_balance          = stripslashes($_POST['opening_balance']);
	$gstin=$_POST['gstin'];
   
    $customercode   = stripslashes($_POST['customercode']);
    $createdate  	= date("Y-m-d");
    $createdby      = stripslashes($_POST['createdby']);
    $userID         = stripslashes($_POST['userID']);

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

		$count=0;
$check_query	= mysqli_query($conn,"SELECT count(ccode) as counts FROM customer_master WHERE ccode = '$customercode' "); // 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 = "customerMasterDashboard.php";</script>
<?php
		

}		
else
{


$sql  = "INSERT INTO customer_master (cname, caddress,gstin,  customer_state, pincode, mobile, opening_balance, ccode, isdelete,createdby,userID,createdate) VALUES ('$customername', '$address1', '$gstin', '$customer_state',  '$pincode', '$mobile', '$opening_balance',   '$customercode', '1','".$createdby."','".$userID."','".$createdate."')";

mysqli_query($conn,$sql);

$customerID = mysqli_insert_id($conn);

$sql_product = "SELECT * FROM product_master";
$result = $conn->query($sql_product);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {

        $productID  = $row['id'];
        $productcode  = $row['productcode'];
        $productname  = $row['productname'];
        $description  = $row['prodct_description'];
        $qty_type  = $row['qty_type'];
        $gst  = $row['gst'];
        
		   
		   
		   $purchase_rate=$row['purchase_rate'];


        $sql_mapping  = "INSERT INTO product_mapping_customer (productID, customerID, productcode, productname, prodct_description, qty_type,  purchase_rate, sale_rate) VALUES ('$productID','$customerID','$productcode','$productname','$description','$qty_type','$purchase_rate',0)";
        
        mysqli_query($conn,$sql_mapping);
        
    }
} 
}
$conn->close();

// productID, customerID, productcode, productname, prodct_description, hsn_code, gst, purchase_rate, sale_rate


header("Location:customerMasterDashboard.php");




?>