<?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'];











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_no='$delivery_challan_no',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")

{
 $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 desc";//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'];
		$cust_po_more_product_id  =$row['cust_po_more_product_id'];
$cust_more_row_id = $row['cust_more_row_id'];
$material_code = row['material_code'];
///////////4use_kg=;
$use_kg='0';
$use_kg01  =$row['use_kg'];
$use_kg_scrap  =$row['use_kg_scrap'];
$use_kg=$use_kg01+$use_kg_scrap;
$qty = $row['qty'];
if($cust_po_more_product_id!=''){
if($qty!='') {
$despatch_qty='';
$bal_po_qty='';
$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'];
$bal_po_qty01 = $row['bal_po_qty'];
$despatch_qty01=$row['despatch_qty'];

}
$despatch_qty=$despatch_qty01+$qty;
$bal_po_qty=$bal_po_qty01-$qty;
 $query1_PO = "update customer_po_more_product_02 set bal_po_qty='$bal_po_qty',despatch_qty='$despatch_qty'  where customer_id = '".$_REQUEST['customer_id']."' and cust_po_more_product_id = '$cust_po_more_product_id'" ;
//echo $query1;
$r1PO=mysqli_query($conn,$query1_PO);
}
}

if($cust_more_row_id!='') {
$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'];
}
if($use_kg!='') {
$despatch_weight='';
$bal_weight='';
$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))
{
$po_number = $row['po_number'];
$despatch_weight01 = $row['despatch_weight'];
$bal_weight01=$row['bal_weight'];
$rate=$row['rate'];

}
$despatch_weight=$despatch_weight01+$use_kg;
$bal_weight=$bal_weight01-$use_kg;
   $query1_PO = "update cust_inward_more_row_02 set bal_weight='$bal_weight',despatch_weight='$despatch_weight'  where  cust_more_row_id = '$cust_more_row_id' " ;
//echo $query1_PO;
$r1PO=mysqli_query($conn,$query1_PO);
}
}



}

$receipt_no=$_POST['receipt_no'];
$delivered_date=$_POST['delivered_date'];
$ack_remark=$_POST['ack_remark'];
$delivered_date=$_POST['delivered_date'];
	$filename1a=$_FILES["ack"]["name"];
if($filename1a!='')
{
$filenameP=$_FILES["ack"]["name"];
$tmp=$_FILES["ack"]["tmp_name"];
move_uploaded_file($tmp,"ack/".$filenameP=$_FILES["ack"]["name"]);
}else {
$filenameP = $_REQUEST['ack'];
}

		$query1 = "update delivery_challan_more_product_02 set delivered_date='$delivered_date'  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 ack_attchment='$filenameP',receipt_no='$receipt_no',ack_remark='$ack_remark', delivered_date='$delivered_date'  where customer_id = '".$_REQUEST['customer_id']."' and delivery_challan_id = '".$_REQUEST['delivery_challan_id']."' " ;
//echo $query1;
$r1=mysqli_query($conn,$query1);


 $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";
$result=mysqli_query($conn,$query2);
while($row=mysqli_fetch_array($result))

		{  
$product_id = $row['product_id'];
$componant = $row['componant'];
$description = $row['description'];

$delivery_challan_more_product_id  = $row['delivery_challan_more_product_id'];
$bmst_recipte_no = $_POST['bmst_recipte_no'.$delivery_challan_more_product_id];
 $query1 = "update delivery_challan_more_product_02 set receipt_no='$bmst_recipte_no' 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++; 
	
	}
	
	}
	//exit();
	
	//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>
