<?
include_once('includes/top.php');
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['verdt'];
echo $verdt;
$dto1=implode("-",array_reverse(explode("-",$verdt)));
$brres=mysqli_query($db,"select * from branch where brcode='$brcode'");
$brrow=mysqli_fetch_array($brres);
?>
<script type="text/javascript" src="number_to_word.js"></script>
<body bgcolor="#0033FF">
<?php //include('includes/top.php'); ?>
<center>
<table>
<TR><TD COLSPAN=2 ALIGN=CENTER><font color='white'><b>   </b></font><BR>
<font color='white'  > <br><u> </u>
</font>
</td></tr>
</TABLE>
<font> <? print "<B>STOCK VERIFICATION STATEMENT AS ON ".$dto1."</B>";?></font>
<link rel="stylesheet" href="style.css" type="text/css" />
<link type="text/css" href="includes/style1.css" rel="stylesheet" />
<link type="text/css" href="css/form.css" rel="stylesheet" />
<style type="text/css">
tr.top td { border-top: thin solid black; }
tr.bottom td { border-bottom: thin solid black; }
tr.row td:first-child { border-left: thin solid black; }
tr.row td:last-child { border-right: thin solid black; }
</style>
<center>
<?

$re=mysqli_query($db,"select gname,ledqty*sellprice,curstk*sellprice,stock_short*sellprice,stock_excess*sellprice from stockverification where stockverifydt='$verdt' and brcode='$brcode' and gname not like '%VIP%' order by gname,itemcode ");
$brres=mysqli_query($db,"select * from branch where brcode='$brcode'");
$brrow=mysqli_fetch_array($brres);
$brnchnm= $brrow[1];

$file =  'stock_ver_con.prn';
$handle = fopen($file, 'w');
$dss=chr(27).chr(71).chr(N);
$dse=chr(27).chr(72);
$initialized = chr(27).chr(64);
$load = chr(27).chr(25).chr(66);
$bold = chr(27).chr(14);
$unbold = chr(20);
$pagelenth=chr(27).chr(67).chr(36);
$Data = $initialized;
$Data = "\n\n\n";
$Data .= "                           KERALA KHADI & VILLAGE INDUSTRIES BOARD  \n";
$Data .= "                           CENTER NAME : ".$brnchnm." \n";
$Data .= "                           STOCK VERIFICATION AS ON $dto1\n";
$Data .= "                                 CONSOLIDATION  \n";

$Data .= chr(15);
$Data.= "|------------------|--------|--------------------|-----------------------|---------------------|--------------------|---------------|\n";
$Data.= "|       Item       |Page No.|Stock as per Ledger |Stock Actually Verified|        Excess       |         Short      |    Remarks    |\n";
$Data.= "|------------------|--------|--------------------|-----------------------|---------------------|--------------------|---------------|\n";

$re=mysqli_query($db,"select gname,ledqty*sellprice,curstk*sellprice,stock_short*sellprice,stock_excess*sellprice from stockverification where stockverifydt='$verdt' and (not ledqty = 0 or not curstk = 0) and brcode='$brcode' and gname not like '%VIP%' order by gname,itemcode");
$i=1;
$tot_led = 0;
$tot_act = 0;
$tot_exc = 0;
$tot_sho = 0;
$mgroup_tot_1 = 0;
$mgroup_tot_2 = 0;
$mgroup_tot_3 = 0;
$mgroup_tot_4 = 0;
$mgr_tot_1 = 0;
$mgr_tot_2 = 0;
$mgr_tot_3 = 0;
$mgr_tot_4 = 0;
$page_tot_1 =0;
$page_tot_2 =0;
$page_tot_3 =0;
$page_tot_4 =0;
$mgname = "";
$mpageno = 1;
$mline = 1;
while($row=mysqli_fetch_array($re))
{
       if ($mline > 25 || ($mgname != $row[0] && $mgname >""))
       {
            $Data .= "|".substr($mgname.str_repeat(" ",18),0,18);
            $Data .= "|".substr(str_repeat(" ",8).$mpageno,-8);
            $mnum = number_format($page_tot_1, 2, '.', '');
            $Data .= "|".substr(str_repeat(" ",20).$mnum,-20);
            $mnum = number_format($page_tot_2, 2, '.', '');
            $Data .= "|".substr(str_repeat(" ",23).$mnum,-23);
            $mnum = number_format($page_tot_3, 2, '.', '');
            $Data .= "|".substr(str_repeat(" ",21).$mnum,-21);
            $mnum = number_format($page_tot_4, 2, '.', '');
            $Data .= "|".substr(str_repeat(" ",20).$mnum,-20)."|               |\n";
            $Data.= "|------------------|--------|--------------------|-----------------------|---------------------|--------------------|---------------|\n";
           //print "\n<tr bgcolor=white onMouseOver=bgColor='silver'; onMouseOut=bgColor='white' style='border:1px solid black;'>";
           // print "<td >$mgname</td><td>$mpageno</td><td align=right>".number_format($page_tot_1,2)."</td><td align=right >".number_format($page_tot_2,2)."</td><td align=right>".number_format($page_tot_3,2)."</td><td align='right'>".number_format($page_tot_4,2)."</td><td></td></tr>";
            $mgroup_tot_1 += $page_tot_1;
            $mgroup_tot_2 += $page_tot_2;
            $mgroup_tot_3 += $page_tot_3;
            $mgroup_tot_4 += $page_tot_4;

            $mline = 1;
            $mpageno++;
            $page_tot_1 =0;
            $page_tot_2 =0;
            $page_tot_3 =0;
            $page_tot_4 =0;
            if ($mgname != $row[0])
            {
                 $Data .= "|".substr($mgname." TOTAL".str_repeat(" ",18),0,18);
                 $Data .= "|".str_repeat(" ",8);
                 $mnum = number_format($mgroup_tot_1, 2, '.', '');
                 $Data .= "|".substr(str_repeat(" ",20).$mnum,-20);
                 $mnum = number_format($mgroup_tot_2, 2, '.', '');
                 $Data .= "|".substr(str_repeat(" ",23).$mnum,-23);
                 $mnum = number_format($mgroup_tot_3, 2, '.', '');
                 $Data .= "|".substr(str_repeat(" ",21).$mnum,-21);
                 $mnum = number_format($mgroup_tot_4, 2, '.', '');
                 $Data .= "|".substr(str_repeat(" ",20).$mnum,-20)."|               |\n";
                 $Data.= "|------------------|--------|--------------------|-----------------------|---------------------|--------------------|---------------|\n";
                 //print "\n<tr bgcolor=white onMouseOver=bgColor='silver'; onMouseOut=bgColor='white' style='border:1px solid black;'>";
                 //print "<td><b>$mgname TOTAL</td><td></td><td align=right><b>".number_format($mgroup_tot_1,2)."</td><td align=right ><b>".number_format($mgroup_tot_2,2)."</td><td align=right><b>".number_format($mgroup_tot_3,2)."</td><td align='right'><b>".number_format($mgroup_tot_4,2)."</td><td></td></tr>";
                 $mgr_tot_1 += $mgroup_tot_1;
                 $mgr_tot_2 += $mgroup_tot_2;
                 $mgr_tot_3 += $mgroup_tot_3;
                 $mgr_tot_4 += $mgroup_tot_4;
                 $mgroup_tot_1 = 0;
                 $mgroup_tot_2 = 0;
                 $mgroup_tot_3 = 0;
                 $mgroup_tot_4 = 0;
                 $mpageno = 1;
            }
       }
       $mgname = $row[0];
       $page_tot_1 += $row[1];
       $page_tot_2 += $row[2];
       $page_tot_3 += $row[4];
       $page_tot_4 += $row[3];
       $mline++;
}
$mgroup_tot_1 += $page_tot_1;
$mgroup_tot_2 += $page_tot_2;
$mgroup_tot_3 += $page_tot_3;
$mgroup_tot_4 += $page_tot_4;
$mgr_tot_1 += $mgroup_tot_1;
$mgr_tot_2 += $mgroup_tot_2;
$mgr_tot_3 += $mgroup_tot_3;
$mgr_tot_4 += $mgroup_tot_4;

$Data .= "|".substr($mgname.str_repeat(" ",18),0,18);
$Data .= "|".substr(str_repeat(" ",8).$mpageno,-8);
$mnum = number_format($page_tot_1, 2, '.', '');
$Data .= "|".substr(str_repeat(" ",20).$mnum,-20);
$mnum = number_format($page_tot_2, 2, '.', '');
$Data .= "|".substr(str_repeat(" ",23).$mnum,-23);
$mnum = number_format($page_tot_3, 2, '.', '');
$Data .= "|".substr(str_repeat(" ",21).$mnum,-21);
$mnum = number_format($page_tot_4, 2, '.', '');
$Data .= "|".substr(str_repeat(" ",20).$mnum,-20)."|               |\n";
$Data.= "|------------------|--------|--------------------|-----------------------|---------------------|--------------------|---------------|\n";

$Data .= "|".substr($mgname." TOTAL".str_repeat(" ",18),0,18);
$Data .= "|".str_repeat(" ",8);
$mnum = number_format($mgroup_tot_1, 2, '.', '');
$Data .= "|".substr(str_repeat(" ",20).$mnum,-20);
$mnum = number_format($mgroup_tot_2, 2, '.', '');
$Data .= "|".substr(str_repeat(" ",23).$mnum,-23);
$mnum = number_format($mgroup_tot_3, 2, '.', '');
$Data .= "|".substr(str_repeat(" ",21).$mnum,-21);
$mnum = number_format($mgroup_tot_4, 2, '.', '');
$Data .= "|".substr(str_repeat(" ",20).$mnum,-20)."|               |\n";
$Data.= "|------------------|--------|--------------------|-----------------------|---------------------|--------------------|---------------|\n";

//print "\n<tr bgcolor=white onMouseOver=bgColor='silver'; onMouseOut=bgColor='white' style='border:1px solid black;'>";
//print "<td >$mgname</td><td>$mpageno</td><td align=right>".number_format($page_tot_1,2)."</td><td align=right >".number_format($page_tot_2,2)."</td><td align=right>".number_format($page_tot_3,2)."</td><td align='right'>".number_format($page_tot_4,2)."</td><td></td></tr>";
//print "\n<tr bgcolor=white onMouseOver=bgColor='silver'; onMouseOut=bgColor='white' style='border:1px solid black;'>";
//print "<td><b>$mgname TOTAL</td><td></td><td align=right><b>".number_format($mgroup_tot_1,2)."</td><td align=right ><b>".number_format($mgroup_tot_2,2)."</td><td align=right><b>".number_format($mgroup_tot_3,2)."</td><td align='right'><b>".number_format($mgroup_tot_4,2)."</td><td></td></tr>";
//********** FOR VIP ITEMS
$re=mysqli_query($db,"select gname,ledqty*sellprice,curstk*sellprice,stock_short*sellprice,stock_excess*sellprice from stockverification where stockverifydt='$verdt' and (not ledqty = 0 or not curstk = 0) and brcode='$brcode' and gname like '%VIP%' order by gname,itemcode");
$i=1;
$tot_led = 0;
$tot_act = 0;
$tot_exc = 0;
$tot_sho = 0;
$mgroup_tot_1 = 0;
$mgroup_tot_2 = 0;
$mgroup_tot_3 = 0;
$mgroup_tot_4 = 0;
$page_tot_1 =0;
$page_tot_2 =0;
$page_tot_3 =0;
$page_tot_4 =0;
$mgname = "";
$mpageno = 1;
$mline = 1;
while($row=mysqli_fetch_array($re))
{
       if ($mline > 25 || ($mgname != $row[0] && $mgname >""))
       {
            //print "\n<tr bgcolor=white onMouseOver=bgColor='silver'; onMouseOut=bgColor='white' style='border:1px solid black;'>";
            //print "<td >$mgname</td><td>$mpageno</td><td align=right>".number_format($page_tot_1,2)."</td><td align=right >".number_format($page_tot_2,2)."</td><td align=right>".number_format($page_tot_3,2)."</td><td align='right'>".number_format($page_tot_4,2)."</td><td></td></tr>";
            $Data .= "|".substr($mgname.str_repeat(" ",18),0,18);
            $Data .= "|".substr(str_repeat(" ",8).$mpageno,-8);
            $mnum = number_format($page_tot_1, 2, '.', '');
            $Data .= "|".substr(str_repeat(" ",20).$mnum,-20);
            $mnum = number_format($page_tot_2, 2, '.', '');
            $Data .= "|".substr(str_repeat(" ",23).$mnum,-23);
            $mnum = number_format($page_tot_3, 2, '.', '');
            $Data .= "|".substr(str_repeat(" ",21).$mnum,-21);
            $mnum = number_format($page_tot_4, 2, '.', '');
            $Data .= "|".substr(str_repeat(" ",20).$mnum,-20)."|               |\n";
            $Data.= "|------------------|--------|--------------------|-----------------------|---------------------|--------------------|---------------|\n";

            $mgroup_tot_1 += $page_tot_1;
            $mgroup_tot_2 += $page_tot_2;
            $mgroup_tot_3 += $page_tot_3;
            $mgroup_tot_4 += $page_tot_4;

            $mline = 1;
            $mpageno++;
            $page_tot_1 =0;
            $page_tot_2 =0;
            $page_tot_3 =0;
            $page_tot_4 =0;
            if ($mgname != $row[0])
            {
//                 print "\n<tr bgcolor=white onMouseOver=bgColor='silver'; onMouseOut=bgColor='white' style='border:1px solid black;'>";
//                 print "<td><b>$mgname TOTAL</td><td></td><td align=right><b>".number_format($mgroup_tot_1,2)."</td><td align=right ><b>".number_format($mgroup_tot_2,2)."</td><td align=right><b>".number_format($mgroup_tot_3,2)."</td><td align='right'><b>".number_format($mgroup_tot_4,2)."</td><td></td></tr>";
              $Data .= "|".substr($mgname." TOTAL".str_repeat(" ",18),0,18);
              $Data .= "|".str_repeat(" ",8);
              $mnum = number_format($mgroup_tot_1, 2, '.', '');
              $Data .= "|".substr(str_repeat(" ",20).$mnum,-20);
              $mnum = number_format($mgroup_tot_2, 2, '.', '');
              $Data .= "|".substr(str_repeat(" ",23).$mnum,-23);
              $mnum = number_format($mgroup_tot_3, 2, '.', '');
              $Data .= "|".substr(str_repeat(" ",21).$mnum,-21);
              $mnum = number_format($mgroup_tot_4, 2, '.', '');
              $Data .= "|".substr(str_repeat(" ",20).$mnum,-20)."|               |\n";
              $Data.= "|------------------|--------|--------------------|-----------------------|---------------------|--------------------|---------------|\n";
                 $mgr_tot_1 += $mgroup_tot_1;
                 $mgr_tot_2 += $mgroup_tot_2;
                 $mgr_tot_3 += $mgroup_tot_3;
                 $mgr_tot_4 += $mgroup_tot_4;
                 $mgroup_tot_1 = 0;
                 $mgroup_tot_2 = 0;
                 $mgroup_tot_3 = 0;
                 $mgroup_tot_4 = 0;
                 $mpageno = 1;
            }
       }
       $mgname = $row[0];
       $page_tot_1 += $row[1];
       $page_tot_2 += $row[2];
       $page_tot_3 += $row[4];
       $page_tot_4 += $row[3];
       $mline++;
}
$mgroup_tot_1 += $page_tot_1;
$mgroup_tot_2 += $page_tot_2;
$mgroup_tot_3 += $page_tot_3;
$mgroup_tot_4 += $page_tot_4;
$mgr_tot_1 += $mgroup_tot_1;
$mgr_tot_2 += $mgroup_tot_2;
$mgr_tot_3 += $mgroup_tot_3;
$mgr_tot_4 += $mgroup_tot_4;
$Data .= "|".substr($mgname.str_repeat(" ",18),0,18);
$Data .= "|".substr(str_repeat(" ",8).$mpageno,-8);
$mnum = number_format($page_tot_1, 2, '.', '');
$Data .= "|".substr(str_repeat(" ",20).$mnum,-20);
$mnum = number_format($page_tot_2, 2, '.', '');
$Data .= "|".substr(str_repeat(" ",23).$mnum,-23);
$mnum = number_format($page_tot_3, 2, '.', '');
$Data .= "|".substr(str_repeat(" ",21).$mnum,-21);
$mnum = number_format($page_tot_4, 2, '.', '');
$Data .= "|".substr(str_repeat(" ",20).$mnum,-20)."|               |\n";
$Data.= "|------------------|--------|--------------------|-----------------------|---------------------|--------------------|---------------|\n";

$Data .= "|".substr($mgname." TOTAL".str_repeat(" ",18),0,18);
$Data .= "|".str_repeat(" ",8);
$mnum = number_format($mgroup_tot_1, 2, '.', '');
$Data .= "|".substr(str_repeat(" ",20).$mnum,-20);
$mnum = number_format($mgroup_tot_2, 2, '.', '');
$Data .= "|".substr(str_repeat(" ",23).$mnum,-23);
$mnum = number_format($mgroup_tot_3, 2, '.', '');
$Data .= "|".substr(str_repeat(" ",21).$mnum,-21);
$mnum = number_format($mgroup_tot_4, 2, '.', '');
$Data .= "|".substr(str_repeat(" ",20).$mnum,-20)."|               |\n";
$Data.= "|------------------|--------|--------------------|-----------------------|---------------------|--------------------|---------------|\n";

//print "\n<tr bgcolor=white onMouseOver=bgColor='silver'; onMouseOut=bgColor='white' style='border:1px solid black;'>";
//print "<td >$mgname</td><td>$mpageno</td><td align=right>".number_format($page_tot_1,2)."</td><td align=right >".number_format($page_tot_2,2)."</td><td align=right>".number_format($page_tot_3,2)."</td><td align='right'>".number_format($page_tot_4,2)."</td><td></td></tr>";
//print "\n<tr bgcolor=white onMouseOver=bgColor='silver'; onMouseOut=bgColor='white' style='border:1px solid black;'>";
//print "<td><b>$mgname TOTAL</td><td></td><td align=right><b>".number_format($mgroup_tot_1,2)."</td><td align=right ><b>".number_format($mgroup_tot_2,2)."</td><td align=right><b>".number_format($mgroup_tot_3,2)."</td><td align='right'><b>".number_format($mgroup_tot_4,2)."</td><td></td></tr>";
$Data .= "|".substr("GRAND TOTAL".str_repeat(" ",18),0,18);
$Data .= "|".str_repeat(" ",8);
$mnum = number_format($mgr_tot_1, 2, '.', '');
$Data .= "|".substr(str_repeat(" ",20).$mnum,-20);
$mnum = number_format($mgr_tot_2, 2, '.', '');
$Data .= "|".substr(str_repeat(" ",23).$mnum,-23);
$mnum = number_format($mgr_tot_3, 2, '.', '');
$Data .= "|".substr(str_repeat(" ",21).$mnum,-21);
$mnum = number_format($mgr_tot_4, 2, '.', '');
$Data .= "|".substr(str_repeat(" ",20).$mnum,-20)."|               |\n";
$Data.= "|------------------|--------|--------------------|-----------------------|---------------------|--------------------|---------------|\n";


//print "\n<tr bgcolor=white onMouseOver=bgColor='silver'; onMouseOut=bgColor='white' style='border:1px solid black;'>";
//print "<td><b>GRAND TOTAL</td><td></td><td align=right><b>".number_format($mgr_tot_1,2)."</td><td align=right ><b>".number_format($mgr_tot_2,2)."</td><td align=right><b>".number_format($mgr_tot_3,2)."</td><td align='right'><b>".number_format($mgr_tot_4,2)."</td><td></td></tr>";
$Data .= "\n\n";
$Data .= "       Stock Verification Officer                                                   Manager in charge \n";
$Data .= "       Dist Khadi and Village Industries                                            $brrow[1] \n";
//print "<tr><td colspan=3>Stock Verification Officer</td><td colspan=3>Manager in charge</td></tr>";
//print "<tr><td colspan=3>Dist Khadi and Village Industries</td><td colspan=3>$brrow[1]</td></tr>";
$Data .= "\n";
$Data .= chr(18);
$Data .= chr(12);
fwrite($handle, $Data);
fclose($handle);
$output = shell_exec("lp -o media=Custom.10x6in '$file'");
$output=shell_exec("C:\\WINDOWS\\system32\\cmd.exe /c stock_ver_con.bat");
?>