<?php
      if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
          case "penyusutan":
            list($dt, $mn, $yr)=split("/", $_REQUEST['fdtrans_date']);
            $periode=$yr . "-" . $mn;

            if(RetRstField("SELECT fnid FROM txdepriciation_hdr WHERE fttrans_code='{$_REQUEST['fttrans_code']}'")) {
                ?>
                  alert('Kode Transaksi sudah dipakai...');
                <?php
            } else if(RetRstField("SELECT fnid FROM txdepriciation_hdr WHERE LEFT(fdtrans_date, 7)='{$periode}' AND fnstatus=1")) {
                ?>
                  alert('Data sudah pernah diproses pada periode ini...');
                <?php
            } else {
                ?>
                  submitbutton('Simpan','','','','');
                <?php
              }
            break;
        }
    }

    function getAsset(){

      // jika hasil request sama dengan tanggal pertama kali proses / (session)
      // maka akan menggunakan tanggal yg pertama kali proses
      // jika tanggal tidak sama dengan session (tanggal pertama kali proses)
      // maka akan menggunakan tanggal pertama kali proses

      if ($_REQUEST['fdtrans_date'] == $_SESSION['fdtrans_date']) {

      list($dt, $mn, $yr)=split("/", $_REQUEST['fdtrans_date']);

      }else{

      list($dt, $mn, $yr)=split("/", $_SESSION['fdtrans_date']);

      }

      $ssql="SELECT a.ftasset_code,
                    a.ftasset_name,
                    a.fcacquistion_price,
                    a.fdpurchase_date,
                    b.fndepriciation_year,
                    b.ffpercentage,
                    b.fntype,
                    c.fttrans_status_name FROM tlasset a,
                    tlasset_category b
                    INNER JOIN tstransaction_status C ON b.fntype=c.fntrans_status_code AND fntrans_group_code = 78
                    WHERE a.ftcategory_code=b.ftcategory_code AND a.fnstatus = 1
                    AND ((YEAR(fdpurchase_date) = '{$yr}'
                    AND MONTH(fdpurchase_date) <= '{$mn}') OR (YEAR(fdpurchase_date) < '{$yr}'))
                    AND (a.fnselling_status = 0 OR (a.fnselling_status=1
                    AND ((YEAR(a.fdselling_date) = '{$yr}'
                    AND MONTH(a.fdselling_date) > '{$mn}') OR (YEAR(a.fdselling_date) > '{$yr}'))))
                    AND ROUND(a.fcacquistion_price)>ROUND((SELECT IFNULL(SUM(c.fcmonthly_amount), 0)
                    FROM txdepriciation_det c INNER JOIN
                    txdepriciation_hdr d
                    ON c.fttrans_code=d.fttrans_code
                    WHERE c.ftasset_code=a.ftasset_code AND d.fnstatus=1))
                    ORDER BY a.ftasset_code ";
                    $rs=mysql_query($ssql);

          $x.= "<BR>
                <table class='ewTable' id='TransactionDetail1' name='TransactionDetail1' border='0' width='100%' cellspacing='0' celpadding='0'>
                <tr class='ewTableHeader'>
                <td  valign='top'>No</th>
                <td  valign='top'>Kode Asset</th>
                <td  valign='top'>Harga Perolehan</th>
                <td  valign='top'>Penyusutan / Bln</th>
                <td  valign='top'>Penyusutan Awal</th>
                <td  valign='top'>Jenis Penyusutan</th>
                </tr>";

          $i=0;
          $j=0;
          while($r = mysql_fetch_assoc($rs)){
            $j++;

            if ($i%2) {
              $class1="ewTableRow"; $class2="ISGridTDeven";
            } else {
              $class1="ewTableRow"; $class2="ISGridTDodd";
            }

            $fcacquistion_price=$r["fcacquistion_price"];
            $fdpurchase_date=$r["fdpurchase_date"];
            $fdtrans_date=$_REQUEST["fdtrans_date"];

            if($r["fcacquistion_price"] > 0){

              if ($r["fntype"] == '1') {

              $fcmonthly_amount = round($r["fcacquistion_price"] / (12 * $r["fndepriciation_year"]),2);

            }else if($r["fntype"] == '2'){

              $fcmonthly_amount= $r["fcacquistion_price"] * $r["ffpercentage"] / 100;

            }

            }else{

              $fcmonthly_amount='0';

            }

            // $fcmonthly_amount = ceil($fcmonthly_amount);

            $ssql="SELECT  IFNULL(a.fcbalance_amount,0) AS fcbalance_amount FROM txdepriciation_det a, txdepriciation_hdr b WHERE
                 a.ftasset_code = '{$r["ftasset_code"]}'  AND a.fttrans_code=b.fttrans_code AND b.fnstatus=1 ORDER BY a.`fnid` DESC LIMIT 1";

            $mysql=mysql_query($ssql);
            $r2=mysql_fetch_assoc($mysql);

            if ($r2["fcbalance_amount"] == 0) {

              $fcbalance_amount = $r["fcacquistion_price"];


            }else{

              $fcbalance_amount = $r2["fcbalance_amount"];

            }

            $x.= "<tr class='$class1'>
                  <td class='$class2'>
                  <input readonly style='background-color: #E0E0E0'  type='text' name='fnindexD0[]' id='fnindexD0$i' value='{$j}' size='3'></td>
                  <td class='$class2'>
                  <input readonly style='background-color: #E0E0E0'  type='text' name='ftasset_codeD0[]' id='ftasset_codeD0$i' value='" . $r['ftasset_code'] . "' size=20>
                  <span style='color: #FF0000'> - </span><input readonly style='background: none; border:none;'  type='text' name='ftasset_nameD0[]' id='ftasset_nameD0$i' value='" . $r['ftasset_name'] . "' size=20></td>
                  <td class='$class2'>
                  <input onblur=\"ElementCurrencyOnBlur(this);\" onfocus=\"ElementCurrencyOnBlur(this); this.select();\" style=\"text-align: center; background-color: #E0E0E0\"  readonly type='text' name='fcacquistion_priceD0[]' id='fcacquistion_priceD0$i' value='" . number_format($fcacquistion_price,2) . "'   size=20></td>
                  <td class='$class2'>
                  <input onblur=\"ElementCurrencyOnBlur(this);\" onfocus=\"ElementCurrencyOnBlur(this); this.select();\" style=\"text-align: center; background-color: #E0E0E0\" readonly type='text' name='fcbalance_amountD0[]' id='fcbalance_amountD0$i' value='" .  number_format($fcmonthly_amount,2) . "'   size=20></td>
                  <td class='$class2'>
                  <input  style=\"text-align: center; background-color: #DAF7F8 \" onblur=\"ElementCurrencyOnBlur(this);\"  type='text' name='fcmonthly_amountD0[]' id='fcmonthly_amountD0$i'  value=''   size=20 autofocus></td>
                  <td class='$class2'>
                  <input readonly style=\"text-align: center; background-color: #E0E0E0 \" onblur=\"ElementCurrencyOnBlur(this);\"  type='text' name='fntypeD0[]' id='fntypeD0$i' value='" . $r['fttrans_status_name'] . "'size=15></td>
                  </tr>";

            $i++;
          }

            $x.= "</table>
                       <input id='TransactionDetailCount1' name='TransactionDetailCount1' value='$num_rows' type='hidden'>";

        return $x;
      }

    Getsecurity('saldo_awal_penyusutan');


    $objAxpDB->ViewName("Daftar Saldo Awal Penyusutan Asset");
    $objAxpDB->ViewSQL("SELECT a.fnid, a.fttrans_code, CONVERT(DATE_FORMAT(a.fdtrans_date, '%M %Y'), CHAR(35)) AS fdperiode, c.fttrans_status_name FROM
                        txdepriciation_hdr a, txdepriciation_det b, tstransaction_status c
                        WHERE a.fttrans_code=b.fttrans_code AND a.fnstatus=c.fntrans_status_code
                        AND c.fntrans_group_code = 2 AND a.fnsaldo_awal = 0
                        GROUP BY a.fttrans_code");
    $objAxpDB->ViewCaption("No., Kode Transaksi, Periode, Status");

    //$objAxpDB->ViewPeriod(TRUE);

    $objAxpDB->FormName("Saldo Awal Penyusutan Asset");
    $objAxpDB->FormSQL("SELECT fttrans_code, fdtrans_date, fnstatus, fnsaldo_awal FROM txdepriciation_hdr");
    $objAxpDB->FormCaption("Kode Transaksi, Tgl. Transaksi, Status, Saldo Awal");
    $objAxpDB->FormPosition("TL, TL, TR,HD");

    $objAxpDB->Lookup("fnstatus", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 2", "fntrans_status_code", "fttrans_status_name", 1,true);

    $sqls = "SELECT a.fnindex,
             a.ftasset_code,
             b.fcacquistion_price,
             a.fcbalance_amount ,
             a.fcmonthly_amount,
             d.fttrans_status_name FROM txdepriciation_det a
             INNER JOIN tlasset b ON a.ftasset_code=b.ftasset_code
             INNER JOIN tlasset_category C ON b.`ftcategory_code`=c.`ftcategory_code`
             INNER JOIN tstransaction_status D ON c.fntype=d.fntrans_status_code AND fntrans_group_code = 78";

      if ($result = RetRstField("SELECT COUNT(fnid) FROM txdepriciation_hdr WHERE fnstatus = 1") != 0){

            $objAxpDB->GridSQL(0, $sqls);
            $objAxpDB->GridCaption(0, "No., Kode Asset, Harga Perolehan, Sisa Penyusutan, Penyusutan Awal, Tipe Penyusutan");
            $objAxpDB->GridSize(0, "1,10,,15,,15");

            $objAxpDB->Lookup("ftasset_code", "SELECT ftasset_code, ftasset_name FROM tlasset", "ftasset_code", "ftasset_name", -1,false);

      }



    if(!$_REQUEST['id']) {

      $fdtrans_date=RetRstField("SELECT DATE_ADD(fdtrans_date, INTERVAL 1 MONTH) AS fdtrans_date FROM txdepriciation_hdr WHERE fnstatus = 1 ORDER BY fdtrans_date DESC LIMIT 1");

      list($yr, $mn, $dt)=split("-", $fdtrans_date);
      // $fdtrans_date = $dt . '/' . $mn . '/' . $yr;
      //
      if ($fdtrans_date == '') {
       $fdtrans_date = date('d/m/Y');
      }else{
       $fdtrans_date = $dt . '/' . $mn . '/' . $yr;
      }

      // SEBELUM TOMBOL PROSES DI KLIK, UNTUK AUTONUMBER
      $strAutoNumber1="ajax_do('query2.php?code=penyusutan_asset&ftmodule_code=acc003&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=fttrans_code&fttrans_code=fttrans_code&table_name=txdepriciation_hdr&ftbranch_code={$_SESSION['strbranch']}');";
      //
      $strAutoNumber0=" function OnChangeDate_fdtrans_date() {{$strAutoNumber1}}
      document.getElementById(\"fdtrans_date\").value = '{$fdtrans_date}';
      OnChangeDate_fdtrans_date();";


      $button_inquiry = "<input type='submit' name='proses' id='proses' value='PROSES' />";
      $objAxpDB->FormLayout(1, $button_inquiry);


     // KETIKA TOMBOL PROSES DI KLIK
     if (isset($_POST['proses'])) {

     $objAxpDB->GridSQL(0, $sqls);
     $objAxpDB->GridCaption(0, "No., Kode Asset, Harga Perolehan, Sisa Penyusutan, Penyusutan /bln, Tipe Penyusutan");
     $objAxpDB->GridSize(0, "1,10,,15,,15");
     $objAxpDB->Lookup("ftasset_code", "SELECT ftasset_code, ftasset_name FROM tlasset", "ftasset_code", "ftasset_name", -1,false);
     $objAxpDB->GridHidden(0, "1");

     //jika session tidak sama maka akan mengganti session yg baru,
     //jika session sama maka session tidak berubah

     if ($_SESSION['fdtrans_date'] != $_POST['fdtrans_date'] ) {

     $_SESSION['fdtrans_date'] = $_POST['fdtrans_date'];

     $fdtrans_date = $_SESSION['fdtrans_date'];

     }else{

     $fdtrans_date = $_POST['fdtrans_date'];

     }

     $ajax="ajax_do('system.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&fdtrans_date=' + document.getElementById(\"fdtrans_date\").value + '&ajax=1&code=grid');";

     $strAutoNumber1="ajax_do('query2.php?code=penyusutan_asset&ftmodule_code=acc003&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=fttrans_code&fttrans_code=fttrans_code&table_name=txdepriciation_hdr&ftbranch_code={$_SESSION['strbranch']}');";

     $strAutoNumber0=" function OnChangeDate_fdtrans_date() {
                         {$strAutoNumber1} {$ajax}
                       }

                       document.getElementById(\"fdtrans_date\").value = '{$fdtrans_date}';

                       OnChangeDate_fdtrans_date();

                       ";

     }

      $objAxpDB->FormJS($strAutoNumber0);

    }


    $objAxpDB->SetStatus("id,fnstatus");

    if ($objAxpDB->GetStatus("id") == ""){
            $objAxpDB->ButtonShow("baru");
    }elseif ($objAxpDB->GetStatus("id") == 0){
            $objAxpDB->ButtonShow("reset, simpan, tutup");
            $objAxpDB->FormLocked("1, 0, 1,1");
            $objAxpDB->GridLocked(0, "1, 1, 1, 1, 1,1,1");
    }elseif ($objAxpDB->GetStatus("fnstatus") == 1) {
            $objAxpDB->ButtonShow("batal,cetak, tutup");
            $objAxpDB->FormLocked("1, 1, 1");
            $objAxpDB->GridLocked(0, "1, 1, 1, 1, 1,1,1");
    }elseif ($objAxpDB->GetStatus("fnstatus") == 9) {
            $objAxpDB->ButtonShow("tutup");
            $objAxpDB->FormLocked("1, 1, 1");
            $objAxpDB->GridLocked(0, "1, 1, 1, 1, 1,1,1");
    }

    if($_REQUEST['ajax']) {
      switch($_REQUEST['code']) {
        case "grid":
          ob_start();
          $html = getAsset();
          ob_end_clean();
?>
          div = document.getElementById('txtHint0');
          var m='<?php echo js_esc($html); ?>';
          m= m.replace(/^\s*|\s*$/g,"");

          div.innerHTML =m;
          RowIndex0();
          reSize();
<?php
          break;
      }
      die("");
      exit;
    }

    $objAxpDB->Process();
    $objAxpDB->WriteHtmlTable();

    // if ($fdtrans_date == '') {
    // $fdtrans_date = $tanggal;
    //}else{
    //$fdtrans_date = $dt . '/' . $mn . '/' . $yr;
    //}
