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





if($_POST['vendor_id']=='other_vendor_id')
{
$vendor_id01 = $_POST['other_vendor_id'];


$qrycalzl = "insert into vendor_master(vendor_name,unique_id,entry_by,entry_date,f_year,status,is_new) values ('$vendor_id01','$unique_id','$user_id','$today','$f_year','1','1')";
$rcalzl=mysqli_query($conn,$qrycalzl);

$qry3z = "select * from vendor_master where  unique_id='$unique_id'";
$re3z= mysqli_query($conn,$qry3z);
while($row=mysqli_fetch_array($re3z))
{  
$vendor_id = $row['vendor_id'];
$vendor_name = $row['vendor_name'];
}
$create_new = "YES";
}else{

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

$vendor_id = $_REQUEST['vendor_id'];

}


}




if($_REQUEST['auto_vendor_no']==''){ 		
							
$query1 = "select * from new_vendorpo_01 where auto_vendor_no!='' and f_year='$f_year' order by vendorpo_id  ";
$result1 = mysqli_query($conn,$query1);
$num_rows2a = mysqli_num_rows($result1);
if($num_rows2a =='0') { 
$auto_vendor_no1 = 0;
$vendorpo_no1=$auto_vendor_no1+1;
$vendor_no= sprintf("%04d", $vendorpo_no1);
} else {
while($row=mysqli_fetch_array($result1))
{
$f_year = $row['f_year'];
$vendorpo_no = $row['auto_vendor_no'];
$vendorpo_no1=$vendorpo_no+1;
$vendor_no= sprintf("%04d", $vendorpo_no1);
}
}		
} else {
$query1a = "select * from new_vendorpo_01 where auto_vendor_no='".$_REQUEST['auto_vendor_no']."' and f_year='$f_year'";
$result1a = mysqli_query($conn,$query1a);
while($row=mysqli_fetch_array($result1a))
{
$f_year = $row['f_year'];
$vendorpo_no = $row['auto_vendor_no'];
$vendor_no= sprintf("%04d", $vendorpo_no);
}}
  
if($_REQUEST['call_follow_up_id']==''){

$qryauto="insert into call_follow_up(customer_id,customer_name,unique_id,sstatus,follow_up_status,entry_date,entry_time,entry_by) values ('$vendor_id','$vendor_name','$unique_id','NEW','CreatNewVendorPO','$today','$time','$user_id')";
$rauto=mysqli_query($conn,$qryauto);
  
  
$qry9 = "select * from call_follow_up where unique_id='$unique_id'";
$re9 = mysqli_query($conn,$qry9);
while($row=mysqli_fetch_array($re9))
{  
$call_follow_up_id = $row['call_follow_up_id'];
}
}else {
$call_follow_up_id = $_REQUEST['call_follow_up_id'];	
}	
//echo $vendor_no;
//exit();	

//$vendor_id = $_POST['vendor_id'];
//$vendor_id01=implode(',',$_POST['vendor_id']);

//exit();
?>
<?php if($_REQUEST['proforma_id']!=''){?>
<script>
javascript:window.location.href="create_po_01.php?customer_id=<?php echo $_REQUEST['customer_id']; ?>&&vendor_no=<? echo $vendor_no ?>&&call_follow_up_id=<?php echo $call_follow_up_id?>&&create_new=<?php echo $create_new?>&&proforma_id=<?php echo $_REQUEST['proforma_id']?>&&vendor_id=<?php echo $vendor_id?>";
</script>

<?php }else{ ?>

<script>
javascript:window.location.href="create_vpo_01.php?customer_id=<?php echo $_REQUEST['customer_id']; ?>&&vendor_no=<? echo $vendor_no ?>&&call_follow_up_id=<?php echo $call_follow_up_id?>&&create_new=<?php echo $create_new?>&&proforma_id=<?php echo $_REQUEST['proforma_id']?>&&vendor_id=<?php echo $vendor_id?>";
</script>

<?php } ?>




