<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
	print "<script> alert('Session timed out'); parent.document.location='inde.php'; </script>";
	return;
}
include("../opendb1.php");
include('mysqli_table.php');
$brqry="select brname from branch where brcode='$brcode'";
$brrs=mysqli_query($db,$brqry);
$brrw=mysqli_fetch_array($brrs);
$brnchnm=$brrw[0];
$fromdt=$_REQUEST['fromdt'];
$todt=$_REQUEST['todt'];
$mstr[]='';
	$i=1;
	$mgname = '.';
	$mtot_cot = 0;
	$mtot_pol = 0;
	$mtot_rel = 0;
	$mtot_spu = 0;
	$mtot_vip = 0;
	$mtot_woo = 0;
	$mtot_oth = 0;
	$mtot_total = 0;
	$mtot_mba= 0;
	$mtot_strbtnml = 0;
	$mtot_strbtspl = 0;
	$mtot_redct = 0;
	$mtot_befrtx = 0;
	$mtot_taxamt = 0;
	$mtot_nettot = 0;
	$mtot_dbtrs = 0;
	$mgr_total = 0;
	$mgr_cot = 0;
	$mgr_pol = 0;
	$mgr_rel = 0;
	$mgr_spu = 0;
	$mgr_vip = 0;
	$mgr_woo = 0;
	$mgr_oth = 0;
	$mgr_mba= 0;
	$mgr_strbtnml = 0;
	$mgr_strbtspl = 0;
	$mgr_redct = 0;
	$mgr_befrtx = 0;
	$mgr_taxamt = 0;
	$mgr_nettot = 0;
	$mgr_dbtrs = 0;

	$ctr=0;
        $mgr_dbtrs=0; $mgr_dbtrs1=0;
	$minvno = '';
        //$tbl='sales_summ'.$brcode.$user;
	//mysqli_query($db,"delete from $tbl");
	//mysqli_query($db,"create table $tbl(gname varchar(50),total double(14,2),mba double(14,2),strbtnml double(14,2),strbtspl double(14,2),redct double(14,2),befrtx double(14,2),taxamt double(14,2),nettot double(14,2),dbttot double(14,2))");
	$qry=mysqli_query($db,"select gname,invdate,sum(total),sum(mba),sum(strbtnml),sum(strbtspl),sum(redct),sum(befrtx),sum(taxamt),sum(nettot),invno,pmode from prod_invoice where invdate between '$fromdt' and '$todt' and brcode='$brcode' group by invno,gname");
	
        $mfrom_date = $fromdt;
	$mto_date = $todt;
			
				
						//$pdf = new FPDF();
                                                $pdf = new FPDF('L','mm','A4');
						$pdf->AddPage();
						
						$pdf->SetFont('Arial','B',10);
						$pdf->SetFont('','B');
						$dedres1=mysqli_query($db,"select brname from branch where brcode='$brcode'") or die("er5:".mysqli_error($db));
						$br_1=mysqli_fetch_array($dedres1);

						$pdf->SetFont('','BU');
						$pdf->Cell(260,10,'KERALA KHADI & VILLAGE INDUSTRIES BOARD',0,1,'C');
						$pdf->SetFont('Arial','',11);
						$brnchnm=strtoupper($brnchnm);						
                                                $pdf->Cell(260,10,'CENTER NAME :'.$brnchnm,0,1,'C');
                                                //$pdf->Ln(4);
						//$pdf->Ln(4);
                                                $pdf->Cell(260,10,'SALES SUMMARY REPORT BILLWISE',0,1,'C');
                                                $pdf->SetFont('Arial','',9);
                                                $pdf->Cell(13,10,'BillNo','LTR');$pdf->Cell(20,10,'Cotton','LTR');$pdf->Cell(20,10,'Poly','LTR');$pdf->Cell(21,10,'Reeled Silk','LTR');
                                                $pdf->Cell(20,10,'Spun Silk','LTR');$pdf->Cell(20,10,'VIP','LTR');$pdf->Cell(15,10,'Woollen','LTR');$pdf->Cell(15,10,'Others','LTR');
                                                $pdf->Cell(15,10,'MDA','LTR');$pdf->Cell(15,10,'State Rbt','LTR');$pdf->Cell(15,10,'State Spl','LTR');
                                                $pdf->Cell(15,10,'Reduct','LTR');$pdf->Cell(20,10,'Net Cash','LTR');$pdf->Cell(15,10,'Tax','LTR');$pdf->Cell(22,10,'Total Amount','LTR');
                                                $pdf->Cell(20,10,'Debtors','LTR');
                                                //$pdf->Ln(4);
                                                $pdf->Ln(4);
                                                                $pdf->Ln(4);
                                                while($row=mysqli_fetch_array($qry))
                                                    {
                                                        if($minvno <>  $row[10] && $ctr <> 0)
                                                            {

                                                                $pdf->Cell(13,10,$minvno,'LTR',0, 'R');$pdf->Cell(20,10,number_format($mtot_cot,2),'LTR',0, 'R');$pdf->Cell(20,10,number_format($mtot_pol,2),'LTR',0, 'R');$pdf->Cell(21,10,number_format($mtot_rel,2),'LTR',0, 'R');
                                                                $pdf->Cell(20,10,number_format($mtot_spu,2),'LTR',0, 'R');$pdf->Cell(20,10,number_format($mtot_vip,2),'LTR',0, 'R');$pdf->Cell(15,10,number_format($mtot_woo,2),'LTR',0, 'R');$pdf->Cell(15,10,number_format($mtot_oth,2),'LTR',0, 'R');
                                                                $pdf->Cell(15,10,number_format($mtot_mba,2),'LTR',0, 'R');$pdf->Cell(15,10,number_format($mtot_strbtnml,2),'LTR',0, 'R');$pdf->Cell(15,10,number_format($mtot_strbtspl,2),'LTR',0, 'R');$pdf->Cell(15,10,number_format($mtot_redct,2),'LTR',0, 'R');
                                                                $pdf->Cell(20,10,number_format($mtot_befrtx,2),'LTR',0, 'R');$pdf->Cell(15,10,number_format($mtot_taxamt,2),'LTR',0, 'R');$pdf->Cell(22,10,number_format($mtot_nettot,2),'LTR',0, 'R');
                                                                $pdf->Cell(20,10,number_format($mtot_dbtrs,2),'LTR',0, 'R');
                                                                $pdf->Ln(4);
                                                                $pdf->Ln(4);
                                                            
                                                                $mgr_cot+=$mtot_cot;
                                                                $mgr_pol+=$mtot_pol;
                                                                $mgr_rel +=$mtot_rel;
                                                                $mgr_spu +=$mtot_spu;
                                                                $mgr_vip +=$mtot_vip;
                                                                $mgr_woo +=$mtot_woo;
                                                                $mgr_oth +=$mtot_oth;
                                                                $mgr_mba+=$mtot_mba;
                                                                $mgr_strbtnml +=$mtot_strbtnml;
                                                                $mgr_strbtspl +=$mtot_strbtspl;
                                                                $mgr_redct +=	$mtot_redct;
                                                                $mgr_befrtx +=	$mtot_befrtx;
                                                                $mgr_taxamt +=	$mtot_taxamt;
                                                                $mgr_nettot +=	$mtot_nettot;
                                                                $mgr_dbtrs +=	$mtot_dbtrs;
                                                                $mtot_cot = 0;
                                                                $mtot_pol = 0;
                                                                $mtot_rel = 0;
                                                                $mtot_spu = 0;
                                                                $mtot_vip = 0;
                                                                $mtot_woo = 0;
                                                                $mtot_oth = 0;
                                                                $mtot_mba= 0;
                                                                $mtot_strbtnml = 0;
                                                                $mtot_strbtspl = 0;
                                                                $mtot_redct = 0;
                                                                $mtot_befrtx = 0;
                                                                $mtot_taxamt = 0;
                                                                $mtot_nettot = 0;
                                                                $mtot_dbtrs = 0;
                                                                //$pdf->Ln(4);
                                                            }


                                                        $minvno = $row[10];
                                                        ++$ctr;
                                                        $date = $row[1];
                                                        // *** getting staff discount from (Especially for Bed) 
        $mstaffdisc_amt = 0;
        $mdummy=0;
        $rbt_tbl=mysqli_query($db,"select item_code,qty,staffdisc,total from prod_invoice where brcode = '$brcode' and invno = '$row[10]' and gname = '$row[0]' and staffdisc > 0");
        while($rrow=mysqli_fetch_array($rbt_tbl))
        {
             $rbt_val=mysqli_query($db,"select rbtbl,rbtvl from partmaster where partcode = '$rrow[0]'");
             $row22=mysqli_fetch_row($rbt_val);                
             if($row22[0]=='Yes')
                  $mstaffdisc_amt += ($row22[1]*$rrow[1])*$rrow[2]/100;
             else
             {
                  $mstaffdisc_amt += $rrow[3]*$rrow[2]/100;
                  $mdummy +=  $rrow[3]*$rrow[2]/100; 
             }
        }
        $mval = $mstaffdisc_amt;
                                                        if($row[11]=='CREDIT')
                                                        {
                                                                $dbtrs=$row[9];
                                                                $cshvl=0;
                                                                $mtot_dbtrs+=$row[9];
                                                        }
                                                        else
                                                        {
                                                                $dbtrs=0;
                                                                $cshvl=$row[9];
                                                                $mtot_nettot += $row[9];

                                                        }
                                                        if($row[0]== 'COTTON')
                                                            {
                                                                $mtot_cot += $row[2];
                                                            }
                                                        else if ($row[0]== 'POLYSTER')
                                                            {
                                                                $mtot_pol += $row[2];
                                                            }
                                                        else if ($row[0]== 'REELED SILK')
                                                            {
                                                                $mtot_rel += $row[2];
                                                            }
                                                        else if ($row[0]== 'SPUNSILK')
                                                            {
                                                                $mtot_spu += $row[2];
                                                            }
                                                        else if ($row[0]== '0% TAXABLE VIP' || $row[0]== '5% TAXABLE VIP' || $row[0]== '14.5% TAXABLE VIP' || $row[0]== '1% TAXABLE VIP')
                                                            {
                                                                $mtot_vip += $row[2];
                                                            }
                                                        else if ($row[0]== 'WOOLEN')
                                                            {
                                                                $mtot_woo += $row[2];
                                                            }
                                                        else
                                                            {
                                                                $mtot_oth += $row[2];
                                                            }
                                                                $mtot_mba+= $row[3];
                                                                $mtot_strbtnml +=$row[4];
                                                                $mtot_strbtspl +=$row[5];
                                                                $mtot_redct +=$mval;
                                                                $mtot_befrtx +=$row[7];
                                                                $mtot_taxamt +=$row[8];
                                                            //}
                                                        
                                                        $i++;
                                                        
                                                        


                                                    }
                                                    //$pdf->Ln(4);
                                                    //$pdf->Ln(4);
                        $mgr_cot += $mtot_cot;
                        $mgr_pol += $mtot_pol;
                        $mgr_rel += $mtot_rel;
                        $mgr_spu += $mtot_spu;
                        $mgr_vip += $mtot_vip;
                        $mgr_woo += $mtot_woo;
                        $mgr_oth += $mtot_oth;
                        $mgr_mba+= $mtot_mba;
                        $mgr_strbtnml += $mtot_strbtnml;
                        $mgr_strbtspl+= $mtot_strbtspl;
                        $mgr_redct+= $mtot_redct;
                        $mgr_befrtx+= $mtot_befrtx;
                        $mgr_taxamt+= $mtot_taxamt;
                        $mgr_nettot+= $mtot_nettot;
                        $mgr_dbtrs+= $mtot_dbtrs;
                        $pdf->Cell(13,10,$minvno,'LTR',0, 'R');$pdf->Cell(20,10,number_format($mtot_cot,2),'LTR',0, 'R');$pdf->Cell(20,10,number_format($mtot_pol,2),'LTR',0, 'R');$pdf->Cell(21,10,number_format($mtot_rel,2),'LTR',0, 'R');
                                                                $pdf->Cell(20,10,number_format($mtot_spu,2),'LTR',0, 'R');$pdf->Cell(20,10,number_format($mtot_vip,2),'LTR',0, 'R');$pdf->Cell(15,10,number_format($mtot_woo,2),'LTR',0, 'R');$pdf->Cell(15,10,number_format($mtot_oth,2),'LTR',0, 'R');
                                                                $pdf->Cell(15,10,number_format($mtot_mba,2),'LTR',0, 'R');$pdf->Cell(15,10,number_format($mtot_strbtnml,2),'LTR',0, 'R');$pdf->Cell(15,10,number_format($mtot_strbtspl,2),'LTR',0, 'R');$pdf->Cell(15,10,number_format($mtot_redct,2),'LTR',0, 'R');
                                                                $pdf->Cell(20,10,number_format($mtot_befrtx,2),'LTR',0, 'R');$pdf->Cell(15,10,number_format($mtot_taxamt,2),'LTR',0, 'R');$pdf->Cell(22,10,number_format($mtot_nettot,2),'LTR',0, 'R');
                                                                $pdf->Cell(20,10,number_format($mtot_dbtrs,2),'LTR',0, 'R');
                        //$pdf->Ln(4);
                                                                $pdf->Ln(4);
                                                                $pdf->Ln(4);
                                                                $pdf->Cell(13,10,'Total','LBTR',0, 'R');$pdf->Cell(20,10,number_format($mgr_cot,2),'LBTR',0, 'R');$pdf->Cell(20,10,number_format($mgr_pol,2),'LBTR',0, 'R');$pdf->Cell(21,10,number_format($mgr_rel,2),'LBTR',0, 'R');
                                                                $pdf->Cell(20,10,number_format($mgr_spu,2),'LBTR',0, 'R');$pdf->Cell(20,10,number_format($mgr_vip,2),'LBTR',0, 'R');$pdf->Cell(15,10,number_format($mgr_woo,2),'LBTR',0, 'R');$pdf->Cell(15,10,number_format($mgr_oth,2),'LBTR',0, 'R');
                                                                $pdf->Cell(15,10,number_format($mgr_mba,2),'LBTR',0, 'R');$pdf->Cell(15,10,number_format($mgr_strbtnml,2),'LBTR',0, 'R');$pdf->Cell(15,10,number_format($mgr_strbtspl,2),'LBTR',0, 'R');$pdf->Cell(15,10,number_format($mgr_redct,2),'LBTR',0, 'R');
                                                                $pdf->Cell(20,10,number_format($mgr_befrtx,2),'LBTR',0, 'R');$pdf->Cell(15,10,number_format($mgr_taxamt,2),'LBTR',0, 'R');$pdf->Cell(22,10,number_format($mgr_nettot,2),'LBTR',0, 'R');
                                                                $pdf->Cell(20,10,number_format($mgr_dbtrs,2),'LBTR',0, 'R');
                                                                 
                                                                $pdf->Ln(4);
                                                                $pdf->Ln(4);

		$pdf->Output();	
?>
