<?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(233,755,12,"<b>(CUSTOMER LEDGER)</b>");
$pdf->ezSetDy(-140);

$head=$_POST['cid']; 
$mhead=$_POST['name']; 

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




$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 where A_Id='$head' ");
while($rstRow = mysqli_fetch_array($nResult))
{
$strFrom=$rstRow['dte'];
}
}
$fdate=date('d-m-Y',strtotime($strFrom));
$tdate=date('d-m-Y',strtotime($strTo));


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



$pdf->addText(30,715,11,"<b>Customer Name:</b>          $mhead");
$pdf->addText(30,700,11,"<b>Customer Code:</b>            $head");


   $pdf->addText(3,680,15,"___________________________________________________________________________________________________");

$date1=Date("j-n-Y");
$pdf->addText(225,740,11,"<b>From $fdate to $tdate</b>");

$Result = MySQLi_Query($link, "Select Dued, C_Limit, City from acc_heads where A_Id='$head'");

while($rstRow1 = mysqli_fetch_array($Result))
{
$dd=$rstRow1['Dued'];
$cl=$rstRow1['C_Limit'];
$cty=$rstRow1['City'];

}
$pdf->addText(414,710,11,"<b>Due Days:</b>        $dd");
$pdf->addText(400,690,11,"<b>Credit Limit:</b>        $cl");
$pdf->addText(30,685,11,"<b>City:</b>                                $cty");




$rst = MySQLi_Query($link, "Select sum(Debit-Credit) as bal from ledgers where A_Id='$head' and D_Date<'$strFrom' group by A_Id");
while($rstRow2 = mysqli_fetch_array($rst))
{
$bal=$rstRow2['bal'];
}
$data = array(); // initialize table data

// First row = Opening Balance
$data[] = array(
    'num' => '',   // No serial number
    'dte' => date('d-m-Y', strtotime($strFrom)),
    'vch' => '',
    'rem' => '<b>Opening Balance</b>',
    'chq' => '',
    'deb' => '',
    'cre' => '',
    'bal' => number_format($bal, 0)
);


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

$count=0;
$one=1;
//$bal=0;
$tdeb=0;
$tcre=0;
while($rstRow = mysqli_fetch_array($nResult))
                {

if (!empty($rstRow['Inv_Det2'])) {
        $voucher = $rstRow['Inv_Det2'];
    } elseif (!empty($rstRow['vch_number'])) {
        $voucher = $rstRow['vch_number'];
    } else {
        $voucher = "-"; // fallback if both are empty
    }

$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'=>$voucher,'rem'=>$rstRow['Details'],'deb'=>$debs,'cre'=>$cres,'bal'=>$bal1);
}

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

$pdf->ezTable($data,$cols,'',
        array('xPos'=>20,'xOrientation'=>'right','fontSize' => 9,'width'=>580,'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'=>85),
                 
                 'rem'=>array('justification'=>'left','width'=>215),
               
                'deb'=>array('justification'=>'right','width'=>56),
                'cre'=>array('justification'=>'right','width'=>56),
                   
                 'bal'=>array('justification'=>'right','width'=>56)
                     
                 
             )));



$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>Last Balance:  </b>       $bal1" , 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();
?>



