<?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>(COMPLETE SALES INFORMATION)</b>");

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



if ($strFrom=='')
{
$nResult = MySQLi_Query($link, "select MIN(Inv_Date) as dte from s_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(!$city && !$prov )
{
$pdf->addText(20,500,12,"<b>Overall Sales</b>");

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

$count=0;
$one=1;
$nResult = MySQLi_Query($link, "Select * from s_details where Inv_Date between '$strFrom' and '$strTo' order by Inv_Date, Inv_No  ");
while($rstRow = mysqli_fetch_array($nResult))
{
$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'],'itype'=>$rstRow['Inv_Type'],'tot'=>$rstRow['Total'],'lit'=>$rstRow['Dis'],'ntot'=>$rstRow['N_Total'],'ware'=>$rstRow['Warehouse'],'city'=>$rstRow['City'],'sman'=>$rstRow['Salesman'],'mode'=>$rstRow['Pmode'],'stpe'=>$sts);
}
$cols = array('num'=>'<b>S. No</b>','dte'=>'<b>Date</b>','inv'=>'<b>Invoice#</b>','cust'=>'<b>Customer Name</b>','itype'=>'<b>Customer Type</b>','tot'=>'<b>Total</b>','lit'=>'<b>Discount</b>','ntot'=>'<b>Net Total</b>','ware'=>'<b>Warehouse</b>','city'=>'<b>City</b>','sman'=>'<b>Salesman</b>','mode'=>'<b>P-Mode</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'=>130),
                 
                 'itype'=>array('justification'=>'left','width'=>50),
                'tot'=>array('justification'=>'right','width'=>55),
                 
                'lit'=>array('justification'=>'right','width'=>45),
                'ntot'=>array('justification'=>'right','width'=>55),
                'ware'=>array('justification'=>'center','width'=>55),
                  'city'=>array('justification'=>'center','width'=>80),
                'sman'=>array('justification'=>'left','width'=>80),
                     'mode'=>array('justification'=>'center','width'=>50),
                   'stpe'=>array('justification'=>'left','width'=>57),
              

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

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

}



elseif(!$city)
{
$pdf->addText(20,500,11,"<b>Province = </b>      $prov");

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

$count=0;
$one=1;
$nResult = MySQLi_Query($link, "Select * from s_details where Inv_Date between '$strFrom' and '$strTo' and Province='$prov' order by Inv_Date, Inv_No  ");
while($rstRow = mysqli_fetch_array($nResult))
{
$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'],'itype'=>$rstRow['Inv_Type'],'tot'=>$rstRow['Total'],'lit'=>$rstRow['Dis'],'ntot'=>$rstRow['N_Total'],'ware'=>$rstRow['Warehouse'],'city'=>$rstRow['City'],'sman'=>$rstRow['Salesman'],'mode'=>$rstRow['Pmode'],'stpe'=>$sts);
}
$cols = array('num'=>'<b>S. No</b>','dte'=>'<b>Date</b>','inv'=>'<b>Invoice#</b>','cust'=>'<b>Customer Name</b>','itype'=>'<b>Customer Type</b>','tot'=>'<b>Total</b>','lit'=>'<b>Discount</b>','ntot'=>'<b>Net Total</b>','ware'=>'<b>Warehouse</b>','city'=>'<b>City</b>','sman'=>'<b>Salesman</b>','mode'=>'<b>P-Mode</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'=>130),
                 
                 'itype'=>array('justification'=>'left','width'=>50),
                'tot'=>array('justification'=>'right','width'=>55),
                 
                'lit'=>array('justification'=>'right','width'=>45),
                'ntot'=>array('justification'=>'right','width'=>55),
                'ware'=>array('justification'=>'center','width'=>55),
                  'city'=>array('justification'=>'center','width'=>80),
                'sman'=>array('justification'=>'left','width'=>80),
                     'mode'=>array('justification'=>'center','width'=>50),
                   'stpe'=>array('justification'=>'left','width'=>57),
              

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

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

}




elseif(!$prov)
{
$pdf->addText(20,500,11,"<b>City = </b>      $city");

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

$count=0;
$one=1;
$nResult = MySQLi_Query($link, "Select * from s_details where Inv_Date between '$strFrom' and '$strTo' and City='$city' order by Inv_Date, Inv_No  ");
while($rstRow = mysqli_fetch_array($nResult))
{
$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'],'itype'=>$rstRow['Inv_Type'],'tot'=>$rstRow['Total'],'lit'=>$rstRow['Dis'],'ntot'=>$rstRow['N_Total'],'ware'=>$rstRow['Warehouse'],'city'=>$rstRow['Province'],'sman'=>$rstRow['Salesman'],'mode'=>$rstRow['Pmode'],'stpe'=>$sts);
}
$cols = array('num'=>'<b>S. No</b>','dte'=>'<b>Date</b>','inv'=>'<b>Invoice#</b>','cust'=>'<b>Customer Name</b>','itype'=>'<b>Customer Type</b>','tot'=>'<b>Total</b>','lit'=>'<b>Discount</b>','ntot'=>'<b>Net Total</b>','ware'=>'<b>Warehouse</b>','city'=>'<b>Province</b>','sman'=>'<b>Salesman</b>','mode'=>'<b>P-Mode</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'=>130),
                 
                 'itype'=>array('justification'=>'left','width'=>50),
                'tot'=>array('justification'=>'right','width'=>55),
                 
                'lit'=>array('justification'=>'right','width'=>45),
                'ntot'=>array('justification'=>'right','width'=>55),
                'ware'=>array('justification'=>'center','width'=>55),
                  'city'=>array('justification'=>'center','width'=>80),
                'sman'=>array('justification'=>'left','width'=>80),
                     'mode'=>array('justification'=>'center','width'=>50),
                   'stpe'=>array('justification'=>'left','width'=>57),
              

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

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

}



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

$pdf->addText(20,485,11,"<b>City = </b>              $city");

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

$count=0;
$one=1;
$nResult = MySQLi_Query($link, "Select * from s_details where Inv_Date between '$strFrom' and '$strTo' and City='$city' and Province='$prov' order by Inv_Date, Inv_No  ");
while($rstRow = mysqli_fetch_array($nResult))
{
$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'],'itype'=>$rstRow['Inv_Type'],'tot'=>$rstRow['Total'],'lit'=>$rstRow['Dis'],'ntot'=>$rstRow['N_Total'],'ware'=>$rstRow['Warehouse'],'sman'=>$rstRow['Salesman'],'mode'=>$rstRow['Pmode'],'stpe'=>$sts);
}
$cols = array('num'=>'<b>S. No</b>','dte'=>'<b>Date</b>','inv'=>'<b>Invoice#</b>','cust'=>'<b>Customer Name</b>','itype'=>'<b>Customer Type</b>','tot'=>'<b>Total</b>','lit'=>'<b>Discount</b>','ntot'=>'<b>Net Total</b>','ware'=>'<b>Warehouse</b>','sman'=>'<b>Salesman</b>','mode'=>'<b>P-Mode</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'=>130),
                 
                 'itype'=>array('justification'=>'left','width'=>65),
                'tot'=>array('justification'=>'right','width'=>60),
                 
                'lit'=>array('justification'=>'right','width'=>50),
                'ntot'=>array('justification'=>'right','width'=>60),
                'ware'=>array('justification'=>'center','width'=>60),
                  
                'sman'=>array('justification'=>'left','width'=>90),
                     'mode'=>array('justification'=>'center','width'=>60),
                   'stpe'=>array('justification'=>'left','width'=>67),
              

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

$pdf->ezSetDy(-20);
$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();
?>



