<?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

$challan_no=$_POST['challan_no'];
$challan_date=$_POST['challan_date'];

$query2="select * from cust_inward_more_row_02 where customer_inward_id = '".$_REQUEST['customer_inward_id']."' AND customer_id = '".$_REQUEST['customer_id']."' AND deleted !='YES'  ORDER BY cust_more_row_id desc";//echo $query2;
$result=mysqli_query($conn,$query2);
$num_rows = mysqli_num_rows($result);
while($row=mysqli_fetch_array($result))
		{  
$cust_more_row_id = $row['cust_more_row_id'];
$material_name =  $_POST['material_name'.$cust_more_row_id];
 $description = mysqli_real_escape_string($conn,$_POST['description'.$cust_more_row_id]);

$bar_size = $_POST['bar_size'.$cust_more_row_id];
$weight = $_POST['weight'.$cust_more_row_id];
$qty = $_POST['qty'.$cust_more_row_id];
$rate = $_POST['rate'.$cust_more_row_id];
$heat_no=$_POST['heat_no'.$cust_more_row_id];







 $query1 = "update cust_inward_more_row_02 set heat_no='$heat_no',challan_no='$challan_no',challan_date='$challan_date',material_name='$material_name',bar_size='$bar_size',description='$description',weight='$weight',bal_weight='$weight', qty = '$qty', rate= '$rate' where customer_id = '".$_REQUEST['customer_id']."' and customer_inward_id = '".$_REQUEST['customer_inward_id']."' and cust_more_row_id='$cust_more_row_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";





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

{

 $queryls = "select * from GRN_series where auto_no!='' and year='$f_year'  ORDER BY auto_id";
//echo $queryls;
$resultls = mysqli_query($conn,$queryls);    
 while($row=mysqli_fetch_array($resultls))
                                                    {
                                                    $auto_no = $row['auto_no'];
													 $company_name = $row['company_name'];
                                                    }
    
                                                     
$auto_no_new=$auto_no+1;
$cont_seriesa= sprintf("%04d", $auto_no_new);
$GRN_NO="$cont_seriesa"."-"."$f_year";


	
		$query1 = "update cust_inward_more_row_02 set status='SAVE',GRN_NO='$GRN_NO'  where customer_id = '".$_REQUEST['customer_id']."' and customer_inward_id = '".$_REQUEST['customer_inward_id']."' " ;
//echo $query1;
$r1=mysqli_query($conn,$query1);
	
	
	$query1 = "update cust_raw_inward  set status='SAVE',GRN_NO='$GRN_NO'  where customer_id = '".$_REQUEST['customer_id']."' and customer_inward_id = '".$_REQUEST['customer_inward_id']."' " ;
//echo $query1;
$r1=mysqli_query($conn,$query1);

$query16cha = "update GRN_series set auto_no='$auto_no_new',GRN_NO='$GRN_NO',year='$f_year' ";
$res16cha=mysqli_query($conn,$query16cha);



			
?>

<script>
javascript:window.location.href="view_all_inwards_customer.php";
</script>
<?php



}/////submit slosed

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


$challan_no=$_POST['challan_no'];
$challan_date=$_POST['challan_date'];
$vehicle_lr_no=$_POST['vehicle_lr_no'];
$version_date=$_POST['version_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['customer_inward_id']==''){ 									


  $qryauto="insert into cust_raw_inward (challan_no,challan_date,vehicle_lr_no,customer_id,customer_name,customer_address,customer_email_id,customer_GST_no,vendor_code,entry_date,entry_by,unique_id) values ('$challan_no','$challan_date','$vehicle_lr_no','$customer_id','$customer_name','$customer_address','$customer_email_id','$customer_GST_no','$vendor_code','$today','$user_name','$unique_id')";
$rauto=mysqli_query($conn,$qryauto);
//echo $rauto;
//exit();
$qry3z = "select * from cust_raw_inward  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))
		{  
		$customer_inward_id = $row['customer_inward_id'];
		}
}else {

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

//exit();

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

$qry97 = "select * from rowmaterial_master where row_id='$row_id'";
$re97 = mysqli_query($conn,$qry97);
while($row=mysqli_fetch_array($re97))
{
$row_id = $row['row_id'];
$bar_size = $row['bar_size'];
$material_name = $row['material_name'];

}  $qryauto="insert into cust_inward_more_row_02 (row_id,bar_size,material_name,customer_id,customer_name,entry_by,entry_date,customer_inward_id) values ('$row_id','$bar_size','$material_name','$customer_id','$customer_name','$user_name','$today','$customer_inward_id')";
$re97 = mysqli_query($conn,$qryauto);



}


//exit();
?>


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


