<style >
    .btn-group button{
        height:30px;}
    
    .amount {
        text-align: right !important;
        font-weight: bold !important;
    }
    
    .center {
        text-align: center !important;
    }
	  
    button {
        margin-left:2px;
    }
   
    #content .wrap #grid_content_1 .row-fluid.scRow .row-fluid.scRow .span12.scCol #grid_block_5 .content.np #listDetails tbody #trInfos td .row-fluid .controls-row .span3 {
        font-size: 11px;
        color: #000;
        font-weight: bold;
    }
    .test {
        font-size: 10px;
    }
    .span3 {
        font-size: 10px;
    }
    tbody {
        font-size: 10px;
    }
	 tbody td {
        font-size:11px;
	}
    #content .wrap #grid_content_1 .row-fluid.scRow .row-fluid.scRow .span12.scCol #grid_block_6 .content.np #listCharge tbody #trInfosCharge td .row-fluid .controls-row .span3 {
        font-weight: bold;
    }


</style>
<script type="application/javascript">
   
    window.onbeforeunload = function (e) {
        if (accounting.unformat( $('#outofbalance').val() ) !== 0 ){
  var message = "The out of balance is different of zero, Confirm leave ",
  e = e || window.event;
  // For IE and Firefox
  if (e) {
    e.returnValue = message;
  }

  // For Safari
  return message;
        }
};

   $(document).ready(function(){
	$('select').select2();
	$.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();
            });
        });	
        jQuery('.datepicker2').datepicker({
                dateFormat : 'dd-mm-yy',
                changeMonth : true,
                changeYear : true
        });
});


    function refreshProject(){
        $.ajax({
        type: "POST",
        url: "<?php echo base_url() ?>project/refreshProject",
        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].projectName + '</option>';
        $('#project').empty();
        $('#project').append(opt);
        }
        });
    }
    
    function displaybanksInfos() {
       $("#acctNo").val($("#bank option:selected").attr("data-bankAccountNo"));
   }
    
    function  activateOrDesactivate(me){
        if($(me).is(':checked')){
             var tr = $(me).parent().parent();
              v = $(tr).find("td").eq(4).text();
             $(tr).find("td").eq(5).children("input:last-child").removeAttr("disabled");
              $(tr).find("td").eq(5).children("input:last-child").val(v);
        }else {
            var tr = $(me).parent().parent();
             $(tr).find("td").eq(5).children("input:last-child").attr("disabled","disabled");
              $(tr).find("td").eq(5).children("input:last-child").val("");
        }
        getTotalDiscountAndTotalApplied();
    }
     
    function loadPayerDetailsValue(){
        $.ajax({
            type: "POST",
            data: "payer=" + $("#payer").val(),
            url: "<?php echo base_url() ?>artransaction/loadPayerDetailsValue",
            success: function(data) {
                $("#bodyListPendingInvoices").empty();
                var rs = eval ("(" + data + ")"); 
                var n=rs.recordSet.length;
                $("#currency").val(rs.currency);
                $("#exchange_rate").val(rs.exchangeRate);
                $("#previewOutOfBalance").val(rs.outOfBalance);
                nList = 0;
                var tbody='';
                for (i=0;i<n;i++){
                    tbody=tbody + '<tr>' + 
                                  '    <td>' + (++nList) + '</td>' +
                                  '    <td>' + rs.recordSet[i].formNo + '</td>' +
                                  '    <td>' + rs.recordSet[i].salesInvoiceDate + '</td>' +
                                  '    <td class="amount">' + accounting.formatNumber(rs.recordSet[i].totalamount, 2)  + '</td>' +
                                  '    <td class="amount">' + accounting.formatNumber(rs.recordSet[i].totalReceivable, 2)  + '</td>' +
                                  '    <td><input onkeyup ="getTotalDiscountAndTotalApplied()" disabled type="text" class="span12 amount" /> </td>' +
                                  '    <td><input onkeyup ="getTotalDiscountAndTotalApplied()" disabled type="text" class="span12 amount" /> </td>' +
                                  '    <td><input data-invoiceID="'+ rs.recordSet[i].ID  +'" onchange="activateOrDesactivate(this)" type="checkbox" /> </td>' +
                                  '</tr>'
                }
                $("#bodyListPendingInvoices").append(tbody);
            }
        });
    }
    
    function selecOrUnselecttAll(me){
        if($(me).is(':checked')){
           $("#listDetails > tbody > tr").each (function(){
              var tr = $(this);
              v = $(tr).find("td").eq(4).text();
              $(tr).find("td").eq(5).children("input:last-child").val("0");
              $(tr).find("td").eq(6).children("input:last-child").val(v);
              $(tr).find("td").eq(7).children("input:last-child").prop("checked",true);
              $(tr).find("td").eq(5).children("input:last-child").removeAttr("disabled");
             $(tr).find("td").eq(6).children("input:last-child").removeAttr("disabled");
           });
        }else {
            $("#listDetails > tbody > tr").each (function(){
              var tr = $(this);
              v = $(tr).find("td").eq(4).text();
              $(tr).find("td").eq(5).children("input:last-child").val("");
              $(tr).find("td").eq(6).children("input:last-child").val("");
              $(tr).find("td").eq(7).children("input:last-child").prop("checked",false);
              $(tr).find("td").eq(5).children("input:last-child").attr("disabled","disabled");
             $(tr).find("td").eq(6).children("input:last-child").attr("disabled","disabled");
            });
        }
        getTotalDiscountAndTotalApplied();
    }
    
    function getTotalDiscountAndTotalApplied(){
           var  totalAppliedInvoice = 0, totalApplied = 0, totalDebit = 0, totalCredit = 0, totalHisto=0;
        
        $("#listDetails > tbody > tr").each (function(){
            var tr = $(this);
            var ck = $(tr).find("td").eq(6).children("input:last-child");
            if($(ck).length){
                if($(ck).is(':checked')){
                    totalAppliedInvoice += accounting.unformat($(tr).find("td").eq(5).children("input:last-child").val())*1;
                }
            }else{
                totalAppliedInvoice += accounting.unformat($(tr).find("td").eq(5).text());
            }
        });
        
        $("#liste_debit_note > tbody > tr").each (function(){
            var tr = $(this);
            var ck = $(tr).find("td").eq(7).children("input:last-child");
            if($(ck).length){
                if($(ck).is(':checked')){
                    totalDebit += accounting.unformat($(tr).find("td").eq(6).children("input:last-child").val())*1;
                }
            }else{
                totalDebit += accounting.unformat($(tr).find("td").eq(6).text());
            }
        });
        
         $("#liste_credit_note > tbody > tr").each (function(){
            var tr = $(this);
            var ck = $(tr).find("td").eq(7).children("input:last-child");
            if($(ck).length){
                if($(ck).is(':checked')){
                    totalCredit += accounting.unformat($(tr).find("td").eq(6).children("input:last-child").val())*1;
                }
            }else{
                   totalCredit += accounting.unformat($(tr).find("td").eq(6).text());
            }
        });
        
        $("#liste_histo > tbody > tr").each (function(){
            var tr = $(this);
            var ck = $(tr).find("td").eq(7).children("input:last-child");
            if($(ck).length){
                if($(ck).is(':checked')){
                    totalHisto += accounting.unformat($(tr).find("td").eq(6).children("input:last-child").val())*1;
                }
            }else{
                   totalHisto += accounting.unformat($(tr).find("td").eq(6).text());
            }
        });
        
        totalApplied = totalAppliedInvoice + totalHisto - totalCredit + totalDebit;
        bankcharges = accounting.unformat($("#bankcharges").val());
        
        $("#totalappliedHistoInvoice").val(accounting.formatNumber(totalHisto, 2));
        
        $("#totalapplied").val(accounting.formatNumber(totalApplied, 2));
        $("#totalreceived").val(accounting.formatNumber(totalApplied+bankcharges, 2));
        $("#amountreceive").val(accounting.formatNumber(totalApplied, 2));
        
        
        $("#totalappliedInvoice").val(accounting.formatNumber(totalAppliedInvoice, 2));
        $("#totalappliedDebit").val(accounting.formatNumber(totalDebit, 2));
        $("#totalappliedCredit").val(accounting.formatNumber(totalCredit, 2));
        
    }
     
    
    function updateTotalReceive(){
        var tRec = accounting.unformat($("#previewOutOfBalance").val())*1 + accounting.unformat($("#amountreceive").val())*1;   
        if (isNaN(ob))
            ob =0;
        if (isNaN(tRec))
            tRec = 0;
        $("#totalreceived").val(accounting.formatNumber(tRec, 2));
        var ob = accounting.unformat($("#totalreceived").val()) - accounting.unformat($("#totalapplied").val()) -accounting.unformat($("#totalotherscharges").val());
        $("#outofbalance").val(accounting.formatNumber(ob, 2));
    }
    
    function editRow(me){
        if(!$("#_mntDiscount").length){
        var tr = $(me).parent().parent();
       
        var td2 =$(tr).find("td").eq(5);
         var v = $(td2).text();
        $(td2).empty();
        $(td2).append (
                "<input id='_montAplied' data-previewvalue='" + v + "' type='text' value ='" + v +"' onkeyup='getTotalDiscountAndTotalApplied()' class='amount span12' />" 
                );
        
        var td3 =$(tr).find("td").eq(6);
         $(td3).empty();
        $(td3).append (
                 '<button type="button" data-invoiceid="'+ $(me).attr("data-invoiceid") +'" data-myid="'+ $(me).attr("data-myid") +'" onclick="updatePaymentRow(this)" class="btn-icon btn-mini btn-success-form"><i class="i-checkmark-3"></i></button>' +
                  '<button type="button" data-invoiceid="'+ $(me).attr("data-invoiceid") +'" data-myid="'+ $(me).attr("data-myid") +'" onclick="canceledit(this)" class="btn-icon btn-mini btn-danger-form"><i class="i-cross"></i></button>'
                );
    }else {
            $("#msgDiv").attr("class","alert alert-error");
             $("#msgDiv").html("Oops !!! A row is being edit ...");
        }
        
    }
    
    function canceledit(me){
        var tr = $(me).parent().parent();
      
         var v = $("#_montAplied").attr("data-previewvalue");
          var td2 =$(tr).find("td").eq(5);
         $(td2).empty(); 
        $(td2).text (v);
        
        var td3 =$(tr).find("td").eq(6);
         $(td3).empty();
        $(td3).append (
                 '<button type="button" data-invoiceid="'+ $(me).attr("data-invoiceid") +'" data-myid="'+ $(me).attr("data-myid") +'" onclick="editRow(this)"   class="btn-icon btn-mini btn-warning-form"><i class="i-pen"></i></button>' +
                 ' <button type="button" data-invoiceid="'+ $(me).attr("data-invoiceid") +'" data-myid="'+ $(me).attr("data-myid") +'" onclick="deleteRow(this)" class="btn-icon btn-mini btn-danger-form"><i class="i-trashcan"></i></button>'
                );
        getTotalDiscountAndTotalApplied();
    }
    
    function updateOtherCharges(){
         var t = accounting.unformat($("#bankcharges").val()) + accounting.unformat($("#totaldiscount").val());
         if (isNaN(t))
            t =0;
         $("#totalotherscharges").val(accounting.formatNumber(t, 2));
         $("#totalotherschargesCopy").val(accounting.formatNumber(t, 2));
         updateTotalReceive();
    } 
     
    function updatePaymentRow(me){
        var tr = $(me).parent().parent();
         $.ajax({
                type: "POST",
                data: {
                    
                  detaildId : $(me).attr("data-myid"),
                  amountApplied : accounting.unformat($("#_montAplied").val()),
                  
                  
                  masterId:  $("#_masterId").val(),
                  memo: $("#memo").val(),
                  bankCharges:  accounting.unformat($("#bankcharges").val()),
                  
                  totalApplied:  accounting.unformat($("#totalapplied").val()),
                  totalReceived : accounting.unformat($("#amountreceive").val()),
                  
                  invoiceId:  $(me).attr("data-invoiceid"),
                  totalReceivable : accounting.unformat($("#_montAplied").attr("data-previewvalue")) - accounting.unformat($("#_montAplied").val()) + accounting.unformat($(tr).find("td").eq(4).text()),
            
                },
                url: "<?php echo base_url() ?>arTransaction/updatePaymentRow",
                success: function(data) {
                    if (data>0){ 
                        $("#msgDiv").attr("class","alert alert-success");
                        $("#msgDiv").html("Well Done !!! Payment Successfully Updated ");
                    } else {
                        $("#msgDiv").attr("class","alert alert-error");
                        $("#msgDiv").html("Oops !!! fail to update data ");
                    }
                    var td1 =$(tr).find("td").eq(5);
                    var v = $("#_montAplied").val();
                    $(td1).empty();
                    $(td1).text(v);
                    
                    var td3 =$(tr).find("td").eq(6);
                     $(td3).empty();
                    $(td3).append (
                             '<button type="button" data-invoiceid="'+ $(me).attr("data-invoiceid") +'" data-myid="'+ $(me).attr("data-myid") +'" onclick="editRow(this)"   class="btn-icon btn-mini btn-warning-form"><i class="i-pen"></i></button>' +
                             ' <button type="button" data-invoiceid="'+ $(me).attr("data-invoiceid") +'" data-myid="'+ $(me).attr("data-myid") +'" onclick="canceledit(this)" class="btn-icon btn-mini btn-danger-form"><i class="i-trashcan"></i></button>'
                            );
                      }
            });
            
        
    }
    function deleleteTR(me){
           var td=me.parentNode;
              var tr=td.parentNode;
              var tBody=tr.parentNode;
              tBody.removeChild(tr);
    }
 
    function deleteRow(me){
            $("#_popupTitle").html("Confirm delete");
            $("#_popupContent").html("Your about to delete definively this row. <br/> Click on ok to confirm");
            $('#_popupFooter').empty();
            $('#_popupFooter').append('<button type="button" class="btn-form btn-success-form" id="_ok">Ok</button>' +
            '<button type="button" class="btn-form btn-danger-form" id="canb">Cancel</button>');
            $("#_popupTitle").html("Confirm delete");
            $("#canb").click(function() {
            $('#popupModal').modal('hide');
         });
          $('#popupModal').modal('show');
         $("#_ok").click(function() {
            $('#popupModal').modal('hide');
          //  discount : accounting.unformat($("#_mntDiscount").val()),
          //  amountApplied : accounting.unformat($("#_montAplied").val()),
            
            var tr = $(me).parent().parent();
          
            amtApplied = accounting.unformat($(tr).find("td").eq(5).text());
            totalReceivable = amtApplied + accounting.unformat($(tr).find("td").eq(4).text());
            $(tr).find("td").eq(5).text("0");
            getTotalDiscountAndTotalApplied();
          
              $.ajax({
                type: "POST",
                data: {
                    
                  detaildId : $(me).attr("data-myid"),
                  
                  
                  masterId:  $("#_masterId").val(),
                  
                  
                  totalApplied:  accounting.unformat($("#totalapplied").val()),
                  totalReceived : accounting.unformat($("#amountreceive").val()),
                  
                  invoiceId:  $(me).attr("data-invoiceid"),
                  totalReceivable : totalReceivable,
                  

                },
                url: "<?php echo base_url() ?>arTransaction/payment_delete_invoice",
                success: function(data) {
                  deleleteTR(me);
                }
            });
    });
    }
    
    function addInvoices(){
        $("#listDetails").append("<tr id='_mytr'><td colspan='7'>\n\
         <div class='alert alert-info'>Out Standing Invoices</div></td></tr>");
         $.ajax({
            type: "POST",
            data: "payer=" + $("#payer").attr("data-id"),
            url: "<?php echo base_url() ?>artransaction/loadPayerDetailsValue",
            success: function(data) {
                var rs = eval ("(" + data + ")"); 
                var n=rs.recordSet.length;
                nList = 0;
                var tbody='';
                for (i=0;i<n;i++){
                    tbody=tbody + '<tr>' + 
                                  '    <td>' + (++nList) + '</td>' +
                                  '    <td>' + rs.recordSet[i].formNo +  '</td>' +
                                  '    <td>' + rs.recordSet[i].salesInvoiceDate + '</td>' +
                                  '    <td class="amount">' + accounting.formatNumber(rs.recordSet[i].totalamount, 2)  + '</td>' +
                                  '    <td class="amount">' + accounting.formatNumber(rs.recordSet[i].totalReceivable, 2)  + '</td>' +
                                  '    <td><input onkeyup ="getTotalDiscountAndTotalApplied()" disabled type="text" class="span12 amount" /> </td>' +
                                  '    <td class="center"><input data-invoiceID="'+ rs.recordSet[i].ID  +'" onchange="activateOrDesactivate(this)" type="checkbox" /> </td>' +
                                  '</tr>'
                }
              $("#listDetails").append(tbody);
                
                
                $("#liste_debit_note_body").append("<tr id='_mytr_debit'><td colspan='8'>\n\
                        <div class='alert alert-info'>Out Standing Debit Note</div></td></tr>");
            
                var n=rs.rsDebit.length;
                nList = 0;
                var tbody='';
                for (i=0;i<n;i++){
                    tbody=tbody + '<tr>' + 
                                  '    <td>' + (++nList) + '</td>' +
                                  '    <td>' + rs.rsDebit[i].formNo +  '</td>' +
                                  '    <td>' + rs.rsDebit[i].debitNoteDate + '</td>' +
                                  '    <td>' + rs.rsDebit[i].invNo + '</td>' +
                                  '    <td class="amount">' + accounting.formatNumber(rs.rsDebit[i].totalAmount, 2)  + '</td>' +
                                  '    <td class="amount">' + accounting.formatNumber(rs.rsDebit[i].total, 2)  + '</td>' +
                                  '    <td><input onkeyup ="getTotalDiscountAndTotalApplied()" disabled type="text" class="span12 amount" /> </td>' +
                                  '    <td class="center"><input data-debitnoteID="'+ rs.rsDebit[i].ID  +'" onchange="activateOrDesactivate_note(this)" type="checkbox" /> </td>' +
                                  '</tr>'
                }
                $("#liste_debit_note_body").append(tbody);
                
              
                
                var n=rs.rsCredit.length;
                nList = 0;
                var tbody='';
                for (i=0;i<n;i++){
                    tbody=tbody + '<tr>' + 
                                  '    <td>' + (++nList) + '</td>' +
                                  '    <td>' + rs.rsCredit[i].formNo +  '</td>' +
                                  '    <td>' + rs.rsCredit[i].creditNoteDate + '</td>' +
                                  '    <td>' + rs.rsCredit[i].invNo + '</td>' +
                                  '    <td class="amount">' + accounting.formatNumber(rs.rsCredit[i].totalAmount, 2)  + '</td>' +
                                  '    <td class="amount">' + accounting.formatNumber(rs.rsCredit[i].total, 2)  + '</td>' +
                                  '    <td><input onkeyup ="getTotalDiscountAndTotalApplied()" disabled type="text" class="span12 amount" /> </td>' +
                                  '    <td class="center"><input data-creditnoteID="'+ rs.rsCredit[i].ID  +'" onchange="activateOrDesactivate_note(this)" type="checkbox" /> </td>' +
                                  '</tr>'
                }
                
                  $("#liste_credit_note_body").append("<tr id='_mytr_credit'><td colspan='8'> <div class='alert alert-info'>Out Standing Credit Note</div></td></tr>");
        
                $("#liste_credit_note_body").append(tbody);
                
                 var n=rs.rsHisto.length;
                nList = 0;
                var tbody='';
                for (i=0;i<n;i++){
                    tbody=tbody + '<tr>' + 
                                  '    <td>' + (++nList) + '</td>' +
                                  '    <td>' + rs.rsHisto[i].formNo +  '</td>' +
                                  '    <td>' + rs.rsHisto[i].invoiceDate + '</td>' +
                                  '    <td>' + rs.rsHisto[i].invNo + '</td>' +
                                  '    <td class="amount">' + accounting.formatNumber(rs.rsHisto[i].totalAmount, 2)  + '</td>' +
                                  '    <td class="amount">' + accounting.formatNumber(rs.rsHisto[i].totalReceivable, 2)  + '</td>' +
                                  '    <td><input onkeyup ="getTotalDiscountAndTotalApplied()" disabled type="text" class="span12 amount" /> </td>' +
                                  '    <td class="center"><input data-historicalinvoiceID="'+ rs.rsHisto[i].ID  +'" onchange="activateOrDesactivate_note(this)" type="checkbox" /> </td>' +
                                  '</tr>'
                }

                $("#liste_histo_body").append("<tr id='_mytr_histo'><td colspan='8'> <div class='alert alert-info'>Out Standing Historical Invoice</div></td></tr>");
                $("#liste_histo_body").append(tbody);

    
                
                $("#btnSave").text("Save");
                $("#btnSave").attr("onclick","saveNewInvoices()");
                
            }
        });
    }
    
    function saveNewInvoices(){
            pDetails ='{"rs":[';
            firstime = true;
            $("#listDetails > tbody > tr").each (function(){
                var tr = $(this);
                var ck = $(tr).find("td").eq(6).children("input:last-child");

                if($(ck).is(':checked')){
                    str ='';
                    if (firstime){
                     firstime = false;
                     str = ''
                    }else {
                       firstime = false;
                        str = ',' 
                    }
                    arBalance = accounting.unformat($(tr).find("td").eq(4).text())*1;
                    mntApplied = accounting.unformat($(tr).find("td").eq(5).children("input:last-child").val())*1;
                    pDetails += str + '{"invoiceID":"' + $(ck).attr("data-invoiceid") + '"' +
                                        ',"amountApplied":"' + mntApplied + '"' +
                                        ',"arBalance":"' + arBalance + '"}';
                }
            });
            pDetails += ']}';
                        
            $.ajax({
                type: "POST",
                data: {
                  lastLine : $("#totalreceived").attr("data-lastRow"),
                  
                  masterId:  $("#_masterId").val(),
                  memo: $("#memo").val(),
                  bankCharges:  accounting.unformat($("#bankcharges").val()),
                  
                  totalApplied:  accounting.unformat($("#totalapplied").val()),
                  totalReceived : accounting.unformat($("#totalreceived").val()),
                
                  pdetail : pDetails,
                  creditNote: getSelectedcreditnote(),
                  debitNote: getSelecteddebitnote(),
                  histo: getSelectedHisto(),
                  
                },
                url: "<?php echo base_url() ?>arTransaction/addpaymentdetailsOnly",
                success: function(data) {
                    
                    if (data>0){ 
                        $("#btnSave").attr("class"," btn-form btn-success-form disabled");
                        $("#msgDiv").attr("class","alert alert-success");
                        $("#msgDiv").html("Well Done !!! Update Successfully made ");
                    } else {
                        $("#msgDiv").attr("class","alert alert-error");
                        $("#msgDiv").html("Oops !!! fail to update data ");
                    }
                    location.reload();
                }
            });
    }
    
     
    function getSelecteddebitnote(){
         pDetails ='{"debit":[';
            firstime = true;
            $("#liste_debit_note > tbody > tr").each (function(){
                var tr = $(this);
                var ck = $(tr).find("td").eq(7).children("input:last-child");

                if($(ck).is(':checked')){
                    str ='';
                    if (firstime){
                     firstime = false;
                     str = ''
                    }else {
                       firstime = false;
                        str = ',' 
                    }
                     
                    mntApplied = accounting.unformat($(tr).find("td").eq(6).children("input:last-child").val())*1;
                    balance = accounting.unformat($(tr).find("td").eq(5).text())-mntApplied;
                    pDetails += str + '{"debitnoteID":"' + $(ck).attr("data-debitnoteID") + '"' +
                                        ',"amountApplied":"' + mntApplied + '"' +
                                        ',"balance":"' + balance + '"}'
                }
            });
            pDetails += ']}';
            return pDetails;
    }
    
    function getSelectedcreditnote(){
         pDetails ='{"credit":[';
            firstime = true;
            $("#liste_credit_note > tbody > tr").each (function(){
                var tr = $(this);
                var ck = $(tr).find("td").eq(7).children("input:last-child");

                if($(ck).is(':checked')){
                    str ='';
                    if (firstime){
                     firstime = false;
                     str = ''
                    }else {
                       firstime = false;
                        str = ',' 
                    } 
                     
                    mntApplied = accounting.unformat($(tr).find("td").eq(6).children("input:last-child").val())*1;
                    balance = accounting.unformat($(tr).find("td").eq(5).text())-mntApplied;
                    pDetails += str + '{"creditnoteID":"' + $(ck).attr("data-creditnoteID") + '"' +
                                        ',"amountApplied":"' + mntApplied + '"' +
                                        ',"balance":"' + balance + '"}'
                }
            });
            pDetails += ']}';
            return pDetails;
    }
    
    function  activateOrDesactivate_note(me){
        if($(me).is(':checked')){
             var tr = $(me).parent().parent();
              v = $(tr).find("td").eq(5).text();
             $(tr).find("td").eq(6).children("input:last-child").removeAttr("disabled");
              $(tr).find("td").eq(6).children("input:last-child").val(v).focus();
        }else{
            var tr = $(me).parent().parent();
            $(tr).find("td").eq(6).children("input:last-child").attr("disabled","disabled");
            $(tr).find("td").eq(6).children("input:last-child").val("");
        }
        getTotalDiscountAndTotalApplied();
    }
   
 

    function updateMaster(){
    if( $("#paymentDetail").validationEngine('validate') && finanEngine('financhk')){
        if(confirm('Please, confirm update ...')){
       
    $.ajax({
    type: "POST",
            data: {
                     masterId:  $("#_masterId").val(),
                    project : $("#project").val(),
                    referenceNo : $("#referenceNo").val(),
                    bankID:  $("#bank").val(),
                     date : $("#receivePaymentDate").val(),
                    memo : $("#memo").val(),
                     formNo : $("#formNo").val(),
                     amountreceive: accounting.unformat($("#amountreceive").val()),
                     paymentmethod:$("#paymentmethod").val()

            },
            url: "<?php echo base_url() ?>Artransaction/update_receivePayment_header", 
            success: function(data) {
                alertbox ("Details successfully saved",1);
            }
    }); //end ajax
    }
}
    }

     function displaybanksInfos() {
       $("#acctNo").val($("#bank option:selected").attr("data-bankAccountNo"));
   }
   
   function fct_total_receive(){
        var bank = accounting.unformat($("#bankcharges").val());
        var totalApplied = accounting.unformat($("#totalapplied").val());
        $("#totalreceived").val(accounting.formatNumber(totalApplied+bank, 2)); 
   }
   

   function delete_credit(me){
           $("#_popupTitle").html("Confirm delete");
            $("#_popupContent").html("Your about to delete definively this row. <br/> Click on ok to confirm");
            $('#_popupFooter').empty();
            $('#_popupFooter').append('<button type="button" class="btn-form btn-success-form" id="_ok">Ok</button>' +
            '<button type="button" class="btn-form btn-danger-form" id="canb">Cancel</button>');
            $("#_popupTitle").html("Confirm delete");
            $("#canb").click(function() {
            $('#popupModal').modal('hide');
         });
          $('#popupModal').modal('show');
         $("#_ok").click(function() {
            $('#popupModal').modal('hide');
          //  discount : accounting.unformat($("#_mntDiscount").val()),
          //  amountApplied : accounting.unformat($("#_montAplied").val()),
            
            var tr = $(me).parent().parent();
            var myid =$(me).attr("data-myid");
            var amt  = accounting.unformat($(tr).find("td").eq(5).text()) + accounting.unformat($(tr).find("td").eq(6).text())
            $(tr).find("td").eq(6).text(0);
            getTotalDiscountAndTotalApplied();
              $.ajax({
                type: "POST",
                data:{
                    
                  detaildId : $(me).attr("data-myid"),
                  amountApplied : accounting.unformat($(tr).find("td").eq(6).text()),
                  
                  masterId:  $("#_masterId").val(),
                  
                  totalApplied:  accounting.unformat($("#totalapplied").val()),
                  totalReceived : accounting.unformat($("#amountreceive").val()),
                  
                  creditNoteID:  $(me).attr("data-creditNoteID"),
                  total: amt,
            
                },
                url: "<?php echo base_url() ?>arTransaction/payment_delete_credit",
                success: function(data) {
                      deleleteTR(me);
                      getTotalDiscountAndTotalApplied();
                }
            });          
           
    });   
   }


   function delete_debit(me){
           $("#_popupTitle").html("Confirm delete");
            $("#_popupContent").html("Your about to delete definively this row. <br/> Click on ok to confirm");
            $('#_popupFooter').empty();
            $('#_popupFooter').append('<button type="button" class="btn-form btn-success-form" id="_ok">Ok</button>' +
            '<button type="button" class="btn-form btn-danger-form" id="canb">Cancel</button>');
            $("#_popupTitle").html("Confirm delete");
            $("#canb").click(function() {
            $('#popupModal').modal('hide');
         });
          $('#popupModal').modal('show');
         $("#_ok").click(function() {
            $('#popupModal').modal('hide');
          //  discount : accounting.unformat($("#_mntDiscount").val()),
          //  amountApplied : accounting.unformat($("#_montAplied").val()),
            
            var tr = $(me).parent().parent();
           
            var amt  = accounting.unformat($(tr).find("td").eq(5).text()) + accounting.unformat($(tr).find("td").eq(6).text())
            $(tr).find("td").eq(6).text(0);
            getTotalDiscountAndTotalApplied();
            
              $.ajax({
                type: "POST",
                data: {
                  detaildId : $(me).attr("data-myid"),
                  amountApplied : accounting.unformat($(tr).find("td").eq(6).text()),
                  
                  masterId:  $("#_masterId").val(),

                  totalApplied:  accounting.unformat($("#totalapplied").val()),
                  totalReceived : accounting.unformat($("#amountreceive").val()),
                  
                  debitNoteID: $(me).attr("data-debitNoteID"),
                  total: amt
                  
                },
                url: "<?php echo base_url() ?>arTransaction/payment_delete_debit",
                success: function(data) {
                      deleleteTR(me);
                      getTotalDiscountAndTotalApplied();
                }
            });          
           
    });   
   }
   
   function delete_histo(me){
    $("#_popupTitle").html("Confirm delete");
    $("#_popupContent").html("Your about to delete definively this row. <br/> Click on ok to confirm");
    $('#_popupFooter').empty();
    $('#_popupFooter').append('<button class="btn-form btn-success-form" id="_ok">Ok</button>' +
    '<button class="btn-form btn-danger-form" id="canb">Cancel</button>');
    $("#_popupTitle").html("Confirm delete");
    $("#canb").click(function() {
    $('#popupModal').modal('hide');
    });
    $('#popupModal').modal('show');
    $("#_ok").click(function() {
    $('#popupModal').modal('hide');
    //  discount : accounting.unformat($("#_mntDiscount").val()),
    //  amountApplied : accounting.unformat($("#_montAplied").val()),

    var tr = $(me).parents("tr");
    var myid =$(me).attr("data-myid");
    
    var amt = accounting.unformat($(tr).find("td").eq("6").text());
    $(tr).find("td").eq("6").text("");
    
    getTotalDiscountAndTotalApplied();
    

    $.ajax({
    type: "POST",
    data: {

    detaildId : $(me).attr("data-myid"),
    amountApplied : accounting.unformat(amt),
    
    historicalinvoiceid: $(me).attr("data-historicalinvoiceid"),

    masterId:  $("#_masterId").val(),
    
    totalApplied : accounting.unformat($("#totalapplied").val()),
    totalReceivable : accounting.unformat($(tr).find("td").eq(5).text()) + amt,

    },
    url: "<?php echo base_url() ?>artransaction/deletePaymentDetail_histo",
    success: function(data) {
        deleleteTR (me);
        alertbox("Payment Successfully deleted ",1);
    }
    });

    });
    }
    
    function getSelectedHisto(){
         pDetails ='{"histo":[';
            firstime = true;
            $("#liste_histo > tbody > tr").each (function(){
                var tr = $(this);
                var ck = $(tr).find("td").eq(7).children("input:last-child");

                if($(ck).is(':checked')){
                    str ='';
                    if (firstime){
                     firstime = false;
                     str = ''
                    }else {
                       firstime = false;
                        str = ',' 
                    } 
                     
                    mntApplied = accounting.unformat($(tr).find("td").eq(6).children("input:last-child").val())*1;
                    balance = accounting.unformat($(tr).find("td").eq(5).text())-mntApplied;
                    pDetails += str + '{"invoiceID":"' + $(ck).attr("data-historicalinvoiceid") + '"' +
                                        ',"amountApplied":"' + mntApplied + '"' +
                                        ',"balance":"' + balance + '"}'
                }
            });
            pDetails += ']}';
            return pDetails;
    }


</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">
            <form id="paymentForm" action="#">
                <div class="block">

                    <div class="head">
                        <h2><strong><?php echo $this->lang->line('title') ?></strong></h2>
                    </div><!-- head -->
                    <div class="content np">
                        <?php
                        $address = "";
                        $memo = "";
                        $totalReceive = 0;
                        $totalApplied = 0;
                        $totalOtherCharges = 0;
                        $totalBankCharge = 0;

                        if ($master) {
                            foreach ($master as $rowIn) {
                                $memo = $rowIn->memo;
                                $totalReceive = $rowIn->totalReceived;
                                $totalApplied = $rowIn->totalApplied;
                                $totalOtherCharges = $rowIn->totalOtherCharge;
                                $totalDiscount = $rowIn->totalDiscount;
                                $totalBankCharge = $rowIn->bankCharges;
                                ?> 
                                <div class="row-fluid scRow"> 
                                    <div class="span6 scCol">
                                        <div class="block" id="grid_block_1">
                                            <div class="controls-row">

                                                <div class="span4"><?php echo $this->lang->line('formno') ?></div>
                                                <div class="span4">
                                                    <input class="span12 validate[required]" id="formNo" name="formNo" value='<?php echo $rowIn->frmNo; ?>' style="color:red;"   type="text" placeholder="Form number ...">
                                                </div>
                                                <div class="span4">
                                                    <input class="span12" type="text" value="<?php echo str_pad($rowIn->serialNo, 8, '0', STR_PAD_LEFT); ?>" placeholder="Serial" disabled="disabled">
                                                </div>
                                            </div> <!-- row -->
                                            <div class="controls-row">
                                                <div class="span4"><?php echo $this->lang->line('date') ?></div>
                                                <div class="span8">
                                                    <input class="datepicker2 validate[required] financhk span12" id="receivePaymentDate" value="<?php echo date("d-m-Y", strtotime($rowIn->receivePaymentDate)) ?>" type="text" />
                                                </div>
                                            </div> <!-- row -->

                                            <div class="controls-row">
                                                <div class="span4"><?php echo $this->lang->line('payer') ?></div>
                                                <div class="span8">
                                                    <div class="span12">
                                                        <input type="hidden" id="_masterId" value="<?php echo $rowIn->myid ?>" />
                                                        <input class="span12" id="payer" data-id="<?php echo $rowIn->customerID ?>" value="<?php echo $rowIn->customerName; ?>" type="text" disabled="disabled"/>
                                                    </div>
                                                </div>
                                            </div> <!--control row -->
                                            <div class="controls-row">
                                                <div class="span4"><?php echo $this->lang->line('currency') ?></div>
                                                <div class="span8">
                                                    <div class="span12">
                                                        <input value="<?php echo $rowIn->currencyWord ?>" type="text" disabled="disabled" class="span12"/> 
                                                    </div><!-- span -->
                                                </div><!-- span -->
                                            </div><!-- control row -->
                                            <div class="controls-row">
                                                <div class="span4"><?php echo $this->lang->line('exchangerate') ?></div>
                                                <div class="span8">
                                                    <input value="<?php echo number_format($rowIn->exchangeRate, 5); ?>" type="text" disabled="disabled" class="span12 validate[required] tar"/>           
                                                </div><!-- span -->
                                            </div><!-- control row -->
                                            <div class="controls-row">
                                                <div class="span4"><?php echo $this->lang->line('project') ?></div>
                                                <div class="span8">
                                                    <select id="project" class="span12">
                                                        <option></option>
                                                        <?php
                                                        if ($project != '') {
                                                            foreach ($project as $Prow) {
                                                                ?>
                                                                <option <?php if ($Prow->ID == $rowIn->projectID) echo "selected" ?> value="<?php echo $Prow->ID; ?>"><?php echo $Prow->project_name; ?></option>
                                                                <?php
                                                            }
                                                        }
                                                        ?>
                                                    </select>
                                                </div>
                                            </div> <!-- row control -->

                                        </div><!-- block --- grid block 1-->                                         
                                    </div><!-- scCol -->

                                    <div class="span6 scCol">
                                        <div class="block" id="grid_block_2">
                                            <div class="controls-row">
                                                <div class="span4"><?php echo $this->lang->line('bank') ?></div>
                                                <div class="span8"><select  onchange="displaybanksInfos()" name="bank" id="bank" class="span12 validate[required]"><option></option>' +
                                                        <?php
                                                        if ($bankaccount) {
                                                            foreach ($bankaccount as $row) {
                                                                ?>
                                                                '<option <?php if ($row->ID == $rowIn->bankID) echo "selected" ?> 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="controls-row">
                                                <div class="span4"><?php echo $this->lang->line('acctno') ?></div>
                                                <div class="span8">
                                                    <input id="acctNo" value="<?php echo $rowIn->bankAccountNo ?>" type="text" disabled="disabled" class="span12"/>
                                                </div><!-- span -->
                                            </div><!-- control row -->

                                            <div class="controls-row">
                                                <div class="span4"><?php echo $this->lang->line('paymentmethod') ?></div>
                                                <div class="span8">
                                                      <select  id="paymentmethod" class="span12 validate[required]"><option></option>
                                                <?php
                                                if ($paymentmethod) {
                                                    foreach ($paymentmethod as $row) {
                                                        ?>
                                                        <option <?php  if($row->ID ==$rowIn->paymentMethodID) echo "selected"; ?> value="<?php echo $row->ID; ?>"> <?php echo $row->name; ?></option>
    <?php }
}
?>
                                            </select>  
                                                   </div><!-- span -->
                                            </div><!-- control row -->
                                            <div class="controls-row">
                                                <div class="span4"><?php echo $this->lang->line('refno') ?></div>
                                                <div class="span8">
                                                    <input id="referenceNo" value="<?php echo $rowIn->referenceNo ?>" type="text" class="span12"/>            
                                                </div><!-- span -->
                                            </div><!-- control row -->

                                            <div class="controls-row">
                                                <div class="span4"><?php echo $this->lang->line('amountreceive') ?></div>
                                                <div class="span8">
                                                    <input value="<?php echo number_format($rowIn->totalReceived, 2) ?>"  onkeyup="updateTotalReceive()" type="text" id="amountreceive" class="span12 amount"/>            
                                                </div><!-- span -->
                                            </div><!-- control row -->
                                        </div><!-- grid block 1-->
                                    </div><!-- scCol -->
                                </div>		
                                <div class="row-fluid scRow">                            
                                    <div class="span12 scCol">
                                        <textarea placeholder="<?php echo $this->lang->line('memo') ?>"  id="memo" class="span12"><?php echo $memo; ?></textarea>
                                    </div>
                                </div>

                                <div class="footer">
                                    <div class="side fr">
                                        <div class="btn-group">
                                            <button  onclick="updateMaster()" class="btn-form btn-success-form" type="button" title="<?php echo $this->lang->line('submit') ?>">
                                                <?php echo $this->lang->line('update') ?>
                                            </button>
                                        </div><!-- btn group -->      
                                    </div><!-- side fr -->      
                                </div><!-- footer -->  
                            </div>
                        </div>
                    </form>

                <?php
                }
            }
            ?><div class="content">    
                <div class="row-fluid scRow">  
                    <div class="span12 scCol">
                        <div class="block">

                            <div class="head">
                                <h2><?php echo $this->lang->line('listofinvoices') ?></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="listDetails" cellpadding="0" cellspacing="0" width="100%">
                                    <thead>
                                        <tr>
                                            <th>#</th>
                                            <th><?php echo $this->lang->line('invoiceno') ?></th>
                                            <th><?php echo $this->lang->line('invoicedate') ?></th>
                                            <th class="amount"><?php echo $this->lang->line('totalamount') ?></th>
                                            <th class="amount"><?php echo $this->lang->line('arbalance') ?></th>
                                            <th class="amount"><?php echo $this->lang->line('amountapplied') ?></th>
                                            <th class="center"><?php echo $this->lang->line('action') ?></th>
                                        </tr>
                                    </thead>
                                    <tbody id="bodyListPendingInvoices">
                                        <?php
                                        $lastRow = 0;
                                        $totalInvoice =0;
                                        $i = 0;
                                        if ($details) {
                                            foreach ($details as $row) {
                                                if ($row->lineNo > $lastRow)
                                                    $lastRow = $row->lineNo;
                                                    $totalInvoice += $row->amountApplied;
                                                ?> 
                                                <tr>
                                                    <td><?php echo ++$i; ?></td>
                                                    <td><?php echo $row->formNo ?></td>
                                                    <td><?php echo $row->salesInvoiceDate ?></td>
                                                    <td class="amount"><?php echo number_format($row->totalamount, 2) ?></td>
                                                    <td class="amount"><?php echo number_format($row->totalReceivable, 2) ?></td>
                                                    <td class="amount"><?php echo number_format($row->amountApplied, 2) ?></td>
                                                    <td class="center">
                                                        <!--button type="button" data-invoiceid="<?php echo $row->invoiceID ?>" data-myid="<?php echo $row->myid ?>" onclick="editRow(this)" class="btn-icon btn-mini btn-warning-form"><i class="i-pen"></i></button-->
                                                        <button type="button" data-invoiceid="<?php echo $row->invoiceID ?>" data-myid="<?php echo $row->myid ?>" onclick="deleteRow(this)" class="btn-icon btn-mini btn-danger-form"><i class="i-trashcan"></i></button>
                                                    </td>
                                                </tr>
                                            <?php
                                            }
                                        }
                                        ?>										
                                    </tbody>
                                </table>  
                                <div class="controls-row">
                                            <div id="msg_div2" class="alert alert-info">
                                                <h4><?php echo $this->lang->line('lastMessage') ?></h4>
                                                <p style="padding-top:10px;"><?php echo $this->lang->line('noMessage') ?></p>
                                            </div>
                                </div>
                            </div><!-- content -->
                        </div><!-- grid block 4-->
                    </div><!-- scCol -->
                </div><!-- row-fluid scRow-->
            </div>
          

                <div id="row_list_debitnote" class="row-fluid scRow">                            
                    <div class="span12 scCol">
                        <div class="block" >
                            <div class="head">
                                <h2><?php echo $this->lang->line('list_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><?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('amountapplied') ?></th>
                                            <th><?php echo $this->lang->line('action') ?></th>
                                        </tr>
                                    </thead>

                                    <tbody id="liste_debit_note_body">
                                         <?php
                                         $totalDebit = 0;
                                      if ($debitDetails) {
                
                                        $i = 0;
                                        foreach ($debitDetails as $row) {
                                            $totalDebit +=$row->amountApplied;
                                            ?>
                                            <tr>
                                                <td><?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->balance, 2); ?></td>
                                                <td class="amount"><?php echo number_format($row->amountApplied, 2); ?></td>
                                                <td class="center">
                                                    <!--button type="button" data-detail="<?php echo $row->ID ?>" data-myid="<?php echo $row->ID ?>" onclick="editRow(this)" class="btn-icon btn-mini btn-warning-form"><i class="i-pen"></i></button-->
                                                    <button type="button" data-detail="<?php echo $row->ID ?>" data-myid="<?php echo $row->ID ?>" data-debitNoteID="<?php echo $row->debitNoteID ?>" onclick="delete_debit(this)" class="btn-icon btn-mini btn-danger-form"><i class="i-trashcan"></i></button>
                                                </td> 
                                            </tr>
                                            <?php
                                        }
                                        }
                                        ?>
                                    </tbody>
                                </table>     
                            </div><!-- content -->
                        </div><!-- grid block 4-->
                    </div><!-- scCol -->
                </div><!-- row-fluid scRow-->


         
                <!--div id="row_list_creditnote" class="row-fluid scRow">                            
                    <div class="span12 scCol">
                        <div class="block" >
                            <div class="head">
                                <h2><?php echo $this->lang->line('list_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
                            <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('amountapplied') ?></th>
                                            <th><?php echo $this->lang->line('action') ?></th>
                                        </tr>
                                    </thead>
                                    <tbody id="liste_credit_note_body">
                                          <?php
                                          $totalCredit = 0;
                                    if ($creditDetails) {
                                       
                                        $i = 0;
                                        foreach ($creditDetails as $row) {
                                            $totalCredit += $row->amountApplied;
                                            ?>
                                            <tr>
                                                <td><?php echo ++$i; ?></td>
                                                <td><?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->balance, 2); ?></td>
                                                <td class="amount"><?php echo number_format($row->amountApplied, 2); ?></td>
                                                <td class="center">
                                                    <button type="button" data-detail="<?php echo $row->ID ?>" data-myid="<?php echo $row->ID ?>" onclick="editRow(this)" class="btn-icon btn-mini btn-warning-form"><i class="i-pen"></i></button>
                                                    <button type="button" data-detail="<?php echo $row->ID ?>" data-myid="<?php echo $row->ID ?>" data-creditNoteID="<?php echo $row->creditNoteID ?>" onclick="delete_credit(this)" class="btn-icon btn-mini btn-danger-form"><i class="i-trashcan"></i></button>
                                                </td>  

                                            </tr>
                                            <?php
                                        }
                                     }
                                        ?>
                                    </tbody>
                                </table>     
                            </div>
                        </div>
                    </div>
                </div -->
                
                <div id="row_list_creditnote" class="row-fluid scRow">                            
                    <div class="span12 scCol">
                        <div class="block" >
                            <div class="head">
                                <h2><?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="liste_histo" 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('amount_aplied') ?></th>
                                            <th><?php echo $this->lang->line('action') ?></th>
                                        </tr>
                                    </thead>
                                    <tbody id="liste_histo_body">
                                          <?php
                                          $totalHisto = 0;
                                    if ($histoDetails) {
                                       
                                        $i = 0;
                                        foreach ($histoDetails as $row) {
                                            $totalHisto += $row->amountApplied;
                                            ?>
                                            <tr>
                                                <td><?php echo ++$i; ?></td>
                                                <td><?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->totalReceivable, 2); ?></td>
                                                <td class="amount"><?php echo number_format($row->amountApplied, 2); ?></td>
                                                <td class="center">
                                                    <!--button type="button" data-detail="<?php echo $row->ID ?>" data-myid="<?php echo $row->ID ?>" onclick="editRow(this)" class="btn-icon btn-mini btn-warning-form"><i class="i-pen"></i></button-->
                                                    <button type="button" data-detail="<?php echo $row->ID ?>" data-myid="<?php echo $row->ID ?>" data-historicalinvoiceID="<?php echo $row->historicalinvoiceID ?>" onclick="delete_histo(this)" class="btn-icon btn-mini btn-danger-form"><i class="i-trashcan"></i></button>
                                                </td>  

                                            </tr>
                                            <?php
                                        }
                                     }
                                        ?>
                                    </tbody>
                                </table>     
                            </div><!-- content np -->
                        </div><!-- grid block 4-->
                    </div><!-- scCol -->
                </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('totalAmounts') ?></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('totalapplied') ?>Total Invoice Applied</div>
                                            <div class="span7">
                                                <input id="totalappliedInvoice" value="<?php echo number_format($totalInvoice,2); ?>" class="span12 amount validate[required min[1]]" type="text" disabled="disabled"  placeholder="0">
                                            </div>
                                        </div><!-- row control -->
                                        <div class="controls-row">
                                                    <div class="span5"><?php echo $this->lang->line('totalapplied') ?>Total Historical Invoice Applied</div>
                                                    <div class="span7">
                                                        <input id="totalappliedHistoInvoice" value="<?php echo number_format($totalHisto,2); ?>" class="span12 amount validate[required min[1]]" type="text" disabled="disabled"  placeholder="0">
                                                    </div>
                                                </div><!-- row control -->
                                        <div class="controls-row">
                                            <div class="span5"><?php echo $this->lang->line('totalapplied') ?>Total Debit Note Applied</div>
                                            <div class="span7">
                                                <input id="totalappliedDebit" value="<?php echo number_format($totalDebit,2); ?>" class="span12 amount validate[required min[1]]" type="text" disabled="disabled"  placeholder="0">
                                            </div>
                                        </div><!-- row control -->
                                        <div class="controls-row">
                                            <div class="span5"><?php echo $this->lang->line('totalapplied') ?>Total Credit Note Applied</div>
                                            <div class="span7">
                                                <input id="totalappliedCredit" value="<?php echo number_format($totalCredit,2); ?>" class="span12 amount validate[required min[1]]" type="text" disabled="disabled"  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" data-oldValue ="<?php echo $totalApplied; ?>" class="span12 amount validate[required]" value="<?php echo number_format($totalInvoice - $totalCredit + $totalDebit, 2); ?>" type="text" disabled="disabled"  placeholder="0">
                                </div>
                            </div><!-- row control -->
                            
                            <div class="controls-row">
                                <div class="span5"><?php echo $this->lang->line('bankcharges') ?></div>
                                <div class="span7">
                                    <input id="bankcharges" data-oldValue ="<?php echo $totalBankCharge; ?>"  value="<?php echo $totalBankCharge; ?>" class="span12 amount" type="text" onkeyup="fct_total_receive()" placeholder="0">
                                </div>
                            </div><!-- row control -->

                            <div class="controls-row">
                                <div class="span5"><?php echo $this->lang->line('totalreceived') ?></div>
                                <div class="span7">                                         
                                    <input data-lastRow="<?php echo $lastRow; ?>" onchange="updateTotalReceive()" id="totalreceived" class="span12 amount" data-oldValue ="<?php echo
                                    $totalReceive - $totalCredit + $totalDebit + $totalBankCharge; ?>" value="<?php echo number_format($totalInvoice - $totalCredit + $totalDebit + $totalBankCharge, 2); ?>" type="text" placeholder="0" disabled="disabled" >
                                </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">
                                    <a href="<?php echo base_url() ?>artransaction/receivedpaymentpost" class="btn-form btn-warning-form ">Confirm List</a>
                                    <a id="btnSave" onclick="addInvoices()" class="btn-form btn-success-form"><?php echo $this->lang->line('addinvoice') ?></a>
                                </div><!-- btn group -->        
                            </div><!-- side fr -->      
                        </div><!-- footer --> 
                    </div><!-- block-->
                </div><!-- span12-->	
            </div><!--row-fluid scRow -->
        </div> <!--content total -->	

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




