<?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(extra_amount) as extra_amount,sum(discount) as discount
FROM (
  SELECT cm.id,cm.ccode,cm.cname,cm.mobile,SUM(sd.tamount) as samount,0 as payamount,sum(sd.extra_amount) as extra_amount,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,0 as extra_amount,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_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']+$row['extra_amount']+$opening_balance)-($row['payamount']+$row['discount']);

$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="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 required>
        </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">Customer Name</label>
        <div class="col-sm-4">
        <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>
    
    <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">Received 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">   Discount Amount</label>
        <div class="col-sm-4">
         <input class="form-control input-lg "  type="number" name="discount" autocomplete="off" required value="0">    
        </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" required>
            <option value="">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">       
       <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=" 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>