<?php
/* 	
 * Author   : Dhidhik / Tanggal : 24-11-2015	
 * note     : 
 * Revisi   : 
 */
//---------Untuk privellege Print and export excel ----------//
if (isset($group_user)) {
    if ($group_user->PrintPrivillage == 1) {
        $cek_autority_print = "";
    } else {
        $cek_autority_print = "disabled";
    }

    if ($group_user->ExcelPrivillage == 1) {
        $cek_autority_excel = "";
    } else {
        $cek_autority_excel = "disabled";
    }
    if ($group_user->EditPrivillage == 1) {
        $cek_autority_edit = "";
    } else {
        $cek_autority_edit = "disabled";
    }
} else {
    $cek_autority_print = "disabled";
    $cek_autority_excel = "disabled";
    $cek_autority_edit = "disabled";
}
//---------END Untuk privellege Print and export excel ----------//
?>
<!--<link rel="stylesheet" type="text/css" href="<?php echo base_url() ?>assets/custom/css/treetable/bootstrap.css">-->
<link rel="stylesheet" type="text/css" href="<?php echo base_url() ?>assets/custom/css/treetable/jquery.treegrid.css">
<script type="text/javascript" src="<?= base_url() ?>assets/custom/js/treetable/jquery.treegrid.bootstrap3.js"></script>
<script type="text/javascript" src="<?= base_url() ?>assets/custom/js/treetable/jquery.treegrid.js"></script>
<style type="text/css">

    .margin-left5persen{
        margin-left:5%; 
    }
    .margin-right5persen{
        margin-right:5%; 
    }
    @media print {
        .noPrint { display: none; visibility: hidden; }
        .displayPrint{
            display: inline-block;
            left: 0px;
            position: fixed;
            background-color: white;
            width: 82%;
        }
        .layout-panel-west{display: none; visibility: hidden; }
        table{
            width: 100%;
        }       
    }
    /* style sheet for "A4" printing */
    @media print and (width: 21cm) and (height: 29.7cm) {
        @page {
            margin: 3cm;
        }
    }

    /* style sheet for "letter" printing */
    @media print and (width: 8.5in) and (height: 11in) {
        @page {
            margin: 1in;
        }
    }

    /* A4 Landscape*/
    @page {
        size: A4 landscape;
    }
</style>
<div id="cc" class="easyui-layout" data-options="fit:true, border:false">

    <div data-options="region:'center',border:false,iconCls:'icon-ok'">
        <div style="padding:2px" id="form-header">
            <a href="#" class="easyui-linkbutton noPrint" data-options="plain:true,iconCls:'icon-print'" onClick="window.print_detail()" <?= $cek_autority_print ?>>Print</a>
            &nbsp;&nbsp;           
            <a href="#" class="easyui-linkbutton noPrint" data-options="plain:true,iconCls:'icon-excel'" onclick="actionExcel()" <?= $cek_autority_excel ?>>Export File </a>
            &nbsp;&nbsp;           
            <a href="#" class="easyui-linkbutton noPrint" data-options="plain:true,iconCls:'icon-reload'" onclick="location.reload();">Refresh</a>
        </div><br />
        <div id="databox" class="displayPrint">
<!--            uri code = <?php //echo $vendorCode;  ?><br>
            total Amount = <?php //echo number_format($totalAmount,2);  ?><br>-->
        </div>	
    </div>

</div>

<script type="text/javascript">
    var shortingText = "";
    var rstTypeShorting = "ASC";
    var pre_url = base_url + 'report/index_account_receivables/pre_ajax_detailPaymentSalesOutstanding';
    var url = base_url + 'report/index_account_receivables/ajax_detailPaymentSalesOutstanding';
    var dataSend = {};
    var noTree = 1;
    var noUrutLevel_1 = 1;
    var stringHTML = "";
    var pages = {limit: limitRowInPage(), pages: 1};
    var tempArSumm = [];
    function variableDefault() {
        shortingText = "";
        rstTypeShorting = "ASC";
        dataSend = {};
        noTree = 1;
        noUrutLevel_1 = 1;
        stringHTML = "";
        pages['pages'] = 1;
        tempArSumm = [];
    }

    function checkShorting(txtCheckShorting) {
        if (shortingText == txtCheckShorting) {
            rstTypeShorting == "DESC" ? rstTypeShorting = "ASC" : rstTypeShorting = "DESC";
        } else {
            rstTypeShorting = "ASC";
            shortingText = txtCheckShorting;
        }
        //alert("cek="+txtCheckShorting+"-"+rstTypeShorting);
        return rstTypeShorting;
    }
    $(document).ready(function () {
        search_detailReceipt_invoices_outstanding("", "");
    });

    function search_detailReceipt_invoices_outstanding(txtTable, txtFieldShorting) {//(namaVariableTabelDiModel, namaField)
//        var url          = base_url + 'report/index_account_receivables/ajax_detailPaymentSalesOutstanding';
        var typeShorting = checkShorting(txtFieldShorting);
        dataSend = {
            pages: pages,
            predata: [],
            cmb_company: "<?= $paramUrlCompanyCode; ?>",
            cmb_branch: "<?= $paramUrlBranchCode; ?>",
            txt_customer: "<?php echo $paramUrlCustomerCode; ?>",
            txt_DateTo: "<?= $paramUrlDateTo; ?>",
            txtTable: txtTable,
            txtField: txtFieldShorting,
            typeShorting: typeShorting,
            dataIn: "collapseAll"
        };
        modal_open('Progress Bar', 'dlg_loading');
        $.post(pre_url, dataSend).done(function (pre_result) {//preAjax
            var preResult = JSON.parse(pre_result);
            //Array Pre_result diambil code nya saja
            var dataTopHeader = {
                CompanyName: preResult.company.CompanyName,
                BranchName: preResult.company.BranchName == undefined || preResult.company.BranchName == "" ? "All Branch" : preResult.company.BranchName,
                reportLabel: " Customer's Invoices Receipt (On Progress) ",
                startdate: "<?= $paramUrlDateTo; ?>",
                enddate: preResult.enddate,
                colspan: 12
            };
            stringHTML += tableHTMLTopHeaderReport(dataTopHeader);//topHeader   

            if (preResult.datas != 0) {//jika pre ajax data ada 
                var pre_arData = [];
                preResult.datas.forEach(function (pre_dtResults) {//untuk mempersiapkan pre Array utk search
                    var pre_code = pre_dtResults['code'];
                    pre_arData.push(pre_code);
                });
                dataSend['predata'] = pre_arData;//isi preArray dengan value baru

                $.post(url, dataSend).done(function (result) {
                    var parseResult = JSON.parse(result);
                    var arTempDatas = [];   // temporer untuk menyimpan   

                    //=== JIKA DATA DITEMUKAN ========================================//
                    if (parseResult.datas != 0) {
                        //console.log("arTempDatas="+JSON.stringify(arTempDatas));
                        arTempDatas = prepareReport(parseResult);
                        stringHTML += createReport(arTempDatas);
                        pages['pages'] = (pages['pages'] * 1) + 1;
                    }//=== END JIKA DATA DITEMUKAN ===================================//
                    else {
                        stringHTML += tableHTMLNotFound();
                    }
                    //console.log("arTempDatas="+JSON.stringify(arTempDatas));
                    $("#databox").html(stringHTML);
                    modal_close('dlg_loading');
                }, 'json');
            } else {
                stringHTML += tableHTMLNotFound();
                $("#databox").html(stringHTML);
                modal_close('dlg_loading');
            }
        }, 'json');
    }

    function print_detail() {
        //var opt_print   = $("input[type='radio'][name='opt_print']:checked").val();
        var data_print = $("#databox").html();
        Popup(data_print);

    }
    function Popup(data) {
        var mywindow = window.open('', 'print-container', 'height=528,width=700');
        //var css = '<link rel="stylesheet" href="<?php echo base_url() ?>assets/custom/css/print.css" type="text/css" />';
        var varCSS = "<style>table{width: 100%;"
                + "font-family: 'Times New Roman';"
                + "font-size: small;"
                + "}"
                + ".thead{border:1px solid #333333;} .margin-left5persen{margin-left:0px;} .margin-right5persen{margin-right:0px;}"
                + ".table-summary{width:auto;}</style>";
        mywindow.document.write('<html><head><title>Print Detail</title>');
        mywindow.document.write(varCSS);
        mywindow.document.write('</head><body >');
        mywindow.document.write(data);
        mywindow.document.write('</body></html>');

        mywindow.document.close(); // necessary for IE >= 10
        mywindow.focus(); // necessary for IE >= 10

        mywindow.print();
        mywindow.close();

        return true;
    }
    function actionExcel() {
        var htmls = "";
        var delRow = "";
        htmls = $('#databox').html();
        htmls = htmls.replace(/<table border='0' /gi, "<table border='1' ");
        htmls = htmls.replace(/<table border="0" /gi, "<table border='1' ");
        htmls = htmls.replace(/border='0'/gi, " ");
        htmls = htmls.replace(/'s0 text-center'/gi, "'s0 text-center' style='font-size:7;text-align: center;font-weight: bold;' ");
        htmls = htmls.replace(/'s1 text-center'/gi, "'s1 text-center' style='font-size:9;text-align: center;font-weight: bold;' ");
        htmls = htmls.replace(/'s2'/gi, "'s2' style='text-align: center;font-weight: bold;' ");
        htmls = htmls.replace(/id='tableHeader'/gi, " style='background-color:#d5d5d5;' ");
        htmls = htmls.replace(/<script/gi, " ");
        delRow = ">$(document).ready(function () {    $('.tree-2').treegrid({        expanderExpandedClass: ' treegrid-expander-expanded ',        expanderCollapsedClass: 'treegrid-expander-collapsed'    });    $('.tree-2').treegrid('";
        htmls = htmls.replace(delRow, " ");
        delRow = "collapseAll');});</";
        htmls = htmls.replace(delRow, " ");
        delRow = "expandAll');});</";
        htmls = htmls.replace(delRow, " ");
        htmls = htmls.replace(/script>/gi, " ");
        delRow = '';
        htmls = htmls.replace(/display: none;/gi, " ");

//        delRow="<tr>"
//            +"<td class='s5' align='right' colspan='10'><hr></td>"
//            +"</tr>";
//        htmls = htmls.replace(delRow, " ");
        //console.log("htmls = "+htmls);
        exportToExcel(htmls);
    }

    function prepareReport(parseResult) {
        var tempCode = "";
        var arTempDatas = [];
        // === LOOP PEMBACAAN DATA ===================================//
        parseResult.datas.forEach(function (dtResults) {
            if (tempCode == "") {//PEMBACAAN DATA PERTAMA KALI INVOICE
                if (dtResults.CustomerCode != null && dtResults.CustomerCode != "" && dtResults.CustomerCode != undefined) {
                    tempCode = dtResults.Journal_Number;
                    var dataJSONLevel0 = arJSONLevel0(dtResults);
                    dataJSONLevel0 != 0 ? arTempDatas.push(dataJSONLevel0) : "";
                }
            }//END PEMBACAAN DATA PERTAMA KALI                    
            else {//PEMBACAAN DATA UNTUK DATA PAYMENT
                var indexCodeLevel_0 = cekArraySummary(arTempDatas, "CustomerCode", dtResults.CustomerCode);//mendapatkan index array VENDOR
                //console.log("indexCodeLevel_0="+indexCodeLevel_0);
                if (indexCodeLevel_0 > -1) {//jika VENDOR ditemukan data yg sama  
                    var indexCodeLevel_1 = cekArraySummary(arTempDatas[indexCodeLevel_0]['data'], "SInvNumber", dtResults.SInvNumber);
                    if (indexCodeLevel_1 > -1) {
                        var indexCodeLevel_2 = cekArraySummary(arTempDatas[indexCodeLevel_0]['data'][indexCodeLevel_1]['data'], "SRecNumber", dtResults.SRecNumber);
                        if (indexCodeLevel_2 > -1) {
                            if (dtResults.FiscalStatus == 0) {
                                arTempDatas[indexCodeLevel_0]['data'][indexCodeLevel_1]['TotalAllPayInv'] = (arTempDatas[indexCodeLevel_0]['data'][indexCodeLevel_1]['TotalAllPayInv'] * 1) + (dtResults.SRecTotalAmnt * 1);
                            }
                            else if (dtResults.FiscalStatus == 1) {
                                arTempDatas[indexCodeLevel_0]['data'][indexCodeLevel_1]['TotalAllPayFiscal'] = (arTempDatas[indexCodeLevel_0]['data'][indexCodeLevel_1]['TotalAllPayFiscal'] * 1) + (dtResults.SRecTotalAmnt * 1);
                            }
                            arTempDatas[indexCodeLevel_0]['data'][indexCodeLevel_1]['data'][indexCodeLevel_2]['SRecTotalAmnt'] = (arTempDatas[indexCodeLevel_0]['data'][indexCodeLevel_1]['data'][indexCodeLevel_2]['SRecTotalAmnt'] * 1) + (dtResults.SRecTotalAmnt * 1);
                        } else {
                            var dataJSONLevel2 = arJSONLevel2(dtResults);
                            if (dataJSONLevel2 != 0) {
                                if (dtResults.FiscalStatus == 0) {
                                    arTempDatas[indexCodeLevel_0]['data'][indexCodeLevel_1]['TotalAllPayInv'] = (arTempDatas[indexCodeLevel_0]['data'][indexCodeLevel_1]['TotalAllPayInv'] * 1) + (dtResults.SRecTotalAmnt * 1);
                                }
                                else if (dtResults.FiscalStatus == 1) {
                                    arTempDatas[indexCodeLevel_0]['data'][indexCodeLevel_1]['TotalAllPayFiscal'] = (arTempDatas[indexCodeLevel_0]['data'][indexCodeLevel_1]['TotalAllPayFiscal'] * 1) + (dtResults.SRecTotalAmnt * 1);
                                }
                                arTempDatas[indexCodeLevel_0]['data'][indexCodeLevel_1]['data'].push(dataJSONLevel2);
                            }
                        }
                    } else {
                        var dataJSONLevel1 = arJSONLevel1(dtResults);
                        dataJSONLevel1 != 0 ? arTempDatas[indexCodeLevel_0]['data'].push(dataJSONLevel1) : "";
                    }
                } else {
                    if (dtResults.CustomerCode != null && dtResults.CustomerCode != "" && dtResults.CustomerCode != undefined) {
                        var dataJSONLevel0 = arJSONLevel0(dtResults);
                        dataJSONLevel0 != 0 ? arTempDatas.push(dataJSONLevel0) : "";
                    }
                }
            }//end PEMBACAAN DATA UNTUK DATA PAYMENT

        });
        // === END LOOP PEMBACAAN DATA ============================/ 
        return arTempDatas;
    }
    function createReport(arTempDatas) {
        var tempStringHtml = "";
        tempStringHtml += createRowReport(arTempDatas);
        tempStringHtml += tableHTMLBlankForNext();
        tempStringHtml += "</table><script>"
                + "$(document).ready(function () {"
                + "    $('.tree-2').treegrid({"
                + "        expanderExpandedClass: ' treegrid-expander-expanded ',"
                + "        expanderCollapsedClass: 'treegrid-expander-collapsed'"
                + "    });"
                + "    $('.tree-2').treegrid('" + dataSend['dataIn'] + "');"
                + "});"
                + "</script" + ">";

//        tempStringHtml += summaryHTMLAll(" ALL ITEM ", "left", tempArSumm);
        tempStringHtml += stringLegend();
        return tempStringHtml;
    }
    function createRowReport(arTempDatas) {
        var tempStringHtml = "";
        //MULAI LOOP
        if (tempArSumm['footer'] == undefined || tempArSumm['footer'] != 'yes') {
            tempArSumm = {
                footer: "yes",
                tempTotalInvoices: 0,
                tempTotalPayInv: 0,
                tempTotalOutstandingInv: 0,
                tempTotalFiscal: 0,
                tempTotalPayFiscal: 0,
                tempTotalOutstandingFiscal: 0,
                CurrencySymbol: ""
            };
        }
        arTempDatas.forEach(function (arDataLevel_0) {//MULAI LOOP UNTUK DAPAT NAMA VENDOR
            var noContentItem = 1;
            pages['pages']<2?tempStringHtml += tableHTMLHeader(arDataLevel_0):null;//membuat tabel header
//            var noUrutLevel_1 = 1;
            arDataLevel_0['data'].forEach(function (arDataLevel_1) {//MULAI LOOP UNTUK DAPAT NAMA PURCHASE INVOICE
                var parentLevel_1 = noTree;
                tempStringHtml += tableHTMLContentFirst(noTree, noUrutLevel_1, arDataLevel_1);
                noTree++;
                //======= SUM UNTUK FOOTER TOTAL ALL =================//
                tempArSumm['CurrencySymbol'] = arDataLevel_1.CurrencySymbol;
                tempArSumm['tempTotalInvoices'] = (tempArSumm['tempTotalInvoices'] * 1) + (arDataLevel_1.SInvTotalAmount * 1);
                tempArSumm['tempTotalPayInv'] = (tempArSumm['tempTotalPayInv'] * 1) + (arDataLevel_1.TotalAllPayInv * 1);
                tempArSumm['tempTotalOutstandingInv'] = (tempArSumm['tempTotalOutstandingInv'] * 1) + ((arDataLevel_1.SInvTotalAmount * 1) - (arDataLevel_1.TotalAllPayInv * 1));
                tempArSumm['tempTotalFiscal'] = (tempArSumm['tempTotalFiscal'] * 1) + (arDataLevel_1.fiscalInvoice * 1);
                tempArSumm['tempTotalPayFiscal'] = (tempArSumm['tempTotalPayFiscal'] * 1) + (arDataLevel_1.TotalAllPayFiscal * 1);
                tempArSumm['tempTotalOutstandingFiscal'] = (tempArSumm['tempTotalOutstandingFiscal'] * 1) + ((arDataLevel_1.fiscalInvoice * 1) - (arDataLevel_1.TotalAllPayFiscal * 1));
                //======= SUM UNTUK FOOTER TOTAL ALL =================//

                var noUrutLevel2 = 1;
                arDataLevel_1['data'].forEach(function (arDataLevel_2) {
                    tempStringHtml += tableHTMLContentOther(noTree, parentLevel_1, noUrutLevel2, arDataLevel_2);
                    noUrutLevel2++;
                    noTree++;
                });

                noTree++;
                noUrutLevel_1++;
                noContentItem++;
            });//END MULAI LOOP UNTUK DAPAT NAMA PURCHASE INVOICE
        });
        //END MULAI LOOP
        tempStringHtml += tableHTMLFooterSummary(tempArSumm);//MEMBUAT FOOTER SUMARY 
        return tempStringHtml;
    }
    function seeMore() {
        modal_open('Progress Bar', 'dlg_loading');
        $.post(pre_url, dataSend).done(function (pre_result) {//preAjax
            var preResult = JSON.parse(pre_result);
            if (preResult.datas != 0) {//jika pre ajax data ada                 
                //Array Pre_result diambil code nya saja
                var pre_arData = [];
                preResult.datas.forEach(function (pre_dtResults) {
                    var pre_code = pre_dtResults['code'];
                    pre_arData.push(pre_code);
                });
                dataSend['predata'] = pre_arData;//isi preArray dengan value baru
                //End Array Pre_result diambil code nya saja

                var prearTempDatas = [];
                var pre_stringHTML = "";
                console.log("dataSend = "+JSON.stringify(dataSend));
                $.post(url, dataSend).done(function (result) {//loadAjax
                    var parseResult = JSON.parse(result);
                    if (parseResult.datas != 0) {
                        $("#footerSumAll_hr").replaceWith("");
                        $("#footerSumAll_row").replaceWith("");
                        prearTempDatas = prepareReport(parseResult);
                        pre_stringHTML += createRowReport(prearTempDatas);
                        pre_stringHTML += tableHTMLBlankForNext();

                        pages['pages'] = (pages['pages'] * 1) + 1;
                        $("#tr-see-more").replaceWith("");//clear row button 
                        $("#rowBlankForNext").replaceWith(pre_stringHTML);//mengganti row blank dengan ros data page baru
//                        $("#tableSumAll").replaceWith(summaryHTMLAll(" ALL ITEM ", "left", tempArSumm));
                        var htmlDataBox = $("#databox").html();//mengambil html pada div #databox yg data sudah baru
                        $("#databox").html(htmlDataBox);//mengisi kembali div #databox agar Tree-nya dapat work                      
                    } else {
                        $("#btn-see-more").prop("disabled", true);
                        $("#btn-see-more").val("Data Completed");
                    }
                    modal_close('dlg_loading');
                }, 'json');
            } else {
                $("#btn-see-more").prop("disabled", true);
                $("#btn-see-more").val("Data Completed");
                modal_close('dlg_loading');
            }
        }, 'json');
    }
    function tableHTMLNotFound() {
//        var strHeader = "<tr><td colspan='12' class='s3' align='left'><b><u>_____</u></b></td></tr>"
        var strHeader = "<tr id='tableHeader'>"//baris heading table
                + "<td width='' class='s4 text-left'><span class='small_bold' >No</span></td>"
                + "<td width='' class='s4 text-center'><span class='small_bold'>Date</span></td>"
                + "<td width='' class='s4 text-center'><span class='small_bold'>No.Transaction</span></td>"
                + "<td width='' class='s4 text-left'><span class='small_bold'>Amount</span></td>"
                + "<td width='' class='s4 text-left'><span class='small_bold'>Receipt</span></td>"
                + "<td width='' class='s4 text-left'><span class='small_bold'>Outstansind Inv</span></td>"
                + "<td width='' class='s4' align='right'><span class='small_bold'>Tax PPN 10%</span></td>"
                + "<td width='' class='s4' align='right'><span class='small_bold'>Pay Tax</span></td>"
                + "<td width='' class='s4' align='right'><span class='small_bold'>Outstanding Tax</span></td>"
                + "<td width='' class='s4 text-left'><span class='small_bold' >Status</span></td>"
                + "</tr>"
                + "<tr>"
                + "<td width='' class='s5 text-center' colspan='10' align='center'><span class='small_bold' >Data Not Found</span></td>"
                + "</tr></table>";
        return strHeader;
    }
    function tableHTMLHeader(data) {
        var strHeader = "<tr><td colspan='10'> </td></tr><tr><td colspan='10'> </td></tr>"
                + "<tr><td colspan='10' class='s3' align='left'><a class='linkParentTab' href='#'onclick=\"parent.tab_edit('Edit Customer','list/customers', 'icon-edit', 'edit', '" + data.CustomerCode + "')\" <?= $cek_autority_edit ?>><u><b>" + data.CustomerName + " -" + data.CustomerCode + " </b></u></a></td></tr>"
                + "<tr id='tableHeader'>"//baris heading table
                + "<td width='' class='s4'><span class='small_bold' >No</span></td>"
                + "<td width='80' class='s4 linkParentTab' style='cursor:pointer;' onclick=\"search_detailReceipt_invoices_outstanding('at_salesinvoice','SInvDate');\"><span class='small_bold'>Date</span></td>"
                + "<td width='' class='s4 linkParentTab' style='cursor:pointer;' onclick=\"search_detailReceipt_invoices_outstanding('at_salesinvoice','SInvNumber');\"><span class='small_bold'>No.Transaction</span></td>"
                + "<td width='' class='s4 linkParentTab' align='right' style='cursor:pointer;' onclick=\"search_detailReceipt_invoices_outstanding('at_salesinvoice','SInvTotalAmount');\"><span class='small_bold'>Amount</span></td>"
                + "<td width='' class='s4 linkParentTab' align='right' style='cursor:pointer;' onclick=\"search_detailReceipt_invoices_outstanding('at_srecdetail','SRecTotalAmnt');\"><span class='small_bold'>Receipt</span></td>"
                + "<td width='' class='s4' align='right' ><span class='small_bold'>Outstanding<br>Inv</span></td>"
                + "<td width='' class='s4' align='right'><span class='small_bold'>Tax PPN 10%</span></td>"
                + "<td width='' class='s4' align='right' ><span class='small_bold'>Pay Tax</span></td>"
                + "<td width='' class='s4' align='right' ><span class='small_bold'>Outstanding<br>Fiscal</span></td>"
                + "<td width='' class='s4 linkParentTab' align='right' style='cursor:pointer;' onclick=\"search_detailReceipt_invoices_outstanding('at_salesinvoice','TransStatus');\"><span class='small_bold' >Status</span></td>"
                + "</tr>";
        return strHeader;
    }
    function tableHTMLContentFirst(noTree, noUrut, data) {
        strContent = "";
        if (data.SInvNumber != null && data.SInvNumber != "" && data.SInvNumber != undefined) {
            var strContent = "<tr class='treegrid-" + noTree + "' style='-webkit-print-color-adjust:exact;background-color:#F4F4FF;font-weight: bold;'>"
                    + "<td width='' class='s5'><span class='small_bold' >" + noUrut + "</span></td>"
                    + "<td width='' class='s5'><span class='small_bold' >" + data.SInvDate + "</span></td>"
                    + "<td width='' class='s5'><span class='small_bold' ><a class='linkParentTab' href=\"#\" onclick=\"parent.tab_edit('Sales Invoices','list/sales_invoice', 'icon-edit', 'edit', '" + data.SInvNumber + "')\" <?= $cek_autority_edit ?>>" + data.SInvNumber + "</a></span></td>"
                    + "<td width='' class='s5' align='right'><span class='small_bold' >" + data.CurrencySymbol + " " + formatRp(data.SInvTotalAmount) + "</span></td>"
                    + "<td width='' class='s5' align='right'><span class='small_bold' >" + data.CurrencySymbol + " " + formatRp(data.TotalAllPayInv) + "</span></td>"
                    + "<td width='' class='s5' align='right'><span class='small_bold' >" + data.CurrencySymbol + " " + formatRp((data.SInvTotalAmount * 1) - (data.TotalAllPayInv)) + "</span></td>"
                    + "<td width='' class='s5' align='right'><span class='small_bold' >Rp. " + formatRp(data.fiscalInvoice) + "</span></td>"
                    + "<td width='' class='s5' align='right'><span class='small_bold' >Rp. " + formatRp(data.TotalAllPayFiscal) + "</span></td>"
                    + "<td width='' class='s5' align='right'><span class='small_bold' >Rp. " + formatRp((data.fiscalInvoice * 1) - (data.TotalAllPayFiscal)) + "</span></td>"
                    + "<td width='' class='s5' align='right'><span class='small_bold' >" + data.TransStatus + "</span></td>"
                    + "</tr>";
        }
        return strContent;
    }
    function tableHTMLContentOther(noTree, noTreeParent, noUrut, data) {
        var strContent = "";
        if (data.SRecNumber != null && data.SRecNumber != "" && data.SRecNumber != undefined) {
            strContent = "<tr  class='treegrid-" + noTree + " treegrid-parent-" + noTreeParent + "' style='-webkit-print-color-adjust:exact;background-color:#EAFFFB;'>"
                    + "<td width='' class='s5'><span class='' >" + noUrut + "</span></td>"
                    + "<td width='' class='s5'><span class='' ></span>" + data.SRecDate + "</td>"
                    + "<td width='' class='s5'><span class='' ><a class='linkParentTab' href=\"#\" onclick=\"parent.tab_edit('Edit Sales Receipt','list/sales_receipt', 'icon-edit', 'edit', '" + data.SRecNumber + "')\"  <?= $cek_autority_edit ?>>" + data.SRecNumber;
            if (data.SOrdNumber != null && data.SOrdNumber != "" && data.SOrdNumber != undefined) {
                strContent += " (DP)";
            } else {
                strContent += "";
            }
            strContent += "</a></span></td>"
                    + "<td width='' class='s5'><span class='' ></span></td>";//total amount
            data.FiscalStatus == 0 ? strContent += "<td width='' class='s5' align='right'><span class='' >" + data.CurrencySymbol + " " + formatRp(data.SRecTotalAmnt) + "</span></td>" : strContent += "<td width='' class='s5'><span class='' ></span></td>";//payment invoice
            strContent += "<td width='' class='s5'><span class='' ></span></td>"//outstanding Invoice
                    + "<td width='' class='s5'><span class='' ></span></td>";//total Fiscal
            data.FiscalStatus == 1 ? strContent += "<td width='' class='s5' align='right'><span class='' >Rp. " + formatRp(data.SRecTotalAmnt) + "</span></td>" : strContent += "<td width='' class='s5'><span class='' ></span></td>";//payment invoice
            strContent += "<td width='' class='s5'><span class='' ></span></td>"//outstanding Fiscal
                    + "<td width='' class='s5' align='right'><span class='' >" + data.TransStatus_Rec + "</span></td>"
                    + "</tr>";
        }
        return strContent;
    }
    function tableHTMLFooterSummary(arTempFooter) {
        var strFooter = "<tr id='footerSumAll_hr'>"
                + "<td class='s5' align='right' colspan='10'><hr class='hrTableSummary' style='size:3px;'></td></tr>"
                + "<tr id='footerSumAll_row'><td class='s5' align='right' colspan='3'><b>TOTAL : </b></td>"
                + "<td width='' class='s5' align='right'><span class='small_bold' ><b>" + arTempFooter['CurrencySymbol'] + " " + formatRp(arTempFooter['tempTotalInvoices']) + "</b></span></td>"
                + "<td width='' class='s5' align='right'><span class='small_bold' ><b>" + arTempFooter['CurrencySymbol'] + " " + formatRp(arTempFooter['tempTotalPayInv']) + "</b></span></td>"
                + "<td width='' class='s5' align='right'><span class='small_bold' ><b>" + arTempFooter['CurrencySymbol'] + " " + formatRp(arTempFooter['tempTotalOutstandingInv']) + "</b></span></td>"
                + "<td width='' class='s5' align='right'><span class='small_bold' ><b>Rp. " + formatRp(arTempFooter['tempTotalFiscal']) + "</b></span></td>"
                + "<td width='' class='s5' align='right'><span class='small_bold' ><b>Rp. " + formatRp(arTempFooter['tempTotalPayFiscal']) + "</b></span></td>"
                + "<td width='' class='s5' align='right'><span class='small_bold' ><b>Rp. " + formatRp(arTempFooter['tempTotalOutstandingFiscal']) + "</b></span></td>"
                + "</tr>";
        return strFooter;
    }
    function tableHTMLFooter() {
        var strFooter = "<tr>"
                + "<td class='' align='right' colspan='10'><hr class='hrTableSummary'></td></tr>";
        return strFooter;
    }
    function tableHTMLBlankForNext() {
        var strFooter = "<tr id='rowBlankForNext'>"
                + "<td class='s5' align='right' colspan='10'></td></tr>";
        strFooter += "<tr id='tr-see-more'><td colspan='5'><br><br><input id='btn-see-more' name='btn-see-more' type='button' onclick='seeMore()' value=' See More ' style='cursor:pointer;'></td></tr>";

        return strFooter;
    }
    function summaryHTMLAll(label, alignTo, datas) {
        //console.log('datas='+datas);
        var strHeader = "<table align='" + alignTo + "' cellpadding='5' cellspacing='0' class='margin-" + alignTo + "5persen table-summary' style='margin-bottom:80px;'>"
                + "<tr id='tableHeader'>"
                + "<td width='' class='s4 text-center' colspan='7' align='center'><span class='small_bold' >Summary <u>" + label + "</u></span></td>"
                + "</tr>"
                + "<tr id='tableHeader'>"
                + "<td width='' class='s4 ' align='left'><span class='small_bold' >No</span></td>"
                + "<td width='' class='s4 ' align='left'><span class='small_bold' >Customer</span></td>"
                + "<td width='' class='s4 ' align='left'><span class='small_bold' >Amount </span></td>"
                + "<td width='' class='s4 ' align='right'><span class='small_bold' >Receipt</span></td>"
                + "<td width='' class='s4 ' align='right'><span class='small_bold' >Outstanding Inv</span></td>"
                + "<td width='' class='s4 ' align='right'><span class='small_bold' >Tax PPN 10%</span></td>"
                + "<td width='' class='s4 ' align='right'><span class='small_bold' >Pay Tax</span></td>"
                + "<td width='' class='s4 ' align='right'><span class='small_bold' >Outstanding Fiscal</span></td>"
                + "<td width='' class='s4 ' align='right'><span class='small_bold' >Balances</span></td>"
                + "</tr>";
        var rowNo = 1;
        var q = 0, qr = 0, qbr = 0, ar = 0, arnotapp = 0, amount = 0;
        datas.forEach(function (data) {
            strHeader += "<tr>"
                    + "<td class='s5 text-left'><span class='small_bold' >" + rowNo + "</span></td>"
                    + "<td class='s5' align='left'><span class='small_bold' >" + data['itemDesc'] + "</span></td>"
                    + "<td class='s5' align='right'><span class='small_bold' >" + formatRp(data['TotalQ']) + "</span></td>"
                    + "<td class='s5' align='right'><span class='small_bold' >" + formatRp(data['TotalQR']) + "</span></td>"
                    + "<td class='s5' align='right'><span class='small_bold' >" + formatRp(data['TotalQBR']) + "</span></td>"
                    + "<td class='s5' align='right'><span class='small_bold' >" + formatRp(data['TotalAR']) + "</span></td>"
                    + "<td class='s5' align='right'><span class='small_bold' >" + formatRp(data['TotalARNotApp']) + "</span></td>"
                    + "<td class='s5' align='right'><span class='small_bold' >" + formatRp(data['TotalAmount']) + "</span></td>"
                    + "</tr>";
            q = (q * 1) + (data['TotalQ'] * 1);
            qr = (qr * 1) + (data['TotalQR'] * 1);
            qbr = (qbr * 1) + (data['TotalQBR'] * 1);
            ar = (ar * 1) + (data['TotalAR'] * 1);
            arnotapp = (ar * 1) + (data['TotalAR'] * 1);
            amount = (amount * 1) + (data['TotalAmount'] * 1);
            rowNo++;
        });
        strHeader += "<tr><td colspan='8'><hr></td></tr>"
                + "<tr>"
                + "<td class='s5' align='right' colspan='2'><span class='small_bold' >Total : </span></td>"
                + "<td class='s5' align='right'><span class='small_bold' >" + formatRp(q) + "</span></td>"
                + "<td class='s5' align='right'><span class='small_bold' >" + formatRp(qr) + "</span></td>"
                + "<td class='s5' align='right'><span class='small_bold' >" + formatRp(qbr) + "</span></td>"
                + "<td class='s5' align='right'><span class='small_bold' >" + formatRp(ar) + "</span></td>"
                + "<td class='s5' align='right'><span class='small_bold' >" + formatRp(arnotapp) + "</span></td>"
                + "<td class='s5' align='right'><span class='small_bold' >" + formatRp(amount) + "</span></td>"
                + "</tr>";

        strHeader += "</table>";
        return strHeader;
    }

    function arJSONLevel0(dtResults) {
        //console.log('dtResults = '+dtResults);
        var arData = 0;
        var tempData = {
            CustomerCode: dtResults.CustomerCode,
            CustomerName: dtResults.CustomerName,
            data: []
        };

        arData = arJSONLevel1(dtResults);
        tempData['data'].push(arData);
        if (arData == 0) {
            tempData = 0;
        }
        return tempData;
    }
    function arJSONLevel1(dtResults) {//Data Invoice
        var tempData = 0;
        if (dtResults.SInvNumber != null && dtResults.SInvNumber != "" && dtResults.SInvNumber != undefined) {
            tempData = {
                CurrencyCode: dtResults.CurrencyCode,
                SInvNumber: dtResults.SInvNumber,
                SInvDate: dateSqlToIndo(dtResults.SInvDate),
                SInvTotalAmount: dtResults.SInvTotalAmount,
                fiscalInvoice: dtResults.fiscalInvoice,
                TransStatus: get_status(dtResults.TransStatus),
                CurrencySymbol: dtResults.CurrencySymbol,
                TotalAllPayInv: 0,
                TotalAllPayFiscal: 0,
                data: []
            };

            if (dtResults.SRecNumber != null && dtResults.SRecNumber != "" && dtResults.SRecNumber != undefined) {
                var arData = arJSONLevel2(dtResults);
                if (arData != 0) {
                    if (dtResults.FiscalStatus == 0) {
                        tempData['TotalAllPayInv'] = (tempData['TotalAllPayInv'] * 1) + (dtResults.SRecTotalAmnt * 1);
                    }
                    else if (dtResults.FiscalStatus == 1) {
                        tempData['TotalAllPayFiscal'] = (tempData['TotalAllPayFiscal'] * 1) + (dtResults.SRecTotalAmnt * 1);
                    }
                    tempData['data'].push(arData);
                }
            }
        } else {
            tempData = 0;
        }
        return tempData;
    }
    function arJSONLevel2(dtResults) {//Data Invoice
        var tempData = 0;
        if ((dtResults.SRecNumber != null && dtResults.SRecNumber != "" && dtResults.SRecNumber != undefined)) {
            tempData = {
                SRecDate: dateSqlToIndo(dtResults.SRecDate),
                SRecNumber: dtResults.SRecNumber,
                SOrdNumber: dtResults.SOrdNumber,
                BankCurrCode: dtResults.BankCurrCode,
                SRecTotalAmnt: dtResults.SRecTotalAmnt,
                SRecCurrency: dtResults.SRecCurrency,
                SRecCurrRate: dtResults.SRecCurrRate,
                FiscalStatus: dtResults.FiscalStatus,
                CurrencySymbol: dtResults.CurrencySymbol,
                TransStatus_Rec: get_status(dtResults.TransStatus_Rec)
            };
        } else {
            //console.log("nooooooooolllllllllllll="+dtResults.SRecNumber);
            tempData = 0;
        }
        return tempData;
    }
    function stringLegend() {
        var strLgnd = {
            marginLeft: '2%',
            datas: [
                {boxColor: '#F4F4FF !important', strLabel: "Data Sales Invoices"},
                {boxColor: '#EAFFFB !important', strLabel: "Data Receipt"},
                {boxColor: '', strLabel: "'Date' : Tanggal Transaksi."},
                {boxColor: '', strLabel: "'No.Transaction' : Nomor Transaksi."},
                {boxColor: '', strLabel: "'Amount' : Jumlah Nominal dari Sales Invoice(tidak termasuk Tax)."},
                {boxColor: '', strLabel: "'Receipt' : Jumlah Nominal pembayaran untuk Sales Invoice."},
                {boxColor: '', strLabel: "'Outstanding Inv' : Jumlah Nominal kekurangan Sales Invoice yang belum dibayar."},
                {boxColor: '', strLabel: "'Tax PPN 10%' : Jumlah Nominal Pajak PPN 10%."},
                {boxColor: '', strLabel: "'Pay Tax' : Jumlah Nominal pembayaran untuk 'Tax PPN 10%'."},
                {boxColor: '', strLabel: "'Outstanding Fiscal' : Jumlah Nominal kekurangan 'Tax PPN 10%' yang belum dibayar."},
                {boxColor: '', strLabel: "'Status' : Status Transaksi."}
            ]
        };
        var rst = legendsTable(strLgnd);
        return rst;
    }
</script>
