<?php
   if($_REQUEST['ajax']) {
    switch($_REQUEST['code']) {
      case "periode":
        $periode=substr($_REQUEST["periode"], 6, 4);

        if(RetRstField("SELECT fnid FROM thsaldoperkiraan WHERE LEFT(fdperiodesaldo, 4)='{$periode}'")) {
?>
           alert('Periode ini sudah ada sebelumnya...');
<?php
        } else {
?>
           submitbutton('Simpan','','','','');
 <?php
        }
                  
        die("");
        break;
    }
  }

function getCoa($periode){
  $periode=substr($periode, 6, 4);
  
  if(RetRstField("SELECT fnid FROM thsaldoperkiraan WHERE LEFT(fdperiodesaldo, 4)='{$periode}'")) {
    $ssql="SELECT b.ftcoa_code AS ftkode, b.ftcoa_name, a.fcdebit, a.fccredit FROM
           thsaldoperkiraan a RIGHT JOIN tlcoa b ON a.ftkode=b.ftcoa_code
           WHERE LEFT(fdperiodesaldo, 4)='{$periode}' AND RIGHT(b.ftcoa_code, 5)<>'.0000'";
  }else{
    $ssql="SELECT ftcoa_code AS ftkode, ftcoa_name, CONVERT('0', DECIMAL(19, 4)) AS fcdebit,
           CONVERT('0', DECIMAL(19, 4)) AS fccredit FROM tlcoa WHERE RIGHT(ftcoa_code, 5)<>'.0000'";
  }
  
  $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 Perkiraan</th>
        <td  valign='top'>Nama Perkiraan</th>
        <td  valign='top'>Debit</th>
        <td  valign='top'>Kredit</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";
    }
    
    $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='ftkodeD0[]' id='ftkodeD0$i' value='" . $r['ftkode'] . "' size=25>
          <td class='$class2'><input readonly style='background-color: #E0E0E0'  type='text' name='ftcoa_nameD0[]' id='ftcoa_nameD0$i' value='{$r["ftcoa_name"]}' size=55></td>
          <td class='$class2'><input onblur=\"ElementCurrencyOnBlur(this);\" type='text' onfocus=\"ElementCurrencyOnBlur(this); this.select();\" name='fcdebitD0[]' id='fcdebitD0$i' value='" .  number_format(floatval($r["fcdebit"]),2) . "' OnChange='SaldoAwalPerkiraanRecall();' size=20></td>
          <td class='$class2'><input onblur=\"ElementCurrencyOnBlur(this);\" type='text' onfocus=\"ElementCurrencyOnBlur(this); this.select();\" name='fccreditD0[]' id='fccreditD0$i' value='" .  number_format(floatval($r["fccredit"]), 2) . "' OnChange='SaldoAwalPerkiraanRecall();' size=20></td>
          </tr>";

    $i++;
  }

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

Getsecurity('saldoawal_perkiraan');
$objAxpDB->ViewName("Daftar Saldo Awal Perkiraan");

$objAxpDB->ViewSQL("SELECT fnid, year(fdperiodesaldo) AS periode, SUM(fcdebit) AS fcdebit, SUM(fccredit) AS fccredit FROM thsaldoperkiraan GROUP BY periode");
$objAxpDB->ViewCaption("ID, Periode, Debit, Kredit");

$objAxpDB->FormName("Saldo Awal Perkiraan");
$objAxpDB->FormSQL("SELECT fdperiodesaldo, CONVERT('0', DECIMAL(14, 9)) AS fcdebit_tot, CONVERT('0', DECIMAL(14, 9)) AS fccredit_tot FROM thsaldoperkiraan");
$objAxpDB->FormCaption("Periode, Total Debit, Total Credit");
$objAxpDB->FormPosition("TL, BR, BR");

$ajax="ajax_do('saldoawal.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&fdperiodesaldo=' + document.getElementById(\"fdperiodesaldo\").value + '&ajax=1&code=grid');";
$ajaxScriptWrite="function OnChangeDate_fdperiodesaldo() {
                   {$ajax}
                 }

                 OnChangeDate_fdperiodesaldo();";

$objAxpDB->FormJS($ajaxScriptWrite);

$objAxpDB->FormScript(2, "onChange=\"{$ajax}\"");

$objAxpDB->GridSQL(0, "SELECT fnindex, '' AS ftcoa_name, ftkode, fcdebit, fccredit FROM thsaldoperkiraan");
$objAxpDB->GridCaption(0, "No., Kode Perkiraan, Nama Perkiraan, Debit, Kredit");
                   
$objAxpDB->SetStatus("id");
if ($objAxpDB->GetStatus("id") == ""){
  $objAxpDB->ButtonShow("baru, tutup");
}elseif ($objAxpDB->GetStatus("id") == 0) {
  $objAxpDB->ButtonShow("reset, simpan, tutup");
  $objAxpDB->FormLocked("0, 1, 1");
  $objAxpDB->GridLocked(0, "1, 1, 0, 0");
} elseif ($objAxpDB->GetStatus("id") > 0) {
  $objAxpDB->ButtonShow("update, tutup");
  $objAxpDB->FormLocked("1 , 1, 1");
  $objAxpDB->GridLocked(0,"1, 1, 1, 1");
}

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

  die("");
  exit;
}

$objAxpDB->Process();

$objAxpDB->WriteHtmlTable();