<?
session_start();
include("../opendb.php");
include('mysqli_table.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'];
$dto1=implode("-",array_reverse(explode("-",$verdt)));
$brres=mysqli_query($db,"select * from branch where brcode='$brcode'");
$brrow=mysqli_fetch_array($brres);
$brnchnm=$brrow[0];
$sql="select * 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";
//echo $sql;
$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;
$tprice=0;
$mctr=1;
$pageno = 1;
//$pdf = new FPDF();
$pdf = new FPDF('L','mm','A4');
$pdf->AddPage();
$pdf->SetFont('Arial','B',10);
$pdf->SetFont('','B');
//$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];
$pdf->SetFont('','BU');
$pdf->Cell(250,5,'KERALA KHADI & VILLAGE INDUSTRIES BOARD',0,1,'C');
$pdf->SetFont('Arial','',11);
$brnchnm=strtoupper($brnchnm);
$pdf->Cell(250,5,'CENTER NAME :'.$brnchnm,0,1,'C');
$pdf->Cell(250,5,'STOCK VERIFICATION STATEMENT AS ON '.$dto1,0,1,'C');
$pdf->SetFont('Arial','',9);
$pdf->Cell(40,10,'Item','LTR');$pdf->Cell(30,10,'Page No.','LTR');$pdf->Cell(40,10,'Stock as per Ledger','LTR');$pdf->Cell(40,10,'Stock Actually Verified','LTR');
$pdf->Cell(30,10,'Excess','LTR');$pdf->Cell(30,10,'Short','LTR');$pdf->Cell(40,10,'Remarks','LTR');
$pdf->Ln(9);
while($row=mysqli_fetch_array($re))
{
	if ($mline > 25 || ($mgname != $row[0] && $mgname >""))
       {
       	$pdf->Cell(40,9,$mgname,'LTRB',0, 'L');$pdf->Cell(30,9,$mpageno,'LTRB',0, 'L');$pdf->Cell(40,9,number_format($page_tot_1,2,'.',','),'LTRB',0, 'R');
              $pdf->Cell(40,9,number_format($page_tot_2,2,'.',''),'LTRB',0, 'R');$pdf->Cell(30,9,number_format($page_tot_3,2,'.',','),'LTRB',0, 'R');$pdf->Cell(30,9,number_format($page_tot_4,2,'.',','),'LTRB',0, 'R');
		$pdf->Cell(40,9,'','LTRB',0, 'R');
		$pdf->Ln(9);
	     	$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])
            	{
                 	$pdf->Cell(40,9,$mgname.' TOTAL','LTRB',0, 'L');$pdf->Cell(30,9,'','LTRB',0, 'L');$pdf->Cell(40,9,number_format($mgroup_tot_1,2,'.',','),'LTRB',0, 'R');$pdf->Cell(40,9,number_format($mgroup_tot_2,2,'.',','),'LTRB',0, 'R');
                 	$pdf->Cell(30,9,number_format($mgroup_tot_3,2,'.',''),'LTRB',0, 'R');$pdf->Cell(30,9,number_format($mgroup_tot_4,2,'.',''),'LTRB',0, 'R');$pdf->Cell(40,9,'','LTRB',0, 'R');
                  	$pdf->Ln(9);
                 	$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;
$pdf->Cell(40,9,$mgname,'LTRB',0, 'L');$pdf->Cell(30,9,$mpageno,'LTRB',0, 'L');$pdf->Cell(40,9,number_format($page_tot_1,2,'.',','),'LTRB',0, 'R');
$pdf->Cell(40,9,number_format($page_tot_2,2,'.',''),'LTRB',0, 'R');$pdf->Cell(30,9,number_format($page_tot_3,2,'.',','),'LTRB',0, 'R');$pdf->Cell(30,9,number_format($page_tot_4,2,'.',','),'LTRB',0, 'R');
$pdf->Cell(40,9,'','LTRB',0, 'R');
$pdf->Ln(9);
$pdf->Cell(40,9,$mgname.' TOTAL','LTRB',0, 'L');$pdf->Cell(30,9,'','LTRB',0, 'L');$pdf->Cell(40,9,number_format($mgroup_tot_1,2,'.',','),'LTRB',0, 'R');$pdf->Cell(40,9,number_format($mgroup_tot_2,2,'.',','),'LTRB',0, 'R');
$pdf->Cell(30,9,number_format($mgroup_tot_3,2,'.',''),'LTRB',0, 'R');$pdf->Cell(30,9,number_format($mgroup_tot_4,2,'.',''),'LTRB',0, 'R');$pdf->Cell(40,9,'','LTRB',0, 'R');
$pdf->Ln(9);
// VIP Items
$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 like '%VIP%' and (not ledqty = 0 or not curstk = 0) order by gname,itemcode ");
$mgname = "";
$mline = 0;
$mpageno = 1;
$page_tot_1 =0;
$page_tot_2 =0;
$page_tot_3 =0;
$page_tot_4 =0;
$mgroup_tot_1 = 0;
$mgroup_tot_2 = 0;
$mgroup_tot_3 = 0;
$mgroup_tot_4 = 0;
while($row=mysqli_fetch_array($re))
{
	if ($mline > 25 || ($mgname != $row[0] && $mgname >""))
       {
       	$pdf->Cell(40,9,$mgname,'LTRB',0, 'L');$pdf->Cell(30,9,$mpageno,'LTRB',0, 'L');$pdf->Cell(40,9,number_format($page_tot_1,2,'.',','),'LTRB',0, 'R');
              $pdf->Cell(40,9,number_format($page_tot_2,2,'.',''),'LTRB',0, 'R');$pdf->Cell(30,9,number_format($page_tot_3,2,'.',','),'LTRB',0, 'R');$pdf->Cell(30,9,number_format($page_tot_4,2,'.',','),'LTRB',0, 'R');
		$pdf->Cell(40,9,'','LTRB',0, 'R');
		$pdf->Ln(9);
	     	$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])
            	{
                 	$pdf->Cell(40,9,$mgname.' TOTAL','LTRB',0, 'L');$pdf->Cell(30,9,'','LTRB',0, 'L');$pdf->Cell(40,9,number_format($mgroup_tot_1,2,'.',','),'LTRB',0, 'R');$pdf->Cell(40,9,number_format($mgroup_tot_2,2,'.',','),'LTRB',0, 'R');
                 	$pdf->Cell(30,9,number_format($mgroup_tot_3,2,'.',''),'LTRB',0, 'R');$pdf->Cell(30,9,number_format($mgroup_tot_4,2,'.',''),'LTRB',0, 'R');$pdf->Cell(40,9,'','LTRB',0, 'R');
                  	$pdf->Ln(9);
                 	$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;
$pdf->Cell(40,9,$mgname,'LTRB',0, 'L');$pdf->Cell(30,9,$mpageno,'LTRB',0, 'L');$pdf->Cell(40,9,number_format($page_tot_1,2,'.',','),'LTRB',0, 'R');
$pdf->Cell(40,9,number_format($page_tot_2,2,'.',''),'LTRB',0, 'R');$pdf->Cell(30,9,number_format($page_tot_3,2,'.',','),'LTRB',0, 'R');$pdf->Cell(30,9,number_format($page_tot_4,2,'.',','),'LTRB',0, 'R');
$pdf->Cell(40,9,'','LTRB',0, 'R');
$pdf->Ln(9);
$pdf->Cell(40,9,$mgname.' TOTAL','LTRB',0, 'L');$pdf->Cell(30,9,'','LTRB',0, 'L');$pdf->Cell(40,9,number_format($mgroup_tot_1,2,'.',','),'LTRB',0, 'R');$pdf->Cell(40,9,number_format($mgroup_tot_2,2,'.',','),'LTRB',0, 'R');
$pdf->Cell(30,9,number_format($mgroup_tot_3,2,'.',''),'LTRB',0, 'R');$pdf->Cell(30,9,number_format($mgroup_tot_4,2,'.',''),'LTRB',0, 'R');$pdf->Cell(40,9,'','LTRB',0, 'R');
$pdf->Ln(9);

// VIP End

$pdf->Cell(40,10,'Grand Total','LTRB',0, 'L');$pdf->Cell(30,10,'','LTRB',0, 'R');$pdf->Cell(40,10,$mgr_tot_1,'LTRB',0, 'R');$pdf->Cell(40,10,$mgr_tot_2,'LTRB',0, 'R');$pdf->Cell(30,10,round($mgr_tot_3,2),'LTRB',0, 'R');
$pdf->Cell(30,10,round($mgr_tot_4,2),'LTRB',0, 'R');$pdf->Cell(40,10,'','LTRB',0, 'R');
$pdf->Ln(4);
$pdf->Ln(4);
$pdf->Output();
?>
