<?php 

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

$accountyear    = $financialYearStart."-".$financialYearEnd;


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.tamount) as pamount, 0 as payamount 
  FROM supplier_master as sm 
  LEFT JOIN purchaseheader as pdi ON sm.id=pdi.supplier_id 
  WHERE sm.isdelete=1 and pdi.flag=1  and sm.id='$supplierid' and pdi.status=''
  GROUP BY sm.id
  
  
 

  UNION ALL
  
  SELECT sm.id,sm.scode,sm.sname,sm.mobile,0 as pamount,sum(sp.received_amount) as payamount 
  FROM supplier_master as sm 
  LEFT JOIN supplier_payment_header as sp ON sm.id=sp.supplierID
  WHERE sm.isdelete=1 and sp.flag=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'];

$sql_InvoiceQuery   = mysqli_query($conn,"SELECT count(*) as invoice FROM supplier_payment_header WHERE accountyear='$accountyear' and status='' ");
$rowInvoiceCount    = mysqli_fetch_array($sql_InvoiceQuery,MYSQLI_ASSOC);

$invoice_count      = $rowInvoiceCount['invoice']+1;

$acyear="PCL/".$syr."-".$eyr."/";

$invoice= $acyear.$invoice_count;

//$invoice_count      = $rowInvoiceCount['invoice']+1;

?>
<!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="paypurchaseInsert.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; ?>" 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 required>            
        </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="number" name="payamount" autocomplete="off" required>    
        </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>Bank</option>
       <option>Gpay</option>
       <option>Phonepe</option>
       <option>Cash</option>
       <option>Cheque</option>
       <option>Other Upi</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 ">        
        <label class="control-label col-sm-4">Remark</label>
        <div class="col-sm-4">
        <input class="form-control input-lg "  type="text" name="remark" 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>