<?php $PageSecurity = 2;
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');
//include('login_user_store.php');

//echo $main_store;


if(isset($_REQUEST['vendor']))  
 $vendor_state=$s->getField('suppliers','id',$_REQUEST['vendor'],'state');
if(isset($_REQUEST['site']))
 $loc_state=$s->getField('locations','loccode',$_REQUEST['site'],'state');

//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 distinct 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);
}

//echo $get_store_id;
//echo $main_store."<br>";
//echo $get_store_id;

if($main_store!=0 && $get_store_id!='')
{
	$all_store=$main_store.",".$get_store_id;
}
else
{
	$all_store=$main_store;
}


$function_filter[]=$_REQUEST['site'];
$function_filter[]=$all_store;
$function_filter[]=date('Y-m-d');


?>

<link href="css/professional/default.css" rel="stylesheet" type="text/css" />
<form name="form1" action="" method="post">
<div id="ItemListDiv">
<br />
<table width="100%" border="0" style="border:1px solid #333333;" align="right" cellspacing="0" cellpadding="3">
<tr style="height:30px;">
	<td style="border-bottom:1px solid #FF0000;"><strong>Select</strong></td>
	<td style="border-bottom:1px solid #FF0000;"><strong>Item Code</strong></td>
	<td style="border-bottom:1px solid #FF0000;"><strong>Item Name</strong></td>
	<td style="border-bottom:1px solid #FF0000;"><strong>UOM</strong></td>
	<td style="border-bottom:1px solid #FF0000;"><strong>Stock Balance</strong></td>
	
</tr>
<?php $where='';
if($_REQUEST['code']!=''){
    $where[]="  StockID LIKE '%".$_REQUEST['code']."%'";
}
if($_REQUEST['name']!=''){
    $where[]=" item_name LIKE '%".htmlspecialchars($_REQUEST['name'])."%'";
}
if($_REQUEST['cat']!=''){
    $where[]=" CategoryID='".$_REQUEST['cat']."'";
}
if($_REQUEST['subcat']!=''){
    $where[]=" SubCategoryID='".$_REQUEST['subcat']."'";
}
if(!empty($where))
$filter2=implode(" and ", $where);
if(!empty($where))
{ 

   
	//$sql=DB_query("SELECT  * FROM item_master WHERE $filter2",$db); 
	/* if($get_store_id!='')
	{
	  $sql=DB_query("SELECT  * FROM item_master WHERE $filter2 and id IN(SELECT itemcode FROM opening_stockitemwise WHERE store IN(".$main_store.",".$get_store_id.")) limit 10",$db); 
	}
	else if($main_store!='')
	{
		$sql=DB_query("SELECT  * FROM item_master WHERE $filter2 and id IN(SELECT itemcode FROM opening_stockitemwise WHERE store IN(".$main_store.")) limit 10",$db); 
	}  
	else
	{ */
		$sql=DB_query("SELECT  * FROM item_master WHERE $filter2 limit 10",$db);
	//}
// echo "SELECT id,StockID,item_name,mst_unitid,item_no FROM item_master WHERE $filter2";
// 	 "SELECT id,StockID,item_name,mst_unitid,item_no FROM item_master WHERE $filter2 and id IN(SELECT itemcode FROM opening_stockitemwise WHERE store IN(SELECT hmis_storeid FROM www_users WHERE id='".$_SESSION['UserID']."'))
//  OR  
// id IN(SELECT itemcode FROM opening_stockitemwise WHERE store IN(SELECT store_id FROM www_users_dtls WHERE user_id='".$_SESSION['UserID']."'))";

//  $sql=DB_query("SELECT id,StockID,item_name,mst_unitid,item_no FROM item_master WHERE $filter2 and id IN(SELECT itemcode FROM opening_stockitemwise WHERE store IN(SELECT hmis_storeid FROM www_users WHERE id='".$_SESSION['UserID']."'))
//  OR  
// id IN(SELECT itemcode FROM opening_stockitemwise WHERE store IN(SELECT store_id FROM www_users_dtls WHERE user_id='".$_SESSION['UserID']."'))",$db); 
$i=0;
if(DB_num_rows($sql)>0)
{
	while($myrow=DB_fetch_array($sql))
	{ 
		$purchase_vat='';
		$$purchase_cst='';
		$parameter=$myrow['id'];
		$unitname= $s->GetFieldValue('unitsofmeasure',"unitid='".$myrow['mst_unitid']."'",'unitname');
		        $op=openingBalancepr($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']);
				$out_11=drugissue($function_filter,$myrow['id']);

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

			 	$total_current=$tot;
			 	$vatid='';
			 	 $purchase_vat=$s->GetFieldValue('item_master',"id='".$myrow['id']."'",'vat_purchase');
	             $purchase_cst=$s->GetFieldValue('item_master',"id='".$myrow['id']."'",'cst_purchase');
	             if($vendor_state==$loc_state)
	             {
	              $vatid=$purchase_vat;
	             }
	             else
	             {
	              $vatid=$purchase_cst;
	             }
	               $taxdtls='';
	             
	               $Taxsql="SELECT salestax_id,tax_code, taxrate,surcharge_rate FROM sales_tax_master where salestax_id='".$vatid."'";
                   $Taxresult1=DB_query($Taxsql,$db);
                   if(DB_num_rows($Taxresult1)>0)
                   {
                   $rowdtls=DB_fetch_array($Taxresult1);
                   $vatdtlsid=$rowdtls['salestax_id'];
                   $vatdesc=$rowdtls['taxrate'];
                   $surcharge_rate=$rowdtls['surcharge_rate'];
                   }

			 	

		
?>
<?php //if($total_current>0) {?>
<tr bgcolor="#c1c1c1">
	<td><input type="checkbox" name="row_id[<?php echo $i;?>]" id="row_id_<?php echo $i;?>" value="<?php echo $parameter;?>" onclick="getItemsList('<?php echo $parameter;?>','<?php echo htmlspecialchars($myrow['StockID'],ENT_QUOTES);?>','<?php echo  htmlspecialchars($myrow['item_name'], ENT_QUOTES);?>','<?php echo $total_current;?>','<?php echo $unitname; ?>','<?php echo $i;?>','<?php echo $vatdesc; ?>','<?php echo $vatdtlsid; ?>','<?php echo $surcharge_rate; ?>')"/></td>
	<td><strong><?php echo $myrow['StockID'];?></strong></td>
	<td><?php echo  $myrow['item_name'];?></td>
	<td><?php echo $unitname;?></td>
	<td><?php echo $total_current;?></td>
	
</tr>
<?php //} ?>
	<?php $i++; } } }?>
 </table>
 </div>
</form>
