<?php $PageSecurity = 11;
include('includes/session.inc');
//include('includes/header.inc');
include('includes/SQL_CommonFunctions.inc');
include('includes/function_lib.php');
 if($_REQUEST['site']!='')
{
$sql="Select prefix from locations where loccode='".$_REQUEST['site']."'";
$result=DB_query($sql,$db);
$row=DB_fetch_array($result);
$prefix=$row['prefix'];

$enquiry_sql="Select enquiryNo from enquiry where site='".$_REQUEST['site']."' order by enquiryNo DESC limit 1";
$enquiry_result=DB_query($enquiry_sql,$db);

if(DB_num_rows($enquiry_result)>0)
{
	$enquiry_row=DB_fetch_array($enquiry_result);
	
	
	$en1=substr($enquiry_row['enquiryNo'],0,6);
	
	$en2=substr($enquiry_row['enquiryNo'],-4);
	
	$next=$en2+1;
	
	$finalString=$en1.str_pad($next,4,"0",STR_PAD_LEFT);
}
else
{
	$finalString=$prefix."/E-0001";
}
?>
<input name="enquiryNo" type="text" value="<?php echo $finalString;?>" style="width:107px;"/>
<?php 
}
if($_REQUEST['quotesite']=='crmquotes')
{
	$sql="Select prefix from locations where loccode='".$_REQUEST['q']."'";
	$result=DB_query($sql,$db);
	$row=DB_fetch_array($result);
	$prefix=$row['prefix']; 
	$quote_sql="Select quotationNo from quotation where site='".$_REQUEST['q']."' order by quotationNo DESC limit 1";
	$quote_result=DB_query($quote_sql,$db);	
	if(DB_num_rows($quote_result)>0)
	{
		$quote_row=DB_fetch_array($quote_result);
		$en1=substr($quote_row['quotationNo'],0,6);
		$en2=substr($quote_row['quotationNo'],-4);
		$next=$en2+1;
		$finalString=$en1.str_pad($next,4,"0",STR_PAD_LEFT);
	}
	else
	{
		$finalString=$prefix."/Q-00001";
	}
?>
<input name="quotationNo" type="text" value="<?php echo $finalString;?>"  style="width:107px;" />
<?php 
}
elseif($_REQUEST['salesite']!='')
{
	$sql="Select prefix from locations where loccode='".$_REQUEST['quotesite']."'";
	$result=DB_query($sql,$db);
	$row=DB_fetch_array($result);
	$prefix=$row['prefix']; 
	$quote_sql="Select id from salesorder where branch='".$_REQUEST['salesite']."' order by id DESC limit 1";
	$quote_result=DB_query($quote_sql,$db);	
	if(DB_num_rows($quote_result)>0)
	{
		$quote_row=DB_fetch_array($quote_result);
		$en1=substr($quote_row['id'],0,6);
		$en2=substr($quote_row['id'],-4);
		$next=$en2+1;
		$finalString=$en1.str_pad($next,4,"0",STR_PAD_LEFT);
	}
	else
	{
		$finalString=$prefix."/SO-0001";
	}
?>
<input name="orderNo" type="text" value="<?php echo $finalString;?>" style="width:107px;"/>
<?php 
}



else if($_REQUEST['type']=='dispatchSite')
{
 
  	$sql="Select prefix from locations where loccode='".$_REQUEST['q']."'";
	$result=DB_query($sql,$db);
	$row=DB_fetch_array($result);
	$prefix=$row['prefix']; 
	
	$dispatch_sql="Select adviceNo from dispatch where site='".$_REQUEST['q']."' order by adviceNo DESC limit 1";
	$dispatch_result=DB_query($dispatch_sql,$db);	
	
	if(DB_num_rows($dispatch_result)>0)
	{
		$disp_row=DB_fetch_array($dispatch_result);
	
	
		$en1=substr($disp_row['adviceNo'],0,6);
	
		$en2=substr($disp_row['adviceNo'],-4);
	
		$next=$en2+1;
	
		$finalString=$en1.str_pad($next,4,"0",STR_PAD_LEFT);
	}
	else
	{
		$finalString=$prefix."/D-0001";
	}
?>
<input name="adviceNo" id="adviceNo" type="text" value="<?php echo $finalString;?>" style="width:107px;" />
<?php 
}
?>