<?php
session_start();
require_once("config/configDB.php"); 

function validation_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $suppliername   = stripslashes($_POST['suppliername']);
    $address1       = stripslashes($_POST['address1']);
    $supplier_state = stripslashes($_POST['state']);
    $pincode        = stripslashes($_POST['pincode']);
    $mobile         = stripslashes($_POST['mobile']);
    $opening_balance          = stripslashes($_POST['opening_balance']);
   
    $suppliercode   = stripslashes($_POST['suppliercode']);
	
    $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(scode) as counts FROM supplier_master WHERE scode = '$suppliercode' "); // 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 = "supplierMasterDashboard.php";</script>
<?php
		

}		
else
{

    $sql  = "INSERT INTO supplier_master (sname, saddress,  supplier_state, pincode, mobile, opening_balance,  scode, isdelete,createdby,userID,createdate) VALUES ('$suppliername', '$address1',  '$supplier_state',  '$pincode', '$mobile', '$opening_balance',   '$suppliercode', '1','".$createdby."','".$userID."','".$createdate."')";

    mysqli_query($conn,$sql);

    $supplierID = mysqli_insert_id($conn);
	
	
	$sql_product = "SELECT * FROM purchase_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'];
		$productname_tamil=$row['productname_tamil'];
        $description  = $row['prodct_description'];
        $qty_type  = $row['qty_type'];
        $quantity  = $row['quantity'];
        $purchase_rate = $row['purchase_rate'];
		$adjustment_qty=$row['adjustment_qty'];


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

$conn->close();
}
    
    header("Location:supplierMasterDashboard.php");

}





?>