<?php 
include 'connection.php';
session_start();
if (!isset($_SESSION['is_logged_in'])) {
    header("Location:login.php");
    die();
    }
function randomPassword() {
    $alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $pass = array(); //remember to declare $pass as an array
    $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
    for ($i = 0; $i < 5; $i++) {
        $n = rand(0, $alphaLength);
        $pass[] = $alphabet[$n];
    }
    return implode($pass); //turn the array into a string
}

	date_default_timezone_set('Asia/Calcutta');
$today = date("d/m/y");
$today1 = date("d-m-Y");
$today2 = date("Y-m-d");
$received_time = date("Y-m-d H:i:s");

$currentyear = date("Y");
$currentmonth = date("m");

if($currentmonth>03) { 
$cuyear = date("Y");
$cuyear1 = date("y")+1;
} 
else { 
$cuyear = date("Y")-1;
$cuyear1 =  date("y");
} 
$f_year = "$cuyear"."-"."$cuyear1";


$unique_id = randomPassword();	

$querylogin = "select * from  mst_users where user_name = '". $_SESSION["LOGIN_NAME"]."' AND password = '".$_SESSION["EMPLOYEE_PASSWORD"]."'";
$resultlogin = mysqli_query($conn,$querylogin);
while($row=mysqli_fetch_array($resultlogin))
{
$user_name = $row['user_name'];
$user_id09 = $row['user_id'];
$login_type = $row['login_type'];

}	
?>



<?php
$delivery_challan_no=$_POST['delivery_challan_no'];
$delivery_challan_date=$_POST['delivery_challan_date'];
$vehicle_no=$_POST['vehicle_no'];
$e_way_bill_no=$_POST['e_way_bill_no'];
$lr_no=$_POST['lr_no'];
$query1 = "update delivery_challan set vehicle_no='$vehicle_no',e_way_bill_no='$e_way_bill_no',lr_no='$lr_no'  where customer_id = '".$_REQUEST['customer_id']."' and delivery_challan_id = '".$_REQUEST['delivery_challan_id']."' " ;
//echo $query1;
$r1=mysqli_query($conn,$query1);
$i='1';
$query2="select * from delivery_challan_more_product_02 where delivery_challan_id = '".$_REQUEST['delivery_challan_id']."' AND customer_id = '".$_REQUEST['customer_id']."' AND deleted !='YES'  ORDER BY delivery_challan_more_product_id asc";//echo $query2;
$result=mysqli_query($conn,$query2);
$num_rows = mysqli_num_rows($result);
while($row=mysqli_fetch_array($result))
		{  
		$row_id=$row['row_id'];
		$product_id=$row['product_id'];
$delivery_challan_more_product_id = $row['delivery_challan_more_product_id'];
$componant =  $_POST['componant'.$delivery_challan_more_product_id];
 $description = mysqli_real_escape_string($conn,$_POST['description'.$delivery_challan_more_product_id]);

$cust_po_more_product_id = $_POST['cust_po_more_product_id'.$delivery_challan_more_product_id];
$cust_more_row_id = $_POST['cust_more_row_id'.$delivery_challan_more_product_id];
$material_code = $_POST['material_code'.$delivery_challan_more_product_id];
$finish_shaft_wt_kg = $_POST['finish_shaft_wt_kg'.$delivery_challan_more_product_id];
$turning_scrap_wt = $_POST['turning_scrap_wt'.$delivery_challan_more_product_id];

$use_kg_scrap = $_POST['use_kg_scrap'.$delivery_challan_more_product_id];

$use_kg = $_POST['use_kg'.$delivery_challan_more_product_id];
$qty = $_POST['qty'.$delivery_challan_more_product_id];
$unit = $_POST['unit'.$delivery_challan_more_product_id];

 
 $qry97 = "select * from customer_po_more_product_02 where product_id='$product_id' and cust_po_more_product_id='$cust_po_more_product_id'";
$re97 = mysqli_query($conn,$qry97);
while($row=mysqli_fetch_array($re97))
{
$po_number = $row['po_number'];
 $rate=$row['rate'];

}
$total='';
$qry97 = "select * from cust_inward_more_row_02 where row_id='$row_id' and cust_more_row_id='$cust_more_row_id'";
$re97 = mysqli_query($conn,$qry97);
while($row=mysqli_fetch_array($re97))
{
$challan_no = $row['challan_no'];
}
$total=$qty*$rate;


 $query1 = "update delivery_challan_more_product_02 set sr_no='$i',delivery_challan_date='$delivery_challan_date',componant='$componant',description='$description',cust_po_more_product_id='$cust_po_more_product_id',cust_po='$po_number',inward_challan_no='$challan_no', cust_more_row_id = '$cust_more_row_id', material_code= '$material_code',finish_shaft_wt_kg='$finish_shaft_wt_kg', turning_scrap_wt='$turning_scrap_wt',use_kg = '$use_kg',use_kg_scrap='$use_kg_scrap', qty ='$qty',unit='$unit',rate_of_po='$rate',total='$total'  where customer_id = '".$_REQUEST['customer_id']."' and delivery_challan_id = '".$_REQUEST['delivery_challan_id']."' and delivery_challan_more_product_id='$delivery_challan_more_product_id'" ;
//echo $query1;
$r1=mysqli_query($conn,$query1);

$i++; 
}











date_default_timezone_set('Asia/Calcutta');

$today = date("Y-m-d");

$time = date("Y-m-d H:i:s");

$currentyear = date("Y");

$currentmonth = date("m");



if($currentmonth>03) { 

$cuyear = date("Y");

$cuyear1 = date("y")+1;

} 

else { 

$cuyear = date("Y")-1;

$cuyear1 =  date("y");

} 

$f_year = "$cuyear"."-"."$cuyear1";



$query1 = "update delivery_challan set delivery_challan_date='$delivery_challan_date' where customer_id = '".$_REQUEST['customer_id']."' and delivery_challan_id = '".$_REQUEST['delivery_challan_id']."' " ;
//echo $query1;
$r1=mysqli_query($conn,$query1);

	
if (isset($_POST['Submit'])=="Save")

{

	


	
		$query1 = "update delivery_challan_more_product_02 set delivery_challan_no='$delivery_challan_no',status='SAVE'  where customer_id = '".$_REQUEST['customer_id']."' and delivery_challan_id = '".$_REQUEST['delivery_challan_id']."' " ;
//echo $query1;
$r1=mysqli_query($conn,$query1);
	
	
	$query1 = "update delivery_challan set   where customer_id = '".$_REQUEST['customer_id']."' and delivery_challan_id = '".$_REQUEST['delivery_challan_id']."' " ;
//echo $query1;
$r1=mysqli_query($conn,$query1);
//exit();
			
?>

<script>
javascript:window.location.href="view_all_delivery_challan.php?customer_id=<?php echo $_REQUEST['customer_id']; ?>&&customer_name=<?php echo $_REQUEST['customer_name']?>&&delivery_challan_id=<?php echo $delivery_challan_id?>&&company_name=<? echo $_REQUEST['company_name']?>";
</script>
<?php



}/////submit slosed

if (isset($_POST[PRO_ADD])=="ADD")
{


$delivery_challan_no=$_POST['delivery_challan_no'];
$delivery_challan_date=$_POST['delivery_challan_date'];

 $queryls = "select * from mst_customer where customer_id = '".$_REQUEST['customer_id']."'";

// $queryls = "select * from mst_customer where customer_id = 14";
$resultls = mysqli_query($conn,$queryls);	

while($row=mysqli_fetch_array($resultls))

{

$customer_id=$row['customer_id'];
 $customer_name=$row['customer_name'];

$customer_address=$row['billing_address'];
$customer_state=$row['state'];
$customer_state_code=$row['state_code'];
$customer_email_id=$row['email_id'];
$customer_GST_no=$row['billing_GST_no'];
$customer_PAN_no = $row['billing_PAN_no'];
$customer_pincode = $row['pincode'];
$customer_tel_no=$row['tel_no'];

$vendor_code=$row['vendor_code'];
$buyer_address=$row['buyer_address'];
$buyer_GST_no=$row['buyer_GST_no'];
$buyer_PAN_no=$row['buyer_PAN_no'];
$buyer_state=$row['buyer_state'];
$buyer_state_code=$row['buyer_state_code'];
$buyer_name = $row['buyer_name'];
$customer_cell_no = $row['cell_no'];


}


if($_REQUEST['delivery_challan_id']==''){ 									


  $qryauto="insert into delivery_challan(delivery_challan_no,delivery_challan_date,customer_id,customer_name,customer_address,customer_state,customer_state_code,customer_email_id,customer_GST_no,customer_PAN_no,customer_pincode,customer_tel_no,vendor_code,buyer_address,buyer_GST_no,buyer_PAN_no,buyer_state,buyer_state_code,buyer_name,customer_cell_no,entry_date,entry_by,unique_id) values ('$delivery_challan_no','$delivery_challan_date','$customer_id','$customer_name','$customer_address','$customer_state','$customer_state_code','$customer_email_id','$customer_GST_no','$customer_PAN_no','$customer_pincode','$customer_tel_no','$vendor_code','$buyer_address','$buyer_GST_no','$buyer_PAN_no','$buyer_state','$buyer_state_code','$buyer_name','$customer_cell_no','$today','$user_name','$unique_id')";
$rauto=mysqli_query($conn,$qryauto);
//echo $rauto;
//exit();
$qry3z = "select * from delivery_challan where  unique_id='$unique_id' and customer_id='$customer_id' and entry_date='$today'";
$re3z= mysqli_query($conn,$qry3z);
while($row=mysqli_fetch_array($re3z))
		{  
		$delivery_challan_id = $row['delivery_challan_id'];
		}
}else {

$delivery_challan_id=$_REQUEST['delivery_challan_id'];
}

//exit();

$product_id=$_REQUEST['product_id'];
if (is_numeric($product_id)) 
 {

$qry97 = "select * from finish_product_master where product_id='$product_id'";
$re97 = mysqli_query($conn,$qry97);
while($row=mysqli_fetch_array($re97))
{
$product_id = $row['product_id'];
$componant = $row['item_code'];
$description = $row['description'];
$bar_size = $row['bar_size'];
$material_name = $row['material_name'];
$row_id=$row['row_id'];
$finish_shaft_wt_kg=$row['finish_shaft_wt_kg'];
$revised_labour_rate_rs=$row['revised_labour_rate_rs'];
$turning_scrap_wt=$row['turning_scrap_wt'];
$category_id=$row['category_id'];
$category_name=$row['category_name'];


}
   $qryauto="insert into delivery_challan_more_product_02 (product_id,componant,description,bar_size,row_id,material_name,customer_id,customer_name,entry_by,entry_date,delivery_challan_id,finish_shaft_wt_kg,revised_labour_rate_rs,turning_scrap_wt,category_name,category_id) values ('$product_id','$componant','$description','$bar_size','$row_id','$material_name','$customer_id','$customer_name','$user_name','$today','$delivery_challan_id','$finish_shaft_wt_kg','$revised_labour_rate_rs','$turning_scrap_wt','$category_name','$category_id')";
$re97 = mysqli_query($conn,$qryauto);




}

//exit();
?>


<script>
javascript:window.location.href="delivery_challan_edit.php?customer_id=<?php echo $_REQUEST['customer_id']; ?>&&customer_name=<?php echo $_REQUEST['customer_name']?>&&delivery_challan_id=<?php echo $delivery_challan_id?>&&company_name=<? echo $_REQUEST['company_name']?>";
</script>
<?php } ?>


