
var decimal = Decimal;
var RadFlag = 0;

//function serialnoload() {
//    var srlno = {};
//    srlno.DeptId = ERPDeptId;

//    $.ajax({
//        type: "POST",
//        url: "../AccountsErp/VoucherNoGetandGets",
//        data: srlno,
//        success: function (result) {
//            getslno(result.oList);
//        }
//    });
//}
//function getslno(result) {
//    for (var k = 0; k < result.length; k++) {
//        $('#PDCNo').val(result[k].PDCNoIs);
//    }
//}

function isNumberInt(evt, selectedvalue) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    $(selectedvalue).val($(selectedvalue).val().replace(/[^0-9\.]/g, ''));
    if (charCode != 8 && charCode != 13 && (charCode < 48 || charCode > 57)) {
        evt.preventDefault();
        warningshow('Digits Only')
        return false;
    }
    return true;
}
$(document).ready(function () {
    
   
    $("#Account").focus();
    $("#TotalAmount").val('0.00');
    $("#TotalAmount1").val('0.00');


    $('#TransferDate,#ChequeDtFrom,#ChequeDtTo,#ChequeDtFrom1,#ChequeDtTo1').daterangepicker({
        minDate: new Date('1/1/2000'),
        maxDate: new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate()),
        singleDatePicker: true,
        showDropdowns: true,
        locale: {
            format: 'DD/MM/YYYY'
        }
    });
    var row1 = $('#txtRowCount').val();  
    for (var p = 1; p < row1; p++)
    {

    }


    $("#TransferDate").keydown(function (e) {
        var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
        if (key == 13) {
           
            e.preventDefault();
        }
    });
    $("#ChequeDtRadio").keydown(function (e) {
        var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
        if (key == 13) {
            $('#ChequeDtFrom').focus();
            e.preventDefault();
        }
    });
    
    $("#ChequeDtFrom").keydown(function (e) {
        var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
        if (key == 13) {
            $('#ChequeDtTo').focus();
            e.preventDefault();
        }
    });
    $("#ChequeDtTo").keydown(function (e) {
        var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
        if (key == 13) {
            $('#btngo').focus();
            e.preventDefault();
        }
    });
    $("#ChequeNoRadio").keydown(function (e) {
        var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
        if (key == 13) {
            $('#ChequeNo').focus();
            e.preventDefault();
        }
    });
    $("#ChequeDtFrom1").keydown(function (e) {
        var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
        if (key == 13) {
            $('#ChequeDtTo1').focus();
            e.preventDefault();
        }
    });
    $("#ChequeDtTo1").keydown(function (e) {
        var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
        if (key == 13) {
            $('#btnlist').focus();
            e.preventDefault();
        }
    });
    $('#ChequeDtRadio').click(function () {
        if ($('#ChequeDtRadio').is(':checked')) {
            $('#ChequeDtFrom').prop('disabled', false);
            $('#ChequeDtTo').prop('disabled', false);
            $('#ChequeDtFrom1').attr('disabled', true);
            $('#ChequeDtTo1').attr('disabled', true);
            //$("#ChequeNo").val('');
            $("#ChequeDtFrom1").val('');
            $("#ChequeDtTo1").val('');
             $("#ChequeDtFrom").val(CurDate);
            $("#ChequeDtTo").val(CurDate);
            $('#btnlist').hide();
            $('#btngo').show();
            $('#btnlist').attr('disabled', true);
            $('#btngo').prop('disabled', false);
            $('#filterr').hide();
            $('#DateCheck').hide();
        }
    });
    $('#ChequeNoRadio').click(function () {
        if ($('#ChequeNoRadio').is(':checked')) {
           
            $('#ChequeDtFrom1').attr('disabled', false);
            $('#ChequeDtTo1').attr('disabled', false);
            $('#ChequeDtFrom').prop('disabled', true);
            $('#ChequeDtTo').prop('disabled', true);
            $("#ChequeDtFrom").val('');
            $("#ChequeDtTo").val('');
            $('#btnlist').show();
            
            $('#filterr').show();
            $('#DateCheck').show();
            $('#btnlist').attr('disabled', false);
            $('#btngo').prop('disabled', true);
            $("#ChequeDtFrom1").val(CurDate);
            $("#ChequeDtTo1").val(CurDate);
        }
    });

    $('#filterr').hide();
    $('#DateCheck').hide();
    $('#ChequeNoRadio').attr(':checked', true);
    $('#btnlist').hide();
    $('#btngo').prop('disabled', false);
   
    
   
    $("#ChequeDtFrom").val(CurDate);
    $("#ChequeDtTo").val(CurDate);
    $('#ChequeDtFrom').prop('disabled', false);
    $('#ChequeDtTo').prop('disabled', false);

   


});
$(document).keydown(function (e) {
    if (e.altKey && e.keyCode == 83) {                        //Alt+S        
        SaveAndUpdate()
    }
    else if (e.altKey && e.keyCode == 82) {                 //Alt+R        
        window.open('../AccountsReport/AccountsReport')
    }
    else if (e.altKey && e.keyCode == 67) {                  //Alt+C
        formrefresh();
    }
});
function warningshow(message, Id) {
    $('#popupmessage').text(message);
    $('#Warningpopup').show();
    $('#' + Id).focus();
    window.setTimeout(function () {
        $('#Warningpopup').fadeOut();
    }, 3000);
}

function Showalerts(Status) {
    var no = $('#PDCNo').val();
    if (Status == 1) {
        formrefresh();
        swal('Reconciliation Done Successfully', "success");
        $('.swal-button swal-button--confirm').focus();
    }
    else {
        swal('Data Already Exists', "", "warning");
        $('.swal-button swal-button--confirm').focus();
    }
}

function GetList() {
    if ($('#Account').val() == "") {
        warningshow('Please select Account', 'Account');
    }
    else {
       


        var AccountId = $('#Account').val();
        var filter = 0;
        var ChequeDtFrom = $('#ChequeDtFrom').val();
        var ChequeDtTo = $('#ChequeDtTo').val();




        var data = {};
        data.AccountId = AccountId;
        data.filter = filter;
        data.ChequeDtFrom = ChequeDtFrom;
        data.ChequeDtTo = ChequeDtTo;


        $.ajax({
            type: "POST",
            url: "../../AccountsErp/ReconciliationGetandGets",
            data: data,
            success: function (result) {

                if (AccountId != 0)

                    console.log(result)
                ShowPDCList(result.oList);
            }
        });
    }
}
var balance = 0;
var obalance = 0;
var l = 0;
function ShowPDCList(result) {
   
    var a = result.length;
    if (a == 0) {
        warningshow('  Pending Reconciliation Not Found');
        formrefresh();
    }
    else {


        $('#RowGet').val = '';
        var responseText = "";
        for (var l = 0; l < result.length; l++) {
            var slno = parseInt(l + 1);

            responseText += '<tr id= ' + 'row' + slno + ' class="jsgrid-header-row"><td class= "jsgrid-cell jsgrid-align-center" style="width:2%"><input type="checkbox" id= ' + 'SlNoCheck' + slno + ' onclick=SumChange(' + slno + ')></td><td class= "jsgrid-cell jsgrid-align-center" style="width:46px">' + slno + '</td><td class= "jsgrid-cell jsgrid-align-center" style="width:100px" id=' + 'ChequeNo' + slno + '>' + result[l].ChequeNo + '</td><td class= "jsgrid-cell jsgrid-align-center" style="width:5%" id=' + 'ChequeDate' + slno + '>' + result[l].ChequeDate + '</td><td class= "jsgrid-cell jsgrid-align-center" style="width:150px" id=' + 'BankName' + slno + '>' + result[l].BankName + '</td><td class= "jsgrid-cell jsgrid-align-center" style="width:150px" id=' + 'BaseAmount' + slno + '>' + parseFloat(result[l].BaseAmount).toFixed(2) + '</td><td class= "jsgrid-cell jsgrid-align-center" style="display:none" id=' + 'AccCode' + slno + '>' + result[l].AccCode + '</td><td class= "jsgrid-cell jsgrid-align-center" style="width:150px" id=' + 'accname' + slno + '>' + result[l].AccName + '</td><td class= "jsgrid-cell jsgrid-align-center" style="width:150px;display:none" id=' + 'AccId' + slno + '>' + result[l].AccId + '</td><td class= "jsgrid-cell jsgrid-align-center" style="width:5%"  ><input type="text"  class="form-control" id=' + 'AccDescription' + slno + '></td><td class= "jsgrid-cell jsgrid-align-center" style="width:3%;" id=' + 'VoucherTypePrefix' + slno + '>' + result[l].VoucherTypePrefix + '</td><td class= "jsgrid-cell jsgrid-align-center" style="width:4%" id=' + 'VoucherNo' + slno + '>' + result[l].VoucherNo + '</td><td class= "jsgrid-cell jsgrid-align-center" style="width:4%" id=' + 'VoucherDate' + slno + '>' + result[l].VoucherDate + '</td><td class= "jsgrid-cell jsgrid-align-center" style="width:150px" id=' + 'Description' + slno + '>' + result[l].Description + '</td><td class= "recamountcss jsgrid-cell jsgrid-align-center" style="width:4%" id=' + 'RefNo' + slno + '>' + result[l].RefNo + '</td><td class= "jsgrid-cell jsgrid-align-center" style="display:none" id=' + 'ProjectJobId' + slno + '>' + result[l].ProjectJobId + '</td><td class= "jsgrid-cell jsgrid-align-center" style="display:none" id=' + 'CostCenterId' + slno + '>' + result[l].CostCenterId + '</td><td class= "jsgrid-cell jsgrid-align-center" style="display:none" id=' + 'CurrencyId' + slno + '>' + result[l].CurrencyId + '</td><td class= "jsgrid-cell jsgrid-align-center" style="display:none" id=' + 'CurrencyRate' + slno + '>' + result[l].CurrencyRate + '</td><td class= "jsgrid-cell jsgrid-align-center" style="display:none" id=' + 'FCAmount' + slno + '>' + result[l].FCAmount + '</td><td class= "jsgrid-cell jsgrid-align-center" style="display:none" id=' + 'BankId' + slno + '>' + result[l].BankId + '</td><td class= "jsgrid-cell jsgrid-align-center" style="display:none" id=' + 'VoEId' + slno + '>' + result[l].VEId + '</td><td class= "jsgrid-cell jsgrid-align-center" style="display:none" id=' + 'obal' + slno + '>' + result[l].obal + '</td><td class= "jsgrid-cell jsgrid-align-center" style="display:none" id=' + 'bal' + slno + '>' + result[l].bal + '</td><td class= "jsgrid-cell jsgrid-align-center" style="display:none" id=' + 'VTId' + slno + '>' + result[l].VTId + '</td></tr>';

            $("#balbook").val(result[l].bal.toFixed(decimal));
           
            $("#balbank").val(result[l].obal.toFixed(decimal));
            $("#opbank").val(result[l].obal.toFixed(decimal));
           
            }
           //  total = parseFloat($('#TotalAmount').val() || 0) + parseFloat($('#BaseAmount' + Id).text() || 0);
       

        $('#PDCReceived').append(responseText);
        $('#PDCReceived').html(responseText + '</tbody>');
        $('#RowGet').val(result.length)
        $('#txtRowCount').val(slno + 1);

        
        //$('#TotalAmount').val(total.toFixed(decimal));

        if (typeof slno == "undefined") {
            warningshow(' Pending Reconciliation Not Found');
        }
    }
}

function SaveAndUpdate() {
  
    if ($('#Account').val() == 0) {
        warningshow('Please select Account', 'Account');
    }
    else if ($('#BankAccount').val() == 0) {
        warningshow('Please select Account', 'BankAccount');
    }
    else if ($('#TransferDate').val() == "") {
        warningshow('Please select TransferDate', 'TransferDate');
    }
    else if ($('#txtRowCount').val() == 0) {
        warningshow('Pending Reconcilation Not Found', 'btngo');
    }

    else {
        var Count = parseInt($('#txtRowCount').val())
        var oArray = new Array();
        for (var i = 1; i < Count; i++) {
            if ($("#SlNoCheck" + i).is(":checked")) {


                DelFlag = 1;

                var BankDate = $('#AccDescription' + i).val();
                
                var TransferDate = $('#TransferDate').val();
                var VoEId = $('#VoEId' + i).text();


                oArray.push({

                    'DelFlag': DelFlag,
                    'BankDate': BankDate,
                    'TransferDate': TransferDate,
                    'VEId': VoEId,

                })



            }
        }


        if (oArray != "") {
            var data = { 'ReconciliationModel': oArray };
            $.ajax(
      {
          type: "POST",
          url: "../AccountsErp/ReconciliationInsert",
          data: data,
          success: function (result) {
              for (var i = 0; i <= result.oList.length; i++) {
                  var status = result.oList[i].Status;
                  Showalerts(status);
                  formrefresh();
                  //serialnoload();
                  // AccountLoad();
              }
          }
      });
        }
    }
}

function formrefresh() {

 
    $('#ChequeDtRadio').prop('checked', true);
    $('#btngo').prop('disabled', false);
    $('#ChequeDtFrom').prop('disabled', false);
    $('#ChequeDtTo').prop('disabled', false);
    $('#ChequeDtFrom1').attr('disabled', true);
    $('#ChequeDtTo1').attr('disabled', true);
    $("#ChequeDtFrom1").val('');
    $("#ChequeDtTo1").val('');
    $('#btnlist').hide();
    $('#filterr').hide();
    $('#DateCheck').hide();
    
    $("#Account").focus();
    $("#Account").val("");
    $("#BankAccount").val("");
    
    $("#TotalAmount").val('0.00');
    $("#TotalAmount1").val('0.00');
    $("#balbank").val('0.00');
    $("#balbook").val('0.00');

    $("#TransferDate").val(CurDate);
   
   
    $("#ChequeDtFrom").val(CurDate);
    $("#ChequeDtTo").val(CurDate);
   
    for (var k = 1; k < ($('#txtRowCount').val()) ; k++) {
        $('#row' + k).remove();
    }
    $('#txtRowCount').val(0);
}

function Clear() {
    if ($('#txtRowCount').val() == 0) {
        formrefresh();
    }
    else {
        var result = confirm("Do you want to Continue?")
        if (result == true)
            formrefresh();
    }
}
var bank = 0;
function SumChange(Id) {
    //alert(Id)
    
    balance = parseFloat($('#bal' + Id).text() || 0);
    $("#balbook").val(balance.toFixed(decimal));
   
   
   
    

 
    var total = 0;
    var total1 = 0;
    var VTId = $('#VTId' + Id).text();
    //alert(VTId);
    if (VTId == 'D') {

        if ($("#SlNoCheck" + Id).is(":checked")) {            
            total = parseFloat($('#TotalAmount').val() || 0) + parseFloat($('#BaseAmount' + Id).text() || 0);
            $('#AccDescription' + Id).val(CurDate); 

        }
        else {
             $('#AccDescription' + Id).val('');
             total = parseFloat($('#TotalAmount').val() || 0) - parseFloat($('#BaseAmount' + Id).text() || 0);
            
                


        }
        $('#TotalAmount').val(total.toFixed(decimal));
        
    }
    else {

        if ($("#SlNoCheck" + Id).is(":checked")) {           
            total = parseFloat($('#TotalAmount1').val() || 0) + parseFloat($('#BaseAmount' + Id).text() || 0);
            $('#AccDescription' + Id).val(CurDate);


        }
        else {
            $('#AccDescription' + Id).val('');
            total = parseFloat($('#TotalAmount1').val() || 0) - parseFloat($('#BaseAmount' + Id).text() || 0);
           
        }
        $('#TotalAmount1').val(total.toFixed(decimal));
       
    }
   
    bank = (parseFloat($('#TotalAmount').val() || 0) - parseFloat($('#TotalAmount1').val() || 0)) + (parseFloat($('#opbank').val() || 0));
    $('#balbank').val(bank.toFixed(decimal));
   // $('#opbank').val(0);


    var pp = parseFloat($('#TotalAmount').val() || 0);
    var ff = parseFloat($('#TotalAmount1').val() || 0);
    if(pp==0.00&&ff==0.00)
    {
       // $('#balbook').val('0.00');
      //  $('#balbank').val('0.00');
    }

   
  
 
   
    




}

function SelectAll() {
    var Cr = 0;
    var Dr=0;
  
        var row = $('#txtRowCount').val();       
        var flag = $("#SlNoHeadCheck").is(":checked");

        
        for (var i = 1; i <= row; i++) {

            document.getElementById("SlNoCheck" + i).checked = flag;

          
            $('#AccDescription' + i).val(CurDate);
            var VTId = $('#VTId' + i).text()

         
            if (VTId == 'D')
            {
                Dr += parseFloat($('#BaseAmount' + i).text());
            }
            else
            {
                Cr +=  parseFloat($('#BaseAmount' + i).text());
            }


            balance = parseFloat($('#bal' + i).text() || 0);
            $("#balbook").val(balance.toFixed(decimal));

           


           
            $('#TotalAmount').val(Dr.toFixed(decimal));
            $('#TotalAmount1').val(Cr.toFixed(decimal));
            bank = (parseFloat($('#TotalAmount').val() || 0) - parseFloat($('#TotalAmount1').val() || 0)) + parseFloat($('#opbank').val() || 0)
            $('#balbank').val(bank.toFixed(decimal));

            if (flag == false) {
                $('#TotalAmount').val('0.00');
                $('#TotalAmount1').val('0.00');
                $('#balbook').val('0.00');
                $('#balbank').val('0.00');
                $('#AccDescription' + i).val('');
            }

    

          

        }
}
function GetRows() {
    if ($('#Account').val() == "") {
        warningshow('Please select Account', 'Account');
    }
    else {
         if ($('#DateCheck').is(':checked')) {
            //alert("hai")
             var filter = 1;
             
         }
         else
         {
             var filter = 0;
         }
        var AccountId = $('#Account').val();
        
        var ChequeDtFrom = $('#ChequeDtFrom1').val();
        var ChequeDtTo = $('#ChequeDtTo1').val();
        var data = {};
        data.AccountId = AccountId;
        data.filter = filter;
        data.ChequeDtFrom = ChequeDtFrom;
        data.ChequeDtTo = ChequeDtTo;
        $.ajax({
            type: "POST",
            url: "../AccountsErp/ReconciliationlistandGets",
            data: data,
            success: function (result) {
                if (AccountId != 0)

                    console.log(result)
                ShowArealist(result.oList);
               


            }
        });
    }
}
function closetable(value) {
    $('#Entry').show();
    $('#listing').hide();
   // if (value == 1)
      //  formrefresh();
}



function ShowArealist(result) {
    var a1 = result.length;
    if (a1 == 0) {
        warningshow(' Reconciliation List Not Found');
        formrefresh();
    }
    else {
        // disable_datatable('tblBank');
        $('#Entry').hide();
        $('#listing').show();
        $('#btnnew').show();
        var responseText = "<thead><tr style=background-color:white;><th width=15px>Slno</th><th>cheque No</th><th>Cheque Date</th><th>Bank</th><th>Amount</th><th>Account Name</th><th>Voucher Type</th><th>Voucher No</th><th>Voucher Date</th><th>Description</th><th>Refer No</th></tr></thead><tbody>";
        for (var i = 0; i < result.length; i++) {
            var slno = parseInt(i + 1);


            if (result[i].Flag == '1') {

                responseText += '<tr style=background-color:powderblue><td>' + slno + '</td><td>' + result[i].ChequeNo + '</td><td>' + result[i].ChequeDate + '</td><td>' + result[i].BankName + '</td><td>' + parseFloat(result[i].BaseAmount).toFixed(2) + '</td><td>' + result[i].AccName + '</td><td>' + result[i].VoucherTypePrefix + '</td><td>' + result[i].VoucherNo + '</td><td>' + result[i].VoucherDate + '</td><td>' + result[i].Description + '</td><td>' + result[i].RefNo + '</td></tr>';

            }
            else {
                responseText += '<tr style=background-color:palevioletred><td>' + slno + '</td><td>' + result[i].ChequeNo + '</td><td>' + result[i].ChequeDate + '</td><td>' + result[i].BankName + '</td><td>' + parseFloat(result[i].BaseAmount).toFixed(2) + '</td><td>' + result[i].AccName + '</td><td>' + result[i].VoucherTypePrefix + '</td><td>' + result[i].VoucherNo + '</td><td>' + result[i].VoucherDate + '</td><td>' + result[i].Description + '</td><td>' + result[i].RefNo + '</td></tr>';

            }



        }
        $('#tblBank').html(responseText + '</tbody><tfoot><tr><th>Slno</th><th>cheque No</th><th>Cheque Date</th><th>Bank</th><th>Amount</th><th>Account Name</th><th>Voucher Type</th><th>Voucher No</th><th>Voucher Date</th><th>Description</th><th>Refer No</th></tr></tfoot>');
        datatableWithsearch('tblBank');
    }
}
   
