<?php error_reporting(0);
session_name('CAKEPHP'); session_start();
//error_reporting(E_ALL ^ E_NOTICE);
$PageSecurity = 3;
 include ('includes/session.inc');
 include ('includes/function_lib.php');
 include('includes/fun-php.php');
 include('includes/fun-HRClass.php');

$_REQUEST=$_SESSION['stock_request'];
//echo "<pre>";
//print_r($_REQUEST);

 ?>
 <script type="text/javascript" language="javascript">
  function toggle_visibility(id) {
       var e = document.getElementById(id);
	      e.style.display = 'none';
		       //alert(e.style.display);
			   window.print();
    }
  </script>
<title>Stock Status Summary Report</title>
<link href="css/purchses.css" rel="stylesheet" type="text/css" />
<style type="text/css"> 
@print {size:A4; margin:20px 20px;}
@media print{
body{ margin:0px; padding:0px; font-size:12px; line-height: 18px; font-family: Arial, Helvetica, sans-serif; color:#000; font-weight:normal;}

h1,h2,h3,h4,h5,p{ margin:0px; padding:0px;}
p{ text-align:left;}

h1{
  font-size: 20px; line-height: 24px;
  font-weight:normal;
  font-family:Arial, Helvetica, sans-serif !important;
  color:#006699;  
}
h2{
  font-size: 18px; line-height: 22px;
  font-weight:normal;
  font-family:Arial, Helvetica, sans-serif !important;
}
thead{display: table-header-group; border-top:#000000 1px solid;}
.wrapper{width:100%; height: auto; float:left; border:#000000 1px solid; border-bottom:#000000 1px solid;}
.div_first, .div_first .full{
  width: 100%; height: auto;
  /*border-bottom: #000000 1px solid;
  border-top: #000000 1px solid;*/
  /*border-left: #000000 1px solid;
  border-right: #000000 1px solid;*/
  float: left; position: relative;
}
 .div_first .left
        {
          width:10%; text-align:left; height:auto; vertical-align:middle; height:auto; padding: 10px; position: absolute; left: 0; top: 0;
        }
        .div_first .middle
        {
          width:60%; text-align:center; padding: 10px; margin:0 auto; position: relative;
        }
        .div_first .right
        {
          width:20%;text-align:right; padding: 10px; position: absolute; right: 0; top: 0;
        }

.div_first .full{text-align: center; border: 0;}
.div_second, .div_third{
  width: 100%;
  height:auto;
  border-top: #000000 1px solid;
  /*border-bottom: #000000 1px solid;*/
  /*border-left: #000000 1px solid;
  border-right: #000000 1px solid;*/
  float: left;
}
.div_second table tr th, .div_second table tr td{border-right:#000000 1px solid; border-bottom:#000000 1px solid;}
.div_second table tr th{text-align: left;}
.div_second table tr th:last-child, .div_second table tr td:last-child{border-right:0;}
.div_second table tr:last-child td{border-bottom:0;}
.div_second table tr td > table tr td{border-right:#000000 1px solid; border-bottom:#000000 1px solid;}
.div_second table tr td > table tr td:last-child{border-right:0;}
.div_second table tr td > table tr:last-child td{border-bottom:0;}

.div_third table tr th, .div_third table tr td{ border-bottom:#000 1px solid; border-right:#000 1px solid;}
.div_third table tr th{background-color:transparent; text-align: left;}
.div_third table tr th:last-child, .div_third table tr td:last-child{border-right:none;}
.div_third table tr:last-child td{border-bottom:none;}

.f12{ font-weight: bold; color:#000; font-size:12px;}
.f11{color:#000; font-size:12px; line-height:18px;}
.f14{color:#000; font-size:18px;}
.mt26{margin-top:14px; width:90%;}
.mt27{margin-top:0px; width:62%;}
.border_top{border-top:#000 1px solid;}
.border_bottom{border-bottom:#000 1px solid;}
.wdt1{width:8%;}
.wdt2{width:100px;}
.wdt3{width: 66px;}
.wdt4{width: 80px;}
.wdt5{width: 52px;}
.wdt6{width: 60px;}
}

@media screen{
/* CSS Document */
body{ margin:0px; padding:0px; background-color:#fff; font-size:12px; line-height: 18px; font-family: Arial, Helvetica, sans-serif; color:#000; font-weight:normal;}
h1,h2,h3,h4,h5,p{ margin:0px; padding:0px;}
p{ text-align:left;}

#foo{width:1070px; height: auto; margin:0px auto;}
.wrapper{width:1070px; height: auto; margin:10px auto; border-top:#000000 1px solid;}

h1{
  font-size: 24px; line-height: 28px;
  font-weight:bold;
  font-family:Arial, Helvetica, sans-serif !important;
  color:#006699;  
}
h2{
  font-size: 18px; line-height: 22px;
  font-weight:normal;
  font-family:Arial, Helvetica, sans-serif !important;  
}
.div_first, .div_first .full{
  width: 100%; height: auto;
  /*border-bottom: #000000 1px solid;
  border-top: #000000 1px solid;*/
  border-left: #000000 1px solid;
  border-right: #000000 1px solid;
  float: left; position: relative; display: table;
}
.div_first .full{text-align: center; border: 0; margin:10px 0 0;}
.div_first .left, .div_first .middle, .div_first .right{float: left; display: table-cell; vertical-align: middle;}
.div_first .left
        {
          width:8%; text-align:center; height:auto; vertical-align:middle; padding: 1%; 
        }
        .div_first .middle
        {
          width:78%; text-align:center; padding: 1%; position: relative; margin:0 auto;
        }
        .div_first .right
        {
          width:8%; text-align:center; padding:1%; 
        }
.div_second, .div_third, .div_forth{
  width: 100%; height: auto;
  /*border-bottom: #000000 1px solid;*/
  border-top: #000000 1px solid;
  border-left: #000000 1px solid;
  border-right: #000000 1px solid;
  float: left;
}
.div_second table tr th, .div_second table tr td{border-right:#000000 1px solid; border-bottom:#000000 1px solid;}
.div_second table tr th{background-color: #ccc; text-align: left;}
.div_second table tr th:last-child, .div_second table tr td:last-child{border-right:0;}
/*.div_second table tr:last-child td{border-bottom:0;}*/
.div_second table tr td > table tr td{border-right:#000000 1px solid; border-bottom:#000000 1px solid;}
.div_second table tr td > table tr td:last-child{border-right:0;}
.div_second table tr td > table tr:last-child td{border-bottom:0;}

.div_third table tr th, .div_third table tr td{ border-bottom:#000 1px solid; border-right:#000 1px solid;}
.div_third table tr th{background-color: #ccc; text-align: left;}
.div_third table tr th:last-child, .div_third table tr td:last-child{border-right:none;}
/*.div_third table tr:last-child td{border-bottom:none;}*/

.f12{ color:#000; font-size:12px; font-weight: bold;}
.f11{ color:#000; font-size:12px; line-height:18px;}
.f14{ color:#000; font-size:18px;}
.mt26{margin-top:4px; width:90%;}
.mt27{margin-top:0px; width:42%;}
.border_top{border-top:#000 1px solid;}
.border_bottom{border-bottom:#000 1px solid;}
.wdt1{width:10%;}
.wdt2{width:180px;}
.wdt3{width: 66px;}
.wdt4{width: 120px;}
.wdt5{width: 52px;}
.wdt6{width: 70px;}
}
</style>
<?php 
$sql1 = "SELECT * FROM companies where CompanyId='".$_SESSION['ACGCompanyId']."'";
$sqlres = DB_query($sql1,$db);
$CompamiesRow = DB_fetch_array($sqlres);
?>
<div class="wrapper">
<div class="div_first">
    <?php $logoImage=($CompamiesRow['CompanyLogo']=='')?'Final_Logo_RM.png':$CompamiesRow['CompanyLogo']; ?>
      <div class="left"><img src="Setup/CompanyLogo/<?php echo $logoImage;  ?>" class="mt26" /></div>
      <div class="middle"><h1>Ayodhya Eye Hospital</h1>
      <span class="f12">A Unit of Mani Mandir Sewa Nyas Samiti</span> <br>
       <span class="f12">Tulsibari, Praikrma Marg, Ayodhya - 224123, Dist. - Faizabad</span>
       <div class="full"><h2>Stock Status Summary Report</h2>
       <span class="f11"><?php echo "<span class='f12'>Phone No.:</span> ".$CompamiesRow['Phone'].", <span class='f12'>Fax No.:</span> ".$CompamiesRow['fax'].", <span class='f12'>Email ID.:</span> ".$CompamiesRow['email'].", <span class='f12'>Website:</span> ".$CompamiesRow['website']; ?><br>
        <?php  echo $_SESSION['mess1']; ?>
        </span></div>
      </div>
      <div class="right"><img src="images/eye-logo-blue.png" vspace="10" width="100%" /></div>
</div>

<?php 
	$from_date=$_REQUEST['from_date'];
	$to_date=$_REQUEST['to_date'];
	$from_date_brk=explode('/',$from_date);
	$from=$from_date_brk[2]."-".$from_date_brk[1]."-".$from_date_brk[0];

				
	$to_date_brk=explode('/',$to_date);
	$to=$to_date_brk[2]."-".$to_date_brk[1]."-".$to_date_brk[0];
				
	$site=$_REQUEST['site'];
	if($_REQUEST['store1']==0)
	{
		 $store12=array();
		 $store_sql = "SELECT id FROM storetype where Store_Site = '".$_REQUEST['site']."'";
		 $store_sql1=DB_query($store_sql,$db);
		 while($st_rows=DB_fetch_array($store_sql1))
		 {
			 $store12[]=$st_rows['id'];
		 }
	
		 $store=implode("','",$store12);
	}
	else
	$store=$_REQUEST['store1'];
	
	if($_REQUEST['maincat']!='0')
		$filter2[]="CategoryID='".$_REQUEST['maincat']."'";
		if($_REQUEST['subcat']!='0')
		$filter2[]="SubCategoryID='".$_REQUEST['subcat']."'";
					
		$item=$_REQUEST['item'];	
	 	//print_r($item);				
		if($item!=''){
			$item_string=implode("','",$item);
			$filter2[]="id in('".$item_string."')";
		}
		if($filter2!='')
		{
			 $filter4=implode(' and ',$filter2);
		}
		
		$filter4;
		$function_filter[]=$site;
		$function_filter[]=$store;	
		$function_filter[]=$from;
		$function_filter[]=$to;
		
	function privious_opening($function_filter,$item)
	{
	 global $db;
	 $receive=array();
	 $total=array();
	  $total1=array();
	 $issueqty=array();	
	 $site=$function_filter[0];
	 $store=$function_filter[1];
	 $fdate1= strtotime(date("Y-m-d", strtotime($function_filter[2])) . " -1 day");
	   $fdate= date("Y-m-d",$fdate1);
	/**************************************  MRN *********************************************/
	
	 
	$sqlmrn=DB_query("select sum(reciveqty) as reciveqty,sum(item_total_rate) as total from addmrnqty 
	inner join addmrn on addmrn.id=addmrnqty.issue_id  where site='".$site."'
	 and StoreCode in ('".$store."')
	  and MRNDate <='".$fdate."' and Cancel_status='N' and reciveqty!='0.00' 
	 and mst_itemid in ('".$item."')  AND addmrn.Approval_status='0'",$db);	
	 
	while($addmrn=DB_fetch_array($sqlmrn))
	{
	if($addmrn['reciveqty']!='')
	{
	$receive[]=$addmrn['reciveqty'];
	$total[]=$addmrn['total'];
	}
	}
		
	   
	/**************************************  Issue *********************************************/

	$sqlissue=DB_query("SELECT sum(issueqty) AS issueqty,sum(totalamt) as total FROM 
                        issueqty INNER JOIN addissue ON addissue.id=issueqty.issue_id where site='".$site."' and
						mst_storeid in ('".$store."')  and issueqty!='0.00' 
						and issuedate <='".$fdate."' and mst_itemid in ('".$item."') ",$db);	
	while($addissue=DB_fetch_array($sqlissue))
	{
	if($addissue['issueqty']!='')
	{
	$issueqty[]=$addissue['issueqty'];
	$total1[]=$addissue['total'];
	}
	}			
		
		
	/*******************************************  Opening ******************************************************/
	$ctd=$function_filter[3];
	if($ctd=='--'){$ctd=date("Y-m-d");}
	$sqlopen=DB_query("select reveive_date as date,itemcode as itemid,stock as reciveqty,amount
	from opening_stockitemwise  where site='".$site."'
	 and store in ('".$store."') and reveive_date <='".$ctd."' 
	  and itemcode in ('".$item."')",$db);
	while($addopen=DB_fetch_array($sqlopen))
	{
	if($addopen['reciveqty']!='')
	{
	$receive[]=$addopen['reciveqty'];	
	$total[]=$addopen['amount'];
	}
	}
	//echo array_sum($receive);
	$qty=array_sum($receive)-array_sum($issueqty);
	$amount=array_sum($total)-array_sum($total1);
	if($qty!=0)	
	$rate1=$amount/$qty;   
	$rate=round($rate1);
	$return_array[]=$qty;
	$return_array[]=$rate;
	$return_array[]=$amount;
	return $return_array;   
	}
	
	
	function openingBalance($function_filter,$item)
	{
		global $db;
		$site=$function_filter[0];
		$store=$function_filter[1];
		$from=$function_filter[2];								
		if($site!='')
		$filter[]="site = '".$site."'";
		if($store!='')
		$filter[]="store in ('".$store."')";
		if($from!='--' && $to!='--')
		$filter[]="reveive_date <= '".$from."'";
		if($item!='')

		$filter[]="itemcode = '".$item."'";
		
		$filter=implode(' and ',$filter);
		$sql1="Select stock, rate,amount from opening_stockitemwise where $filter";
	
		$result1=DB_query($sql1,$db);
		$myrow1=DB_fetch_array($result1);
		$return_array[]=$myrow1['stock'];
		$return_array[]=$myrow1['rate'];
		$return_array[]=$myrow1['amount'];
									
		return $return_array;
	}
	
	function receivedPO($function_filter,$item)
	{
			global $db;
			$site=$function_filter[0];
			$store=$function_filter[1];
			 $from=$function_filter[2];
			$to=$function_filter[3];
			
			if($site!='')
			$filter[]="direct_po.site = '".$site."'";
			if($store!='')
			$filter[]="direct_po.store in ('".$store."')";
			if($from!='--' && $to!='--')
			$filter[]="direct_po.pr_date between '".$from."' and '".$to."'";
			if($item!='')
			$filter[]="direct_po_details.mst_itemid = '".$item."'";
			
			$filter=implode(' and ',$filter);
				$sql1="Select SUM(direct_po_details.qty) AS qty1,sum(rate) as total
				 from direct_po_details left join direct_po 
				   on(direct_po.id=direct_po_details.parent_id) where $filter  AND direct_po.Approval_status='1'";
				 
			$result1=DB_query($sql1,$db);
			$myrow1=DB_fetch_array($result1);
			if($myrow1['qty1']!=0)
			  $rate1=$myrow1['total']/$myrow1['qty1'];
			   $rate=round($rate1,2);
				$return_array[]=$myrow1['qty1'];
				$return_array[]=$rate;

				$return_array[]=$myrow1['total'];
											
				return $return_array;
			
			
	}
                     
	function receivedMRN($function_filter,$item)
	{
			global $db;
			$site=$function_filter[0];
			$store=$function_filter[1];
			 $from=$function_filter[2];
			$to=$function_filter[3];
			
			if($site!='')
			$filter[]="addmrn.site = '".$site."'";
			if($store!='')
			$filter[]="addmrn.StoreCode in ('".$store."')";
			if($from!='--' && $to!='--')
			$filter[]="addmrn.MRNDate between '".$from."' and '".$to."'";
			if($item!='')
			$filter[]="addmrnqty.mst_itemid = '".$item."'";
			
			$filter=implode(' and ',$filter);
				$sql1="Select SUM(addmrnqty.reciveqty),sum(item_total_rate) as total
				 from addmrn left join addmrnqty 
				   on(addmrn.id=addmrnqty.issue_id) where Cancel_Status='N' and reciveqty!='0.00'  and $filter  AND addmrn.Approval_status='1'";
				 
			$result1=DB_query($sql1,$db);
			$myrow1=DB_fetch_array($result1);
				if($myrow1['SUM(addmrnqty.reciveqty)']!=0)
			  $rate1=$myrow1['total']/$myrow1['SUM(addmrnqty.reciveqty)'];
			   $rate=round($rate1,2);
				$return_array[]=$myrow1['SUM(addmrnqty.reciveqty)'];
				$return_array[]=$rate;

				$return_array[]=$myrow1['total'];
											
				return $return_array;
			
			
	}
						

	////1. Issue Slip/////
    function issueSlip($function_filter,$item)
	{
		global $db;
		$site=$function_filter[0];
		$store=$function_filter[1];
		$from=$function_filter[2];
		 $to=$function_filter[3];
		if($site!='')
		$filter[]="addissue.site = '".$site."'";
		if($store!='')
		$filter[]="addissue.mst_storeid in ('".$store."')";
		if($item!='')
		$filter[]="issueqty.mst_itemid = '".$item."'";
		if($from!='--' && $to!='--')
	  $filter[]="addissue.issuedate between '".$from."' and '".$to."'";
		
		$filter=implode(' and ',$filter);
	
			$sql1="Select SUM(issueqty.issueqty),sum(totalamt) as total from addissue left join 
		 issueqty on(addissue.id=issueqty.issue_id) where  issueqty!='0.00' and $filter";
		
		$result1=DB_query($sql1,$db);
		$myrow1=DB_fetch_array($result1);
		
		
		if($myrow1['SUM(issueqty.issueqty)']!=0)
		$rate1=$myrow1['total']/$myrow1['SUM(issueqty.issueqty)'];
		$rate=round($rate1,2);
		$return_array[]=$myrow1['SUM(issueqty.issueqty)'];
		$return_array[]=$rate;
		$return_array[]=$myrow1['total'];
		return $return_array;
	}
	
	function closingquantity($item,$opening_value)
		{			
			global $db;
			$closing_amt_date = str_replace('/', '-', $_REQUEST['to_date']);
			$change_closing_amt_date= date('Y-m-d', strtotime($closing_amt_date));
			//$qty_query=DB_query("select adjustment_type,adjustment_qty from `physical_stock_details` WHERE mstitemid='".$item."' ",$db);
			$qty_query=DB_query("SELECT phy.id,psd.physical_stock_id,psd.mstitemid,psd.adjustment_qty,psd.adjustment_type,phy.date1 FROM physical_stock_details psd,physical_stock phy WHERE psd.physical_stock_id=phy.id AND phy.date1 <='".$change_closing_amt_date."' AND psd.mstitemid='".$item."'",$db);
			while($qty_row1=DB_fetch_array($qty_query))
			{
				if($qty_row1['adjustment_type']=='plus')
				$opening_value+=$qty_row1['adjustment_qty'];
				else if($qty_row1['adjustment_type']=='minus')
				 $opening_value-=$qty_row1['adjustment_qty'];			
			}	
		return $opening_value;
		}
	
function calcPendingQty($item_id,$po_id){
	global $db;
	$sql1="SELECT SUM(qty) as totalqty FROM direct_po_details WHERE mst_itemid=".$item_id." AND parent_id IN (".$po_id.") ";
	$result1=DB_query($sql1,$db);
	$row1=DB_fetch_array($result1);
	$tot_po_qty=$row1['totalqty'];
	//echo $sql;
	$sql2="SELECT SUM(B.reciveqty) as total_qty FROM addmrn A, addmrnqty B 
			WHERE A.id=B.issue_id
			AND B.mst_itemid=".$item_id."
			AND A.PO_WO_MTO IN (".$po_id.")
			AND A.Approval_status=1";
	//echo $sql2;
	$result2=DB_query($sql2,$db);
	$row2=DB_fetch_array($result2);
	$tot_mrn_qty=$row2['total_qty'];
	//echo $tot_po_qty."---".$tot_mrn_qty;
	$grand_tot_qty=$tot_po_qty-$tot_mrn_qty;
	return $grand_tot_qty;
}		
		
?>

 <?php
 $branch=$s->getField('locations','loccode',$_REQUEST['site'],'locationname');
 if($_REQUEST['store1']!='0')
  $storedata="store  ".$s->getField('storetype','id',$_REQUEST['store1'],'StoreName');
  else
  $storedata="ALL Store";
  if($_REQUEST['maincat']!='0' && $_REQUEST['store1']!=='0')
  {
  $sub=$s->getField('stockcategory','id',$_REQUEST['subcat'],'categorydescription');
  $cat=$s->getField('stockmaincategory','id',$_REQUEST['maincat'],'stockmaincategoryname');
  }
  if($_REQUEST['maincat']!='0')
  {
  $message="Showing Records of  ".$storedata." of Branch -- ".$branch."   
  Category -- ".$cat." -- and  SubCategory -- ".$sub." and 
  From  ".$from_date." to ".$to_date."";
  }
  else
  {
   $message="Showing Records of   ".$storedata."  of Branch --  ".$branch."   
   From   ".$from_date." to ".$to_date."";
  }
 
  $_SESSION['mess1']=$message;
   ?>
   <?php //for($k=0; $k<5; $k++){
   
   if($item!=''){
			$item_string=implode("','",$item);
			$filter2[]="id in('".$item_string."')";
			
		}
   
   $item2=$_REQUEST['item'];
	if($item2!=''){
		$item_string1=implode(",",$item2);
		$item1=explode(",",$item_string1);
		$catid='';
		for($mm1=0;$mm1<count($item1);$mm1++){
			$catid[$mm1]=$s->GetFieldValue('item_master','id="'.$item1[$mm1].'"','SubCategoryID');
		}
		 $catid1= array_unique($catid);
		$item_catid=implode("','",$catid1);
		   $main_cat=DB_query("SELECT A.id,A.categorydescription,A.stockmaincategory,B.stockmaincategoryname 
		FROM stockcategory A RIGHT JOIN stockmaincategory B
		ON A.stockmaincategory=B.id WHERE B.CompanyId='".$_SESSION['ACGCompanyId']."' AND A.id IN ('".$item_catid."') ORDER BY B.sequence,A.sequence,B.stockmaincategoryname,A.categorydescription",$db);   
}else if($_REQUEST['maincat']!=0 && $_REQUEST['subcat']!=0){
 $main_cat=DB_query("SELECT A.id,A.categorydescription,A.stockmaincategory,B.stockmaincategoryname 
FROM stockcategory A RIGHT JOIN stockmaincategory B
ON A.stockmaincategory=B.id WHERE B.CompanyId='".$_SESSION['ACGCompanyId']."' AND B.id='".$_REQUEST['maincat']."' AND A.id='".$_REQUEST['subcat']."' ORDER BY B.sequence,A.sequence,B.stockmaincategoryname,A.categorydescription
",$db);
 }else if($_REQUEST['maincat']!=0 && $_REQUEST['subcat']==0){
 $main_cat=DB_query("SELECT A.id,A.categorydescription,A.stockmaincategory,B.stockmaincategoryname 
FROM stockcategory A RIGHT JOIN stockmaincategory B
ON A.stockmaincategory=B.id WHERE B.CompanyId='".$_SESSION['ACGCompanyId']."' AND B.id='".$_REQUEST['maincat']."' ORDER BY B.sequence,A.sequence,B.stockmaincategoryname,A.categorydescription
",$db);
 }else if($_REQUEST['maincat']==0 && $_REQUEST['subcat']!=0){
 $main_cat=DB_query("SELECT A.id,A.categorydescription,A.stockmaincategory,B.stockmaincategoryname 
FROM stockcategory A RIGHT JOIN stockmaincategory B
ON A.stockmaincategory=B.id WHERE B.CompanyId='".$_SESSION['ACGCompanyId']."' AND A.id='".$_REQUEST['subcat']."' ORDER BY B.sequence,A.sequence,B.stockmaincategoryname,A.categorydescription
",$db);
 }else{
   $main_cat=DB_query("SELECT A.id,A.categorydescription,A.stockmaincategory,B.stockmaincategoryname 
FROM stockcategory A RIGHT JOIN stockmaincategory B
ON A.stockmaincategory=B.id WHERE B.CompanyId='".$_SESSION['ACGCompanyId']."' ORDER BY B.sequence,A.sequence,B.stockmaincategoryname,A.categorydescription
",$db);
}
 //$main_cat=DB_query("Select CategoryID from item_master group by CategoryID",$db);
 while($main_cat_row=DB_fetch_array($main_cat)){
	$main_cat_array[]=$main_cat_row['id'];
	//print_r($main_cat_array);
 }
 ?> 
 <div class="div_second">

  <table width="100%" border="0" cellspacing="0" cellpadding="3">
  <!--FOR HEADING-->
  <?php 
	 if($from=="--") { $from=$_SESSION['financialYrStart'];} 
	if($to=="--") { $to=$_SESSION['financialYrEnd']; } 
	
	     /*$query1= "SELECT direct_po.id,direct_po.delivery_date,direct_po_details.qty 
					   FROM direct_po INNER JOIN direct_po_details 
					   ON direct_po.id=direct_po_details.parent_id 
					   WHERE direct_po.Approval_status='1' 
					   AND direct_po.site='".$_REQUEST['site']."' 
					   AND direct_po.mst_potype='1' 
					   AND direct_po.cancel_issue is null 
					   AND direct_po.CompanyId='".$_SESSION['ACGCompanyId']."' 
					   AND direct_po.delivery_date between '".$from."' and '".$to."' 
					   AND direct_po.recive_status='Pending' 
					   GROUP BY direct_po.delivery_date 
					   ORDER BY direct_po.delivery_date ASC";*/
	$query1="SELECT distinct(delivery_date) FROM direct_po
			WHERE Approval_status='1'
			AND site='".$_REQUEST['site']."'
			AND mst_potype='1'
			AND cancel_issue=0
			AND CompanyId='".$_SESSION['ACGCompanyId']."'
			AND delivery_date BETWEEN '".$from."' AND '".$to."'
			AND recive_status='Pending'
			ORDER BY delivery_date ASC";				   
	//echo 	"<br />".$query1;			   
		 $execQuery1=DB_query($query1,$db);
   $tt=DB_num_rows($execQuery1);
   if($tt>5){ $colsp=5;}else{ $colsp=$tt;}			   
   ?>
   
   <tr>
	 <td colspan="5"></td>
	<td colspan="<?php echo $colsp;?>" align="left" style="text-align:left;" class="f12">Tentative Date For Arrival</td>
	<td style="text-align:left;" class="f12">&nbsp;</td>
</tr> 
    <tr>
	<th width="12%" class="f12">Item Code</th>
    <th class="f12">Item Name</th>
	<th width="10%" class="f12">UOM</th>
    <th width="12%" class="f12">Actual Stock</th>
	<!--<th>Actual Stock Boxes</th>-->
   <!-- <th>Intransit Stock</th>-->
	<!--<th>Intransit Stock Boxes</th>-->
    <th width="12%" class="f12">Reserved Stock</th>
	<th width="12%" class="f12">Balance Stock</th>
	 	 <?php 
   $itemarray=array();
   $abc=1;
   while($rowss1=DB_fetch_array($execQuery1)){
		$drlDtae[]=$rowss1['delivery_date'];
		echo '<th style="width:12px;">'.ConvertSQLDate($rowss1['delivery_date']).'</th>';
		$abc++;
		if($abc>5){ break; }
     }
   ?>  
  <th width="12%" class="f12">Total Approx Qty.</th>
  </tr>
<?php
$check='';      
$_SESSION['maincatid']=$main_cat_array; 
for($rd=0;$rd<count($main_cat_array);$rd++){
if($filter2!='')
    $sql1="select * from item_master where SubCategoryID='".$main_cat_array[$rd]."' and $filter4 ORDER BY StockID ASC";
   else
   $sql1="select * from item_master where SubCategoryID='".$main_cat_array[$rd]."' ORDER BY StockID ASC";
   
    $_SESSION['sqlquery_'.$rd]=$sql1;
	//echo "<br />".$sql1;

  $sql=DB_query($sql1,$db);
   $num_row1=DB_num_rows($sql);
   if($num_row1>0)
  {
  $main_cat_array[$rd];
  
?>
 <tr>
	<td colspan="12" align="left" style="text-align:left;" class="f12"><?php echo $s->GetFieldValue('stockmaincategory','id="'.$s->GetFieldValue('stockcategory','id="'.$main_cat_array[$rd].'"','stockmaincategory').'"','stockmaincategoryname');?>&nbsp;<img src="images/orange-right-arrow-button.png" height="20" width="20" />&nbsp;<?php echo $s->GetFieldValue('stockcategory','id="'.$main_cat_array[$rd].'"','categorydescription')?></td>
	
</tr>
<?php		                
 $itemarray=array();
  while($row1=DB_fetch_array($sql))
  {
  
	$privious_open=privious_opening($function_filter,$row1['id']);
	$privious_stock= $privious_open[0];
	   
	$in_1=receivedMRN($function_filter,$row1['id']);
	$po_1=receivedPO($function_filter,$row1['id']);
	$out_1=issueSlip($function_filter,$row1['id']);
    $issueded_qty=$out_1[0];
	
	$actual_stock=$privious_stock+$in_1[0]-$issueded_qty;
	$diffqty=$po_1[0]-$in_1[0];
	//$transsit_stock=$privious_stock+$in_1[0]-$issueded_qty+$diffqty;
     
	 $boxqty=$row1['box_itm_qty'];
  	if(empty($boxqty) || $boxqty==0){$boxqty=1;}
  //echo $boxqty;
  $unit_id=$s->getField('item_master','id',$row1['id'],'mst_unitid');
  $itemname=$s->getField('item_master','id',$row1['id'],'item_name');
   $itemcode=$s->getField('item_master','id',$row1['id'],'StockID');
   ?>
   <!--FOR ITEM LIST AND DATA-->
    <tr>
	
	<td class="f11"><?php echo $itemcode;?></td>
  <td class="f11"><?php echo $itemname;?></td>
  <td class="f11"><?php echo $s->getField('unitsofmeasure','unitid',$unit_id,'unitname');?></td>
   <td class="f11" style="text-align:right;"><?php echo $actual_stock;?></td>
   <!--<td><font size="2"><?php //echo ($actual_stock/$boxqty);?></font></td>-->
 <!--  <td><font size="2"><?php //echo $row25['tot_qty']; ?></font></td>-->
   <!--<td><font size="2"><?php //echo ($transsit_stock/$boxqty);?></font></td>-->
   <td class="f11">&nbsp;</td>
    <td class="f11" style="text-align:right;"><?php echo $actual_stock;  ?></td>
    <?php
	if($from=="--") { $from=$_SESSION['financialYrStart'];} 
	if($to=="--") { $to=$_SESSION['financialYrEnd']; } 
    $itemarray=array();
   //echo "<pre>";
   //print_r($drlDtae);
   $ids_array='';$final_qty='';
    $b=0;
	if(!empty($drlDtae))
	{
   foreach($drlDtae as $key=>$val){ 
  
	 $query3= "SELECT direct_po.id 
	 			FROM direct_po INNER JOIN direct_po_details 
				ON direct_po.id=direct_po_details.parent_id 
				WHERE direct_po.Approval_status='1' 
				AND direct_po.mst_potype='1' 
				AND direct_po.cancel_issue=0
				AND direct_po.CompanyId='".$_SESSION['ACGCompanyId']."' 
				AND direct_po.delivery_date ='".$val."' 
				AND direct_po.recive_status='Pending' 
				AND direct_po_details.mst_itemid='".$row1['id']."'";
  //echo "<br />".$query3;
   $execQuery3=DB_query($query3,$db);
   $ids_array='';$final_qty='';
   while($row3=DB_fetch_array($execQuery3)){
		$ids_array[]=$row3['id'];
	}
	if(!empty($ids_array)){
		$ids_string=implode(',',$ids_array);
		$final_qty=calcPendingQty($row1['id'],$ids_string);
	}else{$final_qty="&nbsp;";}
	?>
	
   <td style="padding:0px;">
   <table width="100%" border="0" cellspacing="0" cellpadding="3">
  <tr>
  <?php $a=0; 
  $a+=$final_qty;
  $b=$b+$a;?>
    <td style="text-align:right;" class="f11"><?php  echo $final_qty;//echo "<br />".$query3;?></td>
  </tr>
</table>
   </td>
    
    <?php    
	
	 } } ?>

     <td style="text-align:right;" class="f11"><?php  echo $b+$actual_stock; ?></td>
</tr>

    <?php   }  }
	
	//}
	
	}
	
	?>
  </table>
  
</div>
  
</div>
