<?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(300,545,12,"<b>(SPECIAL STOCK REPORT WAREHOUSE-WISE)</b>");

$usr=$_GET['usr'];
$strFrom=$_POST['strFrom'];
$strTo=$_POST['strTo'];
$ware=$_POST['sman']; 
$tpe=$_POST['strn']; 
$bval=$_POST['aval']; 


/*
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));

$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 s_stock");
$rst1 = MySQLi_Query($link, "Select Full_Name, Sufix, Item_Code, R_Price, Carton_Qty, Unit_Liter from products order by Article");
while($rstRow = mysqli_fetch_array($rst1))
{
$code1=$rstRow['Sufix'].$rstRow['Item_Code'];
InsertRec("s_stock",array(
"Code" => $code1,
"Name" =>$rstRow['Full_Name'],
"Price" =>$rstRow['R_Price'],
"Carton_Qty" =>$rstRow['Carton_Qty'],
"Unit_Liter" =>$rstRow['Unit_Liter'],

));
}


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

$sin = 0;
$rst3 = MySQLi_Query($link, "Select Name, sum(Carton) as sctn from purchases where Name='$fname' and Inv_Date < '$strFrom' and Warehouse='$ware'  group by Name");
while($rstRow = mysqli_fetch_array($rst3))
{
$sin=$rstRow['sctn'];
}

$sout = 0;
$opn = 0;
$rst6 = MySQLi_Query($link, "Select Name, sum(Carton) as sctn from cust_sales where Name='$fname' and Inv_Date < '$strFrom' and Warehouse='$ware'  group by Name");
while($rstRow = mysqli_fetch_array($rst6))
{
$sout=$rstRow['sctn'];
}

$opn = $sin - $sout;


$tpur=0;
$rst7 = MySQLi_Query($link, "Select Name, sum(Carton) as sctn from purchases where Name='$fname' and Inv_Date between '$strFrom' and '$strTo'  and Warehouse='$ware' and Inv_N!='Transfer'  group by Name");
while($rstRow = mysqli_fetch_array($rst7))
{
$tpur=$rstRow['sctn'];
}

$tsales = 0;
$rst8 = MySQLi_Query($link, "Select Name, sum(Carton) as sctn from cust_sales where Name='$fname' and Inv_Date between '$strFrom' and '$strTo'  and Warehouse='$ware'  group by Name");
while($rstRow = mysqli_fetch_array($rst8))
{
$tsales=$rstRow['sctn'];
}

$t_in = 0;
$rst9 = MySQLi_Query($link, "Select Name, sum(Carton) as ctin from purchases where Name='$fname' and Inv_Date between '$strFrom' and '$strTo' and Warehouse='$ware' and Inv_N='Transfer' and Carton > 0   group by Name");
while($rstRow = mysqli_fetch_array($rst9))
{
$t_in=$rstRow['ctin'];
}

$t_out = 0;
$rst10 = MySQLi_Query($link, "Select Name, sum(Carton) as ctout from purchases where Name='$fname' and Inv_Date between '$strFrom' and '$strTo' and Warehouse='$ware' and Inv_N='Transfer' and Carton < 0   group by Name");
while($rstRow = mysqli_fetch_array($rst10))
{
$t_out=$rstRow['ctout'];
}

$t_out = $t_out * -1; 


$result = mysqli_query($link, "update s_stock set Ob='$opn', Purchase='$tpur', Sale='$tsales', Tin='$t_in', Tout='$t_out' where Name = '$fname' ");

}




if($bval=='yes' and $tpe=='cart')
{
$pdf->addText(20,500,11,"<b>Warehouse = </b> $ware");
$pdf->addText(20,485,11,"<b>Stock in Cartons with Balance Value Included.</b>");

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


$ob=0;
$apr=0;
$count=0;
$one=1;
$tot = 0;
$t_ob = $t_pur = $t_sale = $t_tin = $t_tout = $t_bal = $t_val = 0;

$nResult = MySQLi_Query($link, "Select * from s_stock order by Id  ");
while($rstRow = mysqli_fetch_array($nResult))
{

$count=$one+$count;

$ob = $rstRow['Ob'] + $rstRow['Purchase'] - $rstRow['Sale'] + $rstRow['Tin'] - $rstRow['Tout'];
$apr = $rstRow['Carton_Qty'] * $rstRow['Unit_Liter'] * $ob; 
$tot = $apr * $rstRow['Price'];


$t_ob   += $rstRow['Ob'];
$t_pur  += $rstRow['Purchase'];
$t_sale += $rstRow['Sale'];
$t_tin  += $rstRow['Tin'];
$t_tout += $rstRow['Tout'];
$t_bal  += $ob;
$t_val  += $tot;



$data[] = array('num'=>$count,'cde'=>$rstRow['Code'],'cust'=>$rstRow['Name'],'ob'=>$rstRow['Ob'],'pur'=>$rstRow['Purchase'],'sale'=>$rstRow['Sale'],'tin'=>$rstRow['Tin'],'tout'=>$rstRow['Tout'],'bal'=>$ob,'valu'=>number_format($tot,2));
}


$data[] = array(
    'num' => '',
    'cde' => '',
    'cust' => '<b>Grand Total</b>',
    'ob' => "<b>$t_ob</b>",
    'pur' => "<b>$t_pur</b>",
    'sale' => "<b>$t_sale</b>",
    'tin' => "<b>$t_tin</b>",
    'tout' => "<b>$t_tout</b>",
    'bal' => "<b>$t_bal</b>",
    'valu' => "<b>" . number_format($t_val,2) . "</b>"
);
$cols = array('num'=>'<b>S. No</b>','cde'=>'<b>Code</b>','cust'=>'<b>Product Name</b>','ob'=>'<b>Opening</b>','pur'=>'<b>Purchases</b>','sale'=>'<b>Sale</b>','tin'=>'<b>Transfer In</b>','tout'=>'<b>Transfer Out</b>','bal'=>'<b>Balance</b>','valu'=>'<b>Balance Value</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'=>38),
                 'cde'=>array('justification'=>'center','width'=>55),
                 'cust'=>array('justification'=>'left','width'=>230),
                 'ob'=>array('justification'=>'center','width'=>69),
                 'pur'=>array('justification'=>'center','width'=>69),
                'sale'=>array('justification'=>'center','width'=>69),
                'tin'=>array('justification'=>'center','width'=>69),
                'tout'=>array('justification'=>'center','width'=>69),
                  'bal'=>array('justification'=>'center','width'=>69),
                   'valu'=>array('justification'=>'center','width'=>75),
                

                  )));



}



if($bval!='yes' and $tpe=='cart')
{
$pdf->addText(20,500,11,"<b>Warehouse = </b> $ware");
$pdf->addText(20,485,11,"<b>Stock in Cartons without Balance Value.</b>");

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

$t_ob = $t_pur = $t_sale = $t_tin = $t_tout = $t_bal = $t_val = 0;

$ob=0;
$apr=0;
$count=0;
$one=1;
$tot = 0;
$nResult = MySQLi_Query($link, "Select * from s_stock order by Id  ");
while($rstRow = mysqli_fetch_array($nResult))
{

$count=$one+$count;

$ob = $rstRow['Ob'] + $rstRow['Purchase'] - $rstRow['Sale'] + $rstRow['Tin'] - $rstRow['Tout'];
$apr = $rstRow['Carton_Qty'] * $rstRow['Unit_Liter'] * $ob; 
$tot = $apr * $rstRow['Price'];


$t_ob   += $rstRow['Ob'];
$t_pur  += $rstRow['Purchase'];
$t_sale += $rstRow['Sale'];
$t_tin  += $rstRow['Tin'];
$t_tout += $rstRow['Tout'];
$t_bal  += $ob;
$t_val  += $tot;




$data[] = array('num'=>$count,'cde'=>$rstRow['Code'],'cust'=>$rstRow['Name'],'ob'=>$rstRow['Ob'],'pur'=>$rstRow['Purchase'],'sale'=>$rstRow['Sale'],'tin'=>$rstRow['Tin'],'tout'=>$rstRow['Tout'],'bal'=>$ob);
}
$data[] = array(
    'num' => '',
    'cde' => '',
    'cust' => '<b>Grand Total</b>',
    'ob' => "<b>$t_ob</b>",
    'pur' => "<b>$t_pur</b>",
    'sale' => "<b>$t_sale</b>",
    'tin' => "<b>$t_tin</b>",
    'tout' => "<b>$t_tout</b>",
    'bal' => "<b>$t_bal</b>",
    'valu' => "<b>" . number_format($t_val,2) . "</b>"
);
$cols = array('num'=>'<b>S. No</b>','cde'=>'<b>Code</b>','cust'=>'<b>Product Name</b>','ob'=>'<b>Opening</b>','pur'=>'<b>Purchases</b>','sale'=>'<b>Sale</b>','tin'=>'<b>Transfer In</b>','tout'=>'<b>Transfer Out</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'=>38),
                 'cde'=>array('justification'=>'center','width'=>65),
                 'cust'=>array('justification'=>'left','width'=>240),
                 'ob'=>array('justification'=>'center','width'=>75),
                 'pur'=>array('justification'=>'center','width'=>75),
                'sale'=>array('justification'=>'center','width'=>75),
                'tin'=>array('justification'=>'center','width'=>75),
                'tout'=>array('justification'=>'center','width'=>75),
                  'bal'=>array('justification'=>'center','width'=>75),
                  
                

                  )));

}




if($bval=='yes' and $tpe=='lit')
{

$pdf->addText(20,500,11,"<b>Warehouse = </b> $ware");
$pdf->addText(20,485,11,"<b>Stock in Liters with Balance Value Included.</b>");

$pdf->addText(260,485,11,"<b>From = </b>$fdate <b> To: </b> $tdate ");
$t_ob = $t_pur = $t_sale = $t_tin = $t_tout = $t_bal = $t_val = 0;

$ob=0;
$apr=0;
$count=0;
$one=1;
$tot = 0;
$pur_l = 0;
$ob_l = 0;
$sale_l = 0;
$tin_l = 0;
$tout_l = 0;
$bal_l = 0;

$nResult = MySQLi_Query($link, "Select * from s_stock order by Id  ");
while($rstRow = mysqli_fetch_array($nResult))
{

$count=$one+$count;

$pur_l = $rstRow['Purchase'] * $rstRow['Carton_Qty'] * $rstRow['Unit_Liter']; 
$ob_l = $rstRow['Ob'] * $rstRow['Carton_Qty'] * $rstRow['Unit_Liter']; 
$sale_l = $rstRow['Sale'] * $rstRow['Carton_Qty'] * $rstRow['Unit_Liter']; 
$tin_l = $rstRow['Tin'] * $rstRow['Carton_Qty'] * $rstRow['Unit_Liter']; 
$tout_l = $rstRow['Tout'] * $rstRow['Carton_Qty'] * $rstRow['Unit_Liter']; 

$bal_l = $ob_l + $pur_l - $sale_l + $tin_l - $tout_l;

$tot = $bal_l * $rstRow['Price'];


$t_ob   += $ob_l;
$t_pur  += $pur_l;
$t_sale += $sale_l;
$t_tin  += $tin_l;
$t_tout += $tout_l;
$t_bal  += $bal_l;
$t_val  += $tot;


$data[] = array('num'=>$count,'cde'=>$rstRow['Code'],'cust'=>$rstRow['Name'],'ob'=>$ob_l,'pur'=>$pur_l,'sale'=>$sale_l,'tin'=>$tin_l,'tout'=>$tout_l,'bal'=>$bal_l,'valu'=>number_format($tot,2));
}
$data[] = array(
    'num' => '',
    'cde' => '',
    'cust' => '<b>Grand Total</b>',
    'ob' => "<b>$t_ob</b>",
    'pur' => "<b>$t_pur</b>",
    'sale' => "<b>$t_sale</b>",
    'tin' => "<b>$t_tin</b>",
    'tout' => "<b>$t_tout</b>",
    'bal' => "<b>$t_bal</b>",
    'valu' => "<b>" . number_format($t_val,2) . "</b>"
);
$cols = array('num'=>'<b>S. No</b>','cde'=>'<b>Code</b>','cust'=>'<b>Product Name</b>','ob'=>'<b>Opening</b>','pur'=>'<b>Purchases</b>','sale'=>'<b>Sale</b>','tin'=>'<b>Transfer In</b>','tout'=>'<b>Transfer Out</b>','bal'=>'<b>Balance</b>','valu'=>'<b>Balance Value</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'=>38),
                 'cde'=>array('justification'=>'center','width'=>55),
                 'cust'=>array('justification'=>'left','width'=>230),
                 'ob'=>array('justification'=>'center','width'=>69),
                 'pur'=>array('justification'=>'center','width'=>69),
                'sale'=>array('justification'=>'center','width'=>69),
                'tin'=>array('justification'=>'center','width'=>69),
                'tout'=>array('justification'=>'center','width'=>69),
                  'bal'=>array('justification'=>'center','width'=>69),
                   'valu'=>array('justification'=>'center','width'=>75),
                

                  )));

}



if($bval!='yes' and $tpe=='lit')
{
$pdf->addText(20,500,11,"<b>Warehouse = </b> $ware");
$pdf->addText(20,485,11,"<b>Stock in Liters without Balance Value.</b>");

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

$t_ob = $t_pur = $t_sale = $t_tin = $t_tout = $t_bal = $t_val = 0;

$ob=0;
$apr=0;
$count=0;
$one=1;
$tot = 0;
$pur_l = 0;
$ob_l = 0;
$sale_l = 0;
$tin_l = 0;
$tout_l = 0;
$bal_l = 0;

$nResult = MySQLi_Query($link, "Select * from s_stock order by Id  ");
while($rstRow = mysqli_fetch_array($nResult))
{

$count=$one+$count;

$pur_l = $rstRow['Purchase'] * $rstRow['Carton_Qty'] * $rstRow['Unit_Liter']; 
$ob_l = $rstRow['Ob'] * $rstRow['Carton_Qty'] * $rstRow['Unit_Liter']; 
$sale_l = $rstRow['Sale'] * $rstRow['Carton_Qty'] * $rstRow['Unit_Liter']; 
$tin_l = $rstRow['Tin'] * $rstRow['Carton_Qty'] * $rstRow['Unit_Liter']; 
$tout_l = $rstRow['Tout'] * $rstRow['Carton_Qty'] * $rstRow['Unit_Liter']; 

$bal_l = $ob_l + $pur_l - $sale_l + $tin_l - $tout_l;

$tot = $bal_l * $rstRow['Price'];


$t_ob   += $ob_l;
$t_pur  += $pur_l;
$t_sale += $sale_l;
$t_tin  += $tin_l;
$t_tout += $tout_l;
$t_bal  += $bal_l;
$t_val  += $tot;


$data[] = array('num'=>$count,'cde'=>$rstRow['Code'],'cust'=>$rstRow['Name'],'ob'=>$ob_l,'pur'=>$pur_l,'sale'=>$sale_l,'tin'=>$tin_l,'tout'=>$tout_l,'bal'=>$bal_l);
}
$data[] = array(
    'num' => '',
    'cde' => '',
    'cust' => '<b>Grand Total</b>',
    'ob' => "<b>$t_ob</b>",
    'pur' => "<b>$t_pur</b>",
    'sale' => "<b>$t_sale</b>",
    'tin' => "<b>$t_tin</b>",
    'tout' => "<b>$t_tout</b>",
    'bal' => "<b>$t_bal</b>",
    'valu' => "<b>" . number_format($t_val,2) . "</b>"
);
$cols = array('num'=>'<b>S. No</b>','cde'=>'<b>Code</b>','cust'=>'<b>Product Name</b>','ob'=>'<b>Opening</b>','pur'=>'<b>Purchases</b>','sale'=>'<b>Sale</b>','tin'=>'<b>Transfer In</b>','tout'=>'<b>Transfer Out</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'=>38),
                 'cde'=>array('justification'=>'center','width'=>65),
                 'cust'=>array('justification'=>'left','width'=>240),
                 'ob'=>array('justification'=>'center','width'=>75),
                 'pur'=>array('justification'=>'center','width'=>75),
                'sale'=>array('justification'=>'center','width'=>75),
                'tin'=>array('justification'=>'center','width'=>75),
                'tout'=>array('justification'=>'center','width'=>75),
                  'bal'=>array('justification'=>'center','width'=>75),
                 
                

                  )));

}

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



