<?php
/* 	
 * Author   : Dhidhik / Tanggal : 12-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 id="west-panel" data-options="region:'west',split:true,border:true" class="noPrint" title="Filter" style="width:190px;background-color:#CCCCFF;padding:5px;">
        <form class="form-filter noPrint" method="post" name="formFilterPR" id="formFilterPR" >
            <table width="100%" cellpadding="0" cellspacing="0">
                <!-------copy company dari sini------------------------------------------------------------------------>
                <tr>
                    <td align="left" style="padding-bottom:8px;">
                        <b>by Company</b>
                    </td>
                </tr>
                <tr>
                    <td align="left">                        
                        <select name="cmb_company" id="cmb_company" 
                                class="easyui-combobox" 
                                style="width:174px;"
                                data-options="
                                value:'<?php echo $this->session->userdata('company'); ?>',
                                valueField:'CompanyCode',
                                textField:'CompanyName',
                                url:'<?php echo base_url(); ?>custom/get_data/company_find_master',
                                onSelect: function(rec){
                                var url = '<?php echo base_url(); ?>custom/get_data/branch_by_company_new/'+rec.CompanyCode;
                                $('#cmb_branch').combobox('reload', url);
                                $('#cmb_branch').combobox('setValue', '');
                                }
                                "
                                editable="true">
                        </select>
                    </td>
                </tr>
                <!---end COMPANY select--------------------------------------------------------------------- -->

                <!--ini separator-------------------------------------------->
                <tr><td colspan="2"><div class="tr-divider"></div></td></tr>
                <!--end-------------------------------------------------------->

                <!-------copy branch dari sini------------------------------------------------------------------------>
                <tr>
                    <td align="left" style="padding-bottom:8px;">
                        <b>by Branch/Depo</b>
                    </td>
                </tr>
                <tr>
                    <td align="left">
                        <select name="cmb_branch" id="cmb_branch" 
                                class="easyui-combobox" 
                                style="width:174px;"  
                                data-options="
                                value :'<?php echo $this->session->userdata('branch'); ?>',
                                valueField:'BranchCode', 
                                textField:'BranchName', 
                                url:'<?php echo base_url(); ?>custom/get_data/branch_by_company_new/<?php echo $this->session->userdata('company'); ?>'
                                " editable="true">
                        </select>
                    </td>
                </tr>
                <!------------end copy branch dari sini---------------------------------------------------------------->

                <!--ini separator-------------------------------------------->
                <tr><td colspan="2"><div class="tr-divider"></div></td></tr>
                <!--end-------------------------------------------------------->

                <!-----------------find by date------------------------------------------------------------------------>
                <tr>
                    <td style="padding-bottom:8px;">
                        <b>by Date</b>
                        <input type="checkbox" name="chk_ByDateNew" value="1" id="chk_ByDateNew" style="float:right;" checked disabled />
                    </td>
                </tr>
                <tr>
                    <td>
                        From
                        <span style="float:right;">
                            <input name="txt_DateFrom" id="txt_DateFrom" type="text" class="easyui-datebox " style="width:100px;height:22px;" editable="false" disabled />
                        </span>
                    </td>
                </tr>
                <tr>
                    <td>
                        To
                        <span style="float:right;">
                            <input name="txt_DateTo" id="txt_DateTo" type="text" class="easyui-datebox " style="width:100px;height:22px;" editable="false" disabled />
                        </span>
                    </td>
                </tr>
                <!----------------------------------------------end-------------------------------------------------------------->

                <!--ini separator-------------------------------------------->
                <tr><td colspan="2"><div class="tr-divider"></div></td></tr>
                <!--end-------------------------------------------------------->

                <!-------copy item dari sini------------------------------------------------------------------------>
                <tr>
                    <td align="left" style="padding-bottom:8px;">
                        <b>by Departement</b>
                    </td>
                </tr>
                <tr>                    
                    <td align="left">
                        <input id="txt_depart" name="txt_depart" placeholder="< Departement Name >" style="margin:2px 0px 3px;width:100%;height:18px;border:1px solid #CCC;">
                    </td>
                </tr>
                <!------------end copy item dari sini---------------------------------------------------------------->

                <!--ini separator-------------------------------------------->
                <tr><td colspan="2"><div class="tr-divider"></div></td></tr>
                <!--end-------------------------------------------------------->

                <!-------copy branch dari sini------------------------------------------------------------------------>
                <tr>
                    <td align="left" style="padding-bottom:8px;">
                        <b>Data in </b>
                    </td>
                </tr>
                <tr>
                    <td align="left">
                        <select name="cmb_collapse" id="cmb_collapse" 
                                class="easyui-combobox" 
                                style="width:174px;"  
                                editable="false">
                            <option value="collapseAll" selected > Collapse </option>
                            <option value="expandAll"> Expand </option>
                        </select>

                    </td>
                </tr>
                <!------------end copy branch dari sini---------------------------------------------------------------->

                <!--ini separator-------------------------------------------->
                <tr><td colspan="2"><div class="tr-divider"></div></td></tr>
                <!--end-------------------------------------------------------->

                <!----------------------------------ini by status---------------------------------------------------------->
<!--                <tr>
                    <td style="padding-bottom:8px;"><b>by Status Item</b></td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox" name="chk_TransStatus" value="1" style="margin-left:0px;" checked /> On Progress
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox" name="chk_TransStatus" value="2" style="margin-left:0px;"  checked/> Finished
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox" name="chk_TransStatus" value="3" style="margin-left:0px;"  /> Suspended
                    </td>
                </tr>-->

                <tr class="divTranstatus">
                    <td></td>
                </tr>

                <!------------------------------------------------------end by status-------------------------------------------------------------------------------------------->

                <!--copy dari sini untuk button reset dan search-->
                <tr>
                    <td height="38">
                        <div style="float:right;">
                            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-reload'" onClick="location.reload();" >Reset</a>
                            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onClick="searching_data_pr('', '');" >Search</a>
                        </div>
                    </td>
                </tr>
                <!--end copy dari sini untuk button reset dan search-------------------------->

            </table>
        </form>
    </div>

    <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">

        </div>	
    </div>

</div>

<script type="text/javascript">
    var shortingText = "";
    var rstTypeShorting = "ASC";
    var pre_url = base_url + 'report/index_purchase/pre_ajax_reloadDataPurchaseRequisitionsByDepartement';
    var url = base_url + 'report/index_purchase/ajax_reloadDataPurchaseRequisitionsByDepartement';
    var dataSend = {};
    var noTree = 1;
    var stringHTML = "";
    var pages = {limit: limitRowInPage(), pages: 1};
    var tempArSumm = [];
    function variableDefault() {
        shortingText = "";
        rstTypeShorting = "ASC";
        dataSend = {};
        noTree = 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;
    }
    function startLoad() {
        //========load tanggal=============//
        $('#txt_DateFrom').datebox('enable', true);
        $('#txt_DateTo').datebox('enable', true);

        if ($('#txt_DateFrom').datebox('getValue') == "" || $('#txt_DateFrom').datebox('getValue') == undefined) {
            $('#txt_DateFrom').datebox('setValue', '<?= date('01-m-Y') ?>');
            $('#txt_DateTo').datebox('setValue', 'today');
        }
        //========END load tanggal=============//        

        var searchParameter = {};

        if ($('#cmb_company').length > 0) {
            searchParameter['cmb_company'] = $('#cmb_company').combobox('getValue');
        }
        if ($('#cmb_branch').length > 0) {
            searchParameter['cmb_branch'] = $('#cmb_branch').combobox('getValue');
        }

        searchParameter['chk_TransStatus'] = new Array();
        $.each($('input[name="chk_TransStatus"]:checked'), function () {
            searchParameter['chk_TransStatus'].push($(this).val());
        });

        if ($('#txt_DateFrom').length > 0) {
            searchParameter['txt_DateFrom'] = $('#txt_DateFrom').datebox('getValue');
            searchParameter['txt_DateTo'] = $('#txt_DateTo').datebox('getValue');
        }

        return searchParameter;
    }
    $(document).ready(function () {
        //============= transtatus ========================//
        var listTrans = [
            {"status": "all", "checked": ""},
            {"status": "onprogress", "checked": "checked"},
            {"status": "finish", "checked": "checked"},
            {"status": "suspend", "checked": ""}
        ];//daftar Transtatus yang dipakai, value parameter nya liat di custom js
        toHTMLTranstatusReport("divTranstatus", listTrans);//untuk memanggil html Transtatus        
        //============= transtatus ========================//

        startLoad();//Load awal
        /*===chk_ByDateNew jangan lupa ganti ============*/
        $('#chk_ByDateNew').click(function () {
            if ($(this).is(':checked')) {
                $('#txt_DateFrom').datebox('enable', true);
                $('#txt_DateTo').datebox('enable', true);
                $('#txt_DateFrom').datebox('setValue', '<?= date('01-m-Y') ?>');
                $('#txt_DateTo').datebox('setValue', 'today');
            }
            else {
                $('#txt_DateFrom').datebox('disable', true);
                $('#txt_DateTo').datebox('disable', true);
                $('#txt_DateFrom').datebox('setValue', '');
                $('#txt_DateTo').datebox('setValue', '');
            }

        });
        /*==================end=========================*/
    });

    function searching_data_pr(txtTable, txtFieldShorting) {//(namaVariableTabelDiModel, namaField)
        variableDefault();
        var getValue = startLoad();
        var typeShorting = checkShorting(txtFieldShorting);
        var dataIn = $('#cmb_collapse').combobox('getValue');

        dataSend = {
            pages: pages,
            predata: [],
            cmb_company: getValue['cmb_company'] == undefined ? "" : getValue['cmb_company'],
            cmb_branch: getValue['cmb_branch'] == undefined ? "" : getValue['cmb_branch'],
            txt_depart: document.formFilterPR.txt_depart.value,
            txt_DateFrom: getValue['txt_DateFrom'],
            txt_DateTo: getValue['txt_DateTo'],
            chk_TransStatus: getValue['chk_TransStatus'],
            txtTable: txtTable,
            txtField: txtFieldShorting,
            typeShorting: typeShorting,
            dataIn: dataIn
        };
        if ($('#cmb_branch').combobox('getValue') == undefined) {
            $('#cmb_branch').combobox('clear');
        }
        if ($('#cmb_company').combobox('getValue') == undefined) {
            $('#cmb_company').combobox('clear');
            var strAlert = strMessageAlert_report('companyIsEmpty');
            messageAlertBiasa('Warning', strAlert, 'error');
        }
        else if (strDateIndToEng(dataSend['txt_DateFrom']) > strDateIndToEng(dataSend['txt_DateTo'])) {
            var strAlert = strMessageAlert_report('dateFrom>dateTo');
            messageAlertBiasa('Warning', strAlert, 'error');
        } else {
            modal_open('Progress Bar', 'dlg_loading');
            $.post(pre_url, dataSend).done(function (pre_result) {//preAjax
//                var stringHTML = "";   // untuk menyimpan yang akan ditampilkan ke layar
                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: " Purchase Requisitions By Departement ",
                    startdate: preResult.startdate,
                    enddate: preResult.enddate,
                    colspan: 11
                };
                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" />';css
        var css = '<link rel="stylesheet" type="text/css" href="<?php echo base_url() ?>assets/custom/css/treetable/jquery.treegrid.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(css);
        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='-webkit-print-color-adjust:exact;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='8'><hr></td>"
//            +"</tr>";
//        htmls = htmls.replace(delRow, " ");
        //console.log("htmls = "+htmls);
        exportToExcel(htmls);
    }

    function prepareReport(parseResult) {
        var tempDeptCode = "";
        var arTempDatas = [];
        // === LOOP PEMBACAAN DATA ===================================//
        parseResult.datas.forEach(function (dtResults) {
            if (tempDeptCode == "") {//PEMBACAAN DATA PERTAMA KALI
                tempDeptCode = dtResults.DepartmentCode_PR;
                var dataJSONLevel1_2 = arJSONLevel1_2(dtResults);
                arTempDatas.push(dataJSONLevel1_2);
            }
            else {//PEMBACAAN DATA UNTUK DATA SETERUSNYA
                var indexDeptCode = cekArraySummary(arTempDatas, "DepartmentCode_PR", dtResults.DepartmentCode_PR);//mendapatkan index array
                if (indexDeptCode > -1) {//jika DepartmentCode_PR ditemukan
                    var indexPRCode = cekArraySummary(arTempDatas[indexDeptCode]['data'], "PReqNumber_PR", dtResults.PReqNumber_PR);//mendapatkan index array
                    //console.log("PReqNumber_PR="+dtResults.PReqNumber_PR+" ,["+indexPRCode+"]");
                    if (indexPRCode > -1) {//jika PReqNumber_PR ditemukan
                        //console.log("dtResults.ItemQty_RIDet="+dtResults.ItemQty_RIDet);             

                        var indexPRDetCode = cekArraySummary(arTempDatas[indexDeptCode]['data'][indexPRCode]['data'], "PReqDetailID_PRDet", dtResults.PReqDetailID_PRDet);//mendapatkan index array
                        if (indexPRDetCode > -1) {//jika PReqDetailID_PRDet ditemukan
                            arTempDatas[indexDeptCode]['data'][indexPRCode]['data'][indexPRDetCode]['TotalQtyPR'] = (arTempDatas[indexDeptCode]['data'][indexPRCode]['data'][indexPRDetCode]['TotalQtyPR'] * 1) + (dtResults.ItemQty_PRDet * 1);//menambahkan PR qty
                            arTempDatas[indexDeptCode]['data'][indexPRCode]['data'][indexPRDetCode]['TotalQtyRI'] = (arTempDatas[indexDeptCode]['data'][indexPRCode]['data'][indexPRDetCode]['TotalQtyRI'] * 1) + (dtResults.ItemQty_RIDet * 1);//menambahkan PO qty

                            var indexPOCode = cekArraySummary(arTempDatas[indexDeptCode]['data'][indexPRCode]['data'][indexPRDetCode]['data'], "POrdNumber_PO", dtResults.POrdNumber_PO);//mendapatkan index  
                            if (indexPOCode > -1) {//jika POrdNumber_PO ditemukan

                                var indexPODetCode = cekArraySummary(arTempDatas[indexDeptCode]['data'][indexPRCode]['data'][indexPRDetCode]['data'][indexPOCode]['data'], "POrdDetailID_PODet", dtResults.POrdDetailID_PODet);//mendapatkan index
                                if (indexPODetCode > -1) {//jika indexPODetCode ditemukan
                                    arTempDatas[indexDeptCode]['data'][indexPRCode]['data'][indexPRDetCode]['TotalQtyPO'] = (arTempDatas[indexDeptCode]['data'][indexPRCode]['data'][indexPRDetCode]['TotalQtyPO'] * 1) + (dtResults.ItemQty_PODet * 1);//menambahkan PO qty

                                    var indexRICode = cekArraySummary(arTempDatas[indexDeptCode]['data'][indexPRCode]['data'][indexPRDetCode]['data'][indexPOCode]['data'][indexPODetCode]['data'], "PRecNumber_RI", dtResults.PRecNumber_RI);//mendapatkan index
                                    if (indexRICode > -1) {//jika PRecNumber_RI ditemukan
                                        arTempDatas[indexDeptCode]['data'][indexPRCode]['data'][indexPRDetCode]['TotalQtyPRSpd'] = (arTempDatas[indexDeptCode]['data'][indexPRCode]['data'][indexPRDetCode]['TotalQtyPRSpd'] * 1) + (dtResults.ItemQtySpd_PRDet * 1);//menambahkan PR spd qty
                                        arTempDatas[indexDeptCode]['data'][indexPRCode]['data'][indexPRDetCode]['TotalQtyPOSpd'] = (arTempDatas[indexDeptCode]['data'][indexPRCode]['data'][indexPRDetCode]['TotalQtyPOSpd'] * 1) + (dtResults.ItemQtySpd_PODet * 1);//menambahkan PO spd qty

                                        var indexRIDetCode = cekArraySummary(arTempDatas[indexDeptCode]['data'][indexPRCode]['data'][indexPRDetCode]['data'][indexPOCode]['data'][indexPODetCode]['data'][indexRICode]['data'], "PRecDetailID_RIDet", dtResults.PRecDetailID_RIDet);//mendapatkan index
                                        if (indexRIDetCode < 0) {
                                            //arTempDatas[indexDeptCode]['data'][indexPRCode]['TotalQtyRI'] = (arTempDatas[indexDeptCode]['data'][indexPRCode]['TotalQtyRI']*1) + (dtResults.ItemQty_RIDet*1);//menambahkan PO qty
                                            //console.log("dtResults.ItemQty_RIDet="+dtResults.ItemQty_RIDet);
                                            var dataJSONLevel7 = arJSONLevel7(dtResults);
                                            dataJSONLevel7 != null ? arTempDatas[indexDeptCode]['data'][indexPRCode]['data'][indexPRDetCode]['data'][indexPOCode]['data'][indexPODetCode]['data'][indexRICode]['data'].push(dataJSONLevel7) : "";
                                        }
                                    } else {
                                        var dataJSONLevel6 = arJSONLevel6(dtResults);
                                        dataJSONLevel6 != null ? arTempDatas[indexDeptCode]['data'][indexPRCode]['data'][indexPRDetCode]['data'][indexPOCode]['data'][indexPODetCode]['data'].push(dataJSONLevel6) : "";
                                    }
                                } else {
                                    var dataJSONLevel5 = arJSONLevel5(dtResults);
                                    dataJSONLevel5 != null ? arTempDatas[indexDeptCode]['data'][indexPRCode]['data'][indexPRDetCode]['data'][indexPOCode]['data'].push(dataJSONLevel5) : "";
                                }
                            } else {
                                var dataJSONLevel4 = arJSONLevel4(dtResults);
                                dataJSONLevel4 != null ? arTempDatas[indexDeptCode]['data'][indexPRCode]['data'][indexPRDetCode]['data'].push(dataJSONLevel4) : "";
                            }
                        } else {
                            var dataJSONLevel3 = arJSONLevel3(dtResults);
                            arTempDatas[indexDeptCode]['data'][indexPRCode]['data'].push(dataJSONLevel3);
                        }
                    } else {
                        var dataJSONLevel2 = arJSONLevel2(dtResults);
                        arTempDatas[indexDeptCode]['data'].push(dataJSONLevel2);
                    }
                } else {
                    var dataJSONLevel1_2 = arJSONLevel1_2(dtResults);
                    arTempDatas.push(dataJSONLevel1_2);
                }
            }//END PEMBACAAN DATA UNTUK DATA SETERUSNYA
        });
        // === 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
        var noUrutLevel1 = 1;
        arTempDatas.forEach(function (arDataLevel1) {//MULAI LOOP UNTUK DAPAT NAMA DEPARTEMENT
            var noUrutLevel2 = 1;
            tempStringHtml += tableHTMLHeader(arDataLevel1);//membuat tabel header

            arDataLevel1['data'].forEach(function (arDataLevel2) {//MULAI LOOP UNTUK DAPAT NAMA PR
                var noUrutLevel3 = 1;

                arDataLevel2['data'].forEach(function (arDataLevel3) {//MULAI LOOP UNTUK DAPAT NAMA PR detail
                    var noUrutLevel4 = 1;
                    var parentLevel3 = noTree;
                    noUrutLevel3 == 1 ? tempStringHtml += tableHTMLFooter(noUrutLevel2) : null;//membuat line per PR
                    tempStringHtml += tableHTMLContentFirst(noTree, noUrutLevel3, arDataLevel2, arDataLevel3);
                    noTree++;

                    arDataLevel3['data'].forEach(function (arDataLevel4) {//MULAI LOOP UNTUK DAPAT NAMA PO
                        var noUrutLevel5 = 1;
                        arDataLevel4['data'].forEach(function (arDataLevel5) {//MULAI LOOP UNTUK DAPAT NAMA PO detail
                            var noUrutLevel6 = 1;
                            var parentLevel5 = noTree;
                            tempStringHtml += tableHTMLContentOtherPO(noTree, parentLevel3, noUrutLevel5, arDataLevel4, arDataLevel5);
                            noTree++;

                            arDataLevel5['data'].forEach(function (arDataLevel6) {//MULAI LOOP UNTUK DAPAT NAMA RI
                                var noUrutLevel7 = 1;
                                arDataLevel6['data'].forEach(function (arDataLevel7) {//MULAI LOOP UNTUK DAPAT NAMA RI
                                    tempStringHtml += tableHTMLContentOtherRI(noTree, parentLevel5, noUrutLevel6, arDataLevel6, arDataLevel7);
                                    noTree++;
                                    noUrutLevel7++;
                                });
                                noUrutLevel6++;
                            });
                            noUrutLevel5++;
                        });
                        noUrutLevel4++;
                    });
                    noUrutLevel3++;
                });
                noUrutLevel2++;
            });
            noUrutLevel1++;
        });
        //END MULAI LOOP
//        tempStringHtml += tableHTMLFooterSummary(tempTotalFooter);//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);
//            dataSend['predata'] = preResult.datas;
            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 = "";
                $.post(url, dataSend).done(function (result) {//loadAjax
                    var parseResult = JSON.parse(result);
                    if (parseResult.datas != 0) {
                        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='11' class='s3' align='left'><b><u>_____</u></b></td></tr>"
                + "<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'>Item Code</span></td>"
                + "<td width='' class='s4 text-center'><span class='small_bold'>Unit</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'> Qty PR </span></td>"
                + "<td width='' class='s4 text-left'><span class='small_bold'> Qty PO </span></td>"
                + "<td width='' class='s4 text-left'><span class='small_bold'> Qty RI </span></td>"
                + "<td width='' class='s4 text-right'><span class='small_bold' > Qty PR Spd </span></td>"
                + "<td width='' class='s4 text-right'><span class='small_bold' > Qty PO Spd </span></td>"
                + "<td width='' class='s4 text-left'><span class='small_bold' >Status</span></td>"
                + "</tr>"
                + "<tr>"
                + "<td width='' class='s5 text-center' colspan='11' align='center'><span class='small_bold' >Data Not Found</span></td>"
                + "</tr></table>";
        return strHeader;
    }
    function tableHTMLHeader(data) {
        var strHeader = "<tr><td colspan='11'><br></td></tr><tr><td colspan='11'> </td></tr>"
                + "<tr><td colspan='7' class='s3' align='left'><a class='linkParentTab' href='#'onclick=\"parent.tab_edit('Departments','list/departement', 'icon-edit', 'edit', '" + data.DepartmentCode_PR + "')\" <?= $cek_autority_edit ?>><u><b>" + data.DepartmentDesc_PR + "<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=\"searching_data_pr('at_purchaserequisition','PReqDate');\"><span class='small_bold'>Date</span></td>"
                + "<td width='' class='s4 linkParentTab' style='cursor:pointer;' onclick=\"searching_data_pr('at_preqdetail','ItemCode');\"><span class='small_bold'>Item Code</span></td>"
                + "<td width='' class='s4 linkParentTab' style='cursor:pointer;' onclick=\"searching_data_pr('at_preqdetail','ItemUnit');\"><span class='small_bold'>Unit</span></td>"
                + "<td width='' class='s4 linkParentTab' style='cursor:pointer;' onclick=\"searching_data_pr('at_purchaserequisition','PReqNumber');\"><span class='small_bold'>No.Transaction</span></td>"
                + "<td width='' class='s4 linkParentTab' align='right' style='cursor:pointer;' onclick=\"searching_data_pr('at_preqdetail','ItemQty');\"><span class='small_bold'> Qty PR </span></td>"
                + "<td width='' class='s4 linkParentTab' align='right' style='cursor:pointer;' onclick=\"searching_data_pr('at_preqdetail','ItemQtyPO');\"><span class='small_bold'> Qty PO </span></td>"
                + "<td width='' class='s4 linkParentTab' align='right' style='cursor:pointer;' onclick=\"searching_data_pr('at_preqdetail','ItemQtyRI');\"><span class='small_bold' > Qty RI </span></td>"
                + "<td width='' class='s4 linkParentTab' align='right' style='cursor:pointer;' onclick=\"searching_data_pr('at_preqdetail','ItemQtySpd');\"><span class='small_bold' > Qty PR Spd </span></td>"
                + "<td width='' class='s4' align='right'><span class='small_bold' > Qty PO Spd </span></td>"
                + "<td width='' class='s4 linkParentTab' align='right' style='cursor:pointer;' onclick=\"searching_data_pr('at_purchaserequisition','TransStatus');\"><span class='small_bold' >Status</span></td>"
                + "</tr>";
        return strHeader;
    }
    function tableHTMLContentFirst(noTree, noUrut, data1, data2) {
        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' >" + data1.PReqDate_PR + "</span></td>"
                + "<td width='' class='s5><span class='small_bold' >" + data2.ItemCode_PRDet + "</span></td>"
                + "<td width='' class='s5'><span class='small_bold' >" + data2.ItemUnit_PRDet + "</span></td>"
                + "<td width='' class='s5'><span class='small_bold' ><a class='linkParentTab' href=\"#\" onclick=\"parent.tab_edit('Purchase Requisitons','list/purchases_requisition', 'icon-edit', 'edit', '" + data1.PReqNumber_PR + "')\" <?= $cek_autority_edit ?>>" + data1.PReqNumber_PR + "</a></span></td>"
                + "<td width='' class='s5' align='right'><span class='small_bold' >" + formatRp(data2.ItemQty_PRDet) + "</span></td>"
                + "<td width='' class='s5' align='right'><span class='small_bold' >" + formatRp(data2.ItemQtyPO_PRDet) + "</span></td>"//
                + "<td width='' class='s5' align='right'><span class='small_bold' >" + formatRp(data2.TotalQtyRI) + "</span></td>"//
                + "<td width='' class='s5' align='right'><span class='small_bold' >" + formatRp(data2.TotalQtyPRSpd) + "</span></td>"
                + "<td width='' class='s5' align='right'><span class='small_bold'>" + formatRp(data2.TotalQtyPOSpd) + "</span></td>"
                + "<td width='' class='s5' align='right'><span class='small_bold' >" + data1.TransStatus_PR + "</span></td>"
                + "</tr>";
        return strContent;
    }
    function tableHTMLContentOtherPO(noTree, noTreeParent, noUrut, data1, data2) {
        var strContent = "";
        //console.log("data2="+JSON.stringify(data2));
        if (data2.POrdDetailID_PODet != null && data2.POrdDetailID_PODet != "" && data2.POrdDetailID_PODet != undefined) {
            strContent = "<tr  class='treegrid-" + noTree + " treegrid-parent-" + noTreeParent + "' style='-webkit-print-color-adjust:exact;background-color:#EAFFFB;'>"
                    + "<td width='' class='s5'><span class='small_bold' >" + noUrut + "</span></td>"
                    + "<td width='' class='s5'><span class='small_bold'>" + data1.POrdDate_PO + "</span></td>"
                    + "<td width='' class='s5'><span class='small_bold'></span></td>"
                    + "<td width='' class='s5'><span class='small_bold' > </span></td>"
                    + "<td width='' class='s5'><span class='small_bold' ><a class='linkParentTab' href=\"#\" onclick=\"parent.tab_edit('Purchase Orders ','list/purchases_order', 'icon-edit', 'edit', '" + data1.POrdNumber_PO + "')\"  <?= $cek_autority_edit ?>>" + data1.POrdNumber_PO + "</a></span></td>"
                    + "<td width='' class='s5'><span class='small_bold' ></span></td>"
                    + "<td width='' class='s5' align='right'><span class='small_bold'>" + formatRp(data2.ItemQty_PODet) + "</span></td>"
                    + "<td width='' class='s5'><span class='small_bold' ></span></td>"
                    + "<td width='' class='s5'><span class='small_bold' ></span></td>"
                    + "<td width='' class='s5' align='right'><span class='small_bold'>" + formatRp(data2.ItemQtySpd_PODet) + "</span></td>"
                    + "<td width='' class='s5' align='right'><span class='small_bold' >" + data1.TransStatus_PO + "</span></td>"
                    + "</tr>";
        }
        return strContent;
    }
    function tableHTMLContentOtherRI(noTree, noTreeParent, noUrut, data1, data2) {
        var strContent = "";
        if (data2.PRecDetailID_RIDet != null && data2.PRecDetailID_RIDet != "" && data2.PRecDetailID_RIDet != undefined) {
            strContent = "<tr  class='treegrid-" + noTree + " treegrid-parent-" + noTreeParent + "' style='-webkit-print-color-adjust:exact;background-color:#e6ffcc;'>"
                    + "<td width='' class='s5'><span class='small_bold' >" + noUrut + "</span></td>"
                    + "<td width='' class='s5'><span class='small_bold'>" + data1.PRecDate_RI + "</span></td>"
                    + "<td width='' class='s5'><span class='small_bold'></span></td>"
                    + "<td width='' class='s5'><span class='small_bold' > </span></td>"
                    + "<td width='' class='s5'><span class='small_bold' ><a class='linkParentTab' href=\"#\" onclick=\"parent.tab_edit('Receive Items','list/receive_item', 'icon-edit', 'edit', '" + data1.PRecNumber_RI + "')\"  <?= $cek_autority_edit ?>>" + data1.PRecNumber_RI + "</a></span></td>"
                    + "<td width='' class='s5'><span class='small_bold' ></span></td>"
                    + "<td width='' class='s5'><span class='small_bold' > </span></td>"
                    + "<td width='' class='s5' align='right'><span class='small_bold'>" + formatRp(data2.ItemQty_RIDet) + "</span></td>"
                    + "<td width='' class='s5'><span class='small_bold' > </span></td>"
                    + "<td width='' class='s5'><span class='small_bold' > </span></td>"
                    + "<td width='' class='s5' align='right'><span class='small_bold' >" + data1.TransStatus_RI + "</span></td>"
                    + "</tr>";
        }
        return strContent;
    }
    function tableHTMLFooter(noUrut) {
        var strFooter = "<tr>"
                + "<td class='s5' align='right' colspan='11'><div style='float:left;'>" + (noUrut) + ". </div><hr class='hrTableSummary'></td></tr>";
        return strFooter;
    }
    function tableHTMLFooterSummary(data) {
        var strFooter = "<tr>"
                + "<td class='s5' align='right' colspan='11'><hr class='hrTableSummary' style='size:3px;'></td></tr>"
                + "<tr><td class='s5' align='right' colspan='7'><b>TOTAL : </b></td>"
                + "<td width='' class='s5' align='right'><span class='small_bold' ><b>" + formatRp(Q) + "</b></span></td>"
                + "<td width='' class='s5' align='right'><span class='small_bold' ><b>" + formatRp(QR) + "</b></span></td>"
                + "<td width='' class='s5' align='right'><span class='small_bold' ><b>" + formatRp(QBR) + "</b></span></td>"
                + "<td width='' class='s5' align='right'><span class='small_bold'><b></b></span></td>"
                + "<td width='' class='s5' align='right'><span class='small_bold'><b>" + formatRp(AR) + "</b></span></td>"
                + "<td width='' class='s5' align='right'><span class='small_bold'><b>" + formatRp(ARNotApp) + "</b></span></td>"
                + "<td width='' class='s5' align='right'><span class='small_bold'><b>" + formatRp(Amount) + "</b></span></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' >Nama Item</span></td>"
                + "<td width='' class='s4 ' align='left'><span class='small_bold' >Qty </span></td>"
                + "<td width='' class='s4 ' align='right'><span class='small_bold' >Qty Return</span></td>"
                + "<td width='' class='s4 ' align='right'><span class='small_bold' >Qty Return Non App</span></td>"
                + "<td width='' class='s4 ' align='right'><span class='small_bold' >Amount Return</span></td>"
                + "<td width='' class='s4 ' align='right'><span class='small_bold' >Amount Return NotApp</span></td>"
                + "<td width='' class='s4 ' align='right'><span class='small_bold' >Amount</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 arJSONLevel1_2(dtResults) {
        var tempData = {
            DepartmentCode_PR: dtResults.DepartmentCode_PR,
            DepartmentDesc_PR: dtResults.DepartmentDesc_PR,
            data: [{// data PR
                    BranchCode_PR: dtResults.BranchCode_PR,
                    PReqDate_PR: dateSqlToIndo(dtResults.PReqDate_PR),
                    PReqNumber_PR: dtResults.PReqNumber_PR,
                    TransStatus_PR: get_status(dtResults.TransStatus_PR),
                    data: [{// Data PR Detail
                            PReqDetailID_PRDet: dtResults.PReqDetailID_PRDet,
                            ItemCode_PRDet: dtResults.ItemCode_PRDet,
                            ItemDesc_PRDet: dtResults.ItemDesc_PRDet,
                            ItemUnit_PRDet: dtResults.ItemUnit_PRDet,
                            ItemQty_PRDet: dtResults.ItemQty_PRDet,
                            ItemQtyPO_PRDet: dtResults.ItemQtyPO_PRDet,
                            ItemQtyRI_PRDet: dtResults.ItemQtyRI_PRDet,
                            ItemQtySpd_PRDet: dtResults.ItemQtySpd_PRDet,
                            ItemSpdStatus_PRDet: dtResults.ItemSpdStatus_PRDet,
                            TotalQtyPR: dtResults.ItemQty_PRDet != null && dtResults.ItemQty_PRDet != "" && dtResults.ItemQty_PRDet != undefined ? dtResults.ItemQty_PRDet : 0,
                            TotalQtyPO: dtResults.ItemQty_PODet != null && dtResults.ItemQty_PODet != "" && dtResults.ItemQty_PODet != undefined ? dtResults.ItemQty_PODet : 0,
                            TotalQtyRI: dtResults.ItemQty_RIDet != null && dtResults.ItemQty_RIDet != "" && dtResults.ItemQty_RIDet != undefined ? dtResults.ItemQty_RIDet : 0,
                            TotalQtyPRSpd: dtResults.ItemQtySpd_PRDet != null && dtResults.ItemQtySpd_PRDet != "" && dtResults.ItemQtySpd_PRDet != undefined ? dtResults.ItemQtySpd_PRDet : 0,
                            TotalQtyPOSpd: dtResults.ItemQtySpd_PODet != null && dtResults.ItemQtySpd_PODet != "" && dtResults.ItemQtySpd_PODet != undefined ? dtResults.ItemQtySpd_PODet : 0,
                            data: [] // Data PO
                        }]
                }]
        };
        if (dtResults.POrdNumber_PO != null && dtResults.POrdNumber_PO != "" && dtResults.POrdNumber_PO != undefined) {
            var dataPO = {// Data PO
                POrdDate_PO: dateSqlToIndo(dtResults.POrdDate_PO),
                POrdNumber_PO: dtResults.POrdNumber_PO,
                TransStatus_PO: get_status(dtResults.TransStatus_PO),
                data: [{// Data PO Detail
                        POrdDetailID_PODet: dtResults.POrdDetailID_PODet,
                        ItemQty_PODet: dtResults.ItemQty_PODet,
                        ItemQtySpd_PODet: dtResults.ItemQtySpd_PODet,
                        data: [] // Data RI
                    }]
            };
            if (dtResults.PRecNumber_RI != null && dtResults.PRecNumber_RI != "" && dtResults.PRecNumber_RI != undefined) {
                var dataRI = {// Data RI
                    PRecDate_RI: dateSqlToIndo(dtResults.PRecDate_RI),
                    PRecNumber_RI: dtResults.PRecNumber_RI,
                    TransStatus_RI: get_status(dtResults.TransStatus_RI),
                    data: [{// Data RI Detail
                            PRecDetailID_RIDet: dtResults.PRecDetailID_RIDet,
                            ItemQty_RIDet: dtResults.ItemQty_RIDet
                        }]
                };
                dataPO['data'][0]['data'].push(dataRI);
            }
            tempData['data'][0]['data'][0]['data'].push(dataPO);
        }

        return tempData;
    }
    function arJSONLevel2(dtResults) {
        var tempData = {// data PR
            BranchCode_PR: dtResults.BranchCode_PR,
            PReqDate_PR: dateSqlToIndo(dtResults.PReqDate_PR),
            PReqNumber_PR: dtResults.PReqNumber_PR,
            TransStatus_PR: get_status(dtResults.TransStatus_PR),
            data: [{// Data PR Detail
                    PReqDetailID_PRDet: dtResults.PReqDetailID_PRDet,
                    ItemCode_PRDet: dtResults.ItemCode_PRDet,
                    ItemDesc_PRDet: dtResults.ItemDesc_PRDet,
                    ItemUnit_PRDet: dtResults.ItemUnit_PRDet,
                    ItemQty_PRDet: dtResults.ItemQty_PRDet,
                    ItemQtyPO_PRDet: dtResults.ItemQtyPO_PRDet,
                    ItemQtyRI_PRDet: dtResults.ItemQtyRI_PRDet,
                    ItemQtySpd_PRDet: dtResults.ItemQtySpd_PRDet,
                    ItemSpdStatus_PRDet: dtResults.ItemSpdStatus_PRDet,
                    TotalQtyPR: dtResults.ItemQty_PRDet != null && dtResults.ItemQty_PRDet != "" && dtResults.ItemQty_PRDet != undefined ? dtResults.ItemQty_PRDet : 0,
                    TotalQtyPO: dtResults.ItemQty_PODet != null && dtResults.ItemQty_PODet != "" && dtResults.ItemQty_PODet != undefined ? dtResults.ItemQty_PODet : 0,
                    TotalQtyRI: dtResults.ItemQty_RIDet != null && dtResults.ItemQty_RIDet != "" && dtResults.ItemQty_RIDet != undefined ? dtResults.ItemQty_RIDet : 0,
                    TotalQtyPRSpd: dtResults.ItemQtySpd_PRDet != null && dtResults.ItemQtySpd_PRDet != "" && dtResults.ItemQtySpd_PRDet != undefined ? dtResults.ItemQtySpd_PRDet : 0,
                    TotalQtyPOSpd: dtResults.ItemQtySpd_PODet != null && dtResults.ItemQtySpd_PODet != "" && dtResults.ItemQtySpd_PODet != undefined ? dtResults.ItemQtySpd_PODet : 0,
                    data: [] // Data PO
                }]
        };
        if (dtResults.POrdNumber_PO != null && dtResults.POrdNumber_PO != "" && dtResults.POrdNumber_PO != undefined) {
            var dataPO = {// Data PO
                POrdDate_PO: dateSqlToIndo(dtResults.POrdDate_PO),
                POrdNumber_PO: dtResults.POrdNumber_PO,
                TransStatus_PO: get_status(dtResults.TransStatus_PO),
                data: [{// Data PO Detail
                        POrdDetailID_PODet: dtResults.POrdDetailID_PODet,
                        ItemQty_PODet: dtResults.ItemQty_PODet,
                        ItemQtySpd_PODet: dtResults.ItemQtySpd_PODet,
                        data: [] // Data RI
                    }]
            };
            if (dtResults.PRecNumber_RI != null && dtResults.PRecNumber_RI != "" && dtResults.PRecNumber_RI != undefined) {
                var dataRI = {// Data RI
                    PRecDate_RI: dateSqlToIndo(dtResults.PRecDate_RI),
                    PRecNumber_RI: dtResults.PRecNumber_RI,
                    TransStatus_RI: get_status(dtResults.TransStatus_RI),
                    data: [{// Data RI Detail
                            PRecDetailID_RIDet: dtResults.PRecDetailID_RIDet,
                            ItemQty_RIDet: dtResults.ItemQty_RIDet
                        }]
                };
                dataPO['data'][0]['data'].push(dataRI);
            }
            tempData['data'][0]['data'].push(dataPO);
        }

        return tempData;
    }
    function arJSONLevel3(dtResults) {
        var tempData = {// Data PR Detail
            PReqDetailID_PRDet: dtResults.PReqDetailID_PRDet,
            ItemCode_PRDet: dtResults.ItemCode_PRDet,
            ItemDesc_PRDet: dtResults.ItemDesc_PRDet,
            ItemUnit_PRDet: dtResults.ItemUnit_PRDet,
            ItemQty_PRDet: dtResults.ItemQty_PRDet,
            ItemQtyPO_PRDet: dtResults.ItemQtyPO_PRDet,
            ItemQtyRI_PRDet: dtResults.ItemQtyRI_PRDet,
            ItemQtySpd_PRDet: dtResults.ItemQtySpd_PRDet,
            ItemSpdStatus_PRDet: dtResults.ItemSpdStatus_PRDet,
            TotalQtyPR: dtResults.ItemQty_PRDet != null && dtResults.ItemQty_PRDet != "" && dtResults.ItemQty_PRDet != undefined ? dtResults.ItemQty_PRDet : 0,
            TotalQtyPO: dtResults.ItemQty_PODet != null && dtResults.ItemQty_PODet != "" && dtResults.ItemQty_PODet != undefined ? dtResults.ItemQty_PODet : 0,
            TotalQtyRI: dtResults.ItemQty_RIDet != null && dtResults.ItemQty_RIDet != "" && dtResults.ItemQty_RIDet != undefined ? dtResults.ItemQty_RIDet : 0,
            TotalQtyPRSpd: dtResults.ItemQtySpd_PRDet != null && dtResults.ItemQtySpd_PRDet != "" && dtResults.ItemQtySpd_PRDet != undefined ? dtResults.ItemQtySpd_PRDet : 0,
            TotalQtyPOSpd: dtResults.ItemQtySpd_PODet != null && dtResults.ItemQtySpd_PODet != "" && dtResults.ItemQtySpd_PODet != undefined ? dtResults.ItemQtySpd_PODet : 0,
            data: [] // Data PO
        };
        if (dtResults.POrdNumber_PO != null && dtResults.POrdNumber_PO != "" && dtResults.POrdNumber_PO != undefined) {
            var dataPO = {// Data PO
                POrdDate_PO: dateSqlToIndo(dtResults.POrdDate_PO),
                POrdNumber_PO: dtResults.POrdNumber_PO,
                TransStatus_PO: get_status(dtResults.TransStatus_PO),
                data: [{// Data PO Detail
                        POrdDetailID_PODet: dtResults.POrdDetailID_PODet,
                        ItemQty_PODet: dtResults.ItemQty_PODet,
                        ItemQtySpd_PODet: dtResults.ItemQtySpd_PODet,
                        data: [] // Data RI
                    }]
            };
            if (dtResults.PRecNumber_RI != null && dtResults.PRecNumber_RI != "" && dtResults.PRecNumber_RI != undefined) {
                var dataRI = {// Data RI
                    PRecDate_RI: dateSqlToIndo(dtResults.PRecDate_RI),
                    PRecNumber_RI: dtResults.PRecNumber_RI,
                    TransStatus_RI: get_status(dtResults.TransStatus_RI),
                    data: [{// Data RI Detail
                            PRecDetailID_RIDet: dtResults.PRecDetailID_RIDet,
                            ItemQty_RIDet: dtResults.ItemQty_RIDet
                        }]
                };
                dataPO['data'][0]['data'].push(dataRI);
            }
            tempData['data'].push(dataPO);
        }

        return tempData;
    }
    function arJSONLevel4(dtResults) {
        var tempData = null;
        if (dtResults.POrdNumber_PO != null && dtResults.POrdNumber_PO != "" && dtResults.POrdNumber_PO != undefined) {
            var dataPO = {// Data PO
                POrdDate_PO: dateSqlToIndo(dtResults.POrdDate_PO),
                POrdNumber_PO: dtResults.POrdNumber_PO,
                TransStatus_PO: get_status(dtResults.TransStatus_PO),
                data: [{// Data PO Detail
                        POrdDetailID_PODet: dtResults.POrdDetailID_PODet,
                        ItemQty_PODet: dtResults.ItemQty_PODet,
                        ItemQtySpd_PODet: dtResults.ItemQtySpd_PODet,
                        data: [] // Data RI
                    }]
            };
            if (dtResults.PRecNumber_RI != null && dtResults.PRecNumber_RI != "" && dtResults.PRecNumber_RI != undefined) {
                var dataRI = {// Data RI
                    PRecDate_RI: dateSqlToIndo(dtResults.PRecDate_RI),
                    PRecNumber_RI: dtResults.PRecNumber_RI,
                    TransStatus_RI: get_status(dtResults.TransStatus_RI),
                    data: [{// Data RI Detail
                            PRecDetailID_RIDet: dtResults.PRecDetailID_RIDet,
                            ItemQty_RIDet: dtResults.ItemQty_RIDet
                        }]
                };
                dataPO['data'][0]['data'].push(dataRI);
            }
            tempData = dataPO;
        }
        return tempData;
    }
    function arJSONLevel5(dtResults) {
        var tempData = null;
        if (dtResults.PReqDetailID_PODet != null && dtResults.PReqDetailID_PODet != "" && dtResults.PReqDetailID_PODet != undefined) {
            var dataPODet = {// Data PO Detail
                POrdDetailID_PODet: dtResults.POrdDetailID_PODet,
                ItemQty_PODet: dtResults.ItemQty_PODet,
                ItemQtySpd_PODet: dtResults.ItemQtySpd_PODet,
                data: [] // Data RI
            };
            if (dtResults.PRecNumber_RI != null && dtResults.PRecNumber_RI != "" && dtResults.PRecNumber_RI != undefined) {
                var dataRI = {// Data RI
                    PRecDate_RI: dateSqlToIndo(dtResults.PRecDate_RI),
                    PRecNumber_RI: dtResults.PRecNumber_RI,
                    TransStatus_RI: get_status(dtResults.TransStatus_RI),
                    data: [{// Data RI Detail
                            PRecDetailID_RIDet: dtResults.PRecDetailID_RIDet,
                            ItemQty_RIDet: dtResults.ItemQty_RIDet
                        }]
                };
                dataPODet['data'].push(dataRI);
            }
            tempData = dataPODet;
        }
        return tempData;
    }
    function arJSONLevel6(dtResults) {
        var tempData = null;
        if (dtResults.PRecNumber_RI != null && dtResults.PRecNumber_RI != "" && dtResults.PRecNumber_RI != undefined) {
            var dataRI = {// Data RI
                PRecDate_RI: dateSqlToIndo(dtResults.PRecDate_RI),
                PRecNumber_RI: dtResults.PRecNumber_RI,
                TransStatus_RI: get_status(dtResults.TransStatus_RI),
                data: [{// Data RI Detail
                        PRecDetailID_RIDet: dtResults.PRecDetailID_RIDet,
                        ItemQty_RIDet: dtResults.ItemQty_RIDet
                    }]
            };
            tempData = dataRI;
        }
        return tempData;
    }
    function arJSONLevel7(dtResults) {
        var tempData = null;
        if (dtResults.PRecDetailID_RIDet != null && dtResults.PRecDetailID_RIDet != "" && dtResults.PRecDetailID_RIDet != undefined) {
            var dataRIDet = {// Data RI Detail
                PRecDetailID_RIDet: dtResults.PRecDetailID_RIDet,
                ItemQty_RIDet: dtResults.ItemQty_RIDet
            }
            tempData = dataRIDet;
        }
        return tempData;
    }
    function stringLegend() {
        var strLgnd = {
            marginLeft: '2%',
            datas: [
                {boxColor: '#F4F4FF', strLabel: "Data Purchase Requisitions"},
                {boxColor: '#EAFFFB', strLabel: "Data Purchase Order"},
                {boxColor: '#e6ffcc', strLabel: "Data Receipt Items"},
                {boxColor: '', strLabel: "'Date' : Tanggal Transaksi."},
                {boxColor: '', strLabel: "'Item Code' : Kode Item."},
                {boxColor: '', strLabel: "'Unit' : Satuan Item."},
                {boxColor: '', strLabel: "'No.Transaction' : Nomor Transaksi."},
                {boxColor: '', strLabel: "'Qty PR' : Jumlah Item dari Transaksi Purchase Requisition."},
                {boxColor: '', strLabel: "'Qty PO' : Jumlah Item dari Transaksi Purchase Order."},
                {boxColor: '', strLabel: "'Qty RI' : Jumlah Item dari Transaksi Receive Item."},
                {boxColor: '', strLabel: "'Qty PR Spd' : Jumlah Item dari Transaksi Purchase Requisition yang telah disuspend."},
                {boxColor: '', strLabel: "'Qty PO Spd' : Jumlah item dari Transaksi Purchase Order yang telah disuspend."},
                {boxColor: '', strLabel: "'Status' : Status Transaksi."}
            ]
        };
        var rst = legendsTable(strLgnd);
        return rst;
    }

</script>
