<?
session_start();
include_once("test.php");
include_once("opendb.php");
include_once("includes/function.php");
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
$crno=$_REQUEST['crno'];
$flg=$_REQUEST['flg'];

if(strlen($user)<=0)
{
    print "<script> alert('Invalid User'); document.location='inde.php' </script>";
    return;
}
$company=mysqli_query($db,"select * from company_master  where brcode='$brcode'")or die(mysqli_error($db));
$company1=mysqli_fetch_row($company);
$add =explode(",", $company1[4]);
$add1=$company1[5];
$cont='Tel:'.$company1[7].",".'Fax:'.$company1[8];
$add2=$company1[6];
$tran=mysqli_query($db,"select * from cshrec  where brcode='$brcode' and crno='$crno' ")or die(mysqli_error($db));
$tran1=mysqli_fetch_row($tran);
$amount=explode('.', $tran1[5]);
?>
<script type="text/javascript" src="number_to_word.js"></script>
 <style>
  .conts{

   border:0px solid black;
   margin-left:0px
   margin-right:0px;
  vertical-align:bottom;
   }
 .conts1{

   border:0px solid black;
   border-bottom:1px solid black;
   border-style:dotted;
   margin-left:0px
   margin-right:0px;
   vertical-align:bottom;
   min-width:600px;
  }

  .cshtot table, td,th {
  width :90px;
  height:30px;
  bordre:1px solid black;
  border-collapse:collapse;
  }
 p{

  margin:0px;
  padding:0px;
 }
#add_td{
font-size:12px;
height:15px;
}
#add_td br{
margin:0px;
padding:0px;
}
</style>
</head>
<?
$tr=0;$tp=0;
$ssq="select sum(dramt)-sum(cramt) from journal2 where brcode='$brcode' and (partic='$tran1[3]')";
//echo $ssq;
$res2=mysqli_query($db,$ssq)or die('jour err:'.mysqli_error($db));
$r2=mysqli_fetch_row($res2);

$re=mysqli_query($db,"select * from ledger where led='$tran1[3]' and brcode='$brcode'");
$opb=mysqli_fetch_array($re);

if($opb[drcr]=='Cr' and $r2[0]<0)
{
$openbal=$opb[op]+abs($r2[0]);
$tp+=$openbal;
}

else if($opb[drcr]=='Cr' and $r2[0]>=0)
{
$openbal=$opb[op]-$r2[0];
if($openbal>0)
{

$tp+=$openbal;
}
else
{

$tr+=$openbal;
}

}


if($opb[drcr]=='Dr' and $r2[0]>=0)
{
if($opb[op]>=0)
$openbal= $opb[op] +abs($r2[0]);
else
$openbal= $opb[op]+$r2[0];

if($openbal>0)
{

$tr+=$openbal;
}
else
{

$tp+=abs($openbal);
}
}
if($opb[drcr]=='Dr' and $r2[0]<0)
{
if($opb[op]>=0)
$openbal= $opb[op]+ $r2[0];
else
$openbal= $opb[op]+$r2[0];

//print $opb[op].'-'.$r2[0];
 if($openbal>=0)
    {
    
     $tr+=$openbal;
     }
 else
    {
    
     $tp+=$openbal;
     }

}



$bal=$tr-$tp+$r[0];

 $totdramt+=abs($tr);

 $totcramt+=abs($tp);






  	$file =  'cshrec_prn.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 .= $pagelenth;
	$Data .=" \n\n\n\n\n\n\n";
   	$Data .=" \n\n\n\n\n\n";
    $Data .= "                              Cash /Cheque Receipt Voucher\n";
    $Data .= " Cash Receipt No. : ".$crno."\n\n";
    $Data .= " Date  : ".month_alph($tran1[0])."\n\n";
    $Data .= " Received From : ".$tran1[3]."\n\n";
//    $mwords = toWords($tran1[5])." only";
    $mamt = round($tran1[5],2);
    $mwords = word($mamt);
    $Data .= " The Sum Of ".$mamt.' ('.$mwords.")\n";
    $Data .= " For ".$tran1[6]."\n";
    $Data .= " By Cash/Cheque : Cash \n";
    $Data .= " Total ".round($amount[0],2)." ".round($amount[1],2)."\n";
    $Data .= " Manager                    Accountant                 Receiver\n\n";
    $Data .= " --------------             --------------             --------------\n";
	$Data .= "Balance Amount Due : ".$bal."											 ";
	$Data .= chr(18);
	$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 cshrec_prn.bat");
   
	if($flg==1)
	{
		header('location:cshrec.php');
	}
	else
	{
		 header('location:accounts/print_cshrec1.php');
	}
?>
