<?
include_once('includes/top.php');
include_once('opendb.php');
include_once('test.php');

$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];

//$br=$_SESSION['branch'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}
$cd=$_REQUEST[cd];
if($cd=='')
{
$cd=$_REQUEST[ledg];
}
?>
<html>
<head>
<title>Edit Ledger</title>

<link href="css/printline.css" type="text/css" rel="stylesheet" media="screen"  />

<script type="text/javascript">

/** code below for drag the confirmation div */
window.onload = function(){
    draggable('conf_panel');
};

var dragObj = null;
function draggable(id)
{
    var obj = document.getElementById(id);
    obj.style.position = "absolute";
    obj.onmousedown = function(){
            dragObj = obj;
    }
}

document.onmouseup = function(e){
    dragObj = null;
};

document.onmousemove = function(e){
    var x = e.pageX;
    var y = e.pageY;

    if(dragObj == null)
        return;

    dragObj.style.left = x +"px";
    dragObj.style.top= y +"px";
};
</script>


 <style type="text/css"> 

body{	
/* IE10 Consumer Preview */ 
background-image: -ms-radial-gradient(left bottom, circle farthest-side, #FFFFFF 0%, #C7C7C7 100%);

/* Mozilla Firefox */ 
background-image: -moz-radial-gradient(left bottom, circle farthest-side, #FFFFFF 0%, #C7C7C7 100%);

/* Opera */ 
background-image: -o-radial-gradient(left bottom, circle farthest-side, #FFFFFF 0%, #C7C7C7 100%);

/* Webkit (Safari/Chrome 10) */ 
background-image: -webkit-gradient(radial, left bottom, 0, left bottom, 973, color-stop(0, #FFFFFF), color-stop(1, #C7C7C7));

/* Webkit (Chrome 11+) */ 
background-image: -webkit-radial-gradient(left bottom, circle farthest-side, #FFFFFF 0%, #C7C7C7 100%);

/* W3C Markup, IE10 Release Preview */ 
background-image: radial-gradient(circle farthest-side at left bottom, #FFFFFF 0%, #C7C7C7 100%);

} 
 td{font-family:'verdana';font-size:14;color:black; text-align:right;}

    #conf_panel{  
        margin-top:2px;
        position: absolute;  
        top: 1px;  
        left: 450px;  
        text_align:center;
        
        -moz-box-shadow: 0 0 2px 2px #888;
        -webkit-box-shadow: 0 0 2px 2px#888;
         box-shadow: 0 0 2px 2px #888;
     
        background-color: white;  
      
        color: black;  
        display: none; 
        border:1px solid #C2DFFF; 

    }  
tr:hover {    background: gold!important; }

.conf_tbl th{
  

   // background-color: grey;  
    font-weight: bold;  
    color: white; 
    border:1px solid #D7D7D1;

    border-collapse:collapse;
   

background: rgb(181,189,200); /* Old browsers */
background: -moz-linear-gradient(top, rgba(181,189,200,1) 0%, rgba(130,140,149,1) 36%, rgba(40,52,59,1) 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(181,189,200,1)), color-stop(36%,rgba(130,140,149,1)), color-stop(100%,rgba(40,52,59,1))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, rgba(181,189,200,1) 0%,rgba(130,140,149,1) 36%,rgba(40,52,59,1) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, rgba(181,189,200,1) 0%,rgba(130,140,149,1) 36%,rgba(40,52,59,1) 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top, rgba(181,189,200,1) 0%,rgba(130,140,149,1) 36%,rgba(40,52,59,1) 100%); /* IE10+ */
background: linear-gradient(to bottom, rgba(181,189,200,1) 0%,rgba(130,140,149,1) 36%,rgba(40,52,59,1) 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#b5bdc8', endColorstr='#28343b',GradientType=0 ); /* IE6-9 */

 }


#myTable tr:nth-child(odd) td{

background: -moz-linear-gradient(top, rgba(246,248,249,1) 0%, rgba(229,235,238,0.94) 50%, rgba(215,222,227,0.94) 51%, rgba(233,237,240,0.91) 81%, rgba(245,247,249,1) 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(246,248,249,1)), color-stop(50%,rgba(229,235,238,0.94)), color-stop(51%,rgba(215,222,227,0.94)), color-stop(81%,rgba(233,237,240,0.91)), color-stop(100%,rgba(245,247,249,1))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, rgba(246,248,249,1) 0%,rgba(229,235,238,0.94) 50%,rgba(215,222,227,0.94) 51%,rgba(233,237,240,0.91) 81%,rgba(245,247,249,1) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, rgba(246,248,249,1) 0%,rgba(229,235,238,0.94) 50%,rgba(215,222,227,0.94) 51%,rgba(233,237,240,0.91) 81%,rgba(245,247,249,1) 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top, rgba(246,248,249,1) 0%,rgba(229,235,238,0.94) 50%,rgba(215,222,227,0.94) 51%,rgba(233,237,240,0.91) 81%,rgba(245,247,249,1) 100%); /* IE10+ */
background: linear-gradient(to bottom, rgba(246,248,249,1) 0%,rgba(229,235,238,0.94) 50%,rgba(215,222,227,0.94) 51%,rgba(233,237,240,0.91) 81%,rgba(245,247,249,1) 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f6f8f9', endColorstr='#f5f7f9',GradientType=0 ); /* IE6-9 */


}
#myTable tr:nth-child(even) td{
background: rgb(255,255,255); /* Old browsers */
background: -moz-linear-gradient(top, rgba(255,255,255,1) 0%, rgba(241,241,241,1) 50%, rgba(225,225,225,1) 51%, rgba(246,246,246,1) 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(255,255,255,1)), color-stop(50%,rgba(241,241,241,1)), color-stop(51%,rgba(225,225,225,1)), color-stop(100%,rgba(246,246,246,1))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, rgba(255,255,255,1) 0%,rgba(241,241,241,1) 50%,rgba(225,225,225,1) 51%,rgba(246,246,246,1) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, rgba(255,255,255,1) 0%,rgba(241,241,241,1) 50%,rgba(225,225,225,1) 51%,rgba(246,246,246,1) 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top, rgba(255,255,255,1) 0%,rgba(241,241,241,1) 50%,rgba(225,225,225,1) 51%,rgba(246,246,246,1) 100%); /* IE10+ */
background: linear-gradient(to bottom, rgba(255,255,255,1) 0%,rgba(241,241,241,1) 50%,rgba(225,225,225,1) 51%,rgba(246,246,246,1) 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#f6f6f6',GradientType=0 ); /* IE6-9 */
}



.conf_tbl td{
    height:32px;
    border: 1px solid #A9A9A9;
  
  text-align:left;
     font-size:4 px;  
    color: black; 
background: -moz-linear-gradient(top, rgba(246,248,249,1) 0%, rgba(229,235,238,0.94) 50%, rgba(215,222,227,0.94) 51%, rgba(233,237,240,0.91) 81%, rgba(245,247,249,1) 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(246,248,249,1)), color-stop(50%,rgba(229,235,238,0.94)), color-stop(51%,rgba(215,222,227,0.94)), color-stop(81%,rgba(233,237,240,0.91)), color-stop(100%,rgba(245,247,249,1))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, rgba(246,248,249,1) 0%,rgba(229,235,238,0.94) 50%,rgba(215,222,227,0.94) 51%,rgba(233,237,240,0.91) 81%,rgba(245,247,249,1) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, rgba(246,248,249,1) 0%,rgba(229,235,238,0.94) 50%,rgba(215,222,227,0.94) 51%,rgba(233,237,240,0.91) 81%,rgba(245,247,249,1) 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top, rgba(246,248,249,1) 0%,rgba(229,235,238,0.94) 50%,rgba(215,222,227,0.94) 51%,rgba(233,237,240,0.91) 81%,rgba(245,247,249,1) 100%); /* IE10+ */
background: linear-gradient(to bottom, rgba(246,248,249,1) 0%,rgba(229,235,238,0.94) 50%,rgba(215,222,227,0.94) 51%,rgba(233,237,240,0.91) 81%,rgba(245,247,249,1) 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f6f8f9', endColorstr='#f5f7f9',GradientType=0 ); /* IE6-9 */



 }

.conf_tbl input[type="text"]  {
   // padding: 0px 5px 0px 5px;
    border-style:none;
    border-color: white;//transparent;
    background-color: transparent;
    font-size:12px;

}


 </style>

<html>
<head>
<script>
var tr_amnt=[];
var subjv=[];

 var typ;
 var partic;
 var lst;
 var cnt=1;
 var id2;
 var conf_id;
 var jno;

  function edit_bal(id1, juno, typ1, led){


       var j;
         jno=juno;
        id2='txtfld'+id1;
        lst='drp_list'+id1;
        partic=led;
        typ=typ1;
       conf_id='conf'+id1;

        for(j=1;j<cnt;j++)
        {

           if(j!= id1){
          
             document.getElementById('txtfld'+j).innerHTML=tr_amnt[j];
 
             document.getElementById('drp_list'+j).innerHTML="";

           }
        }
       
    var curr_amt=tr_amnt[id1];


    document.getElementById(id2).innerHTML="<input type='text' name='amt' placeholder='"+curr_amt+"' onkeyup='ajax_led_edit(this.value )' size='5'/>";
   document.getElementById(id2).getElementsByTagName("input")[0].focus();
   document.getElementById(lst).innerHTML="<input type='button' class='conf'  value='View' onclick='cofirm_fun()'>";

  }

    function ajax_led_edit(val ){


     if(!isNaN(val))
     {

     
	if (window.XMLHttpRequest)
  	{// code for IE7+, Firefox, Chrome, Opera, Safari
  	xmlhttp=new XMLHttpRequest();
 	 }
	else
 	 {// code for IE6, IE5
  	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}
	xmlhttp.onreadystatechange=function()
  	{
  	if (xmlhttp.readyState==4 && xmlhttp.status==200)
    	{
       if(val!=""){
  
   	 document.getElementById(lst).innerHTML=xmlhttp.responseText;


             if(document.getElementById('amt_excd').value=='y'){

            alert('Sorry!! The amount must be either equel or less than the Opp amount.!!');
             document.getElementById(id2).getElementsByTagName("input")[0].value='';
             document.getElementById(id2).getElementsByTagName("input")[0].focus();

              }
        }
       else{
       document.getElementById(lst).innerHTML="";
       }
   
   	 }
     }
    
 xmlhttp.open("GET","ajax_led_edit.php?val="+val+"&& typ="+typ+"&& partic="+partic+"&& jvno="+jno,true);
 xmlhttp.send();
  }

else
  {
  alert('Enter Digit');

  document.getElementById(id2).getElementsByTagName("input")[0].value='';
  document.getElementById(id2).getElementsByTagName("input")[0].focus();
 }


  
}

function ajax_led_edit1(pt,val){
 


  var amt=document.getElementById(id2).getElementsByTagName("input")[0].value;

    if (window.XMLHttpRequest)
  	{// code for IE7+, Firefox, Chrome, Opera, Safari
  	xmlhttp=new XMLHttpRequest();
 	 }
	else
 	 {// code for IE6, IE5
  	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}
	xmlhttp.onreadystatechange=function()
  	{
  	if (xmlhttp.readyState==4 && xmlhttp.status==200)
    	{
            document.getElementById('msg1').innerHTML=xmlhttp.responseText;

            if(document.getElementById('msg').value=='0'){

            alert('Sorry!! It Already Selected /Saved');

            }
      
   	      document.getElementById(id2).getElementsByTagName("input")[0].value='';
          
		 document.getElementById(id2).getElementsByTagName("input")[0].focus();
 		document.getElementById(lst).innerHTML="<input type='button' class='conf'  value='Confirm' onclick='cofirm_fun()'>";
      
   
   	 }
     }
    
 xmlhttp.open("GET","ajax_led_edit1.php?val="+val+"&& partic="+partic+"&& jvno="+jno+"&& amt="+amt,true);
 xmlhttp.send();
 }

 function cofirm_fun(){


     tt=[];
    var list = document.getElementsByClassName("alist");
    for (var i = 0; i < list.length; i++) {
        list[i].style.display = 'none';
    }
    var e = document.getElementById('conf_panel');
    if(e.style.display == 'block') {
        e.style.display = 'none';
    } else {
        e.style.display = 'block';
    }




  if (window.XMLHttpRequest)
  	{// code for IE7+, Firefox, Chrome, Opera, Safari
  	xmlhttp=new XMLHttpRequest();
 	 }
	else
 	 {// code for IE6, IE5
  	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}
	xmlhttp.onreadystatechange=function()
  	{
  	 if (xmlhttp.readyState==4 && xmlhttp.status==200)
    	 {
            document.getElementById('conf_panel').innerHTML=xmlhttp.responseText;


            if(subjv.length<1){


  		 document.getElementById("svbttn").disabled=true;
               document.getElementById("delbttn").disabled=true;
 		 }
  		else{

   		 document.getElementById("svbttn").disabled=false;
               document.getElementById("delbttn").disabled=false;
        
 		 }


        }
      }
    
 xmlhttp.open("GET","ajaxconf_led_edit.php?jvno="+jno+"&& partic="+partic,true);
 xmlhttp.send();

}

function upd_led(journo,flag){

    document.getElementById('conf_panel').innerHTML="Loading...";

  if (window.XMLHttpRequest)
  	{// code for IE7+, Firefox, Chrome, Opera, Safari
  	xmlhttp=new XMLHttpRequest();
 	 }
	else
 	 {// code for IE6, IE5
  	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}
	xmlhttp.onreadystatechange=function()
  	{
  	 if (xmlhttp.readyState==4 && xmlhttp.status==200)
    	 {
            document.getElementById('conf_panel').innerHTML=xmlhttp.responseText;

             var input = document.getElementById ("myTable");
             input.style.filter = 'alpha(opacity=' + 20 + ')';


         
        }
      }
    //var url="ajaxupd_led_edit.php?jvno="+journo+"&& partic="+partic+"&& subjv="+subjv+"&&flg="+flag;
    //alert(url);
 xmlhttp.open("GET","ajaxupd_led_edit.php?jvno="+journo+"&& partic="+partic+"&& subjv="+subjv+"&&flg="+flag,true);
 xmlhttp.send(); 

 }


function conf_filter(str,r){

 if(r.checked){

  subjv.push(str);

 }
 else{
  var index = subjv.indexOf(str);

    if (index > -1) {
     subjv.splice(index, 1);
   }  

 }
 
  if(subjv.length<1){


   document.getElementById("svbttn").disabled=true;
   document.getElementById("delbttn").disabled=true;
  }
  else{

    document.getElementById("svbttn").disabled=false;
     document.getElementById("delbttn").disabled=false;
  }

 
}

/** code below for close popup div **/
function close_div(){
   var list = document.getElementsByClassName("alist");
             for (var i = 0; i < list.length; i++) {
               list[i].style.display = 'none';
             }
    		var e = document.getElementById('conf_panel');
   		 if(e.style.display == 'block') {
       		 e.style.display = 'none';
   		 } else {
        	e.style.display = 'none';
              }

 }

</script>


</head>
  
<body>
<style>

 th{font-family:'verdana';font-size:14;color:white;background-color:#5b5858}
 body{background-color:ivory}
</style>
<body>

<center>
</br></br>
<font face='arial narrow' size=3 >Transaction Details of <b><? print $cd?> </font></center>
<form action='<?print $PHP_SELF?>'>

<input type=hidden name=cd value='<?print $cd?>'>



<table  cellspacing=1 cellpadding=1 width=100% class='conf_tbl' id='myTable' style="border_collpase:collapse;background-color:white;font-size:4px;"  >
<tr><th height=35>Date <th>Vouch/Rec. No. </th><th>Debit </th><th>Credit</th> <th>Balance</th><th>Ref</th><th>Cleared On</th><th>Edit</th><th width='80'>-</th></tr>

<?


$str = preg_replace('/\s+/', '', $cd);
$str = str_replace("-", "", $str);
$str1=substr($str, 0, 6);
 $dyn_tbl='led_'.$str1.$brcode;

 mysqli_query($db,"drop table $dyn_tbl");
 mysqli_query($db,"CREATE TABLE $dyn_tbl (id int primary key auto_increment ,jvno varchar(10), partic varchar(200),sub_jvno int(10),type varchar(10),amt double(10,2),j_type varchar(10),tr_no int,brcode varchar(100))");
 mysqli_query($db,"delete from $tbl");


$re11=mysqli_query($db,"select * from ledger where led='$cd' and brcode='$brcode'");
$opbbb=mysqli_fetch_array($re11);
$grp=$opbbb[grp];
$re111=mysqli_query($db,"select desig from bruser where username='$user' and branch='$brcode'");

$opbbb1=mysqli_fetch_array($re111);
$grp1=$opbbb1[desig];

if(($grp1!='ccn' )||($brcode != 'PSN236'))
{
$i=1;
$cd=str_replace('~','&',$cd);

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

$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);


  $jno='opb';
  $typ=$opb[drcr];

  $damt=0;
  $camt=0;

  $fldid='txtfld'.$i;
  $r2=mysqli_query($db,"select sum(amt) from ledger_edit where brcode='$brcode' and jvno='$jno' and partic='$cd' ");
   $re2=mysqli_fetch_array($r2); 
 $oppamt=$re2[0];

if(strtoupper($opb[drcr])=="DR")
{
if($opb[op]>=0)
{
print "\n<tr ><td></td> <td>Opening balance</td><td align=right>".putComa($opb[op])."</td><td id='".$fldid."'>".putComa($oppamt)."</td>";
$tr+=$opb[op];
$tp+=$oppamt;
$tr1+=$opb[op];

}
else
{
print "\n<tr ><td></td> <td>Opening balance</td><td id='".$fldid."'>".putComa($oppamt)."</td><td align=right>".putComa(abs($opb[op]))."</td>";
$tp+=abs($opb[op]);
$tr+=$oppamt;
$tr1-=$opb[op];


}

}
else if(strtoupper($opb[drcr])=="CR")
{
if($opb[op]>=0)
{
print "\n<tr ><td></td> <td>Opening balance</td><td id='".$fldid."'>".putComa($oppamt)."</td><td align=right>".putComa($opb[op])."</td>";
$tp+=$opb[op];
$tr+=$oppamt;
$tr1+=-($opb[op]);

}
else
{
print "\n<tr ><td></td> <td>Opening balance</td><td align=right>".putComa(abs($opb[op]))."</td><td id='".$fldid."'>".putComa($oppamt)."</td>";
$tr+=abs($opb[op]);
$tp+=$oppamt;
$tr1+=abs($opb[op]);


}

}
 

print "\n<script>tr_amnt[$i]='$re2[0]'; </script>";



print "<td colspan='3'></td><td>";
?>

<input type='radio' name='edt' onclick="edit_bal('<? echo $i; ?>','<? echo $jno; ?>','<? echo $typ; ?>','<? echo $cd; ?>')" /></td>


<td id='drp_list<? echo $i;?>'></td></tr>	
<?
print"<script>cnt++;</script>";
$i++;
$sql="select * from journal2 where brcode='$brcode' and partic='$cd' order by date,jvno ";

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

$s=$tr-$tp;
while($row=mysqli_fetch_array($result))
{

   

  $jno=$row[jvno];
  $typ=$row[type];

  $fldid='txtfld'.$i;

  $r2=mysqli_query($db,"select sum(amt) from ledger_edit where brcode='$brcode' and jvno='$jno' and partic='$cd' ");
   $re2=mysqli_fetch_array($r2); 


   $r3=mysqli_query($db,"select sum(amt) from ledger_edit where brcode='$brcode' and sub_jvno='$jno' and partic='$cd' ");
   $re3=mysqli_fetch_array($r3);


     
   if($row[type]=='Cr') //$row[cr]==$cd)
    {


 $camt=$row[cramt]-$re3[0];
 $damt=$re2[0];
$tr1-=$row[cramt];
 $s+=$damt-$camt;
print "\n<script>tr_amnt[$i]='$damt'; </script>";

//$s=$tr-$tp;
 print "<tr ><td>".datetohtml($row[date])."</td><td>  <a href='viewtrans.php?code=$row[jvno]' title='Ref:$row[no]'> To $row[narr]-($row[j_type])</a></td><td align=right id='".$fldid."'>".putComa($damt)."</td><td align=right>".putComa($camt)."</td><td align=right>".number_format($s,2,".",",")."</td>";
// <td>$row[against]:$row[no]</td>";
$sql="select * from breco where brcode='$brcode' and jvno='$row[jvno]'";
$resu=mysqli_query($db,$sql) or die ('Error'.mysqli_error($db));
$r=mysqli_fetch_array($resu);


 if($row[against]=='o_form')
  print "<td><a href='mis/vform.php?fno=$row[no]'>$row[against]:$row[no]</a></td>";
 else
print "<td>$row[no]</td>";
print "<td>".datetohtml($r[2])."</td>";
  

  }
 else //if($row[dr]==$cd)
  {
 $damt=$row[dramt]-$re3[0];
 $camt=$re2[0];

$tr1+=$row[dramt];

 $s+=$damt-$camt;

print "<script>tr_amnt[$i]='$camt'; </script>";

 print "<tr><td>".datetohtml($row[date])."</td><td> <a href='viewtrans.php?code=$row[jvno]' title='Ref:$row[against] $row[no]'>By $row[narr]-($row[j_type])</a></td><td align=right>".putComa($damt)."</td><td align=right id='".$fldid."'>".putComa($camt)."</td><td align=right>".number_format($s,2,".",",")."</td>";

$sql="select * from breco where brcode='$brcode' and jvno='$row[jvno]'";
$resu=mysqli_query($db,$sql) or die ('Error'.mysqli_error($db));
$r=mysqli_fetch_array($resu); 

if($row[against]=='o_form')

  print "<td><a href='mis/vform.php?fno=$row[no]'>$row[against]:$row[no]</a></td>";
 else
 print "<td>$row[against]:$row[no]</td>";
print "<td>".datetohtml($r[2])."</td>";

//<td align=center><input type=button value='Del' onClick=document.location='deldat.php?code=$row[0]'></td>

 }


?><td>

<input type='radio' name='edt' onclick="edit_bal('<? echo $i; ?>','<? echo $jno; ?>','<? echo $typ; ?>','<? echo $cd; ?>')" /></td>

<td id='drp_list<? echo $i;?>'></td></tr>
<?
$tp+=$camt;
$tr+=$damt;
$i++;

print"<script>cnt++;</script>";
}
print "<tr ><td colspan=2 align=center><font color=black><B>Total</td>";
print "<td align=right><font color=black>".putComa($tr)."</td>";
print "<td align=right><font color=black>".putComa(abs($tp))."</td><td colspan=5></td></tr>";

/*print ($tp-$tr);
$bal=($tr-$tp);
//*******************************************************
// if(strtoupper($opb[drcr])=="DR" && $opb[op]<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'");
 $balrow=mysqli_fetch_row($balres);

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

if($tr>$tp)
print "<tr><td colspan=2 align=center><b><font color=black>Balance</td><td align=right><font color=black><b>".round(abs($bal),2)."</td><td><font color=black><b> Debit </td><td colspan=5></td></tr>";
else
print "<tr><td colspan=2 align=center><b><font color=black>Balance</td><td align=right><font color=black><b>".round(abs($bal),2)."</td><td><font color=black> <b>Credit </td><td colspan=5></td></tr>";

print "<tr><th height=25 colspan=9></th></table>";


//**************************************************************8888888888888888888888888



function xyz()
{
$sql="select * from actrans where brcode='$brcode' and(dr='$cd' or cr='$cd')  order by dt desc";
$result=mysqli_query($db,$sql) or die (mysqli_error($db));
$re=mysqli_query($db,"select * from ledger where led='$cd' and brcode='$brcode'");
$opb=mysqli_fetch_array($re);

?>
<center>
<font face='arial black'>Transaction Details of <? echo $cd?> </font>
<table bgcolor=black cellspacing=1 cellpadding=1 width=100%>
<th>Date <th>Vouch/Rec. No. <th>Debit <th>Credit <th>Mode <th>Narration <th>Action
<?
$tr=0; $tp=0;
if(strtoupper($opb[drcr])=="DR")
{
print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td></td> <td>Opening balance</td><td align=right>$opb[op]</td><td colspan=4></td></tr>";
$tr+=$opb[op];
}
else if(strtoupper($opb[drcr])=="CR")
{
print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td></td> <td>Opening balance</td><td></td><td align=right>$opb[op]</td><td colspan=4></td></tr>";
$tp+=$opb[op];
}

while($row=mysqli_fetch_array($result))
{
if($row[cr]==$cd)
{
 print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td>".datetohtml($row[dt])."</td><td>($row[vouch]) To $row[dr]</td><td align=right></td><td align=right>".putComa($row[amt])."</td> <td>$row[mode]</td><td>$row[narr]</td>";
//<td align=center><input type=button value='Del' onClick=document.location='deldat.php?code=$row[0]'></td>
print "</tr>";
$tp+=$row[amt];
}
else if($row[dr]==$cd)
{
 print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td>".datetohtml($row[dt])."</td><td>($row[vouch]) By $row[cr]</td><td align=right>".putComa($row[amt])."</td><td align=right></td> <td>$row[mode]</td><td>$row[narr]</td>";
// <td align=center><input type=button value='Del' onClick=document.location='deldat.php?code=$row[0]'></td>
print "</tr>";
$tr+=$row[amt];
}
}
print "<tr><td colspan=2 align=center><font color=black><B>Total</td>";
print "<td align=right><font color=black>".putComa($tr)."</td>";
print "<td align=right><font color=black>".putComa($tp)."</td></tr>";

/*$bal=($tr-$tp);
//*******************************************************

      $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>".putcoma($bal)."</td><td><font color=black> Debit </tr><td>";
else
print "<tr><td colspan=2 align=center><b><font color=black>Balance</td><td align=right><font color=black>".putcoma($bal)."</td><td><font color=black> Credit </tr></td>";
?>


</center>
<?
} // end fnct xyz
?>
</table><br>
<input type=button value="<- Back" onClick=history.back(1)>

<?
} // end fnct xyz
elseif($grp=='Sundry Debtors')

{
$cd=str_replace('~','&',$cd);

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

$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($opb[op]>=0)
{
print "\n<tr style={background-color:silver}><td></td> <td>Opening balance</td><td align=right>".putComa($opb[op])."</td><td colspan=4></td></tr>";
$tr+=$opb[op];
$tr1+=$opb[op];
}
else
{
print "\n<tr style={background-color:silver}><td></td> <td>Opening balance</td><td></td><td align=right>".putComa(abs($opb[op]))."</td><td colspan=3></td></tr>";
$tp+=abs($opb[op]);
$tr1-=$opb[op];


}

}
else if(strtoupper($opb[drcr])=="CR")
{
if($opb[op]>=0)
{
print "\n<tr style={background-color:silver}><td></td> <td>Opening balance</td><td></td><td align=right>".putComa($opb[op])."</td><td colspan=4></td></tr>";
$tp+=$opb[op];
$tr1+=-($opb[op]);
}
else
{
print "\n<tr style={background-color:silver}><td></td> <td>Opening balance</td><td align=right>".putComa(abs($opb[op]))."</td><td colspan=3></td></tr>";
$tr+=abs($opb[op]);
$tr1+=abs($opb[op]);

}

}
$sql="select * from journal2 where brcode='$brcode' and partic='$cd' 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];
$tp+=$row[cramt];

$s=$tr-$tp;
 print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td>".datetohtml($row[date])."</td><td>  <a href='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 align=right>".number_format($s,2,".",",")."</td>";
// <td>$row[against]:$row[no]</td>";
 if($row[against]=='o_form')
  print "<td><a href='mis/vform.php?fno=$row[no]'>$row[against]:$row[no]</a></td>";
 else
print "<td>$row[no]</td>";
  

//<td align=center><input type=button value='Del' onClick=document.location='deldat.php?code=$row[0]'></td>
print "</tr>";
  }
 else //if($row[dr]==$cd)
  {
$tr1+=$row[dramt];
$tr+=$row[dramt];

$s=$tr-$tp;

 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 align=right>".number_format($s,2,".",",")."</td>";
 if($row[against]=='o_form')
  print "<td><a href='mis/vform.php?fno=$row[no]'>$row[against]:$row[no]</a></td>";
 else
 print "<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>";
 }

}
print "<tr style={background-color:silver}><td colspan=2 align=center><font color=black><B>Total</td>";
print "<td align=right><font color=black>".putComa($tr)."</td>";
print "<td align=right><font color=black>".putComa(abs($tp))."</td><td colspan=3></td></tr>";
$balres=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where brcode='$brcode' and partic='$cd'");
 $balrow=mysqli_fetch_row($balres);

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

if($tr>$tp)
print "\n<tr><td colspan=2 align=center><b><font color=black>Balance.</td><td align=right><font color=black><b>".round(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>".round(abs($bal),2)."</td><td><font color=black> <b>Credit </td><td colspan=2></td></tr>";




//**************************************************************8888888888888888888888888



function xyz()
{
$sql="select * from actrans where brcode='$brcode' and(dr='$cd' or cr='$cd')  order by dt desc";
$result=mysqli_query($db,$sql) or die (mysqli_error($db));
$re=mysqli_query($db,"select * from ledger where led='$cd' and brcode='$brcode'");
$opb=mysqli_fetch_array($re);

?>
<center>
<font face='arial black'>Transaction Details of <? print $cd?> </font>
<table bgcolor=black cellspacing=1 cellpadding=1 width=100%>
<th>Date <th>Vouch/Rec. No. <th>Debit <th>Credit <th>Mode <th>Narration <th>Action
<?
$tr=0; $tp=0;
if(strtoupper($opb[drcr])=="DR")
{
print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td></td> <td>Opening balance</td><td align=right>$opb[op]</td><td colspan=4></td></tr>";
$tr+=$opb[op];
}
else if(strtoupper($opb[drcr])=="CR")
{
print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td></td> <td>Opening balance</td><td></td><td align=right>$opb[op]</td><td colspan=4></td></tr>";
$tp+=$opb[op];
}

while($row=mysqli_fetch_array($result))
{
if($row[cr]==$cd)
{
 print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td>".datetohtml($row[dt])."</td><td>($row[vouch]) To $row[dr]</td><td align=right></td><td align=right>".putComa($row[amt])."</td> <td>$row[mode]</td><td>$row[narr]</td>";
//<td align=center><input type=button value='Del' onClick=document.location='deldat.php?code=$row[0]'></td>
print "</tr>";
$tp+=$row[amt];
}
else if($row[dr]==$cd)
{
 print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td>".datetohtml($row[dt])."</td><td>($row[vouch]) By $row[cr]</td><td align=right>".putComa($row[amt])."</td><td align=right></td> <td>$row[mode]</td><td>$row[narr]</td>";
// <td align=center><input type=button value='Del' onClick=document.location='deldat.php?code=$row[0]'></td>
print "</tr>";
$tr+=$row[amt];
}
}
print "<tr><td colspan=2 align=center><font color=black><B>Total</td>";
print "<td align=right><font color=black>".putComa($tr)."</td>";
print "<td align=right><font color=black>".putComa($tp)."</td></tr>";

/*$bal=($tr-$tp);
//*******************************************************

      $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>".round($bal,2)."</td><td><font color=black> Debit </tr><td>";
else
print "<tr><td colspan=2 align=center><b><font color=black>Balance</td><td align=right><font color=black>".round($bal,2)."</td><td><font color=black> Credit </tr></td>";
?>


</center>
<?
} // end fnct xyz
?>
</table><br>
<input type=button value="<- Back" onClick=history.back(1)>

<?

}
?>

 </form>
<div id='msg1'>
</div>


<div id="conf_panel" class="alist" style="display:none;">
 
</div>
<div id="msgbox" >
 
</div>