<?
session_start();
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
//$br=$_SESSION['branch'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}

?>

<?php //include('logo2.php'); ?>
<?php //include('includes/top.php'); ?>


<?

  $fromdt=$_REQUEST['dfrom'];
  $todt=$_REQUEST['dto'];
  $type=$_REQUEST['type'];
  $from_dt = $fromdt;
  $to_dt = $todt;
    $fromdt=implode('-', array_reverse(explode('-', $fromdt)));
  $todt=implode('-', array_reverse(explode('-', $todt)));
  if($type == 'ALL')
  {
    $qry=mysqli_query($db,"select invno,invdate,sum(total),sum(mba),sum(strbtnml),sum(strbtspl),sum(redct),sum(befrtx),sum(taxamt),sum(nettot),customer,invdate,sum(total*staffdisc/100) from prod_invoice where invdate between '$fromdt' and '$todt' and brcode='$brcode' and biltpe = 3 group by invno ");
    //print "<tr><th COLSPAN=11>Billwise Report for the period from <$fromdt> to <$todt></th>";
  }
   if($type == 'CASH')
  {
    $qry=mysqli_query($db,"select invno,invdate,sum(total),sum(mba),sum(strbtnml),sum(strbtspl),sum(redct),sum(befrtx),sum(taxamt),sum(nettot),customer,invdate,sum(total*staffdisc/100) from prod_invoice where invdate between '$fromdt' and '$todt' and brcode='$brcode' and biltpe = 3 and pmode='CASH' group by invno ");
    //print "<tr><th COLSPAN=11>Billwise Report for the period from <$fromdt> to <$todt></th>";
  }
  if($type == 'CREDIT')
  {
    $qry=mysqli_query($db,"select invno,invdate,sum(total),sum(mba),sum(strbtnml),sum(strbtspl),sum(redct),sum(befrtx),sum(taxamt),sum(nettot),customer,invdate,sum(total*staffdisc/100) from prod_invoice where invdate between '$fromdt' and '$todt' and brcode='$brcode' and biltpe = 3 and pmode='CREDIT' group by invno ");
    //print "<tr><th COLSPAN=11>Billwise Report for the period from <$fromdt> to <$todt></th>";
  }
    $brqry="select brname from branch where brcode='$brcode'";
  $brrs=mysqli_query($db,$brqry);
  $brrw=mysqli_fetch_array($brrs);
  $brnchnm=$brrw[0];
  $file =  'staff_billreport.prn';
  $handle = fopen($file, 'w');
  $dss=chr(27).chr(71).chr(N);
  $dse=chr(27).chr(72);
  $initialized = chr(27).chr(64);
  $load = chr(27).chr(25).chr(66);
  $bold = chr(27).chr(14);
  $unbold = chr(20);
  $pagelenth=chr(27).chr(67).chr(36);
  $Data = $initialized;
  $Data .= "                          KERALA KHADI & VILLAGE INDUSTRIES BOARD\n";
  $Data .= "                          CENTER NAME : $brnchnm \n";
  $Data .= "          Khadi Staff Sale & Discount Statement for the period from $from_dt to $to_dt\n\n";
  $Data .= chr(15)."\n\n";
$Data.= "|-------|--------------------------|----------|-----------|---------|------------|------------|----------|--------|------|------------|\n";
$Data.= "|Bill No|           Staff Name     |   Date   |   Amount  |   MDA   |State Rebate|State Rebate|Reduction/|Net Cash| Tax  |Total Amount|\n";
$Data.= "|       |                          |          |           |         |    Normal  |   Special  |Staff Disc|        |      |            |\n";
$Data.= "|-------|--------------------------|----------|-----------|---------|------------|------------|----------|--------|------|------------|\n";
//    print "<table class='report' border='1'>";
  $mfrom_date = $fromdt;
  $mto_date = $todt;

//  print "<tr><th>Bill No.</th><th>Staff Name</th><th>Date</th><th>Amount</th><th>MDA</th><th>State Rebate Normal</th><th>State Rebate Spl</th><th>Reduction /<br>Staff Discount</th><th>Net Cash</th><th>Tax</th><th>Total Amount</th>";
       $colspan=6;
  $mstr[]='';
  $i=1;
  $mgname = '.';
  $mtot_total = 0;
  $mtot_mba= 0;
  $mtot_strbtnml = 0;
  $mtot_strbtspl = 0;
  $mtot_redct = 0;
  $mtot_befrtx = 0;
  $mtot_taxamt = 0;
  $mtot_nettot = 0;
  $mgr_total = 0;
  $mgr_mba= 0;
  $mgr_strbtnml = 0;
  $mgr_strbtspl = 0;
  $mgr_redct = 0;
  $mgr_befrtx = 0;
  $mgr_taxamt = 0;
  $mgr_nettot = 0;
  $ctr=0;
    while($row=mysqli_fetch_array($qry))
    {
        $mstaffdisc_amt = 0;
        if ($row[12]>0)
        {
            $rbt_tbl=mysqli_query($db,"select item_code,qty,staffdisc,total from prod_invoice where brcode = '$brcode' and invno = '$row[0]'");
            while($rrow=mysqli_fetch_array($rbt_tbl))
            {
                $rbt_val=mysqli_query($db,"select rbtbl,rbtvl from partmaster where partcode = '$rrow[0]'");
                $row22=mysqli_fetch_row($rbt_val);
                if($row22[0]=='Yes')
                {
                   $mstaffdisc_amt += ($rrow[1]*$row22[1])*$rrow[2]/100;
                }
                else
                {
                   $mstaffdisc_amt += $rrow[3]*$rrow[2]/100;
                   $mdummy +=  $rrow[3]*$rrow[2]/100;

                }
            }
        }
        $mdisc_red = $mstaffdisc_amt+$row[6];
        $Data .= "| ".substr($row[0].str_repeat(" ",7),0,6);
        $Data .= "|".substr($row[10].str_repeat(" ",26),0,26);
        $minvdt=implode('-', array_reverse(explode('-', $row[11])));
        $Data .= "|".substr($minvdt.str_repeat(" ",10),0,10);
    $mnum = number_format($row[2], 2, '.', '');
    $Data .= "|".substr(str_repeat(" ",11).$mnum,-11);
        $mnum = number_format($row[3], 2, '.', '');
    $Data .= "|".substr(str_repeat(" ",11).$mnum,-9);
        $mnum = number_format($row[4], 2, '.', '');
    $Data .= "|".substr(str_repeat(" ",12).$mnum,-12);
        $mnum = number_format($row[5], 2, '.', '');
    $Data .= "|".substr(str_repeat(" ",12).$mnum,-12);
        $mnum = number_format($mdisc_red, 2, '.', '');
    $Data .= "|".substr(str_repeat(" ",10).$mnum,-10);
        $mnum = number_format($row[7], 2, '.', '');
    $Data .= "|".substr(str_repeat(" ",8).$mnum,-8);
        $mnum = number_format($row[8], 2, '.', '');
    $Data .= "|".substr(str_repeat(" ",6).$mnum,-6);
        $mnum = number_format($row[9], 2, '.', '');
    $Data .= "|".substr(str_repeat(" ",12).$mnum,-12)."|\n";
       //print "<tr><td>".$row[0]."</td><td align='right'>$row[10]</td><td align='right'>$row[11]</td><td align='right'>$row[2]</td><td align='right'>$row[3]</td><td align='right'>$row[4]</td><td align='right'>$row[5]</td><td align='right'>$mdisc_red</td><td align='right'>$row[7]</td><td align='right'>$row[8]</td><td align='right'>$row[9]</td></tr>";
    $mgr_total += $row[2];
    $mgr_mba += $row[3];
    $mgr_strbtnml += $row[4];
    $mgr_strbtspl += $row[5];
    $mgr_redct += $mdisc_red;
    $mgr_befrtx += $row[7];
    $mgr_taxamt += $row[8];
    $mgr_nettot += $row[9];
    $mtot_total += $row[2];
    $mtot_mba += $row[3];
    $mtot_strbtnml += $row[4];
    $mtot_strbtspl += $row[5];
    $mtot_redct += $mdisc_amt;
    $mtot_befrtx += $row[7];
    $mtot_taxamt += $row[8];
    $mtot_nettot += $row[9];
    $i++;
  }
    $Data.= "|-------|--------------------------|----------|-----------|---------|------------|------------|----------|--------|------|------------|\n";

    $Data.= "|                 T O T A L                   ";
    $mnum = number_format($mgr_total, 2, '.', '');
    $Data .= "|".substr(str_repeat(" ",11).$mnum,-11);
        $mnum = number_format($mgr_mba, 2, '.', '');
    $Data .= "|".substr(str_repeat(" ",11).$mnum,-9);
        $mnum = number_format($mgr_strbtnml, 2, '.', '');
    $Data .= "|".substr(str_repeat(" ",12).$mnum,-12);
        $mnum = number_format($mgr_strbtspl, 2, '.', '');
    $Data .= "|".substr(str_repeat(" ",12).$mnum,-12);
        $mnum = number_format($mgr_redct, 2, '.', '');
    $Data .= "|".substr(str_repeat(" ",10).$mnum,-10);
        $mnum = number_format($mgr_befrtx, 2, '.', '');
    $Data .= "|".substr(str_repeat(" ",8).$mnum,-8);
        $mnum = number_format($mgr_taxamt, 2, '.', '');
    $Data .= "|".substr(str_repeat(" ",6).$mnum,-6);
        $mnum = number_format($mgr_nettot, 2, '.', '');
    $Data .= "|".substr(str_repeat(" ",12).$mnum,-12)."|\n";
    $Data.= "|---------------------------------------------|-----------|---------|------------|------------|----------|--------|------|------------|\n";
  //print "<tr><td><b>Grand Total</td><td></td><td></td><td align='right'><b>".$mtot_total."</td><td align='right'><b>".$mtot_mba."</td><td align='right'><b>".$mtot_strbtnml."</td><td align='right'><b>".$mtot_strbtspl."</td><td align='right'><b>".$mtot_redct."</td><td align='right'><b>".$mtot_befrtx."</td><td align='right'><b>".$mtot_taxamt."</td><td align='right'><b>".$mtot_nettot."</td></tr>";
  //print "</table>";
//}
$Data .= chr(18);
$Data .=chr(12);
fwrite($handle, $Data);
fclose($handle);
//print "lp -o media=Custom.10x6in $file";
//$output=shell_exec("C:\\WINDOWS\\system32\\cmd.exe /c staff_bill.bat");
$output = shell_exec("lp -o media=Custom.10x6in '$file'");
header('location:staff_billreport.php');
?>