<?php
if(isset($_POST['Save']))
{
	for($i=0;$i<count($_REQUEST['clientid']);$i++)
	{
	$month=explode('-',$_REQUEST['month'][$i]);
	$datafetch=DB_query("select * from sale_budget where client_id='".$_REQUEST['clientid'][$i]."'
	and month='".$month[0]."' and year='".$month[1]."' and fin_year='".$_REQUEST['fin_year'][$i]."'",$db);
	$n= DB_num_rows($datafetch);
	$abs=DB_fetch_array($datafetch);
	if($n>0)
	{
	for($k=0;$k<count($_REQUEST['expected_order']);$k++)
	{
	if($_REQUEST['expected_order'][$i][$k])
	{
	$con="parant_id='".$abs['id']."' and item='".$_REQUEST['item_id'][$i][$k]."'";
	$data1['expected_order']=$_REQUEST['expected_order'][$i][$k];
	$s->RecordEdit('sale_budget_details', $data1,$con);// RECORD UPDATE HERE
	}
	}
	}
	else
	{
	$data['branch']=$_REQUEST['site'];
	$data['client_id']=$_REQUEST['clientid'][$i];
	$data['fin_year']=$_REQUEST['fin_year'][$i];
	$data['month']=$month[0];
	$data['year']=$month[1];
	if($_REQUEST['month'][$i]!='000')
	{
	$s->RecordInsert('sale_budget', $data);
	$LastId=DB_Last_Insert_ID($db,'salesorder','id');
	}
	for($k=0;$k<count($_REQUEST['expected_order']);$k++)
	{
	if($_REQUEST['expected_order'][$i][$k])
	{
	$data1['parant_id']=$LastId;
	$data1['item']=$_REQUEST['item_id'][$i][$k];
	$data1['expected_order']=$_REQUEST['expected_order'][$i][$k];
	$s->RecordInsert('sale_budget_details', $data1);// RECORD INSERT HERE 
	}
	}
	}
	}
	$s->pageLocation("index.php?route=CRM/sale_order_assignment_view".$rurl);/// PAGE REDIRECTION
}
?>

<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Sale Budget Preparations</h1>
 <form action="" method="post">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
<tr class="row1">
 <td class="tabledata1">Branch:</td>
 <td><select name="site" id="site" style="width:150px;">
<option value="0">---Select---</option>
<?php 
$site="select loccode,locationname from locations where loccode IN ('".$_SESSION['AssignedBranch']."') ";
$result=DB_query($site,$db);		
while($mrow=DB_fetch_array($result))
{?>
<option value="<?php echo $mrow['loccode']?>" <?php if($_REQUEST['site']==$mrow['loccode']) echo "selected";?>><?php echo $mrow['locationname']?></option>
<?php } ?>
</select></td>
	<td class="tabledata1">Range :</td>
	<td>
	<?php 
	$sqlcheck=DB_query("Select * from clientmaster where CompanyId='".$_SESSION['ACGCompanyId']."'   and  Site IN ('".$_SESSION['AssignedBranch']."') and Zone in(".$_SESSION['AssignedSites'].")",$db);
 $numdata=DB_num_rows($sqlcheck);
  $numdata;
 ?>
	<select name="range" style="width:150px;">
 <?php 
 if($numdata>10)
 {
   for($i=10,$j=0;$i<$numdata;$i=$i+20,$j=$j+10)
   
   {  $p='';
   if($i==$_REQUEST['range'])
   $p='selected';
 ?>
 <option  value="<?php echo $i ?>" <?php  echo $p;?>><?php echo $j+1; ?>-<?php echo $j+10; ?></option>
 <?php }
 }
 else
 	{
 	for($i=10,$j=0;$i<10;$i=$i+20,$j=$j+10)
   
   {  $p='';
   if($i==$_REQUEST['range'])
   $p='selected';
 ?>
 <option  value="<?php echo $i ?>" <?php  echo $p;?>><?php echo $j+1; ?>-<?php echo $j+10; ?></option>	
<?php	}
	}
 ?>
 </select> 
 </td>
<tr class="row1">
 <td class="tabledata1">Client </td>
 <td colspan="3"><input type="text" name="search_text" id="search_text" style="width:150px;" /></td>
</tr>
<tr class="row1"> 
<td colspan="4"><p align="center"><input type="submit" name="proceed" value="proceed" class="search"/></p></td>
</tr>
 </table>
 <br>
<form action=""  method="post">
<table width="80%" border="1" cellspacing="0" cellpadding="0" style="border:#000000 solid 1px;" align="center">
  <tr>
    <td class="tabledata">Client Name</td>
    <td class="tabledata">Finacial Year</td>
    <td class="tabledata">Month</td>
	<td class="tabledata">Items</td>
     <td class="tabledata">Expected Order</td>
  </tr>
  <?php
  if(isset($_POST['proceed']))
  {
  if($_REQUEST['search_text']!='')
  	$search_text=trim($_REQUEST['search_text']);
  if($_REQUEST['range']==10)
	  $filter2="limit 0,10";
	  
	 else if($_REQUEST['range']>10)
	  $filter2="limit ".$_REQUEST['range'].",10"; 
  $j=0;
  $FinY=DB_query("SELECT *,YEAR(BookPeriodStart) AS SYear,YEAR(BookPeriodEnd) AS EYEAR FROM companies where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
  $FinYRow=DB_fetch_array($FinY);
    $sql="Select * from clientmaster where CompanyId='".$_SESSION['ACGCompanyId']."' and c_name LIKE '%$search_text%' and  Site IN ('".$_SESSION['AssignedBranch']."') and Zone in(".$_SESSION['AssignedSites'].")   order by c_name ASC $filter2";
    $result=DB_query($sql,$db);
    while($row=DB_fetch_array($result))
	{	
  ?>  
  <tr>
    <td class="tabledata1"><?php echo $row['c_name']; ?>
    <input type="hidden" name="clientid[]" id="clientid_<?php echo $j; ?>" value="<?php echo $row['id'] ?>" /> </td>
    <td class="tabledata1"><input type="hidden" name="fin_year[]" id="fin_year" value="<?php echo $FinYRow['SYear'].'-'.$FinYRow['EYEAR']; ?>" /><?php echo $FinYRow['SYear'].'-'.$FinYRow['EYEAR']; ?></td>
    <td class="tabledata1"><select name="month[]" id="month_<?php echo $j; ?>" style="width:150px;" onchange="checkdata(this.value,<?php echo $j?>)">
	<option value="000">Select</option>
	<?php 
	$diffdata=($FinYRow['EYEAR']-$FinYRow['SYear'])+1;
	for($i=0;$i<$diffdata;$i++)
	{
	if($i==0) { ?>
	<option value="04-<?php echo $FinYRow['SYear'] ?>"<?php if($_REQUEST['month']=='04'.'-'.$FinYRow['SYear']){?>selected="selected"<?php }?>>April-<?php echo $FinYRow['SYear'] ?></option>
	<option value="05-<?php echo $FinYRow['SYear'] ?>"<?php if($_REQUEST['month']=='05'.'-'.$FinYRow['SYear']){?>selected="selected"<?php }?>>May-<?php echo $FinYRow['SYear'] ?></option>
	<option value="06-<?php echo $FinYRow['SYear'] ?>"<?php if($_REQUEST['month']=='06'.'-'.$FinYRow['SYear']){?>selected="selected"<?php }?>>June-<?php echo $FinYRow['SYear'] ?></option>
	<option value="07-<?php echo $FinYRow['SYear'] ?>"<?php if($_REQUEST['month']=='07'.'-'.$FinYRow['SYear']){?>selected="selected"<?php }?>>July-<?php echo $FinYRow['SYear'] ?></option>
	<option value="08-<?php echo $FinYRow['SYear'] ?>"<?php if($_REQUEST['month']=='08'.'-'.$FinYRow['SYear']){?>selected="selected"<?php }?>>August-<?php echo $FinYRow['SYear'] ?></option>
	<option value="09-<?php echo $FinYRow['SYear'] ?>"<?php if($_REQUEST['month']=='09'.'-'.$FinYRow['SYear']){?>selected="selected"<?php }?>>September-<?php echo $FinYRow['SYear'] ?></option>
	<option value="10-<?php echo $FinYRow['SYear'] ?>"<?php if($_REQUEST['month']=='10'.'-'.$FinYRow['SYear']){?>selected="selected"<?php }?>>Octoober-<?php echo $FinYRow['SYear'] ?></option>
	<option value="11-<?php echo $FinYRow['SYear'] ?>"<?php if($_REQUEST['month']=='11'.'-'.$FinYRow['SYear']){?>selected="selected"<?php }?>>November-<?php echo $FinYRow['SYear'] ?></option>
	<option value="12-<?php echo $FinYRow['SYear'] ?>"<?php if($_REQUEST['month']=='12'.'-'.$FinYRow['SYear']){?>selected="selected"<?php }?>>December-<?php echo $FinYRow['SYear'] ?></option>
	<?php } else {?>
	<option value="01-<?php echo $FinYRow['EYEAR'] ?>"<?php if($_REQUEST['month']=='01'.'-'.$FinYRow['EYEAR']){?>selected="selected"<?php }?>>January-<?php echo $FinYRow['EYEAR'] ?></option>
	<option value="02-<?php echo $FinYRow['EYEAR'] ?>"<?php if($_REQUEST['month']=='02'.'-'.$FinYRow['EYEAR']){?>selected="selected"<?php }?>>February-<?php echo $FinYRow['EYEAR'] ?></option>
	<option value="03-<?php echo $FinYRow['EYEAR'] ?>"<?php if($_REQUEST['month']=='03'.'-'.$FinYRow['EYEAR']){?>selected="selected"<?php }?>>March-<?php echo $FinYRow['EYEAR'] ?></option>
	<?php } } ?>	
	</select></td>
	<td style="width:250px;" class="tabledata1">
	<?php 
	if($row['assigned_items']!='')
	{
	$expl_item=explode(",",$row['assigned_items']);
							for($m=0;$m<count($expl_item);$m++)
							{
							$GetItem=DB_query("select * from item_master where id='".$expl_item[$m]."'",$db);
							$ItemRow=DB_fetch_array($GetItem);
	echo "<span style='background-color:#666666; color:#FFFFFF; display:block; padding:3px; margin:0px;'>".$ItemRow['item_name']."</span>
	<div style='height:1px;'></div>";
echo	"<input type='hidden' name='item_id[$j][$m]' value='".$expl_item[$m]."' />";
	}
	}
	?>
	 </td>
    <td style="width:70px;" class="tabledata1">
	<div id="exp_<?php echo $j; ?>">
	<?php 	
if($row['assigned_items']!='')
	{
for($m=0;$m<count($expl_item);$m++)
							{ 
							echo "<input type='text' name='expected_order[$j][$m]' id='expected_order_".$m."' value='' style='width:100%; margin:0px; padding:2px; background-color:#CCCCCC; border:#000000 solid 1px;'  />
	<div style='height:1px;'></div>";
	 }
	 }else
	 {
	echo "<input type='text' name='expected_order[]' id='expected_order_".$j."'/>";
	 }
	 
	  ?>
	</div>
	</td>
  </tr>
  <?php $j++; }  }?>
  <tr>
  <td colspan="4" style="height:5px;"></td>
  </tr>
</table>
<p align="center"><input type="submit" name="Save" value="Save" /></p>
</form>
</div>
</div>
<script>
function checkdata(str,i)
	{
	//alert(str);
	if (str.length==0)
	{
	document.getElementById("exp_"+i).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)
	{ 
	//alert(xmlhttp.responseText);
	//alert(i);
	document.getElementById("exp_"+i).innerHTML=xmlhttp.responseText;
	}
	}
	var client=document.getElementById("clientid_"+i).value;
	xmlhttp.open("GET","get_sale_budgetdatacheck.php?site="+str+"&client="+client+"&j="+i,true);
	xmlhttp.send();
}	




	
</script>


