<?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(200,755,12,"<b>(ACCOUNTS CATEGORY REPORT)</b>");
$pdf->ezSetDy(-110);

$mhead=$_POST['item3']; 
$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,12,"<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 ledgers.M_Head, ledgers.Code, SUM(ledgers.Debit) as debs, SUM(ledgers.Credit) as cres, SUM(ledgers.Debit-ledgers.Credit) as bal, acc_heads.A_Id from ledgers, acc_heads where ledgers.Code=acc_heads.Name and ledgers.M_Head='$mhead' group by ledgers.Code order by ledgers.Code ");

$count=0;
$one=1;
$bal=0;
$tdeb=0;
$tcre=0;
while($rstRow = mysqli_fetch_array($nResult))
                {
$debs=number_format($rstRow['debs'], 0);
$cres=number_format($rstRow['cres'], 0);
$bals=number_format($rstRow['bal'], 0);

//$bal=$bal+$deb-$cre; 
//$bal1=number_format($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,'id'=>$rstRow['A_Id'],'head'=>$rstRow['Code'],'deb'=>$debs,'cre'=>$cres,'bal'=>$bals);
}

$cols = array('num'=>'<b>S. No</b>','id'=>'<b>Accounts Code</b>','head'=>'<b>Accounts Name</b>','deb'=>'<b>Total Debits</b>','cre'=>'<b>Total Credits</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'=>45),
                 'id'=>array('justification'=>'center','width'=>78),
                 'head'=>array('justification'=>'left','width'=>220),
                 
                 
                 
                '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')
{
   

$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(228,740,11,"<b>From</b> $fdate <b>to</b> $tdate");

$nResult = MySQLi_Query($link, "Select M_Head, Code, A_Id, SUM(Debit) as debs, SUM(Credit) as cres, SUM(Debit-Credit) as bal from ledgers where M_Head='$mhead' 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))
                {
$debs=number_format($rstRow['debs'], 0);
$cres=number_format($rstRow['cres'], 0);
$bals=number_format($rstRow['bal'], 0);

//$bal=$bal+$deb-$cre; 
//$bal1=number_format($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,'id'=>$rstRow['A_Id'],'head'=>$rstRow['Code'],'deb'=>$debs,'cre'=>$cres,'bal'=>$bals);
}

$cols = array('num'=>'<b>S. No</b>','id'=>'<b>Accounts Code</b>','head'=>'<b>Accounts Name</b>','deb'=>'<b>Total Debits</b>','cre'=>'<b>Total Credits</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'=>45),
                 'id'=>array('justification'=>'center','width'=>78),
                 'head'=>array('justification'=>'left','width'=>220),
                 
                 
                 
                '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>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->ezSetDy(-10);
$pdf->ezText("<b>Owner GLAKB</b>", 12, array("justification" => 'right'));

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



