<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
$branch=$_SESSION['branch'];
$df=$_REQUEST['dfrom'];
$dt=$_REQUEST['dto'];
$cd=$_REQUEST['lednm'];
$mop=$_REQUEST['opbl'];
if(strlen($brcode)<=0)
{
    print "<script> alert('Invalid User'); document.location='ind.php' </script>";
    return;
}
include_once("../test.php");
include_once ("../opendb.php");
$cd=str_replace('~','&',$cd);
$re=mysqli_query($db,"select * from ledger where led='$cd' and brcode='$brcode'");
$opb=mysqli_fetch_array($re);
?>
<style>
td{font-family:'verdana';font-size:14;color:black;}
th{font-family:'verdana';font-size:14;color:white;background-color:#5b5858}
body{background-color:ivory}
</style>
<link rel="stylesheet" href="style.css" type="text/css" />
<?php //include_once('top.php');
$file =  'custledger.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 .= "       Ledger Details of $cd for the period from <$df> to <$dt>\n".chr(15);
$Data .= "       |----------|------------------------------------------------------------------|--------------|--------------|--------------|\n";
$Data .= "       |   Date   |                             Details                              |    Debit     |    Credit    |   Balance    |\n";
$Data .= "       |----------|------------------------------------------------------------------|--------------|--------------|--------------|\n";
$tr=0.0;$tp=0.0;$tr1=0.0;
$sql="select * from ledger where led='$cd' and brcode='$brcode'";
$re=mysqli_query($db,$sql);
$opb=mysqli_fetch_array($re);
if(strtoupper($opb[drcr])=="DR")
{
	if($mop>=0)
	{
  	    $Data .= "       |          | Opening Balance                                                   |";
        $mnum = number_format($mop, 2, '.', '');
        $Data .= substr(str_repeat(" ",14).$mnum,-14)."|              |              |\n";
//		print "\n<tr style={background-color:silver}><td></td> <td>Opening balance</td><td align=right>".number_format($mop,2,'.',',')."</td><td colspan=4></td></tr>";
		$tr+=$mop;
		$tr1+=$mop;
	}
	else
	{
  	    $Data .= "       |          | Opening Balance                                                   |              |";
        $mnum = number_format(abs($mop), 2, '.', '');
        $Data .= substr(str_repeat(" ",14).$mnum,-14)."|              |\n";
//		print "\n<tr style={background-color:silver}><td></td> <td>Opening balance</td><td></td><td align=right>".number_format(abs($mop),2,'.',',')."</td><td colspan=3></td></tr>";
		$tp+=abs($mop);
		$tr1+=$mop;
	}
}
else if(strtoupper($opb[drcr])=="CR")
{
	if($mop>=0)
	{
  	    $Data .= "       |          | Opening Balance                                                   |              |";
        $mnum = number_format($mop, 2, '.', '');
        $Data .= substr(str_repeat(" ",14).$mnum,-14)."|              |\n";
//		print "\n<tr style={background-color:silver}><td></td> <td>Opening balance</td><td></td><td align=right>".number_format($mop,2,'.',',')."</td><td colspan=4></td></tr>";
		$tp+=$mop;
		$tr1+=-($mop);
	}
	else
	{
  	    $Data .= "       |          | Opening Balance                                                   |";
        $mnum = number_format(abs($mop), 2, '.', '');
        $Data .= substr(str_repeat(" ",14).$mnum,-14).'|              |              |\n';
//		print "\n<tr style={background-color:silver}><td></td> <td>Opening balance</td><td align=right>".number_format(abs($mop),2,'.',',')."</td><td colspan=3></td></tr>";
		$tr+=abs($mop);
		$tr1+=$mop;
	}
}
$sql="select * from journal2 where brcode='$brcode' and partic='$cd' and date>='$df' and date<='$dt' order by date,jvno ";
$result=mysqli_query($db,$sql) or die ('Error'.mysqli_error($db));
while($row=mysqli_fetch_array($result))
{
   if($row[type]=='Cr') //$row[cr]==$cd)
    {
        $tr1-=$row[cramt];
  	    $Data .= "       |".$row[date]."|";
        $Data .= substr("To $row[narr]-($row[j_type])".str_repeat(" ",66),0,66)."|              |";
        $mnum = number_format($row[cramt], 2, '.', '');
        $Data .= substr(str_repeat(" ",14).$mnum,-14)."|";
        $mnum = number_format($tr1, 2, '.', '');
        $Data .= substr(str_repeat(" ",14).$mnum,-14)."|\n";

        $tp+=$row[cramt];
    }
    else //if($row[dr]==$cd)
    {
        $tr1+=$row[dramt];
  	    $Data .= "       |".$row[date]."|";
        $Data .= substr("By $row[narr]-($row[j_type])".str_repeat(" ",66),0,66)."|";
        $mnum = number_format($row[dramt], 2, '.', '');
        $Data .= substr(str_repeat(" ",14).$mnum,-14)."|              |";
        $mnum = number_format($tr1, 2, '.', '');
        $Data .= substr(str_repeat(" ",14).$mnum,-14)."|\n";
    }
    $tr+=$row[dramt];
 }
$Data .= "       |          |                           TOTAL                                  |";
$mnum = number_format(abs($tr), 2, '.', '');
$Data .= substr(str_repeat(" ",14).$mnum,-14).'|';
$mnum = number_format(abs($tp), 2, '.', '');
$Data .= substr(str_repeat(" ",14).$mnum,-14)."|\n";
//print "<tr style={background-color:silver}><td colspan=2 align=center><font color=black><B>Total</td>";
//print "<td align=right><font color=black>".number_format($tr,2,'.',',')."</td>";
//print "<td align=right><font color=black>".number_format(abs($tp),2,'.',',')."</td><td colspan=2></td></tr>";

/*print ($tp-$tr);
$bal=($tr-$tp);
//*******************************************************
// if(strtoupper($opb[drcr])=="DR" && $mop<0)
// 	$bal*=-1;
  //$bal=number_format($bal,2,'','');
//$bal=number_format($bal,2,'.',','); //,2,'','');
//********************************************************
*/
$balres=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where brcode='$brcode' and partic='$cd' and date >= '$df' and date <= '$dt' ");
 $balrow=mysqli_fetch_row($balres);

 if($opb[drcr]=='Dr')
 {
    if($mop>=0)
	$bal=($balrow[0]+$mop)-$balrow[1]; //print '<br>bal'.$row[2];
     else
	$bal=($balrow[1]+abs($mop))-$balrow[0]; //print '<br>bal'.$row[2];
 }
 else
{
    if($mop>=0)
	 $bal=($balrow[1]+$mop)-$balrow[0];
    else
	 $bal=($balrow[0]+abs($mop))-$balrow[1];
}

if($tr>$tp)
{
    $Data .= "       |          |                           Balance                                |";
    $mnum = number_format(abs($bal), 2, '.', '');
    $Data .= substr(str_repeat(" ",14).$mnum,-14)."|Debit         |              |\n";
//print "\n<tr><td colspan=2 align=center><b><font color=black>Balance</td><td align=right><font color=black><b>".number_format(abs($bal),2,'.',',')."</td><td><font color=black><b> Debit </td><td colspan=2></td></tr>";
}
else
{
//print "\n<tr><td colspan=2 align=center><b><font color=black>Balance</td><td align=right><font color=black><b>".number_format(abs($bal),2,'.',',')."</td><td><font color=black> <b>Credit </td><td colspan=2></td></tr>";
    $Data .= "       |          |                           Balance                                |";
    $mnum = number_format(abs($bal), 2, '.', '');
    $Data .= substr(str_repeat(" ",14).$mnum,-14)."|Credit         |              |\n";
}
$Data .= "       |----------|------------------------------------------------------------------|--------------|--------------|--------------|\n";
$Data .= chr(18);
$Data .= chr(12);
fwrite($handle, $Data);
fclose($handle);
$output = shell_exec("lp -o media=Custom.10x6in '$file'");
header('location:vmdebtordt.php');
