<?
session_start();
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='inde.php'; </script>";
 return;
}
include_once('testtot.php');
$verdt=$_REQUEST['dfrom'];

$brres=mysqli_query($db,"select * from branch where brcode='$brcode'");
$brrow=mysqli_fetch_array($brres);
$i=1;
$tprice=0;
$mctr=1;
$mctr_1=1;
$pageno = 1;
$filename ="stkver_abs.xls";
$re=mysqli_query($db,"select gname,sum(ledqty*sellprice),sum(curstk*sellprice),sum(stock_short*sellprice),sum(stock_excess*sellprice),count(itemcode) from stockverification where stockverifydt='$verdt' and brcode='$brcode' group by gname");
$contents.="Item \t Stock as per Ledger \t Stock actually verified \t Excess \t Short \t Remarks \n";
$i=1;
$tot_led = 0;
$tot_act = 0;
$tot_exc = 0;
$tot_sho = 0;
$ctr_khadi = 1;
$ctr_vip = 1;
$mstr_vip[]="";
$mstr_khadi[]="";
$mkhadi_tot_1 = 0;
$mkhadi_tot_2 = 0;
$mkhadi_tot_3 = 0;
$mkhadi_tot_4 = 0;
$mvip_tot_1 = 0;
$mvip_tot_2 = 0;
$mvip_tot_3 = 0;
$mvip_tot_4 = 0;

while($row=mysqli_fetch_array($re))
{

       $mtot_pages = ceil($row[5]/25);
      
       if(strpos($row[0], "VIP")>0)
       { 
            $mstr_vip[$ctr_vip]="   \t    \t  \t \t      \t\t \n";
           
            $mstr_vip[$ctr_vip] .=" ".$row[0]."(".$mtot_pages." PAGES) \t ".number_format($row[1],2)."   \t     ".number_format($row[2],2)."      \t ".number_format($row[4],2)." \t ".number_format($row[3],2)."\t\t \n";
            /* */
            $ctr_vip++;
            $mvip_tot_1 += $row[1];
            $mvip_tot_2 += $row[2];
            $mvip_tot_3 += $row[4];
            $mvip_tot_4 += $row[3];
            
       }
       
  
       else
       {
            $mstr_khadi[$ctr_khadi]="   \t    \t \t       \t\t \n";
          
           $mstr_khadi[$ctr_khadi] .=" ".$row[0]."(".$mtot_pages." PAGES) \t ".number_format($row[1],2)."   \t     ".number_format($row[2],2)."      \t ".number_format($row[4],2)." \t ".number_format($row[3],2)."\t\t \n";
           
            $ctr_khadi++;
            $mkhadi_tot_1 += $row[1];
            $mkhadi_tot_2 += $row[2];
            $mkhadi_tot_3 += $row[4];
            $mkhadi_tot_4 += $row[3];

       }
      
       $tot_led += $row[1];
       $tot_act += $row[2];
       $tot_exc += $row[4];
       $tot_sho += $row[3];
       
}


for ($ctr=1;$ctr<$ctr_khadi;$ctr++)
{
    print $mstr_khadi[$ctr];
}
$contents="  KHADI TOTAL \t ".number_format($mkhadi_tot_1,2)."   \t     ".number_format($mkhadi_tot_2,2)."      \t ".number_format($mkhadi_tot_3,2)." \t ".number_format($mkhadi_tot_4,2)." \t \n";
for ($ctr=1;$ctr<$ctr_vip;$ctr++)
{
      print $mstr_vip[$ctr];
}
$contents=" VIP TOTAL \t ".number_format($mkhadi_tot_1,2)."   \t     ".number_format($mkhadi_tot_2,2)."      \t ".number_format($mkhadi_tot_3,2)." \t ".number_format($mkhadi_tot_4,2)."\t \n";
$contents="  GRAND TOTAL \t ".number_format($mkhadi_tot_1+$mvip_tot_1,2)."   \t     ".number_format($mkhadi_tot_2+$mvip_tot_2,2)."      \t ".number_format($mkhadi_tot_3+$mvip_tot_3,2)." \t ".number_format($mkhadi_tot_4+$mvip_tot_4,2)."\t\t \n";
$contents="    Certified that the stock as per the stock list of    \t Certified that the stock as per the stock list of          \t Manager in charge \t\t\t \n";
$contents="   \t ".$brrow[1]."   \t    ".$brrow[1]."       \t ".$brrow[1]." \t\t\t \n";
$contents="   \t has been physically verified in my presence and the   \t    has been physically verified in my presence and the       \t ".$brrow[1]." \t\t\t \n";
$contents="   \t rate, quantity and value noted against each item is correct.   \t    rate, quantity and value noted against each item is correct.      \t ".$brrow[1]." \t\t\t \n";
$contents="   \t Stock Verification Officer   \t    Manager in charge       \t  \t\t\t \n";
$contents="   \t Dist Khadi and Village Industries  \t    ".$brrow[1]."      \t  \t\t\t \n";
$contents = strip_tags($contents);
header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);
print $contents;
?>