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

$unique_id = randomPassword();	

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";



$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
$po_number=$_POST['po_number'];
$po_date=$_POST['po_date'];
$version_no=$_POST['version_no'];
$version_date=$_POST['version_date'];


$filename1a=$_FILES["PO_copy"]["name"];
if($filename1a!='')
{
$filenameP=$_FILES["PO_copy"]["name"];
$tmp=$_FILES["PO_copy"]["tmp_name"];
move_uploaded_file($tmp,"cpo_copy/".$filenameP=$_FILES["PO_copy"]["name"]);
}else {
$filenameP = $_REQUEST['PO_copy'];
}
$i='1';

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

$qty = $_POST['qty'.$cust_po_more_product_id];
$rate = $_POST['rate'.$cust_po_more_product_id];
$start_date = $_POST['start_date'.$cust_po_more_product_id];
$end_date = $_POST['end_date'.$cust_po_more_product_id];
$HSN_code = $_POST['HSN_code'.$cust_po_more_product_id];

$sr_no = $_POST['sr_no'.$cust_po_more_product_id];

$query2hsn="SELECT * FROM HSN_code_master WHERE HSN_code = '$HSN_code'";
//echo $query2hsn;
$result1hsn = mysqli_query($conn,$query2hsn);
while($query3hsn=mysqli_fetch_array($result1hsn))
{
$CGST = $query3hsn['CGST'];
$SGST = $query3hsn['SGST'];
$IGST = $query3hsn['IGST'];
}


if($_REQUEST['customer_po_id']!=''){



$query1 = "update customer_po_more_product_02 set sr_no='$sr_no',po_number='$po_number',po_date='$po_date',item_code='$item_code',description='$description',rate='$rate', qty = '$qty',bal_po_qty='$qty', start_date= '$start_date',end_date = '$end_date',HSN_code ='$HSN_code',CGST_rate='$CGST',SGST_rate='$SGST',IGST_rate='$IGST'  where customer_id = '".$_REQUEST['customer_id']."' and customer_po_id = '".$_REQUEST['customer_po_id']."' and cust_po_more_product_id='$cust_po_more_product_id' and customer_po_id!='' and customer_po_id!='Direct'" ;
//echo $query1;
$r1=mysqli_query($conn,$query1);
}
$i++; 
}




$query1 = "update customer_PO set po_number='$po_number',po_date='$po_date',version_no='$version_no',version_date='$version_date',cpo_attachment='$filenameP'  where customer_id = '".$_REQUEST['customer_id']."' and customer_po_id = '".$_REQUEST['customer_po_id']."' " ;
$r1=mysqli_query($conn,$query1);



	
if (isset($_POST['Submit'])=="Save")
{
$query1 = "update customer_po_more_product_02 set status='SAVE'  where customer_id = '".$_REQUEST['customer_id']."' and customer_po_id = '".$_REQUEST['customer_po_id']."' " ;
$r1=mysqli_query($conn,$query1);
	
	
$query1 = "update customer_PO set status='SAVE' where customer_id = '".$_REQUEST['customer_id']."' and customer_po_id = '".$_REQUEST['customer_po_id']."' " ;
$r1=mysqli_query($conn,$query1);
			
?>

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



}/////submit slosed

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


$po_number=$_POST['po_number'];
$po_date=$_POST['po_date'];
$version_no=$_POST['version_no'];
$version_date=$_POST['version_date'];

$queryls = "select * from mst_customer where customer_id = '".$_REQUEST['customer_id']."'";
$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'];
}

$filename1a=$_FILES["PO_copy"]["name"];
if($filename1a!='')
{
$filenameP=$_FILES["PO_copy"]["name"];
$tmp=$_FILES["PO_copy"]["tmp_name"];
move_uploaded_file($tmp,"cpo_copy/".$filenameP=$_FILES["PO_copy"]["name"]);
}else {
$filenameP = $_REQUEST['PO_copy'];
}


$customer_po_id=$_REQUEST['customer_po_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'];
$item_code = $row['item_code'];
$description = $row['description'];
$bar_size = $row['bar_size'];
$material_name = $row['material_name'];
$row_id=$row['row_id'];
$HSN_code=$row['HSN_code'];
$rate=$row['revised_labour_rate_rs'];

}





$qryauto="insert into customer_po_more_product_02 (product_id,item_code,description,bar_size,row_id,material_name,customer_id,customer_name,entry_by,entry_date,customer_po_id,HSN_code,rate) values ('$product_id','$item_code','$description','$bar_size','$row_id','$material_name','$customer_id','$customer_name','$user_name','$today','$customer_po_id','$HSN_code','$rate')";
$re97 = mysqli_query($conn,$qryauto);



}

//exit();
?>


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


