<?php

    if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
            case "transno":
                /* if(RetRstField("SELECT fnid FROM tlperiode WHERE ftjournal_no='{$_REQUEST['fttrans_no']}'")) {
                    ?>
                    alert('No. transaksi sudah dipakai...\n System akan mengenerate No. transaksi kembali ...\n Silahkan ulangi proses penyimpanan...  ');
                    OnChangeDate_fdtrans_date();
                    <?php
                } else { */
                    ?>
                    submitbutton('Simpan','','','','');
                    <?php
                // }
                break; 
        }
        die("");
    }

    Getsecurity('tutup_buku');

    $objAxpDB->ViewName("Daftar Tutup Buku");
    // $objAxpDB->ViewSQL("SELECT fnID, fdStart_Period, fdStart_Input, fdStart_Month_Period, fdEnd_Month_Period, CASE WHEN fbClosed = 0 THEN 'Tidak' ELSE 'Ya' END FROM tlperiode");
    $objAxpDB->ViewSQL("SELECT fnID, fdStart_Period, fdStart_Input, fdStart_Month_Period, fdEnd_Month_Period, CASE WHEN fbClosed = 0 THEN 'Tidak' ELSE 'Ya' END FROM tlperiode WHERE fbclosed = 1 ");
    $objAxpDB->ViewCaption("ID, Periode Awal, Input Awal, Awal Bulan, Akhir Bulan, Tutup Buku");
    
    if($_REQUEST['id']) {
            /*$strAutoNumber1="ajax_do('query2.php?code=2&ftmodule_code=acc002&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=ftjournal_no&no_trans=ftjournal_no&table_name=txgeneral_journal_hdr&ftbranch_code={$_SESSION['strbranch']}');";
            $ajaxTransDate="ajax_do('akunting.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=tgl&tgl=' + document.getElementById(\"fdtrans_date\").value + '');";
            $strAutoNumber0=" function OnChangeDate_fdtrans_date() {
                                           {$strAutoNumber1}
                                           {$ajaxTransDate}
                                     }

                              OnChangeDate_fdtrans_date();"; */
            // $objAxpDB->FormJS($strAutoNumber0);
            echo "<script>window.history.back();</script>";
            die();
    } 
    
    $objAxpDB->FormName("Tutup Buku");
    $objAxpDB->FormSQL("SELECT fdstart_period FROM tlperiode");
    $objAxpDB->FormCaption("Periode Awal");
    $objAxpDB->FormPosition("HD");
    $objAxpDB->FormLocked("1");
    
    $objAxpDB->GridName(0, "Periode Akunting Tutup Buku Terakhir");
    $objAxpDB->GridSQL(0, "SELECT fdstart_period, fdstart_input, fdstart_month_period, fdend_month_period FROM tlperiode WHERE fbclosed = 1 AND YEAR(fdstart_period) = ( SELECT YEAR(MAX(fdstart_period)) FROM tlperiode WHERE fbclosed = 1) #");
    $objAxpDB->GridCaption(0, "Periode Awal, Input Awal, Awal Bulan, Akhir Bulan");
    $objAxpDB->GridLocked(0, "1, 1, 1, 1");
    $objAxpDB->GridHidden(0, "1, 1, 0, 0");
    $objAxpDB->GridAutoAdd(0, false);
    
    $closing_date = RetRstField("SELECT fdstart_month_period FROM tlperiode WHERE fbclosed = 0 AND YEAR(fdstart_period) = ( SELECT YEAR(MAX(fdstart_period)) FROM tlperiode) ORDER BY fdstart_month_period LIMIT 1");
    if ($closing_date == '') {
        // new period
        $objAxpDB->GridSQL(1, "SELECT fdstart_period, fdstart_period AS fdstart_input, fdstart_period AS fdstart_month_period, LAST_DAY(fdstart_period) as fdend_month_period FROM tscompany_info #");
    } else {
        $objAxpDB->GridSQL(1, "SELECT fdstart_period, fdstart_input, fdstart_month_period, fdend_month_period FROM tlperiode WHERE fbclosed = 0 AND YEAR(fdstart_period) = ( SELECT YEAR(MAX(fdstart_period)) FROM tlperiode) ORDER BY fdstart_month_period LIMIT 1 #");
    }
    
    $objAxpDB->GridName(1, "Periode Akunting Akan Ditutup");
    $objAxpDB->GridCaption(1, "Periode Awal, Input Awal, Awal Bulan, Akhir Bulan");
    $objAxpDB->GridLocked(1, "1, 1, 1, 1");
    $objAxpDB->GridHidden(1, "1, 1, 0, 0");    
    $objAxpDB->GridAutoAdd(1, false);
    
    $objAxpDB->SetStatus("id,fbclosed");
    if ($objAxpDB->GetStatus("id") == "") {
            $objAxpDB->ButtonShow("baru");
    } elseif ($objAxpDB->GetStatus("id") == 0) {
            $objAxpDB->ButtonShow("simpan, tutup");
    } 
    
    $objAxpDB->Process();

    $objAxpDB->WriteHtmlTable();


