<?php $PageSecurity = 4;
global  $db;
include('includes/session.inc');
include('includes/function_lib.php');
include('includes/stockcalculation.php');

if($_REQUEST['loaditem'])
{
	$result  = DB_query("Select * from item_master where item_name like '".$_REQUEST['itemname']."%' ORDER BY  item_name ",$db);
	if(DB_num_rows($result)>0)
	{
		while ($data = DB_fetch_array($result)) 
	    {?>
			<option value="<?php echo $data['item_name']; ?>">
		<?php 
		}
	}
}
if(isset($_REQUEST['subdis']))
{	
	 $store_sql2=DB_query("select * from stockcategory where stockmaincategory ='".$_REQUEST["subgrp"]."'",$db);
	 if(DB_num_rows($store_sql2)>0)
	 { ?>		
		<option value="">Select </option>
		<?php while($sub=DB_fetch_assoc($store_sql2))
		 { ?>
			<option value="<?php echo $sub['id'] ?>"><?php echo $sub['categorydescription'] ?></option>
	<?php } ?>
	
	<?php }
	else
	echo '<option value="">Select </option>' ;
	exit;
} ?>

<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<?php
if($_REQUEST['searchitem']!='') 
{ 
	$filter='';
	if($_REQUEST['maingrp']!='')
    $filter[]=" CategoryID ='".$_REQUEST['maingrp']."'";
		//$query.=
	if($_REQUEST['subgrp']!='')
		$filter[]="SubCategoryID ='".$_REQUEST['subgrp']."'";
		//$query.=" and SubCategoryID ='".$_REQUEST['subgrp']."'";
	if($_REQUEST['itemcode']!='')
		$filter[]="StockID like '%".$_REQUEST['itemcode']."%'";
		//$query.=" and StockID like '%".$_REQUEST['itemcode']."%'";
	if($_REQUEST['itemname']!='')
		$filter[]="item_name like '%".$_REQUEST['itemname']."%'";
		//$query.=" and item_name like '%".$_REQUEST['itemname']."%'";

	$sitesname = $_REQUEST['site'];
	$function_filter[]=$sitesname;
	$function_filter[]=$_REQUEST['store'];;
//$data_date=explode('/',$_REQUEST['date']);
					     // $date55=$data_date[2].'-'.$data_date[1].'-'.$data_date[0];
					     // $date22= strtotime(date("Y-m-d", strtotime($date55)) . " +0 day");
                            // $date1= date("Y-m-d",$date22);
$function_filter[]=FormatDateForSQL($_REQUEST['invoicedate']);


	
	if(!empty($filter))
	$filter1=implode(" and ",$filter);
	else
	$filter1="1=1";	
	
?>
<table width="100%" border="0" id="item_table1" cellspacing="0" cellpadding="3" class="thborder">
	<tr>
		<th width="5%">Select</th>
		<th width="13%">Item Code</th>
		<th>Item Name</th>
		<th width="32%">Item Description</th>
		<th width="9%">UOM</th>
		<th width="18%">Stock Bal</th>
	</tr>
	<?php 
		//echo "Select * from item_master where '".$filter1."'";
		//echo "Select * from item_master where $filter1";
		//exit;
		$sql=DB_query("Select * from item_master where $filter1 order by item_name ",$db); 
		
		$i=0;
		if(DB_num_rows($sql)>0)
		{
			while($myrow=DB_fetch_array($sql))
			{ 
				$unitname=$s->getField('unitsofmeasure','unitid',$myrow['mst_unitid'],'unitname');
				$secunitname=$s->getField('unitsofmeasure','unitid',$myrow['conv_unit'],'unitname');
		
				$parameter=$myrow['id'];	
	
				$op=openingBalance($function_filter,$myrow['id']);
				$in_1=receivedMRN($function_filter,$myrow['id']);
				$out_1=issueSlip($function_filter,$myrow['id']);
				$out_2=issueSiteToSite($function_filter,$myrow['id']);    
				$in_2=receivedSiteToSite($function_filter,$myrow['id']);
				$out_3=issueMatarialReturn($function_filter,$myrow['id']);
				$in_3=receivedReturn($function_filter,$myrow['id']);  
				$out_4=Rgpchallan($function_filter,$myrow['id']);
				$out_5=Nrgp($function_filter,$myrow['id']);
				$in_4=Rgpreturn($function_filter,$myrow['id']);  
				$in_5=FinishedMR($function_filter,$myrow['id']);
				$in_6=MaterialConReceived($function_filter,$myrow['id']);
				$out_6=MaterialConIssue($function_filter,$myrow['id']);
				$in_7=PhysicalStockPlus($function_filter,$myrow['id']);
				$out_7=PhysicalStockMinus($function_filter,$myrow['id']);
				$out_8=dispatchIssue($function_filter,$myrow['id']);
				$out_9=jobWorkChallan57Issue($function_filter,$myrow['id']);
				$in_8=jobWorkChallan57Received($function_filter,$myrow['id']);
				$in_9=directPurchase($function_filter,$myrow['id']);
				$in_10=saleReturn($function_filter,$myrow['id']);

				$out_10=invoice($function_filter,$myrow['id']);
				$in_11=storereceipt($function_filter,$myrow['id']);

				$invalue=($op+$in_1+$in_2+$in_3+$in_4+$in_5 + $in_6 + $in_7 + $in_8+$in_9 + $in_10+$in_11);
				$outvalue=($out_1+$out_2+$out_3+$out_4+$out_5 + $out_6 + $out_7 + $out_8 + $out_9 + $out_10);
				$tot=$invalue-$outvalue;
				$stockallitems=$tot;

			 	$total_current=$tot;
			 	
			 	if($total_current>0) 
	 			{
	 				//$mrp 	 = getMrpRate($myrow['id'],$_REQUEST['client']);
	 				$netrate = getNetRate($myrow['id'],$_REQUEST['client']);
	 				$details=getMrpRate($myrow['id']);
			        $mrp=$details['mrp_rate'];
			        $abtment=$details['abtment'];

	 				
				?>
	 				<tr>
					<td><input type="checkbox" name="row_id[<?php echo $i;?>]" id="row_id_<?php echo $i;?>" value="<?php echo $parameter;?>" 
						onclick="item_to_table(this,'<?php echo $parameter;?>','<?php echo htmlspecialchars($myrow['StockID'], ENT_QUOTES);?>','<?php echo  htmlspecialchars($myrow['item_name'], ENT_QUOTES);?>','<?php echo $unitname ?>','<?php echo $total_current;?>','<?php echo $i;?>',<?php echo $_REQUEST['store'] ?>,'<?php echo $myrow['item_exciable']; ?>','<?php echo $myrow["net_rate"];?>','<?php echo $myrow["conv_factor"]; ?>','<?php echo $total_current; ?>','<?php echo $myrow['id']; ?>','<?php echo $secunitname; ?>','<?php echo $mrp ?>','<?php echo $abtment; ?>','<?php echo $netrate; ?>')"></td>
					<td><strong><?php echo $myrow['StockID'];?></strong></td>
					<td><?php echo  $myrow['item_name'];?></td>
					<td><?php echo $myrow['item_desc'];?></td>
					<td><?php  echo $unitname;?></td>
					<td><?php echo $total_current; ?></td>
			</tr>
					<?php $i++; 
	 			}
	 		} 
	 } else { ?>
	 <tr><td colspan="6">No Records</td></tr>
	 <?php } ?>
 </table>
<?php } ?>

</div>
</div>


<?php 
		function getMrpRate($item_id)
		{
			 
				global $db;
		 
				 $query = "SELECT pm.mrp_rate,pm.abtment FROM price_list p,price_list_items pm WHERE p.id=pm.price_list_id AND  '".ConvertSQLDate($_REQUEST['invoicedate'])."' BETWEEN p.from_date AND p.to_date AND pm.item_id='".$item_id."' order by p.id desc limit 1"; 
				//echo "<br/>";
				
			    $sql=DB_query($query,$db); 
				if(DB_num_rows($sql)>0)
					return DB_fetch_array($sql);
			
			
		}


		function getNetRate($itemid,$client_id)
		{
			 global $db;
			  $query="SELECT rate_master_items.net_rate as net_rate FROM rate_master,rate_master_items WHERE rate_master.id = `rate_master_items`.rate_master_id 	AND rate_master_items.item_id='".$itemid."' AND rate_master.party_id ='".$client_id."' and '".FormatDateForSQL($_REQUEST['invoicedate'])."' BETWEEN rate_master.effictive_to_date AND rate_master.effictive_from_date";
			 
			$result =DB_query($query,$db);
			if(DB_num_rows($result)>0)
			{
				$myrow = DB_fetch_array($result);
				if($myrow['net_rate']=='')
					return 0;
				else
					return $myrow['net_rate'] ;
			}
			else
			{
				  $query="SELECT rate_master_items.net_rate FROM rate_master,rate_master_items WHERE rate_master.id = `rate_master_items`.rate_master_id 	AND rate_master_items.item_id='".$itemid."'   and '".FormatDateForSQL($_REQUEST['invoicedate'])."' BETWEEN rate_master.effictive_to_date AND rate_master.effictive_from_date";
				 
				$result = DB_query($query,$db);
				if(DB_num_rows($result)>0)
				{
					$myrow = DB_fetch_array($result);
					if($myrow['net_rate']=='')
						return 0;
					else
						return $myrow['net_rate'] ;
				}
				else
				{
					return 0;
				}

			}

			/*global $db;
			$query="SELECT net_rate FROM rate_master WHERE ItemId='".$itemid."' AND PartyId='".$client_id."' AND DeActivate=0 and '".FormatDateForSQL($_REQUEST['invoicedate'])."' BETWEEN EffictiveDate_from AND EffictiveDate_to";
			 $sql=DB_query($query,$db); 
				if(DB_num_rows($sql)>0)
				{
					while($myrow=DB_fetch_array($sql))
					{
						if($myrow['net_rate']=='')
							return 0;
						else
							return  $myrow['net_rate'];
					}
				}
				else
				{
					$query="SELECT net_rate FROM rate_master WHERE ItemId='".$itemid."' AND DeActivate=0 and '".FormatDateForSQL($_REQUEST['invoicedate'])."' BETWEEN EffictiveDate_from AND EffictiveDate_to";
					$sql=DB_query($query,$db); 
			 		while($myrow=DB_fetch_array($sql))
					{
						if($myrow['net_rate']=='')
							return 0;
						else
							return  $myrow['net_rate'];
					}

			 	
				}*/
				

		}

?>


