<?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(210,755,12,"<b>(CUSTOMER TARGET REPORT)</b>");
$pdf->ezSetDy(-110);

$prov=$_POST['sman']; 
$sman=$_POST['sman1']; 
$cust=$_POST['cust']; 

$strFrom=$_POST['strFrom'];
$strTo=$_POST['strTo'];
$bval=$_POST['aval']; 



if ($strFrom=='')
{
$nResult = MySQLi_Query($link, "select MIN(D_Date) as dte from ledgers");
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,710,15,"___________________________________________________________________________________________________");

$date1=Date("j-n-Y");
$pdf->addText(225,740,12,"<b>From $fdate to $tdate</b>");


if(!empty($prov) and empty($sman) and empty($cust) )
{

$pdf->addText(30,715,12,"<b>Customer Type:</b>      $prov");


$nResult = MySQLi_Query($link, "Select cust_sales.Inv_Type, cust_sales.Customer, sum(cust_sales.N_Total) as ntot, cust_sales.Salesman, acc_heads.M_Target from cust_sales, acc_heads where cust_sales.Customer = acc_heads.Name and cust_sales.Inv_Type='$prov' and cust_sales.Inv_Date between '$strFrom' and '$strTo' group  by cust_sales.Customer order by cust_sales.Province, cust_sales.City ");

//$nResult = MySQLi_Query($link, "Select cust_sales.Inv_Type, cust_sales.Customer, sum(cust_sales.N_Total) as ntot, cust_sales.Salesman from cust_sales where cust_sales.Inv_Type='$prov' and cust_sales.Inv_Date between '$strFrom' and '$strTo' group  by cust_sales.Customer order by cust_sales.Province, cust_sales.City ");

$count=0;
$one=1;
$cre=0;

while($rstRow = mysqli_fetch_array($nResult))
{
    $cre += $rstRow['ntot'];
    $date = date('d-m-Y', strtotime($rstRow['Inv_Date']));
    $count = $one + $count;
if($rstRow['ntot']>=$rstRow['M_Target'])
{
$target = 'Yes';
}
else
 {
$target = 'No';
}   
    $data[] = array(
        'num'  => $count,
       'vch'  => $rstRow['Customer'],

        'name' => $rstRow['Salesman'],
         'cre'  => number_format($rstRow['M_Target'], 2),
                'deb'  => number_format($rstRow['ntot'], 2),
                'rem'  => $target,
              

    );
}

$cols = array(
    'num'  => '<b>S. No</b>',
    'vch'  => '<b>Customer</b>',
    'name' => '<b>Salesman</b>',
     'cre'  => '<b>Target</b>',
    'deb'  => '<b>Net Sales</b>',
      'rem'  => '<b>Target Achieved</b>'
   
);

$pdf->ezTable(
    $data,
    $cols,
    '',
    array(
        'xPos' => 20,
        'xOrientation' => 'right',
        'fontSize' => 10,
        'width' => 580,
        'showHeadings' => 1,
        'shaded' => 0,
        'showLines' => 1,
        'cols' => array(
            'num'  => array('justification' => 'center', 'width' => 38),
          
            'vch'  => array('justification' => 'left', 'width' => 200),
            'name' => array('justification' => 'left',   'width' => 120),
              'cre'  => array('justification' => 'right',   'width' => 65),
            'deb'  => array('justification' => 'right',   'width' => 65),
             'rem'  => array('justification' => 'center',   'width' => 85),
          
        )
    )
);



}



elseif(!empty($prov) and !empty($sman) and empty($cust))
{

$pdf->addText(30,715,12,"<b>Customer Type:</b>      $prov");
$pdf->addText(330,715,12,"<b>Salesman:</b>      $sman");


$nResult = MySQLi_Query($link, "Select cust_sales.Inv_Type, cust_sales.Customer, sum(cust_sales.N_Total) as ntot, cust_sales.Salesman, acc_heads.M_Target from cust_sales, acc_heads where cust_sales.Customer = acc_heads.Name and cust_sales.Inv_Type='$prov' and cust_sales.Salesman='$sman' and cust_sales.Inv_Date between '$strFrom' and '$strTo' group  by cust_sales.Customer order by cust_sales.Province, cust_sales.City ");

//$nResult = MySQLi_Query($link, "Select cust_sales.Inv_Type, cust_sales.Customer, sum(cust_sales.N_Total) as ntot, cust_sales.Salesman from cust_sales where cust_sales.Inv_Type='$prov' and cust_sales.Inv_Date between '$strFrom' and '$strTo' group  by cust_sales.Customer order by cust_sales.Province, cust_sales.City ");

$count=0;
$one=1;
$cre=0;

while($rstRow = mysqli_fetch_array($nResult))
{
    $cre += $rstRow['ntot'];
    $date = date('d-m-Y', strtotime($rstRow['Inv_Date']));
    $count = $one + $count;
if($rstRow['ntot']>=$rstRow['M_Target'])
{
$target = 'Yes';
}
else
 {
$target = 'No';
}   
    $data[] = array(
        'num'  => $count,
       'vch'  => $rstRow['Customer'],

        'name' => $rstRow['Salesman'],
         'cre'  => number_format($rstRow['M_Target'], 2),
                'deb'  => number_format($rstRow['ntot'], 2),
                'rem'  => $target,
              

    );
}

$cols = array(
    'num'  => '<b>S. No</b>',
    'vch'  => '<b>Customer</b>',
    'name' => '<b>Salesman</b>',
     'cre'  => '<b>Target</b>',
    'deb'  => '<b>Net Sales</b>',
      'rem'  => '<b>Target Achieved</b>'
   
);

$pdf->ezTable(
    $data,
    $cols,
    '',
    array(
        'xPos' => 20,
        'xOrientation' => 'right',
        'fontSize' => 10,
        'width' => 580,
        'showHeadings' => 1,
        'shaded' => 0,
        'showLines' => 1,
        'cols' => array(
            'num'  => array('justification' => 'center', 'width' => 38),
          
            'vch'  => array('justification' => 'left', 'width' => 200),
            'name' => array('justification' => 'left',   'width' => 120),
              'cre'  => array('justification' => 'right',   'width' => 65),
            'deb'  => array('justification' => 'right',   'width' => 65),
             'rem'  => array('justification' => 'center',   'width' => 85),
          
        )
    )
);



}



elseif(empty($prov) and empty($sman) and !empty($cust))
{

$pdf->addText(30,715,12,"<b>Customer Name:</b>      $cust");


$nResult = MySQLi_Query($link, "Select cust_sales.Inv_Type, cust_sales.Customer, sum(cust_sales.N_Total) as ntot, cust_sales.Salesman, acc_heads.M_Target from cust_sales, acc_heads where cust_sales.Customer = acc_heads.Name and cust_sales.Customer='$cust' and cust_sales.Inv_Date between '$strFrom' and '$strTo' group  by cust_sales.Customer order by cust_sales.Province, cust_sales.City ");

//$nResult = MySQLi_Query($link, "Select cust_sales.Inv_Type, cust_sales.Customer, sum(cust_sales.N_Total) as ntot, cust_sales.Salesman from cust_sales where cust_sales.Inv_Type='$prov' and cust_sales.Inv_Date between '$strFrom' and '$strTo' group  by cust_sales.Customer order by cust_sales.Province, cust_sales.City ");

$count=0;
$one=1;
$cre=0;

while($rstRow = mysqli_fetch_array($nResult))
{
    $cre += $rstRow['ntot'];
    $date = date('d-m-Y', strtotime($rstRow['Inv_Date']));
    $count = $one + $count;
if($rstRow['ntot']>=$rstRow['M_Target'])
{
$target = 'Yes';
}
else
 {
$target = 'No';
}   
    $data[] = array(
        'num'  => $count,
       'vch'  => $rstRow['Customer'],

        'name' => $rstRow['Salesman'],
         'cre'  => number_format($rstRow['M_Target'], 2),
                'deb'  => number_format($rstRow['ntot'], 2),
                'rem'  => $target,
              

    );
}

$cols = array(
    'num'  => '<b>S. No</b>',
    'vch'  => '<b>Customer</b>',
    'name' => '<b>Salesman</b>',
     'cre'  => '<b>Target</b>',
    'deb'  => '<b>Net Sales</b>',
      'rem'  => '<b>Target Achieved</b>'
   
);

$pdf->ezTable(
    $data,
    $cols,
    '',
    array(
        'xPos' => 20,
        'xOrientation' => 'right',
        'fontSize' => 10,
        'width' => 580,
        'showHeadings' => 1,
        'shaded' => 0,
        'showLines' => 1,
        'cols' => array(
            'num'  => array('justification' => 'center', 'width' => 38),
          
            'vch'  => array('justification' => 'left', 'width' => 200),
            'name' => array('justification' => 'left',   'width' => 120),
              'cre'  => array('justification' => 'right',   'width' => 65),
            'deb'  => array('justification' => 'right',   'width' => 65),
             'rem'  => array('justification' => 'center',   'width' => 85),
          
        )
    )
);



}

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



