<?
session_start();
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($user)<=0)
{
   print "<script> alert('Invalid User'); document.location='inde.php' </script>";
   return;
}
$file =  'creditsale.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";
$fromdt=$_REQUEST['dfrom'];
$todt=$_REQUEST['dto'];
$mfrom_date = $fromdt;
$mto_date = $todt;
$fromdt = implode('-', array_reverse(explode('-', $fromdt)));
$todt = implode('-', array_reverse(explode('-', $todt)));
$mgrp = "";
$mgrp1[]="";
$mtotal1[]=0;
$qry1=mysqli_query($db,"select distinct gname from prod_invoice where invdate between '$fromdt' and '$todt' and brcode='$brcode' and pmode='CREDIT' group by gname");
$i=0;
$mflg = 1;
while($row=mysqli_fetch_array($qry1))
{
       if($mflg == 1)
       {
           $mgrp .= "|".$row[0]."|";
           $mgrp1[$i++]=$row[0];
       }
       if (substr($row[0],-11)=='TAXABLE VIP')
           $mflg = 0;
}
$grp_nos = $i;
$qry=mysqli_query($db,"select invno,invdate,customer,gname,sum(befrtx),sum(taxamt),sum(nettot),sum(total) from prod_invoice where invdate between '$fromdt' and '$todt' and brcode='$brcode' and pmode='CREDIT' group by invno,gname");

$Data .= "     Credit Sale Report for the period from <$mfrom_date> to <$mto_date>\n".chr(15);
$Data .= "|------|----------|------------------|---------|---------|---------|--------|---------|------|----|------|---------|-------|---------|\n";
$Data .= "|BillNo|Bill Date |       Party      | Cotton  | Reeled  | Spunsilk|Polyster|   VIP   | Wool |Yarn|Others|  Total  |  Tax  |   Net   |\n";
$Data .= "|------|----------|------------------|---------|---------|---------|--------|---------|------|----|------|---------|-------|---------|\n";
$i=1;
$minvno = "";
$mgr_total = 0;
$mgr_tax = 0;
$mgr_nettot = 0;
$mcotton = 0;
$mreel = 0;
$mspun = 0;
$mpoly = 0;
$mvip = 0;
$mwool = 0;
$myarn = 0;
$moth  = 0;
$mtotal = 0;
$mtax = 0;
$mnettot = 0;
$mtcotton = 0;
$mtreel = 0;
$mtspun = 0;
$mtpoly = 0;
$mtvip = 0;
$mtwool = 0;
$mtyarn = 0;
$mtoth  = 0;
$mttotal = 0;
$mttax = 0;
$mtnettot = 0;
while($row=mysqli_fetch_array($qry))
{
    if($minvno <> $row[0])
    {
        if($i<>1)
        {
                $mnum = number_format($mcotton, 2, '.', '');
                $Data .= substr(str_repeat(" ",9).$mnum,-9).'|';
                $mnum = number_format($mreel, 2, '.', '');
                $Data .= substr(str_repeat(" ",9).$mnum,-9).'|';
                $mnum = number_format($mspun, 2, '.', '');
                $Data .= substr(str_repeat(" ",9).$mnum,-9).'|';
                $mnum = number_format($mpoly, 2, '.', '');
                $Data .= substr(str_repeat(" ",8).$mnum,-8).'|';
                $mnum = number_format($mvip, 2, '.', '');
                $Data .= substr(str_repeat(" ",9).$mnum,-9).'|';
                $mnum = number_format($mwool, 0, '.', '');
                $Data .= substr(str_repeat(" ",6).$mnum,-6).'|';
                $mnum = number_format($myarn, 0, '.', '');
                $Data .= substr(str_repeat(" ",4).$mnum,-4).'|';
                $mnum = number_format($moth, 0, '.', '');
                $Data .= substr(str_repeat(" ",6).$mnum,-6).'|';
                $mnum = number_format($mtotal, 2, '.', '');
                $Data .= substr(str_repeat(" ",9).$mnum,-9).'|';
                $mnum = number_format($mtax, 2, '.', '');
                $Data .= substr(str_repeat(" ",7).$mnum,-7).'|';
                $mnum = number_format($mnettot, 2, '.', '');
                $Data .= substr(str_repeat(" ",9).$mnum,-9)."|\n";
        }
        $invdt = implode('-', array_reverse(explode('-', $row[1])));
        $Data .= '|'.$row[0].'|'.$invdt.'|';
        $Data .= substr($row[2].str_repeat(" ",18),0,18).'|';
        $mtcotton += $mcotton;
        $mtreel += $mreel;
        $mtspun += $mspun;
        $mtpoly += $mpoly;
        $mtvip += $mvip;
        $mtwool += $mwool;
        $mtyarn += $myarn;
        $mtoth  += $moth;
        $mttotal += $mtotal;
        $mttax += $mtax;
        $mtnettot += $mnettot;
        $mcotton = 0;
        $mreel = 0;
        $mspun = 0;
        $mpoly = 0;
        $mvip = 0;
        $mwool = 0;
        $myarn = 0;
        $moth  = 0;
        $mtotal = 0;
        $mtax = 0;
        $mnettot = 0;
    }
    if ($row[3]=='COTTON')
         $mcotton +=$row[7];
    if ($row[3]=='REELED SILK')
              $mreel +=$row[7];
    if ($row[3]=='SPUNSILK')
            $mspun +=$row[7];
    if ($row[3]=='0% TAXABLE VIP' || $row[3]=='1% TAXABLE VIP' || $row[3]=='5% TAXABLE VIP' ||$row[3]=='14.5% TAXABLE VIP')
            $mvip +=$row[7];
    if ($row[3]=='POLYSTER')
            $mpoly +=$row[7];
    if ($row[3]=='WOOLEN')
            $mwool +=$row[7];
    if ($row[3]=='YARN')
            $myarn +=$row[7];
    if ($row[3]=='OTHERS')
            $moth +=$row[7];
    $mtotal += $row[7];
    $mtax += $row[5];
    $mnettot += $row[6];
    $mgr_total += $row[3];
    $i++;
    $minvno = $row[0];
}
$mnum = number_format($mcotton, 2, '.', '');
$Data .= substr(str_repeat(" ",9).$mnum,-9).'|';
$mnum = number_format($mreel, 2, '.', '');
$Data .= substr(str_repeat(" ",9).$mnum,-9).'|';
$mnum = number_format($mspun, 2, '.', '');
$Data .= substr(str_repeat(" ",9).$mnum,-9).'|';
$mnum = number_format($mpoly, 2, '.', '');
$Data .= substr(str_repeat(" ",8).$mnum,-8).'|';
$mnum = number_format($mvip, 2, '.', '');
$Data .= substr(str_repeat(" ",9).$mnum,-9).'|';
$mnum = number_format($mwool, 0, '.', '');
$Data .= substr(str_repeat(" ",6).$mnum,-6).'|';
$mnum = number_format($myarn, 0, '.', '');
$Data .= substr(str_repeat(" ",4).$mnum,-4).'|';
$mnum = number_format($moth, 0, '.', '');
$Data .= substr(str_repeat(" ",6).$mnum,-6).'|';
$mnum = number_format($mtotal, 2, '.', '');
$Data .= substr(str_repeat(" ",9).$mnum,-9).'|';
$mnum = number_format($mtax, 2, '.', '');
$Data .= substr(str_repeat(" ",7).$mnum,-7).'|';
$mnum = number_format($mnettot, 2, '.', '');
$Data .= substr(str_repeat(" ",9).$mnum,-9)."|\n";
        $mtcotton += $mcotton;
        $mtreel += $mreel;
        $mtspun += $mspun;
        $mtpoly += $mpoly;
        $mtvip += $mvip;
        $mtwool += $mwool;
        $mtyarn += $myarn;
        $mtoth  += $moth;
        $mttotal += $mtotal;
        $mttax += $mtax;
        $mtnettot += $mnettot;

$Data .= "|------------------------------------|---------|---------|---------|--------|---------|------|----|------|---------|-------|---------|\n";
$Data .= "|                TOTAL               |";
$mnum = number_format($mtcotton, 2, '.', '');
$Data .= substr(str_repeat(" ",9).$mnum,-9).'|';
$mnum = number_format($mtreel, 2, '.', '');
$Data .= substr(str_repeat(" ",9).$mnum,-9).'|';
$mnum = number_format($mtspun, 2, '.', '');
$Data .= substr(str_repeat(" ",9).$mnum,-9).'|';
$mnum = number_format($mtpoly, 2, '.', '');
$Data .= substr(str_repeat(" ",8).$mnum,-8).'|';
$mnum = number_format($mtvip, 2, '.', '');
$Data .= substr(str_repeat(" ",9).$mnum,-9).'|';
$mnum = number_format($mtwool, 0, '.', '');
$Data .= substr(str_repeat(" ",6).$mnum,-6).'|';
$mnum = number_format($mtyarn, 0, '.', '');
$Data .= substr(str_repeat(" ",4).$mnum,-4).'|';
$mnum = number_format($mtoth, 0, '.', '');
$Data .= substr(str_repeat(" ",6).$mnum,-6).'|';
$mnum = number_format($mttotal, 2, '.', '');
$Data .= substr(str_repeat(" ",9).$mnum,-9).'|';
$mnum = number_format($mttax, 2, '.', '');
$Data .= substr(str_repeat(" ",7).$mnum,-7).'|';
$mnum = number_format($mtnettot, 2, '.', '');
$Data .= substr(str_repeat(" ",9).$mnum,-9)."|\n";
$Data .= "|------------------------------------|---------|---------|---------|--------|---------|------|----|------|---------|-------|---------|\n";
$Data .= chr(18);
$Data .= chr(12);
fwrite($handle, $Data);
fclose($handle);
$output = shell_exec("lp -o media=Custom.10x6in '$file'");
$output=shell_exec("C:\\WINDOWS\\system32\\cmd.exe /c creditsale.bat");
$fromdt = implode('-', array_reverse(explode('-', $fromdt)));
$todt = implode('-', array_reverse(explode('-', $todt)));
header('location:creditsale_report.php?fromdt='.$fromdt.'&&todt='.$todt);

?>