<?php ob_start();
session_start();
include 'connection.php';
if (!isset($_SESSION['is_logged_in'])) {
    header("Location:login.php");
    die();
    }
error_reporting(0);
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_name = $row['user_name'];
$user_id = $row['user_id'];
$our_company = $row['our_company'];
}

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



$customer_id = $_REQUEST['customer_id'];
$vendor_id = $_REQUEST['vendor_id'];
$proforma_id = $_REQUEST['proforma_id'];

$customer_name=  mysqli_real_escape_string($conn,$_REQUEST['customer_name']);
$vendorpo_no=$_REQUEST['vendorpo_no'];
$currency = $_POST['currency'];
$vendorpo_date=$_POST['vendorpo_date'];
$contact_person=$_POST['contact_person'];
$contact_no=$_POST['contact_no'];
$contact_email = $_POST['contact_email'];
$auto_vendor_no = $_REQUEST['vendor_no'];
$GST_type = $_POST['GST_type'];
$address = $_REQUEST['address'];
$vendor_name = $_POST['vendor_name'];

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




if($_POST['contact_id']=='other_contact_id')
{
$contact_name = $_POST['contact_name'];
$mobile_no = $_POST['mobile_no'];
$email_id = $_POST['email_id'];

$qryautoc2="insert into vendor_contact(contact_name,mobile_no,email_id,vendor_id,unique_id) values ('$contact_name','$mobile_no','$email_id','$vendor_id','$unique_id')";
$rautoc2=mysqli_query($conn,$qryautoc2);

$querycon = "select * from  vendor_contact where unique_id = '$unique_id'";
$resultcon = mysqli_query($conn,$querycon);
while($row=mysqli_fetch_array($resultcon))
{
$contact_id = $row['contact_id'];
}
}else{

$contact_id = $_POST['contact_id'];
}


$querycon = "select * from  vendor_contact where contact_id = '$contact_id'";
$resultcon = mysqli_query($conn,$querycon);
while($row=mysqli_fetch_array($resultcon))
{
$contact_person = $row['contact_name'];
$contact_no = $row['mobile_no'];
$contact_email = $row['email_id'];

}



$qrycust = "select * from mst_customer where customer_id = '".$_REQUEST['customer_id']."'";
$resultcust = mysqli_query($conn,$qrycust);
while($row=mysqli_fetch_array($resultcust))
{  
$customer_name = $row['customer_name'];
}

$qrycustpro = "select * from proforma_make_table_1 where proforma_id = '".$_REQUEST['proforma_id']."'";
$resultcustpro = mysqli_query($conn,$qrycustpro);
while($row=mysqli_fetch_array($resultcustpro))
{  
$final_proforma_no = $row['final_proforma_no'];
$po_number = $row['po_number'];
$po_date = $row['po_date'];
}


$create_new = $_REQUEST['create_new'];


if($_REQUEST['vendorpo_id']==''){ 	
$queryeng="insert into new_vendorpo_01(vendorpo_no,vendorpo_date,vendor_id,vendor_name,address,f_year,entry_by,unique_id,entry_date,call_follow_up_id,status,po_number,po_date,final_proforma_no,proforma_id,customer_name,customer_id,currency,GST_type,auto_vendor_no) values ('$vendorpo_no','$vendorpo_date','$vendor_id','$vendor_name','$address','$f_year','$user_id','$unique_id','$today','$call_follow_up_id','$status','$po_number','$po_date','$final_proforma_no','$proforma_id','$customer_name','$customer_id','$currency','$GST_type','$auto_vendor_no')";
$resulrenq=mysqli_query($conn,$queryeng);
  
  
$qry9enq = "select * from new_vendorpo_01 where unique_id='$unique_id'";
$re9enq = mysqli_query($conn,$qry9enq);
while($row=mysqli_fetch_array($re9enq))
{  
$vendorpo_id = $row['vendorpo_id'];
}
}else {
$vendorpo_id = $_REQUEST['vendorpo_id'];	

$query16cha = "update new_vendorpo_01 set vendorpo_date='$vendorpo_date',currency='$currency',f_year='$f_year',contact_person='$contact_person',contact_no='$contact_no',contact_email='$contact_email',GST_type='$GST_type',address='$address' where vendorpo_id ='$vendorpo_id'";
$res16cha=mysqli_query($conn,$query16cha);
}	





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





$query2="select * from vendorpo_more_product_02 where vendorpo_id = '$vendorpo_id' AND vendor_id = '".$_REQUEST['vendor_id']."' AND deleted !='YES'  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_type=$row['product_type'];
$product_price =  $_POST['product_price'.$more_vproduct_id];
$qty = $_POST['qty'.$more_vproduct_id];
$unit = $_POST['unit'.$more_vproduct_id];
$discount_value = $_POST['discount_value'.$more_vproduct_id];
$product_name = mysqli_real_escape_string($conn,$_POST['product_name'.$more_vproduct_id]);
$itemdesc =  mysqli_real_escape_string($conn,$_POST['itemdesc'.$more_vproduct_id]);
$HSN_code = $_POST['HSN_code'.$more_vproduct_id];

$filename1=$_FILES["pro_photo".$more_vproduct_id]["name"];
if($filename1!='')
{
$filename=$_FILES["pro_photo".$more_vproduct_id]["name"];
$tmp=$_FILES["pro_photo".$more_vproduct_id]["tmp_name"];
move_uploaded_file($tmp,"product_photo/".$filename=$_FILES["pro_photo".$more_vproduct_id]["name"]);
}else {
$filename = $_REQUEST['pro_photo'.$more_vproduct_id];
}



if($discount_value==''){ 
$po_price = $product_price;
}else{
if(is_numeric($qty)) {
$po_price = $_POST['po_price'.$more_vproduct_id]/$qty;
}
}

$queryqm = "update vendorpo_more_product_02 set pro_status='Save',vendorpo_no='$vendorpo_no',product_name='$product_name',po_price='$po_price',qty='$qty',product_price='$product_price',unit = '$unit',discount_value='$discount_value',HSN_code='$HSN_code',GST_invtype='GST',CGST_rate='$CGST',SGST_rate='$SGST',IGST_rate='$IGST',vendor_name='$vendor_name',itemdesc='$itemdesc',vendorpo_date='$vendorpo_date',vendor_id='$vendor_id',pro_photo='$filename',po_number='$po_number',po_date='$po_date',final_proforma_no='$final_proforma_no',proforma_id='$proforma_id',customer_name='$customer_name',customer_id='$customer_id',f_year='$f_year',entry_by='$user_id',entry_date='$today',call_follow_up_id='$call_follow_up_id' where vendor_id = '".$_REQUEST['vendor_id']."' and vendorpo_id = '$vendorpo_id' and more_vproduct_id='$more_vproduct_id'" ;
$resultqm=mysqli_query($conn,$queryqm);

$i++;



$search_product_name = preg_replace('/[^A-Za-z0-9-()]/', '', $product_name);



$qrycon = "select * from vendor_product_master where search_vproduct_name='$search_product_name' and vendor_id = '".$_REQUEST['vendor_id']."'";
$resultcon1 = mysqli_query($conn,$qrycon);
while($row=mysqli_fetch_array($resultcon1))
{  
$vproduct_id = $row['vproduct_id'];

$queryvpp1 = "update vendorpo_more_product_02 set product_id='$vproduct_id' where vendor_id = '".$_REQUEST['vendor_id']."' and vendorpo_id = '$vendorpo_id' and more_vproduct_id='$more_vproduct_id'";
$resultvpp1=mysqli_query($conn,$queryvpp1);

}

$qrycon2 = "select * from vendor_products where search_product_name='$search_product_name'";
$resultcon2 = mysqli_query($conn,$qrycon2);
while($row=mysqli_fetch_array($resultcon2))
{  
$product_idb = $row['product_id'];

$queryvpp1a = "update vendor_product_master set product_id='$product_idb' where vendor_id = '".$_REQUEST['vendor_id']."' and search_vproduct_name='$search_product_name'";
$resultvpp1a=mysqli_query($conn,$queryvpp1a);

}



//$queryvp = "update cpo_master set vendorpo_date='$vendorpo_date',vendorpo_no='$vendorpo_no_new',vendorpo_id='$vendorpo_id',vendor_id='$vendor_id' where proforma_id = '".$_REQUEST['proforma_id']."'";
//$resultvp=mysqli_query($conn,$queryvp);

}


//exit();

?>
<script>

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

</script>
<?php

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



$customer_name= mysqli_real_escape_string($conn,$_POST['customer_name']);
$currency = $_POST['currency'];

$customer_id=$_REQUEST['customer_id'];
$call_follow_up_id=$_REQUEST['call_follow_up_id'];

$query2="select * from vendorpo_more_product_02 where vendorpo_id = '".$_REQUEST['vendorpo_id']."' AND vendor_id = '".$_REQUEST['vendor_id']."' AND deleted !='YES'  ORDER BY more_vproduct_id desc";
//echo $query2;
$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_price01 =  $_POST['product_price'.$more_vproduct_id];
$unit = $_POST['unit'.$more_vproduct_id];
$discount_value = $_POST['discount_value'.$more_vproduct_id];
$qty01 = $_POST['qty'.$more_vproduct_id];
$itemdesc =  mysqli_real_escape_string($conn,$_POST['itemdesc'.$more_vproduct_id]);
$product_name= $_REQUEST['product_name'.$more_vproduct_id];
$pro_status = $_POST['pro_status'.$more_vproduct_id];
$pro_photo = $_REQUEST['pro_photo'.$more_vproduct_id];

if($discount_value==''){ 
$po_price01 = $product_price01;
}else{
if(is_numeric($qty)) {
$po_price01 = $_POST['po_price'.$more_vproduct_id]/$qty;
}
}

$query1 ="update vendorpo_more_product_02 set product_name='$product_name',po_price='$po_price01',qty='$qty01',product_price='$product_price01',unit='$unit',discount_value='$discount_value',HSN_code='$HSN_code',GST_invtype='GST',CGST_rate='$CGST',SGST_rate='$SGST',IGST_rate='$IGST',itemdesc='$itemdesc',pro_status='temp',pro_photo='$pro_photo' where vendor_id = '".$_REQUEST['vendor_id']."' and vendorpo_id = '$vendorpo_id' and more_vproduct_id='$more_vproduct_id'" ;
//echo $query1;
$r1=mysqli_query($conn,$query1);

$i++; }
$price =  '';
 
$qty = '';
$uom = '';
$due_date = '';
$discount = '';
$lead_time = '';
$product_desc = '';
$more_product_id01=$_POST['state'];
$country=$_POST['country'];


if($more_product_id01!=''){
 $multiple_modelno=implode(',',$more_product_id01);
$arr_m = explode(",", $multiple_modelno);
foreach ($arr_m as $more_product_id) {


if (is_numeric($more_product_id)) 
 {
$query2 = "select * from proforma_more_product_02 where more_product_id='$more_product_id'";
$result2 = mysqli_query($conn,$query2);
while($row=mysqli_fetch_array($result2))
{
$product_name = $row['product_name'];
$pro_photo = $row['pro_photo'];
$unit = $row['unit'];
$product_id = $row['product_id'];
$qty_pro = $row['qty'];
$product_price = $row['product_price'];
$HSN_code = $row['HSN_code'];
$GST_invtype = $row['GST_invtype'];

$query22 = "select * from vendorpo_more_product_02 where more_product_id ='$more_product_id'";
$result22 = mysqli_query($conn,$query22);
while($row=mysqli_fetch_array($result22))
{
$qtyv+= $row['qty'];
}

$qty = $qty_pro-$qtyv;


if($more_product_id01!=''){
$query2 ="insert into vendorpo_more_product_02 (vendorpo_id,product_id,customer_id,product_name,unit,qty,product_price,po_price,pro_status,vendorpo_no,customer_name,pro_photo,vendor_name,vendor_id,auto_vendor_no,more_product_id,HSN_code,GST_invtype) values ('$vendorpo_id','$product_id','$customer_id','$product_name','$unit','$qty','$product_price','$product_price','temp','$vendorpo_no','$customer_name','$pro_photo','$vendor_name','$vendor_id','$auto_vendor_no','$more_product_id','$HSN_code','$GST_invtype')";
$r2=mysqli_query($conn,$query2);




$search_vproduct_name = preg_replace('/[^A-Za-z0-9-()]/', '', $product_name);

$query07 = "select * from vendor_product_master where search_vproduct_name='$search_vproduct_name' AND vendor_id = '".$_REQUEST['vendor_id']."'";
$result07 = mysqli_query($conn,$query07);
$num_rows07 = mysqli_num_rows($result07);
if($num_rows07=='0')
{
$query27 ="insert into vendor_product_master (vendor_id,product_name,product_details,unit,price,pro_photo,search_vproduct_name,status) values ('$vendor_id','$product_name','$itemdesc','$unit','$price','$pro_photo','$search_vproduct_name','1')";
$result27=mysqli_query($conn,$query27);
}


}
}
} else {

$filename1=$_FILES["pro_photo"]["name"];
if($filename1!='')
{
$filename=$_FILES["pro_photo"]["name"];
$tmp=$_FILES["pro_photo"]["tmp_name"];
move_uploaded_file($tmp,"product_photo/".$filename=$_FILES["pro_photo"]["name"]);
}


$query2 ="insert into vendorpo_more_product_02 (vendorpo_id,product_id,customer_id,product_name,unit,qty,product_price,po_price,pro_status,vendorpo_no,customer_name,pro_photo,vendor_name,vendor_id,auto_vendor_no,HSN_code,GST_invtype) values ('$vendorpo_id','','$customer_id','','','','','','temp','$vendorpo_no','','','$vendor_name','$vendor_id','$auto_vendor_no','$HSN_code','$GST_invtype')";
$r2=mysqli_query($conn,$query2);

} 

}
}





//exit();

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