<style >
    .btn-group button{
        height:30px;}
    
    .amount {
        text-align: right !important;
    }



</style>
<script type="application/javascript">
   
   $(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(6).children("input:last-child").removeAttr("disabled");
              $(tr).find("td").eq(5).children("input:last-child").val("0");
              $(tr).find("td").eq(6).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(6).children("input:last-child").attr("disabled","disabled");
              $(tr).find("td").eq(5).children("input:last-child").val("");
              $(tr).find("td").eq(6).children("input:last-child").val("");
        }
        getTotalDiscountAndTotalApplied();
        updateOtherCharges();
    }
     
    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 totalDiscount = 0;
        var totalApplied = 0;
        $("#listDetails > tbody > tr").each (function(){
            var tr = $(this);
            var ck = $(tr).find("td").eq(7).children("input:last-child");
            if($(ck).is(':checked')){
                totalDiscount += accounting.unformat($(tr).find("td").eq(5).children("input:last-child").val())*1;
                totalApplied += accounting.unformat($(tr).find("td").eq(6).children("input:last-child").val())*1;
            }
        });
         if (isNaN(totalDiscount))
            totalDiscount =0;
        if (isNaN(totalApplied))
            totalApplied = 0;
        $("#totalapplied").val(accounting.formatNumber(totalApplied, 2));
        $("#totaldiscount").val(accounting.formatNumber(totalDiscount, 2));
        updateOtherCharges ();
    }
     
    
    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 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 savePayment(){
        if( $("#paymentForm").validationEngine('validate')){
            pDetails ='{"rs":[';
            firstime = true;
            $("#listDetails > 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 = ',' 
                    }
                    arBalance = accounting.unformat($(tr).find("td").eq(4).text())*1;
                    mntDiscount = accounting.unformat($(tr).find("td").eq(5).children("input:last-child").val())*1;
                    mntApplied = accounting.unformat($(tr).find("td").eq(6).children("input:last-child").val())*1;
                    pDetails += str + '{"invoiceID":"' + $(ck).attr("data-invoiceid") + '"' +
                                  ',"amountApplied":"' + mntApplied + '"' +
                                  ',"arBalance":"' + arBalance + '"' +
                                  ',"discount":"' + mntDiscount + '"}';
                }
            });
            pDetails += ']}';

            $.ajax({
                type: "POST",
                data: {
                  memo: $("#memo").val(),
                  customerID: $("#payer").val(),
                  currencyID: $("#payer option:selected").attr("data-currencyID"),
                  exchangeRate: $("#exchange_rate").val(),
                  projectID: $("#project").val(),
                  formNo: $("#formNo").val(),
                  receivePaymentDate: $("#createdDate").val(),
                  bankID: $("#bank").val(),
                  outOfBalance: accounting.unformat($("#outofbalance").val()),
                  amountReceived:  accounting.unformat($("#amountreceive").val()),
                  paymentMethodID: $("#paymentmethod").val(),
                  referenceNo: $("#refno").val(),
                  bankCharges:  accounting.unformat($("#bankcharges").val()),
                  totalDiscount:  accounting.unformat($("#totaldiscount").val()),
                  totalOtherCharge:  accounting.unformat($("#totalotherscharges").val()),
                  totalApplied:  accounting.unformat($("#totalapplied").val()),
                  pdetail : pDetails
                },
                url: "<?php echo base_url() ?>arTransaction/saveNewReceivePayment",
                success: function(data) {
                    if (data>0){ 
                        $("#btnSave").attr("class"," btn btn-primary disabled");
                        $("#msgDiv").attr("class","alert alert-success");
                        $("#msgDiv").html("Well Done !!! Payment Successfully created ");
                    } else {
                        $("#msgDiv").attr("class","alert alert-error");
                        $("#msgDiv").html("Oops !!! fail to insert data ");
                    }
                }
            });
        }
    }


</script>
<style>
    tbody td {
        font-size:11px;}
    </style>


<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="paymentForm" action="#">    
                <div class="row-fluid">
                    <div class="alert alert-info">
                        <strong><?php echo $this->lang->line('title') ?></strong>
                    </div>
                </div>
      	<div class="block">
      	<div class="content">
                <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('payer') ?></div>
                                    <div class="span8">
                                        <select class="span12 validate[required]" id="payer" onchange="loadPayerDetailsValue(this)">
                                            <option></option>
                                    <?php
                                        if ($customer) {
                                            foreach ($customer as $row) {
                                                ?>
                                                  <option data-currencyID="<?php echo $row->currencyID; ?>" value="<?php echo $row->ID; ?>"> <?php echo $row->customerName; ?></option>
                                            <?php }
                                    } ?>
                                        </select>
                                    </div>
                                </div> <!-- row -->

                                <div class="controls-row">
                                    <div class="span4"><?php echo $this->lang->line('currency') ?></div>
                                    <div class="span8">
                                        <input id="currency" name="currency" type="text" disabled="disabled" placeholder="Currency" class="span12 validate[required]">            
                                    </div><!-- span -->
                                </div><!-- control row -->
                                <div class="controls-row">
                                    <div class="span4"><?php echo $this->lang->line('exchangerate') ?></div>
                                    <div class="span8">
                                        <input class="span12 validate[required]" id="exchange_rate" name="exchange_rate" type="text" placeholder="Example: 3.2" />           
                                    </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="span10 validate[required]">
                                            <option></option>
                                            <?php
                                            if ($project != '') {
                                                foreach ($project as $row) {
                                                    ?>
                                                    <option value="<?php echo $row->ID; ?>"><?php echo $row->project_name; ?></option>
                                                    <?php }
                                                } ?>
                                        </select>
                                        <div class="btn-group span1">
                                            <button class="btn btn-mini btn-primary  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/new"  id="btnNewPro">
                                                        <i class="icon-plus-sign"></i><?php echo $this->lang->line('addnewproject') ?></a></li>
                                                <li><a href="#" onclick="refreshProject()"><i class="icon-refresh"></i><?php echo $this->lang->line('refreshlist') ?></a></li>
                                            </ul>
                                        </div><!-- btn group -->
                                    </div>
                                </div> <!-- row control -->
                                <div class="controls-row">
                                    <div class="span4"><?php echo $this->lang->line('formno') ?></div>
                                    <div class="span5">
                                        <input class="span12 validate[required]" id="formNo" name="formNo" value='<?php echo "$formNo"; ?>' style="color:red;"   type="text" placeholder="Form number ...">
                                    </div>
                                    <div class="span3">
                                        <input  class="span12"   type="text" placeholder="Serial" disabled="disabled">
                                    </div>
                                </div> <!-- row -->
                                <div class="controls-row">
                                    <div class="span4"><?php echo $this->lang->line('date') ?></div>
                                    <div class="span8">
                                        <div class="input-prepend span12">
                                            <span class="add-on span2"><i class="i-calendar"></i></span>
                                            <?php
                                            $data = array(
                                                'name' => 'createdDate',
                                                'id' => 'createdDate',
                                                'type' => 'text',
                                                'onkeydown' => 'return false',
                                                'class' => 'span10 datepicker validate[required]',);
                                            echo form_input($data, date(element('dtFormat', $this->session->userdata('logged_in'))));
                                            ?>
                                        </div>
                                    </div>
                                </div> <!-- row -->
                        </div><!-- 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 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" disabled="disabled" name="acctNo" type="text" placeholder="" 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 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="refno" name="refno" type="text" placeholder="Ref No" class="span12">            
                                    </div><!-- span -->
                                </div><!-- control row -->
                                
                                 <div class="controls-row">
                                    <div class="span4"><?php echo $this->lang->line('previewOutOfBalance') ?></div>
                                    <div class="span8">
                                        <input id="previewOutOfBalance" disabled="disabled" name="previewOutOfBalance" type="text" placeholder="0.00" class="span12 amount validate[required]">            
                                    </div><!-- span -->
                                </div><!-- control row -->
                                
                                <div class="controls-row">
                                    <div class="span4"><?php echo $this->lang->line('amountreceive') ?></div>
                                    <div class="span8">
                                        <input onkeyup="updateTotalReceive()" id="amountreceive" name="amountreceive" type="text" placeholder="0.00" class="span12 amount validate[required,min[1]]">            
                                    </div><!-- span -->
                                </div><!-- control row -->
                        </div><!-- grid block 1-->
                    </div><!-- scCol -->
                </div>
                </div>
                </div>
                <div class="row-fluid scRow">                            
                    <div class="span12 scCol">
                        <div class="block" id="grid_block_4">
                            <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('discount') ?></th>
                                            <th class="amount"><?php echo $this->lang->line('amountapplied') ?></th>
                                            <th><input onchange="selecOrUnselecttAll(this)" type="checkbox" /></th>
                                        </tr>
                                    </thead>
                                    <tbody id="bodyListPendingInvoices">
                                    </tbody>
                                </table>     
                            </div><!-- content -->
                        </div><!-- grid block 4-->
                    </div><!-- scCol -->
                </div><!-- row-fluid scRow-->

                <div class="row-fluid scRow">  

                    <div class="span4 scCol">
                        <div class="block" id="grid_block_4">
                            <div class="content">
                                <div class="controls-row">
                                    <textarea  id="adress" class="span12" placeholder="Adress"></textarea>
                                </div>
                                 <div class="controls-row">
                                    <textarea  id="memo" class="span12" placeholder="Memo"></textarea>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="span4 scCol">
                        <div class="block" id="grid_block_4">
                            <div class="content np">
                                <div class="controls-row">

                                    <div class="span5"><?php echo $this->lang->line('totaldiscount') ?></div>
                                    <div class="span7">
                                        <input id="totaldiscount"  class="span12 amount" type="text" placeholder="0" disabled="disabled" >
                                    </div>
                                </div><!-- row control -->
                                <div class="controls-row">
                                    <div class="span5"><?php echo $this->lang->line('bankcharges') ?></div>
                                    <div class="span7">
                                        <input id="bankcharges" class="span12 amount" type="text" onkeyup="updateOtherCharges()" placeholder="0">
                                    </div>
                                </div><!-- row control -->
                                <div class="controls-row">
                                    <div class="span5"><?php echo $this->lang->line('totalotherscharges') ?></div>
                                    <div class="span7">
                                        <input id="totalotherscharges" disabled="disabled"  class="span12 amount" type="text" placeholder="0">
                                    </div>
                                </div><!-- row control -->
                               
                                <div class="footer">
                                    <div class="side fr">
                                        <div class="btn-group"> </div><!-- btn group -->      
                                        </div><!-- side fr -->      
                                </div><!-- footer --> 
                            </div>    
                        </div><!-- grid block 4-->
                    </div><!-- scCol -->
                    
                    <div class="span4 scCol">
                        <div class="block" id="grid_block_4">
                            <div class="content np">
                                <div class="controls-row">

                                    <div class="span5"><?php echo $this->lang->line('totalreceived') ?></div>
                                    <div class="span7">
                                        <input onchange="updateTotalReceive()" id="totalreceived" class="span12 amount" type="text" placeholder="0" disabled="disabled" >
                                    </div>
                                </div><!-- row control -->
                                <div class="controls-row">
                                    <div class="span5"><?php echo $this->lang->line('totalapplied') ?></div>
                                    <div class="span7">
                                        <input id="totalapplied" 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('totalotherscharges') ?></div>
                                    <div class="span7">
                                        <input id="totalotherschargesCopy" disabled="disabled"  class="span12 amount" type="text" placeholder="0">
                                    </div>
                                </div><!-- row control -->
                                 <div class="controls-row">
                                    <div class="span5"><?php echo $this->lang->line('outofbalance') ?></div>
                                    <div class="span7">
                                        <input id="outofbalance" disabled="disabled"  class="span12 amount" type="text" placeholder="0">
                                    </div>
                                </div><!-- row control -->
                               
                            </div>    
                        </div><!-- grid block 4-->
                    </div><!-- scCol -->
                    
                </div><!-- row-fluid scRow-->
                
                
                
                <div class="row-fluid scRow">  
                    <div class="span8 scCol">
                        <div class="block" id="grid_block_4">
                            <div class="content">
                                <div style="padding:0;" class="controls-row">
                                    <div id="msgDiv" style="min-height: 22px;" class="alert alert-info">  </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="span4 scCol">
                        <div class="block" id="grid_block_4">
                            <div class="content">
                                <div class="controls-row side fr">
                                    <a href="<?php echo base_url() ?>artransaction/receivedpaymentpost/" class="btn ">Go to Conform Page</a>
                                    <a id="btnSave" onclick="savePayment()" class="btn btn-primary"><?php echo $this->lang->line('save') ?></a>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                </div><!-- row-fluid scRow-->
            </form>
        </div><!-- grid 1 -->
    </div><!-- wrap -->
</div><!-- content -->



