<?

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,750,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(270,780,13,"<b>(GLAKB)</b>");
$pdf->addText(0,717,15,"___________________________________________________________________________________________________");
$pdf->ezSetDy(-105);


$pdf->addText(160,755,12,"<b>SCHEME DISCOUNT REPORT (CUSTOMER-WISE)</b>");



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

$sman=$_POST['sman']; 
$province=$_POST['ware'];
$cust=$_POST['cust'];


if ($strFrom=='')
{
$nResult1 = MySQLi_Query($link, "select MIN(D_Date) as dte from ledgers ");
while($rstRow1 = mysqli_fetch_array($nResult1))
{
$strFrom=$rstRow1['dte'];
}
}

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


if(!empty($province))
{
$pdf->addText(20,725,12,"<b>Province=</b>   $province");


$result = MySQLi_Query($link, "select ledgers.A_Id, ledgers.Code, ledgers.Details, ledgers.Credit, acc_heads.Province, acc_heads.Salesman from ledgers, acc_heads where ledgers.Code=acc_heads.Name and ledgers.M_Head='Customer' and ledgers.D_Date between '$strFrom' and '$strTo' and L_Type='DIS' and acc_heads.Province='$province'  order by ledgers.Code");
$count=0;
$one=1;
$deb=0;
$cre=0;

$data = array();
$gt_debit = 0;
$gt_credit = 0;

while($rstRow = mysqli_fetch_array($result))
{
    $count = $one + $count;

    
    $gt_credit += $rstRow['Credit'];   // accumulate credit

      
    $credit = number_format($rstRow['Credit'], 2);          

    $data[] = array(
        'num'  => $count,
       
        'idd'  => $rstRow['A_Id'],
        'pgrp' => $rstRow['Code'],
        'id'   => $rstRow['Details'],
      
        'pack' => $credit
    );
}

// 👉 ADD GRAND TOTAL ROW
$data[] = array(
    'num'  => '',
    'idd'  => '',
    'pgrp' => '',
    'id'   => '<b>Grand Total</b>',
    'pack' => '<b>' . number_format($gt_credit, 2) . '</b>'
);

$cols = array(
    'num'=>'<b>S. No</b>',
    'idd'=>'<b>Code</b>',
    'pgrp'=>'<b>Customer Name</b>',
    'id'=>'<b>Narration</b>',
    'pack'=>'<b>Discount</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),
         
            'idd'=>array('justification'=>'center','width'=>85),
            'pgrp'=>array('justification'=>'left','width'=>200),
            'id'=>array('justification'=>'left','width'=>182),
            'pack'=>array('justification'=>'right','width'=>65)
        )
    )
);
}


if(!empty($sman))
{
$pdf->addText(20,725,12,"<b>Salesman=</b>   $sman");


$result = MySQLi_Query($link, "select ledgers.A_Id, ledgers.Code, ledgers.Details, ledgers.Credit, acc_heads.Province, acc_heads.Salesman from ledgers, acc_heads where ledgers.Code=acc_heads.Name and ledgers.M_Head='Customer' and ledgers.D_Date between '$strFrom' and '$strTo' and L_Type='DIS' and ledgers.Salesman='$sman'  order by ledgers.Code");
$count=0;
$one=1;
$deb=0;
$cre=0;

$data = array();
$gt_debit = 0;
$gt_credit = 0;

while($rstRow = mysqli_fetch_array($result))
{
    $count = $one + $count;

    
    $gt_credit += $rstRow['Credit'];   // accumulate credit

      
    $credit = number_format($rstRow['Credit'], 2);          

    $data[] = array(
        'num'  => $count,
       
        'idd'  => $rstRow['A_Id'],
        'pgrp' => $rstRow['Code'],
        'id'   => $rstRow['Details'],
      
        'pack' => $credit
    );
}

// 👉 ADD GRAND TOTAL ROW
$data[] = array(
    'num'  => '',
    'idd'  => '',
    'pgrp' => '',
    'id'   => '<b>Grand Total</b>',
    'pack' => '<b>' . number_format($gt_credit, 2) . '</b>'
);

$cols = array(
    'num'=>'<b>S. No</b>',
    'idd'=>'<b>Code</b>',
    'pgrp'=>'<b>Customer Name</b>',
    'id'=>'<b>Narration</b>',
    'pack'=>'<b>Discount</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),
         
            'idd'=>array('justification'=>'center','width'=>85),
            'pgrp'=>array('justification'=>'left','width'=>200),
            'id'=>array('justification'=>'left','width'=>182),
            'pack'=>array('justification'=>'right','width'=>65)
        )
    )
);
}




if(!empty($cust))
{
$pdf->addText(20,725,12,"<b>Customer=</b>   $cust");


$result = MySQLi_Query($link, "select ledgers.A_Id, ledgers.Code, ledgers.Details, ledgers.Credit, acc_heads.Province, acc_heads.Salesman from ledgers, acc_heads where ledgers.Code=acc_heads.Name and ledgers.M_Head='Customer' and ledgers.D_Date between '$strFrom' and '$strTo' and L_Type='DIS' and ledgers.Code='$cust'  order by ledgers.Code");
$count=0;
$one=1;
$deb=0;
$cre=0;

$data = array();
$gt_debit = 0;
$gt_credit = 0;

while($rstRow = mysqli_fetch_array($result))
{
    $count = $one + $count;

    
    $gt_credit += $rstRow['Credit'];   // accumulate credit

      
    $credit = number_format($rstRow['Credit'], 2);          

    $data[] = array(
        'num'  => $count,
       
        'idd'  => $rstRow['A_Id'],
        'pgrp' => $rstRow['Code'],
        'id'   => $rstRow['Details'],
      
        'pack' => $credit
    );
}

// 👉 ADD GRAND TOTAL ROW
$data[] = array(
    'num'  => '',
    'idd'  => '',
    'pgrp' => '',
    'id'   => '<b>Grand Total</b>',
    'pack' => '<b>' . number_format($gt_credit, 2) . '</b>'
);

$cols = array(
    'num'=>'<b>S. No</b>',
    'idd'=>'<b>Code</b>',
    'pgrp'=>'<b>Customer Name</b>',
    'id'=>'<b>Narration</b>',
    'pack'=>'<b>Discount</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),
         
            'idd'=>array('justification'=>'center','width'=>85),
            'pgrp'=>array('justification'=>'left','width'=>200),
            'id'=>array('justification'=>'left','width'=>182),
            'pack'=>array('justification'=>'right','width'=>65)
        )
    )
);
}



        ob_clean();

        $pdf->ezStream();
?>
