<?php 

require_once("config/configDB.php"); 
require "financial_year_calculation.php";
require "userPermission.php";
require "accOverFetchData.php";


if($_GET['id']) {
  $supplierid     =   $_GET['id'];
}

$sql = mysqli_query($conn,"SELECT a.id,a.scode,a.sname,a.mobile,sum(a.pamount) as pamount,sum(payamount) as payamount
FROM (
  SELECT sm.id,sm.scode,sm.sname,sm.mobile,sum(pdi.amount) as pamount, 0 as payamount 
  FROM supplier_master as sm 
  LEFT JOIN purchase_dieselinvoice as pdi ON sm.id=pdi.supplierid 
  WHERE sm.isdelete=1 and pdi.isdelete=1  and sm.id='$supplierid' and pdi.status=''
  GROUP BY sm.id
  
  UNION ALL
		
		SELECT sm.id,sm.scode,sm.sname,sm.mobile,sum(poh.total_cost) as pamount,0 as payamount 
	
		FROM supplier_master as sm 
	
		LEFT JOIN explosive_utiliz_hdr as poh ON sm.id=poh.contractor
	
		WHERE sm.isdelete=1 and poh.status='' and sm.id='$supplierid' 
	
		GROUP BY sm.id

 
 UNION ALL
		
		SELECT sm.id,sm.scode,sm.sname,sm.mobile,sum(pdief.amount) as pamount, 0 as payamount 
	
		FROM supplier_master as sm 
	
		LEFT JOIN externalfill_diesel as pdief ON sm.id=pdief.supplierid 
	
		WHERE sm.isdelete=1 and pdief.isdelete=1 and sm.id='$supplierid' and pdief.status=''
	
		GROUP BY sm.id
		
		UNION ALL
		
		SELECT sm.id,sm.scode,sm.sname,sm.mobile,sum(pdieu.amount) as pamount, 0 as payamount 
	
		FROM supplier_master as sm 
	
		LEFT JOIN externalpro_diesel as pdieu ON sm.id=pdieu.supplierid 
	
		WHERE sm.isdelete=1 and pdieu.isdelete=1 and sm.id='$supplierid' and pdieu.status=''
	
		GROUP BY sm.id
  
 

  UNION ALL
  
  SELECT sm.id,sm.scode,sm.sname,sm.mobile,0 as pamount,sum(sp.amount) as payamount 
  FROM supplier_master as sm 
  LEFT JOIN supplier_payment as sp ON sm.id=sp.supplierid
  WHERE sm.isdelete=1 and sp.isdelete=1 and sm.id='$supplierid' and sp.status=''
  GROUP BY sm.id


) a GROUP BY a.id  ");
$row=mysqli_fetch_array($sql,MYSQLI_ASSOC);


$sname          = $row['sname'];
$pamount        = $row['pamount']-$row['payamount'];



?>
<!DOCTYPE html>
<html>
<head>
    
    <?php 
      require 'meta.php'; 
      require 'header.php'; 
    ?>

</head>
<body>
    <?php require 'menu.php' ?>
<div class="container-fluid">
    <h2 class="text-center">Supplier Payment Details </h2><br>
    <form class="form-horizontal" action="accOverInsert.php" method="post" id="accOverFrom" onSubmit="return checkForm(this);" autocomplete="off">
      <div class="form-group">        
        
        <label class="control-label col-sm-4">Invoice</label>
        <div class="col-sm-4">
          <input type="text" class="form-control input-lg" value="<?php echo $invoice_count; ?>" name="invoice" autocomplete="off" readonly>
        </div>
        
      </div>
      <div class="form-group">        
        <label class="control-label col-sm-4">Date</label>
        <div class="col-sm-4">
          <input class="form-control input-lg datepicker-invoicedate"  type="text" name="invoicedate" autocomplete="off" readonly>            
        </div>
      </div>    

      
      <div class="form-group">        
        <label class="control-label col-sm-4">Supplier Name</label>
        <div class="col-sm-4">
        <input class="form-control input-lg purchase-supplier" value="<?php echo $sname; ?>"  type="text" name="pcategory" autocomplete="off" readonly>   
        <input type="hidden" name="supplierid" value="<?php echo $supplierid; ?> "> 
        </div>
      </div>  

      <div class="form-group">        
        <label class="control-label col-sm-4">Pending Amount</label>
        <div class="col-sm-4">
        <input class="form-control input-lg total-purchase-amount"  type="text" value="<?php echo $pamount; ?>" name="tamount" autocomplete="off" readonly>    
        </div>
      </div>

      <div class="form-group">        
        <label class="control-label col-sm-4">Pay Amount</label>
        <div class="col-sm-4">
        <input class="form-control input-lg pay-amount"  type="text" name="payamount" autocomplete="off">    
        </div>
      </div>
      <div class="form-group">        
        <label class="control-label col-sm-4">Pay Type</label>
        <div class="col-sm-4">
          <select class="form-control input-lg selectpicker select-payment-type" data-live-search="true" name="paymenttype" id="supplierdetails">
            <option disabled selected>Select your Option</option>
            <option value="cash">Cash</option>
            <option value="cheque">Cheque</option>
            <option value="NEFT">NEFT</option>
          
          </select>     
        </div>
      </div>
      <div class="form-group paymentreference">        
        <label class="control-label col-sm-4">Payment Reference</label>
        <div class="col-sm-4">
        <input class="form-control input-lg pay-reference"  type="text" name="payreference" autocomplete="off">   
        </div>
      </div>

      
     <div class="form-group">        
      <div class="col-sm-12 text-center">
          <button type="submit" name="myButton" class="btn btn-default btn-lg">Save</button>
      </div>
    </div>
  </form>   
</div>


  <?php require 'footer.php' ?>
  <script type="text/javascript">var base_url = "<?= $base_url ?>";</script>
  
  <script type="text/javascript" src="accOverScript.js"></script>
  
  <script>

  var checkForm = function(form) {

    //
    // validate form fields
    //

    form.myButton.disabled = true;
    return true;
  };


</script>
  
  <script>
	$(document).ready(function(){

    $('.paymentreference').hide();

	  var date_input=$('input[name="invoicedate"]'); //our date input has the name "date"
		var container=$('form >  .datepicker-invoicedate').length>0 ? $('form > .datepicker-invoicedate').parent() : "body";
		date_input.datepicker({
			  format: 'dd-mm-yyyy',
        container: container,
			  todayHighlight: true,
			  autoclose: true,
    });
    $('input[name="invoicedate"]').datepicker( "setDate", new Date());  

	  $('#accOverFrom').bootstrapValidator({
        // To use feedback icons, ensure that you use Bootstrap v3.1.0 or later
        feedbackIcons: {
            valid: '',
            invalid: '',
            validating: ''
        },
        fields: {
          payamount: {
                validators: {
                    notEmpty: {
                        message: 'This required and cannot be empty'
                    },
                    regexp: {
                        regexp: /^[0-9]+$/,
                        message: 'The closing KS can only consist of number'
                    }
                }
            },
            paymenttype: {
                validators: {
                    notEmpty: {
                        message: 'This required and cannot be empty'
                    }
                }
          }
        }
    });
    

  });
  

</script>


</body>
</html>