<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
$user_level=$_SESSION['user_level'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='../inde.php' </script>";
  return;
}
include_once("../test.php");
include_once("../opendb.php");
$sea=$_REQUEST['sea'];
$df=$_REQUEST['dfrom'];
$dt=$_REQUEST['dto'];

$brres=mysqli_query($db,"select * from branch where brcode='$brcode'");
$brrow=mysqli_fetch_array($brres);
$brnchnm= $brrow[1];
$query1 = "SELECT * FROM ledger where brcode = '$brcode' and grp = 'Sundry Debtors' order by led";

//if (date('m',time())<4)
  //$myear = (date('Y',time())-1);
//else
  //$myear = (date('Y',time()));
  $mfrom_date = $df;
  $mto_date= $dt;
  $df = implode('-', array_reverse(explode('-', $df)));
  $dt = implode('-', array_reverse(explode('-', $dt)));
  $file =  'debt_rpt.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 .= "       Debtors Report for the period from <$mfrom_date> to <$mto_date>";
  $Data .= chr(15)."\n\n";
  $Data .= "|----------------------------------|-----------------|-----------------|-----------------|-----------------|-----------------|-----------------|\n";
  $Data .= "|           Debtor Name            | Opening Balance | Purchase Amount |Collection Amount|Collection Amount| CHQ dishonoured | Closing Balance |\n";
  $Data .= "|                                                    |                 |     By Cash     |     By Bank     |       Amt       |                 |\n";
  $Data .= "|----------------------------------|-----------------|-----------------|-----------------|-----------------|-----------------|-----------------|\n";
$result = mysqli_query($db,$query1);
$i=1;
$mtop_bal=0;
$mtdramt=0;
$mtcramt=0;
$mtbnk_amt=0;
$mtcl_bal=0;
while($row=mysqli_fetch_array($result))
{
  






$mjour = "select sum(dramt),sum(cramt) from journal2 where partic = '$row[0]' and date < '$df' ";
			//echo $mjour;
  $jrow = mysqli_fetch_array(mysqli_query($db,$mjour));
  $mdramt = $jrow[0];
  $mcramt = $jrow[1];
  $mop_bal = $row[2]+$mdramt-$mcramt;
  $mjour = "select sum(dramt),sum(cramt) from journal2 where partic = '$row[0]' and date >= '$df' and date <= '$dt' and j_type = 'BR'";
  $jrow1 = mysqli_fetch_array(mysqli_query($db,$mjour));
  $mdramt = $jrow1[0];
  $mbnk_amt = $jrow1[1];
  $mcl_bal = $mop_bal+$mdramt-$mbnk_amt;
  $mjour = "select sum(dramt),sum(cramt) from journal2 where partic = '$row[0]' and date >= '$df' and date <= '$dt' and j_type = 'JV'";
  $jrow1 = mysqli_fetch_array(mysqli_query($db,$mjour));
  $mjvdramt = $jrow1[0];
  $mjvcramt = $jrow1[1];
  $mcl_bal = $mcl_bal+$mjvdramt-$mjvcramt;



  $mjour = "select sum(dramt),sum(cramt) from journal2 where partic = '$row[0]' and date >= '$df' and date <= '$dt' and j_type = 'BP'";
  //echo $mjour;
  //echo "<br>";
  $jrow1 = mysqli_fetch_array(mysqli_query($db,$mjour));
  $mjvdramt1 = $jrow1[0];
  $mjvbnk = $jrow1[1];
  $mcl_bal = $mcl_bal+$mjvdramt-$mjvbnk;

  $mjour = "select sum(dramt),sum(cramt) from journal2 where partic = '$row[0]' and date >= '$df' and date <= '$dt' and j_type !='BR' and j_type !='BP' and j_type !='JV'";
  $jrow1 = mysqli_fetch_array(mysqli_query($db,$mjour));
  $mdramt = $jrow1[0];
  $mcramt = $jrow1[1];
  $mcl_bal = $mcl_bal+$mdramt-$mcramt;


















  if (abs($mop_bal)>0  || abs($mdramt)>0 || abs($mcramt)>0 || abs($mbnk_amt)>0 || abs($mcl_bal)>0)
  {
      $Data .= "| ".substr($row[0].str_repeat(" ",33),0,33);
      $mnum = number_format($mop_bal, 2, '.', '');
      $Data .= "|".substr(str_repeat(" ",16).$mnum,-16)." ";
      $mnum = number_format($mdramt, 2, '.', '');
      $Data .= "|".substr(str_repeat(" ",16).$mnum,-16)." ";
      $mnum = number_format($mcramt, 2, '.', '');
      $Data .= "|".substr(str_repeat(" ",16).$mnum,-16)." ";
      $mnum = number_format($mbnk_amt, 2, '.', '');
      $Data .= "|".substr(str_repeat(" ",16).$mnum,-16)." ";

		 $mnum = number_format($mjvdramt, 2, '.', '');
		 $Data .= "|".substr(str_repeat(" ",16).$mnum,-16)." ";

      $mnum = number_format($mcl_bal, 2, '.', '');
      $Data .= "|".substr(str_repeat(" ",16).$mnum,-16)." |\n";
  }
  //print"\n<form action='editledger.php'><input type=hidden name=ledg value='$row[0]'>";
  //print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'>";
  //print "<td>".$i++."</td><td><a href='vmledgerdt.php?cd=".$row[0]."&sea=$sea&df=$df&dt=$dt' >$row[0]</a></td><td align=right>".number_format($mop_bal,2,'.',',')."</td><td align=right>".number_format($mdramt,2,'.',',')."</td><td align=right>".number_format($mcramt,2,'.',',')."</td><td align='right'>".number_format($mbnk_amt,2,'.',',')."</td><td align=right>".number_format($mcl_bal,2,'.',',')."</td>";
  $mtop_bal+=$mop_bal;
  $mtdramt+=$mdramt;
  $mtjvdramt+=$mjvdramt;
  $mtcramt+=$mcramt;
  $mtbnk_amt+=$mbnk_amt;
  $mtcl_bal+=$mcl_bal;
  //print "<td>".$i++."</td><td><a href='vmledger-test.php?cd=".$row[0]."&sea=$sea'>$row[0]</a></td><td>$row[1]</td><td align=right>".putComa($row[2])."</td><td align=right>$row[3]</td>";
  //print "</tr>";
}
  $Data .= "|----------------------------------|-----------------|-----------------|-----------------|-----------------|-----------------|-----------------|\n";
  $Data .= "|           T O T A L              ";
  $mnum = number_format($mtop_bal, 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",16).$mnum,-16)." ";
  $mnum = number_format($mtdramt, 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",16).$mnum,-16)." ";
  $mnum = number_format($mtcramt, 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",16).$mnum,-16)." ";
  $mnum = number_format($mtbnk_amt, 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",16).$mnum,-16)." ";
  $mnum = number_format($mtjvdramt, 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",16).$mnum,-16)." ";
  $mnum = number_format($mtcl_bal, 2, '.', '');
  $Data .= "|".substr(str_repeat(" ",16).$mnum,-16)." |\n";
//print "<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'>";
//print "<td></td><td><b>T O T A L</td><td align=right><b>".number_format($mtop_bal,2,'.',',')."</td><td align=right><b>".number_format($mtdramt,2,'.',',')."</td><td align=right><b>".number_format($mtcramt,2,'.',',')."</td><td align='right'><b>".number_format($mtbnk_amt,2,'.',',')."</td><td align=right><b>".number_format($mtcl_bal,2,'.',',')."</td>";
  $Data .= "|----------------------------------|-----------------|-----------------|-----------------|-----------------|-----------------|-----------------|\n";
  $Data .= chr(18);
$Data .= chr(12);
fwrite($handle, $Data);
fclose($handle);
//print "lp -o media=Custom.10x6in $file";
$output = shell_exec("lp -o media=Custom.10x6in '$file'");
$output=shell_exec("C:\\WINDOWS\\system32\\cmd.exe /c debt_rpt.bat");
header('location:listdebtors.php');
?>