<?php ob_start();
session_start();
include 'connection.php';

if (!isset($_SESSION['is_logged_in'])) {
    header("Location:login.php");
    die();
    }

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



$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_id = $row['user_id'];
}

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();

$MRdate = $_POST['MRdate'];
$MRnumber = $_POST['MRnumber'];
$vendorpo_no = $_REQUEST['vendorpo_no'];
$vendor_id = $_REQUEST['vendor_id'];
$vendorpo_id = $_REQUEST['vendorpo_id'];


$qry10 = "select * from vendor_master where vendor_id = '".$_REQUEST['vendor_id']."'";
$re10 = mysqli_query($conn,$qry10);
while($row=mysqli_fetch_array($re10))
{  
$vendor_name = $row['vendor_name'];
}




$query2="select * from vendorpo_more_product_02 where vendorpo_id = '$vendorpo_id' AND vendor_id = '".$_REQUEST['vendor_id']."' AND deleted !='YES'  and material_received_status='' ORDER BY more_vproduct_id desc";
$result=mysqli_query($conn,$query2);
$num_rows = mysqli_num_rows($result);
while($row=mysqli_fetch_array($result))
{  
$more_vproduct_id = $row['more_vproduct_id'];
$product_id = $row['product_id'];
$product_name = $row['product_name'];
$order_qty = $row['qty'];
$recieved_qty =  $_POST['recieved_qty'.$more_vproduct_id];
$qty = $_POST['qty'.$more_vproduct_id];
$balance_qty = $_POST['balance_qty'.$more_vproduct_id];


if($balance_qty<='0'){
$Rstatus = "DONE";
}else{
$Rstatus = "";
}

$query2a ="insert into matrial_recieved_activty (more_vproduct_id,product_name,recieved_qty,MRnumber,MRdate,vendorpo_no,vendor_id,vendorpo_id,f_year,entry_by,entry_date,product_id,vendor_name,order_qty) values ('$more_vproduct_id','$product_name','$recieved_qty','$MRnumber','$MRdate','$vendorpo_no','$vendor_id','$vendorpo_id','$f_year','$user_id','$today','$product_id','$vendor_name','$order_qty')";
$r2a=mysqli_query($conn,$query2a);


//echo "<br>";

$qry3z = "select * from vendorpo_more_product_02 where vendorpo_id = '$vendorpo_id' AND vendor_id = '".$_REQUEST['vendor_id']."' and more_vproduct_id='$more_vproduct_id'";
$re3z= mysqli_query($conn,$qry3z);
while($row=mysqli_fetch_array($re3z))
{  
$recieved_qty_prev = $row['recieved_qty'];
}

if($recieved_qty_prev!='')
{
$total_recieved_qty = $recieved_qty_prev+$recieved_qty;
}else{
$total_recieved_qty = $recieved_qty;
}

$queryvp = "update vendorpo_more_product_02 set recieved_qty='$total_recieved_qty',balance_qty='$balance_qty',material_received_status='$Rstatus' where more_vproduct_id = '$more_vproduct_id'";
$resultvp=mysqli_query($conn,$queryvp);



$qry5z = "select * from vendor_product_master where vendor_id = '".$_REQUEST['vendor_id']."' and vproduct_id='$product_id'";
$re5z= mysqli_query($conn,$qry5z);
while($row=mysqli_fetch_array($re5z))
{  
$stock_or = $row['stock'];
$product_id_or = $row['product_id'];
}


$qry6z = "select * from vendor_products where product_id='$product_id_or'";
$re6z= mysqli_query($conn,$qry6z);
while($row=mysqli_fetch_array($re6z))
{  
$stock_or1 = $row['stock'];
}


if($stock_or1!='')
{
$total_stock_qty1 = $stock_or1+$recieved_qty;
}else{
$total_stock_qty1 = $recieved_qty;
}


//echo "<br>";

if($stock_or!='')
{
$total_stock_qty = $stock_or+$recieved_qty;
}else{
$total_stock_qty = $recieved_qty;
}


$queryvp09 = "update vendor_product_master set stock='$total_stock_qty' where vendor_id = '".$_REQUEST['vendor_id']."' and vproduct_id='$product_id'";
$resultvp09=mysqli_query($conn,$queryvp09);




$queryvp10 = "update vendor_products set stock='$total_stock_qty1' where product_id='$product_id_or'";
$resultvp10=mysqli_query($conn,$queryvp10);

}

$totalbalnce = $totalOqt-$totalRqty;


//echo $vendor_no;
//exit();	
?>

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


