<?php include('common/header.php'); ?>
  <!-- Main Sidebar Container -->
<?php include('common/sidenav.php'); ?>


<?php
 include_once ('includes/config.php');
   
// Check connection
    if ($con->connect_error) {
    die("Connection failed: " . $con->connect_error);
    } 

if(isset($_POST['submit']))
{
 
$sess= $_SESSION['sess_user'];


   
//insert.php
$vendor =$_POST['vendor'];
$addbalance = $_POST['addbalance'];
$date =$_POST['date'];









 // Check connection
    if ($con->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    } 
  else

 

    
    $sql="INSERT INTO vendorAdditional_balance(`vendor_id`,`add_balance`,`balance_date`)Values('$vendor','$addbalance','$date')";
    
 
 
  


if ($con->multi_query($sql) === TRUE) 
{
    // echo "Successfully Changed";

      echo "<div class='container'><div class='custom-alert'>Record Successfully Added!</div></div>";
        echo "<script>setTimeout(function() { window.location.href='receivable.php'; }, 1000);</script>";
 

?>
<!--  <script>
        alert('Record has Been Added Successfully.');
        window.location.href='receivable.php';
    </script>-->
<?php
}
else {
    echo "Error: " . $sql . "<br>" . $con->error;
  }

$con->close();
}
 ?>
 
  <?php
//vendor
include('includes/config.php');
$v = '';

$query = "SELECT * FROM `vendors` where vs_status='0' ORDER BY company_name ASC";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_array($result))
{
 $v .= '<option value="'.$row["company_name"].'">'.$row["company_name"].' </option>';
}
?>


  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
  

    <!-- Content Header (Page header) -->
    <section class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1>Receivable List</h1>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item">  <button id="formButton" type="button"  class="btn btn-danger"><i class="fa fa-plus">Add Previous Balance</i></button></li>
             
            <!--  <li class="breadcrumb-item active"><a href="#">Dashboard</a></li>-->
            </ol>
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>

    <!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <div class="row">
          <div class="col-12">


            <div class="card">
              <div class="card-header">
                    <!-- add vehicle -->
    
      	<form id="form1" class="form-hidden" method="post">
          			<h3>Add Info</h3>

          	    <div class="row">
          	        <div class="col-md-4">
          	            <div class="form-group">
          	                  <label>Vendor</label>
                                <select  required="required" class="form-control selectpicker action2"  data-live-search="true" id="vendor" name="vendor">
                  <option value="">Select Vendor</option>
                  <?php echo $v; ?>
                  </select>
          	            </div>
          	            
          	        </div>
          	        
          	        <div class="col-md-4">
          	            <div class="form-group">
          	                  <label>Amount</label>
                                <select  required="required" readonly class="form-control action2"  data-live-search="true" id="amount" name="amount">
                  <option value="">Select Amount</option>
                
                  </select>
          	            </div>
          	            
          	        </div>
          	        <div class="col-md-4">
          	            <div class="form-group">
          	                  <label>Add Balance</label>
                 <input type="number" onkeyup="addbalances();" id="addbalance" placeholder="Enter Amount" required="required" class="form-control" name="addbalance">
          	            </div>
          	            
          	        </div>
     
          	    </div>
          	    
          	    <div class="row">
          	         <div class="col-md-4">
          	            <div class="form-group">
          	                  <label>New Balance</label>
                 <input type="number" readonly placeholder="Enter Amount" required="required" class="form-control" id="newbalance" name="newbalance">
          	            </div>
          	            
          	        </div>
          	           <div class="col-md-4">
          	            <div class="form-group">
          	                  <label>Date</label>
                 <input type="date"   required="required" class="form-control" name="date">
          	            </div>
          	            
          	        </div>
          	    </div>

               
                <div class="row">
                  <div class="col-md-6">
                      <div class="form-group">
                           
                      <div class="form-example-int mg-t-15">
                            <button name="submit" class="btn btn-danger notika-btn-success">Submit</button>
                        </div>
                     
                  </div>
                </div>
                </div>
           </form>
              

    <!-- end add vehicle -->
       
                <h3 class="card-title">Lists</h3>
              </div>
        
              <!-- /.card-header -->
              <div class="card-body">
               <table id="example1" class="table table-striped">
                  <thead class="bg bg-danger">
                  <tr>
                    <th>S No</th>
                    <th>Company</th>
                    <th>Amount</th>
                    <th>Received</th>
                    <th>Balance</th>
                     <th>Status</th>

                    <th>Action</th>
                  </tr>
                  </thead>
                  <tbody>

  <?php
  $allbalanceamount = 0;
$allpaidtotal = 0;
$alltotalamount = 0;
    include('includes/config.php');
               
      $no=0;
      $result = mysqli_query($con, "SELECT * FROM `vendors` where vs_status='0' Group by company_name ORDER BY v_id ASC");
       while($row = mysqli_fetch_assoc($result)) {
       $no++; 
       
          
  ?>
                  <tr>
                      <?php $id = $row['v_id']; ?>
                    <td><?php echo $no; ?></td>
                  
                    <td><?php echo $companyname = $row['company_name']; ?></td>
                   
                        <td><?php
    include('includes/config.php');
               
     
      $resultres = mysqli_query($con, "SELECT Sum(ship_total) as totalamount FROM `shipment` as s WHERE s.vendor_id='".$companyname."' And s.s_status='1'
");
       while($rowrev = mysqli_fetch_assoc($resultres)) {
        $shiptotalamount = $rowrev['totalamount'];
  ?>
  <?php
  //additional balance by vendor
    include('includes/config.php');
               
     
      $resultres = mysqli_query($con, "SELECT sum(add_balance) as addtionalbalance FROM `vendorAdditional_balance` WHERE vendor_id='$companyname'");
       while($rowrev = mysqli_fetch_assoc($resultres)) {
        $newtotalamount = $rowrev['addtionalbalance'];
        
        
        $totalamount = $shiptotalamount += $newtotalamount;
  ?>
  <?php if($totalamount == '') { ?>
    
   <!-- end condition -->
  <?php } else  ?>
 
  <?php echo $totalamount; ?>
  
  
  <!-- end total loop -->
  <?php }  ?>
  
  <!-- end loop additional balance -->
  
  <?php } ?>
  </td>
             
                      <td>  <?php
                        // check generate amount
    include('includes/config.php');
               
     
      $resultres = mysqli_query($con, "   SELECT SUM(generate_amount) as totalpaid FROM `payment_generate` as s  WHERE s.vendor_id='$companyname'
");
       while($rowrev = mysqli_fetch_assoc($resultres)) {
        $totalpaid = $rowrev['totalpaid'];
        $balanceamount = $totalamount - $totalpaid;
        
        $allbalanceamount += $balanceamount;
        $allpaidtotal += $totalpaid;
        $alltotalamount += $totalamount;
  ?><?php  if($totalpaid ==0){echo 0; } else if($totalamount == 0) { echo '0';} else { echo $totalpaid; } ?></td>
                    <td><?php   echo $balanceamount; //loop end 
                    }?></td>
                    <td>
                           
                          
                        <?php if(!$totalamount) { ?>  
                          
                         <span class="text-danger"><b>Payable</b></span>
                        <?php } else if($totalpaid == $totalamount ) { ?>
                        <span class="text-success"><b>Paid</b></span>
                      <?php  } else if($totalpaid >= $totalamount) {  ?>
                      
                       <span class="text-info"><b>Advance</b></span>
                         <?php } else if($totalamount == 0) { ?>
                        <span class="text-info"><b>Advance</b></span>
                        
                       <?php } else { ?>
                        <span class="text-danger"><b>Payable</b></span>
                        <?php } ?>
                        
                    </td>

                   
                  <td>
                        
                         <?php //check status
                         if($totalamount ==0) { ?>
                           <a href="addpayment.php?id=<?php echo $id; ?>"><i class="fas fa-plus"><b>Add Payment</b></i></a>||
                         <span class="text-danger"><b>Shipment Not Delivered.</b></i></span>
                         
                             <?php } elseif($totalpaid == $totalamount) { ?>
                             
                              <a href="addpayment.php?id=<?php echo $id; ?>"><i class="fas fa-plus"><b>Add Payment</b></i></a>
                             
                        <a   href="vendorbalance.php?id=<?php echo $id; ?>"  ><i   class="fa fa-eye"></i>Additional Balance History</a>||
                        <?php } else { ?>
                        
                        <a href="addpayment.php?id=<?php echo $id; ?>"><i class="fas fa-plus"><b>Add Payment</b></i></a>
                         <!-- <b>Currently Working On this event</b>-->
                         
                          ||
                        
                        <a   href="vendorbalance.php?id=<?php echo urlencode($companyname);  ?>"  ><i   class="fa fa-eye"></i>Additional Balance History</a>
                        <?php } ?>
                        
                        
                       
                        
                  <!--<i class="fas fa-edit"></i>
                          ||
                        <a  onclick="return confirm('Are you sure <?php echo $row['company_name']; ?> ?')" href="vendordelete.php?id=<?php echo $id; ?>"  ><i style="font-size:15px; color:red;"  class="fa fa-trash"></i></a>-->
                        
                    </td>

                    
                  </tr>

                    


    <?php
    }

  
  ?>
 
                  </tbody>
                  <tfoot class="bg-danger">
                      <td></td>
                      
                      <td><b>All Total</b></td>
                      <td><b><?php echo $alltotalamount ; ?></b></td>
                      <td><b><?php echo $allpaidtotal ; ?></b></td>
                      <td><b><?php echo $allbalanceamount; ?></b></td>
                      <td></td>
                      <td></td>
                  </tfoot>
                </table>
                     
                
                
                
                
                
              </div>
              <!-- /.card-body -->
            </div>
            <!-- /.card -->
          </div>
          <!-- /.col -->
        </div>
        <!-- /.row -->
      </div>
      <!-- /.container-fluid -->
    </section>
    <!-- /.content -->

<script>
    //customer
$(document).ready(function(){
 $('.action2').change(function(){
  if($(this).val() != '')
  {
   var action2 = $(this).attr("id");
   var query = $(this).val();
   var result = '';
   if(action2 == "vendor")
   {
    result = 'amount';
   }
   $.ajax({
    url:"fetchbalance.php",
    method:"POST",
    data:{action2:action2, query:query},
    success:function(data){
     $('#'+result).html(data);
    }
   })
  }
 });
});

//customer

</script>

<Script>
function addbalances()
{
    d = Number(document.getElementById('amount').value);
	e = Number(document.getElementById('addbalance').value);
	f= d + e;
	 
	document.getElementById('newbalance').value = f;

   	
  /* 	a = Number(document.getElementById('payable').value);
	b = Number(document.getElementById('g_amount').value);
	c= a + b;

   	document.getElementById('add_payable').value = c;*/
   	
}
</script>





  </div>
  <!-- /.content-wrapper -->


<?php include('common/footer.php'); ?>