<?php  include('Class_Party.php');
 
if($_REQUEST['Save']!='' && $_REQUEST['action']!='Edit')
{
	
	$fstatus=true;
	if($_REQUEST['site']=='0')
	{
		$msgErr[]="Please Select The Branch";
		$fstatus=false;
	}
	if($_REQUEST['quotationNo']=='')
	{
		$msgErr[]="Please Enter The Quotation No";
		$fstatus=false;
	}
	if($_REQUEST['quotationDate']=='')
	{
		$msgErr[]="Please Enter The Quotation Date";
		$fstatus=false;
	}
	if($_REQUEST['party']=='0')
	{
		$msgErr[]="Please Select The Party ";
		$fstatus=false;
	}
	if($_REQUEST['quotationType']=='0')
	{
		$msgErr[]="Please Select The Quotation Type ";
		$fstatus=false;
	}
	/*if($_REQUEST['enquiryNo']=='')
	{
		$msgErr[]="Please Enter The Enquiry No ";
		$fstatus=false;
	}*/
	if($_REQUEST['validTill']=='')
	{
		$msgErr[]="Please Enter The Valid Date ";
		$fstatus=false;
	}
 
	if($_REQUEST['basicAmount']=='')
	{
		$msgErr[]="Please Enter The Basic Amount   ";
		$fstatus=false;
	}
	if($_REQUEST['totalDiscountedPrice']=='')
	{
		$msgErr[]="Please Enter The Total Discount Price   ";
		$fstatus=false;
	}
	if($_REQUEST['taxableAmount']=='')
	{
		$msgErr[]="Please Enter The Taxable Amount ";
		$fstatus=false;
	}
	if($_REQUEST['grandTotal']=='')
	{
		$msgErr[]="Please Enter The Grand Total ";
		$fstatus=false;
	}
	
	
	
}

if($_REQUEST['Save']!='' && $_REQUEST['action']!='Edit' && $fstatus)
{
	 $insert1="Insert into quotation set 
					site='".$_REQUEST['site']."',    
					quotationNo='".$_REQUEST['quotationNo']."',  
					quotationDate='".FormatDateForSQL($_REQUEST['quotationDate'])."',  
					party='".$_REQUEST['party']."',   
					quotationType='".$_REQUEST['quotationType']."',
					quotation_subject='".$_REQUEST['quotation_subject']."',
					quotation_funnel  ='".$_REQUEST['quotation_funnel']."',
					quotation_stage  ='".$_REQUEST['quotation_stage']."',
					enquiryNo='".$_REQUEST['leadid']."',  
					source='".$_REQUEST['source1']."',  
					partyContact='".$_REQUEST['partyContact2']."',  
					validTill='".ConvertSQLDate($_REQUEST['validTill'])."',  
					pack_forward='".$_REQUEST['pack_forward']."',  
					freight= '".$_REQUEST['freight']."', 
					payTerms='".$_REQUEST['payTerms']."',  
					salesTax='".$_REQUEST['salesTax']."',  
					Excise='".$_REQUEST['Excise']."',  
					insurance='".$_REQUEST['insurance']."',  
					basicAmount='".$_REQUEST['basicAmount']."',  
					totalDiscountedPrice='".$_REQUEST['totalDiscountedPrice']."',  
					exciseDuty='".$_REQUEST['exciseDuty']."',  
					exciseValue='".$_REQUEST['exciseValue']."',  
					ECessValue='".$_REQUEST['ECessValue']."',  
					SAHCessValue='".$_REQUEST['SAHCessValue']."',  
					taxableAmount='".$_REQUEST['taxableAmount']."',
					VatCst='".$_REQUEST['VatCst']."',  
					VatCstValue='".$_REQUEST['VatCstValue']."',  
					freightValue='".$_REQUEST['freightValue']."',  
					packagingCharges='".$_REQUEST['packagingCharges']."',  
					additionalCharges='".$_REQUEST['additionalCharges']."',  
					roundOff='".$_REQUEST['roundOff']."',  
					grandTotal='".$_REQUEST['grandTotal']."',  
					createdBy='".$_SESSION['UserID']."',  
					createdDate='".date('Y-m-d')."',
					CompanyId='".$_SESSION['ACGCompanyId']."'  ";
					
					
					
	$result1=DB_query($insert1,$db);
	
	  $masterId=DB_Last_Insert_ID($db,'quotation','id');
	 
	if($masterId!='')
	{
		
		for($cnt=0;$cnt<$_REQUEST['item_code'];$cnt++)
		{	
		  if($_REQUEST['item_code'][$cnt]!=''){
			  $insert2="Insert into quotation_details set
						parentId='".$masterId."',  
						itemCode='".$_REQUEST['item_code'][$cnt]."',  
						quantity='".$_REQUEST['stock_qty'][$cnt]."',   
						price='".$_REQUEST['stock_price'][$cnt]."',
						PerUnits ='".$_REQUEST['stock_perunit'][$cnt]."', 
						totalValue='".$_REQUEST['stock_value'][$cnt]."',  
						discountType='".$_REQUEST['discountType'][$cnt]."',  
						discount='".$_REQUEST['discount'][$cnt]."',  
						valueAfterDisc='".$_REQUEST['valueAfterDisc'][$cnt]."',  
						requisitionDate='".date('Y-m-d')."'  ";
			$result2=DB_query($insert2,$db);
			}
		}
	}
	prnMsg(_('Inserted Successfully'),'Success');
echo "<script>location.href='index.php?route=CRM/ViewQuotes&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."&action=view&Id=".$masterId."'</script>"; 
}



if($_REQUEST['Save']!='' && $_REQUEST['action']=='Edit')
{

  	
			
 			$insert2="update  quotation set 
					quotationDate='".FormatDateForSQL($_REQUEST['quotationDate'])."',
					partyContact='".$_REQUEST['partyContact2']."',
				    quotationType='".$_REQUEST['quotationType']."',  
					quotation_stage='".$_REQUEST['quotation_stage']."',  
					validTill='".FormatDateForSQL($_REQUEST['validTill'])."' where id='".$_REQUEST['QID']."' ";
				
				
				$query1=DB_query($insert2,$db);
		
	 	unset($_SESSION['qutotation_itemcode']);
 unset($_SESSION['itemcode']);	
	$s->pageLocation("index.php?route=CRM/ViewQuotes".$rurl);	

	
 }
?>
<script language="JavaScript" type="text/javascript">
	function enquiryDetails(str)
	{
		var branch=document.getElementById("site").value;
		
		if (str.length==0)
  		{
  			document.getElementById("enquiryId").innerHTML="";
  			return;
  		}
		if (window.XMLHttpRequest)
  		{// code for IE7+, Firefox, Chrome, Opera, Safari
  			xmlhttp=new XMLHttpRequest();
  		}
		else
  		{// code for IE6, IE5
  			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  		}
		xmlhttp.onreadystatechange=function()
  		{
  			if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		{
    			document.getElementById("enquiryId").innerHTML=xmlhttp.responseText;
    		}
  		}
		xmlhttp.open("GET","showEnquiryDetails.php?party="+str+"&branch="+branch,true);
		xmlhttp.send();
	}
	function showEnqDetails(str)
	{
		if (str.length==0)
  		{
  			document.getElementById("enqdetailsId").innerHTML="";
  			return;
  		}
		if (window.XMLHttpRequest)
  		{// code for IE7+, Firefox, Chrome, Opera, Safari
  			xmlhttp=new XMLHttpRequest();
  		}
		else
  		{// code for IE6, IE5
  			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  		}
		xmlhttp.onreadystatechange=function()
  		{
  			if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		{
    			document.getElementById("enqdetailsId").innerHTML=xmlhttp.responseText;
    		}
  		}
		xmlhttp.open("GET","showEnquiryDetails.php?enquiryNo="+str,true);
		xmlhttp.send();
	}
function calculateDiscount(str,a)
{
	var discType=document.getElementById("discountType_"+a).value;
	var value=document.getElementById("totalValue_"+a).value;
	if(str==''){str=0;}
	if(discType=='A') 
	{
		var discount=str;
	}
	else
	if(discType=='P') 
	{
		var discount=value*(str/100);	
	}
	document.getElementById("valueAfterDisc_"+a).value=(parseFloat(value)-parseFloat(discount)).toFixed(2);
}
function calculateBasic()
{
	var totalBasic=0;	
	
	var cnt=document.getElementById("hiditemcount").value;
		
	if(cnt=='undefined')
	{		
	 var totalBasic1=document.getElementById("stock_value").value;
	 var totalBasic=parseFloat(totalBasic)+parseFloat(totalBasic1);
	 
	}	
	
	else
	{
	for(var i=0;i<cnt;i++)
	{
	   var totalBasic1=document.getElementById("stock_value_"+i).value;
	 
	   var totalBasic=parseFloat(totalBasic)+parseFloat(totalBasic1);
	  }
	 
	}
 	  document.getElementById("basicAmount").value=totalBasic;
	 
}

function calculateDiscounted()
{
	var totalDiscounted=0;
	
	
	
	var cnt=document.getElementById("hiditemcount").value;
			
	if(cnt=='undefined')
	{		
	 var totalDiscounted1=document.getElementById("valueAfterDisc").value;
 	    
 	  var totalDiscounted=parseFloat(totalDiscounted)+parseFloat(totalDiscounted1);
	 
	}
	
	else
	{
	
      for(var i=0;i<cnt;i++)
      {
 	    var totalDiscounted1=document.getElementById("valueAfterDisc_"+i).value;
 	    
 	    var totalDiscounted=parseFloat(totalDiscounted)+parseFloat(totalDiscounted1);
 
 	 }
	
}	
	document.getElementById("totalDiscountedPrice").value=totalDiscounted;
}
function checkApplicable(str)
{
/*	for(var i=1; i<6;i++)
	{
		if(document.getElementById("menu"+i).value=='NA')
		{
			document.getElementById("remarks"+i).disabled=true;
			document.getElementById("value"+i).disabled=true;
		}
		else
		if(document.getElementById("menu"+i).value=='AS')
		{
			document.getElementById("remarks"+i).disabled=false;
			document.getElementById("value"+i).disabled=false;
		}
	}
	*/
}
function ValueAfterDiscount(i)
{
	
	  var disc=document.getElementById("discount_"+i).value;
	  var disctype=document.getElementById("discountType_"+i).value;
	  if(disc=='')
	  {
		 var stockvalue=document.getElementById("stock_value_"+i).value;
		
		 document.getElementById("valueAfterDisc_"+i).value=stockvalue;
	  }
	  else
	  {
	  	if(disctype=='A')
	  	{
	  	 var stockvalue=document.getElementById("stock_value_"+i).value;
	  	 var valueafterdisc=parseFloat(stockvalue)-parseFloat(disc);
	  	 document.getElementById("valueAfterDisc_"+i).value=valueafterdisc;
	  	}
	    if(disctype=='P')
	  	{
	  		var stockvalue=document.getElementById("stock_value_"+i).value;
	  		var discpercvalue=(parseFloat(stockvalue)*parseFloat(disc))/100;
	  		var finalValue=(parseFloat(stockvalue)-parseFloat(discpercvalue)).toFixed(2);
	  		document.getElementById("valueAfterDisc_"+i).value=finalValue;
	  		
	  		
	  	}
	  }
	  
	
}
function claculateExcise(str)
{
	var discounted=document.getElementById("totalDiscountedPrice").value;
	if (str.length==0)
  		{
  			document.getElementById("exciseId").innerHTML="";
  			return;
  		}
		if (window.XMLHttpRequest)
  		{// code for IE7+, Firefox, Chrome, Opera, Safari
  			xmlhttp=new XMLHttpRequest();
  		}
		else
  		{// code for IE6, IE5
  			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  		}
		xmlhttp.onreadystatechange=function()
  		{
  			if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		{
    			document.getElementById("exciseId").innerHTML=xmlhttp.responseText;
    		}
  		}
		xmlhttp.open("GET","calculateExcise.php?taxId="+str+"&discounted="+discounted,true);
		xmlhttp.send();
}
function calculateVATCST(str)
{
	var taxableAmount=document.getElementById("taxableAmount").value;
	if (str.length==0)
  		{
  			document.getElementById("VATId").innerHTML="";
  			return;
  		}
		if (window.XMLHttpRequest)
  		{// code for IE7+, Firefox, Chrome, Opera, Safari
  			xmlhttp=new XMLHttpRequest();
  		}
		else
  		{// code for IE6, IE5
  			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  		}
		xmlhttp.onreadystatechange=function()
  		{
  			if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		{
    			document.getElementById("VATId").innerHTML=xmlhttp.responseText;
    		}
  		}
		xmlhttp.open("GET","calculateExcise.php?salesId="+str+"&taxable="+taxableAmount,true);
		xmlhttp.send();
}
function calculateECess()
{
	var exciseValue=document.getElementById("exciseValue").value;
	var ECessPercentage=document.getElementById("ECessPercentage").value;
	var total=exciseValue*(ECessPercentage/100);
	document.getElementById("ECessValue").value=total.toFixed(2);
}
function calculateSAHCess()
{
	var exciseValue=document.getElementById("exciseValue").value;
	var SAHCessPercentage=document.getElementById("SAHCessPercentage").value;
	var total=exciseValue*(SAHCessPercentage/100);
	document.getElementById("SAHCessValue").value=total.toFixed(2);
}
function calculateTaxable()
{
	var totalDiscountedPrice=document.getElementById("totalDiscountedPrice").value;
	var exciseValue=document.getElementById("exciseValue").value;
	var ECessValue=document.getElementById("ECessValue").value;
	var SAHCessValue=document.getElementById("SAHCessValue").value;
	
	if(totalDiscountedPrice==''){totalDiscountedPrice=0;}
	if(exciseValue==''){exciseValue=0;}
	if(ECessValue==''){ECessValue=0;}
	if(SAHCessValue==''){SAHCessValue=0;}
	
	var TOTAL=parseFloat(totalDiscountedPrice)+parseFloat(exciseValue)+parseFloat(ECessValue)+parseFloat(SAHCessValue);
	document.getElementById("taxableAmount").value=TOTAL.toFixed(2);
}
function calculateGrand()
{
	var a=document.getElementById("totalDiscountedPrice").value;
	var b=document.getElementById("taxableAmount").value;
	var c=document.getElementById("VatCstValue").value;
	var d=document.getElementById("value2").value;
	var e=document.getElementById("value1").value;
	var f=document.getElementById("value4").value;
	
	
	if(b==''){b=0;}
	if(c==''){c=0;}
	if(d==''){d=0;}
	if(e==''){e=0;}
	if(f==''){f=0;}
	
	var GRAND=parseFloat(b)+parseFloat(c)+parseFloat(d)+parseFloat(e)+parseFloat(f);
	grandBrk=GRAND.toFixed(2).split('.');
	grandA=grandBrk[0];
	grandB=grandBrk[1];
	//alert(grandA);
	//alert(grandB);
	if(grandB>'50' || grandB=='50')
	{
		GRAND11=parseInt(GRAND)+1;
	}
	else
	if(grandB<'50')
	{
		GRAND11=parseInt(GRAND);
	}	
	document.getElementById("grandTotal").value=GRAND11.toFixed(2);
	
	document.getElementById("roundOff").value="."+grandB;
	
}
 

function GetLeads(str)
{
	var site=document.getElementById("site").value;
	if (str.length==0)
  {
  document.getElementById("hid_Leads").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("hid_Leads").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","CRM_GetLeads.php?str="+str+"&site="+site,true);
xmlhttp.send();
	
	
	
	
	
	

}
</script>
<?php 
if($_REQUEST['action']=='Edit')
{ 
				 $sql1=DB_query("Select * from quotation where id='".$_REQUEST['QID']."'",$db);
				$result1=DB_fetch_array($sql1);
				$site= $result1['site']; 
				$quotationNo=$result1['quotationNo'];  
				$quotationDate=ConvertSqlDate($result1['quotationDate']);  
					$party=$result1['party'];   
					$quotationType=$result1['quotationType'];
					$quotation_subject=$result1['quotation_subject'];
					$quotation_funnel  =$result1['quotation_funnel'];
					$quotation_stage  =$result1['quotation_stage'];
				 	$enquiryNo =$result1['enquiryNo'];
  					$source=$result1['source1'];  
					$partyContact=$result1['partyContact'];  
			        $validTill=ConvertSQLDate($result1['validTill']);  
					$pack_forward=$result1['pack_forward'];  
					$freight=$result1['freight']; 
					$payTerms=$result1['payTerms'];  
					$salesTax=$result1['salesTax'];  
					$Excise=$result1['Excise'];  
					$insurance=$result1['insurance'];  
					$basicAmount=$result1['basicAmount'];  
					$totalDiscountedPrice=$result1['totalDiscountedPrice'];  
					$exciseDuty=$result1['exciseDuty'];  
					$exciseValue=$result1['exciseValue'];  
					$ECessValue=$result1['ECessValue'];  
					$SAHCessValue=$result1['SAHCessValue'];  
					$taxableAmount=$result1['taxableAmount'];
					$VatCst=$result1['VatCst'];  
					$VatCstValue=$result1['VatCstValue'];  
					$freightValue=$result1['freightValue'];  
					$packagingCharges=$result1['packagingCharges'];  
					$additionalCharges=$result1['additionalCharges'];  
					$roundOff=$result1['roundOff'];  
				  	$grandTotal=$result1['grandTotal'];  
					$createdBy=$_SESSION['UserID'];  
					
					$ConvertIntoSales=$result1['ConvertIntoSales'];  
						
					$SalesOrderNo=$result1['SalesOrderNo'];  
}


?>



<?php //echo $ConvertIntoSales;?>
	<table border="0">
<tr>
    <td>	
	<?php  if( $ConvertIntoSales=='N'){?>
		<a href="index.php?route=CRM/CRMSalesOrder&QID=<?php  echo $_REQUEST['QID'].$rurl;?>">
	<input type="button" name="Create" value="Convert into Sales Order" style="background-color:#d1d4be; color:#006699; font-weight:800; padding-top:3px; padding-bottom:3px;" /></a>
	
	<?php } elseif($_REQUEST['action']!='AddNew'){?>
		<a href="index.php?route=CRM/CRMSalesOrder&action=Edit&SID=<?php echo $SalesOrderNo?>&QID=<?php  echo $_REQUEST['QID'].$rurl;?>">
	<input type="button" name="ViewSaleOrder" value="View Sales Order" style="background-color:#d1d4be; color:#006699; font-weight:800; padding-top:3px; padding-bottom:3px;" /></a>
	
	
	<?php }
	if($_REQUEST['action']=='Edit'){
	?>
	 <a href="CRM/tcpdf/PDFQuotes.php?record=<?php echo $_REQUEST['QID'].$rurl;?>">
	 <input type="button" name=" Export to PDF" value=" Export to PDF" style=" background-image:
	 url(images/PDF.gif)background-color:#d1d4be; color:#006699; font-weight:800; padding-top:3px; padding-bottom:3px; " />
</a><?php } ?>
	 
</td></tr></table>
 <form action="" method="post"  name="frm1">
	<div style="width:100%"><!---Now Inserted-->
		<div style="width:800px;"><!---Now Inserted-->
			<fieldset style="background-color:#FFFFFF"><!---Now Inserted-->
			<legend>Quote Management</legend>
<table width="100%" cellpadding="0" cellspacing="0" border="0" bgcolor="#FFFFFF" style="border:1px solid #CCC;" align="center">

<h3>Quote Offer To Customer</h3>	
<?php


if(count($msgErr>0))
{
?>
<tr><td colspan="4">
<?php
for($k=0;$k<count($msgErr);$k++) 
{
echo "<div class='error'>".$msgErr[$k]."</div>";
}
?>	
</td></tr>	
<?php	
}
 
?>	
<tr>
<td>
<table width="80%" border="0" cellspacing="1" cellpadding="2">
	
  <tr>
    <th>Branch<label><font color="#FF0000">*</font></label></th>
    <td>
    <select name="site"   id="site" onchange="ShowDiv(this.value,'CODE','codeGenerator.php?quotesite=crmquotes');" style="width:107px;">
    <option value="0">Select</option>
    <?php 
    $site_sql=DB_query("Select loccode, locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')",$db);
    while($site_row=DB_fetch_array($site_sql))
	{?>
		<option value="<?php echo $site_row['loccode']?>"<?php echo($site==$site_row['loccode']?'selected':'')?>><?php echo $site_row['locationname']?></option>
	<?php 	
	}
    ?>
    </select></td>
    <th>Quotation No.</th>
    <td id="CODE"><input name="quotationNo" type="text" value="<?php echo $quotationNo;?>" style="width:auto;"/></td>
  </tr>
  <tr>
    <th>Quotation Date <label><font color="#FF0000">*</font></label></th>
    <td><input type="text" name="quotationDate" value="<?php if(isset($quotationDate)){echo $quotationDate;}?>" style="width:auto" class="datepicker" /></td>
    <th>Format<label><font color="#FF0000">*</font></label></th>
    <td><select name="format" style="width:107px;">
      <option value="Standard">Standard</option>
    </select></td>
  </tr>
  <tr>
    <th>Account Name<font color="#FF0000">*</font></th>
    <td>
    	<?php
	 // echo "Select vender_client,AccountsID,AccountName,ledgertype from accountledger where ledgertype in('leads','client')";
		?>
	<select name="party" style="width:107px;" onchange="GetLeads(this.value)" >
      <option value="000">Select</option>
	   <!--<?php  echo $ClassParty->PartList($party);?>-->
	   
	   <?php
	   			//echo  $_SESSION['UserSites'];
	   	$client_sql=DB_query("Select vender_client,AccountsID,AccountName,ledgertype from accountledger where ledgertype in('leads','client')",$db);
						  while($client_row=DB_fetch_array($client_sql))
						  {
							 if($client_row['ledgertype']=='client'){
							  $ClientSQL="Select Site,Zone from clientmaster where CompanyId='".$_SESSION['ACGCompanyId']."' and  Site='".$_SESSION['AssignedBranch']."' 
							 and id='".$client_row['vender_client']."'  ";
							 }
							 else  if($client_row['ledgertype']=='leads')
							 {
							 $ClientSQL="Select Site,Zone from clientmaster where CompanyId='".$_SESSION['ACGCompanyId']."' and  Site='".$_SESSION['AssignedBranch']."' 
							 and id='".$client_row['AccountName']."'  ";
							 }
							  $ClientRes=DB_query($ClientSQL,$db);
							  if(DB_num_rows($ClientRes)>0)
							  {
						  ?>
							<option value="<?php echo $client_row['AccountsID'];?>"<?php echo($party==$client_row['AccountsID']?'selected':'')?>><?php  
							if($client_row['ledgertype']=='leads')
							echo $s->GetFieldValue('clientmaster',"id='".$client_row['AccountName']."'",'c_name');
							else 
						  echo $client_row['AccountName'];?></option>
						  <?php }
						  } ?>
     
    </select>     </td>
    <th>Type of Quotation<font color="#FF0000">*</font></th>
    <td><select name="quotationType" style="width:107px;">
      <option value="M"<?php echo($quotationType=='M'?'selected':'')?>>Material</option>
      <option value="E"<?php echo($quotationType=='E'?'selected':'')?>>Export</option>
      <option value="R"<?php echo($quotationType=='R'?'selected':'')?>>Retail</option>
    </select></td>
    </tr>
  <tr>
    <th>Against Enquiry<label><font color="#FF0000">*</font></label></th>
    <td><div id='hid_Leads'>
	<?php 
	if($_REQUEST['action']=='Edit')
		$enquery_leadid=$s->GetFieldValue('crm_leads',"leadid='".$result1['enquiryNo']."'",'leadcode');
		else
		 $enquery_leadid=$enquiryNo;
		//echo $enquery_leadid;
	?>
	
	
	<input type="text" name="enquiryNo" style="width:107px;" value="<?php  echo $enquery_leadid;?>" /></div></td>
    <th>Quote Stage</th>
    <td><input type="text" style="width:auto;"  name="quotation_stage" value="<?php  echo $quotation_stage;?>"/></td>
  </tr>
  
  <tr>
    <th>Mobile No.</th>
    <td><input type="text" name="partyContact2" style="width:auto" value="<?php if(isset($partyContact)){echo $partyContact;} else { echo $_REQUEST['partyContact'];}?>" /></td>
    <th>Valid Till<label><font color="#FF0000">*</font></label></th>
    <td><input name="validTill" type="text" value="<?php if(isset($validTill)){echo $validTill;} ?>" style="width:auto;" class="datepicker"/></td>
  </tr>
  <tr>
    <th>Funnel</th>
    <td><input type="text" style="width:auto;" name="quotation_funnel" value="<?php  echo $quotation_funnel;?>" /></td>
  </tr>
</table>
</td>
</tr>
<tr>
<td valign="top">
 
   <div id='hid_LeadsRecord'>

	<h3>Account  Billing  &  shipping  Address  Information</h3>

 	<!--- table 2 --><table width="80%" border="0">	
<tr>
<td>
<?php 
 if($_REQUEST['action']=='Edit')
 {
     $getlead=DB_query( "select * from crm_accounts where mst_leadid='".$enquiryNo."'  ",$db);
	 while($row_lead=DB_fetch_array($getlead))
	 {
		$leadcode=$row_lead['leadcode'];
		$company_name=$row_lead['company_name'];
		$address1=$row_lead['address1'];
		$groupid=$row_lead['groupid'];
		$city=$row_lead['city'];
		$zipcode=$row_lead['zipcode'];
		$country=$row_lead['country'];
		$Shipping_address=$row_lead['Shipping_address'];
		$Shipping_zipcode=$row_lead['Shipping_zipcode'];
		$Shipping_city=$row_lead['Shipping_city'];
		$Shipping_country=$row_lead['Shipping_country'];

  	 }
 }
?>
<table width="100%" border="0" cellspacing="1" cellpadding="2">
  <tr>
    <th>Billing Address</th>
    <td>
    <input type="text" name="address1" style="width:auto;"   value="<?php  echo $address1;?>"/></td>
    <th>Shipping Address</th>
    <td id="CODE"><input type="text" name="Shipping_address" style="width:auto;"   value="<?php  echo $Shipping_address;?>"/></td>
  </tr>
  <tr>
    <th>Billing city</th>
    <td><input name="city" type="text"  style="width:auto;" value="<?php  echo $city?>"/></td>
    <th nowrap="nowrap">shipping City</th>
    <td><input name="Shipping_city" type="text"  style="width:auto;" value="<?php  echo $Shipping_city?>"/></td>
  </tr>
	
<tr>
    <th nowrap="nowrap">Billing Postal Code</th>
    <td>
    <input type="text" name="zipcode" style="width:auto;"  value="<?php  echo $zipcode?>" /></td>
    <th>Shipping Postal Code</th>
    <td><input type="text" name="Shipping_zipcode" style="width:auto;"  value="<?php  echo $Shipping_zipcode?>" /></td>
    </tr>
	
	<tr>
    <th nowrap="nowrap">Billing Country</th>
    <td><input name="country" type="text"  style="width:auto;" value="<?php  echo $country?>"/></td>
    <th>Shipping Country</th>
    <td><input name="Shipping_country" type="text"  style="width:auto;" value="<?php  echo $Shipping_country?>"/></td>
    </tr>
</table>
</td>
</tr>
 
  
 </table>
 
 <div>
 <?php 
 if($_REQUEST['action']=='Edit'){?>
 <h3>Item Details</h3>
 <table align="center" width="100%">
<tr>
<th>Item Code</th>
<th>Item Name</th>
<th>Unit</th>
<th>Qty</th>
<th>Price</th>
<th>Per Unit(s)</th>
<th>Value</th>
<th>Discount Type</th>
<th>Discount</th>
 <th>Total</th>
</tr>
	<?php $total=0; $i=0;$after_discount=0;
	echo "select * from quotation_details where parentId='".$_REQUEST['QID']."' ";
	$getitem=DB_query("select * from quotation_details where parentId='".$_REQUEST['QID']."' ",$db); 
    while($row=DB_fetch_array($getitem))
 	{
  	 $itemquery=DB_query("select * from item_master where id='".$row['itemCode']."'" , $db);
 	while($item_row=DB_fetch_array($itemquery))
	{
		if($i%2==0)
		{$bg="#CCCCCC";}
		else
		$bg="#C8BECB";
 	?>
	<tr bgcolor="<?php echo $bg;?>">
     <td><?php echo $row['itemCode'];?><input type="hidden" name="item_code[<?php echo $i;?>]" id="item_code_<?php echo $i;?>" value="<?php echo $row['itemCode'];?>">	</td>
   <td><?php echo $item_row['item_name'];?><input type="hidden" name="item_name[<?php echo $i;?>]" id="item_name[<?php echo $i;?>]" value="<?php echo $item_row['item_name'];?>"/>	</td>
   <td><?php		$grade_query=DB_query("select * from unitsofmeasure where unitid='".$item_row['unit']."'", $db);
		while($grade_row=DB_fetch_array($grade_query))
		 {
		 echo $grade_row['unitname'];?>
		<?php } ?><input type="hidden" name="Unit[<?php echo $i;?>]" id="Unit[<?php echo $i;?>]" value="<?php $grade_row['id'];?>"/>			</td>
    <td><input type="text" name="stock_qty[<?php echo $i;?>]" id="stock_qty_<?php echo $i;?>" style="width:80px;"  value="<?php echo $row['quantity'];?>" readonly="true"/></td>
    <td><input type="text" name="stock_price[<?php echo $i;?>]" id="stock_price_<?php echo $i;?>" style="width:60px;" value="<?php echo $row['price'];?>" readonly="true" /></td>
    <td><input type="text" name="stock_perunit[<?php echo $i;?>]" id="stock_perunit_<?php echo $i;?>" style="width:50px;" value="<?php echo $row['PerUnits'];?>" readonly="true"/></td>
    <td><input type="text" name="stock_value[<?php echo $i;?>]" id="stock_value_<?php echo $i;?>" style="width:50px;" value="<?php echo $row['totalValue'];?>" readonly="true"/></td>
 	 <td><select   name="discountType[<?php echo $i;?>]"
	 id="discountType_<?php echo $i;?>"  style="width:auto">
         <option value="A" <?php  echo ($row['discountType']=='A'?'selected':'')?>>Amount</option>
         <option value="P" <?php  echo ($row['discountType']=='P'?'selected':'')?>>Percent</option>
       </select></td>
 <td>
<?php   $total+=$row['total_stock'];
$after_discount+=$row['stock_value']-$row['total_stock'];
 ?>
<input type="text" name="discount[<?php echo $i;?>]" id="discount_<?php echo $i;?>" style="width:50px;" value="<?php echo $row['discount'];?>" /></td>
 
<td><input type="text" name="valueAfterDisc[<?php echo $i;?>]" id="valueAfterDisc_<?php echo $i;?>" value="<?php echo $row['valueAfterDisc']?>" size="15"/></td>
   
</tr>

   
  
  <?php } 
    $i++;
  }?>
</table>
 </div>
<input type="hidden" name="itemcount_totalamt" id="itemcount_totalamt" value="<?php echo $total;?>" />
<input type="hidden" name="itemcount_AfterDisc" id="itemcount_AfterDisc" value="<?php echo $after_discount;?>" />

<input type="hidden" name="hiditemcount" id="hiditemcount" value="<?php echo $i;?>" />
 <?php }else{ ?>
 
<div>
<h3>Item Details</h3>
<input type="button" onclick="addRow('dataTable')" value="Add More" />&nbsp;
<input type="button" class="crmbutton small delete"  onclick="deleteRow('dataTable')" value="Delete" />
<div style="overflow: auto; width: 800px">
<table align="center" width="700px;" border="0">
<tr><th style="text-align:center;" colspan="11">Item Details</th></tr>
<tr>
<th>Sr No</th>
<th>Item Code</th>
<th>Item Name</th>
<th>Qty</th>
<th>Price</th>
<th>Per Unit(s)</th>
<th>Value</th>
<th>Discount Type</th>
<th>Discount</th>
 <th>Total</th>
</tr>
  <tbody id="dataTable">
  	<tr>  
    <td><input type="checkbox" name="chk" />    	
    	<input type="hidden" value="" id="user_shipping" name="user_shipping[]" />
    	<input type="button" name="profile_popup" value="+" id="profile_popup"  onclick="openPopupProfile(this.id)" /></td>
    	<td><input type="text" name="item_code[]" id="item_code" value=""   >	</td>
   <td><input type="text" name="item_name[]" id="item_name" />	</td>
    <td><input type="text" name="stock_qty[]" id="stock_qty" style="width:80px;"  readonly="true"/></td>
    <td><input type="text" name="stock_price[]" id="stock_price" style="width:60px;"   /></td>
    <td><input type="text" name="stock_perunit[]" id="stock_perunit" style="width:50px;" onchange="calamount();" /></td>
    <td><input type="text" name="stock_value[]" id="stock_value" style="width:50px;" value="" /></td>
     <td><select   name="discountType[]" id="discountType"  style="width: 98%">
         <option value="A">Amount</option>
         <option value="P">Percent</option>
       </select></td>
<td>
<?php  $total+=$row['discount'];
$after_discount+=$row['stock_value']-$row['total_stock'];
 ?>
<input type="text" name="discount[]" id="discount" style="width:50px;" onchange="caldiscount();"  /></td> 
<td><input type="text"   name="valueAfterDisc[]" id="valueAfterDisc" size="15"/>
	<input type="hidden" name="hiditemcount" id="hiditemcount" value="undefined" />
	
</td>
 </tr>
</tbody>
 </table>
 </div>

 <?php }?>
 </div>
</div>
<br>
<br>
<table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td valign="top" width="50%">
<table width="90%" border="0" cellspacing="1" cellpadding="2" align="left" bgcolor="#FFFFFF" style="border:1px solid #ccc;">
	<tr>
    <th style="text-align:center;" colspan="2">Terms And Conditions</th>
    </tr>
    <tr>
      <th width="19%" scope="row">Pkg &amp; Forwarding</th>
      <td width="28%" nowrap="nowrap">
      <select name="pack_forward" id="menu1" style="width:107px;" onchange="checkApplicable(this.value)">
      	<option value="AS">As per calculation</option>
      	<option value="NA">N/A</option>
      </select><input type="text" name="PFRemarks" id="remarks1" value="" style="width:auto;" />
      </td>
      </tr>
      <tr>
    <th>Freight</th>
    <td><select name="freight" id="menu2"
    style="width:107px;;" onchange="checkApplicable(this.value)">
      	<option value="AS">As per calculation</option>
      	<option value="NA">N/A</option>
      </select><input type="text" name="freightRemarks" id="remarks2" value="" style="width:auto;" /></td>
    </tr>
  <tr>
    <th>Payment Terms</th>
    <td><select name="payTerms" style="width:107px;">
      	<?php 
      	$payment_sql=DB_query("Select * from paymentterms",$db);
		while($payment_row=DB_fetch_array($payment_sql))
		{?>
			<option value="<?php echo $payment_row['termsindicator'];?>"><?php echo $payment_row['terms'];?></option>
		<?php 	
		}
      	?>
      </select></td>
    </tr>
    <tr>
    <th>Sales Tax</th>
    <td><select name="salesTax" id="menu3" style="width:107px;" onchange="checkApplicable(this.value)">
      	<option value="AS">As per calculation</option>
      	<option value="NA">N/A</option>
      </select><input type="text" name="salesRemarks" id="remarks3" value="" style="width:auto;"/></td>
  </tr>
  <tr>
    <th>Excise</th>
    <td><select name="Excise" id="" style="width:107px;" onchange="checkApplicable(this.value)">
      	<option value="AS">As per calculation</option>
      	<option value="NA">N/A</option>
      </select><input type="text" name="exciseRemarks" id="" value="" style="width:auto;" /></td>
    </tr>
    <tr>
    <th nowrap="nowrap">Insurance</th>
    <td><select name="insurance" id="menu4" style="width:107px;" onchange="checkApplicable(this.value)">
      	<option value="AS">As per calculation</option>
      	<option value="NA">N/A</option>
      </select><input type="text" name="insRemarks" id="remarks4" value="" style="width:auto;"/></td>
    </tr>
</table>	
</td>
<td valign="top">
<table width="90%" border="0" cellspacing="1" cellpadding="0" align="right" bgcolor="#FFFFFF" style="border:1px solid #ccc;">
    		<tr>
    	<th style="text-align:center;" colspan="2">Amount And Taxes</th>
    </tr>
    <tr>
    	  <tr>
    	    <th>Basic Amount<label><font color="#FF0000">*</font></label></th>
    	    <td><input name="basicAmount" type="text" value="<?php echo $basicAmount;?>" style="width:auto;" onfocus="calculateBasic()" id="basicAmount"   /></td>
    	    
  	    </tr>
    	  <tr>
    	    <th nowrap="nowrap">Discounted Price<label><font color="#FF0000">*</font></label></th>
    	    <td><input name="totalDiscountedPrice" id="totalDiscountedPrice" type="text" value="<?php echo $totalDiscountedPrice;?>" style="width:auto;" onfocus ="calculateDiscounted()"/>
			
			
			</td>
    	    
  	    </tr>
    	  <tr>
    	    <th>Excise</th>
    	    <td>
    	    <select name="exciseDuty" style="width:107px;" onchange="claculateExcise(this.value)">
    	    <option value="0">Select</option>
    	    <?php 
    	    $excise_sql=DB_query("Select * from excise_tax where caption like '%excise%' and form_type='Sales'",$db);
			while($excise_row=DB_fetch_array($excise_sql))
			{?>
				<option value="<?php echo $excise_row['id'];?>"><?php echo $excise_row['caption'];?></option>
			<?php 	
			}
    	    ?> 
  	      </select><div id="exciseId" style="width:110px;"><input name="exciseValue" id="exciseValue" type="text" value="<?php echo $exciseValue;?>" style="width:auto;" /></div>
    	   </tr>
    	     
    	     <?php 
    	     $cessSql=DB_query("Select caption from excise_tax where caption like '%cess%'",$db);
			 $k=1;
			 while($cessRow=DB_fetch_array($cessSql))
			 {
			 	$caption[$k]=$cessRow['caption']; 
				
				$rate_sql=DB_query("Select def_rate from excise_tax where caption='".$caption[$k]."'",$db);
				while($rate_row=DB_fetch_array($rate_sql))
				{
					$rate[$k]=$rate_row['def_rate'];
				}
				$k++;
			 } 
    	     ?>
    	     <tr>
    	    <th>E-Cess</th>
    	    <td><input name="ECessPercentage" id="ECessPercentage" type="text" value="<?php echo $rate[1];?>" style="width:auto;" readonly />
    	    	<input name="ECessValue" id="ECessValue" type="text" value="<?php echo $ECessValue?>" style="width:auto;" readonly onfocus="calculateECess()"/></td>
    	    </tr>
    	    <tr>
            <th nowrap="nowrap">SAH-E Cess </th>
    	    <td><input name="SAHCessPercentage" id="SAHCessPercentage" type="text" value="<?php echo $rate[2];?>" style="width:auto;" readonly/>
    	    	<input name="SAHCessValue" id="SAHCessValue" type="text" value="<?php echo $SAHCessValue?>" style="width:auto;" readonly onfocus="calculateSAHCess()"/></td>
  	    	</tr>
    	  
    	  
   <tr>
   	<th>Taxable Amount<label><font color="#FF0000">*</font></label></th>
   	<td><input name="taxableAmount" id="taxableAmount" type="text" value="<?php echo $taxableAmount?>" style="width:auto;" onfocus="calculateTaxable()"/></td>
   </tr>
    	  
    	  
    	  
    	  <tr>
    	    <th>VAT/CST</th>
    	    <td>
    	    <select name="VatCst" id="value3" style="width:107px;" onchange="calculateVATCST(this.value)">
    	    <option value="" style="width:auto;">Select</option>
    	    <?php 
    	    $salesSql=DB_query("Select * from sales_tax_master",$db);
			while($salesRow=DB_fetch_array($salesSql))
			{?>
				<option value="<?php echo $salesRow['salestax_id'];?>" <?php echo $VatCst==$salesRow['salestax_id']?'selected':''?>><?php echo $salesRow['tax_code'];?></option>
			<?php 	
			}
    	    ?>
  	      </select>
    	      <div id="VATId" style="width:110px;"> <input name="VatCstValue" id="VatCstValue" type="text" value="<?php echo $VatCstValue?>" style="width:auto;" readonly /></div></td>
    	    
  	    </tr>
    	  <tr>
    	    <th>Freight</th>
    	    <td><input name="freightValue" id="value2" type="text" value="<?php echo $freightValue?>" style="width:auto;" /></td>
    	    
  	    </tr>
    	  <tr>
    	    <th>Pkg Charges</th>
    	    <td><input name="packagingCharges" id="value1" type="text" value="<?php echo $packagingCharges;?>" style="width:auto;" /></td>
    	    
  	    </tr>
  	    <tr>
    	    <th>Additional Charges</th>
    	    <td><input name="additionalCharges" id="value4" type="text" value="<?php  echo $additionalCharges;?>" style="width:auto;" /></td>
    	    
  	    </tr>
    	  <tr>
    	    <th>Round Off</th>
    	    <td><input name="roundOff" id="roundOff" type="text" value="<?php echo $roundOff;?>" readonly="true" style="width:auto;" /></td>
    	    
  	    </tr>
    	  <tr>
    	    <th>Grand Total<label><font color="#FF0000">*</font></label></th>
    	    <td><input name="grandTotal" id="grandTotal" type="text"  style="width:auto;" onfocus="calculateGrand()" readonly="readonly" value="<?php echo $grandTotal  ;?>" /></td>
    	    
  	    </tr>
  	    </table>
</td>
</tr></table>
<p align="center"><input type="submit" name="Save" value="Save"  /></p>
</fieldset>
</div>
</div>
</form>

<script>
	
function openPopupProfile(i)
{ 
	    var site=document.getElementById("site").value;
	   
		window.open("pickcrmquotesitems.php?rowcount="+i+"&site="+site,"profiles_popup_window","height=425,width=640,toolbar=no,menubar=no,dependent=yes,resizable=yes");
}

function calamount()
{	
	var stock_qty=document.getElementById("stock_qty").value;
	var stock_price=document.getElementById("stock_price").value;
	var stock_perunit=document.getElementById("stock_perunit").value;
	if(stock_perunit>1)
	{
	var price=parseFloat(document.getElementById("stock_price").value)/parseFloat(document.getElementById("stock_perunit").value);	
	}
	else
	{
	var price=parseFloat(document.getElementById("stock_price").value);	
	}
	var amount=parseFloat(document.getElementById("stock_qty").value)*price;   
    document.getElementById("stock_value").value=amount;	
	

}

function caldiscount()
{

var discounttype=document.getElementById("discountType").value;
var tvalue=parseFloat(document.getElementById("stock_value").value);
if(discounttype=='Amount')
{
var amount=tvalue-parseFloat(document.getElementById("discount").value);	
}	
else
{
var amount=(tvalue*parseFloat(document.getElementById("discount").value))/100;	
}	
document.getElementById("valueAfterDisc").value=amount.toFixed(2);	
}


	
</script>