<?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);
//$pdf->addText(18,760,8,"MIS GENERATED REPORT");
$date1=Date("j-n-Y");
$pdf->ezSetDy(-80);
$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(195,755,12,"<b>(RAW MATERIAL STOCK REPORT)</b>");
$usr=$_GET['usr']; 
$frm=$_POST['strFrom']; 
$to=$_POST['strTo'];
$val=$_POST['strn']; 


if ($frm=='')
{
$nResult = MySQLi_Query($link, "select MIN(Inv_Date) as dte from raw_purchases ");
while($rstRow = mysqli_fetch_array($nResult))
{
$frm=$rstRow['dte'];
}
}


$frm=date('Y-m-d',strtotime($frm));
$to=date('Y-m-d',strtotime($to));


$frm1=date('d-m-Y',strtotime($frm));
$to1=date('d-m-Y',strtotime($to));



$result42 = mysqli_query($link,"delete from temp_stock where addby='$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");



//$pdf->addText(22, 730, 11, "<b>Raw Material Name:</b>   $cname");



if($val=='oa')
{
$date1=Date("j-n-Y");
$pdf->addText(260,740,11,"<b>(As on $date1)</b>");

$rst6 = MySQLi_Query($link, "Select Category, Name, SKU, SUM(Qty) as stk from raw_purchases group by Category, Name order by Category, Name");
while($rstRow = mysqli_fetch_array($rst6))
{
InsertRec("temp_stock",array(
"Category" => $rstRow['Category'],
"Name" =>$rstRow['Name'],
"SKU" => $rstRow['SKU'],
"S_In" => $rstRow['stk'],
"addby" => $usr,

));
}


$rst7 = MySQLi_Query($link, "Select Category, Name, SKU, SUM(Qty) as stk from raw_sales group by Category, Name Order by Category, Name");
while($rstRow = mysqli_fetch_array($rst7))
{
InsertRec("temp_stock",array(
"Category" => $rstRow['Category'],
"Name" =>$rstRow['Name'],
"SKU" => $rstRow['SKU'],
"S_Out" => $rstRow['stk'],
"addby" => $usr,

));
}




$count=0;
$ntot=0;
$one=1;
$bal=0;
$rst1 = MySQLi_Query($link, "Select Category, Name, SKU, SUM(S_In) as stkin, SUM(S_Out) as stkout,  SUM(S_In)-SUM(S_Out) as stk from temp_stock where addby='$usr' group by Category, Name ");
while($rstRow = mysqli_fetch_array($rst1))
{

$count=$one+$count;
//$idate=strtotime($rstRow['D_Date']);
//$idate = date("d-m-Y", $idate);
//$docno=$rstRow['Inv_Type']."--".$rstRow['Inv_No'];
$bal=$rstRow['stk'];

$data[] = array('num'=>$count,'cat'=>$rstRow['Category'],'prod'=>$rstRow['Name'],'size'=>$rstRow['SKU'],'sin'=>number_format($rstRow['stkin']),'sout'=>number_format($rstRow['stkout']),'bal'=>number_format($bal));

}
$cols = array('num'=>'<b>S. No</b>','cat'=>'<b>Category</b>','prod'=>'<b>Product Name</b>','size'=>'<b>SKU</b>','sin'=>'<b>Purchases</b>','sout'=>'<b>Qty Issued</b>','bal'=>'<b>Balance</b>');

$pdf->ezTable($data,$cols,'',
        array('xPos'=>20,'xOrientation'=>'right','fontSize' => 10,'width'=>680,'showHeadings'=>1,'shaded'=>0, 'showLines'=>2,'cols'=>array(
                  'num'=>array('justification'=>'center','width'=>35),
                   'cat'=>array('justification'=>'center','width'=>55),
                'prod'=>array('justification'=>'left','width'=>200),
                 'size'=>array('justification'=>'center','width'=>60),
                 'sin'=>array('justification'=>'right','width'=>70),
                 
                'sout'=>array('justification'=>'right','width'=>70),
                   
                 'bal'=>array('justification'=>'right','width'=>70)
                
                    
             )));
}


if($val=='dw')
{
$pdf->ezSetDy(-10);
$pdf->addText(22, 730, 10, "<b>From:</b>   $frm1 <b>To: </b> $to1");
$rst6 = MySQLi_Query($link, "Select Category, Name, Size, SUM(Qty) as stk from raw_purchases where Inv_Date between '$frm' and '$to' group by Category, Name,  Size");
while($rstRow = mysqli_fetch_array($rst6))
{
InsertRec("temp_stock",array(
"Category" => $rstRow['Category'],
"Name" =>$rstRow['Name'],
"Size" => $rstRow['Size'],
"S_In" => $rstRow['stk'],
"addby" => $usr,

));
}


$rst7 = MySQLi_Query($link, "Select Category, Name, Size, SUM(Qty) as stk from raw_sales where Inv_Date between '$frm' and '$to' group by Category, Name,  Size");
while($rstRow = mysqli_fetch_array($rst7))
{
InsertRec("temp_stock",array(
"Category" => $rstRow['Category'],
"Name" =>$rstRow['Name'],
"Size" => $rstRow['Size'],
"S_Out" => $rstRow['stk'],
"addby" => $usr,

));
}




$count=0;
$ntot=0;
$one=1;
$bal=0;
$rst1 = MySQLi_Query($link, "Select Category, Name, Size, SUM(S_In) as stkin, SUM(S_Out) as stkout,  SUM(S_In)-SUM(S_Out) as stk from temp_stock where addby='$usr' group by Category, Name, Size ");
while($rstRow = mysqli_fetch_array($rst1))
{

$count=$one+$count;
//$idate=strtotime($rstRow['D_Date']);
//$idate = date("d-m-Y", $idate);
//$docno=$rstRow['Inv_Type']."--".$rstRow['Inv_No'];
$bal=$rstRow['stk'];

$data[] = array('num'=>$count,'cat'=>$rstRow['Category'],'prod'=>$rstRow['Name'],'size'=>$rstRow['Size'],'sin'=>$rstRow['stkin'],'sout'=>$rstRow['stkout'],'bal'=>number_format($bal));

}
$cols = array('num'=>'<b>S. No</b>','cat'=>'<b>Category</b>','prod'=>'<b>Product Name</b>','size'=>'<b>Size</b>','sin'=>'<b>Purchases</b>','sout'=>'<b>Qty Issued</b>','bal'=>'<b>Balance</b>');

$pdf->ezTable($data,$cols,'',
        array('xPos'=>20,'xOrientation'=>'right','fontSize' => 10,'width'=>680,'showHeadings'=>1,'shaded'=>0, 'showLines'=>2,'cols'=>array(
                  'num'=>array('justification'=>'center','width'=>35),
                   'cat'=>array('justification'=>'center','width'=>55),
                'prod'=>array('justification'=>'left','width'=>160),
                 'size'=>array('justification'=>'left','width'=>125),
                 'sin'=>array('justification'=>'right','width'=>60),
                 
                'sout'=>array('justification'=>'right','width'=>60),
                   
                 'bal'=>array('justification'=>'right','width'=>60)
                
                    
             )));
}

$pdf->ezSetDy(-15);



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