<?php
function ValSaveProyeksi() {
     for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
        $ssql="select fnid from txpayment_projection_det where ftpo_no='{$_REQUEST['ftpo_noD0'][$j]}' and fnstatus=1";

         if(RetRstField($ssql)) {
              echo "<script>
                                      alert('No PO :{$_REQUEST['ftpo_noD0'][$j]}  sudah digunakan  ...');
                                      window.history.back();
                                 </script>";
                                die("No PO :{$_REQUEST['ftpo_noD0'][$j]}  sudah digunakan  ...");
         }
     }
}


 function SaveData() {
     ValSaveProyeksi();
    global $objAxpDB;
    $objAxpDB->Allow_Save(false);
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "simpan":
      $_REQUEST['fdtrans_date']=getDateYmd($_REQUEST['fdtrans_date']);      
       $strSQL['Insert txpayment_projection_hdr'][]="
                INSERT INTO txpayment_projection_hdr (
                    fnid,ftpp_no,fdtrans_date,ftcurrency_code,fckurs,
                    ftnotes,fnstatus,ftcreated_user,
                    fdcreated_date,ftmodified_user,fdmodified_date
                )
                SELECT 
                    '' AS fnid,'{$_REQUEST['ftpp_no']}' AS ftpp_no,'{$_REQUEST['fdtrans_date']}' AS fdtrans_date,'{$_REQUEST['ftcurrency_code']}' AS ftcurrency_code,'". CCur($_REQUEST['fckurs'])."' AS fckurs,
                    '{$_REQUEST['ftnotes']}' AS ftnotes,'1' AS fnstatus,'{$_SESSION['username']}' AS ftcreated_user,
                    NOW() AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date";
        
         for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
            
            $_REQUEST['ftdue_dateD0'][$j]=getDateYmd($_REQUEST['ftdue_dateD0'][$j]);
          
                 $strSQL['Insert txpayment_projection_det'][]="
                                 INSERT INTO txpayment_projection_det (
                                    fnid,ftpp_no,fnindex,fcpajak,ftvat_no,ftinvoice_supplier,
                                    ftsupplier_code,ftpo_no,fcamount,
                                    ftcv_no,ftreceipt_no,ftdescription,
                                    ftdue_date,fnstatus,ftcreated_user,
                                    fdcreated_date,ftmodified_user,fdmodified_date,
                                    fcoutstanding_amount,fcpayment_amount,fnpayment_status,
                                    fcamountUSD,fcoutstanding_amountUSD,fcpayment_amountUSD,
                                    ftacc_no
                                  )
                                  SELECT
                                    '' AS fnid,'{$_REQUEST['ftpp_no']}' AS ftpp_no,'{$j}' AS fnindex,'". CCur($_REQUEST['fcpajakD0'][$j])."' AS fcpajak,'{$_REQUEST['ftvat_noD0'][$j]}' AS ftvat_no,'{$_REQUEST['ftinvoice_supplierD0'][$j]}' AS ftinvoice_supplier,
                                    '{$_REQUEST['ftsupplier_codeD0'][$j]}' AS ftsupplier_code, '{$_REQUEST['ftpo_noD0'][$j]}' AS ftpo_no, '".CCur($_REQUEST['fcamountD0'][$j])."' AS fcamount,
                                    '{$_REQUEST['ftcv_noD0'][$j]}' AS ftcv_no,'{$_REQUEST['ftreceipt_noD0'][$j]}' AS ftreceipt_no,'{$_REQUEST['ftdescriptionD0'][$j]}' AS ftdescription,
                                    '{$_REQUEST['ftdue_dateD0'][$j]}' AS ftdue_date,'1' AS fnstatus,'{$_SESSION['username']}' AS ftcreated_user,
                                    NOW() AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date,
                                    '".CCur($_REQUEST['fcamountD0'][$j])."' AS fcoutstanding_amount,'0' AS fcpayment_amount,'1' as fnpayment_status,
                                    '".CCur($_REQUEST['fcamountUSDD0'][$j])."' AS fcamountUSD,
                                    '".CCur($_REQUEST['fcamountUSDD0'][$j])."' AS fcoutstanding_amountUSD,
                                    '0' AS fcpayment_amountUSD,
                                    '{$_REQUEST['ftacc_noD0'][$j]}'";
             }
       
       break;
    }
//    pr($strSQL);
    return $strSQL;
 }
 
 function UpdateData() {   
    global $objAxpDB;
    $objAxpDB->Allow_Update(false);
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "update":      
       
          for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
            
             $_REQUEST['ftdue_dateD0'][$j]=getDateYmd($_REQUEST['ftdue_dateD0'][$j]);
              $ssql="select fnid from txpayment_projection_det where fnid='{$_REQUEST['fnidD0'][$j]}' and fnstatus=1 and  fnpayment_status=1";

             if(RetRstField($ssql)) {
//             $strSQL['Update txpayment_projection_det'][]="
//                             REPLACE INTO txpayment_projection_det (
//                                fnid,ftpp_no,fnindex,fcpajak,
//                                ftsupplier_code,ftpo_no,fcamount,
//                                ftcv_no,ftreceipt_no,ftdescription,
//                                ftdue_date,fnstatus,ftcreated_user,
//                                fdcreated_date,ftmodified_user,fdmodified_date,
//                                fcoutstanding_amount,fcpayment_amount,fnpayment_status,
//                                fcamountUSD,fcoutstanding_amountUSD,fcpayment_amountUSD,
//                                ftacc_no
//                              )
//                              SELECT
//                                '{$_REQUEST['fnidD0'][$j]}' AS fnid,'{$_REQUEST['ftpp_no']}' AS ftpp_no,'{$_REQUEST['fnindexD0'][$j]}' AS fnindex,'". CCur($_REQUEST['fcpajakD0'][$j])."' AS fcpajak,
//                                '{$_REQUEST['ftsupplier_codeD0'][$j]}' AS ftsupplier_code, '{$_REQUEST['ftpo_noD0'][$j]}' AS ftpo_no, '".CCur($_REQUEST['fcamountD0'][$j])."' AS fcamount,
//                                '{$_REQUEST['ftcv_noD0'][$j]}' AS ftcv_no,'{$_REQUEST['ftreceipt_noD0'][$j]}' AS ftreceipt_no,'{$_REQUEST['ftdescriptionD0'][$j]}' AS ftdescription,
//                                '{$_REQUEST['ftdue_dateD0'][$j]}' AS ftdue_date,'{$_REQUEST['fnstatusD0'][$j]}' AS fnstatus,'{$_SESSION['username']}' AS ftcreated_user,
//                                NOW() AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date,
//                                '".CCur($_REQUEST['fcamountD0'][$j])."' AS fcoutstanding_amount,'".CCur($_REQUEST['fcamountD0'][$j])."' AS fcpayment_amount,'1' as fnpayment_status,
//                                '".CCur($_REQUEST['fcamountUSDD0'][$j])."' AS fcamountUSD,
//                                '".CCur($_REQUEST['fcamountUSDD0'][$j])."' AS fcoutstanding_amountUSD,
//                                '0' AS fcpayment_amountUSD,
//                                  '{$_REQUEST['ftacc_noD0'][$j]}'";

                    $strSQL['Update txpayment_projection_det'][]="
                             UPDATE txpayment_projection_det                               
                               SET  fcamount='".CCur($_REQUEST['fcamountD0'][$j])."',
                                ftreceipt_no='{$_REQUEST['ftreceipt_noD0'][$j]}',
                                ftdescription='{$_REQUEST['ftdescriptionD0'][$j]}',
                                ftdue_date='{$_REQUEST['ftdue_dateD0'][$j]}',fnstatus='{$_REQUEST['fnstatusD0'][$j]}',
                                ftmodified_user='{$_SESSION['username']}',fdmodified_date=NOW(),
                                fcoutstanding_amount= '".CCur($_REQUEST['fcamountD0'][$j])."',
                                fcpayment_amount=0,fnpayment_status='1',
                                fcamountUSD='".CCur($_REQUEST['fcamountUSDD0'][$j])."',
                                fcoutstanding_amountUSD='".CCur($_REQUEST['fcamountUSDD0'][$j])."',
                                fcpayment_amountUSD='0',
                                ftacc_no='{$_REQUEST['ftacc_noD0'][$j]}',
                                ftvat_no='{$_REQUEST['ftvat_noD0'][$j]}',
                                ftinvoice_supplier='{$_REQUEST['ftinvoice_supplierD0'][$j]}' 
                                  WHERE ftpo_no='{$_REQUEST['ftpo_noD0'][$j]}' and
                                                ftpp_no= '{$_REQUEST['ftpp_no']}'";
                            
             }
             
         }
       break;
    }
//     pr($strSQL);
//     die("");
    return $strSQL;
 }
 
   function CancelData() {    
        global $objAxpDB;
//       $objAxpDB->Allow_Cancel(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
          case "batal":  
        
         // batal -%
//          $ssql1="SELECT IFNULL(MAX(ftpp_no),'')  AS M
//                      FROM txpayment_projection_hdr
//                      WHERE ftpp_no LIKE '{$_REQUEST['ftpp_no']}-%'";
//            $no=RetRstField($ssql1);
//            if($no!="") {
//                $no=$no+1;
//                $str1="-{$no}";
//            }
//            else {
//                $str1="-1";
//            }
//
//             $ftpp_no_new="{$_REQUEST['ftpp_no']}{$str1}";
//
//             $strSQL["batal"][]="update txpayment_projection_hdr
//                                set fnstatus=9,ftpp_no='{$ftpp_no_new}'
//                                where ftpp_no='{$_REQUEST['ftpp_no']}'";
//
//             $strSQL["batal"][]="update txpayment_projection_det
//                                set ftpp_no='{$ftpp_no_new}'
//                                where ftpp_no='{$_REQUEST['ftpp_no']}'";

            //--------------------------------------------
            break;
        }
        
        return $strSQL;
      }
      

?>
