<?php
     function ValDM() {

        $ssql="SELECT a.fnid as fnid FROM tlcoa_setting a LEFT JOIN tlcoa b ON a.ftcoa_code = b.ftcoa_code
                WHERE ftmodule_code = 'keuangan' AND b.ftcoa_code IS NULL ";

        $rs=mysql_query($ssql);
        $r=mysql_fetch_assoc($rs);
        if($r['fnid']) {
            echo "<script>
                      alert('Inputan COA Setting tidak valid ...');
                      window.history.back();
                 </script>";
            die("Inputan COA Setting tidak valid ...");
        }
    }

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

                $dt=getDateYmd($_REQUEST['fdtrans_date']);

                $ssql="SELECT COUNT(fnid) as jml FROM  txdebit_memo_hist WHERE ftsupplier_code='{$_REQUEST['ftsupplier_code']}'";
                $rs=mysql_query($ssql);
                $r=mysql_fetch_assoc($rs);
                if($r['jml']) {
                     $strSQL['Update txdebit_memo_hist'][]="
                                    UPDATE txdebit_memo_hist  SET
                                    fcdm_amount=fcdm_amount+".CCur($_REQUEST['fcinvoice_amount'])."  
                                    WHERE ftsupplier_code='{$_REQUEST['ftsupplier_code']}'";         
                }
                else {
                   $strSQL['Insert txdebit_memo_hist'][]="
                               INSERT INTO txdebit_memo_hist (
                                        fnid,ftbranch_code,ftsupplier_code,
                                        fdtrans_date,fcdm_amount,ftcreated_user,
                                        fdcreated_date,ftmodified_user,fdmodified_date
                                     ) 
                                    SELECT 
                                        '' AS fnid,'{$_SESSION['strbranch']}' AS ftbranch_code,'{$_REQUEST['ftsupplier_code']}' AS ftsupplier_code,
                                        '{$dt}' AS fdtrans_date,'".CCur($_REQUEST['fcinvoice_amount'])."' AS fcdm_amount,'{$_SESSION['username']}' AS ftcreated_user,
                                        NOW() AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date";

                }

                 $ftbankpym_no= getAutoNumber($_SESSION['strbranch'],'TN010','txbankpym_hdr','ftbankpym_no',$_REQUEST['fdtrans_date']);
                 $ftjournal_no= getAutoNumber($_SESSION['strbranch'],'TN014','txbankpym_hdr','ftjournal_no',$_REQUEST['fdtrans_date']);
                
                 //Bank Keluar
                /* $strSQL["Insert txbankpym_hdr"][] =
                    "INSERT INTO txbankpym_hdr
                    (fnid,ftbankpym_no,ftjournal_no,
                    ftbranch_code,fdtrans_date,ftacc_no1,
                    ftacc_desc,ftreference,fcamount,
                    fccomparison_amount,ftremarks,fnstatus,
                    ftprint_counter,fnprint_status,
                    ftcreated_user,fdcreated_date,ftmodified_user,
                    fdmodified_date
                    )
                    SELECT
                    '' AS fnid,'{$ftbankpym_no}' AS ftbankpym_no,'{$ftjournal_no}' AS ftjournal_no,
                    '{$_SESSION['strbranch']}' AS ftbranch_code,'".CDate($_REQUEST['fdtrans_date'])."' AS fdtrans_date,'{$_REQUEST['ftacc_no1']}' AS ftacc_no1,
                    '' AS ftacc_desc,'{$_REQUEST['ftdm_no']}' AS ftreference,'".CCur($_REQUEST['fcinvoice_amount'])*CCur($_REQUEST['fcrate'])."' AS fcamount,
                   '".CCur($_REQUEST['fcinvoice_amount'])*CCur($_REQUEST['fcrate'])."'  AS fccomparison_amount,'{$_REQUEST['ftnotes']}' AS ftremarks,'0' AS fnstatus,
                    '' AS ftprint_counter,'' AS fnprint_status,
                   '{$_SESSION['username']}' AS ftcreated_user,NOW() AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,
                    NOW() AS fdmodified_date";
                 
                   for($j=1; $j<=1; $j++) {
                                   
                        $strSQL["Insert txbankpym_det"][] ="
                            INSERT INTO txbankpym_det
                            (
                            fnid,ftbranch_code,fnindex,
                            ftbankpym_no,fdtrans_date,ftacc_no,
                            ftacc_desc,fnpayment_method,ftgiro_no,
                            fdgiro_date,fdgiro_cliring_date,fnstatus_giro,
                            ftdescription,fcamount,ftcreated_user,
                            fdcreated_date,ftmodified_user,fdmodified_date
                            )
                            SELECT
                            '' AS fnid,'{$_SESSION['strbranch']}' AS ftbranch_code,'{$j}' AS fnindex,
                            '{$ftbankpym_no}' AS ftbankpym_no,'".CDate($_REQUEST['fdtrans_date'])."' AS fdtrans_date, '' AS ftacc_no,
                            '' AS ftacc_desc,null AS fnpayment_method,'' AS ftgiro_no,
                            now() AS fdgiro_date, now() AS fdgiro_cliring_date,'0' AS fnstatus_giro,
                             '{$_REQUEST['ftnotes']}' AS ftdescription, '".CCur($_REQUEST['fcinvoice_amount'])*CCur($_REQUEST['fcrate'])."'  AS fcamount,'{$_SESSION['username']}' AS ftcreated_user,
                            NOW() AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date";

                   }*/

                     break;
        }
        return $strSQL;
     }
     
    function CancelData() {    
        global $objAxpDB;
//        $objAxpDB->Allow_Cancel(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
          case "batal": 

            $strSQL['Update txdebit_memo_hist'][]="
                                UPDATE txdebit_memo_hist  SET
                                fcdm_amount=fcdm_amount-".CCur($_REQUEST['fcinvoice_amount'])."  
                                WHERE ftsupplier_code='{$_REQUEST['ftsupplier_code']}'"; 
                                
         //-----------------------------------------------------
         // batal -%
//         $ssql1="SELECT IFNULL(MAX(ftdm_no),'')  AS M
//                      FROM txdebit_memo_hdr
//                      WHERE ftdm_no LIKE '{$_REQUEST['ftdm_no']}-%'";
//            $no=RetRstField($ssql1);
//            if($no!="") {
//                $no=$no+1;
//                $str1="-{$no}";
//            }
//            else {
//                $str1="-1";
//            }
//
//             $ftdm_no_new="{$_REQUEST['ftdm_no']}{$str1}";
//
//
//             $strSQL["batal"][]="update txdebit_memo_hdr
//                                set fnstatus=9,ftdm_no='{$ftdm_no_new}'
//                                where ftdm_no='{$_REQUEST['ftdm_no']}'";
//
//             $strSQL["batal"][]="update txdebit_memo_det
//                                set ftdm_no='{$ftdm_no_new}'
//                                where ftdm_no='{$_REQUEST['ftdm_no']}'";
         //--------------------------------------------

            break;
        }
        return $strSQL;
    }
    

?>
