<?php $PageSecurity = 2;
global  $db;
include('includes/DefineSuppTransClass.php');
include('includes/DefinePOClass.php');
include('includes/SQL_CommonFunctions.inc');
include('includes/session.inc');
include('includes/function_lib.php');
include('includes/stockcalculation.php');
   $function_filter[0]=$_REQUEST['site'];
    $function_filter[1]=$_REQUEST['store'];
    $function_filter[2]=date('Y-m-d');

    //For User Store
$sql_user_store="select hmis_storeid,StoreCode from www_users left join storetype on storetype.id=www_users.hmis_storeid where www_users.id='".$_SESSION['UserID']."'";
$result_user_store=DB_query($sql_user_store,$db);
$rs_user_store=DB_fetch_array($result_user_store);
$main_store=$rs_user_store[0];


$sql_other_store="select store_id,StoreCode from www_users_dtls left join storetype on storetype.id=www_users_dtls.store_id where user_id='".$_SESSION['UserID']."'";
$result_other_store=DB_query($sql_other_store,$db);
while($rs_other_store=DB_fetch_array($result_other_store))
{
  $store[]=$rs_other_store['store_id'];
}
if(!empty($store))
{
  $get_store_id=implode(",",$store);
}

?>
<link rel="stylesheet" href="css/new_mxerp.css">

<div class="body_contain">
<div class="tab_div">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr class="row1">
	<td width="13%" align="right">Main Category</td>
	<td width="35%"><select id="srch_cateory" onchange="getSubCategory(this.value)">
		<option value="">Select</option>
		<?php
        $item_query="SELECT * FROM stockmaincategory where CompanyId='".$_SESSION['ACGCompanyId']."'";
		$res=DB_query($item_query ,$db);
		while($item_row1=DB_fetch_array($res)){
		$p='';
		if($item_row1['id'] == $_REQUEST['CategoryID'])	
		$p="selected";?>
		 <option value="<?php echo $item_row1['id'];?>"<?php echo $p;?>><?php echo $item_row1['stockmaincategoryname'];?></option>
       <?php }?>
      </select></td>
      <td width="13%" align="right">Sub Category</td>
      <td><select id="srch_subcateory" >
		<option value="">Select</option>
		<?php
        $item_subquery="SELECT * FROM stockcategory where CompanyId='".$_SESSION['ACGCompanyId']."'";
		$subres=DB_query($item_subquery ,$db);
		while($item_subrow=DB_fetch_array($subres)){
		$p='';
		if($item_subrow['id'] == $_REQUEST['CategoryID'])	
		$p="selected";?>
		 <option value="<?php echo $item_subrow['id'];?>"<?php echo $p;?>><?php echo $item_subrow['categorydescription'];?></option>
       <?php }?>
      </select></td>
    </tr>
    <tr class="row1">
   <td align="right">Code</td>
	<td ><input type="text" id="srch_code" placeholder="Item Code"/></td>
  <td align="right">Name</td>
	<td ><input type="text" id="srch_name" placeholder="Item Name"/></td>  
</tr>
<tr class="row1">
	<td colspan="4" style="text-align:center;"><input type="button" value="Search" class="search" onclick="getdata1()" style="font-size:12px;"></td>
</tr>
</table>
<br />
<div id="ItemListDiv">
<form name="form1" action="" method="post">
<table width="100%" border="0" 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 id,StockID,item_name,item_desc,mst_unitid,mrp_rate,net_rate from item_master where CompanyId='".$_SESSION['ACGCompanyId']."'";

$sql=DB_query("SELECT  * FROM item_master WHERE id IN(SELECT itemcode FROM opening_stockitemwise WHERE store='".$_REQUEST['store']."') ORDER BY StockID DESC limit 30",$db); 
$i=0;
if(DB_num_rows($sql)>0)
{
	while($myrow=DB_fetch_array($sql))
	{ 
	
		
		 $parameter=$myrow['id'];
		$unitname= $s->GetFieldValue('unitsofmeasure',"unitid='".$myrow['mst_unitid']."'",'unitname');
	         
	        
	        	$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']);
				$out_11=drugissue($function_filter,$myrow['id']);
				 $in_11=storereceipt($function_filter,$myrow['id']);

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

			 	$total_current=$stockallitems;
		
	
		 $data1='';
			$unit=DB_query("select * from  unitsofmeasure 
		where unitid ='".$myrow['mst_unitid']."' ",$db);
		$numrow=DB_num_rows($unit);
		if($numrow>0)
		{                  
						   while($unit_row=DB_fetch_array($unit))
						   {
						   $data1.="<option value=".$unit_row['unitid'].">".$unit_row['unitname']."</option>";
						   }
		}

 $data2='';
			$unit2=DB_query("select * from  unitsofmeasure 
		where unitid ='".$myrow['conv_unit']."' ",$db);
		$numrow2=DB_num_rows($unit2);
		if($numrow2>0)
		{                  
						   while($unit_row2=DB_fetch_array($unit2))
						   {
						   $data2.="<option value=".$unit_row2['unitid'].">".$unit_row2['unitname']."</option>";
						   }
		}
			



						    $confac=$myrow['conv_factor'];
						   ?>
<tr>
	<td><input type="checkbox" name="row_id[<?php echo $i;?>]" id="row_id_<?php echo $i;?>" value="<?php echo $parameter;?>" 
		onclick="getItemsList(this,'<?php echo $parameter;?>','<?php echo $myrow['StockID'];?>','<?php echo   string_sanitize($myrow['item_name']);?>','<?php echo string_sanitize($myrow['item_name']);?>','<?php echo  $data1;?>','<?php echo  $data2;?>',<?php echo $total_current; ?>,<?php echo $i;?>,'<?php echo $confac; ?>')"/></td>
	<td><?php echo $myrow['StockID'];?></td>
	<td><?php echo  $myrow['item_name'];?></td>
	<td><?php echo $myrow['item_desc'];?></td>
	<td><?php $unit=$s->getField('unitsofmeasure','unitid',$myrow['mst_unitid'],'unitname'); echo $unit;?></td>
	<td><?php echo $total_current; ?></td>
</tr>
	<?php $i++;}} ?>
 </table>
</form>
</div>
<?php
function string_sanitize($s) 
{	
    $result = preg_replace("/[^a-zA-Z0-9]+/", "", html_entity_decode($s, ENT_QUOTES));
    return $result;
} 
?>
</div>
</div>
