<?
session_start();

if(strlen($brcode)<=0)
{
  print "<script> alert('Invalid User'); document.location='ind.php' </script>";
  return;
}

include("test.php");
include ("opendb.php");
?>
<style>
// td{font-family:'arial narrow';font-size:14;color:black;}
// th{font-family:'arial narrow';font-size:14;color:white;background-color:#5b5858}
// body{background-color:ivory}
</style>
<link rel="stylesheet" href="style.css" type="text/css" />
<font face='arial narrow' size=3 >Transaction Details of <b><? print $cd?> </font>
<form action='<?print $PHP_SELF?>'>
<input type=hidden name=cd value='<?print $cd?>'>
<input type=hidden name=sea value='<?print $sea?>'>
<?
if($d&&$m)
{
?>
<table>
  <tr><td>Date from </td>
      <td>dd<input type=text name='d' size=2 value='<?print $d?>'>
	  mm<input type=text name='m' size=2 value='<?Print $m?>'>
          yy<input type=text name='y' size=4 value='<?Print $y?>'>
      </td></tr>
   <tr>
     <td> To </td>
      <td>dd<input type=text name='d1' size=2 value='<?Print $d1 ?>'>
	  mm<input type=text name='m1' size=2 value='<?Print $m1 ?>'>
          yy<input type=text name='y1' size=4 value='<?Print $y1 ?>'>
      </td><td>Pswd <input type=password name='psd'></td>
      <td>
<input type=submit value='Show' name='show'><input type=submit value='Print' name='prn' ></td>
   </tr>
</table>
<?
}
else
{
?>
<table>
  <tr><td>Date from </td>
      <td>dd<input type=text name='d' size=2 value='1'>
	  mm<input type=text name='m' size=2 value='04'>
          yy<input type=text name='y' size=4 value='<?Print (date('Y',time())-1)?>'>
      </td></tr>
   <tr>
     <td> To </td>
      <td>dd<input type=text name='d1' size=2 value='<?Print date('d',time())?>'>
	  mm<input type=text name='m1' size=2 value='<?Print date('m',time())?>'>
          yy<input type=text name='y1' size=4 value='<?Print date('Y',time())?>'>
      </td><td>Pswd <input type=password name='psd'></td>
      <td><input type=submit value='Show' name='show'><input type=submit value='Print' name='prn' ></td>
   </tr>
</table>
<?
}
?>
</form>
<?
$re11=mysqli_query($db,"select * from ledger where led='$cd' and brcode='$brcode'");
$opbbb=mysqli_fetch_array($re11);
$grp=$opbbb[grp];


if(($prn&&$psd=='ccn'&&$grp!='Sundry Debtors') || ($brcode =='PSN172' && $prn) || ($brcode =='PSN236' && $prn))
{
$cd=str_replace('~','&',$cd);
$df=$y.'-'.$m.'-'.$d;
$dt=$y1.'-'.$m1.'-'.$d1;
print "<script> window.open('prnledgerdw.php?df=".$df."&dt=".$dt."&cd=".$cd."'); </script>";
return;
}
elseif(($show&&$psd=='ccn'&&$grp!='Sundry Debtors') || ($brcode =='PSN236' && $show))
{
$cd=str_replace('~','&',$cd);
$df=$y.'-'.$m.'-'.$d;
$dt=$y1.'-'.$m1.'-'.$d1;
print " <center>Transactions from ".datetohtml($df).' to '.datetohtml($dt)."</center>";
?>
<table  cellspacing=1 cellpadding=1 width=100%>
<th width=80>Date <th>Vouch/Rec. No. <th>Debit <th>Credit <th>Ref. 

<?
$tr=0;$tp=0;
$ssq="select sum(dramt)-sum(cramt) from journal2 where brcode='$brcode' and date<'$df' and (partic='$cd')";

$res2=mysqli_query($db,$ssq)or die('jour err:'.mysqli_error());
$r2=mysqli_fetch_row($res2);

$re=mysqli_query($db,"select * from ledger where led='$cd' and brcode='$brcode'");
$opb=mysqli_fetch_array($re);
//print 'Type :'.$opb[drcr];
if($opb[drcr]=='Cr' and $r2[0]<0)
{
//$openbal=$opb[op]+abs($r2[0]);print 'op'.$opb[op].' d -op'.$r2[0];
// following if else is changed ****************************
if($opb[op]<0)
 $openbal=$opb[op]+abs($r2[0]);
else
$openbal=$opb[op]+abs($r2[0]);
//print $opb[op].'--'.$r2[0];
//**********************************************************
print "<tr><td></td><td> Opening Balance</td><td></td><td align=right>".putComa($openbal)."</td><td colspan=2></td>";
$tp+=$openbal;
}
else if($opb[drcr]=='Cr' and $r2[0]>=0)
{
$openbal=$opb[op]-$r2[0]; 
if($openbal>0)
{
print "<tr><td></td><td> Opening Balance</td><td></td><td align=right>".putComa($openbal)."</td><td colspan=2></td>";
$tp+=$openbal;
}
else
{
print "<tr><td></td><td> Opening Balance</td><td align=right>".putComa(abs($openbal))."</td><td colspan=2></td>";
$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)
{
print "<tr><td></td><td> Opening Balance</td><td align=right>".putComa($openbal)."</td><td colspan=2></td>";
$tr+=$openbal;
}
else
{
print "<tr><td></td><td> Opening Balance</td><td>&nbsp;</td><td align=right>".putComa(abs($openbal))."</td><td colspan=2></td>";
$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];


 if($openbal>=0)
    {
    print "<tr><td></td><td> Opening Balance</td><td align=right>".putComa($openbal)."</td><td colspan=2></td>";
     $tr+=$openbal;
     }
 else
    {
    print "<tr><td></td><td> Opening Balance</td><td></td><td align=right>".putComa(abs($openbal))."</td><td colspan=2></td>";
     $tp+=$openbal;
     }

}
$sql="select * from journal2 where brcode='$brcode' and partic='$cd' and date>='$df' and date<='$dt'order by date ";

$result=mysqli_query($db,$sql) or die (mysqli_error());

while($row=mysqli_fetch_array($result))
{
   if($row[type]=='Cr') //$row[cr]==$cd)
    {
 print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td>".datetohtml($row[date])."</td><td> &nbsp;&nbsp; <a href='multientry/viewtrans.php?code=$row[jvno]' title='Ref:$row[no]'> To $row[narr]-($row[j_type])</a></td><td align=right></td><td align=right>".putComa($row[cramt])."</td> <td>$row[against]:$row[no]</td>";
//<td align=center><input type=button value='Del' onClick=document.location='deldat.php?code=$row[0]'></td>
print "</tr>";
$totcramt+=$row[cramt];
  }
 else //if($row[dr]==$cd)
  {
 print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td>".datetohtml($row[date])."</td><td> <a href='multientry/viewtrans.php?code=$row[jvno]' title='Ref:$row[against] $row[no]'>By $row[narr]-($row[j_type])</a></td><td align=right>".putComa($row[dramt])."</td><td align=right></td> <td>$row[against]:$row[no]</td>";
//<td align=center><input type=button value='Del' onClick=document.location='deldat.php?code=$row[0]'></td>
print "</tr>";

$totdramt+=$row[dramt];
 }

}
print "<tr style={background-color:silver}><td colspan=2 align=center><font color=black><B>Total</td>"; 

 $totdramt+=abs($tr);

 //$totcramt+=abs($tp); changed now
$totcramt+=abs($tp);
print "<td align=right><font color=black>".putComa($totdramt)."</td>";
print "<td align=right><font color=black>".putComa(abs($totcramt))."</td><td colspan=2></td></tr>";

/*if($totdramt>=0)
$bal=($totdramt-$totcramt);
else
{$bal=($totdramt+$totcramt); $bal*=-1;}
*/

if(abs($totdramt)>abs($totcramt))
{
$r1=mysqli_query($db,"select $totdramt-$totcramt");
$trow=mysqli_fetch_array($r1);
$bal=abs($trow[0]);
//$bal=(abs($totdramt)-abs($totcramt));
print "\n<tr><td colspan=2 align=center><b><font color=black>Balance</td><td align=right><font color=black><b>".putcoma(abs($bal))."</td><td><font color=black><b> Debit </td><td colspan=2></td></tr>";
}
else if(abs($totdramt)<abs($totcramt))
{
$r11=mysqli_query($db,"select $totcramt-$totdramt");
$trow=mysqli_fetch_array($r11);
$bal=abs($trow[0]);

//$bal=(abs($totcramt)-abs($totdramt)); 

print "\n<tr><td colspan=2 align=center><b><font color=black>Balance</td><td></td><td align=right><font color=black><b>".putcoma(abs($bal))."</td><td><font color=black> <b>Credit </td> </tr>";
}


//*******************************************************
// if(strtoupper($opb[drcr])=="DR" && $opb[op]<0)
// 	$bal*=-1;
  $bal=number_format($bal,2,'','');
//********************************************************



//**************************************************************8888888888888888888888888
?>
</table><br>
<?
} // end main if

//=============================================================================================
elseif($prn&&$grp=='Sundry Debtors')
{
$cd=str_replace('~','&',$cd);
$df=$y.'-'.$m.'-'.$d;
$dt=$y1.'-'.$m1.'-'.$d1;
print "<script> window.open('prnledgerdw.php?df=".$df."&dt=".$dt."&cd=".$cd."'); </script>";
return;
}
elseif($show&&$grp=='Sundry Debtors')
{
$cd=str_replace('~','&',$cd);
$df=$y.'-'.$m.'-'.$d;
$dt=$y1.'-'.$m1.'-'.$d1;
print " <center>Transactions from ".datetohtml($df).' to '.datetohtml($dt)."</center>";
?>
<table  cellspacing=1 cellpadding=1 width=100%>
<th width=80>Date <th>Vouch/Rec. No. <th>Debit <th>Credit <th>Ref. 

<?
$tr=0;$tp=0;
$ssq="select sum(dramt)-sum(cramt) from journal2 where brcode='$brcode' and date<'$df' and (partic='$cd')";

$res2=mysqli_query($db,$ssq)or die('jour err:'.mysqli_error());
$r2=mysqli_fetch_row($res2);

$re=mysqli_query($db,"select * from ledger where led='$cd' and brcode='$brcode'");
$opb=mysqli_fetch_array($re);
//print 'Type :'.$opb[drcr];
if($opb[drcr]=='Cr' and $r2[0]<0)
{
//$openbal=$opb[op]+abs($r2[0]);print 'op'.$opb[op].' d -op'.$r2[0];
// following if else is changed ****************************
if($opb[op]<0)
 $openbal=$opb[op]+abs($r2[0]);
else
$openbal=$opb[op]+abs($r2[0]);
//print $opb[op].'--'.$r2[0];
//**********************************************************
print "<tr><td></td><td> Opening Balance</td><td></td><td align=right>".putComa($openbal)."</td><td colspan=2></td>";
$tp+=$openbal;
}
else if($opb[drcr]=='Cr' and $r2[0]>=0)
{
$openbal=$opb[op]-$r2[0]; 
if($openbal>0)
{
print "<tr><td></td><td> Opening Balance</td><td></td><td align=right>".putComa($openbal)."</td><td colspan=2></td>";
$tp+=$openbal;
}
else
{
print "<tr><td></td><td> Opening Balance</td><td align=right>".putComa(abs($openbal))."</td><td colspan=2></td>";
$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)
{
print "<tr><td></td><td> Opening Balance</td><td align=right>".putComa($openbal)."</td><td colspan=2></td>";
$tr+=$openbal;
}
else
{
print "<tr><td></td><td> Opening Balance</td><td>&nbsp;</td><td align=right>".putComa(abs($openbal))."</td><td colspan=2></td>";
$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];


 if($openbal>=0)
    {
    print "<tr><td></td><td> Opening Balance</td><td align=right>".putComa($openbal)."</td><td colspan=2></td>";
     $tr+=$openbal;
     }
 else
    {
    print "<tr><td></td><td> Opening Balance</td><td></td><td align=right>".putComa(abs($openbal))."</td><td colspan=2></td>";
     $tp+=$openbal;
     }

}
$sql="select * from journal2 where brcode='$brcode' and partic='$cd' and date>='$df' and date<='$dt'order by date ";

$result=mysqli_query($db,$sql) or die (mysqli_error());

while($row=mysqli_fetch_array($result))
{
   if($row[type]=='Cr') //$row[cr]==$cd)
    {
 print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td>".datetohtml($row[date])."</td><td> &nbsp;&nbsp; <a href='multientry/viewtrans.php?code=$row[jvno]' title='Ref:$row[no]'> To $row[narr]-($row[j_type])</a></td><td align=right></td><td align=right>".putComa($row[cramt])."</td> <td>$row[against]:$row[no]</td>";
//<td align=center><input type=button value='Del' onClick=document.location='deldat.php?code=$row[0]'></td>
print "</tr>";
$totcramt+=$row[cramt];
  }
 else //if($row[dr]==$cd)
  {
 print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td>".datetohtml($row[date])."</td><td> <a href='multientry/viewtrans.php?code=$row[jvno]' title='Ref:$row[against] $row[no]'>By $row[narr]-($row[j_type])</a></td><td align=right>".putComa($row[dramt])."</td><td align=right></td> <td>$row[against]:$row[no]</td>";
//<td align=center><input type=button value='Del' onClick=document.location='deldat.php?code=$row[0]'></td>
print "</tr>";

$totdramt+=$row[dramt];
 }

}
print "<tr style={background-color:silver}><td colspan=2 align=center><font color=black><B>Total</td>"; 

 $totdramt+=abs($tr);

 //$totcramt+=abs($tp); changed now
$totcramt+=abs($tp);
print "<td align=right><font color=black>".putComa($totdramt)."</td>";
print "<td align=right><font color=black>".putComa(abs($totcramt))."</td><td colspan=2></td></tr>";

/*if($totdramt>=0)
$bal=($totdramt-$totcramt);
else
{$bal=($totdramt+$totcramt); $bal*=-1;}
*/

if(abs($totdramt)>abs($totcramt))
{
$r1=mysqli_query($db,"select $totdramt-$totcramt");
$trow=mysqli_fetch_array($r1);
$bal=abs($trow[0]);
//$bal=(abs($totdramt)-abs($totcramt));
print "\n<tr><td colspan=2 align=center><b><font color=black>Balance</td><td align=right><font color=black><b>".putcoma(abs($bal))."</td><td><font color=black><b> Debit </td><td colspan=2></td></tr>";
}
else if(abs($totdramt)<abs($totcramt))
{
$r11=mysqli_query($db,"select $totcramt-$totdramt");
$trow=mysqli_fetch_array($r11);
$bal=abs($trow[0]);

//$bal=(abs($totcramt)-abs($totdramt)); 

print "\n<tr><td colspan=2 align=center><b><font color=black>Balance</td><td></td><td align=right><font color=black><b>".putcoma(abs($bal))."</td><td><font color=black> <b>Credit </td> </tr>";
}


//*******************************************************
// if(strtoupper($opb[drcr])=="DR" && $opb[op]<0)
// 	$bal*=-1;
  $bal=number_format($bal,2,'','');
//********************************************************
if($bal>0)
{
//print "\n<tr><td colspan=2 align=center><b><font color=black>Balance</td><td align=right><font color=black><b>".putcoma(abs($bal))."</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></td><td align=right><font color=black><b>".putcoma(abs($bal))."</td><td><font color=black> <b>Credit </td> </tr>";


//**************************************************************8888888888888888888888888
?>
</table><br>
<?
} // end main if


//========================================

?>
<input type=button value="<- Back" onClick=document.location='listledgers2.php?sea=<?print$sea?>'>

 
