<?php include('common/header.php'); ?>
  <!-- Main Sidebar Container -->
<?php include('common/sidenav.php'); ?>
<style>
    .box{
       
        display: none;
       
    }
    .Cheque{ background: #fff; color:#000; }
  
</style>

<?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
$id = $_GET['id'];
$vendorid =$_POST['vendor_id'];
$date = $_POST['date'];
$bill_no =$_POST['bill_no'];
$chtype =$_POST['type'];
$chequeno = $_POST['cheque_no'];
$bill_amount =$_POST['bill_amount'];
$generate_amount =$_POST['generate_amount'];


$bank = $_POST['bank_name'];
//$new_balance =$_POST['new_balance'];





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

 /*$sqls = "SELECT * FROM `payment_generate` WHERE bill_no='$bill_no'";
    $results = $con->query($sqls);
       if ($results->num_rows > 0) 

  {
      
 //  echo "<script type='text/javascript'>alert('This Payment Couldn't Be Save Bill No Already Exist.');</script>";
     // echo "<script>(window.location.href='addpayment.php?id=$id');</script>";
  }*/
 ?>
<!--  <script>
        alert('This Payment Couldnt Be Save Bill No Already Exist..');
        window.location.href='addpayment.php?id=<?php echo $id; ?>';
    </script>-->
//   <?php   // if($generate_amount > $bill_amount){
 
//           echo "<div class='container'><div class='custom-alert'>Generate Amount Should Be Less Than or Equal to Bill Amount.</div></div>";
//         echo "<script>setTimeout(function() { window.location.href='addpayment.php?id=$id; '; }, 2000);</script>";

    ?>
     <!--<script>
        alert('Generate Amount Should Be Less Than or Equal to Bill Amount.');
        window.location.href='addpayment.php?id=<?php echo $id; ?>';
    </script>->
    <?php // } else {
    $query="INSERT INTO payment_generate(`vendor_id`,`p_date`,`payment_type`,`bill_no`,`generate_amount`,`cheque_no`,`bank_name`)Values('$vendorid','$date','$chtype','$bill_no','$generate_amount','$chequeno','$bank')";
    
 
        $res = mysqli_query($con, $query);
    
  //  }
    
    
    
?>
 <!-- <script>
        alert('Record has Been Added Successfully.');
        window.location.href='payment.php';
    </script>-->
<?php
     echo "<div class='container'><div class='custom-alert'>Record Successfully Added!</div></div>";
        echo "<script>setTimeout(function() { window.location.href='payment.php'; }, 1000);</script>";


$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>Add Payment</h1>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item">  <a  href="payment.php" ><i class="fa fa-link">Payment List</i></a></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">
   
                <h3 class="card-title">Add Info</h3>
              </div>
        
              <!-- /.card-header -->
              <div class="card-body">

                           <!-- add driver -->
    
      	<form  method="post">
      	  	
         
          	          
                      <div class="row">
                      <div class="col-md-4">
                        
                        
                        <div class="form-group">
                            <label>Payment Date</label>
                                <input type="date" required="required" class="form-control" name="date">
                        </div>
                        
                    </div>
                     
          	      
          	      
                      <div class="col-md-4">
                         <div class="form-group">
                        <label>Payment Type</label>
                        <select class="form-control" name="type" required>
                            <option value="">Select Type</option>
                            <option value="Cash">Cash</option>
                            <option value="Cheque">Cheque</option>
                            <option value="Transefer-Via-Account">Transefer-Via-Account</option>
                        </select>
                    </div>
                    
                    </div>
          	    </div>
          	    
          	    <div class="row">
          	        <div class="col-md-4"></div>
          	        <div class="col-md-4"><div class="form-group">
          	         <div class="Cheque box"><label>Cheque No#</label><input type="text" name="cheque_no" class="form-control" placeholder="Enter Cheque No#"></div>
          	         
          	         
          	         
          	         <!--bank-->
          	            <div class="Transefer-Via-Account box"><label>Bank Name</label><input type="text" name="bank_name" class="form-control" placeholder="Enter Name"></div>
          	         <!--bank name end-->
          	         
          	         </div></div>
          	        <div class="col-md-4">
          	         
          	         </div>
          	    </div>
          	    
          	    
          	     <!-- account info -->
          	     <div class="row">
                        <div class="col-md-12">
                        <h3 align="center">Account Info</h3>
                         </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                               <table  class="table table-striped">
                  <thead class="bg bg-danger">
                  <tr>
                   
                    <th style="width:40%;" class="text-center">Vendor</th>
                    <th style="width:20%;" class="text-center">Bill Amount</th>
                    <th style="width:20%;" class="text-center">Received Amount</th>
                    <th style="width:20%;" class="text-center">Balance Amount</th>
                    
                  </tr>
                  </thead>
                  <tbody>
                    
  
  <?php
    include('includes/config.php');
               $id= $_GET['id'];
      $no=0;
      $result = mysqli_query($con, "SELECT * FROM `vendors` where v_id='$id'");
       while($row = mysqli_fetch_assoc($result)) {
       $no++; 
       $companyname = $row['company_name'];
       
          
  ?>
      
      
                  <tr>
                     
                    <td>  <select readonly style="width:100%;" class="form-control" id="vendor" name="vendor">
                                   <option><?php echo $row['company_name']; ?></option> 
                            </select>   <input type="text" hidden class="form-control" name="vendor_id" value="<?php echo $companyname; ?>"></td>
                    <td><?php
                     // check total bill amount
    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'];
  ?>
  
  <!-- additional balance for vendor -->
  
  <?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;
       }
  ?>
  
  
                        <input type="number" class="form-control" readonly name="bill_amount" value="<?php echo $totalamount; ?>">
                        <?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;
  ?>
                        <input type="number" name="amount" readonly class="form-control" required placeholder="amount" value="<?php  if(!$totalpaid) { echo 0;}else{ echo $totalpaid; } ?>"><?php } ?></td>
                    <td><input type="number" name="balance" readonly class="form-control" required placeholder="amount" value="<?php echo $balanceamount; ?>"></td>
                   
                    
                
                    
                  </tr>

                    
 <?php } ?>
                  </tbody>
                </table>
                     
                        </div>
                    </div>
                    <!-- end account info -->

                    
                    
                    
                    
                    	     <!-- shipment info -->
          	     <div class="row">
                        <div class="col-md-12">
                        <h3 align="center">Shipment Details</h3>
                         </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                              <table id="example1" class="table table-striped">
                  <thead class="bg bg-danger">
                  <tr>
                    <th>S No</th>
                    <th>Slip No</th>
                    <th>SM No#</th>
                    <th>Vendor</th>
                    <th>Date</th>
                    <th>Material</th>
                    <th>Qty</th>
                    <th>Measurement</th>
                    <th>Rate</th>
                    <th>Amount</th>
                  
                    <th>Status</th>
                     
                  </tr>
                  </thead>
                  <tbody>

  <?php
  $totalshipment =0;
    include('includes/config.php');
            //   $fromdate = $_POST['from_date'];
            //   $todate = $_POST['to_date'];
      $no=0;
      $result = mysqli_query($con, "SELECT * FROM `shipment` as s  Where s.vendor_id='$companyname' And s.s_status='1' ORDER BY s.sh_id DESC");
       while($row = mysqli_fetch_assoc($result)) {
       $no++; 
      
      $totalshipment +=  $row['ship_total'];
      $company = $row['company_name'] ?? '';
  ?>
                  <tr>
                      <?php $id = $row['sh_id']; ?>
                    <td><?php echo $no; ?></td>
                    <td><?php echo $row['slip_no']; ?></td>
                    <td><?php echo $row['order_num']; ?></td>
                    <td><?php echo $row['vendor_id']; ?></td>
                      <td><?php $prdate = $row['s_date']; $prdates =date("D-M-j-Y",strtotime($prdate)); echo $prdates; ?>
                   
                    <td><?php echo $row['material_id']; ?></td>
                    <td><?php echo $row['qty']; ?></td>
                     <td><?php echo $row['measurement_total']; ?></td>
                    <td><?php echo $row['s_rate']; ?></td>
                   <td><?php echo $row['ship_total']; ?></td>
                     
                            
                     
                     
                    
                    <td>
                        <?php $status = $row['s_status']; ?>
                        <?php if($status == 0){?>
                        <span class="text-info">Pending</span>
                        <?php } ?>
                        <?php if($status == 1){?>
                        <span class="text-success">Delivered</span>
                        <?php } ?>
                      
                    </td>
                   
                   
                   
                  </tr>
                    
                    


    <?php
    }

  
  ?>
  <tfoot class="bg-danger">
      <tr>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td><b>Total </b></td>
          <td><b><?php echo $totalshipment; ?></b></td>
          <td></td>
      </tr>
  </tfoot>
 
                  </tbody>
                </table>
                        </div>
                    </div>
                    <!-- end shipment info -->
                    
                    
                    
                    
          	    
          	    
          	    
                <!-- additional balance -->    
                    <div class="row">
                        <div class="col-md-12">
                        <h3 align="center">Additional Balance History</h3>
                         </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                               <table id="example2" class="table table-striped">
                  <thead class="bg bg-danger">
                  <tr>
                   
                    <th style="width:40%;" class="text-center">Vendor</th>
                    <th style="width:20%;" class="text-center">Previous Balance Amount</th>
                    <th style="width:20%;" class="text-center">Date</th>
                    
                    
                  </tr>
                  </thead>
                  <tbody>
                    
  
  <?php
    include('includes/config.php');
    //get last balance
               $id= $_GET['id'];
      $no=0;
      $result = mysqli_query($con, "SELECT * FROM `vendorAdditional_balance` as vb where vb.vendor_id='$companyname'");
       while($row = mysqli_fetch_assoc($result)) {
       $no++; 
       $lastbalance = $row['balance_amount'];
       
          
  ?>
  
  <!-- additional balance  -->
      
      
                  <tr>
                     
                    <td class="text-center"> <?php echo $companyname; ?>  </td>
                    <td class="text-center">
                        <?php echo $row['add_balance']; ?>
                    </td>
                    <td class="text-center" ><?php $prdate = $row['balance_date']; 
                      echo date("d/m/Y", strtotime($prdate));  ?></td>
                    
                   
                    
                
                    
                  </tr>

                    <?php } ?>

                  </tbody>
                </table>
                     
                        </div>
                    </div>
                
                
                <!-- end Additional Balance -->
                
                
                             
                <!-- payment history -->   
                    <div class="row">
                        <div class="col-md-12">
                        <h3 align="center">Payment History</h3>
                         </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                               <table id="example3" class="table table-striped">
                  <thead class="bg bg-danger">
                  <tr>
                    <th class="text-center">SNo</th>
                 
                    <th  class="text-center">Vendor</th>
                     <th  class="text-center">Date</th>
                    <th  class="text-center">CH#</th>
                    
                      <th  class="text-center">Amount</th>
                    
                  
                   
                    
                    
                  </tr>
                  </thead>
                  <tbody>
                    
  
  <?php
  $totalgeneratePayment = 0;
    include('includes/config.php');
    //get last balance
               $id= $_GET['id'];
      $no=0;
      $result = mysqli_query($con, "SELECT * FROM `payment_generate` as pg where pg.vendor_id='$companyname' ORDER BY pg.p_date desc");
       while($row = mysqli_fetch_assoc($result)) {
       $no++; 

          
  ?>
  
  <!-- additional balance  -->
      
      
                  <tr>
                     <td class="text-center"> <?php echo $no; ?>  </td>
                  
                    <td class="text-center"> <?php echo $companyname; ?>  </td>
                     <td class="text-center" ><?php $prdate = $row['p_date']; 
                      echo date("d/m/Y", strtotime($prdate));  ?></td>
                    <td class="text-center"><?php echo $paymenttype =  $row['payment_type']; if($paymenttype == 'Cheque') { ?>
                    : <?php  echo $row['cheque_no']; ?>
                    <?php } ?>
                    </td>
                    
                    <td class="text-center">
                        <?php echo $row['generate_amount']; ?>
                    </td>

                    
                    <?php $totalgeneratePayment +=$row['generate_amount']; ?>
                    
                
                    
                  </tr>

                    <?php } ?>

                  </tbody>
                    <tfoot class="bg-danger">
                     <tr>
                     <td></td>
                  
                     <td></td>
                      <td></td
                    
                         <td></td>
                    <td class="text-center"><b>Total</b></td>
                    
                    <td class="text-center"><b><?php echo $totalgeneratePayment; ?></b></td>
                 

                   </tr>
                  
                </table>
                     
                        </div>
                    </div>
                
                
                <!-- end payment history -->
                
                
                
                 <!-- payment generate -->
                 <div class="row">
                        <div class="col-md-12">
                        <h3 align="center">Payment Generate</h3>
                         </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                               <table  class="table table-striped">
                  <thead class="bg bg-danger">
                  <tr>
                   
                    <th style="width:40%;" class="text-center">Vendor</th>
                    <th style="width:20%;" class="text-center">Previous Balance Amount</th>
                    <th style="width:20%;" class="text-center">Generate Amount</th>
                    <th style="width:20%;" class="text-center">New Balance Amount</th>
                    
                  </tr>
                  </thead>
                  <tbody>
                    
  
  <?php
    include('includes/config.php');
    //get last balance
               $id= $_GET['id'];
      $no=0;
      $result = mysqli_query($con, "SELECT * FROM `payment_generate` where vendor_id='$companyname' Order By pay_id DESC LIMIT 1");
       while($row = mysqli_fetch_assoc($result)) {
       $no++; 
       $lastbalance = $row['balance_amount'];
       }
          
  ?>
  
  <!--  payment generate  -->
      
      
                  <tr>
                     
                    <td>  <select readonly style="width:100%;" class="form-control" id="vendor" name="vendor">
                                   <option><?php echo $companyname; ?></option> 
                            </select>   </td>
                    <td><input type="number" id="total_bill" class="form-control" readonly name="bill_amount" 
                    value="<?php echo $balanceamount; ?>">
                    </td>
                    <td><input type="number" id="generate_amount" onkeyup="minus();" name="generate_amount" class="form-control" required placeholder="amount"></td>
                    <td><input type="number" id="new_balance" readonly name="new_balance" class="form-control" required placeholder="amount" value="<?php echo $balanceamount; ?>"></td>
                   
                    
                
                    
                  </tr>

                    

                  </tbody>
                </table>
                     
                        </div>
                    </div>
                
                
                <!-- end payment generate -->
               
                    
                <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>
                <!-- end search button -->
               
           </form>
              

    <!-- end add driver -->
                
                
                
                
                
              </div>
              <!-- /.card-body -->
            </div>
            <!-- /.card -->
          </div>
          <!-- /.col -->
        </div>
        <!-- /.row -->
      </div>
      <!-- /.container-fluid -->
    </section>
    <!-- /.content -->





<Script>
function minus()
{
    d = Number(document.getElementById('total_bill').value);
	e = Number(document.getElementById('generate_amount').value);
	f= d - e;
	 
	document.getElementById('new_balance').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>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
    $("select").change(function(){
        $(this).find("option:selected").each(function(){
            var optionValue = $(this).attr("value");
            if(optionValue){
                $(".box").not("." + optionValue).hide();
                $("." + optionValue).show();
            } else{
                $(".box").hide();
            }
        });
    }).change();
});
</script>

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


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