<?php
include ('class.ezpdf.php');
include 'includes/config.php';
include 'defs.php';
$pdf = new Cezpdf('A4','portrait');
$pdf->selectFont('fonts/Times-Roman.afm');
$pdf->addJpegFromFile('images/logo1.jpg',30,740,70);
//$pdf->addText(18,750,9,"MIS GENERATED REPORT");
$date1=Date("j-n-Y");
//$pdf->ezStartPageNumbers(526, 16, 10, 'right', '', 1);
$pdf->setColor(0.72, 0.72, 0.72);
$pdf->filledRectangle(220, 770, 160, 30, 'rgb');
$pdf->setColor(0,0,0);
$pdf->addText(275,780,13,"<b>(GLAKB)</b>");
$pdf->addText(220,755,12,"<b>(ACCOUNTS HEAD REPORT)</b>");
$pdf->ezSetDy(-140);

$head=$_POST['searchBox']; 
$mhead=$_POST['strdup']; 
$code=$_POST['str1']; 

$strFrom=$_POST['strFrom'];
$strTo=$_POST['strTo'];
$val=$_POST['strn'];




$header = $pdf->openObject();
$pdf->addText(20, 16, 10, "<b>Contact us @</b> Sabzal Road - Block No 3, STN Colony, Near Spini Road, Quetta (+92 81 2873538)");
$pdf->addText(20, 30, 10, "<b>________________________________________________________________________________________________________________</b>");

$pdf->closeObject();
$pdf->addObject($header, "all");



if ($strFrom=='')
{
$nResult = MySQLi_Query($link, "select MIN(D_Date) as dte from ledgers ");
while($rstRow = mysqli_fetch_array($nResult))
{
$strFrom=$rstRow['dte'];
}
}
$fdate=date('d-m-Y',strtotime($strFrom));
$tdate=date('d-m-Y',strtotime($strTo));

$pdf->addText(30,715,11,"<b>Accounts Category:</b>      $mhead");
$pdf->addText(30,700,11,"<b>Accounts Head:</b>             $head");
$pdf->addText(30,685,11,"<b>Accounts Code:</b>             $code");

if($val=='oa')
{
    //$pdf->addText(3,700,15,"___________________________________________________________________________________________________");

$date1=Date("j-n-Y");
$pdf->addText(260,740,11,"<b>(As on $date1)</b>");

$nResult = MySQLi_Query($link, "Select * from ledgers where Code='$head' order by D_Date, Id ");

$count=0;
$one=1;
$bal=0;
$tdeb=0;
$tcre=0;
while($rstRow = mysqli_fetch_array($nResult))
                {
$deb=$rstRow['Debit'];
$cre=$rstRow['Credit'];
$debs=number_format($rstRow['Debit'], 0);
$cres=number_format($rstRow['Credit'], 0);
$bal=$bal+$deb-$cre; 
  $bal1=number_format($bal, 0);        
$date=date('d-m-Y',strtotime($rstRow['D_Date']));
$tdeb=$rstRow['Debit']+$tdeb;
$tcre=$rstRow['Credit']+$tcre;
$count=$one+$count;

$data[] = array('num'=>$count,'dte'=>$date,'vch'=>$rstRow['vch_number'],'rem'=>$rstRow['Details'],'chq'=>$rstRow['Chq'],'deb'=>$debs,'cre'=>$cres,'bal'=>$bal1);
}

$cols = array('num'=>'<b>S. No</b>','dte'=>'<b>Date</b>','vch'=>'<b>Vch#</b>','rem'=>'<b>Particulars</b>','chq'=>'<b>Cheque No.</b>','deb'=>'<b>Debit</b>','cre'=>'<b>Credit</b>','bal'=>'<b>Balance</b>');

$pdf->ezTable($data,$cols,'',
        array('xPos'=>20,'xOrientation'=>'right','fontSize' => 10,'width'=>680,'showHeadings'=>1,'shaded'=>0, 'showLines'=>2,'cols'=>array(
                'num'=>array('justification'=>'center','width'=>38),
                 'dte'=>array('justification'=>'center','width'=>60),
                 'vch'=>array('justification'=>'center','width'=>45),
                 
                 'rem'=>array('justification'=>'left','width'=>150),
                 'chq'=>array('justification'=>'center','width'=>68),
                'deb'=>array('justification'=>'right','width'=>68),
                'cre'=>array('justification'=>'right','width'=>68),
                   
                 'bal'=>array('justification'=>'right','width'=>68)
                     
                 
             )));



$ball=$tdeb-$tcre;
  $tdeb=number_format($tdeb, 0);          
            $tcre=number_format($tcre, 0);          
          
$pdf->ezSetDy(-10);
    $pdf->ezText("<b>Total Debits :</b>          $tdeb", 11, array("justification" => 'left'));
$pdf->ezSetDy(-5);
$pdf->ezText("<b>Total Credits :  </b>       $tcre" , 11, array("justification" => 'left'));

          $pdf->ezText("___________________________________________", 9, array("justification" => 'left'));

            
 $ball=number_format($ball, 0);          
          

                           $pdf->ezSetDy(-5);
             $pdf->ezText("<b>Balance :  </b>                 $ball" , 11, array("justification" => 'left'));
                     $pdf->ezText("___________________________________________", 9, array("justification" => 'left'));

}

if($val=='dw')
{
   

$strFrom=date('Y-m-d',strtotime($strFrom));
$strTo=date('Y-m-d',strtotime($strTo));

$nlt = MySQLi_Query($link, "Select sum(Debit-Credit) as pbal from ledgers where Code='$head' and D_Date<'$strFrom' group by Code");
     while($rstRow22 = mysqli_fetch_array($nlt))
                {
                    $pbal=$rstRow22['pbal'];
  }
$pbal1=number_format($pbal, 0);
$pdf->addText(420,685,11,"<b>Previous Balance =</b> $pbal1");

$pdf->addText(230,740,11,"<b>From</b> $fdate <b>to</b> $tdate");

$nResult = MySQLi_Query($link, "Select * from ledgers where Code='$head' and D_Date between '$strFrom' and '$strTo' order by D_Date, Id ");

$count=0;
$one=1;
$bal=0;
$tdeb=0;
$tcre=0;
$obal=0;
while($rstRow = mysqli_fetch_array($nResult))
                {
$deb=$rstRow['Debit'];
$cre=$rstRow['Credit'];
$debs=number_format($rstRow['Debit'], 2);
$cres=number_format($rstRow['Credit'], 2);
$bal=$bal+$deb-$cre; 
$obal=$pbal+$bal;

$bal1=number_format($bal, 2);        
$date=date('d-m-Y',strtotime($rstRow['D_Date']));
$tdeb=$rstRow['Debit']+$tdeb;
$tcre=$rstRow['Credit']+$tcre;
$count=$one+$count;

$data[] = array('num'=>$count,'dte'=>$date,'vch'=>$rstRow['vch_number'],'rem'=>$rstRow['Details'],'chq'=>$rstRow['Chq'],'deb'=>$debs,'cre'=>$cres,'bal'=>$bal1);
}

$cols = array('num'=>'<b>S. No</b>','dte'=>'<b>Date</b>','vch'=>'<b>Vch#</b>','rem'=>'<b>Particulars</b>','chq'=>'<b>Cheque No.</b>','deb'=>'<b>Debit</b>','cre'=>'<b>Credit</b>','bal'=>'<b>Balance</b>');

$pdf->ezTable($data,$cols,'',
        array('xPos'=>20,'xOrientation'=>'right','fontSize' => 10,'width'=>680,'showHeadings'=>1,'shaded'=>0, 'showLines'=>2,'cols'=>array(
                'num'=>array('justification'=>'center','width'=>38),
                 'dte'=>array('justification'=>'center','width'=>60),
                 'vch'=>array('justification'=>'center','width'=>45),
                 
                 'rem'=>array('justification'=>'left','width'=>150),
                 'chq'=>array('justification'=>'center','width'=>68),
                'deb'=>array('justification'=>'right','width'=>68),
                'cre'=>array('justification'=>'right','width'=>68),
                   
                 'bal'=>array('justification'=>'right','width'=>68)
                     
                 
             )));



$ball=$tdeb-$tcre;
  $tdeb=number_format($tdeb, 0);          
            $tcre=number_format($tcre, 0);          
          
$pdf->ezSetDy(-10);
    $pdf->ezText("<b>Total Debits :</b>              $tdeb", 11, array("justification" => 'left'));
$pdf->ezSetDy(-5);
$pdf->ezText("<b>Total Credits :  </b>           $tcre" , 11, array("justification" => 'left'));

          $pdf->ezText("___________________________________________", 9, array("justification" => 'left'));

            
$ball=number_format($ball, 0);          
$pdf->ezSetDy(-5);
$pdf->ezText("<b>Dates Balance :  </b>         $ball" , 11, array("justification" => 'left'));

$nlt1 = MySQLi_Query($link, "Select sum(Debit-Credit) as pbal23 from ledgers where Code='$head' group by Code");
while($rstRow21 = mysqli_fetch_array($nlt1))
{
$pbal23=$rstRow21['pbal23'];
}

$pdf->ezSetDy(-5);
$pbal24=number_format($pbal23, 0);

$pdf->ezText("<b>Current Balance :  </b>     $pbal24" , 11, array("justification" => 'left'));


                     $pdf->ezText("___________________________________________", 9, array("justification" => 'left'));

}




$pdf->ezText("<b>Owner GLAKB</b>", 12, array("justification" => 'right'));
$pdf->ezSetDy(-5);

ob_clean();
$pdf->ezStream();
?>



