<?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(245,755,12,"<b>(PRODUCT LEDGER)</b>");
$pdf->ezSetDy(-150);

$prod=$_POST['item3']; 
$ware=$_POST['sman']; 
$strFrom=$_POST['strFrom'];
$strTo=$_POST['strTo'];

$result421 = mysqli_query($link, "select API, Size, Category, Category2, Carton_Qty, Unit_Liter from products where Full_Name='$prod'");
while($row1  = mysqli_fetch_object($result421))
{
$api=$row1->API;
$size=$row1->Size;
$cat=$row1->Category;
$cat1=$row1->Category2;
$cqty=$row1->Carton_Qty;
$ulit=$row1->Unit_Liter;

}


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,670,15,"___________________________________________________________________________________________________");

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


if(!empty($ware) && !empty($prod))
{
$rep_val = "Warecart";
}



$nResult = mysqli_query($link, "delete from pledger");


switch ($rep_val) {
case "Warecart":


$pdf->addText(30,715,12,"<b>Warehouse:</b>                     $ware");
$pdf->addText(490,695,12,"<b>Unit = Liters</b>");

$pdf->addText(30,695,12,"<b>Product Category:</b>          $cat1 ($cat)");
$pdf->addText(30,675,12,"<b>Product Description:</b>      $prod ($api     $size)");


$nResult1 = mysqli_query($link, "
    SELECT * FROM purchases 
    WHERE Inv_Date BETWEEN '$strFrom' AND '$strTo' 
      AND Warehouse='$ware' and Name='$prod' 
    ORDER BY addon
");


while($rstRow1 = mysqli_fetch_array($nResult1))
                {

InsertRec("pledger",array(
"Inv_No" => $rstRow1['Inv_No'],
"Inv_Det" => $rstRow1['Inv_Det'],

"Inv_Date" =>$rstRow1['Inv_Date'],
"Inv_Type" => $rstRow1['Inv_Type'],
"Description" => $rstRow1['Company'],
"Name" => $rstRow1['Name'],
"S_In" => $rstRow1['Qty'],
"Rate" => $rstRow1['Rate'],
"addon" => $rstRow1['addon'],
"Warehouse" => $rstRow1['Warehouse'],
"Inv_N" => $rstRow1['Inv_N'],

));
}



$nResult2 = mysqli_query($link, "
    SELECT * FROM cust_sales 
    WHERE Inv_Date BETWEEN '$strFrom' AND '$strTo' 
      AND Warehouse='$ware' and Name='$prod' 
    ORDER BY addon
");


while($rstRow2 = mysqli_fetch_array($nResult2))
                {

InsertRec("pledger",array(
"Inv_No" => $rstRow2['Inv_No'],
"Inv_Det" => $rstRow2['Inv_Det'],

"Inv_Date" =>$rstRow2['Inv_Date'],
"Inv_Type" => $rstRow2['Inv_Type'],
"Description" => $rstRow2['Customer'],
"Name" => $rstRow2['Name'],
"S_Out" => $rstRow2['Qty'],
"Rate" => $rstRow2['Rate'],
"addon" => $rstRow2['addon'],
"Warehouse" => $rstRow2['Warehouse'],
"Inv_N" => $rstRow2['Inv_N'],

));
}

$count = 0;
$bal1 = 0;
$nResult3 = mysqli_query($link, "SELECT * FROM pledger ORDER BY Inv_Date, addon");

while($rstRow3 = mysqli_fetch_array($nResult3))
                {
$count++;
$idate=$rstRow3['Inv_Date'];
$idate=strtotime($idate);
$idate = date("d-m-Y", $idate);
$bal1 = $bal1 + $rstRow3['S_In'] - $rstRow3['S_Out'];
$intpe=$rstRow3['Inv_N'];
if($intpe=='PIB')
{
$itype = 'Purchases';    
}
elseif($intpe=='POB')
{
$itype = 'Op. Balance';    
}
elseif($intpe=='Transfer')
{
$itype = 'Transfer';    
}
elseif($intpe=='SIB')
{
$itype = 'Sales';    
}
elseif($intpe=='SIR')
{
$itype = 'Sales Return';    
}


$data[] = array('num'=>$count,'dte'=>$idate,'tpe'=>$itype,'des'=>$rstRow3['Description'],'inv'=>$rstRow3['Inv_Det'],'rate'=>$rstRow3['Rate'],'sin'=>number_format($rstRow3['S_In'],2),'sout'=>number_format($rstRow3['S_Out'],2),'bal'=>number_format($bal1,2));
}

$cols = array('num'=>'<b>S. No</b>','dte'=>'<b>Date</b>','tpe'=>'<b>Invoice Type</b>','des'=>'<b>Company / Customer</b>','inv'=>'<b>Invoice#</b>','rate'=>'<b>Rate</b>','sin'=>'<b>Stock In</b>','sout'=>'<b>Stock Out</b>','bal'=>'<b>Balance</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'=>'left','width'=>60),
                 'des'=>array('justification'=>'left','width'=>150),
                 
                 'inv'=>array('justification'=>'center','width'=>52),
                 'rate'=>array('justification'=>'center','width'=>55),
                'sin'=>array('justification'=>'center','width'=>55),
                'sout'=>array('justification'=>'center','width'=>55),
                   
                 'bal'=>array('justification'=>'center','width'=>55)
                     
                 
             )));




break;

}
$tqty = $bal1 / $ulit;
$tcart = $tqty / $cqty;

$pdf->ezSetDy(-10);

$pdf->ezText("<b>Closing Balance :  </b>     $bal1 Liters      ||    $tqty Packs     ||     $tcart Cartons" , 11, array("justification" => 'left'));


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



