<?
session_start();
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];

$str = preg_replace('/\s+/', '', $partic);
$str = str_replace("-", "", $str);
$str = str_replace(".", "", $str);
$str1=substr($str, 0, 6);
$dyn_tbl='led_'.$str1.$brcode;
$typ=$_REQUEST['typ'];
$val=$_REQUEST['val'];
$partic=$_REQUEST['partic'];
$jvno=$_REQUEST['jvno'];
$amt=0;
$flg=$_SESSION['flg'];

$tbl="tmpjournal2".$user;



 	if($typ=='Dr')
 	{
		print $sql="select jvno,j_type,jvno,cramt from $tbl where brcode='$brcode' and partic='$partic' and type='Cr' and cramt >='$val' order by date,jvno ";
 
                $r1=mysqli_query($db,"select dramt from $tbl where brcode='$brcode' and jvno='$jvno' and partic='$partic' ");
                //print $r=("select dramt from journal2 where brcode='$brcode' and jvno='$jvno' and partic='$partic' ");



                

                  


	 }
	if($typ=='Cr'){
  		 $sql="select jvno,j_type,jvno,dramt from $tbl where brcode='$brcode' and partic='$partic' and type='Dr' and dramt>='$val' order by date,jvno ";

                $r1=mysqli_query($db,"select cramt from $tbl where brcode='$brcode' and jvno='$jvno' and partic='$partic'");


	 }

        if($jvno=='opb'){

                    $r1=mysqli_query($db,"select op from ledger where brcode='$brcode' and led='$partic' ");

                   print $r=("select opp from ledger where brcode='$brcode' and led='$partic' ");

            

                 }


            $re1=mysqli_fetch_array($r1);

            $tr_amt=abs($re1[0]);
       

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


         $tr_amt=$re1[0];

         $opp_amt=$re2[0]+$re3[0]+$val;


        if($tr_amt<$opp_amt)
        {

         print "<input type='hidden' id='amt_excd' value='y' >";
        }
      
        else{

	   

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

  	       <select name='no' style"width:60px;" id='trn_lst' onchange="ajax_led_edit1('<? echo $partic;?>',this.value);"><option ><small>-select-</small></option>
<?
	    $i=1; 

 	while($row=mysqli_fetch_array($result))
	{
         
            $amt=0;
            $net=0;

        $valid1=mysqli_query($db,"select * from ledger_edit where brcode='$brcode' and sub_jvno='$row[0]' and  partic='$partic'");
         while($valid2=mysqli_fetch_array($valid1))
	   {
           $amt+=$valid2[amt];
          }
         
         $valid=mysqli_query($db,"select * from $dyn_tbl where brcode='$brcode' and sub_jvno='$row[0]' ");
         while($valid1=mysqli_fetch_array($valid))
	   {
           $amt+=$valid1[amt];
          }
          
             $net=$val+$amt;
              if($row[3]>=$net){           
 			print "<option value='".$row[0]."'>".$row[3].":(".$row[2].")</option>";
                   
			
              }   
           $i++;
	}

 ?>
	</select>

<?
    }//close of else to check amount exceed the opposite amt.

?>
