<?php 
    session_name('CAKEPHP'); session_start();
    ob_start();
	$PageSecurity = 3;
	include ('includes/session.inc');
	include ('includes/function_lib.php');
	include('includes/fun-php.php');
	include('includes/fun-HRClass.php');
 	global $db; 
	//$_REQUEST=$_SESSION['stock_ledger_print'];
	//echo "<pre>";
//print_r($_REQUEST);

$Compamies=DB_query("select * from companies",$db);
$CompamiesRow=DB_fetch_array($Compamies);
$sql1 = "SELECT * FROM companies where CompanyId='".$_SESSION['ACGCompanyId']."'";
  $sqlres = DB_query($sql1,$db);
  $companyRow = DB_fetch_array($sqlres);
  $s=new db_query(); 
  
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
  <title>Stock Valuation Register</title>

<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;}
.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:4px; width:90%;}
.mt27{margin-top:0px; width:62%;}
.mt28{margin-top:0px; width:100%;}
.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{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:4px; width:90%;}
.mt27{margin-top:0px; width:52%;}
.mt28{margin-top:0px; width:90%;}
.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>

<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>
 </head>
 <body>
<div id="foo">
  <p style="text-align:right;"><input type="button" onclick="javascript:toggle_visibility('foo');" value="Print" /></p>
</div>

<div class="wrapper">
<div class="main">
<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>  
    
      </div>
      <div class="right"><img src="images/eye-logo-blue.png" vspace="10" width="100%" /></div>
 <?php
   $final_closing=array();
     $item=$_SESSION['get_item1'];
     //print_r($item);

     if($item!='')
     {
        $item_string=implode("','",$item);
        $filter2[]="id in('".$item_string."')";
     }

      if($filter!='')
  {
    $filter=implode(" and ",$filter);
  }
   
   if($filter2!='')
  {
    $filter2=implode(" and ",$filter2);
  }
   
   $item2=$_REQUEST['item'];
   $_SESSION['get_item2']=$item2;
   
     $get_maincat=$_SESSION['main_cat'];
     $get_subcat=$_SESSION['sub_cat'];

  if($item2!='')
    {
    $item_string1=implode(",",$item2);
    $item1=explode(",",$item_string1);
    $catid='';
    for($mm1=0;$mm1<count($item);$mm1++)
      {
       $catid[$mm1]=$s->GetFieldValue('item_master','id="'.$item[$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.stockmaincategoryname,A.categorydescription",$db);   
      }
               else if($get_maincat!=0 && $get_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='".$get_maincat."' AND A.id='".$get_subcat."'
                  ORDER BY B.stockmaincategoryname,A.categorydescription
                ",$db);
                 }
                 else if($get_maincat!=0 && $get_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='".$get_maincat."' ORDER BY B.stockmaincategoryname,A.categorydescription
                ",$db);
                 }
                 else if($get_maincat==0 && $get_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='".$get_subcat."' ORDER BY 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.stockmaincategoryname,A.categorydescription
                ",$db);
                }

 while($main_cat_row=DB_fetch_array($main_cat))
 {
  $main_cat_array[]=$main_cat_row['id'];
}

 ?>
</div>

<div class="div_second">
<table width="100%" border="1" cellspacing="0" cellpadding="3">
  <tr>
    <td rowspan="2" align="center" class="f12">Item Code</td>
    <td rowspan="2" align="center" class="f12">Item Name</td>
    <td rowspan="2" align="center" class="f12">HSN Code</td>
    <td rowspan="2" align="center" class="f12">GST(%)</td>
    <td rowspan="2" align="center" class="f12">Unit</td>
    <td colspan="3" align="center" class="f12">Opening Balance</td>
    <td colspan="3" align="center" class="f12">Received</td>
    <td colspan="3" align="center" class="f12">Issued</td>
    <td colspan="3" align="center" class="f12">Closing Balance</td>
    </tr>
  <tr>
    <td align="center" class="f12">Qty</td>
    <td align="center" class="f12">Rate</td>
    <td align="center" class="f12">Value</td>
    <td align="center" class="f12">Qty</td>
    <td align="center" class="f12">Rate</td>
    <td class="f12">Value</td>
    <td align="center" class="f12">Qty</td>
    <td align="center" class="f12">Rate</td>
    <td align="center" class="f12">Value</td>
    <td align="center" class="f12">Qty</td>
    <td align="center" class="f12">Rate</td>
    <td align="center" class="f12">Value</td>
  </tr>
   <?php
    
  
    
     // $sql="select sum(inwardqty) as receiveqty ,sum(inwared_value) as amount
       //   from stock_inward where type!='OPN'";
     // $query=DB_query($sql,$db);
     
    $_SESSION['main_cat_array']=$main_cat_array;

    $filter=$_SESSION['filter'];
    $filter2=$_SESSION['filter2'];

    
     for($rd=0;$rd<count($main_cat_array);$rd++)
     {
         if($filter2!='')
          $sql1="select * from item_master where SubCategoryID='".$main_cat_array[$rd]."' and $filter2 ORDER BY id ASC";

         else
         $sql1="select * from item_master where SubCategoryID='".$main_cat_array[$rd]."' ORDER BY id ASC";
      // echo $sql1;


   
   

   
   $sql2=$sql1;
  $sql=DB_query($sql1,$db);
  $sql2=DB_query($sql2,$db);
   $num_row2=DB_num_rows($sql2);

 
    ?>
  
    
     <?php 
 
   $from=$_SESSION['from_date'];
   $to=$_SESSION['to_date'];
   $store1=$_SESSION['store1'];
  
   
     $print_array[]=$_REQUEST['site'];
     $print_array[]=$_REQUEST['store1'];
     $print_array[]=$from;
   //  echo $from;

     $_SESSION['print_array']=$print_array;
  
     $num=DB_num_rows($sql);

    while($row1=DB_fetch_array($sql))
    {
     // echo $row1['id'];
     //echo "select * from stock_inward where mst_itemid='".$row1['id']."' and type='OP' and $filter";

    //-------------------------------------    Privious Opening     ---------------------------------//
      $ope='';$opeamt='';$operate='';
      $opamt='';$opvalue='';$issueqty='';$issuevalue='';

     $op_data=DB_query("select sum(inwardqty_primary) as inwardqty_primary,sum(inwared_value) as inwared_value from stock_valuation_tbl where mst_itemid='".$row1['id']."' and type='OP' 
      and site='".$_REQUEST['site']."'  and store='".$store1."'  and DATE(trasaction) <= '".$to."'",$db);


     $privious_opening=DB_fetch_array($op_data);
     if(DB_num_rows($op_data)>0)
     {
      $opamt[]=$privious_opening['inwardqty_primary'];
      $opvalue[]=$privious_opening['inwared_value'];
    }
    else
    {
     $op_data1=DB_query("select sum(inwardqty_primary) as inwardqty_primary,sum(inwared_value) as inwared_value from stock_valuation_tbl where mst_itemid='".$row1['id']."' and type='OP' 
      and site='".$_REQUEST['site']."'  and store='".$store1."'  and DATE(trasaction) between '".$to."' and '".$to."'",$db);
     $privious_opening1=DB_fetch_array($op_data1);
       if(DB_num_rows($op_data1)>0)
       {
        $opamt[]=$privious_opening1['inwardqty_primary'];
        $opvalue[]=$privious_opening1['inwared_value'];
      }

    }


 
   /* ------------------- Previous Receive Starts Here----------------------------------- */
   $in_rate_prev=0;
      $priv_rec=DB_query("select * from stock_valuation_tbl where mst_itemid='".$row1['id']."' and type!='OP' and site='".$_REQUEST['site']."'  and store='".$store1."' and inwardqty_primary!='0.00'  and  DATE(trasaction) < '".$from."'",$db);
     while($privious_receive=DB_fetch_array($priv_rec))
     {
        //$opamt[]=$privious_receive['qty'];
        //$opvalue[]=$privious_receive['amt'];
       


      $sql_po_rate_prev="select addmrnqty.mrprate as rate from addmrnqty inner join addmrn on addmrn.id=addmrnqty.issue_id where mst_itemid='".$row1['id']."' and site='".$_REQUEST['site']."' and  MRNDate <= date_format('".$privious_receive['trasaction']."','%Y-%m-%d')  and addmrnqty.batch_no='".$privious_receive['batch_no']."' order by addmrn.id desc limit 1";
         $result_po_rate_prev=DB_query($sql_po_rate_prev,$db);
         $rs_po_rate_prev=DB_fetch_array($result_po_rate_prev);
          if($rs_po_rate_prev['rate']=='')
         {
           $sql_po_rate_op_prev="select rate from opening_stockitemwise where itemcode='".$row1['id']."' and site='".$_REQUEST['site']."' and reveive_date < date_format('".$privious_receive['trasaction']."','%Y-%m-%d') order by id desc limit 1";
           $result_po_rate_op_prev=DB_query($sql_po_rate_op_prev,$db);
           $rs_po_rate_op_prev=DB_fetch_array($result_po_rate_op_prev);
         }
             if($rs_po_rate_prev['rate']!='')
             {
              
               $in_rate_prev=$rs_po_rate_prev['rate'];
             }
             else
             {
             
               $in_rate_prev=$rs_po_rate_op_prev['rate'];
             }
            // $recamt+=$receivingdata['inwardqty_primary']*$in_rate;
              
             $opamt[]=$privious_receive['inwardqty_primary']; 
             $opvalue[]=$privious_receive['inwardqty_primary']*$in_rate_prev;
     }
  /* ------------------- Previous Receive Ends Here----------------------------------- */


   $out_rate_prev=0;
   $priv_iss=DB_query("select * from stock_valuation_tbl where mst_itemid='".$row1['id']."' and site='".$_REQUEST['site']."'  and store='".$store1."' and  DATE(trasaction) < '".$from."' and outwardqty_primary!='0.00' ",$db);
    while($privious_issue=DB_fetch_array($priv_iss))
    {
      if(DB_num_rows($priv_iss)>0)
       { 
             $sql_po_rate_out_prev="select addmrnqty.mrprate as rate from addmrnqty inner join addmrn on addmrn.id=addmrnqty.issue_id where mst_itemid='".$row1['id']."' and site='".$_REQUEST['site']."' and  MRNDate <= date_format('".$privious_issue['trasaction']."','%Y-%m-%d')  and addmrnqty.batch_no='".$privious_issue['batch_no']."' order by addmrn.id desc limit 1";
           $result_po_rate_out_prev=DB_query($sql_po_rate_out_prev,$db);
           $rs_po_rate_out_prev=DB_fetch_array($result_po_rate_out_prev);

           if($rs_po_rate_out_prev['rate']=='')
           {
             $sql_po_rate_out_op_prev="select rate from opening_stockitemwise where itemcode='".$row1['id']."' and site='".$_REQUEST['site']."' and reveive_date < date_format('".$privious_issue['trasaction']."','%Y-%m-%d') order by id desc limit 1";
             $result_po_rate_out_op_prev=DB_query($sql_po_rate_out_op_prev,$db);
             $rs_po_rate_out_op_prev=DB_fetch_array($result_po_rate_out_op_prev);
           }
               if($rs_po_rate_out_prev['rate']!='')
               {
                
                 $out_rate_prev=$rs_po_rate_out_prev['rate'];
               }
               else
               {
          
                 $out_rate_prev=$rs_po_rate_out_op_prev['rate'];
               }
           //$iseamt+=$issuingdata['outwardqty_primary']*$out_rate;   
            $issueqty[]=$privious_issue['outwardqty_primary'];
            $issuevalue[]=$privious_issue['outwardqty_primary']*$out_rate_prev;
       }
   }

      if(!empty($opamt))
    $ope1=array_sum($opamt); else $ope1=0;
    if(!empty($opvalue))  
    $opeamt1=array_sum($opvalue);  
    if($opeamt1==''){$opeamt1=0;}
    if(!empty($issueqty))
    $issue1=array_sum($issueqty); else $issue1=0;
    if(!empty($issuevalue))
    $issue_val1=array_sum($issuevalue); else $issue_val1=0;
 
     //$ope=$opengingdata['inwardqty'];
     //if($ope==''){$ope=0;}
     //$opeamt=$opengingdata['inwared_value'];
     $ope=$ope1-$issue1;
     $opeamt=$opeamt1-$issue_val1;
    
     if($ope!=0)
     $operate=round($opeamt/$ope,2);
     if($operate==''){$operate=0;}

    
   $rec='';$recamt='';$recrate='';$in_rate='';
   //echo "select * from stock_valuation_tbl where mst_itemid='".$row1['id']."'  and type!='OP'      and $filter";

    $receivingdata1=DB_query("select * from stock_valuation_tbl where mst_itemid='".$row1['id']."'  and type!='OP' and inwardqty_primary!='0.00' and $filter",$db);
    while($receivingdata=DB_fetch_array($receivingdata1))
    {
         $rec+=$receivingdata['inwardqty_primary'];

         /* echo $sql_mrn_batch="select issue_id from addmrnqty inner join addmrn where mst_itemid='".$receivingdata['mst_itemid']."' and batch_no='".$receivingdata['batch_no']."' and MRNDate<=date_format('".$receivingdata['trasaction']."','%Y-%m-%d') "; */
        
         $sql_po_rate="select addmrnqty.mrprate as rate from addmrnqty inner join addmrn on addmrn.id=addmrnqty.issue_id where mst_itemid='".$row1['id']."' and site='".$_REQUEST['site']."' and  MRNDate <= date_format('".$receivingdata['trasaction']."','%Y-%m-%d')  and addmrnqty.batch_no='".$receivingdata['batch_no']."' order by addmrn.id desc limit 1";
         $result_po_rate=DB_query($sql_po_rate,$db);
         $rs_po_rate=DB_fetch_array($result_po_rate);
          if($rs_po_rate['rate']=='')
         {
           $sql_po_rate_op="select rate from opening_stockitemwise where itemcode='".$row1['id']."' and site='".$_REQUEST['site']."' and reveive_date <= date_format('".$receivingdata['trasaction']."','%Y-%m-%d') order by id desc limit 1";
           $result_po_rate_op=DB_query($sql_po_rate_op,$db);
           $rs_po_rate_op=DB_fetch_array($result_po_rate_op);
         }
             if($rs_po_rate['rate']!='')
             {
              
               $in_rate=$rs_po_rate['rate'];
             }
             else
             {
             
               $in_rate=$rs_po_rate_op['rate'];
             }
            //echo $in_rate."###".$$receivingdata['inwardqty_primary'];
         $recamt+=$receivingdata['inwardqty_primary']*$in_rate;
    }

     
     //$rec=$receivingdata['qty'];
     if($rec==''){$rec=0;}
     //$recamt=$receivingdata['amt'];
     if($recamt==''){$recamt=0;}
      if($rec!=0)
     $recrate=round($recamt/$rec,2);

     if($recrate==''){$recrate=0;}
//echo "select sum(outwardqty_primary) as qty,sum(outward_value) as amt 
                  // from stock_valuation_tbl where mst_itemid='".$row1['id']."' and inward_rate!=''
                  //  and $filter";
    /*  $issuingdata=DB_fetch_array(DB_query("select  sum(outwardqty_primary) as qty,sum(outward_value) as amt 
                   from stock_valuation_tbl where mst_itemid='".$row1['id']."'    and $filter",$db)); */

      $ise='';$iseamt='';$iserate='';$out_rate='';
     $issuingdata1=DB_query("select * from stock_valuation_tbl where mst_itemid='".$row1['id']."'  and outwardqty_primary!='0.00'  and $filter",$db);
     while($issuingdata=DB_fetch_array($issuingdata1))
     {

       $ise+=$issuingdata['outwardqty_primary'];
      

         $sql_po_rate_out="select addmrnqty.mrprate as rate from addmrnqty inner join addmrn on addmrn.id=addmrnqty.issue_id where mst_itemid='".$row1['id']."' and site='".$_REQUEST['site']."' and  MRNDate <= date_format('".$issuingdata['trasaction']."','%Y-%m-%d')  and addmrnqty.batch_no='".$issuingdata['batch_no']."' order by addmrn.id desc limit 1";
         $result_po_rate_out=DB_query($sql_po_rate_out,$db);
         $rs_po_rate_out=DB_fetch_array($result_po_rate_out);

         if($rs_po_rate_out['rate']=='')
         {
           $sql_po_rate_out_op="select rate from opening_stockitemwise where itemcode='".$row1['id']."' and site='".$_REQUEST['site']."' and reveive_date <= date_format('".$issuingdata['trasaction']."','%Y-%m-%d') order by id desc limit 1";
           $result_po_rate_out_op=DB_query($sql_po_rate_out_op,$db);
           $rs_po_rate_out_op=DB_fetch_array($result_po_rate_out_op);
         }
             if($rs_po_rate_out['rate']!='')
             {
              
               $out_rate=$rs_po_rate_out['rate'];
             }
             else
             {
        
               $out_rate=$rs_po_rate_out_op['rate'];
             }

         //echo $out_rate."###".$issuingdata['outwardqty_primary']."<hr/>";
         $iseamt+=$issuingdata['outwardqty_primary']*$out_rate;

     }
    //echo $recamt."#####".$iseamt;
    
     //$ise=$issuingdata['qty'];
     if($ise==''){$ise=0;}
     //$iseamt=$issuingdata['amt'];
     if($iseamt==''){$iseamt=0;}
      if($ise!=0)
     $iserate=round($iseamt/$ise,2);
 
     if($iserate==''){$iserate=0;}
  $closingqty='';$closingamt='';$closingrate='';
  $closingqty=($ope+$rec)-$ise;
  if($closingqty==''){$closingqty=0;}
  $closingamt=($opeamt+$recamt)-$iseamt;
  if($closingamt==''){$closingamt=0;}
   if($closingqty!=0)
  $closingrate=round($closingamt/$closingqty,2);
  if($closingrate==''){$closingrate=0;}
 if($catname!=$main_cat_array[$rd])
   { 

 
   ?>
  <tr>
  <td colspan="17" align="left" style="text-align:left;"><b><?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')?></b></td>
</tr>
    <?php $catname=$main_cat_array[$rd]; } ?>
  <tr>
    <td><?php echo $itemcode=$s->getField('item_master','id',$row1['id'],'StockID'); ?></td>
    <td><?php echo $s->getField('item_master','id',$row1['id'],'item_name');?></td>
    <td><?php echo $s->getField('item_master','id',$row1['id'],'hsn_code');?></td>
    <td><?php echo $s->getField('item_master','id',$row1['id'],'gst_percentage');?></td>
    <td><?php $unit_id=$s->getField('item_master','id',$row1['id'],'mst_unitid'); echo $s->getField('unitsofmeasure','unitid',$unit_id,'unitname');?></td>
    <td style="text-align:right;"><?php echo number_format((float)$ope,2); ?></td>
    <td style="text-align:right;"><?php echo number_format((float)$operate,2); ?></td>
    <td style="text-align:right;"><?php echo number_format((float)$opeamt,2); ?></td>
    <td style="text-align:right;"><?php echo number_format((float)$rec,2); ?></td>
    <td style="text-align:right;"><?php echo number_format((float)$recrate,2); ?></td>
    <td style="text-align:right;"><?php echo number_format((float)$recamt,2); ?></td>
    <td style="text-align:right;"><?php echo number_format((float)$ise,2); ?></td>
    <td style="text-align:right;"><?php echo number_format((float)$iserate,2); ?></td>
    <td style="text-align:right;"><?php echo number_format((float)$iseamt,2); ?></td>
    <td style="text-align:right;"><?php echo number_format((float)$closingqty,2); ?></td>
    <td style="text-align:right;"><?php echo number_format((float)$closingrate,2); ?></td>
    <td style="text-align:right;"><?php echo number_format((float)$closingamt,2); $final_closing[]=$closingamt; ?></td>
  </tr>

   <?php 
 
     }    }   ?>
     
  <?php //} ?>

    <tr>
 <td align="right" colspan="16"><strong>Closing</strong></td>
 <td align="right"><?php if(!empty($final_closing) && array_sum($final_closing)!='0') { echo round(array_sum($final_closing),2); } else { echo "0.00"; } ?></td>
     </tr>
    
</table>

</div>
</div>

</div>

</body>
</html>
<?php 
$filename="stock_valuation_report";
header("Content-Type: application/xls");    
header("Content-Disposition: attachment; filename=$filename.xls");  
header("Pragma: no-cache"); 
header("Expires: 0");
?>