<?php

function ValUangMukaPenjualan() {
    if($_REQUEST['ftgiro_no']!="") {
        $ssql="select fnid
                from txadvanced_receive
                where fnstatus_giro<>9 and ftgiro_no='".$_REQUEST['ftgiro_no']."'";

        $rs=mysql_query($ssql);
        $r=mysql_fetch_assoc($rs);
        if($r['fnid']) {
            echo "<script>
                      alert('No. Giro sudah pernah dipakai ...');
                      window.history.back();
                 </script>";
            die("No. Giro sudah pernah dipakai ...");
        }
    }
}

function ValBatalUM() {

    $ssql="select c.ftar_no
            from txadvanced_receive a
            INNER JOIN txar_det1 b ON a.fttrans_no = b.ftgiro_no
            INNER JOIN txar_hdr c ON b.ftar_no = c.ftar_no
            where c.fnstatus<>9 and c.fnstatus<>3
                    and a.fttrans_no='{$_REQUEST['fttrans_no']}'";

    $rs=mysql_query($ssql);
    $fttrans_no="";
    while($r=mysql_fetch_assoc($rs)) {
        $fttrans_no.=$r['ftar_no'];
    }

    if($fttrans_no!="") {
         $massage=" Penerimaan Piutang berikut harus dibatalkan terlebih dahulu \\n";
         $massage.="---------------------------------------------------------\\n";
         $massage.=$fttrans_no;
    }
    if($massage!="") {
      echo "<script>
            alert('{$massage}');
            window.history.back();
        </script>";
        die("");
    }

}


function SaveDataLast() {
    ValUangMukaPenjualan();

    global $objAxpDB;
    $strSQL=array();
    switch($_REQUEST[mode]) {
        case "simpan":
            if((($_REQUEST['fnpayment_method']==0)||($_REQUEST['fnpayment_method']==1))&&($_REQUEST['ftgiro_no']!='')) {
                $strSQL['Insert txgiro_hist'][]="
                            INSERT INTO txgiro_hist (
                                fnid,ftbranch_code,ftacc_no,ftacc_desc,
                                ftdocument_no,ftcustomer_code,ftcustomer_name,
                                ftcredit_no,fnpayment_method,ftgiro_no,
                                fdgiro_date,fdgiro_cliring_date,fdtrans_date,
                                ftremarks,fnstatus_giro,fncategory,
                                fncategory2,fcamount,fccredit_memo,fcother_cost,
                                ftother_sign,fctotal,fcrate,ftprint_counter,
                                fnprint_status,ftcreated_user,fdcreated_date,
                                ftmodified_user,fdmodified_date,fttype
                                )
                            SELECT
                                '' AS fnid,'{$_SESSION['strbranch']}' AS ftbranch_code,'{$_REQUEST['ftacc_no']}' AS ftacc_no,'' AS ftacc_desc,
                                '{$_REQUEST['fttrans_no']}' AS ftdocument_no,'{$_REQUEST['ftcustomer_code']}' AS ftcustomer_code,'' AS ftcustomer_name,
                                '' AS ftcredit_no,'{$_REQUEST['fnpayment_method']}' AS fnpayment_method,'{$_REQUEST['ftgiro_no']}' AS ftgiro_no,
                                '".CDate($_REQUEST['fdgiro_date'])."' AS fdgiro_date,'".CDate($_REQUEST['fdcliring_date'])."' AS fdgiro_cliring_date,'".CDate($_REQUEST['fdtrans_date'])."' AS fdtrans_date,
                                '{$_REQUEST['ftremarks']}' AS ftremarks,'0' AS fnstatus_giro,'' AS fncategory,
                                '' AS fncategory2,'".CCur($_REQUEST['fcamount'])."' AS fcamount,'' AS fccredit_memo,0 AS fcother_cost,
                                '' AS ftother_sign,'".CCur($_REQUEST['fcamount'])."' AS fctotal,'".CCur($_REQUEST['fcrate'])."' AS fcrate,'0' AS ftprint_counter,
                                '0' AS fnprint_status,'{$_SESSION['username']}' AS ftcreated_user,NOW() AS fdcreated_date,
                                '{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date ,
                                'UM' AS fttype";

            }

        break;
    }
    return $strSQL;
}


function UpdateDataLast() {
    global $objAxpDB;
    $strSQL=array();
    switch($_REQUEST[mode]) {
        case "update":
            if((($_REQUEST['fnpayment_method']==0)||($_REQUEST['fnpayment_method']==1))&&($_REQUEST['ftgiro_no']!='')) {
                $strSQL['Insert txgiro_hist'][]="
                            INSERT INTO txgiro_hist (
                                fnid,ftbranch_code,ftacc_no,ftacc_desc,
                                ftdocument_no,ftcustomer_code,ftcustomer_name,
                                ftcredit_no,fnpayment_method,ftgiro_no,
                                fdgiro_date,fdgiro_cliring_date,fdtrans_date,
                                ftremarks,fnstatus_giro,fncategory,
                                fncategory2,fcamount,fccredit_memo,fcother_cost,
                                ftother_sign,fctotal,fcrate,ftprint_counter,
                                fnprint_status,ftcreated_user,fdcreated_date,
                                ftmodified_user,fdmodified_date,fttype
                                )
                            SELECT
                                '' AS fnid,'{$_SESSION['strbranch']}' AS ftbranch_code,'{$_REQUEST['ftacc_no']}' AS ftacc_no,'' AS ftacc_desc,
                                '{$_REQUEST['fttrans_no']}' AS ftdocument_no,'{$_REQUEST['ftcustomer_code']}' AS ftcustomer_code,'' AS ftcustomer_name,
                                '' AS ftcredit_no,'{$_REQUEST['fnpayment_method']}' AS fnpayment_method,'{$_REQUEST['ftgiro_no']}' AS ftgiro_no,
                                '".CDate($_REQUEST['fdgiro_date'])."' AS fdgiro_date,'".CDate($_REQUEST['fdcliring_date'])."' AS fdgiro_cliring_date,'".CDate($_REQUEST['fdtrans_date'])."' AS fdtrans_date,
                                '{$_REQUEST['ftremarks']}' AS ftremarks,'0' AS fnstatus_giro,'' AS fncategory,
                                '' AS fncategory2,'".CCur($_REQUEST['fcamount'])."' AS fcamount,'' AS fccredit_memo,0 AS fcother_cost,
                                '' AS ftother_sign,'".CCur($_REQUEST['fcamount'])."' AS fctotal,'".CCur($_REQUEST['fcrate'])."' AS fcrate,'0' AS ftprint_counter,
                                '0' AS fnprint_status,'{$_SESSION['username']}' AS ftcreated_user,NOW() AS fdcreated_date,
                                '{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date ,
                                'UM' AS fttype";
            }
            
        break;
    }
    return $strSQL;
}

function CancelData() {
    ValBatalUM();
    
    global $objAxpDB;
    $strSQL=array();
    switch($_REQUEST[mode]) {
        case "batal":
            $strSQL['Update txadvanced_receive'][]="
                        UPDATE txadvanced_receive SET fngiro_status=9 WHERE fttrans_no='{$_REQUEST['fttrans_no']}'";

            $strSQL['Update txgiro_hist'][]="
                        UPDATE txgiro_hist SET fnstatus_giro=9 WHERE ftgiro_no='{$_REQUEST['ftgiro_no']}' AND ftdocument_no='{$_REQUEST['fttrans_no']}' and fnstatus_giro <> 9 ";

        break;
    }
    return $strSQL;
}

function RevisiData(){
    global $objAxpDB;
    //$objAxpDB->Allow_Revisi(true);
    $strSQL=array();
    switch($_REQUEST[mode]) {
        case "simpan_revisi":
            ValUangMukaPenjualan();

            // Data lama penonomoran ditambah format '-'
            $ssql1="SELECT IFNULL(MAX(fttrans_no),'')  AS M
                      FROM txadvanced_receive
                      WHERE fttrans_no LIKE '{$_REQUEST['fttrans_no']}-%'";
            $no=RetRstField($ssql1);

            if($no!="") {
                $no1=explode("-",$no);
                $no=(int)$no1[1]+1;
                $str1="-{$no}";
            }
            else {
                $str1="-1";
            }

            $fttrans_no_new="{$_REQUEST['fttrans_no']}{$str1}";

            $strSQL["Revisi"][]="update txadvanced_receive
                    set fdmodified_date=now(), ftmodified_user='{$_SESSION['username']}',
                    fnstatus=3, fttrans_no='{$fttrans_no_new}'
                    where fttrans_no='{$_REQUEST['fttrans_no']}'";

            $strSQL["Revisi"][]="update txgiro_hist
                        set ftdocument_no='{$fttrans_no_new}', fnstatus_giro='9'
                        where ftdocument_no='{$_REQUEST['fttrans_no']}' and fttype='UM'";

            $_REQUEST['fdgiro_date'][$j]=getDateYmd($_REQUEST['fdgiro_date'][$j]);
            $_REQUEST['fdcliring_date'][$j]=getDateYmd($_REQUEST['fdcliring_date'][$j]);

            if(($_REQUEST['fnpayment_methodD0'][$j]==0)||($_REQUEST['fnpayment_methodD0'][$j]==1)) {
                $fnstatus="0";
            } else {
                $fnstatus="1";
            }

            // Simpan ulang
            
            // Call Function Revisi General Ledger
            if(function_exists("RevisiGL")) {
                $m = RevisiGL($fttrans_no_new);
                if ($m != '') { $strSQL=array_merge_recursive($strSQL,$m); }
            }

            break;
    }

    return $strSQL;
}


function RevisiDataLast() {
    global $objAxpDB;
    $strSQL=array();
    switch($_REQUEST[mode]) {
        case "simpan_revisi":
            if((($_REQUEST['fnpayment_method']==0)||($_REQUEST['fnpayment_method']==1))&&($_REQUEST['ftgiro_no']!='')) {
                $strSQL['Insert txgiro_hist'][]="
                            INSERT INTO txgiro_hist (
                                fnid,ftbranch_code,ftacc_no,ftacc_desc,
                                ftdocument_no,ftcustomer_code,ftcustomer_name,
                                ftcredit_no,fnpayment_method,ftgiro_no,
                                fdgiro_date,fdgiro_cliring_date,fdtrans_date,
                                ftremarks,fnstatus_giro,fncategory,
                                fncategory2,fcamount,fccredit_memo,fcother_cost,
                                ftother_sign,fctotal,fcrate,ftprint_counter,
                                fnprint_status,ftcreated_user,fdcreated_date,
                                ftmodified_user,fdmodified_date,fttype
                                )
                            SELECT
                                '' AS fnid,'{$_SESSION['strbranch']}' AS ftbranch_code,'{$_REQUEST['ftacc_no']}' AS ftacc_no,'' AS ftacc_desc,
                                '{$_REQUEST['fttrans_no']}' AS ftdocument_no,'{$_REQUEST['ftcustomer_code']}' AS ftcustomer_code,'' AS ftcustomer_name,
                                '' AS ftcredit_no,'{$_REQUEST['fnpayment_method']}' AS fnpayment_method,'{$_REQUEST['ftgiro_no']}' AS ftgiro_no,
                                '".CDate($_REQUEST['fdgiro_date'])."' AS fdgiro_date,'".CDate($_REQUEST['fdcliring_date'])."' AS fdgiro_cliring_date,'".CDate($_REQUEST['fdtrans_date'])."' AS fdtrans_date,
                                '{$_REQUEST['ftremarks']}' AS ftremarks,'0' AS fnstatus_giro,'' AS fncategory,
                                '' AS fncategory2,'".CCur($_REQUEST['fcamount'])."' AS fcamount,'' AS fccredit_memo,0 AS fcother_cost,
                                '' AS ftother_sign,'".CCur($_REQUEST['fcamount'])."' AS fctotal,'".CCur($_REQUEST['fcrate'])."' AS fcrate,'0' AS ftprint_counter,
                                '0' AS fnprint_status,'{$_SESSION['username']}' AS ftcreated_user,NOW() AS fdcreated_date,
                                '{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date ,
                                'UM' AS fttype";

            }

        break;
    }
    return $strSQL;
}

