<?php 
if($_REQUEST['item_popup']!='')
{
	$_SESSION['site']=$_REQUEST['site'];
	$_SESSION['enquiryNo']=$_REQUEST['enquiryNo'];
	$_SESSION['enquiryDate']=$_REQUEST['enquiryDate'];
	$_SESSION['enquirySrc']=$_REQUEST['enquirySrc'];
	$_SESSION['enquiryParty']=$_REQUEST['enquiryParty'];
	$_SESSION['enquiryType']=$_REQUEST['enquiryType'];
	$_SESSION['partyContact']=$_REQUEST['partyContact'];
}
if($_REQUEST['Cancel']!='')
{
	unset($_SESSION['site']);
	unset($_SESSION['enquiryNo']);
	unset($_SESSION['enquiryDate']);
	unset($_SESSION['enquirySrc']);
	unset($_SESSION['enquiryParty']);
	unset($_SESSION['enquiryType']);
	unset($_SESSION['FinalEnquiryArray']);
	unset($_SESSION['partyContact']);
}
if($_REQUEST['Save'])
{
$fstatus=true;
  if($_REQUEST['site']=='000')
  {
  	  $msgErr[]="Please Select Branch Name"	;
      $fstatus=false;
  }
  if($_REQUEST['enquiryNo']=='')
  {
  	  $msgErr[]="Please Enter Enquiry no"	;
      $fstatus=false;
  }	
	 if($_REQUEST['enquiryDate']=='')
  {
  	  $msgErr[]="Please Select Enquiry Date";
      $fstatus=false;
  }
   if($_REQUEST['enquiryParty']=='000')
  {
  	  $msgErr[]="Please Select Party  Name"	;
      $fstatus=false;
  }
   if($_REQUEST['enquiryType']=='000')
  {
  	  $msgErr[]="Please Select Branch Name"	;
      $fstatus=false;
  }
	
	
	
	
}
if($_REQUEST['Save']!='' and $fstatus)
{
	
	$insert1="Insert into enquiry set 
							site='".$_REQUEST['site']."',    
							enquiryNo='".$_REQUEST['enquiryNo']."',  
							enquiryDate='".FormatDateForSQL($_REQUEST['enquiryDate'])."',  
							enquirySrc='".$_REQUEST['enquirySrc']."',
							enquiryType='".$_REQUEST['enquiryType']."', 
							party='".$_REQUEST['enquiryParty']."',   
							partyContact='".$_REQUEST['partyContact']."',  
							createdDate='".FormatDateForSQL(date('d/m/Y'))."',  
							createdBy='".$_SESSION['UserID']."',
							CompanyId='".$_SESSION['ACGCompanyId']."'";
							
	$result1=DB_query($insert1,$db);
	
	$masterId=DB_Last_Insert_ID($db,'enquiry','id');
	
	if($masterId!='')
	{
		for($j=0;$j<$_REQUEST['count'];$j++)
		{	
			$insert2="Insert into enquiry_details set
							parentId='".$masterId."',  
							itemCode='".$_REQUEST['itemCode'][$j]."',  
							quantity='".$_REQUEST['quantity'][$j]."',   
							price='".$_REQUEST['price'][$j]."',
							pricePer='".$_REQUEST['pricePer'][$j]."',  
							totalValue='".$_REQUEST['totalValue'][$j]."',  
							requisitionDate='".FormatDateForSQL($_REQUEST['requisitionDate'][$j])."',  
							status='".$_REQUEST['status'][$j]."'  ";
			$result2=DB_query($insert2,$db);
		}						
	}
	


prnMsg( _('Record inserted successfully'),'success');	
	unset($_SESSION['site']);
	unset($_SESSION['enquiryNo']);
	unset($_SESSION['enquiryDate']);
	unset($_SESSION['enquirySrc']);
	unset($_SESSION['enquiryParty']);
	unset($_SESSION['enquiryType']);
	unset($_SESSION['FinalEnquiryArray']);
echo "<script>location.href='index.php?route=enquiry_list&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."&action=view&Id=".$masterId."'</script>";


}
?>
<script language="JavaScript" type="text/javascript">
function partyDetails(str)
{
	if (str.length==0)
  {
  document.getElementById("partyId").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("partyId").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","showPartyDetails.php?code="+str,true);
xmlhttp.send();
}
function contactDetails(str)
{
	if (str.length==0)
  {
  document.getElementById("contactId").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("contactId").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","showPartyDetails.php?contact="+str,true);
xmlhttp.send();
}
function generateCode(str)
{
	
	if (str.length==0)
  {
  document.getElementById("codeId").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("codeId").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","codeGenerator.php?site="+str,true);
xmlhttp.send();
}
function item_window()
{
	window.open("EnquiryItems.php?action=add&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>","item_popup_window","height=500,width=900,toolbar=no,menubar=no,dependent=yes,resizable =yes, scrollbars=yes");
	
}
function calculateTotal(a)
{
	var qty=document.getElementById("quantity_"+a).value;
	var price=document.getElementById("price_"+a).value;
	var unit=document.getElementById("pricePer_"+a).value;
	
	
	var total=qty*(price/unit);
	document.getElementById("totalValue_"+a).value=total.toFixed(2);
}
</script>
<p><h2 align="center">Enquiry Management</h2></p>
<form action="" method="post">
<table width="70%">	
	<?php if(count($msgErr)>0){?>
   <tr>
   <td colspan="4"><?php
   for($i=0;$i<count($msgErr);$i++)
   {
     echo "<div class='error'>".$msgErr[$i].'</div>';
   }
    ?></td>
   </tr>
   <?php } ?>
	<tr><td bgcolor="#CCCCCC">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td bgcolor="#FFFFFF">
<table width="100%" border="0" cellspacing="1" cellpadding="2" align="center">
  <tr>
    <th width="19%" scope="row"><strong>Branch</strong></th>
    <td width="31%">
    <select name="site" style="width: 100%" onchange="generateCode(this.value)" >
    	<option value="0">Select</option>
    	<?php $site_sql=DB_query("Select loccode, locationname from locations",$db);
    	while($site_row=DB_fetch_array($site_sql))
		{?>
			<option value="<?php echo $site_row['loccode'];?>"<?php echo($_SESSION['site']==$site_row['loccode']?'selected':'')?>><?php echo $site_row['locationname'];?></option>
		<?php 	
		}
    	?>
    </select></td>
    <th width="25%"><strong>Enquiry No.</strong></th>
    <td width="25%" id="codeId"><input name="enquiryNo" type="text" value="<?php echo $_SESSION['enquiryNo'];?>" size="25" style="width:100%;"/></td>
  </tr>
  <tr>
    <th scope="row"><strong>Date</strong></th>
    <td><input name="enquiryDate" type="text" value="<?php if($_SESSION['enquiryDate']!=''){echo $_SESSION['enquiryDate'];}else{echo date('d/m/Y');}?>" size="25"  style="width:100%;" class="datepicker"/></td>
    <th><strong>Enquiry Source</strong></th>
    <td><select name="enquirySrc"  style="width:100%;">  
      <option value="CC"<?php echo ($_SESSION['enquirySrc']=='CC'?'selected':'')?>>Cold Call</option>
      <option value="EC"<?php echo ($_SESSION['enquirySrc']=='EC'?'selected':'')?>>Existing Customer</option>
      <option value="SG"<?php echo ($_SESSION['enquirySrc']=='SG'?'selected':'')?>>Self Generated</option>
      <option value="EMP"<?php echo ($_SESSION['enquirySrc']=='EMP'?'selected':'')?>>Employee</option>
      <option value="PTN"<?php echo ($_SESSION['enquirySrc']=='PTN'?'selected':'')?>>Partner</option>
      <option value="DM"<?php echo ($_SESSION['enquirySrc']=='DM'?'selected':'')?>>Direct Mail</option>
      <option value="WEB"<?php echo ($_SESSION['enquirySrc']=='WEB'?'selected':'')?>>Web Site</option>
      <option value="O"<?php echo ($_SESSION['enquirySrc']=='O'?'selected':'')?>>Other</option>
    </select></td>
  </tr>
  <tr>
    <th scope="row"><strong>Party Name</strong></th>
    <td><select name="enquiryParty" style="width:100%;" onchange="partyDetails(this.value)">
      <option value="000">---Select---</option>
      <?php 
      $party=DB_query("Select id,client_code,c_name from clientmaster",$db);
	  while($party_row=DB_fetch_array($party))
	  {?>
	  <option value="<?php echo $party_row['client_code'];?>"<?php echo($_SESSION['enquiryParty']==$party_row['client_code']?'selected':'')?>><?php echo $party_row['c_name'];?></option>	
	  <?php 	
	  }
      ?>
    </select></td>
    <th><strong>Type Of Enquiry</strong></th>
    <td><select name="enquiryType" style="width:100%;">
      <option value="M"<?php echo ($_SESSION['enquiryType']=='M'?'selected':'')?>>Material</option>
      <option value="E"<?php echo ($_SESSION['enquiryType']=='E'?'selected':'')?>>Export</option>
      <option value="R"<?php echo ($_SESSION['enquiryType']=='R'?'selected':'')?>>Retail</option>
    </select></td>
  </tr>
</table>
</td>
</tr>
<tr>
<td valign="top">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td colspan="4" bgcolor="#FFFFFF">
 <div id="partyId" style="width:100%; margin:0 auto;">
    <table width="100%" cellpadding="2" cellspacing="1" border="0" align="center">
        
<?php 
if($_SESSION['enquiryParty']!='')
{
	$sql222="Select * from clientmaster where client_code='".$_SESSION['enquiryParty']."'";
	$result222=DB_query($sql222,$db);
	$row222=DB_fetch_array($result222);

	$countryName=$s->getField('country_master','id',$row222['pre_country'],'country');
	$stateName=$s->getField('state_master','id',$row222['pre_state'],'state');
	$cityName=$s->getField('city_master','id',$row222['pre_city'],'city');
}
?> <tr>
<th width="19%" scope="row" rowspan="3"><strong>Address</strong></th>
<td width="31%"><input name="partyaddress1" type="text" value="<?php echo $row222['pre_address1'];?>" size="25"  style="width:100%;"/></td>
<th width="25%" scope="row"><strong>Contact Person</strong></th>
<td width="25%">
	<select name="partyContact" style="width: 100%" onchange="contactDetails(this.value)">
	<?php 
	if($_SESSION['enquiryParty']!='')
	{
		$contact_sql=DB_query("Select * from contact_information_master where master_code='".$_SESSION['enquiryParty']."' and master_prefix='C'",$db);
		while ($contact_row=DB_fetch_array($contact_sql)) 
		{?>
			<option value="<?php echo $contact_row['id'];?>"<?php echo ($_SESSION['partyContact']==$contact_row['id']?'selected':'')?>><?php echo $contact_row['person'];?></option>
		<?php 	
		}
	}
	?>
	</select>
</td>
</tr>
          <tr>
            
            <td><input name="partyaddress2" type="text" value="<?php echo $row222['pre_address2'];?>" size="25" style="width:100%;" /></td>
            <th scope="row"><strong>Mobile No.</strong></th>
            <?php 
            if($_SESSION['enquiryParty']!='')
            {
            	$sql="Select phone from contact_information_master where id='".$_SESSION['partyContact']."'";
				$result=DB_query($sql,$db);
				$myrow=DB_fetch_array($result);
            }
            ?>
            <td id="contactId"><input name="ContactMobile" type="text" value="<?php echo $myrow['phone'];?>" size="25" style="width:100%;"/></td>
            </tr>
<tr>
<td><input type="text" name="partyaddress3" value="<?php if($_SESSION['enquiryParty']!=''){echo $row222['pre_address3'].", ".$countryName.", ".$stateName.", ".$cityName;}?>" style="width: 100%"></td>	
</tr>
</table>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td></tr></table>


<p align="left"><input type="submit" name="item_popup" value="Pick Items" id="item_popup"  onclick="item_window();" /></p>
<table width="100%" border="0" cellpadding="2" cellspacing="2">
<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>Date Of Requisition</th>
<th>Status</th>
</tr>
<?php 
if($_SESSION['FinalEnquiryArray']!='')
{
	$tt=explode(',', $_SESSION['FinalEnquiryArray']);
	for($i=0;$i<count($tt);$i++)
	{
		$item_sql=DB_query("Select * from item_master where StockID='".$tt[$i]."'",$db);
		$item_row=DB_fetch_array($item_sql);
	
		
		$rate_sql=DB_query("Select Rate from rate_master where ItemId ='".$tt[$i]."' and PartyId='".$_SESSION['enquiryParty']."' and DeActivate='0'",$db);
		$rate_row=DB_fetch_array($rate_sql);
		$_REQUEST['price']=$rate_row['Rate'];
	
	?>
<tr bgcolor="#FFFFFF">
<td><?php echo $tt[$i];?><input type="hidden" name="itemCode[<?php echo $i;?>]" id="" value="<?php echo $tt[$i];?>" /></td>
<td><?php echo $item_row['item_name'];?></td>
<td><?php 
$unitName=$s->getField('unitsofmeasure','unitid',$item_row['unit'],'unitname');
echo $unitName;?></td>
<td><input type="text" name="quantity[<?php echo $i;?>]" id="quantity_<?php echo $i;?>" value="" /></td>
<td><input type="text" name="price[<?php echo $i;?>]" id="price_<?php echo $i;?>" value="<?php echo $_REQUEST['price'];?>" /></td>
<td><input type="text" name="pricePer[<?php echo $i;?>]" id="pricePer_<?php echo $i;?>" value="<?php echo $_REQUEST['pricePer'];?>" /></td>
<td><input type="text" name="totalValue[<?php echo $i;?>]" id="totalValue_<?php echo $i;?>" value="" onfocus="calculateTotal(<?php echo $i;?>)" /></td>
<td><input type="text" name="requisitionDate[<?php echo $i;?>]" id="requisitionDate_<?php echo $i;?>" value="<?php echo date('d/m/Y')?>" class="datepicker" /></td>
<td>
<select name="status[<?php echo $i;?>]" style="width: 70px">
<option value="F">Forwarding</option>	
<option value="B">Block</option>
</select></td>
</tr>
<?php 	
} }
?>	<input type="hidden" name="count" value="<?php echo $i;?>" />
</table>
<p align="center"><input type="submit" name="Save" value="Save" /><input type="submit" name="Cancel" value="Cancel"></p>
</form>