<?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);
$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(270,780,13,"<b>(GLAKB)</b>");
$pdf->addText(210,755,12,"<b>(OVERALL BANK STATEMENT)</b>");
$pdf->addText(3,720,15,"___________________________________________________________________________________________________");
$date1=Date("j-n-Y");
$pdf->ezSetDy(-105);
$strFrom=$_POST['strFrom'];
$strTo=$_POST['strTo'];
$val=$_POST['strn'];
$usr=$_GET['usr']; 

$pdf->addText(18, 710, 11, "<b>$comp</b>");

$header = $pdf->openObject();
$pdf->addText(20, 16, 10, "<b>Contact us @</b> Balochistan Technical Education and Vocational Training Authority. 355/125-B, Jinnah Town Samungli Road, Quetta");
$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));

if($val=='oa')
{
$pdf->addText(255,735,12,"<b>(As on $date1)</b>");

$nResult = MySQLi_Query($link, "Select M_Head, Code, Sum(Debit) as debs, sum(Credit) as cres, Sum(Debit-Credit) as bal from ledgers where M_Head='Assets' and Code!='Cash' group by Code order by Code ");

$count=0;
$one=1;
$bal=0;
$tdeb=0;
$tcre=0;
while($rstRow = mysqli_fetch_array($nResult))
{
$codee=$rstRow['Code'];
$rst = MySQLi_Query($link, "Select Details from acc_heads where Name='$codee'");

while($rt = mysqli_fetch_array($rst))
{
$det=$rt['Details'];
}
$debs=number_format($rstRow['debs'], 0);
$cres=number_format($rstRow['cres'], 0);
$bal=number_format($rstRow['bal'], 0);          
$date=date('d-m-Y',strtotime($rstRow['D_Date']));
$tdeb=$rstRow['debs']+$tdeb;
$tcre=$rstRow['cres']+$tcre;
$count=$one+$count;

$data[] = array('num'=>$count,'code'=>$codee,'det'=>$det,'deb'=>$debs,'cre'=>$cres,'bal'=>$bal);
//$data[] = array('num'=>$count,'code'=>"<c:alink:direct_head.php?cde=$code1>$code1</c:alink>",'deb'=>$debs,'cre'=>$cres,'bal'=>$bal);
}

$cols = array('num'=>'<b>S. No</b>','code'=>'<b>Bank Title</b>','det'=>'<b>Description</b>','deb'=>'<b>Receipts</b>','cre'=>'<b>Expenditures</b>','bal'=>'<b>Balance</b>');

$pdf->ezTable($data,$cols,'',
        array('xPos'=>20,'xOrientation'=>'right','fontSize' => 10,'width'=>700,'showHeadings'=>1,'shaded'=>1, 'showLines'=>1,'cols'=>array(
                'num'=>array('justification'=>'center','width'=>48),
                 'code'=>array('justification'=>'left','width'=>180),
                 'det'=>array('justification'=>'left','width'=>125),
                'deb'=>array('justification'=>'right','width'=>72),
                'cre'=>array('justification'=>'right','width'=>72),
                   
                 'bal'=>array('justification'=>'right','width'=>72)
                     
                 
             )));



$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')
{


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

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

$nResult = MySQLi_Query($link, "Select M_Head, Code, Sum(Debit) as debs, sum(Credit) as cres, Sum(Debit-Credit) as bal from ledgers where M_Head='Assets' and Code!='Cash' and D_Date between '$strFrom' and '$strTo' group by Code order by Code ");

$count=0;
$one=1;
$bal=0;
$tdeb=0;
$tcre=0;
while($rstRow = mysqli_fetch_array($nResult))
{
    $codee=$rstRow['Code'];
$rst = MySQLi_Query($link, "Select Details from acc_heads where Name='$codee'");

while($rt = mysqli_fetch_array($rst))
{
$det=$rt['Details'];
}
$debs=number_format($rstRow['debs'], 0);
$cres=number_format($rstRow['cres'], 0);
$bal=number_format($rstRow['bal'], 0);          
$date=date('d-m-Y',strtotime($rstRow['D_Date']));
$tdeb=$rstRow['debs']+$tdeb;
$tcre=$rstRow['cres']+$tcre;
$count=$one+$count;

$data[] = array('num'=>$count,'code'=>$codee,'det'=>$det,'deb'=>$debs,'cre'=>$cres,'bal'=>$bal);
//$data[] = array('num'=>$count,'code'=>"<c:alink:direct_head.php?cde=$code1>$code1</c:alink>",'deb'=>$debs,'cre'=>$cres,'bal'=>$bal);
}

$cols = array('num'=>'<b>S. No</b>','code'=>'<b>Bank Title</b>','det'=>'<b>Description</b>','deb'=>'<b>Receipts</b>','cre'=>'<b>Expenditures</b>','bal'=>'<b>Balance</b>');

$pdf->ezTable($data,$cols,'',
        array('xPos'=>20,'xOrientation'=>'right','fontSize' => 10,'width'=>700,'showHeadings'=>1,'shaded'=>1, 'showLines'=>1,'cols'=>array(
                'num'=>array('justification'=>'center','width'=>48),
                 'code'=>array('justification'=>'left','width'=>180),
                 'det'=>array('justification'=>'left','width'=>125),
                'deb'=>array('justification'=>'right','width'=>72),
                'cre'=>array('justification'=>'right','width'=>72),
                   
                 'bal'=>array('justification'=>'right','width'=>72)
                     
                 
             )));

$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'));
}
$pdf->ezText("<b>Owner GLAKB</b>", 12, array("justification" => 'right'));

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



