<?php
 function SaveData() {
    global $objAxpDB;
    $objAxpDB->Allow_Save(false);
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "simpan":
      $_REQUEST['fdperiodesaldo']=getDateYmd($_REQUEST['fdperiodesaldo']);
      
      for($j=0; $j<count($_REQUEST['fnindexD0']); $j++) {
        $strSQL['Insert thsaldoperkiraan'][]="
                INSERT INTO thsaldoperkiraan (
                ftbranch_code,fnindex,ftkode,
                fdperiodesaldo,fcdebit,fccredit,
                ftcreated_user,fdcreated_date)
                SELECT
                '{$_SESSION['strbranch']}' AS ftbranch_code,'{$j}' AS fnindex,
                '{$_REQUEST['ftkodeD0'][$j]}' AS ftkode,'{$_REQUEST["fdperiodesaldo"]}' AS fdperiodesaldo, '".CCur($_REQUEST['fcdebitD0'][$j])."' AS fcdebit,
                '".CCur($_REQUEST['fccreditD0'][$j])."' AS fccredit,'{$_SESSION['username']}' AS ftcreated_user,
                  NOW() AS fdcreated_date";
         }
       break;
    }
//    pr($strSQL);
    return $strSQL;
 }

 function UpdateData() {
    global $objAxpDB;
    $objAxpDB->Allow_Update(false);
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "update":
      $_REQUEST['fdperiodesaldo']=getDateYmd($_REQUEST['fdperiodesaldo']);
      $periode=substr($_REQUEST["fdperiodesaldo"], 0, 4);

      for($j=0; $j<count($_REQUEST['fnindexD0']); $j++):
        if(RetRstField("SELECT fnid FROM thsaldoperkiraan WHERE LEFT(fdperiodesaldo, 4)='{$periode}' AND ftkode='{$_REQUEST['ftkodeD0'][$j]}'")) {
          $strSQL['Update thsaldoperkiraan'][]="
                UPDATE thsaldoperkiraan SET
                fnindex='{$j}',
                fcdebit='".CCur($_REQUEST['fcdebitD0'][$j])."',
                fccredit='".CCur($_REQUEST['fccreditD0'][$j])."',
                ftmodified_user='{$_SESSION['username']}',
                fdmodified_date=NOW()
                WHERE
                LEFT(fdperiodesaldo, 4)='{$periode}' AND 
                ftkode='{$_REQUEST['ftkodeD0'][$j]}'";
        }else{
            $strSQL['Insert thsaldoperkiraan'][]="
                INSERT INTO thsaldoperkiraan (
                ftbranch_code,fnindex,ftkode,
                fdperiodesaldo,fcdebit,fccredit,
                ftcreated_user,fdcreated_date,
                ftmodified_user,fdmodified_date)
                SELECT
                '{$_SESSION['strbranch']}' AS ftbranch_code,'{$j}' AS fnindex,
                '{$_REQUEST['ftkodeD0'][$j]}' AS ftkode,'{$_REQUEST["fdperiodesaldo"]}' AS fdperiodesaldo, '".CCur($_REQUEST['fcdebitD0'][$j])."' AS fcdebit,
                '".CCur($_REQUEST['fccreditD0'][$j])."' AS fccredit,'{$_SESSION['username']}' AS ftcreated_user,
                NOW() AS fdcreated_date, '{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date";
        }
      endfor;

      break;
    }
    
    return $strSQL;
 }