<?
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("opendb.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['dfrom'];
  $fromdt=implode("-",array_reverse(explode("-",$fromdt)));
  $todt=$_REQUEST['dto'];
  $todt=implode("-",array_reverse(explode("-",$todt)));
  $type=$_REQUEST['type'];
  $redtype=$_REQUEST['redtype'];
  $mfrom_date = $fromdt;
    $mto_date = $todt;
       $colspan=8;
    $mstr[]='';
    $i=1;
    $mgname = '.';
    $mtot_total = 0;
    $mtot_mba= 0;
    $mtot_strbtnml = 0;
    $mtot_strbtspl = 0;
    $mtot_redct = 0;
    $mtot_befrtx = 0;
    $mtot_taxamt = 0;
    $mtot_nettot = 0;
    $mgr_total = 0;
    $mgr_mba= 0;
    $mgr_strbtnml = 0;
    $mgr_strbtspl = 0;
    $mgr_redct = 0;
    $mgr_befrtx = 0;
    $mgr_taxamt = 0;
    $mgr_nettot = 0;
    $ctr=0;
  $qry=mysqli_query($db,"select gname,invdate,sum(total),sum(mba),sum(strbtnml),sum(strbtspl),sum(redct),sum(befrtx),sum(taxamt),sum(nettot),min(invno),max(invno) from prod_invoice where invdate between '$fromdt' and '$todt' and brcode='$brcode' and gname='$type' group by gname,invdate");
        $mfrom_date = $fromdt;
  $mto_date = $todt;
  $pdf = new FPDF('P','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(190,7,'KERALA KHADI & VILLAGE INDUSTRIES BOARD',0,1,'C');
  $pdf->SetFont('Arial','',11);
  $brnchnm=strtoupper($brnchnm);
        $pdf->Cell(190,5,'FORM NO. 1',0,1,'C');
        $pdf->SetFont('Arial','',9);
  $pdf->Cell(190,5,'MONTHLY REBATE STATEMENT',0,1,'C');
        $pdf->SetFont('Arial','',9);
  $pdf->Cell(190,5,$type,0,1,'C');
  $pdf->Cell(190,5,'CENTER NAME :'.$brnchnm,0,1,'C');
  if(($redtype=='mda')or ($redtype=='MDA'))
  {
    $pdf->Cell(1,10,'','');$pdf->Cell(20,10,'','');
          $pdf->Cell(20,10,'Date','LTR');$pdf->Cell(20,10,'From Bill','LTR');$pdf->Cell(20,10,'To Bill','LTR');
    $pdf->Cell(30,10,'Amount','LTR');$pdf->Cell(30,10,'MDA','LTR');$pdf->Cell(30,10,'Total','LTR');
    $pdf->Cell(20,10,'Remarks','LTR');
  }
  if(($redtype=='strbtnml')or ($redtype=='State Rebate Normal'))
  {
    $pdf->Cell(1,10,'','');$pdf->Cell(20,10,'','');
           $pdf->Cell(20,10,'Date','LTR');$pdf->Cell(20,10,'From Bill','LTR');$pdf->Cell(20,10,'To Bill','LTR');
    $pdf->Cell(30,10,'Amount','LTR');$pdf->Cell(30,10,'State Rbt Normal','LTR');$pdf->Cell(30,10,'Total','LTR');
    $pdf->Cell(20,10,'Remarks','LTR');
  }
  if(($redtype=='strbtspl')or ($redtype=='State Rebate Special'))
  {
    $pdf->Cell(1,10,'','');$pdf->Cell(20,10,'','');
           $pdf->Cell(20,10,'Date','LTR');$pdf->Cell(20,10,'From Bill','LTR');$pdf->Cell(20,10,'To Bill','LTR');
    $pdf->Cell(30,10,'Amount','LTR');$pdf->Cell(30,10,'State Rebate Spl','LTR');$pdf->Cell(30,10,'Total','LTR');
    $pdf->Cell(20,10,'Remarks','LTR');
  }
        $pdf->Ln(5);
        while($row=mysqli_fetch_array($qry))
      {
      if($mgname <> $row[0])
      {
        if($mgname <> '.')
        {
    $pdf->Cell(1,10,'','');$pdf->Cell(20,10,'','');
    $pdf->Cell(20,10,$mgname.' Total','LTR',0, 'R');$pdf->Cell(20,10,number_format($mgr_total,2),'LTR',0, 'R');
    $pdf->Cell(20,10,number_format($mgr_mba,2),'LTR',0, 'R');$pdf->Cell(20,10,number_format($mgr_strbtnml,2),'LTR',0, 'R');
    $pdf->Cell(20,10,number_format($mgr_strbtspl,2),'LTR',0, 'R');$pdf->Cell(20,10,number_format($mgr_redct,2),'LTR',0, 'R');
    $pdf->Cell(20,10,number_format($mgr_befrtx,2),'LTR',0, 'R');$pdf->Cell(20,10,number_format($mgr_taxamt,2),'LTR',0, 'R');
    $pdf->Cell(20,10,number_format($mgr_nettot,2),'LTR',0, 'R');
    $mgr_total = 0;
          $mgr_mba= 0;
          $mgr_strbtnml = 0;
          $mgr_strbtspl = 0;
          $mgr_redct = 0;
          $mgr_befrtx = 0;
          $mgr_taxamt = 0;
          $mgr_nettot = 0;
   }
  $pdf->Ln(5);
  $pdf->Cell(1,10,'','');$pdf->Cell(20,10,'','');
  $pdf->Cell(20,10,$row[0],'LT',0, 'R');$pdf->Cell(20,10,'','T',0, 'R');$pdf->Cell(20,10,'','T',0, 'R');
  $pdf->Cell(30,10,'','T',0, 'R');$pdf->Cell(30,10,'','T',0, 'R');$pdf->Cell(30,10,'','T',0, 'R');
  $pdf->Cell(20,10,'','TR',0, 'R');
    $pdf->Ln(9);
  $mgname = $row[0];
      }
  $date = $row[1];
  if(($redtype=='mda')or ($redtype=='MDA'))
  {
    $date1 = date_create($date);
    $pdf->Cell(1,9,'','');$pdf->Cell(20,9,'','');
    $pdf->Cell(20,9,date_format($date1, 'd-m-Y'),'LTRB',0, 'R');$pdf->Cell(20,9,$row[10],'LTRB',0, 'R');
    $pdf->Cell(20,9,$row[11],'LTRB',0, 'R');$pdf->Cell(30,9,$row[2],'LTRB',0, 'R');$pdf->Cell(30,9,$row[3],'LTRB',0, 'R');
    $pdf->Cell(30,9,$row[3],'LTRB',0, 'R');$pdf->Cell(20,9,'','LTRB',0, 'R');
      $pdf->Ln(9);
  }

  if(($redtype=='strbtnml')or ($redtype=='State Rebate Normal'))
  {
    $date1 = date_create($date);
    $pdf->Cell(1,9,'','');$pdf->Cell(20,9,'','');
    $pdf->Cell(20,9,date_format($date1, 'd-m-Y'),'LTRB',0, 'R');$pdf->Cell(20,9,$row[10],'LTRB',0, 'R');
    $pdf->Cell(20,9,$row[11],'LTRB',0, 'R');$pdf->Cell(30,9,$row[2],'LTRB',0, 'R');$pdf->Cell(30,9,$row[4],'LTRB',0, 'R');
    $pdf->Cell(30,9,$row[4],'LTRB',0, 'R');$pdf->Cell(20,9,'','LTRB',0, 'R');
      $pdf->Ln(9);
  }
  if(($redtype=='strbtspl')or ($redtype=='State Rebate Special'))
  {
    $date1 = date_create($date);
    $pdf->Cell(1,9,'','');$pdf->Cell(20,9,'','');
    $pdf->Cell(20,9,date_format($date1, 'd-m-Y'),'LTRB',0, 'R');$pdf->Cell(20,9,$row[10],'LTRB',0, 'R');
    $pdf->Cell(20,9,$row[11],'LTRB',0, 'R');$pdf->Cell(30,9,$row[2],'LTRB',0, 'R');$pdf->Cell(30,9,$row[5],'LTRB',0, 'R');
    $pdf->Cell(30,9,$row[5],'LTRB',0, 'R');$pdf->Cell(20,9,'','LTRB',0, 'R');
      $pdf->Ln(9);
  }
   $mgr_total += $row[2];
      $mgr_mba += $row[3];
      $mgr_strbtnml += $row[4];
      $mgr_strbtspl += $row[5];
      $mgr_redct += $row[6];
      $mgr_befrtx += $row[7];
      $mgr_taxamt += $row[8];
      $mgr_nettot += $row[9];
      $mtot_total += $row[2];
      $mtot_mba += $row[3];
      $mtot_strbtnml += $row[4];
      $mtot_strbtspl += $row[5];
      $mtot_redct += $row[6];
      $mtot_befrtx += $row[7];
      $mtot_taxamt += $row[8];
      $mtot_nettot += $row[9];
      $i++;
  }
  if(($redtype=='mda')or ($redtype=='MDA'))
  {
    $pdf->Cell(1,10,'','');$pdf->Cell(20,10,'','');
    $pdf->Cell(20,10,'Grand Total','LTRB',0, 'R');$pdf->Cell(20,10,'','LTRB',0, 'R');$pdf->Cell(20,10,'','LTRB',0, 'R');
    $pdf->Cell(30,10,$mtot_total,'LTRB',0, 'R');$pdf->Cell(30,10,$mtot_mba,'LTRB',0, 'R');
    $pdf->Cell(30,10,$mtot_mba,'LTRB',0, 'R');$pdf->Cell(20,10,'','LTRB',0, 'R');
  }
  if(($redtype=='strbtnml')or ($redtype=='State Rebate Normal'))
  {
    $pdf->Cell(1,10,'','');$pdf->Cell(20,10,'','');
    $pdf->Cell(20,10,'Grand Total','LTRB',0, 'R');$pdf->Cell(20,10,'','LTRB',0, 'R');$pdf->Cell(20,10,'','LTRB',0, 'R');
    $pdf->Cell(30,10,$mtot_total,'LTRB',0, 'R');$pdf->Cell(30,10,$mtot_strbtnml,'LTRB',0, 'R');
    $pdf->Cell(30,10,$mtot_strbtnml,'LTRB',0,'R');$pdf->Cell(20,10,'','LTRB',0, 'R');
  }
  if(($redtype=='strbtspl')or ($redtype=='State Rebate Special'))
  {
    $pdf->Cell(1,10,'','');$pdf->Cell(20,10,'','');
    $pdf->Cell(20,10,'Grand Total','LTRB',0, 'R');$pdf->Cell(20,10,'','LTRB',0, 'R');$pdf->Cell(20,10,'','LTRB',0, 'R');
    $pdf->Cell(30,10,$mtot_total,'LTRB',0, 'R');$pdf->Cell(30,10,$mtot_strbtspl,'LTRB',0, 'R');
    $pdf->Cell(30,10,$mtot_strbtspl,'LTRB',0, 'R');$pdf->Cell(20,10,'','LTRB',0, 'R');
  }
        $pdf->Ln(9);
  $pdf->Output();
?>