<?php
include ('class.ezpdf.php');
include 'includes/config.php';
include 'defs.php';
$pdf = new Cezpdf('A4','landscape');
$pdf->selectFont('fonts/Times-Roman.afm');
$pdf->addJpegFromFile('images/logo1.jpg',40,520,55);
$date1=Date("j-n-Y");
$pdf->ezSetDy(-90);
$pdf->ezStartPageNumbers(800, 16, 9, 'right', '', 1);
$pdf->setColor(0.72, 0.72, 0.72);
$pdf->filledRectangle(220, 805, 160, 20, 'rgb');
$pdf->setColor(0,0,0);
$pdf->addText(290,560,13,"<b>GULF LUBE AHMAD ULLAH KHAN BROTHERS</b>");
$pdf->addText(325,545,12,"<b>(COMPLETE PURCHASE INFORMATION)</b>");

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



if ($strFrom=='')
{
$nResult = MySQLi_Query($link, "select MIN(Inv_Date) as dte from p_details");
while($rstRow = mysqli_fetch_array($nResult))
{
$strFrom=$rstRow['dte'];
}
}

$fdate=date('d-m-Y',strtotime($strFrom));
$tdate=date('d-m-Y',strtotime($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(!$cmp)
{
$pdf->addText(20,500,12,"<b>Overall Purchases</b>");

$pdf->addText(20,485,11,"<b>From = </b>            $fdate <b> To: </b> $tdate ");

$count=0;
$one=1;
$nResult = MySQLi_Query($link, "Select * from purchases 
where Inv_Date between '$strFrom' and '$strTo' 
and Inv_Type='Dir Purchase' 
order by Inv_Date, Inv_No");

$count = 0;
$data = array();

/* -------------------------
   GRAND TOTAL INITIALIZATION
---------------------------*/
$gtot = $gdis = $gft = $gst = $gntot = $gqty = $gcart = 0;

/* -------------------------
   SUB TOTAL INITIALIZATION
---------------------------*/
$inv_prev = "";
$stot = $sdis = $sft = $sst = $sntot = $sqty = $scart = 0;

while($rstRow = mysqli_fetch_array($nResult))
{
    $inv_curr = $rstRow['Inv_No'];

    // -------------------------
    // WHEN INVOICE CHANGES → PRINT SUBTOTAL
    // -------------------------
    if($inv_prev != "" && $inv_prev != $inv_curr)
    {
        $data[] = array(
            'num'   => '',
            'dte'   => '',
            'comp'  => '<b>Sub Total</b>',
            'inv'   => '',
            'prod'  => '',
            'lit'   => '<b>'.number_format($sqty,2).'</b>',
            'cart'  => '<b>'.number_format($scart,2).'</b>',
            'rate'  => '',
            'tot'   => '<b>'.number_format($stot,2).'</b>',
            'dis'   => '<b>'.number_format($sdis,2).'</b>',
            'ft'    => '<b>'.number_format($sft,2).'</b>',
            'st'    => '<b>'.number_format($sst,2).'</b>',
            'ntot'  => '<b>'.number_format($sntot,2).'</b>'
        );

        // reset sub totals
        $stot = $sdis = $sft = $sst = $sntot = $sqty = $scart = 0;
    }

    // -------------------------
    // ADD TO SUBTOTAL
    // -------------------------
    $sqty  += $rstRow['Qty'];
    $scart += $rstRow['Carton'];
    $stot  += $rstRow['Total'];
    $sdis  += $rstRow['R_Dis'];
    $sft   += $rstRow['F_Amt'];
    $sst   += $rstRow['S_Amt'];
    $sntot += $rstRow['N_Total'];

    // -------------------------
    // ADD TO GRAND TOTAL
    // -------------------------
    $gqty  += $rstRow['Qty'];
    $gcart += $rstRow['Carton'];
    $gtot  += $rstRow['Total'];
    $gdis  += $rstRow['R_Dis'];
    $gft   += $rstRow['F_Amt'];
    $gst   += $rstRow['S_Amt'];
    $gntot += $rstRow['N_Total'];

    // -------------------------
    // NORMAL ROW
    // -------------------------
    $idate = date("d-m-Y", strtotime($rstRow['Inv_Date']));
    $count++;

    $data[] = array(
        'num'   => $count,
        'dte'   => $idate,
        'comp'  => $rstRow['Company'],
        'inv'   => $rstRow['Inv_Det'],
        'prod'  => $rstRow['Name'],
        'lit'   => $rstRow['Qty'],
        'cart'  => $rstRow['Carton'],
        'rate'  => $rstRow['Rate'],
        'tot'   => number_format($rstRow['Total'],2),
        'dis'   => number_format($rstRow['R_Dis'],2),
        'ft'    => number_format($rstRow['F_Amt'],2),
        'st'    => number_format($rstRow['S_Amt'],2),
        'ntot'  => number_format($rstRow['N_Total'],2)
    );

    $inv_prev = $inv_curr;
}

/* -----------------------------------------------------
   ADD LAST INVOICE SUBTOTAL (IMPORTANT)
-----------------------------------------------------*/
if($inv_prev != "")
{
    $data[] = array(
        'num'   => '',
        'dte'   => '',
        'comp'  => '<b>Sub Total</b>',
        'inv'   => '',
        'prod'  => '',
        'lit'   => '<b>'.number_format($sqty,2).'</b>',
        'cart'  => '<b>'.number_format($scart,2).'</b>',
        'rate'  => '',
        'tot'   => '<b>'.number_format($stot,2).'</b>',
        'dis'   => '<b>'.number_format($sdis,2).'</b>',
        'ft'    => '<b>'.number_format($sft,2).'</b>',
        'st'    => '<b>'.number_format($sst,2).'</b>',
        'ntot'  => '<b>'.number_format($sntot,2).'</b>'
    );
}

/* -----------------------------------------------------
   NOW ADD GRAND TOTAL (ALWAYS SHOWS CORRECTLY)
-----------------------------------------------------*/
$data[] = array(
    'num'  => '',
    'dte'  => '',
    'comp' => '<b>GRAND TOTAL</b>',
    'inv'  => '',
    'prod' => '',
    'lit'  => '<b>'.number_format($gqty,2).'</b>',
    'cart' => '<b>'.number_format($gcart,2).'</b>',
    'rate' => '',
    'tot'  => '<b>'.number_format($gtot,2).'</b>',
    'dis'  => '<b>'.number_format($gdis,2).'</b>',
    'ft'   => '<b>'.number_format($gft,2).'</b>',
    'st'   => '<b>'.number_format($gst,2).'</b>',
    'ntot' => '<b>'.number_format($gntot,2).'</b>'
);

$cols = array(
    'num'=>'<b>S. No</b>',
    'dte'=>'<b>Date</b>',
  
    'comp'=>'<b>Company</b>',
    'inv'=>'<b>Invoice#</b>',
      'prod'=>'<b>Product</b>',
    'lit'=>'<b>QTY (LIT)</b>',
  'cart'=>'<b>QTY (CTN)</b>',
  'rate'=>'<b>Rate</b>',
  
    'tot'=>'<b>Total</b>',
    'dis'=>'<b>Discount</b>',
    'ft'=>'<b>FED Tax</b>',
    'st'=>'<b>Sales Tax</b>',
    'ntot'=>'<b>Net Total</b>'
    );


$pdf->ezTable(
    $data,
    $cols,
    '',
    array(
        'xPos'=>20,
        'xOrientation'=>'right',
        'fontSize'=>8,
        'width'=>680,
        'showHeadings'=>1,
        'shaded'=>0,
        'showLines'=>2,
        'cols'=>array(
            'num'=>array('justification'=>'center','width'=>38),
            'dte'=>array('justification'=>'center','width'=>50),
          
            'comp'=>array('justification'=>'left','width'=>105),
            'inv'=>array('justification'=>'center','width'=>50),
                'prod'=>array('justification'=>'left','width'=>140),
                  'lit'=>array('justification'=>'center','width'=>53),
                    'cart'=>array('justification'=>'center','width'=>55),
                      'rate'=>array('justification'=>'center','width'=>50),
            'tot'=>array('justification'=>'center','width'=>57),
            'dis'=>array('justification'=>'center','width'=>55),
            'ft'=>array('justification'=>'center','width'=>55),
            'st'=>array('justification'=>'center','width'=>55),
            'ntot'=>array('justification'=>'center','width'=>58),
         
            
        )
    )
);

//$tamt1=number_format($tamt,1);

//$pdf->ezSetDy(-20);
//$pdf->ezText("<b>Total Purchases = </b> $tamt1", 10, array("justification" => 'left'));

}



else
{
$pdf->addText(20,500,11,"<b>Company = </b>      $cmp");

$pdf->addText(20,485,11,"<b>From = </b>            $fdate <b> To: </b> $tdate ");

$count=0;
$one=1;
$nResult = MySQLi_Query($link, "Select * from purchases 
where Inv_Date between '$strFrom' and '$strTo' 
and Inv_Type='Dir Purchase' and Company='$cmp' 
order by Inv_Date, Inv_No");

$count = 0;
$data = array();

/* -------------------------
   GRAND TOTAL INITIALIZATION
---------------------------*/
$gtot = $gdis = $gft = $gst = $gntot = $gqty = $gcart = 0;

/* -------------------------
   SUB TOTAL INITIALIZATION
---------------------------*/
$inv_prev = "";
$stot = $sdis = $sft = $sst = $sntot = $sqty = $scart = 0;

while($rstRow = mysqli_fetch_array($nResult))
{
    $inv_curr = $rstRow['Inv_No'];

    // -------------------------
    // WHEN INVOICE CHANGES → PRINT SUBTOTAL
    // -------------------------
    if($inv_prev != "" && $inv_prev != $inv_curr)
    {
        $data[] = array(
            'num'   => '',
            'dte'   => '',
            'comp'  => '<b>Sub Total</b>',
            'inv'   => '',
            'prod'  => '',
            'lit'   => '<b>'.number_format($sqty,2).'</b>',
            'cart'  => '<b>'.number_format($scart,2).'</b>',
            'rate'  => '',
            'tot'   => '<b>'.number_format($stot,2).'</b>',
            'dis'   => '<b>'.number_format($sdis,2).'</b>',
            'ft'    => '<b>'.number_format($sft,2).'</b>',
            'st'    => '<b>'.number_format($sst,2).'</b>',
            'ntot'  => '<b>'.number_format($sntot,2).'</b>'
        );

        // reset sub totals
        $stot = $sdis = $sft = $sst = $sntot = $sqty = $scart = 0;
    }

    // -------------------------
    // ADD TO SUBTOTAL
    // -------------------------
    $sqty  += $rstRow['Qty'];
    $scart += $rstRow['Carton'];
    $stot  += $rstRow['Total'];
    $sdis  += $rstRow['R_Dis'];
    $sft   += $rstRow['F_Amt'];
    $sst   += $rstRow['S_Amt'];
    $sntot += $rstRow['N_Total'];

    // -------------------------
    // ADD TO GRAND TOTAL
    // -------------------------
    $gqty  += $rstRow['Qty'];
    $gcart += $rstRow['Carton'];
    $gtot  += $rstRow['Total'];
    $gdis  += $rstRow['R_Dis'];
    $gft   += $rstRow['F_Amt'];
    $gst   += $rstRow['S_Amt'];
    $gntot += $rstRow['N_Total'];

    // -------------------------
    // NORMAL ROW
    // -------------------------
    $idate = date("d-m-Y", strtotime($rstRow['Inv_Date']));
    $count++;

    $data[] = array(
        'num'   => $count,
        'dte'   => $idate,
        'comp'  => $rstRow['Company'],
        'inv'   => $rstRow['Inv_Det'],
        'prod'  => $rstRow['Name'],
        'lit'   => $rstRow['Qty'],
        'cart'  => $rstRow['Carton'],
        'rate'  => $rstRow['Rate'],
        'tot'   => number_format($rstRow['Total'],2),
        'dis'   => number_format($rstRow['R_Dis'],2),
        'ft'    => number_format($rstRow['F_Amt'],2),
        'st'    => number_format($rstRow['S_Amt'],2),
        'ntot'  => number_format($rstRow['N_Total'],2)
    );

    $inv_prev = $inv_curr;
}

/* -----------------------------------------------------
   ADD LAST INVOICE SUBTOTAL (IMPORTANT)
-----------------------------------------------------*/
if($inv_prev != "")
{
    $data[] = array(
        'num'   => '',
        'dte'   => '',
        'comp'  => '<b>Sub Total</b>',
        'inv'   => '',
        'prod'  => '',
        'lit'   => '<b>'.number_format($sqty,2).'</b>',
        'cart'  => '<b>'.number_format($scart,2).'</b>',
        'rate'  => '',
        'tot'   => '<b>'.number_format($stot,2).'</b>',
        'dis'   => '<b>'.number_format($sdis,2).'</b>',
        'ft'    => '<b>'.number_format($sft,2).'</b>',
        'st'    => '<b>'.number_format($sst,2).'</b>',
        'ntot'  => '<b>'.number_format($sntot,2).'</b>'
    );
}

/* -----------------------------------------------------
   NOW ADD GRAND TOTAL (ALWAYS SHOWS CORRECTLY)
-----------------------------------------------------*/
$data[] = array(
    'num'  => '',
    'dte'  => '',
    'comp' => '<b>GRAND TOTAL</b>',
    'inv'  => '',
    'prod' => '',
    'lit'  => '<b>'.number_format($gqty,2).'</b>',
    'cart' => '<b>'.number_format($gcart,2).'</b>',
    'rate' => '',
    'tot'  => '<b>'.number_format($gtot,2).'</b>',
    'dis'  => '<b>'.number_format($gdis,2).'</b>',
    'ft'   => '<b>'.number_format($gft,2).'</b>',
    'st'   => '<b>'.number_format($gst,2).'</b>',
    'ntot' => '<b>'.number_format($gntot,2).'</b>'
);

$cols = array(
    'num'=>'<b>S. No</b>',
    'dte'=>'<b>Date</b>',
  
    'comp'=>'<b>Company</b>',
    'inv'=>'<b>Invoice#</b>',
      'prod'=>'<b>Product</b>',
    'lit'=>'<b>QTY (LIT)</b>',
  'cart'=>'<b>QTY (CTN)</b>',
  'rate'=>'<b>Rate</b>',
  
    'tot'=>'<b>Total</b>',
    'dis'=>'<b>Discount</b>',
    'ft'=>'<b>FED Tax</b>',
    'st'=>'<b>Sales Tax</b>',
    'ntot'=>'<b>Net Total</b>'
    );


$pdf->ezTable(
    $data,
    $cols,
    '',
    array(
        'xPos'=>20,
        'xOrientation'=>'right',
        'fontSize'=>8,
        'width'=>680,
        'showHeadings'=>1,
        'shaded'=>0,
        'showLines'=>2,
        'cols'=>array(
            'num'=>array('justification'=>'center','width'=>38),
            'dte'=>array('justification'=>'center','width'=>50),
          
            'comp'=>array('justification'=>'left','width'=>105),
            'inv'=>array('justification'=>'center','width'=>50),
                'prod'=>array('justification'=>'left','width'=>140),
                  'lit'=>array('justification'=>'center','width'=>53),
                    'cart'=>array('justification'=>'center','width'=>55),
                      'rate'=>array('justification'=>'center','width'=>50),
            'tot'=>array('justification'=>'center','width'=>57),
            'dis'=>array('justification'=>'center','width'=>55),
            'ft'=>array('justification'=>'center','width'=>55),
            'st'=>array('justification'=>'center','width'=>55),
            'ntot'=>array('justification'=>'center','width'=>58),
         
            
        )
    )
);


}



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



