   
      <?php
		error_reporting(E_ALL);
	
	
			
include "dbconfig.php";
			
		
			if (isset($_POST['pay_type']))
			{
			$pay_type=$_POST['pay_type'];
			
			
			if($pay_type=="dr")
			
			{
			
			?>
            
			
        
                  <?php 
                          $sql="SELECT * from employees";
                          $res=mysqli_query($con,$sql);
                  while($client=mysqli_fetch_assoc($res))
                      {
						  // now we will also fetch the current balance of these accounts
						  
						  
						  
						  
                  ?>			
                  <option value=<?php echo $client['id'];?>><?php echo $client['first_name'];?>
                  </option>
                 
                  <?php 
				  
					  }
				  }?>
              
              
		<?php		
	if($pay_type=="cr")
		
		{
			?>
		
        	
        
                  <?php 
                          $sql="SELECT * from customers";
						  $res=mysqli_query($con,$sql);
                  while($client=mysqli_fetch_assoc($res))
                      {
						  
						  
                  ?>			
                  <option value=<?php echo $client['id'];?>><?php echo $client['name'];?>
                  </option>
                 
                  <?php 
				  
					  }
		}}?>
              
			
		
		<?php 
			if (isset($_POST['source']))
			{
        $source=$_POST['source'];
		?>
		 <select name="project" id="project" class="form-control required">
        
        <?php 
		  $sql="SELECT * from transactions where source_id=$source GROUP by project_id order by id DESC LIMIT 1";
		  $res=mysqli_query($con,$sql);
           while($project=mysqli_fetch_assoc($res))
                      {
						  
						  
                  ?>			
                  <option value=<?php echo $project['project_id'];?>><?php echo $project['current_bal'];?>
                  </option>
                 
                  <?php 
				  
					  }
		}?>
        <?php 
			if (isset($_POST['source_id']))
			{
        $source_id=$_POST['source_id'];
		?>
		 <select name="project" id="project" class="form-control required">
        
        <?php 
		
		  $sql="SELECT p.current_bal, c.name AS source_name, t.name AS project_name
		  FROM `transactions` AS p
		  INNER JOIN clients c ON c.id = p.source_id
		  INNER JOIN project t ON t.id = p.project_id
		  WHERE p.id
		  IN (
		  
		  SELECT max( id ) 
		  FROM `transactions` 
		  WHERE source_id =$source_id 'cr'
		  GROUP BY project_id
		  )
		  ";
		  echo $sql;
		  
		  $res=mysqli_query($con,$sql);
		  $rowcount=mysqli_num_rows($result);
		  
		  if($rowcount==0)
			{
				//this client has not made any payment yet
		  $sql="SELECT p.current_bal, c.name AS source_name, t.name AS project_name
		  FROM `transactions` AS p
		  INNER JOIN clients c ON c.id = p.source_id
		  INNER JOIN project t ON t.id = p.project_id
		  WHERE p.id
		  IN (
		  
		  SELECT max( id ) 
		  FROM `transactions` 
		  WHERE source_id =$source_id
		  AND payment_type = 'nw'
		  GROUP BY project_id
		  )
		  "
		  ;
				
				
           while($project=mysqli_fetch_assoc($res))
                      {
						  
						  
                  ?>			
                  <option value=<?php echo $project['project_id'];?>><?php echo $project['current_bal'];?>
                  </option>
       <?php 
		}
		}
		
		
		
		
		}?>
              
              