<?php 

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

$edit_id  =  $_GET['spid'];

$sql = mysqli_query($conn,"SELECT * FROM customer_payment_header WHERE id='$edit_id' ");
 
$row=mysqli_fetch_array($sql,MYSQLI_ASSOC);

$invoice        = $row['cashno'];
$invoicedate_temp    = new DateTime($row['collectiondate']);
$invoicedate 		= $invoicedate_temp->format('d-m-Y');
$customerid     = $row['customerID'];
$amount         = $row['received_amount'];
$pay_type       = $row['paymenttype'];
$paymentreference = $row['paymentreference'];
$discount = $row['discount'];
$tray_amount = $row['tray_amount'];
$sales_inv=$row['sales_inv'];

$cash_amount=$row['cash_amount'];
$bank_amount=$row['bank_amount'];

$remark = $row['remark'];

$pending=$row['pending'];


$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 ,sum(tray_amount) as tray_amount
FROM (
  SELECT cm.id,cm.ccode,cm.cname,cm.mobile,SUM(sd.tamount) as samount,0 as payamount,0 as discount,0 as tray_amount 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,0 as tray_amount 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);

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

?>
<!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">Edit Customer Payment Details </h2><br>
    <form class="form-horizontal" action="paydiesel_salesUpdate.php" method="post" id="accOverFrom">
    
    <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>
          <input type="hidden" value="<?php echo $edit_id; ?>" name="edit_id">
          <input type="hidden" value="<?php echo $sales_inv;?>" name="sales_inv">
        </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" value="<?php echo $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 $pending; ?>" name="tamount" autocomplete="off" readonly>
        </div> 
      </div>
    
    
    <div class="form-group"> 
        <label class="control-label col-sm-4">Paid Amount</label>
        <div class="col-sm-4">
      <input class="form-control input-lg pay-amount"  type="text" name="payamount" value="<?php echo $amount; ?>" 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="text" name="discount" autocomplete="off" required value="<?php echo $discount; ?>">    
        </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="selectpaymenttype">
            <option value="<?php echo $pay_type;?>"><?php echo $pay_type;?></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="paymentreference" autocomplete="off" value="<?php echo $paymentreference; ?>"
        </div> 
      </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" value="<?php echo $remark; ?>">  
        </div> 
      </div>
     


      
     <div class="form-group">        
      <div class="col-sm-12 text-center">
          <button type="submit" 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>
	$(document).ready(function(){

	  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: false,
			  autoclose: true,
    });
    //$('input[name="invoicedate"]').datepicker( "setDate", new Date());  

  
    if($('#selectpaymenttype').val()=='cash') {
      $('.paymentreference').hide();
    } else {
      $('.paymentreference').show();
    }

	  $('#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>