<?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,750,70);
$date1=Date("j-n-Y");
$pdf->ezSetDy(-100);
$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(270,780,13,"<b>(GLAKB)</b>");
//$pdf->addText(215,755,12,"<b>(PRODUCTS STOCK REPORT)</b>");

$usr=$_GET['usr'];



$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");

$result42 = mysqli_query($link,"delete from stock");
$rst1 = MySQLi_Query($link, "Select Sufix, Item_Code, Full_Name, Unit_Liter from products order by Article");
while($rstRow = mysqli_fetch_array($rst1))
{
$code1=$rstRow['Sufix']."-".$rstRow['Item_Code'];
InsertRec("stock",array(
"Code" => $code1,
"Name" =>$rstRow['Full_Name'],
"SKU" => $rstRow['Unit_Liter'],
"addby" => $usr,

));
}


$pdf->addText(20,725,12,"<b>STOCK OPENING BALANCE REPORT</b>");
//$pdf->addText(20,705,11,"<b>From = </b>            $fdate <b> To: </b> $tdate ");
$pdf->addText(410,720,11,"<b>WAREHOUSES</b>");


$rst2 = MySQLi_Query($link, "Select Name from stock order by Name");
while($rstRow = mysqli_fetch_array($rst2))
{
$fname=$rstRow['Name'];


$rst3 = MySQLi_Query($link, "Select Name, sum(Carton) as sctn, SUM(Qty) as tlit  from purchases where Name='$fname' and Inv_Type='Op. Balance' and Warehouse='Karachi'  group by Name");
while($rstRow = mysqli_fetch_array($rst3))
{
$sctn=$rstRow['sctn'];
$tlit=$rstRow['tlit'];

$result = mysqli_query($link, "update stock set K_Sin='$sctn', K_Sout='$tlit' where Name = '$fname' ");
}

$rst4 = MySQLi_Query($link, "Select Name, sum(Carton) as sctn, SUM(Qty) as tlit from purchases where Name='$fname' and Inv_Type='Op. Balance' and Warehouse='Lahore'  group by Name");
while($rstRow = mysqli_fetch_array($rst4))
{
$sctn=$rstRow['sctn'];
$tlit=$rstRow['tlit'];
$result = mysqli_query($link, "update stock set L_Sin='$sctn', L_Sout='$tlit' where Name = '$fname' ");
}

$rst5 = MySQLi_Query($link, "Select Name, sum(Carton) as sctn, SUM(Qty) as tlit from purchases where Name='$fname' and Inv_Type='Op. Balance' and Warehouse='Quetta'  group by Name");
while($rstRow = mysqli_fetch_array($rst5))
{
$sctn=$rstRow['sctn'];
$tlit=$rstRow['tlit'];

$result = mysqli_query($link, "update stock set Q_Sin='$sctn', Q_Sout='$tlit' where Name = '$fname' ");
}



}



$count=0;
$one=1;
$nResult = MySQLi_Query($link, "Select Code, Name, SKU, K_Sin, K_Sout, L_Sin, L_Sout, Q_Sin, Q_Sout from stock group by Name order by Id");
while($rstRow = mysqli_fetch_array($nResult))
{

$count=$one+$count;
$data[] = array('num'=>$count,'name'=>$rstRow['Name'],'sku'=>$rstRow['SKU'],'kstk'=>$rstRow['K_Sin'],'kstk1'=>$rstRow['K_Sout'],'lstk'=>$rstRow['L_Sin'],'lstk1'=>$rstRow['L_Sout'],'qsin'=>$rstRow['Q_Sin'],'qsin1'=>$rstRow['Q_Sout']);
}
$cols = array('num'=>'<b>S. No</b>','name'=>'<b>Product Full Name</b>','sku'=>'<b>SKU</b>','kstk'=>'<b>KHI (CTN)</b>','kstk1'=>'<b>KHI (LIT)</b>','lstk'=>'<b>LHR (CTN)</b>','lstk1'=>'<b>LHR (LIT)</b>','qsin'=>'<b>QTA (CTN)</b>','qsin1'=>'<b>QTA (LIT)</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),
                 'name'=>array('justification'=>'left','width'=>182),
                 'sku'=>array('justification'=>'center','width'=>40),
                 'kstk'=>array('justification'=>'right','width'=>52),
                'kstk1'=>array('justification'=>'right','width'=>52),
                'lstk'=>array('justification'=>'right','width'=>52),
                'lstk1'=>array('justification'=>'right','width'=>52),
                'qsin'=>array('justification'=>'right','width'=>52),
                'qsin1'=>array('justification'=>'right','width'=>52),
                

                  )));






$pdf->ezSetDy(-30);


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



