<script language="javascript" type="text/javascript">
function get_itemsubgroup(str)
{
	if(str=="")
  {
  document.getElementById("sub_id").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("sub_id").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","item_subcategory.php?q="+str,true);
xmlhttp.send();
}
function get_item(str)
{
	if(str=="")
  {
  document.getElementById("itemid").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("itemid").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","Get_itemcode.php?q="+str,true);
xmlhttp.send();
}
</script>
 <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td ><div align="right"><A HREF="javascript:history.go(-1)"> [Go Back]</A> </div></td>
  </tr>
</table><br /><br />
<?php 
if($_REQUEST['Search']!='' && $_REQUEST['rtype']!='')
{
	 print_r($_REQUEST);
	 
	 $item_array='';
	 $item_array=$_REQUEST['item'];
	 echo "<pre>"; echo "ITEMS = ";
	 print_r($item_array); exit;
	 $item_new_array="'".implode("','",$item_array)."'";
	 
	 $Type =$_REQUEST['Type'];
	 $RateType =$_REQUEST['RateType'];
	 $EffictiveDate =$_REQUEST['EffictiveDate'];
	 //$partytype =$_REQUEST['partytype'];
	
	 $main_group=$_REQUEST['main_group'];
	 $sub_group=$_REQUEST['sub_group'];
	
	if($_REQUEST['rtype']=='Party'){
	$PartyId =$_REQUEST['PartyId'];}
	else{
	
 	$ItemId =implode("', '",$_REQUEST['ItemId']);
	//$ItemId="' "
	}
	
    $filter=array();
	if($main_group!='0')
	$filter[]="CategoryID='".$main_group."' ";
	if($sub_group!='0')
	$filter[]="SubCategoryID='".$sub_group."' ";
	
	if($item_array!='' && $filter!='')
	{
		for($cnt=0;$cnt<count($item_array);$cnt++)
		{
			//echo "select * from item_master where StockID ='".$item_array[$cnt]."'";
			$query=DB_query("select * from item_master where StockID in(".$item_new_array.")",$db);
   		}
	}
   else
    if($filter!='' && $item_array=='')
   {
     $filter=implode(" and ",$filter);
	 //echo "select * from item_master where $filter";
	 $query=DB_query("select * from item_master where $filter",$db);
   }
       
	   
	  
 }
?>

<?php
if($_REQUEST['Save'])
{
 	
	for($str=0;$str<$_REQUEST['count_rows'];$str++)
	{
		//echo "rate - ".$_REQUEST['Rate'][$str];
		if($_REQUEST['Rate'][$str]!='')
		{
			$check_sql="select Rate from rate_master where ItemId='".$_REQUEST['ItemId'][$str]."' and PartyId='".$_REQUEST['PartyId']."' and Category='".$_REQUEST['Type']."' and DeActivate='0'";
			$check_res=DB_query($check_sql,$db);
			$check_row=DB_fetch_array($check_res);
			//echo $check_row['Rate'];
			if($check_row['Rate']!=$_REQUEST['Rate'][$str])
			{
			$update_sql="Update rate_master set DeActivate='1' where ItemId='".$_REQUEST['ItemId'][$str]."' and PartyId='".$_REQUEST['PartyId']."' and Category='".$_REQUEST['Type']."' and Rate='".$check_row['Rate']."'";
			$update_sql=DB_query($update_sql,$db);
			
			$insert_new="Insert into rate_master set
						id='', 
						ItemId='".$_REQUEST['ItemId'][$str]."',  
						PartyId='".$_REQUEST['PartyId']."',   
						Category='".$_REQUEST['Type']."',      
						Rate='".$_REQUEST['Rate'][$str]."',    
						Branch_id='".$_SESSION['UserStockLocation']."',    
						EffictiveDate='".FormatDateForSQL($_REQUEST['EffictiveDate'])."' ";
			$insert_new_result=DB_query($insert_new,$db);
			}
			
		}
		else
		{
			$rate_insert="Insert into rate_master set
						id='', 
						ItemId='".$_REQUEST['ItemId'][$str]."',  
						PartyId='".$_REQUEST['PartyId']."',   
						Category='".$_REQUEST['Type']."',      
						Rate='".$_REQUEST['Rate'][$str]."',    
						Branch_id='".$_SESSION['UserStockLocation']."',    
						EffictiveDate='".FormatDateForSQL($_REQUEST['EffictiveDate'])."' ";	
			$rate_master_result=DB_query($rate_insert,$db);
		//echo "<pre>";
		//print_r($rate_insert);
		}
	}
 //$s->pageLocation('ratemaster_party.php?rtype='.$_REQUEST['rtype']);
 //exit;
}

if($_REQUEST['Search'])
{
?>
<form action="" method="post">
 <input name="Save" type="submit" value="Save" /> &nbsp; <a href="ratemaster_party.php?rtype=<?php  echo $_REQUEST['rtype']?>">
<input name="Cancel" type="button" value="Cancel" /></a>
<br />
<br />
<table width="60%">
<?php 
$vendor_sql=DB_query("Select vendor_name from suppliers where vendor_code='".$PartyId."'",$db);
$vendor=DB_fetch_array($vendor_sql);
?>
 <tr>
  <th colspan="4"><strong><h2 align="center"><?php echo $vendor['vendor_name'];?></h2></strong></th>
  </tr>
</table>
 <table width="60%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <th width="19%"><strong>MAIN <br />
      GROUP </strong></th>
	<th width="29%"><strong>SUB GROUP </strong></th>
    <th width="35%"><strong>ITEM</strong></th>
    <th width="17%"><strong>RATE
 
	    <input name="Type" type="hidden"  value="<?php echo $_REQUEST['Type']?>"/>
	    <input name="EffictiveDate" type="hidden" value="<?php echo $_REQUEST['EffictiveDate']?>"/>
	    <input name="RateType" type="hidden" value="<?php echo $_REQUEST['RateType']?>"/>
        <?php if($_REQUEST['rtype']=='Party'){?>
				<input name="PartyId" type="hidden" value="<?php echo $PartyId;?>"/>
				<?php }else {?>
					<input name="ItemId" type="hidden" value="<?php echo $ItemId?>"/>
					<?php } ?>
	</strong></th>
  </tr>
  </table>
  <div style="overflow: auto; height:350px" > 
  <table width="60%" border="0" cellspacing="0" cellpadding="0">
  <?php 
   if(DB_num_rows($query)>0)
   {
   $i=0;
   while($rdata=DB_fetch_array($query)){
	$qrate_master=DB_query("select * from rate_master where ItemId='".$rdata['StockID']."' and PartyId='".$PartyId."' and Category='".$_REQUEST['Type']."' and DeActivate='0'",$db);
  	$datarate_master=DB_fetch_array($qrate_master);
  ?>
  <tr>        
   <td width="28%"><?php echo $rdata['CategoryID']; ?><input name="stockid[<?php echo $i;?>]" type="hidden" value="<?php echo $rdata['CategoryID'];?>" readonly="true" /></td>
    <td width="22%"><?php echo $rdata['SubCategoryID']; ?></td>
	<td width="37%"><?php echo $rdata['StockID']." - ".$rdata['item_name']; ?><input type="hidden" name="ItemId[<?php echo $i;?>]" id="ItemId" value="<?php echo $rdata['StockID'];?>" /></td>
    <td width="13%"><input name="Rate[<?php echo $i;?>]" type="text"  value="<?php  echo $datarate_master['Rate']?>" size="15"<?php echo $rdata['rates'];?>/></td>
  </tr>
   <?php $i++;} ?>
   <input type="hidden" name="count_rows" id="count_rows" value="<?php echo $i;?>" />
   <?php } ?>
   
</table>
</div><br />
</form>
	<?php }else
{
	?>
<form action="" method="post">	<table width="40%" align="center"><tr><td bgcolor="#FFFFFF">
<table width="100%" border="0" cellspacing="1" cellpadding="1" align="center">
  <tr>
    <th width="40%"><strong>VENDOR</strong></th>
    <td>
	<select name="vendor" style="width: 100%">
	<option value="0">Select</option>
	<?php 
		$vendor_sql=DB_query("Select id,vendor_code,vendor_name from suppliers",$db);
		while($vendor_row=DB_fetch_array($vendor_sql))
		{?>
		<option value="<?php echo $vendor_row['id'];?>"><?php echo $vendor_row['vendor_name'];?></option>
		<?php 	
		}?>
	</select>
	</td>
  </tr>
  <tr>
    <th><strong>CLIENT</strong></th>
    <td>
	<select name="client" id="client" style="width: 100%">
	<option value="0">Select</option>
	<?php 
		$client_sql=DB_query("select id,client_code,c_name from clientmaster",$db);
		while($client_row=DB_fetch_array($client_sql))
		{?>
		<option value="<?php echo $client_row['id'];?>"><?php echo $client_row['c_name'];?></option>
		<?php 	
		}?>
	</select>
	</td>
  </tr>
  <tr>
    <th><strong>ITEM MAIN GROUP </strong></th>
    <td><select name="main_group" id="main_group" onchange="get_itemsubgroup(this.value)" style="width: 100%">
	<option value="0">Select</option>
	<?php 
		$main_cat_sql=DB_query("select * from stockmaincategory",$db);
		while($row1=DB_fetch_array($main_cat_sql)){
	?>  
	<option value="<?php echo $row1['stockmaincategoryid'];?>"><?php echo $row1['stockmaincategoryname'];?></option>
	<?php }?>
	</select></td>
  </tr>
  <tr>
    <th><strong>ITEM SUB GROUP </strong></th>
    <td><div  id="sub_id"><select name="sub_group" id="sub_group" onchange="get_item(this.value)" style="width: 100%">
      <option value="0">Select</option>
	  <?php 
		$sub_cat_sql=DB_query("select categoryid,categorydescription from stockcategory",$db);
		while($row2=DB_fetch_array($sub_cat_sql)){
	?>  
	<option value="<?php echo $row2['categoryid'];?>"><?php echo $row2['categorydescription'];?></option>
	<?php }?>
    </select></div></td>
  </tr>
  <tr>
    <th><strong>ITEM</strong></th>
    <td><div id="itemid"><select name="item" id="item" style="width: 100%">
      <option value="0">Select</option>
	  <?php 
		$item_sql=DB_query("select StockID,item_name from item_master",$db);
		while($row3=DB_fetch_array($item_sql)){
	?>  
	<option value="<?php echo $row3['StockID'];?>"><?php echo $row3['StockID']." - ".$row3['item_name'];?></option>
	<?php }?>
    </select></div></td>
  </tr>
</table></td></tr></table>
<p align="center"><input type="submit" name="ShowRate" value="Show Rate" /></p>
</form>	
<br /><br />
<?php 
if($_REQUEST['ShowRate']!='')
{
	$Filter=array();
	
	if($_REQUEST['vendor']!='0')
	$Filter[]="PartyId='".$_REQUEST['vendor']."' and Category='Purchase'";
	if($_REQUEST['client']!='0')
	$Filter[]="PartyId='".$_REQUEST['client']."' and Category='Sales'";
	if($_REQUEST['main_group']!='0')
	$Filter[]="ItemId LIKE '".$_REQUEST['main_group']."%'";
	if($_REQUEST['sub_group']!='0')
	$Filter[]="ItemId LIKE '".$_REQUEST['sub_group']."%'";
	if($_REQUEST['item']!='0')
	$Filter[]="ItemId='".$_REQUEST['item']."'";
	
	if($Filter!='')
	{
		$Filter=implode(" and ",$Filter);
	}
	$get_rate_master=DB_query("Select * from rate_master where $Filter and DeActivate='0'",$db);
	//echo "Select * from rate_master where $Filter and DeActivate='0' order by ItemId";
}
else
{
	$get_rate_master=DB_query("Select * from rate_master",$db);
}

?>	
 <table width="100%" border="0" cellspacing="1" cellpadding="1">
  <tr>
	<th width="10%"><strong>Item Code</strong></th>
    <th width="45%"><strong>Item Name</strong></th>
	<th><strong>Party</strong></th>
    <th width="15%"<strong>Rate</strong></th>
    <th width="10%"<strong>Effective Date</strong></th>
  </tr>

  <?php 
   if(DB_num_rows($get_rate_master)>0)
   {
   $i=0;
   while($getdata=DB_fetch_array($get_rate_master))
   {
 
   	$item_sql=DB_query("Select CategoryID,SubCategoryID,StockID,item_name from item_master where id='".$getdata['ItemId']."' ",$db);
	$item_row=DB_fetch_array($item_sql);
  ?>
  <tr bgcolor="#FFFFFF">        
    <td><?php echo $item_row['StockID']; ?></td>
	<td><?php echo $item_row['item_name']; ?></td>
    <td><?php 
    if($getdata['Category']=='Sales')
	{
		echo $clientId=$s->getField('clientmaster','id',$getdata['PartyId'],'c_name');
	}
	else {
	$vendor_sql1=DB_query("Select vendor_name from suppliers where vendor_code='".$getdata['PartyId']."'",$db);
	while($vendor_row1=DB_fetch_array($vendor_sql1))
	echo $vendor_row1['vendor_name']; }?></td>
	 <td><?php echo $getdata['Rate']; ?>/- <?php if($getdata['DeActivate']=='1')echo "(Old)";?></td>
	 <td><?php echo ConvertSQLDate($getdata['EffictiveDate']); ?></td>
  </tr>
   <?php $i++;} ?>
   <input type="hidden" name="count_rows" id="count_rows" value="<?php echo $i;?>" />
   <?php } else {?>
   <tr>
   <td colspan="4"><strong><h2 align="center"><font color="#FF0000">No record found !</font></h2></strong></td>
   </tr>
<?php } ?>
</table>
	
<?php 	} ?>
<?php
include('includes/footer.inc');
?>