<?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(220,755,12,"<b>(LIKEAGE STOCK REPORT)</b>");
$pdf->ezSetDy(-110);

$comp=$_POST['comp']; 
$ware=$_POST['ware']; 
$strFrom=$_POST['strFrom'];
$strTo=$_POST['strTo'];
$opt=$_POST['strn'];




if ($strFrom=='')
{
$nResult = MySQLi_Query($link, "select MIN(Inv_Date) as dte from purchases");
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(3,710,15,"___________________________________________________________________________________________________");

$pdf->addText(222,740,12,"<b>From $fdate to $tdate</b>");




/*

*/

if($opt=='oall')
{
$pdf->addText(30,715,12,"<b>Warehouse:</b>   $ware");
$pdf->addText(460,715,12,"<b>Overall Report</b>");

$nResult1 = mysqli_query($link, "SELECT Inv_Date, D_No, Name, Carton_Qty, Unit, sum(Qty) as tlit, sum(Tqty) as tpak, sum(Carton) as tcart  FROM purchases 
    WHERE Inv_Date BETWEEN '$strFrom' AND '$strTo' 
      AND Warehouse='$ware' and Inv_N='Likeage Stock' group by Name order by Name, D_No
");

$tlit1 = 0;
$tpak1 = 0;
$tcart1 = 0;
$tlit2 = 0;
$tpak2 = 0;
$tcart2 = 0;

while($rstRow3 = mysqli_fetch_array($nResult1))
{
$count++;
$idate=$rstRow3['Inv_Date'];
$idate=strtotime($idate);
$idate = date("d-m-Y", $idate);
$tlit1=abs($rstRow3['tlit']);
$tpak1=abs($rstRow3['tpak']);
$tcart1=abs($rstRow3['tcart']);
$tlit2 = $tlit2 + $rstRow3['tlit'] * -1;
$tpak2 = $tpak2 + $rstRow3['tpak'] * -1;
$tcart2 = $tcart2 + $rstRow3['tcart'] * -1;



$data[] = array('num'=>$count,'dte'=>$idate,'tpe'=>$rstRow3['D_No'],'des'=>$rstRow3['Name'],'cart'=>$rstRow3['Carton_Qty'],'unit'=>$rstRow3['Unit']."L",'sin'=>number_format($tlit1,2),'sout'=>number_format($tpak1,2),'bal'=>number_format($tcart1,2));
}

$cols = array('num'=>'<b>S. No</b>','dte'=>'<b>Date</b>','tpe'=>'<b>Ref. #</b>','des'=>'<b>Product Name</b>','cart'=>'<b>Unit/Carton</b>','unit'=>'<b>SKU</b>','sin'=>'<b>Qty (LIT)</b>','sout'=>'<b>Qty (PCK)</b>','bal'=>'<b>Qty (CTN)</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'=>52),
                   'tpe'=>array('justification'=>'center','width'=>45),
                 'des'=>array('justification'=>'left','width'=>180),
                  'cart'=>array('justification'=>'center','width'=>57), 
                  'unit'=>array('justification'=>'center','width'=>45),
               
                'sin'=>array('justification'=>'center','width'=>53),
                'sout'=>array('justification'=>'center','width'=>53),
                   
                 'bal'=>array('justification'=>'center','width'=>53)
                     
                 
             )));


}



if($opt=='dall')
{

$pdf->addText(30,715,12,"<b>Warehouse:</b>   $ware");
$pdf->addText(460,715,12,"<b>Detailed Report</b>");

$nResult1 = mysqli_query($link, "SELECT Inv_Date, D_No, Name, Carton_Qty, Unit, Qty, Tqty, Carton FROM purchases 
    WHERE Inv_Date BETWEEN '$strFrom' AND '$strTo' 
      AND Warehouse='$ware' and Inv_N='Likeage Stock' order by Inv_Date, D_No
");
$tlit1 = 0;
$tpak1 = 0;
$tcart1 = 0;
$tlit2 = 0;
$tpak2 = 0;
$tcart2 = 0;

while($rstRow3 = mysqli_fetch_array($nResult1))
{
$count++;
$idate=$rstRow3['Inv_Date'];
$idate=strtotime($idate);
$idate = date("d-m-Y", $idate);
$tlit1=abs($rstRow3['Qty']);
$tpak1=abs($rstRow3['Tqty']);
$tcart1=abs($rstRow3['Carton']);
$tlit2 = $tlit2 + $rstRow3['Qty'] * -1;
$tpak2 = $tpak2 + $rstRow3['Tqty'] * -1;
$tcart2 = $tcart2 + $rstRow3['Carton'] * -1;



$data[] = array('num'=>$count,'dte'=>$idate,'tpe'=>$rstRow3['D_No'],'des'=>$rstRow3['Name'],'cart'=>$rstRow3['Carton_Qty'],'unit'=>$rstRow3['Unit']."L",'sin'=>number_format($tlit1,2),'sout'=>number_format($tpak1,2),'bal'=>number_format($tcart1,2));
}

$cols = array('num'=>'<b>S. No</b>','dte'=>'<b>Date</b>','tpe'=>'<b>Ref. #</b>','des'=>'<b>Product Name</b>','cart'=>'<b>Unit/Carton</b>','unit'=>'<b>SKU</b>','sin'=>'<b>Qty (LIT)</b>','sout'=>'<b>Qty (PCK)</b>','bal'=>'<b>Qty (CTN)</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'=>52),
                   'tpe'=>array('justification'=>'center','width'=>45),
                 'des'=>array('justification'=>'left','width'=>180),
                  'cart'=>array('justification'=>'center','width'=>57), 
                  'unit'=>array('justification'=>'center','width'=>45),
               
                'sin'=>array('justification'=>'center','width'=>53),
                'sout'=>array('justification'=>'center','width'=>53),
                   
                 'bal'=>array('justification'=>'center','width'=>53)
                     
                 
             )));

}


$tlit2 = number_format($tlit2,2);
$tpak2 = number_format($tpak2,2);
$tcart2 = number_format($tcart2,2);



$pdf->ezSetDy(-10);

$pdf->ezText("<b>Grand Total :       $tlit2 Liters      ||    $tpak2 Packs     ||     $tcart2 Cartons </b>" , 11, array("justification" => 'left'));


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



