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

require "financial_year_calculation.php";


$accountyear        = $financialYearStart."-".$financialYearEnd;

function validation_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
$saleID         = $_POST['edit_id']; 
 $utilize_code   = stripslashes($_POST['utilize_code']);
    $utilizedate       = stripslashes($_POST['utilizedate']);
    
   $contractor =stripslashes($_POST['contractor']);
   $process_type =stripslashes($_POST['process_type']);
   
   $starting_hrs =stripslashes($_POST['starting_hrs']);
   $closing_hrs =stripslashes($_POST['closing_hrs']);
   
   $starting_unit =stripslashes($_POST['starting_unit']);
   $closing_unit =stripslashes($_POST['closing_unit']);
   
   $running_hrs =stripslashes($_POST['running_hrs']);
   $running_unit =stripslashes($_POST['running_unit']);
   
	
    $one   = stripslashes($_POST['one']);
	$two          = stripslashes($_POST['two']);
	 $three          = stripslashes($_POST['three']);
	  $four         = stripslashes($_POST['four']);
	  $six        = stripslashes($_POST['six']);
   $eight          = stripslashes($_POST['eight']);
	 
	$hole   = stripslashes($_POST['hole']);
	$totalfeet   = stripslashes($_POST['totalfeet']);
	$labour   = "0";
	
	
	$cost_feet   = stripslashes($_POST['cost_feet']);
	$total_cost   = stripslashes($_POST['total_cost']);
	$remark   = stripslashes($_POST['remark']);
	
    $createdate  	= date("Y-m-d");
     $createdby      = $_SESSION['username'];
    $userID         = $_SESSION['user_id'];

$explosiveID=$_POST['explosiveID'];
$qty=$_POST['qty'];


$msql2=mysqli_query($conn,"select * from supplier_master where id='".$contractor."'");
        
		while($rw2=mysqli_fetch_array($msql2))
		{
			
		$sname=$rw2['sname'];
			
		}
		
		


$itemcount      = count($explosiveID);

$autoinc1=mysqli_query($conn,"ALTER TABLE explosive_utiliz_hdr CHANGE id id INT(11) NOT NULL AUTO_INCREMENT  ");
	
	$autoinc2=mysqli_query($conn,"ALTER TABLE explosive_utiliz_dtl CHANGE id id INT(11) NOT NULL AUTO_INCREMENT  ");
	
	
	
	$delhdr=mysqli_query($conn,"update  explosive_utiliz_hdr set status='DELETED'  where id='$saleID' and status=''");
$deldtl=mysqli_query($conn,"update  explosive_utiliz_dtl set status='DELETED'  where hdr_id='$saleID' and status=''");	

$count=0;
$check_query	= mysqli_query($conn,"SELECT count(utilize_code) as counts FROM explosive_utiliz_hdr WHERE utilize_code = '$utilize_code' and status=''  "); // 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>";
		?>

<?php
		

}		
else
{



$sql  = "INSERT INTO explosive_utiliz_hdr (utilize_code, utilizedate,contractor,contractor_name,process_type,starting_hrs,closing_hrs,starting_unit,closing_unit,running_hrs,running_unit,  one, two,  three,  four,six,eight,hole,totalfeet,labour,cost_feet,total_cost,remark ,status,isdelete,accountyear,createdby,userID,createdate) VALUES ('$utilize_code', '$utilizedate','$contractor','$sname','$process_type', '$starting_hrs','$closing_hrs','$starting_unit','$closing_unit','$running_hrs','$running_unit', '$one',  '$two',  '$three',  '$four','$six','$eight','$hole','$totalfeet','$labour','$cost_feet', '$total_cost','$remark','','1','".$accountyear."','".$createdby."','".$userID."','".$createdate."')";

//echo $sql;

mysqli_query($conn,$sql);
$last_id = mysqli_insert_id($conn);



 foreach ($explosiveID as $key => $value) {
 
 $prid=$explosiveID[$key];
 $msqpr=mysqli_query($conn,"select * from product_master where id='".$prid."'");
        
		while($rwpr=mysqli_fetch_array($msqpr))
		{
			
		$product_name=$rwpr['productname'];
		
			}

    $sql_saledetails  = "INSERT INTO explosive_utiliz_dtl( hdr_id,utilizedate,explosiveID, explosivename, qty,status,create_date, flag) VALUES ('$last_id', '$utilizedate','$explosiveID[$key]','$product_name','$qty[$key]','','$createdate','1')";
	
	//echo $sql_saledetails;

    mysqli_query( $conn, $sql_saledetails );

}


$conn->close();

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

header("Location:explosive_utilizeDashboard.php");




?>