<?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'];
$sman=$_POST['sman']; 
$tpe=$_POST['strn']; 
$qua=$_POST['strn1']; 


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

$year = date('Y', strtotime($strFrom));

$fdate=date('d-m-Y',strtotime($strFrom));
$tdate=date('d-m-Y',strtotime($strTo));


$result42 = mysqli_query($link,"delete from tso_stock");
$rst1 = MySQLi_Query($link, "Select Full_Name, Carton_Qty, Unit_Liter, API, Grade, Size, Category2 from products order by Article");
while($rstRow = mysqli_fetch_array($rst1))
{
InsertRec("tso_stock",array(
"Code" => $code1,
"Name" =>$rstRow['Full_Name'],
"Api" =>$rstRow['API'],
"Grade" =>$rstRow['Grade'],
"Size" =>$rstRow['Size'],
"Cat" =>$rstRow['Category2'],

"Carton_Qty" =>$rstRow['Carton_Qty'],
"Unit_Liter" =>$rstRow['Unit_Liter'],

));
}


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

$d1 = $year."-01-01";
$d2 = $year."-01-31";
$s1 = 0;

$rst3 = MySQLi_Query($link, "Select Name, sum(Carton) as sn1 from cust_sales where Name='$fname' and Inv_Date between '$d1' and '$d2' and Salesman='$sman'  group by Name");
while($rstRow = mysqli_fetch_array($rst3))
{
$s1=$rstRow['sn1'];
}

$d1 = $year."-02-01";
$d2 = $year."-02-31";
$s2 = 0;

$rst4 = MySQLi_Query($link, "Select Name, sum(Carton) as sn2 from cust_sales where Name='$fname' and Inv_Date between '$d1' and '$d2' and Salesman='$sman'  group by Name");
while($rstRow = mysqli_fetch_array($rst4))
{
$s2=$rstRow['sn2'];
}


$d1 = $year."-03-01";
$d2 = $year."-03-31";
$s3 = 0;


$rst5 = MySQLi_Query($link, "Select Name, sum(Carton) as sn3 from cust_sales where Name='$fname' and Inv_Date between '$d1' and '$d2' and Salesman='$sman'  group by Name");
while($rstRow = mysqli_fetch_array($rst5))
{
$s3=$rstRow['sn3'];
}


$d1 = $year."-04-01";
$d2 = $year."-04-31";
$s4 = 0;


$rst6 = MySQLi_Query($link, "Select Name, sum(Carton) as sn4 from cust_sales where Name='$fname' and Inv_Date between '$d1' and '$d2' and Salesman='$sman'  group by Name");
while($rstRow = mysqli_fetch_array($rst6))
{
$s4=$rstRow['sn4'];
}


$d1 = $year."-05-01";
$d2 = $year."-05-31";
$s5 = 0;


$rst7 = MySQLi_Query($link, "Select Name, sum(Carton) as sn5 from cust_sales where Name='$fname' and Inv_Date between '$d1' and '$d2' and Salesman='$sman'  group by Name");
while($rstRow = mysqli_fetch_array($rst7))
{
$s5=$rstRow['sn5'];
}


$d1 = $year."-06-01";
$d2 = $year."-06-31";
$s6 = 0;


$rst8 = MySQLi_Query($link, "Select Name, sum(Carton) as sn6 from cust_sales where Name='$fname' and Inv_Date between '$d1' and '$d2' and Salesman='$sman'  group by Name");
while($rstRow = mysqli_fetch_array($rst8))
{
$s6=$rstRow['sn6'];
}

$d1 = $year."-07-01";
$d2 = $year."-07-31";
$s7 = 0;


$rst9 = MySQLi_Query($link, "Select Name, sum(Carton) as sn7 from cust_sales where Name='$fname' and Inv_Date between '$d1' and '$d2' and Salesman='$sman'  group by Name");
while($rstRow = mysqli_fetch_array($rst9))
{
$s7=$rstRow['sn7'];
}



$d1 = $year."-08-01";
$d2 = $year."-08-31";
$s8 = 0;


$rst10 = MySQLi_Query($link, "Select Name, sum(Carton) as sn8 from cust_sales where Name='$fname' and Inv_Date between '$d1' and '$d2' and Salesman='$sman'  group by Name");
while($rstRow = mysqli_fetch_array($rst10))
{
$s8=$rstRow['sn8'];
}


$d1 = $year."-09-01";
$d2 = $year."-09-31";
$s9 = 0;


$rst11 = MySQLi_Query($link, "Select Name, sum(Carton) as sn9 from cust_sales where Name='$fname' and Inv_Date between '$d1' and '$d2' and Salesman='$sman'  group by Name");
while($rstRow = mysqli_fetch_array($rst11))
{
$s9=$rstRow['sn9'];
}


$d1 = $year."-10-01";
$d2 = $year."-10-31";
$s10 = 0;


$rst12 = MySQLi_Query($link, "Select Name, sum(Carton) as sn10 from cust_sales where Name='$fname' and Inv_Date between '$d1' and '$d2' and Salesman='$sman'  group by Name");
while($rstRow = mysqli_fetch_array($rst12))
{
$s10=$rstRow['sn10'];
}


$d1 = $year."-11-01";
$d2 = $year."-11-31";
$s11 = 0;


$rst13 = MySQLi_Query($link, "Select Name, sum(Carton) as sn11 from cust_sales where Name='$fname' and Inv_Date between '$d1' and '$d2' and Salesman='$sman'  group by Name");
while($rstRow = mysqli_fetch_array($rst13))
{
$s11=$rstRow['sn11'];
}

$d1 = $year."-12-01";
$d2 = $year."-12-31";
$s12 = 0;


$rst14 = MySQLi_Query($link, "Select Name, sum(Carton) as sn12 from cust_sales where Name='$fname' and Inv_Date between '$d1' and '$d2' and Salesman='$sman'  group by Name");
while($rstRow = mysqli_fetch_array($rst14))
{
$s12=$rstRow['sn12'];
}

$result = mysqli_query($link, "update tso_stock set M1='$s1', M2='$s2', M3='$s3', M4='$s4', M5='$s5', M6='$s6', M7='$s7', M8='$s8', M9='$s9', M10='$s10', M11='$s11', M12='$s12' where Name = '$fname' ");

}




if($qua=='s1' and $tpe=='cart')
{
$pdf->addText(360,500,14,"<b>TSO $sman</b>");
$pdf->addText(220,545,12,"<b>PRODUCT MIX TARGETS 1ST QUARTER SALES REPORT JAN-MARCH $year.</b>");

$pdf->addText(20,485,12,"<b>Sales in Cartons.</b>");


$ob=0;
$apr=0;
$count=0;
$one=1;
$tot = 0;
$nResult = MySQLi_Query($link, "SELECT * FROM tso_stock ORDER BY Id");
$count = 0;
$tot_m1 = $tot_m2 = $tot_m3 = $tot_val = 0;

while($rstRow = mysqli_fetch_array($nResult)) {
    $count++;
    $ob = $rstRow['M1'] + $rstRow['M2'] + $rstRow['M3'];

    // Accumulate totals
    $tot_m1 += $rstRow['M1'];
    $tot_m2 += $rstRow['M2'];
    $tot_m3 += $rstRow['M3'];
    $tot_val += $ob;

    $data[] = array(
        'num'  => $count,
        'cust' => $rstRow['Name'],
        'api'  => $rstRow['Api'],
        'grd'  => $rstRow['Grade'],
        'siz'  => $rstRow['Size'],
        'm1'   => number_format($rstRow['M1'], 2),
        'm2'   => number_format($rstRow['M2'], 2),
        'm3'   => number_format($rstRow['M3'], 2),
        'valu' => number_format($ob, 2)
    );
}

// Add Grand Total row
$data[] = array(
    'num'  => '',
    'cust' => '<b>Grand Total</b>',
    'api'  => '',
    'grd'  => '',
    'siz'  => '',
    'm1'   => '<b>'.number_format($tot_m1, 2).'</b>',
    'm2'   => '<b>'.number_format($tot_m2, 2).'</b>',
    'm3'   => '<b>'.number_format($tot_m3, 2).'</b>',
    'valu' => '<b>'.number_format($tot_val, 2).'</b>'
);

$cols = array(
    'num'=>'<b>S. No</b>',
    'cust'=>'<b>Product Name</b>',
    'api'=>'<b>API</b>',
    'grd'=>'<b>SAE</b>',
    'siz'=>'<b>Pack Size</b>',
    'm1'=>'<b>JAN</b>',
    'm2'=>'<b>FEB</b>',
    'm3'=>'<b>MARCH</b>',
    'valu'=>'<b>Total</b>'
);

$pdf->ezTable($data, $cols, '',
    array(
        'xPos'=>20,
        'xOrientation'=>'right',
        'fontSize'=>11,
        'width'=>680,
        'showHeadings'=>1,
        'shaded'=>1,
        'showLines'=>1,
        'cols'=>array(
            'num'=>array('justification'=>'center','width'=>38),
            'cust'=>array('justification'=>'left','width'=>250),
            'api'=>array('justification'=>'center','width'=>72),
            'grd'=>array('justification'=>'center','width'=>72),
            'siz'=>array('justification'=>'center','width'=>72),
            'm1'=>array('justification'=>'center','width'=>72),
            'm2'=>array('justification'=>'center','width'=>72),
            'm3'=>array('justification'=>'center','width'=>72),
            'valu'=>array('justification'=>'center','width'=>75),
        )
    )
);


}




if($qua=='s2' and $tpe=='cart')
{
$pdf->addText(360,500,14,"<b>TSO $sman</b>");
$pdf->addText(220,545,12,"<b>PRODUCT MIX TARGETS 2ND QUARTER SALES REPORT APRIL-JUNE $year.</b>");

$pdf->addText(20,485,12,"<b>Sales in Cartons.</b>");


$ob=0;
$apr=0;
$count=0;
$one=1;
$tot = 0;
$nResult = MySQLi_Query($link, "SELECT * FROM tso_stock ORDER BY Id");
$count = 0;
$tot_m10 = $tot_m11 = $tot_m12 = $tot_val = 0;

while($rstRow = mysqli_fetch_array($nResult)) {

    $count++;
    $ob = $rstRow['M4'] + $rstRow['M5'] + $rstRow['M6'];

    // Accumulate totals
    $tot_m10 += $rstRow['M4'];
    $tot_m11 += $rstRow['M5'];
    $tot_m12 += $rstRow['M6'];
    $tot_val += $ob;

    $data[] = array(
        'num'  => $count,
        'cust' => $rstRow['Name'],
        'api'  => $rstRow['Api'],
        'grd'  => $rstRow['Grade'],
        'siz'  => $rstRow['Size'],
        'm1'   => number_format($rstRow['M4'], 2),
        'm2'   => number_format($rstRow['M5'], 2),
        'm3'   => number_format($rstRow['M6'], 2),
        'valu' => number_format($ob, 2)
    );
}

// Add Grand Total row
$data[] = array(
    'num'  => '',
    'cust' => '<b>Grand Total</b>',
    'api'  => '',
    'grd'  => '',
    'siz'  => '',
    'm1'   => '<b>'.number_format($tot_m10, 2).'</b>',
    'm2'   => '<b>'.number_format($tot_m11, 2).'</b>',
    'm3'   => '<b>'.number_format($tot_m12, 2).'</b>',
    'valu' => '<b>'.number_format($tot_val, 2).'</b>'
);

$cols = array(
    'num'=>'<b>S. No</b>',
    'cust'=>'<b>Product Name</b>',
    'api'=>'<b>API</b>',
    'grd'=>'<b>SAE</b>',
    'siz'=>'<b>Pack Size</b>',
    'm1'=>'<b>APRIL</b>',
    'm2'=>'<b>MAY</b>',
    'm3'=>'<b>JUNE</b>',
    'valu'=>'<b>Total</b>'
);

$pdf->ezTable($data, $cols, '',
    array(
        'xPos'=>20,
        'xOrientation'=>'right',
        'fontSize'=>11,
        'width'=>680,
        'showHeadings'=>1,
        'shaded'=>1,
        'showLines'=>1,
        'cols'=>array(
            'num'=>array('justification'=>'center','width'=>38),
            'cust'=>array('justification'=>'left','width'=>250),
            'api'=>array('justification'=>'center','width'=>72),
            'grd'=>array('justification'=>'center','width'=>72),
            'siz'=>array('justification'=>'center','width'=>72),
            'm1'=>array('justification'=>'center','width'=>72),
            'm2'=>array('justification'=>'center','width'=>72),
            'm3'=>array('justification'=>'center','width'=>72),
            'valu'=>array('justification'=>'center','width'=>75),
        )
    )
);


}



if($qua=='s3' and $tpe=='cart')
{
$pdf->addText(360,500,14,"<b>TSO $sman</b>");
$pdf->addText(220,545,12,"<b>PRODUCT MIX TARGETS 3RD QUARTER SALES REPORT JULY-SEP $year.</b>");

$pdf->addText(20,485,12,"<b>Sales in Cartons.</b>");


$ob=0;
$apr=0;
$count=0;
$one=1;
$tot = 0;
$nResult = MySQLi_Query($link, "SELECT * FROM tso_stock ORDER BY Id");
$count = 0;
$tot_m10 = $tot_m11 = $tot_m12 = $tot_val = 0;

while($rstRow = mysqli_fetch_array($nResult)) {

    $count++;
    $ob = $rstRow['M7'] + $rstRow['M8'] + $rstRow['M9'];

    // Accumulate totals
    $tot_m10 += $rstRow['M7'];
    $tot_m11 += $rstRow['M8'];
    $tot_m12 += $rstRow['M9'];
    $tot_val += $ob;

    $data[] = array(
        'num'  => $count,
        'cust' => $rstRow['Name'],
        'api'  => $rstRow['Api'],
        'grd'  => $rstRow['Grade'],
        'siz'  => $rstRow['Size'],
        'm1'   => number_format($rstRow['M7'], 2),
        'm2'   => number_format($rstRow['M8'], 2),
        'm3'   => number_format($rstRow['M9'], 2),
        'valu' => number_format($ob, 2)
    );
}

// Add Grand Total row
$data[] = array(
    'num'  => '',
    'cust' => '<b>Grand Total</b>',
    'api'  => '',
    'grd'  => '',
    'siz'  => '',
    'm1'   => '<b>'.number_format($tot_m10, 2).'</b>',
    'm2'   => '<b>'.number_format($tot_m11, 2).'</b>',
    'm3'   => '<b>'.number_format($tot_m12, 2).'</b>',
    'valu' => '<b>'.number_format($tot_val, 2).'</b>'
);

$cols = array(
    'num'=>'<b>S. No</b>',
    'cust'=>'<b>Product Name</b>',
    'api'=>'<b>API</b>',
    'grd'=>'<b>SAE</b>',
    'siz'=>'<b>Pack Size</b>',
    'm1'=>'<b>JULY</b>',
    'm2'=>'<b>AUG</b>',
    'm3'=>'<b>SEP</b>',
    'valu'=>'<b>Total</b>'
);

$pdf->ezTable($data, $cols, '',
    array(
        'xPos'=>20,
        'xOrientation'=>'right',
        'fontSize'=>11,
        'width'=>680,
        'showHeadings'=>1,
        'shaded'=>1,
        'showLines'=>1,
        'cols'=>array(
            'num'=>array('justification'=>'center','width'=>38),
            'cust'=>array('justification'=>'left','width'=>250),
            'api'=>array('justification'=>'center','width'=>72),
            'grd'=>array('justification'=>'center','width'=>72),
            'siz'=>array('justification'=>'center','width'=>72),
            'm1'=>array('justification'=>'center','width'=>72),
            'm2'=>array('justification'=>'center','width'=>72),
            'm3'=>array('justification'=>'center','width'=>72),
            'valu'=>array('justification'=>'center','width'=>75),
        )
    )
);


}

if($qua=='s4' and $tpe=='cart')
{
$pdf->addText(360,500,14,"<b>TSO $sman</b>");
$pdf->addText(220,545,12,"<b>PRODUCT MIX TARGETS 4TH QUARTER SALES REPORT OCT-DEC $year.</b>");

$pdf->addText(20,485,12,"<b>Sales in Cartons.</b>");


$ob=0;
$apr=0;
$count=0;
$one=1;
$tot = 0;
$nResult = MySQLi_Query($link, "SELECT * FROM tso_stock ORDER BY Id");
$count = 0;
$tot_m10 = $tot_m11 = $tot_m12 = $tot_val = 0;

while($rstRow = mysqli_fetch_array($nResult)) {

    $count++;
    $ob = $rstRow['M10'] + $rstRow['M11'] + $rstRow['M12'];

    // Accumulate totals
    $tot_m10 += $rstRow['M10'];
    $tot_m11 += $rstRow['M11'];
    $tot_m12 += $rstRow['M12'];
    $tot_val += $ob;

    $data[] = array(
        'num'  => $count,
        'cust' => $rstRow['Name'],
        'api'  => $rstRow['Api'],
        'grd'  => $rstRow['Grade'],
        'siz'  => $rstRow['Size'],
        'm1'   => number_format($rstRow['M10'], 2),
        'm2'   => number_format($rstRow['M11'], 2),
        'm3'   => number_format($rstRow['M12'], 2),
        'valu' => number_format($ob, 2)
    );
}

// Add Grand Total row
$data[] = array(
    'num'  => '',
    'cust' => '<b>Grand Total</b>',
    'api'  => '',
    'grd'  => '',
    'siz'  => '',
    'm1'   => '<b>'.number_format($tot_m10, 2).'</b>',
    'm2'   => '<b>'.number_format($tot_m11, 2).'</b>',
    'm3'   => '<b>'.number_format($tot_m12, 2).'</b>',
    'valu' => '<b>'.number_format($tot_val, 2).'</b>'
);

$cols = array(
    'num'=>'<b>S. No</b>',
    'cust'=>'<b>Product Name</b>',
    'api'=>'<b>API</b>',
    'grd'=>'<b>SAE</b>',
    'siz'=>'<b>Pack Size</b>',
    'm1'=>'<b>OCT</b>',
    'm2'=>'<b>NOV</b>',
    'm3'=>'<b>DEC</b>',
    'valu'=>'<b>Total</b>'
);

$pdf->ezTable($data, $cols, '',
    array(
        'xPos'=>20,
        'xOrientation'=>'right',
        'fontSize'=>11,
        'width'=>680,
        'showHeadings'=>1,
        'shaded'=>1,
        'showLines'=>1,
        'cols'=>array(
            'num'=>array('justification'=>'center','width'=>38),
            'cust'=>array('justification'=>'left','width'=>250),
            'api'=>array('justification'=>'center','width'=>72),
            'grd'=>array('justification'=>'center','width'=>72),
            'siz'=>array('justification'=>'center','width'=>72),
            'm1'=>array('justification'=>'center','width'=>72),
            'm2'=>array('justification'=>'center','width'=>72),
            'm3'=>array('justification'=>'center','width'=>72),
            'valu'=>array('justification'=>'center','width'=>75),
        )
    )
);


}





if($qua=='s1' and $tpe=='lit')
{
$pdf->addText(360,500,14,"<b>TSO $sman</b>");
$pdf->addText(220,545,12,"<b>PRODUCT MIX TARGETS 1ST QUARTER SALES REPORT JAN-MARCH $year.</b>");

$pdf->addText(20,485,12,"<b>Sales in Liters.</b>");


$ob=0;
$apr=0;
$count=0;
$one=1;
$tot = 0;
$nResult = MySQLi_Query($link, "SELECT * FROM tso_stock ORDER BY Id");
$count = 0;
$tot_m1 = $tot_m2 = $tot_m3 = $tot_val = 0;
$m1_l = $m2_l = $m3_l = 0;
while($rstRow = mysqli_fetch_array($nResult)) {
    

$m1_l = $rstRow['M1'] * $rstRow['Carton_Qty'] * $rstRow['Unit_Liter']; 
$m2_l = $rstRow['M2'] * $rstRow['Carton_Qty'] * $rstRow['Unit_Liter']; 
$m3_l = $rstRow['M3'] * $rstRow['Carton_Qty'] * $rstRow['Unit_Liter']; 

    $count++;
    $ob = $m1_l + $m2_l + $m3_l;

    // Accumulate totals
    $tot_m1 += $m1_l;
    $tot_m2 += $m2_l;
    $tot_m3 += $m3_l;
    $tot_val += $ob;

    $data[] = array(
        'num'  => $count,
        'cust' => $rstRow['Name'],
        'api'  => $rstRow['Api'],
        'grd'  => $rstRow['Grade'],
        'siz'  => $rstRow['Size'],
        'm1'   => number_format($m1_l, 2),
        'm2'   => number_format($m2_l, 2),
        'm3'   => number_format($m3_l, 2),
        'valu' => number_format($ob, 2)
    );
}

// Add Grand Total row
$data[] = array(
    'num'  => '',
    'cust' => '<b>Grand Total</b>',
    'api'  => '',
    'grd'  => '',
    'siz'  => '',
    'm1'   => '<b>'.number_format($tot_m1, 2).'</b>',
    'm2'   => '<b>'.number_format($tot_m2, 2).'</b>',
    'm3'   => '<b>'.number_format($tot_m3, 2).'</b>',
    'valu' => '<b>'.number_format($tot_val, 2).'</b>'
);

$cols = array(
    'num'=>'<b>S. No</b>',
    'cust'=>'<b>Product Name</b>',
    'api'=>'<b>API</b>',
    'grd'=>'<b>SAE</b>',
    'siz'=>'<b>Pack Size</b>',
    'm1'=>'<b>JAN</b>',
    'm2'=>'<b>FEB</b>',
    'm3'=>'<b>MARCH</b>',
    'valu'=>'<b>Total</b>'
);

$pdf->ezTable($data, $cols, '',
    array(
        'xPos'=>20,
        'xOrientation'=>'right',
        'fontSize'=>11,
        'width'=>680,
        'showHeadings'=>1,
        'shaded'=>1,
        'showLines'=>1,
        'cols'=>array(
            'num'=>array('justification'=>'center','width'=>38),
            'cust'=>array('justification'=>'left','width'=>250),
            'api'=>array('justification'=>'center','width'=>72),
            'grd'=>array('justification'=>'center','width'=>72),
            'siz'=>array('justification'=>'center','width'=>72),
            'm1'=>array('justification'=>'center','width'=>72),
            'm2'=>array('justification'=>'center','width'=>72),
            'm3'=>array('justification'=>'center','width'=>72),
            'valu'=>array('justification'=>'center','width'=>75),
        )
    )
);


}



if($qua=='s2' and $tpe=='lit')
{
$pdf->addText(360,500,14,"<b>TSO $sman</b>");
$pdf->addText(220,545,12,"<b>PRODUCT MIX TARGETS 4TH QUARTER SALES REPORT APR-JUNE $year.</b>");

$pdf->addText(20,485,12,"<b>Sales in Liters.</b>");


$ob=0;
$apr=0;
$count=0;
$one=1;
$tot = 0;
$nResult = MySQLi_Query($link, "SELECT * FROM tso_stock ORDER BY Id");
$count = 0;
$tot_m10 = $tot_m11 = $tot_m12 = $tot_val = 0;
$m1_l = $m2_l = $m3_l = 0;

while($rstRow = mysqli_fetch_array($nResult)) {

$m1_l = $rstRow['M4'] * $rstRow['Carton_Qty'] * $rstRow['Unit_Liter']; 
$m2_l = $rstRow['M5'] * $rstRow['Carton_Qty'] * $rstRow['Unit_Liter']; 
$m3_l = $rstRow['M6'] * $rstRow['Carton_Qty'] * $rstRow['Unit_Liter']; 

    $count++;
    $ob = $m1_l + $m2_l + $m3_l;

    // Accumulate totals
    $tot_m10 += $m1_l;
    $tot_m11 += $m2_l;
    $tot_m12 += $m3_l;
    $tot_val += $ob;


    $data[] = array(
        'num'  => $count,
        'cust' => $rstRow['Name'],
        'api'  => $rstRow['Api'],
        'grd'  => $rstRow['Grade'],
        'siz'  => $rstRow['Size'],
        'm1'   => number_format($m1_l, 2),
        'm2'   => number_format($m2_l, 2),
        'm3'   => number_format($m3_l, 2),
        'valu' => number_format($ob, 2)
    );
}

// Add Grand Total row
$data[] = array(
    'num'  => '',
    'cust' => '<b>Grand Total</b>',
    'api'  => '',
    'grd'  => '',
    'siz'  => '',
    'm1'   => '<b>'.number_format($tot_m10, 2).'</b>',
    'm2'   => '<b>'.number_format($tot_m11, 2).'</b>',
    'm3'   => '<b>'.number_format($tot_m12, 2).'</b>',
    'valu' => '<b>'.number_format($tot_val, 2).'</b>'
);

$cols = array(
    'num'=>'<b>S. No</b>',
    'cust'=>'<b>Product Name</b>',
    'api'=>'<b>API</b>',
    'grd'=>'<b>SAE</b>',
    'siz'=>'<b>Pack Size</b>',
    'm1'=>'<b>OCT</b>',
    'm2'=>'<b>NOV</b>',
    'm3'=>'<b>DEC</b>',
    'valu'=>'<b>Total</b>'
);

$pdf->ezTable($data, $cols, '',
    array(
        'xPos'=>20,
        'xOrientation'=>'right',
        'fontSize'=>11,
        'width'=>680,
        'showHeadings'=>1,
        'shaded'=>1,
        'showLines'=>1,
        'cols'=>array(
            'num'=>array('justification'=>'center','width'=>38),
            'cust'=>array('justification'=>'left','width'=>250),
            'api'=>array('justification'=>'center','width'=>72),
            'grd'=>array('justification'=>'center','width'=>72),
            'siz'=>array('justification'=>'center','width'=>72),
            'm1'=>array('justification'=>'center','width'=>72),
            'm2'=>array('justification'=>'center','width'=>72),
            'm3'=>array('justification'=>'center','width'=>72),
            'valu'=>array('justification'=>'center','width'=>75),
        )
    )
);


}



if($qua=='s3' and $tpe=='lit')
{
$pdf->addText(360,500,14,"<b>TSO $sman</b>");
$pdf->addText(220,545,12,"<b>PRODUCT MIX TARGETS 4TH QUARTER SALES REPORT JULY-SEP $year.</b>");

$pdf->addText(20,485,12,"<b>Sales in Liters.</b>");


$ob=0;
$apr=0;
$count=0;
$one=1;
$tot = 0;
$nResult = MySQLi_Query($link, "SELECT * FROM tso_stock ORDER BY Id");
$count = 0;
$tot_m10 = $tot_m11 = $tot_m12 = $tot_val = 0;
$m1_l = $m2_l = $m3_l = 0;

while($rstRow = mysqli_fetch_array($nResult)) {

$m1_l = $rstRow['M7'] * $rstRow['Carton_Qty'] * $rstRow['Unit_Liter']; 
$m2_l = $rstRow['M8'] * $rstRow['Carton_Qty'] * $rstRow['Unit_Liter']; 
$m3_l = $rstRow['M9'] * $rstRow['Carton_Qty'] * $rstRow['Unit_Liter']; 

    $count++;
    $ob = $m1_l + $m2_l + $m3_l;

    // Accumulate totals
    $tot_m10 += $m1_l;
    $tot_m11 += $m2_l;
    $tot_m12 += $m3_l;
    $tot_val += $ob;


    $data[] = array(
        'num'  => $count,
        'cust' => $rstRow['Name'],
        'api'  => $rstRow['Api'],
        'grd'  => $rstRow['Grade'],
        'siz'  => $rstRow['Size'],
        'm1'   => number_format($m1_l, 2),
        'm2'   => number_format($m2_l, 2),
        'm3'   => number_format($m3_l, 2),
        'valu' => number_format($ob, 2)
    );
}

// Add Grand Total row
$data[] = array(
    'num'  => '',
    'cust' => '<b>Grand Total</b>',
    'api'  => '',
    'grd'  => '',
    'siz'  => '',
    'm1'   => '<b>'.number_format($tot_m10, 2).'</b>',
    'm2'   => '<b>'.number_format($tot_m11, 2).'</b>',
    'm3'   => '<b>'.number_format($tot_m12, 2).'</b>',
    'valu' => '<b>'.number_format($tot_val, 2).'</b>'
);

$cols = array(
    'num'=>'<b>S. No</b>',
    'cust'=>'<b>Product Name</b>',
    'api'=>'<b>API</b>',
    'grd'=>'<b>SAE</b>',
    'siz'=>'<b>Pack Size</b>',
    'm1'=>'<b>OCT</b>',
    'm2'=>'<b>NOV</b>',
    'm3'=>'<b>DEC</b>',
    'valu'=>'<b>Total</b>'
);

$pdf->ezTable($data, $cols, '',
    array(
        'xPos'=>20,
        'xOrientation'=>'right',
        'fontSize'=>11,
        'width'=>680,
        'showHeadings'=>1,
        'shaded'=>1,
        'showLines'=>1,
        'cols'=>array(
            'num'=>array('justification'=>'center','width'=>38),
            'cust'=>array('justification'=>'left','width'=>250),
            'api'=>array('justification'=>'center','width'=>72),
            'grd'=>array('justification'=>'center','width'=>72),
            'siz'=>array('justification'=>'center','width'=>72),
            'm1'=>array('justification'=>'center','width'=>72),
            'm2'=>array('justification'=>'center','width'=>72),
            'm3'=>array('justification'=>'center','width'=>72),
            'valu'=>array('justification'=>'center','width'=>75),
        )
    )
);


}




if($qua=='s4' and $tpe=='lit')
{
$pdf->addText(360,500,14,"<b>TSO $sman</b>");
$pdf->addText(220,545,12,"<b>PRODUCT MIX TARGETS 4TH QUARTER SALES REPORT OCT-DEC $year.</b>");

$pdf->addText(20,485,12,"<b>Sales in Liters.</b>");


$ob=0;
$apr=0;
$count=0;
$one=1;
$tot = 0;
$nResult = MySQLi_Query($link, "SELECT * FROM tso_stock ORDER BY Id");
$count = 0;
$tot_m10 = $tot_m11 = $tot_m12 = $tot_val = 0;
$m1_l = $m2_l = $m3_l = 0;

while($rstRow = mysqli_fetch_array($nResult)) {

$m1_l = $rstRow['M10'] * $rstRow['Carton_Qty'] * $rstRow['Unit_Liter']; 
$m2_l = $rstRow['M11'] * $rstRow['Carton_Qty'] * $rstRow['Unit_Liter']; 
$m3_l = $rstRow['M12'] * $rstRow['Carton_Qty'] * $rstRow['Unit_Liter']; 

    $count++;
    $ob = $m1_l + $m2_l + $m3_l;

    // Accumulate totals
    $tot_m10 += $m1_l;
    $tot_m11 += $m2_l;
    $tot_m12 += $m3_l;
    $tot_val += $ob;


    $data[] = array(
        'num'  => $count,
        'cust' => $rstRow['Name'],
        'api'  => $rstRow['Api'],
        'grd'  => $rstRow['Grade'],
        'siz'  => $rstRow['Size'],
        'm1'   => number_format($m1_l, 2),
        'm2'   => number_format($m2_l, 2),
        'm3'   => number_format($m3_l, 2),
        'valu' => number_format($ob, 2)
    );
}

// Add Grand Total row
$data[] = array(
    'num'  => '',
    'cust' => '<b>Grand Total</b>',
    'api'  => '',
    'grd'  => '',
    'siz'  => '',
    'm1'   => '<b>'.number_format($tot_m10, 2).'</b>',
    'm2'   => '<b>'.number_format($tot_m11, 2).'</b>',
    'm3'   => '<b>'.number_format($tot_m12, 2).'</b>',
    'valu' => '<b>'.number_format($tot_val, 2).'</b>'
);

$cols = array(
    'num'=>'<b>S. No</b>',
    'cust'=>'<b>Product Name</b>',
    'api'=>'<b>API</b>',
    'grd'=>'<b>SAE</b>',
    'siz'=>'<b>Pack Size</b>',
    'm1'=>'<b>OCT</b>',
    'm2'=>'<b>NOV</b>',
    'm3'=>'<b>DEC</b>',
    'valu'=>'<b>Total</b>'
);

$pdf->ezTable($data, $cols, '',
    array(
        'xPos'=>20,
        'xOrientation'=>'right',
        'fontSize'=>11,
        'width'=>680,
        'showHeadings'=>1,
        'shaded'=>1,
        'showLines'=>1,
        'cols'=>array(
            'num'=>array('justification'=>'center','width'=>38),
            'cust'=>array('justification'=>'left','width'=>250),
            'api'=>array('justification'=>'center','width'=>72),
            'grd'=>array('justification'=>'center','width'=>72),
            'siz'=>array('justification'=>'center','width'=>72),
            'm1'=>array('justification'=>'center','width'=>72),
            'm2'=>array('justification'=>'center','width'=>72),
            'm3'=>array('justification'=>'center','width'=>72),
            'valu'=>array('justification'=>'center','width'=>75),
        )
    )
);


}



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



