<?php


function SaveData() {
    global $objAxpDB;
    $strSQL=array();
    $objAxpDB->Allow_Save(true);
    switch($_REQUEST[mode]) {
    case "simpan":

      for($j=0; $j < count($_REQUEST['ftasset_codeD0']); $j++) {

          $penyusutan_perbulan = CCur($_REQUEST['fcmonthly_amountD0'][$j]);
          $harga_perolehan   = CCur($_REQUEST['fcacquistion_priceD0'][$j]);
          $tipe = $_REQUEST['fntypeD0'][$j];
          $index = $j+1;
          
          $ssql="SELECT IFNULL(SUM(a.fcmonthly_amount), 0) as fcmonthly_amount_tot FROM txdepriciation_det a, txdepriciation_hdr b WHERE
          a.ftasset_code = '{$_REQUEST['ftasset_codeD0'][$j]}' AND a.fttrans_code=b.fttrans_code AND b.fnstatus=1";

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

          $fcbalance_amount= $harga_perolehan - ($r2["fcmonthly_amount_tot"] + $penyusutan_perbulan);

          $fcbalance_amount=($fcbalance_amount > 0) ? $fcbalance_amount: 0;

          $strSQL['Insert Penyusutan Asset Det'][]="
           INSERT INTO txdepriciation_det
           (fnindex,
            fttrans_code,
            ftasset_code,
            fcmonthly_amount,
            fcbalance_amount,
            ftcreated_user,
            fdcreated_date)
           SELECT
           '{$index}' AS fnindex,
           '{$_REQUEST['fttrans_code']}' AS fttrans_code,
           '{$_REQUEST['ftasset_codeD0'][$j]}' AS ftasset_code,
           '".CCur($_REQUEST['fcmonthly_amountD0'][$j])."' AS fcmonthly_amount,
           '".CCur($fcbalance_amount)."' AS fcbalance_amount,
           '{$_SESSION['username']}' AS ftcreated_user,
            now() AS fdcreated_date";
           
           $index = $index + 1;
        }


      break;
    }
    // echo $_REQUEST['fdtrans_date'];
    // echo "<pre>";
    // print_r($strSQL);
    // echo "</pre>";
    return $strSQL;
 }

 function format_indo($date){
     $BulanIndo = array("Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "November", "Desember");

     $tahun = substr($date, 0, 4);
     $bulan = substr($date, 5, 2);
     $tgl   = substr($date, 8, 2);
     $result = $tgl . "-" . $BulanIndo[(int)$bulan-1]. "-". $tahun;
     return($result);
 }
 function CancelData() {
    global $objAxpDB;
    $strSQL=array();
    switch($_REQUEST[mode]) {

        case "batal":

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

        $cek_tanggal = "SELECT fdTrans_Date FROM txdepriciation_hdr
        WHERE fnstatus = 1
        AND  LEFT(fdtrans_date, 7) > '{$fdtrans_date}'
        AND  fnstatus = 1
        ORDER BY fdtrans_date";

        $rs=mysql_query($cek_tanggal) or die ("query error {$ssql}");



        while($r=mysql_fetch_assoc($rs)) {

           $indo = $r['fdTrans_Date'];
           $indo = format_indo($indo);
           $tanggal.="- {$indo}\\n";

        }

        if($tanggal!="") {
             $massage=" Sudah Ada Transaksi Penyusutan, Batalkan Penyusutan Terlebih Dahulu \\n";
             $massage.="---------------------------------------------------------\\n";
             $massage.=$tanggal;
        }

        if($massage!="") {

        echo "<script>
              alert('{$massage}');
              window.history.back();
          </script>";
         die("");
        break;

      }

    }
    // case "simpan":
    //   $strSQL['Cancel Penyusutan Asset'][]="UPDATE txdepriciation_hdr SET fnstatus='9', ftmodified_user='{$_SESSION['username']}', fdmodified_date=now()";
    //
    //   break;

    return $strSQL;
 }


 //
 // function SaveData() {
 //     global $objAxpDB;
 //     $strSQL=array();
 //     $objAxpDB->Allow_Save(true);
 //     switch($_REQUEST[mode]) {
 //     case "simpan":
 //
 //       $strSQL['Delete Penyusutan Asset Det'][]="DELETE FROM txdepriciation_det WHERE fttrans_code='{$_REQUEST['fttrans_code']}'";
 //
 //       for($j=0; $j < count($_REQUEST['ftasset_codeD0']); $j++) {
 //
 //             $penyusutan_perbulan = CCur($_REQUEST['fcmonthly_amountD0'][$j]);
 //             $harga_perolehan     = CCur($_REQUEST['fcacquistion_priceD0'][$j]);
 //             $tipe = $_REQUEST['fntypeD0'][$j];
 //
 //             $ssql="SELECT IFNULL(SUM(a.fcmonthly_amount), 0) as fcmonthly_amount_tot FROM txdepriciation_det a, txdepriciation_hdr b WHERE
 //              a.ftasset_code = '{$_REQUEST['ftasset_codeD0'][$j]}' AND a.fttrans_code=b.fttrans_code AND b.fnstatus=1";
 //
 //             $mysql=mysql_query($ssql);
 //             $r2=mysql_fetch_assoc($mysql);
 //
 //             $fcbalance_amount=$harga_perolehan - ($r2["fcmonthly_amount_tot"] + $penyusutan_perbulan);
 //             $fcbalance_amount=($fcbalance_amount > 0) ? $fcbalance_amount: 0;
 //
 //           $strSQL['Insert Penyusutan Asset Det'][]="
 //            INSERT INTO txdepriciation_det
 //            (fnindex,
 //             fttrans_code,
 //             ftasset_code,
 //             fcmonthly_amount,
 //             fcbalance_amount,
 //             ftcreated_user,
 //             fdcreated_date)
 //            SELECT
 //            '{$j}' AS fnindex,
 //            '{$_REQUEST['fttrans_code']}' AS fttrans_code,
 //            '{$_REQUEST['ftasset_codeD0'][$j]}' AS ftasset_code,
 //            '".CCur($_REQUEST['fcmonthly_amountD0'][$j])."' AS fcmonthly_amount,
 //            '".CCur($fcbalance_amount)."' AS fcbalance_amount,
 //            '{$_SESSION['username']}' AS ftcreated_user,
 //             now() AS fdcreated_date";
 //         }
 //
 //       break;
 //     }
 //
 //     return $strSQL;
 //  }
