<?php $PageSecurity = 4;
global  $db;
include('includes/session.inc');
include('includes/SQL_CommonFunctions.inc');
include('includes/function_lib.php');
//include('includes/stockcalculation.php');
include('includes/stockcalculation.php');
$sitesname = $_REQUEST['site'];
$function_filter[]=$sitesname;
$function_filter[]='';

$function_filter[]=date('Y-m-d');
?>



	
	<?php 
		$subquery='';
        if($_REQUEST['main']!='')
      	  $subquery.=" and CategoryID='".$_REQUEST['main']."'"; 
        if($_REQUEST['sub']!='')
       	 $subquery.=" and SubCategoryID='".$_REQUEST['sub']."'"; 
        if($_REQUEST['srch_name']!='')
        	 $subquery.=" and item_name Like '%".$_REQUEST['srch_name']."%'";
        if($_REQUEST['code']!='')
     		 $subquery.=" and (StockID Like '%".$_REQUEST['code']."%' )";
     	

	
     $sql=DB_query("Select * from item_master where CompanyId='".$_SESSION['ACGCompanyId']."' ".$subquery." ",$db); 	
	?>

	<table width="100%" border="0" cellspacing="3" cellpadding="2" align="center">
	<tr>
  	<th style="text-align:center"><strong>Select</strong></th>
    <th style="text-align:center"><strong>Item Code</strong></th>
	<th style="text-align:center"><strong>Item Name</strong></th>
	<th style="text-align:center"><strong>Unit</strong></th>
	<th style="text-align:center"><strong>Stock Qty</strong></th>
	</tr>
	<?php	$i=0;
	if(DB_num_rows($sql)>0)
	{
		while($myrow=DB_fetch_array($sql))
	  	{ 	
		 $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']);
				$invalue=($op+$in_1+$in_2+$in_3+$in_4+$in_5 + $in_6 + $in_7 + $in_8+$in_9+$in_10);
				$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;
		    	 $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='';
		 $secondaryunit=DB_query("select * from  unitsofmeasure where  unitid='".$myrow['conv_unit']."'   ",$db);
		$numrow1=DB_num_rows($secondaryunit);
		if($numrow1>0)
		{
			               
						   while($unit_row1=DB_fetch_array($secondaryunit))
						   {
						   $data2.="<option value=".$unit_row1['unitid'].">".$unit_row1['unitname']."</option>";
						   }
		 
		 }
         
		 if(!is_null($myrow['conv_factor']))
		  $convac=$myrow['conv_factor'];
		 else
		 $convac='null';	
		
			$details=getMrpRate($myrow['id']);
			$mrp=$details['mrp_rate'];
			$abtment=$details['abtment'];

			$netrate  =  getNetRate($myrow['id'],$_REQUEST['client']);

			  
		
		

		
  ?>
 <tr bgcolor="#CCCCCC">
      <td style="text-align:center"><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 htmlentities($myrow['StockID'], ENT_QUOTES);?>','<?php  echo string_sanitize($myrow['item_name']); ?> ','<?php echo string_sanitize($myrow['item_desc']);?>','<?php echo $myrow['net_rate'];?>','<?php echo $myrow['mrp_rate'];?>','<?php echo $data1; ?>','<?php echo $data2; ?>',<?php echo $total_current; ?>,'<?php echo $convac; ?>','<?php echo $i;?>','<?php echo $myrow['item_exciable'];?>','<?php echo $_REQUEST['action']; ?>','<?php echo $mrp ?>','<?php echo $abtment ?>','<?php echo $netrate; ?>')"/></td>
      <td style="text-align:center"><strong><?php echo $myrow['StockID'];?></strong></td>
	     <td style="text-align:center"><?php echo  $myrow['item_name'];?></td>
     
      <td style="text-align:center"><?php $unit=$s->getField('unitsofmeasure','unitid',$myrow['mst_unitid'],'unitname'); echo $unit;?></td>
      <td style="text-align:center"><?php echo $total_current;?></td>
	 
    </tr>
<?php $i++;} }?>
 </table>
<?php 
	
	
	function getMrpRate($item_id)
	{
			global $db;
		   $query = "SELECT pm.mrp_rate,pm.abtment,pm.basic_rate FROM price_list p,price_list_items pm WHERE p.id=pm.price_list_id AND  '".ConvertSQLDate($_REQUEST['qutationdate'])."' BETWEEN p.from_date AND p.to_date AND pm.item_id='".$item_id."' order by p.id desc limit 1"; 
				 
		   $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['qutationdate'])."' BETWEEN rate_master.effictive_from_date AND rate_master.effictive_to_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['qutationdate'])."' 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;
				}

			}

			 
				

		}

		 
?>




</script>
<?php
function string_sanitize($s) {
    $result = preg_replace("/[^a-zA-Z0-9]+/", "", html_entity_decode($s, ENT_QUOTES));
    return $result;
} 
?>
