<?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(330,545,12,"<b>(SALES REPORT SALESMAN-WISE)</b>");

$usr=$_GET['usr'];
$strFrom=$_POST['strFrom'];
$strTo=$_POST['strTo'];
$sman=$_POST['sman']; 
$prod=$_POST['prov'];



if ($strFrom=='')
{
$nResult = MySQLi_Query($link, "select MIN(Inv_Date) as dte from cust_sales");
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(!$prod)
{
$pdf->addText(20,500,11,"<b>Salesman = </b>     $sman");

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

$count=0;
$one=1;
$nResult = MySQLi_Query($link, "Select * from cust_sales where Inv_Date between '$strFrom' and '$strTo' and Salesman='$sman' order by Inv_Date, Inv_No  ");
while($rstRow = mysqli_fetch_array($nResult))
{
$tcart=$rstRow['Carton']+$tcart;
$tamt=$rstRow['N_Total']+$tamt;

$idate=$rstRow['Inv_Date'];
$idate=strtotime($idate);
$idate = date("d-m-Y", $idate);

$inv=$rstRow['Inv_No'];
$invn=$rstRow['Inv_N'];
$invd=$invn."-".$inv;

if($invn=='SIB')
{
$sts='Sales';
}
if($invn=='SIR')
{
$sts='Sales Return';
}

$count=$one+$count;
$data[] = array('num'=>$count,'dte'=>$idate,'inv'=>$invd,'cust'=>$rstRow['Customer'],'name'=>$rstRow['Name'],'lit'=>$rstRow['Qty'],'cart'=>$rstRow['Carton'],'rate'=>$rstRow['Rate'],'tot'=>$rstRow['N_Total'],'sman1'=>$rstRow['Warehouse'],'stpe'=>$sts);
}
$cols = array('num'=>'<b>S. No</b>','dte'=>'<b>Date</b>','inv'=>'<b>Invoice#</b>','cust'=>'<b>Customer Name</b>','name'=>'<b>Product Name</b>','lit'=>'<b>Liters</b>','cart'=>'<b>Cartons</b>','rate'=>'<b>Price</b>','tot'=>'<b>Amount</b>','sman1'=>'<b>Warehouse</b>','stpe'=>'<b>Inv-Type</b>');
$pdf->ezTable($data,$cols,'',
        array('xPos'=>20,'xOrientation'=>'right','fontSize' => 9,'width'=>680,'showHeadings'=>1,'shaded'=>0, 'showLines'=>2,'cols'=>array(
                'num'=>array('justification'=>'center','width'=>38),
                 'dte'=>array('justification'=>'center','width'=>55),
                 'inv'=>array('justification'=>'center','width'=>60),
                 'cust'=>array('justification'=>'left','width'=>140),
                 'name'=>array('justification'=>'left','width'=>148),
                'lit'=>array('justification'=>'right','width'=>50),
                'cart'=>array('justification'=>'right','width'=>50),
                'rate'=>array('justification'=>'right','width'=>50),
                'tot'=>array('justification'=>'right','width'=>60),
                  'sman1'=>array('justification'=>'left','width'=>70),
                   'stpe'=>array('justification'=>'left','width'=>70),
              

                  )));
$tcart1=number_format($tcart,1);
$tamt1=number_format($tamt,1);

$pdf->ezSetDy(-20);
$pdf->ezText("<b>Total Cartons Sold = </b>  $tcart1", 10, array("justification" => 'left'));
$pdf->ezSetDy(-10);
$pdf->ezText("<b>Total Sales Amount = </b> $tamt1", 10, array("justification" => 'left'));

}



else
{
$pdf->addText(20,485,11,"<b>Product Name = </b>  $prod");
$pdf->addText(20,500,11,"<b>Salesman = </b>          $sman");

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

$count=0;
$one=1;
$nResult = MySQLi_Query($link, "Select * from cust_sales where Inv_Date between '$strFrom' and '$strTo' and Name='$prod' and Salesman='$sman' order by Inv_Date, Inv_No  ");
while($rstRow = mysqli_fetch_array($nResult))
{
$tcart=$rstRow['Carton']+$tcart;
$tamt=$rstRow['N_Total']+$tamt;

$idate=$rstRow['Inv_Date'];
$idate=strtotime($idate);
$idate = date("d-m-Y", $idate);

$inv=$rstRow['Inv_No'];
$invn=$rstRow['Inv_N'];
$invd=$invn."-".$inv;

if($invn=='SIB')
{
$sts='Sales';
}
if($invn=='SIR')
{
$sts='Sales Return';
}

$count=$one+$count;
$data[] = array('num'=>$count,'dte'=>$idate,'inv'=>$invd,'cust'=>$rstRow['Customer'],'name'=>$rstRow['Warehouse'],'lit'=>$rstRow['Qty'],'cart'=>$rstRow['Carton'],'rate'=>$rstRow['Rate'],'tot'=>$rstRow['N_Total'],'sman1'=>$rstRow['Salesman'],'stpe'=>$sts);
}
$cols = array('num'=>'<b>S. No</b>','dte'=>'<b>Date</b>','inv'=>'<b>Invoice#</b>','cust'=>'<b>Customer Name</b>','name'=>'<b>Target Warehouse</b>','lit'=>'<b>Liters</b>','cart'=>'<b>Cartons</b>','rate'=>'<b>Price</b>','tot'=>'<b>Amount</b>','sman1'=>'<b>Salesman</b>','stpe'=>'<b>Inv-Type</b>');
$pdf->ezTable($data,$cols,'',
        array('xPos'=>20,'xOrientation'=>'right','fontSize' => 9,'width'=>680,'showHeadings'=>1,'shaded'=>0, 'showLines'=>2,'cols'=>array(
                'num'=>array('justification'=>'center','width'=>38),
                 'dte'=>array('justification'=>'center','width'=>55),
                 'inv'=>array('justification'=>'center','width'=>60),
                 'cust'=>array('justification'=>'left','width'=>140),
                 'name'=>array('justification'=>'left','width'=>148),
                'lit'=>array('justification'=>'right','width'=>50),
                'cart'=>array('justification'=>'right','width'=>50),
                'rate'=>array('justification'=>'right','width'=>50),
                'tot'=>array('justification'=>'right','width'=>55),
                  'sman1'=>array('justification'=>'left','width'=>100),
                   'stpe'=>array('justification'=>'left','width'=>70),
              

                  )));
$tcart1=number_format($tcart,1);
$tamt1=number_format($tamt,1);

$pdf->ezSetDy(-20);
$pdf->ezText("<b>Total Cartons Sold = </b>  $tcart1", 10, array("justification" => 'left'));
$pdf->ezSetDy(-10);
$pdf->ezText("<b>Total Sales Amount = </b> $tamt1", 10, array("justification" => 'left'));

}




$pdf->ezSetDy(-30);


$pdf->ezText("<b>Owner GLAKB</b>", 12, array("justification" => 'right'));
ob_clean();
$pdf->ezStream();
?>



