<style>
    .btn-group button {
        height:30px;}

    .amount{
        text-align: right !important;
    }

    .quantity {
        text-align: center !important;
  }
    
</style>

<script>
$(document).ready(function(){
	jQuery('.datepicker2').datepicker({
			dateFormat : 'dd-mm-yy',
			changeMonth : true,
			changeYear : true
			});
    $('.add_more').click(function(e){
        e.preventDefault();
        $(this).before("<input name='file_load[]' type='file' class='uni' multiple/>");
    });
	
	jQuery("#content").find("input").keyup(function() {
		jQuery(this).val(jQuery(this).val().toUpperCase());
	});
        $('select').select2();
        /*****GST Register supplier's color change start******/    
		function format(state) {
		var text = state.text.match(/\(GST\)/g);
		var modTxt = state.text.replace("(GST)","");
		if(text != null)  return '<span style="color:#85c420;">'+modTxt+"</span>";
		else              return modTxt;
		}
		$("#supplierID").select2({
		formatResult: format,
		formatSelection: format,
		escapeMarkup: function(m) { return m; }
		});
		/*****GST Register supplier's color change end******/
        $.each($(".select2-container"), function (i, n) {
            $(n).next().show().fadeTo(0, 0).height("0px").css("left", "auto"); // make the original select visible for validation engine and hidden for us
            $(n).prepend($(n).next());
            $(n).delay(500).queue(function () {
                $(this).removeClass("validate[required]"); //remove the class name from select2 container(div), so that validation engine dose not validate it
                $(this).dequeue();
            });
        });	
	<?php if($invoiceNo): ?>
		$('.amountApplied<?php echo $invoiceNo; ?>').focus();
		var $container = $("html,body");
		var $scrollTo = $('.amountApplied<?php echo $invoiceNo; ?>');
		$container.animate({scrollTop: $scrollTo.offset().top - $container.offset().top, scrollLeft: 0},500);
	<?php endif; ?>
});
function saveSupplierPayment() {
	if( $("#supplierPayment_Form").validationEngine('validate') && checkInvoiceSelection()){
		var balance = accounting.unformat($("#outOfBalance").val());
		if (balance != 0){
			alert ("<?php echo $this->lang->line('balanceError'); ?>"+balance);
			return false;
		}else {
			$("#supplierPayment_Form").submit();
			
			//var urls = "<?php echo base_url();?>aptransaction/payment";
			//					window.location = urls;
		};
	}
}

function checkInvoiceSelection(){
	var Return = false;
	$("#listInvoice tbody tr").each(function(){
		if($("td:last input",this).is(':checked')){
			Return=true;
		}
	});

	$("#liste_credit_note tbody tr").each(function(){
		if($("td:last input",this).is(':checked')){
			Return=true;
		}
	});

	$("#list_histo_invoice tbody tr").each(function(){
		if($("td:last input",this).is(':checked')){
			Return=true;
		}
	});

	if(!Return){
		alert("Please select at least one invoice before save !");
	}	
	return Return;
}


	function changeList(obj){
		var id = obj.value;
		var urls = "<?php echo base_url();?>aptransaction/payment/"+id;
		window.location = urls;
	/*	alert(id);
		$.ajax({
			type: "POST",
			url: "<?php base_url()?>supplierpayment/newForm/",
			data: "supplier="+id,
			dataType:"json",
			success: function(content){
				if(content.status == "success") {
					$('#supplierAddress').val(content.message.addressName);
				} else {
					$("#error").html('<p>'+content.message+'</p>');
				}
			}
		}); 
		return false;
	*/}
 function getDiscount(me){
	var tr = $(me).attr("data-trId");
	var payable = accounting.unformat($("#" + tr + " td:eq(5)").text());
	if (isNaN(payable )) 
	    payable =0;
	var discount =  payable * accounting.unformat($(me).val()) / 100;
	var afterDiscount = payable - discount;
	var amountApplied = accounting.unformat($("#" + tr + " td:eq(7)").text());

	$("#" + tr + " td:eq(7) input").val(accounting.formatnumber(discount,2));
	if (amountApplied > afterDiscount)
		$("#" + tr + " td:eq(7) input").val(accounting.unformat(afterDiscount));
	getTotal();
	
 }
 
	 
 function getTotal (){
//	  
//	$("#totalDiscount").val("");
//	$("#totalApplied").val("");
//	
//	 var n =$("#listInvoice > tbody > tr").length-1;
//	 var amount =0;
//	 var discount = 0;
//	 var apBalance = 0;
//	 var amountApplied =0;
//	 var totalAmountApplied = 0;
//	 for (i=0; i<=n; i++) {
//		 	amountApplied = accounting.unformat($('#listInvoice > tbody > tr:eq(' + i + ') > td:eq(7) input').val())*1;
//			totalAmountApplied= totalAmountApplied +  amountApplied;
//			if (isNaN(amountApplied))
//				amountApplied = 0;
//			//count discount only if amt applied > 0
////			if (amountApplied > 0)
//				discount= discount + (accounting.unformat($('#listInvoice > tbody > tr:eq(' + i + ') > td:eq(6) input').val()) *1); 
//	 }
//         
// 	$("#totalDiscount").val(accounting.formatNumber(discount,2) );  
//	$("#totalApplied").val(accounting.formatNumber(totalAmountApplied,2));
//  
//	//$("#totalOtherCharges").val(total + totalCharge);
//	calculateBalanceAfterApplied(totalAmountApplied,discount);
//	//hoi(totalAmountApplied,discount);
//	$("#amountPaid").val(accounting.formatNumber($("#totalApplied").val(),2));
//	$("#totalPaid").val(accounting.formatNumber($("#totalApplied").val(),2));
//	$("#outOfBalance").val('0.00');
//        

    var  totalAppliedInvoice = 0, totalApplied = 0, totalDebit = 0, totalCredit = 0, totalhisto = 0;
        
        $("#listInvoice > tbody > tr").each (function(){
            var tr = $(this);
            var amt = $("td:eq(6)", tr).find("input").val();
            totalAppliedInvoice += accounting.unformat(amt)*1;
        });
        
        
        $("#liste_debit_note > tbody > tr").each (function(){
            var tr = $(this);
             var amt = $("td:eq(6)", tr).find("input").val();
            totalDebit += accounting.unformat(amt)*1;
        });
        
         $("#liste_credit_note > tbody > tr").each (function(){
            var tr = $(this);
            var amt = $("td:eq(6)", tr).find("input").val();
            totalCredit += accounting.unformat(amt)*1;
        });
        

         $("#list_histo_invoice > tbody > tr").each (function(){
            var tr = $(this);
            var amt = $("td:eq(6)", tr).find("input").val();
            totalhisto += accounting.unformat(amt)*1;
        });
        

        totalApplied = totalAppliedInvoice  + totalhisto + totalCredit - totalDebit;
       
        //$("#totalApplied").val(accounting.formatNumber(totalApplied, 2));
        $("#totalPaid, #amountPaid").val(accounting.formatNumber(totalApplied, 2));
        
         $("#totalappliedHistoInvoice").val(accounting.formatNumber(totalhisto, 2));
        $("#totalappliedInvoice").val(accounting.formatNumber(totalAppliedInvoice, 2));
        $("#totalappliedDebit").val(accounting.formatNumber(totalDebit, 2));
        $("#totalappliedCredit").val(accounting.formatNumber(totalCredit, 2));
        
        //alert(totalAppliedInvoice);
  }
  
	function calculateTotalCharges(me) {
		var charges = $(me).val();
		var totalDiscount = accounting.unformat($("#totalDiscount").val())*1;
		if (isNaN(charges )) 
			charges =0;
		if (isNaN(totalDiscount)) 
			totalDiscount =0;
//		var totalCharges = totalDiscount - charges;
		var totalCharges = charges - totalDiscount; //to get (-)val
		$("#totalOtherCharges").val(accounting.formatNumber(totalCharges,2));
		$("#otherCharges").val(accounting.formatNumber(totalCharges,2));
		calculateBalanceAfterCharges(totalCharges); 
  }
  
	function updateTotalAmountPaid(me) {
		//alert($(me).val());
		var totalPaid = $(me).val();
		
		$("#totalPaid").val(accounting.formatNumber(totalPaid,2));
		calculateBalanceAfterPaid(totalPaid);
	}

	function reverseAmountPaid(){
		var totalPaid = 0;
		var amountPaid = accounting.unformat($("#amountPaid").val())*1;
		var totalApplied = accounting.unformat($("#totalApplied").val())*1;
		var totalCharges = accounting.unformat($("#totalCharges").val())*1;
		var outOfBalance = accounting.unformat($("#outOfBalance").val())*1;

		if (isNaN(outOfBalance )) 
			totalPaid =0;
		
		if (isNaN(totalApplied )) 
			totalApplied =0;
		
		if (isNaN(totalCharges )) 
			totalCharges =0;
				
//		totalPaid = amountPaid + outOfBalance  ;
		totalPaid = amountPaid - outOfBalance  ;
		$("#amountPaid").val(accounting.formatNumber(totalPaid,2));
		$("#totalPaid").val(accounting.formatNumber(totalPaid,2));
		$("#outOfBalance").val('0.00');
	}
	
	function updateAmountApplied(me) {
                var tr = $(me).parents("tr");
//		var tr = $(me).attr("data-trId");
	/*	var rRow = $(me).parents("tr");
		var payable = accounting.unformat($("td:eq(5)", rRow).text());
		if (isNaN(payable )) 
			payable =0;
		var balance = accounting.unformat($("#outOfBalance").val());
//		alert (balance);
		if (isNaN(balance )) 
			balance =0;
		if (balance > 0){
//			balance = balance * (-1);
			//alert ('balance='+balance+' payable='+payable);
			if (balance > payable){
				//applied = payable
				$('td:eq(7)', $(me).parents('tr')).find("input").val(accounting.formatNumber(payable,2));
//				$(rRow).find("td:eq(7) input").val(accounting.formatNumber(payable,2));
                        }else{
				//applied = balance
				//$("td:eq(7)", rRow).val(balance);
				$('td:eq(7)', $(me).parents('tr')).find("input").val(accounting.formatNumber(balance,2));
//				$(rRow).find("td:eq(7) input").val(accounting.formatNumber(balance,2));
		}
            }
            
            */
           
           if($(me).is(":checked")){
                $("td:eq(6)", tr).find("input").val($("td:eq(5)", tr).text());
                $("td:eq(6)", tr).find("input").removeAttr("readonly");
                
            }else{
              $("td:eq(6)", tr).find("input").val("");
              $("td:eq(6)", tr).find("input").attr("readonly","readonly");
            }
            getTotal();
          // $(tr).val()
	}

  	function calculateBalance() {
		var totalPaid = accounting.unformat($("#totalPaid").val())*1;
		var totalApplied = accounting.unformat($("#totalApplied").val())*1;
		var totalCharges = accounting.unformat($("#totalCharges").val())*1;
		if (isNaN(totalPaid )) 
			totalPaid =0;
		
		if (isNaN(totalApplied )) 
			totalApplied =0;
		
		if (isNaN(totalCharges )) 
			totalCharges =0;
				
//		var outOfBalance = totalApplied - totalPaid - totalCharges;
		var outOfBalance = totalPaid - totalApplied - totalCharges;
	
		$("#outOfBalance").val(accounting.formatNumber(outOfBalance,2));
		if (outOfBalance<0)
			$("#outOfBalance").attr("style", "color:red;");
		else
			$("#outOfBalance").attr("style", "color:black;");
//		if (outOfBalance == 0)
//			$("#mysubmit").removeAttr('readonly');
//			$("#mysubmit").show();
//		else	$("#mysubmit").attr('readonly','readonly');	
//			$("#mysubmit").show();
  }

  	function calculateBalanceAfterPaid(me) {
		var totalPaid = me;
		var totalApplied = accounting.unformat($("#totalApplied").val())*1;
		var totalCharges = accounting.unformat($("#totalOtherCharges").val())*1;
		
		if (isNaN(totalApplied )) 
			totalApplied =0;
		
		if (isNaN(totalCharges )) 
			totalCharges =0;
				
//		var outOfBalance = totalApplied - totalPaid - totalCharges;
		var outOfBalance = totalPaid - totalApplied - totalCharges;
//		alert ('after paid'+outOfBalance +'='+ totalApplied +'-'+ totalPaid +'-'+ totalCharges);
		
		$("#outOfBalance").val(accounting.formatNumber(outOfBalance,2));
		if (outOfBalance<0)
			$("#outOfBalance").attr("style", "color:red;");
		else
			$("#outOfBalance").attr("style", "color:black;");
	}

  	function calculateBalanceAfterApplied(me,discount) {
//		alert('calculateBalanceAfterApplied');
		var totalPaid = accounting.unformat($("#totalPaid").val())*1;
		var totalApplied = me;
		var totalCharges = 0 ;//$("#totalOtherCharges").val()*1;
		var bankCharges = accounting.unformat($("#BankCharges").val())*1;
		if (isNaN(bankCharges))
			bankCharges =0;
		if (isNaN(totalPaid )) 
			totalPaid =0;
		
//		totalCharges =discount - bankCharges;
		totalCharges = bankCharges - discount  ;
		$("#totalOtherCharges").val(accounting.formatNumber(totalCharges,2));		
		$("#otherCharges").val(accounting.formatNumber(totalCharges,2));
//		var outOfBalance = totalApplied - totalPaid - totalCharges;
		var outOfBalance = totalPaid - totalApplied - totalCharges;
		//alert ('after applied'+outOfBalance +'='+ totalApplied +'-'+ totalPaid +'-'+ bankCharges +'+'+discount);
		
		$("#outOfBalance").val(accounting.formatNumber(outOfBalance,2));
		if (outOfBalance<0)
			$("#outOfBalance").attr("style", "color:red;");
		else
			$("#outOfBalance").attr("style", "color:black;");
  	}
  
    function calculateBalanceAfterCharges(me) {
		var totalPaid = accounting.formatNumber($("#totalPaid").val())*1;
		var totalApplied = accounting.formatNumber($("#totalApplied").val())*1;
		var totalCharges = me;
		if (isNaN(totalApplied)) 
			totalApplied =0;
		if (isNaN(totalPaid)) 
			totalPaid =0;
				
//		var outOfBalance = totalApplied - totalPaid - totalCharges;
		var outOfBalance = totalPaid - totalApplied - totalCharges;
		//alert ('after charges'+outOfBalance +'='+ totalApplied +'-'+ totalPaid +'-'+ totalCharges);
		$("#outOfBalance").val(accounting.formatNumber(outOfBalance,2));
		if (outOfBalance<0)
			$("#outOfBalance").attr("style", "color:red;");
		else
			$("#outOfBalance").attr("style", "color:black;");
  }
  
	function refreshSupplier(){
  		//<span style="color:#85c420;">'+modTxt+"</span>
		$.ajax({
			type: "POST",
			url: "<?php echo base_url() ?>supplier/refreshSupplier",
			success: function(data) {
			
			   var resul = eval ("(" + data + ")"); 
    	       var n=resul.records.length;
    	       var i=0;
    	       var opt='' +
				'<option></option>';
    	        for (i=0;i<n;i++)
    	        	if(resul.records[i].gstRegNo != ''){
    	        		opt=opt + '<option value="' + resul.records[i].id + '">' + resul.records[i].supplierName + ' (GST)</option>';
    	        	}else{
    	        		opt=opt + '<option value="' + resul.records[i].id + '">' + resul.records[i].supplierName + '</option>';
    	        	}
				$('#supplier').empty();
    	      $('#supplier').append(opt);
			}
			 });
		 
	}
	function refreshProject(){
		$.ajax({
			type: "POST",
			url: "<?php echo base_url() ?>project/ProjectSetup",
			success: function(data) {
			
			   var resul = eval ("(" + data + ")"); 
			   var n=resul.records.length;
			   var i=0;
			   var opt='' +
				'<option></option>';
				for (i=0;i<n;i++)
					opt=opt + '<option value="' + resul.records[i].id + '">' + resul.records[i].project_name + '</option>';
				$('#projectID').empty();
				$('#projectID').append(opt);
			}
		 });
		 
	}


 function displaybanksInfos() {
       $("#acctNo").val($("#bank option:selected").attr("data-bankAccountNo"));
   }
   
   function refreshPayMethod(){
        $.ajax({
        type: "POST",
        url: "<?php echo base_url() ?>OthersPayment/refreshPayMethod",
        success: function(data) {
	
           var resul = eval ("(" + data + ")"); 
           var n=resul.records.length;
           var i=0;
           var opt='' +
                '<option></option>';
                for (i=0;i<n;i++)
                        opt=opt + '<option value="' + resul.records[i].id +'">' + resul.records[i].name + '</option>';
                $('#paymentMethodID').empty();
                $('#paymentMethodID').append(opt);
        }
   });	
}
    
    function copy_amount(me){
       if($(me).is(":checked")){
            var tr = $(me).parent().parent();
            $("td:eq(6)", tr).find("input").removeAttr("readonly");
            $("td:eq(6)", tr).find("input").val($("td:eq(5)", tr).text());
            getTotal();
       }else{
            var tr = $(me).parent().parent();
            $("td:eq(6)", tr).find("input").attr("readonly","readonly");
            $("td:eq(6)", tr).find("input").val("");
            getTotal();
        }
    }
   
   
</script>
<div id="content"> 
    <div class="wrap">
        <div class="head">
            <div class="info">
                    <!--<h1><?php echo element('compName', $this->session->userdata('logged_in')) ?> [ 
                <?php echo element('compNo', $this->session->userdata('logged_in')) ?> ]</h1>-->
                <ul class="breadcrumb">
                    <li><a href='<?php echo base_url() . "home" ?>'><?php echo $this->lang->line('dashboard') ?></a> <span class="divider">-</span></li>
                    <li><a href='<?php echo base_url() . "gl/home" ?>'> <?php echo $this->lang->line('module') ?></a> <span class="divider">-</span></li>
                    <li class="active"><?php echo $this->lang->line('title') ?></li>
                </ul>
            </div>

            <div class="search">
                <form action="<?php echo base_url() ?>admin/search" method="post">
                    <input name="search_text" type="text" placeholder="search..."/>                                
                    <button type="submit"><span class="i-magnifier"></span></button>
                </form>
            </div>        

        </div><!-- head --> 
        <div class="content" id="grid_content_1">
            <form id="supplierPayment_Form" name="supplierPayment_Form" method="post" action="<?php echo base_url(); ?>aptransaction/saveSupplierPayment">
                <input type="hidden" id="ID" name="ID" value="<?php echo $ID; ?>" />
                <div class="block">
                    <div class="head">
                        <h2><?php echo $this->lang->line('title') ?></h2>
                    </div><!-- head -->
                    <div class="row-fluid scRow">                            
                        <div class="span6 scCol">
                            <div class="block" id="grid_block_1">

                                <div class="content">
                                    <div class="content nb"> <!-- row 1 -->
                                        <div class="span3"><?php echo $this->lang->line('supplier'); ?> *</div>
                                        <div class="span9">
                                            <?php /*
                                            $preselpayee = $supplierID;
                                            $options = '';
                                            $options['null'] = '';
                                            if ($supplier)
                                                foreach ($supplier as $row) {
                                                    if ($row->gstRegNo != '' && $row->gstRegNo != '0') {
                                                        $options[$row->ID] = $row->supplierName . ' (GST)';
                                                    } else {
                                                        $options[$row->ID] = $row->supplierName;
                                                    }
                                                }
                                            $js = 'id="supplierID" onChange="changeList(this)" class="span11 validate[required]"';
                                            echo form_dropdown('supplierID', $options, $preselpayee, $js);*/
                                            ?>    
                                            <select onchange="changeList(this)" name="supplierID" id="supplierID" class="span11 validate[required]"><option></option>
                                                <?php
                                                if ($supplier) {
                                                    foreach ($supplier as $row) {
                                                        if ($row->gstRegNo != '' && $row->gstRegNo != '0') {
                                                        ?>
                                                    
                                                        <option value="<?php echo $row->ID; ?>" <?php echo ($supplierID == $row->ID)?"selected='selected'":""; ?>><?php echo $row->supplierName . ' (GST)'; ?></option>
                                                        <?php
                                                        } else {
                                                        ?>
                                                        <option value="<?php echo $row->ID; ?>" <?php echo ($supplierID == $row->ID)?"selected='selected'":""; ?>><?php echo $row->supplierName; ?></option>
												    
												    <?php }
													}
												}
												?>
                                            </select>   
                                            <div class="btn-group span1">
                                                <button class="btn-icon btn-small dropdown-toggle" data-toggle="dropdown">
                                                    <span class="i-arrow-down-2"></span>
                                                </button>
                                                <ul class="dropdown-menu">
                                                    <!-- dropdown menu links -->
                                                    <li><a target="_blank" href="<?php echo base_url() ?>supplier/SupplierNew"  id="btnNewSupplier">
                                                            <i class="i-plus-3"></i> <?php echo $this->lang->line('addNewSupplier') ?></a></li>
                                                    <li><a href="#" onclick="refreshSupplier()" id="btnNewSupplier">
                                                            <i class="i-ccw "></i><?php echo $this->lang->line('refreshlist') ?></a></li>
                                                </ul>
                                            </div><!-- btn group -->
                                        </div>                
                                    </div><!-- End Row 1 -->
                                    <!-- div class="controls-row">
                                          <div class="span3"><?php echo $this->lang->line('address'); ?></div>
                                          <div class="span8" id="supplierAddress"  style="line-height:1">
                                    <?php
                                    if ($supplierInfo) {
                                        echo $supplierInfo->line1 . '<br>' . $supplierInfo->line2 . '<br>';
                                        echo $supplierInfo->line3 . '<br>' . $supplierInfo->supplierPostCode . ' ' . $supplierInfo->city;
                                        echo '<br>Tel:' . $supplierInfo->phoneNumber1 . ' Fax:' . $supplierInfo->fax;
                                    }
                                    ?>
                                    </div>
                                    </div -->
                                    <div class="content nb">
                                        <div class="span3"><?php echo $this->lang->line('amtPaid'); ?></div>
                                        <div class="span9">
                                            <?php
                                            $data = array(
                                                'name' => 'amountPaid',
                                                'readonly' => 'readonly',
                                                'id' => 'amountPaid',
                                                'onBlur' => 'updateTotalAmountPaid(this)',
                                                'ondblclick' => 'reverseAmountPaid()',
                                                'class' => 'span12');
                                            echo form_input($data, set_value('amountPaid'));
                                            ?>
                                        </div>
                                    </div>

                                    <div class="content nb"> 
                                        <div class="span3"><?php echo $this->lang->line('paymentMethod'); ?> *</div>
                                        <div class="span9">
                                              
                                            <select name="paymentMethodID" id="paymentMethodID" class="span11 validate[required]"><option></option>
                                                <?php
                                                if ($paymentmethod) {
                                                    foreach ($paymentmethod as $row) {
                                                        ?>
                                                    
                                                        <option value="<?php echo $row->ID; ?>"><?php echo $row->name; ?></option>
												    
												    <?php 
													}
												}
												?>
                                            </select>
                                            <div class="btn-group span1">
                                                <button class="btn-icon btn-small dropdown-toggle" data-toggle="dropdown">
                                                    <span class="i-arrow-down-2"></span>
                                                </button>
                                                <ul class="dropdown-menu">
                                                    <!-- dropdown menu links -->
                                                    <li><a target="_blank" href="<?php echo base_url() ?>setting/masterCodeSetup"  id="btnLocation">
                                                            <i class="i-plus-3"></i><?php echo $this->lang->line('addpaymethod') ?></a></li>


                                                    <li><a href="#" onclick="refreshPayMethod()"><i class="i-ccw"></i><?php echo $this->lang->line('refreshlist') ?></a></li>
                                                </ul>
                                            </div> <!-- btnn group -->
                                        </div> 
                                    </div> 
                                    <div class="content nb">
                                        <div class="span3"><?php echo $this->lang->line('refNo'); ?> *</div>
                                        <div class="span9">
                                            <?php
                                            $data = array(
                                                'name' => 'referenceNo',
                                                'id' => 'referenceNo',
                                                'class' => 'span12 validate[maxSize[30][required]]',);
                                            echo form_input($data, set_value('referenceNo'));
                                            ?>
                                        </div>
                                    </div> 
                                    <div class="content nb">
                                        <div class="span3"><?php echo $this->lang->line('acctDescription'); ?> *</div>
                                        <div class="span9">
                                            <?php
                                            $data = array(
                                                'name' => 'acctDescription',
                                                'id' => 'acctDescription',
                                                'class' => 'span12 validate[maxSize[30][required]]',);
                                            echo form_input($data, set_value('acctDescription'));
                                            ?>
                                        </div>
                                    </div>
                                    <div class="content nb">
                                        <div class="span3"><?php echo $this->lang->line('paymentDate'); ?></div>
                                        <div class="span9"><div class="input-prepend span12"><span class="add-on span2"><i class="i-calendar"></i></span>
                                                <?php
                                                $data = array(
                                                    'name' => 'paymentDate',
                                                    'id' => 'paymentDate',
                                                    'type' => 'text',
                                                    'class' => 'span10 datepicker2 tac financhk');
                                                echo form_input($data, date("d-m-Y"));
                                                ?>
                                            </div>
                                        </div>    
                                    </div>      
                                </div><!-- content-->
                            </div><!-- grid block 1-->
                        </div><!-- scCol -->
                        <div class="span6 scCol">
                            <div class="block" id="grid_block_2">
                                <div class="content">
                                    <div class="content nb">
                                        <div class="span3"><?php echo $this->lang->line('bank') ?> *</div>
                                        <div class="span9">
                                            <select onchange="displaybanksInfos()" name="bank" id="bank" class="span12 validate[required]"><option></option>' +
                                                <?php
                                                if ($bankaccount) {
                                                    foreach ($bankaccount as $row) {
                                                        ?>
                                                        '<option data-currentBalance="<?php echo $row->currentBalance; ?>" data-bankAccountNo="<?php echo $row->bankAccountNo; ?>"  value="<?php echo $row->ID; ?>"><?php echo $row->bankShortName . " | ";
                                                echo str_replace("'", " ", $row->branch, $count);
                                                ?></option>'+
    <?php }
}
?>
                                            </select>
                                        </div><!-- span -->
                                    </div><!-- control row -->
                                    <div class="content nb">
                                        <div class="span3"><?php echo $this->lang->line('accno') ?></div>
                                        <div class="span9">
                                            <input id="acctNo" readonly="readonly" name="acctNo" type="text" placeholder="" class="span12">
                                        </div><!-- span -->
                                    </div><!-- control row -->

                                    <div class="content nb">
                                        <div class="span3"><?php echo $this->lang->line('formNo'); ?></div>
                                        <div class="span9">
                                            <?php
                                            $data = array(
                                                'style' => "color:red;",
                                                'name' => 'formNo',
                                                'id' => 'formNo',
                                                'class' => 'span8 validate[maxSize[30]]',
                                                'value' => $formNo,);
                                            echo form_input($data, set_value('formNo'));
                                            echo '<input  class="span4"   type="text" placeholder="Serial No" readonly="readonly">';
                                            ?>
                                        </div>
                                    </div>              
                                    <div class="content nb"> <!-- row 1 -->
                                        <div class="span3"><?php echo $this->lang->line('project'); ?></div>
                                        <div class="span9">
                                            <?php
                                            $preselproject = '0';
                                            $options = '';
                                            $options[] = $this->lang->line('pleaseselect');
                                            if ($project)
                                                foreach ($project as $row) {
                                                    $options[$row->ID] = $row->project_name;
                                                }
                                            $js = 'class="span11"';
                                            echo form_dropdown('projectID', $options, $preselproject, $js);
                                            ?>       
                                            <div class="btn-group span1">
                                                <button class="btn-icon btn-small dropdown-toggle" data-toggle="dropdown">
                                                    <span class="i-arrow-down-2"></span>
                                                </button>
                                                <ul class="dropdown-menu">
                                                    <!-- dropdown menu links -->
                                                    <li><a target="_blank" href="<?php echo base_url() ?>project/projectSetup"  id="btnNewProject">
                                                            <i class="i-plus-3"></i> <?php echo $this->lang->line('addNewProject') ?></a></li>
                                                    <li><a href="#" onclick="refreshProject()" id="btnNewProject">
                                                            <i class="i-ccw "></i><?php echo $this->lang->line('refreshlist') ?></a></li>
                                                </ul>
                                            </div><!-- btn group -->
                                        </div>                
                                    </div><!-- End Row 1 -->
                                    <div class="content nb">
                                        <div class="span3"><?php echo $this->lang->line('currency'); ?></div>
                                        <div class="span9">
                                            <?php
                                            $preselcurr = ($supplierID == 0 || $supplierID == '') ? '1' : $supplierInfo->currencyID;
                                            $options = '';
                                            $options[] = '--Please Select--';
                                            foreach ($currency as $row) {
                                                $options[$row->fldid] = $row->fldcurr_code;
                                            }
                                            $js = 'class="span12"';
                                            echo form_dropdown('currencyID', $options, $preselcurr, $js);
                                            ?>       
                                        </div> 
                                    </div>
                                    <div class="content nb">
                                        <div class="span3"><?php echo $this->lang->line('exchangeRate'); ?></div>
                                        <div class="span9">
                                            <?php
                                            $data = array(
                                                'name' => 'exchangeRate',
                                                'id' => 'exchangeRate',
                                                'class' => 'span12 validate[maxSize[30]] tar',
                                                'placeholder' => 'Example: 3.20000',
                                                'value' => ($exchangeRate) ? number_format($exchangeRate->rateConv, 5) : '',);
                                            echo form_input($data, set_value('exchangeRate'));
                                            ?>
                                        </div>
                                    </div>      
                                </div><!-- content-->
                            </div><!-- grid block 2-->
                        </div><!-- scCol -->
                    </div><!-- row-fluid scRow-->
                </div>
                <div class="row-fluid scRow">                            
                    <div class="span12 scCol">
                        <textarea name="memo" placeholder = "<?php echo $this->lang->line('memo'); ?>" id="memo" class="span12"></textarea>
                    </div><!-- scCol -->
                </div><!-- scCol -->



                <div class="row-fluid">
                    <div class="span12 scCol">
                        <div class="block" id="grid_block_4">
                            <div class="head">
                                <h2><?php echo $this->lang->line('pending_invoices'); ?></h2>
                                <ul class="buttons">
                                    <li><a class="block_toggle collapsed" data-toggle="collapse" data-parent="#accordion2" href="#collapseOne">
                                            <span class="i-arrow-down-3"></span>
                                        </a></li>
                                </ul>                                        
                            </div><!-- head -->
                            <div class="content np">
                                <table name="listInvoice" id="listInvoice" cellpadding="0" cellspacing="0" width="100%" id="test" class="editable" border="0">
                                    <thead>
                                        <tr>
                                            <th style="display:none"></th>
                                            <th>#</th>
                                            <th><?php echo $this->lang->line('invoiceFormNo'); ?></th>
                                            <th><?php echo $this->lang->line('invoiceNo'); ?></th>
                                            <th><?php echo $this->lang->line('invoiceDate'); ?></th>
                                            <th><?php echo $this->lang->line('totalAmount'); ?></th>   
                                            <th><?php echo $this->lang->line('apBalance'); ?></th>               
                                            <th  class="tar"><?php echo $this->lang->line('amountApplied'); ?></th>   
                                            <th class="tac"><input type="checkbox" readonly /></th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        //echo var_dump($rsPendingInvoice);
                                        if ($rsPendingInvoice) {
                                            $i = 1;
                                            //for new payment, ID = supplierInvoiceID. for update payemnt, ID=detailPaymentID
                                            foreach ($rsPendingInvoice as $rs) {
                                                echo '<tr id="' . $rs->ID . '"><td>' . $i++;
                                                $suppPaymentID = isset($rs->suppPaymentID) ? $rs->suppPaymentID : "";
                                                $suppInvoiceID = isset($rs->suppInvoiceID) ? $rs->suppInvoiceID : "";
                                                echo '<input type="hidden" id="suppPaymentID[]" name="suppPaymentID[]" value="' . $suppPaymentID . '"/>';
                                                echo '<input type="hidden" id="suppInvoiceID[]" name="suppInvoiceID[]" value="' . $suppInvoiceID . '"/>';
                                                echo '</td><td>' . $rs->formNo . '</td>';
                                                echo '<td >' . $rs->supplierInvoiceNo . '</td>';
                                                echo '<td>' . date("d-m-Y", strtotime($rs->invoiceDate)) . '</td>';
                                                echo '<td class="tar">' . number_format($rs->totalAmount, 2, '.', ',') . '</td>';
                                                echo '<td class="tar">' . number_format($rs->totalPayable, 2, '.', ',') . '</td>';
                                               
                                                echo '<td>';
                                                echo '<div>';
                                                $dataAmuontApplied = array(
                                                    'name' => 'amountApplied[]',
                                                    'readonly' => 'readonly',
                                                    'onkeyup' => 'getTotal()',
                                                    'class' => 'amountApplied' . $rs->supplierInvoiceNo . ' span12 validate[custom[number]] tar',);
                                                echo form_input($dataAmuontApplied);
                                                echo '</div>';
                                                echo '</td>'
                                                . '<td class="tac"><input type="checkbox" onclick="updateAmountApplied(this)" /></td></tr>';
                                            }
                                        }
                                        ?>
                                    
                                    </tbody>
                                </table>             
                            </div>                            
                        </div><!-- content-->
                    </div><!-- grid block 3-->
                </div><!-- row-fluid scRow-->

<?php //akmal 20151105. remove table debit note from suppllier payemnt
/*							

                <div class="row-fluid <?php echo ($rsPendingDebit) ? "":"hide"; ?>" >
                    <div class="span12 scCol">
                        <div class="block" id="grid_block_4">
                            <div class="head">
                                <h2>Pending Debit Note</h2>
                                <ul class="buttons">
                                    <li><a class="block_toggle collapsed" data-toggle="collapse" data-parent="#accordion2" href="#collapseOne">
                                            <span class="i-arrow-down-3"></span>
                                        </a>
                                    </li>
                                </ul>                                        
                            </div><!-- head -->
                            <div class="content np">
                                <table id="liste_debit_note" cellpadding="0" cellspacing="0" width="100%">
                                    <thead>
                                        <tr>
                                            <th>#</th>
                                            <th>Form No.</th>
                                            <th>Date </th>
                                            <th>Invoice No</th>
                                            <th class="amount">Total Amount</th>
                                            <th class="amount">Balance</th>
                                            <th class="amount">Amount Applied</th>
                                            <th class="tac"><input type="checkbox" readonly/></th>
                                        </tr>
                                    </thead>

                                    <tbody id="liste_debit_note_body">
<?php
$totalDebit = 0;
if ($rsPendingDebit) {
    $i = 0;
    foreach ($rsPendingDebit as $row) {
        ?>
                                                <tr>
                                                    <td><input value="<?php echo $row->ID; ?>" type="hidden" name="debitNoteId[]"/><?php echo ++$i; ?></td>
                                                    <td><?php echo $row->formNo; ?></td>
                                                    <td><?php echo $row->debitNoteDate; ?></td>
                                                    <td><?php echo $row->invNo; ?></td>
                                                    <td class="amount"><?php echo number_format($row->totalAmount, 2); ?></td>
                                                    <td class="amount"><?php echo number_format($row->total, 2); ?></td>
                                                    <td class="amount">
                                                        <input class="span12 amount" onkeyup="getTotal(this)" type="text" name="debit_amountApplier[]" readonly />
                                                    </td> 
                                                    <td class="tac"><input type="checkbox" onclick="copy_amount(this)" /></td>
                                                </tr>
        <?php
    }
}
?>
                                    </tbody>
                                </table>          
                            </div>                            
                        </div><!-- content-->
                    </div><!-- grid block 3-->
                </div><!-- row-fluid scRow-->
*/ ?>
                <div class="row-fluid <?php echo ($rsPendingCredit) ? "":"hide"; ?>">
                    <div class="span12 scCol">
                        <div class="block" id="grid_block_4">
                            <div class="head">
                                <h2><?php echo $this->lang->line('totalApplied') ?><?php echo $this->lang->line('pending_credit_note') ?></h2>
                                <ul class="buttons">
                                    <li><a class="block_toggle collapsed" data-toggle="collapse" data-parent="#accordion2" href="#collapseOne">
                                            <span class="i-arrow-down-3"></span>
                                        </a></li>
                                </ul>                                        
                            </div><!-- head -->
                            <div class="content np">
                                <table id="liste_credit_note" cellpadding="0" cellspacing="0" width="100%">
                                    <thead>
                                        <tr>
                                            <th>#</th>
                                            <th><?php echo $this->lang->line('formNo') ?></th>
                                            <th><?php echo $this->lang->line('date') ?></th>
                                            <th><?php echo $this->lang->line('invno') ?></th>
                                            <th class="amount"><?php echo $this->lang->line('total_amount') ?></th>
                                            <th class="amount"><?php echo $this->lang->line('balance') ?></th>
                                            <th class="amount"><?php echo $this->lang->line('amound_applied') ?></th>
                                            <th><input type="checkbox" readonly /></th>
                                        </tr>
                                    </thead>

                                    <tbody id="liste_debit_note_body">
									<?php
									if (!empty($rsPendingCredit)) {
									    $i = 0;
									    foreach ($rsPendingCredit as $row) {
									        ?>
                                                <tr>
                                                    <td><?php echo ++$i; ?></td>
                                                    <td><input value="<?php echo $row->ID; ?>" type="hidden" name="creditNoteId[]"/><?php echo $row->formNo; ?></td>
                                                    <td><?php echo $row->creditNoteDate; ?></td>
                                                    <td><?php echo $row->invNo; ?></td>
                                                    <td class="amount"><?php echo number_format($row->totalAmount, 2); ?></td>
                                                    <td class="amount"><?php echo number_format($row->total, 2); ?></td>
                                                    <td class="amount">
                                                        <input class="span12 amount" type="text" name="credit_amountApplier[]" readonly onkeyup="getTotal(this)" />
                                                    </td> 
                                                    <td class="tac"><input type="checkbox" onclick="copy_amount(this)"  /></td>
                                                </tr>
									        <?php
									    }
									}
									?>
                                    </tbody>
                                </table>          
                            </div>                            
                        </div><!-- content-->
                    </div><!-- grid block 3-->
                </div><!-- row-fluid scRow-->



                <div class="row-fluid <?php echo ($rsPendingHistoricalInvoice) ? "":"hide"; ?>">
                    <div class="span12 scCol">
                        <div class="block" id="grid_block_4">
                            <div class="head">
                                <h2><?php echo $this->lang->line('totalApplied') ?><?php echo $this->lang->line('pending_histo_inv') ?></h2>
                                <ul class="buttons">
                                    <li><a class="block_toggle collapsed" data-toggle="collapse" data-parent="#accordion2" href="#collapseOne">
                                            <span class="i-arrow-down-3"></span>
                                        </a></li>
                                </ul>                                        
                            </div><!-- head -->
                            <div class="content np">
                                <table id="list_histo_invoice" cellpadding="0" cellspacing="0" width="100%">
                                    <thead>
                                        <tr>
                                            <th>#</th>
                                            <th><?php echo $this->lang->line('formNo') ?></th>
                                            <th>Date </th>
                                            <th><?php echo $this->lang->line('invno') ?></th>
                                            <th class="amount"><?php echo $this->lang->line('total_amount') ?></th>
                                            <th class="amount"><?php echo $this->lang->line('balance') ?></th>
                                            <th class="amount"><?php echo $this->lang->line('amound_applied') ?></th>
                                            <th><input type="checkbox" readonly /></th>
                                        </tr>
                                    </thead>

                                    <tbody>
<?php
if ($rsPendingHistoricalInvoice) {
    $i = 0;
    foreach ($rsPendingHistoricalInvoice as $row) {
        ?>
                                                <tr>
                                                    <td><?php echo ++$i; ?></td>
                                                    <td><input value="<?php echo $row->ID; ?>" type="hidden" name="hInvoiceID[]"/><?php echo $row->formNo; ?></td>
                                                    <td><?php echo $row->invoiceDate; ?></td>
                                                    <td><?php echo $row->invNo; ?></td>
                                                    <td class="amount"><?php echo number_format($row->totalAmount, 2); ?></td>
                                                    <td class="amount"><?php echo number_format($row->totalPayable, 2); ?></td>
                                                    <td class="amount">
                                                        <input class="span12 amount" type="text" name="histo_amountApplier[]" readonly onkeyup="getTotal(this)" />
                                                    </td> 
                                                    <td class="tac"><input type="checkbox" onclick="copy_amount(this)"  /></td>
                                                </tr>
        <?php
    }
}
?>
                                    </tbody>
                                </table>          
                            </div>                            
                        </div><!-- content-->
                    </div><!-- grid block 3-->
                </div><!-- row-fluid scRow-->




                <div class="content">    
                    <div class="row-fluid scRow">  
                        <div class="span12 scCol">
                            <div class="block">
                                <div class="head">
                                    <h2><?php echo $this->lang->line('total_amount') ?></h2>
                                    <ul class="buttons">
                                        <li><a href="#" class="block_toggle"><span class="i-arrow-down-3"></span></a></li>
                                    </ul>
                                </div>
                                <div class="content np">				
                                    <div class="span6 scCol">
                                        <div class="block" id="grid_block_4">							
                                            <div class="content np">
                                                <div class="controls-row">
                                                    <div class="span5"><?php echo $this->lang->line('total_amount_applied_inv') ?></div>
                                                    <div class="span7">
                                                        <input id="totalappliedInvoice"  class="span12 amount validate[required min[1]]" type="text" readonly="readonly"  placeholder="0">
                                                    </div>
                                                </div><!-- row control -->
                                                 <div class="controls-row">
                                                    <div class="span5"><?php echo $this->lang->line('total_amount_applied_histo') ?></div>
                                                    <div class="span7">
                                                        <input id="totalappliedHistoInvoice"  class="span12 amount validate[required min[1]]" type="text" readonly="readonly"  placeholder="0">
                                                    </div>
                                                </div><!-- row control -->
												<!--akmal 20151105. remove denitnote applied from supplier payment-->
                                                <!--div class="controls-row">
                                                    <div class="span5 ">Total Debit Note Applied</div>
                                                    <div class="span7">
                                                        <input id="totalappliedDebit"  class="span12 amount validate[required min[1]]" type="text" readonly="readonly"  placeholder="0">
                                                    </div>
                                                </div--><!-- row control -->
                                                <div class="controls-row">
                                                    <div class="span5"><?php echo $this->lang->line('total_amount_applied_credit') ?></div>
                                                    <div class="span7">
                                                        <input id="totalappliedCredit"  class="span12 amount validate[required min[1]]" type="text" readonly="readonly"  placeholder="0">
                                                    </div>
                                                </div><!-- row control -->
                                            </div>
                                        </div><!-- grid block 1-->
                                    </div><!-- span6 scCol -->           
                                    <div class="span6 scCol">
                                        <div class="block" id="grid_block_4">
                                            <div class="content np">
                                                <!--div class="controls-row">
                                                    <div class="span5"><?php echo $this->lang->line('totalApplied') ?></div>
                                                    <div class="span7">
                                                        <input id="totalApplied" name="totalApplied"  class="span12 amount validate[required]" type="text" readonly="readonly"  placeholder="0">
                                                    </div>
                                                </div--><!-- row control -->

                                                <div class="controls-row">
                                                    <div class="span5"><?php echo $this->lang->line('TotalPaid') ?></div>
                                                    <div class="span7">                                         
                                                        <input  onchange="updateTotalReceive()" name="totalPaid" id="totalPaid" class="span12 amount" type="text" placeholder="0" readonly >
                                                    </div>
                                                </div><!-- row control -->

                                            </div><!-- grid block 1-->
                                        </div><!-- grid block 1-->
                                    </div><!-- span6 scCol -->					
                                </div><!-- content np-->
                                <div class="footer">
                                    <div class="side fr">
                                        <div class="btn-group">
                                            <?php
                                            $data = array(
                                                'name' => 'validat',
                                                'class' => 'btn-form btn-warning-form',
                                                'onClick' => "$('#validate').validationEngine('hide');");
                                            echo form_button($data, 'Hide prompts');
                                            $data = array(
                                                'name' => 'mysubmit',
                                                'id' => 'mysubmit',
                                                //	'readonly'=>'readonly',
                                                'class' => 'btn-form btn-success-form');
                                            $js = 'onclick="saveSupplierPayment()"';
                                            echo form_button($data, $this->lang->line('save'), $js);
                                            //echo form_submit($data,'Submit',$js);
                                            ?>
                                        </div>
                                    </div>
                                </div>
                            </div><!-- block-->
                        </div><!-- span12-->	
                    </div><!--row-fluid scRow -->
                </div> <!--content total -->	

        </div><!-- grid 1 -->
        </form>
    </div><!-- wrap -->
</div><!-- content -->

<?php /* function saveSupplierPayment1() {
  var balance = $("#outOfBalance").val();
  if (balance != 0){
  alert ("<?php echo $this->lang->line('balanceError'); ?>"+balance);
  return false;
  }
  var journalNo = $('#journal_number').val();
  var bil = 1;
  //		var addressID = $('#').val();
  var postData = {
  'journalNo'	: journalNo,
  'description': description,
  'effdate'	: effdate,
  'supplierID'	: $('#supplierID').val(),
  'projectID'	: $('#projectID').val(),
  'amountPaid' : $('#amountPaid').val(),
  'paymentMethodID' : $('#paymentMethodID').val(),
  'referenceNo' : $('#referenceNo').val(),
  'accountID' : $('#accountID').val(),
  'formNo' : $('#formNo').val(),
  'paymentDate' : $('#paymentDate').val(),
  'currencyID' : $('#currencyID').val(),
  'memo' 		: $('#memo').val(),
  'totalDiscount' : $('#totalDiscount').val(),
  'bankCharges' : $('#bankCharges').val(),
  'totalOtherCharge' : $('#totalOtherCharge').val(),
  'totalPaid'  : $('#totalPaid').val(),
  'totalApplied' : $('#totalApplied').val(),
  'outOfBalance' : $('#outOfBalance').val(),
  };
  $.ajax({
  type: "POST",
  dataType: "json",
  url: "<?php echo base_url();?>supplierPayment/save",
  data: postData ,
  success: function(content){
  $("#error").html('<p>'+content.message+'</p>');
  if(content.status == "success") {
  $('#journal_number').val(content.message.seqNumber);
  $("#btnAddnew").show();
  }else if(content.status == "success1"){
  $('#journal_number').val(content.journalID).change();
  $('#description').val(content.message.description);
  $('#effdate').val(content.message.effdate);
  for (var i=0; i<content.datatbls.length; i++){
  if(content.datatbls[i].amount_dr != '0')
  $("#oTable tbody").append('<tr><td style="display:none">' +
  content.datatbls[i].ID + '</td><td>' +
  bil + '</td><td>' +
  content.datatbls[i].acctcode + '</td><td id="description">' +
  //							content.datatbls[i].acctcode + ' [' + content.datatbls[i].acctname + ']</td><td>' +
  content.datatbls[i].description + '</td><td class="tar" id="amount_dr">' +
  parseFloat(content.datatbls[i].amount_dr).toFixed(2) + '</td><td class="tar" id="amount_cr"></td><td class="tac">' +
  '<button class="btn btn-mini btn-danger removeline" title="<?php echo $this->lang->line('delete') ?>"><span class="i-trashcan"></button>' + '</td></tr>');
  if(content.datatbls[i].amount_cr != '0')
  $("#oTable tbody").append('<tr><td style="display:none">' +
  content.datatbls[i].ID + '</td><td>' +
  bil + '</td><td>' +
  content.datatbls[i].acctcode + '</td><td id="description">' +
  //							content.datatbls[i].acctcode + ' [' + content.datatbls[i].acctname + ']</td><td>' +
  content.datatbls[i].description + '</td><td class="tar" id="amount_dr"></td><td class="tar" id="amount_cr">' +
  parseFloat(content.datatbls[i].amount_cr).toFixed(2) + '</td><td class="tac">' +
  '<button class="btn btn-mini btn-danger removeline" title="<?php echo $this->lang->line('delete') ?>"><span class="i-trashcan"></button>' + '</td></tr>');
  //						if(i == 0)
  if(content.datatbls[i].amount_cr != '0')
  total_credit = parseFloat(total_credit) + parseFloat($('#oTable > tbody > tr:eq(' + i + ') > td:eq(5)').text());
  if(content.datatbls[i].amount_dr != '0')
  total_debit = parseFloat(total_debit) + parseFloat($('#oTable > tbody > tr:eq(' + i + ') > td:eq(4)').text());
  bil++;
  }
  $('#bill').val(content.datatbls.length);
  $("#btnAddnew").show();
  $("#amountcredit").val(total_credit.toFixed(2));
  $("#amountdebit").val(total_debit.toFixed(2));
  $("#totalamount").val((parseFloat(total_debit) - parseFloat(total_credit)).toFixed(2));
  }else if(content.status == "failed"){
  alert(content.message);
  }
  }
  });        // Here your ajax action after delete confirmed
  //		location.reload();
  }

  } */ ?>