<?php 

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

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


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

$sql = mysqli_query($conn,"SELECT a.id,a.ccode,a.cname,a.mobile,sum(a.samount) as samount,sum(payamount) as payamount,sum(discount) as discount 
FROM (
  SELECT cm.id,cm.ccode,cm.cname,cm.mobile,SUM(sd.tamount) as samount,0 as payamount,0 as discount from customer_master as cm LEFT JOIN saleheader sd ON cm.id=sd.customer_id WHERE cm.isdelete=1 and sd.status='' and cm.id='$customerid' GROUP BY cm.id
  
  UNION ALL
  
  SELECT cm.id,cm.ccode,cm.cname,cm.mobile,0 as samount,sum(sc.received_amount) as payamount,sum(discount) as discount from customer_master as cm LEFT JOIN customer_payment_header as sc ON cm.id=sc.customerID WHERE cm.isdelete=1 and sc.status='' and cm.id='$customerid' GROUP by cm.id
  
 

) a GROUP BY a.id  ");





$row=mysqli_fetch_array($sql,MYSQLI_ASSOC);

$sql_return=mysqli_query($conn,"select Sum(tamount) as return_amount from sale_return_header where status='' and customer_id='$customerid' ");
$row_return=mysqli_fetch_array($sql_return,MYSQLI_ASSOC);

$return_amount=$row_return['return_amount'];

$sql_opening=mysqli_query($conn,"select cname,opening_balance from customer_master where id='$customerid' ");
$row_opening=mysqli_fetch_array($sql_opening,MYSQLI_ASSOC);

$opening_balance=$row_opening['opening_balance'];



$cname=$row_opening['cname'];
//$pamount        = $row['samount']-($row['payamount']+$row['discount']);

$pamount1        = ($row['samount']+$opening_balance)-($row['payamount']+$row['discount']+$return_amount);

$pamount=number_format($pamount1,2,'.','');

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

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

$acyear="SCL/".$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">Customer Payment Details </h2><br>
    <form class="form-horizontal" action="paysales_collectionInsert.php" method="post" id="accOverFrom" onSubmit="return checkForm(this);" autocomplete="off">
    
    
    <div class=" col-sm-12">
    
    <div class=" col-sm-2">
        <label for="inputlg">Invoice</label>
         <input type="text" class="form-control input-lg" value="<?php echo $invoice; ?>" name="invoice" autocomplete="off" readonly required>
    </div>
    
     <div class=" col-sm-2">
        <label for="inputlg">Date</label>
         <input class="form-control input-lg datepicker-invoicedate"  type="text" name="invoicedate" autocomplete="off" readonly required> 
    </div>
    
     <div class=" col-sm-4">
        <label for="inputlg">Customer Name</label>
         <input class="form-control input-lg purchase-supplier" value="<?php echo $cname; ?>"  type="text" name="pcategory" autocomplete="off" readonly>   
        <input type="hidden" name="supplierid" value="<?php echo $customerid; ?> ">  
    </div>
    
     <div class=" col-sm-2">
        <label for="inputlg">Pending Amount</label>
          <input class="form-control input-lg total-purchase-amount"  type="text" value="<?php echo $pamount; ?>" name="tamount" autocomplete="off" readonly> 
        <input class="form-control input-lg "  type="hidden" value="<?php echo $tray_amount; ?>" name="tray_amount" autocomplete="off" readonly>   
    </div>
    
    <div class=" col-sm-2">
        <label for="inputlg">Received Amount</label>
          <input class="form-control input-lg pay-amount"  type="number" name="payamount" autocomplete="off" required>    
    </div>
    
    </div>
    
    
       <div class=" col-sm-12">
       
       
       <div class=" col-sm-2">
        <label for="inputlg">Discount Amount</label>
           <input class="form-control input-lg "  type="number" name="discount" autocomplete="off" required>    
    </div>
    
    <div class=" col-sm-2">
        <label for="inputlg">Cash Amount</label>
           <input class="form-control input-lg "  type="number" name="cash_amount" autocomplete="off" required>    
    </div>
    
     <div class=" col-sm-2">
        <label for="inputlg">Bank Amount</label>
           <input class="form-control input-lg "  type="number" name="bank_amount" autocomplete="off" required>    
    </div>
    <div class=" col-sm-2">
        <label for="inputlg">Pay Type</label>
           <select class="form-control input-lg selectpicker select-payment-type" data-live-search="true" name="paymenttype" id="supplierdetails" required>
            <option value="">Select your Option</option>
            <option>Bank</option>
           <option>Cash</option>
            <option>Cash And Bank</option>
          
          </select>       
    </div>
    
     <div class=" col-sm-2">
        <label for="inputlg">Payment Reference</label>
            <input class="form-control input-lg pay-reference"  type="text" name="payreference" autocomplete="off">   
    </div>
    
     <div class=" col-sm-2">
        <label for="inputlg">Remark</label>
            <input class="form-control input-lg "  type="text" name="remark" autocomplete="off"> 
    </div>
       
       </div>
          
<div class=" col-sm-12">
&nbsp;
</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>