<?php

if(!function_exists(ValAR)){
  function ValAR() {
    if(RetRstField("SELECT fnid FROM tlcustomer WHERE ftcustomer_code='{$_REQUEST['ftcustomer_code']}'")) {
      echo"<script>alert('Customer tidak valid...'); window.history.back();</script>";
      exit;
    }
  }
}

switch($_REQUEST[mode]) {


//    case "batal":
//        $ssql = "SELECT a.fnid FROM txdelivery_order_hdr a
//                    INNER JOIN txsales_order_hdr b ON a.ftso_no=b.ftso_no
//                WHERE a.ftso_no='{$_REQUEST['ftso_no']}' AND
//                    a.fnstatus<>9";
//
//        $rs=mysql_query($ssql);
//    //   die($str);
//        if(RetRstField($ssql)) {
//            echo "<script>
//                    alert('No. SO sudah terlampir pada DO, DO harus dibatal kan terlebih dahulu');
//                    window.history.back();
//                </script>";
//                die("");
//        }
//      break;
}


/* Tidak digunakan
 function FinalData() {
    global $objAxpDB;
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "final":
        $dt=getDateYmd($_REQUEST['fdtrans_date']);
        $dt2=getDateYmd($_REQUEST['fddue_date']);

        $ssql = mysql_query("SELECT ftdo_no FROM txdelivery_order_hdr WHERE ftso_no='{$_REQUEST['ftso_no']}'");
        $d = mysql_fetch_assoc($ssql);

        $ftdo_no = $d['ftdo_no'];

        // SO
        $strSQL['Delete SO Det'][]="DELETE FROM txsales_order_det WHERE ftso_no='{$_REQUEST['ftso_no']}'";

        // DO
        $strSQL['Delete DO Det'][]="DELETE FROM txdelivery_order_det WHERE ftdo_no='{$ftdo_no}'";

        $ftstock_out_no=getAutoNumber($_SESSION['strbranch'],'inv005','txstock_out_hdr','ftstock_out_no',$_REQUEST['fdtrans_date']);

        $strSQL['Insert Stock Out Hdr'][]="
                INSERT INTO txstock_out_hdr
                (ftbranch_code, ftstock_out_no, ftcategory_code,
                ftstock_out_company, ftdocument_no, fdtrans_date,
                fntotal_qty, ftnotes, fnstatus, ftcreated_user,
                fdcreated_date, ftmodified_user, fdmodified_date, ftstock_type_code)
                SELECT
                '{$_SESSION['strbranch']}' AS ftbranch_code, '{$ftstock_out_no}' AS ftstock_out_no, '4' AS ftcategory_code,
                '{$_REQUEST['ftcustomer_code']}' AS  ftstock_out_company, '{$ftdo_no}' AS ftdocument_no,'{$dt}' AS  fdtrans_date,
                '".CCur($_REQUEST['fftotal_qty'])."' AS fntotal_qty,'{$_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, 'inv' as ftstock_type_code";

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

            // SO
            $strSQL['Insert SO Det'][]="
                     INSERT INTO txsales_order_det
                     (ftbranch_code, ftso_no,
                     fnindex, ftitem_code,
                     ftwarehouse_code, ftlocation_code,
                     fnqty_available,
                     fnqty, ftuom_code,
                     fcprice, ffdiscount,
                     fcdiscount, fcamount,
                     fnqty_base, ffformula,
                     fnqty_accept, fnqty_accept_base,
                     ftcreated_user, fdcreated_date,
                     ftmodified_user, fdmodified_date)
                     SELECT
                     '{$_SESSION['strbranch']}' AS ftbranch_code, '{$_REQUEST['ftso_no']}' AS ftso_no,
                     '{$j}' AS fnindex, '{$_REQUEST['ftitem_codeD0'][$j]}' AS ftitem_code,
                     '{$_REQUEST['ftwarehouse_codeD0'][$j]}' AS ftwarehouse_code, '{$_REQUEST['ftlocation_codeD0'][$j]}' AS ftlocation_code,
                     '".CCur($_REQUEST['fnqty_availableD0'][$j])."' AS fnqty_available,
                     '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty, '{$_REQUEST['ftuom_codeD0'][$j]}' AS ftuom_code,
                     '".CCur($_REQUEST['fcpriceD0'][$j])."' AS fcprice, '".CCur($_REQUEST['ffdiscountD0'][$j])."' AS ffdiscount,
                     '".CCur($_REQUEST['fcdiscountD0'][$j])."' AS fcdiscount, '".CCur($_REQUEST['fcamountD0'][$j])."' AS fcamount,
                     '".CCur($_REQUEST['fnqty_baseD0'][$j])."' AS fnqty_base, '".CCur($_REQUEST['ffformulaD0'][$j])."' AS ffformula,
                     '".(double)CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty_accept, '".(double)CCur($_REQUEST['fnqty_baseD0'][$j])."' AS fnqty_accept_base,
                     '{$_SESSION['username']}' AS ftcreated_user, now() AS fdcreated_date,
                     '{$_SESSION['username']}' AS ftmodified_user, now() AS fdmodified_date";

            // DO
            $ssql2 = mysql_query("select ftitem_name,ftprint_name from tlstock_hdr where ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'");
            $r2 = mysql_fetch_assoc($ssql2);

            $strSQL['Insert DO Det'][]="
                     INSERT INTO txdelivery_order_det
                        (ftbranch_code, ftdo_no, fnindex,
                        ftitem_code, ftitem_name, ftprint_name,
                        ftwarehouse_code, ftlocation_code,
                        fnqty_available, fnqty_so,
                        fnqty, ftuom_code, fdtrans_date,
                        ftdescription,
                        fnqty_base,ffformula,
                        ftcreated_user, fdcreated_date,
                        ftmodified_user, fdmodified_date)
                     SELECT
                        '{$_SESSION['strbranch']}' AS ftbranch_code, '{$ftdo_no}' AS ftdo_no, '{$j}' AS fnindex,
                        '{$_REQUEST['ftitem_codeD0'][$j]}' AS ftitem_code, '{$r['ftitem_name']}' AS ftitem_name, '{$r2['ftprint_name']}' AS ftprint_name,
                        '{$_REQUEST['ftwarehouse_codeD0'][$j]}' AS ftwarehouse_code, '{$_REQUEST['ftlocation_codeD0'][$j]}' AS ftlocation_code,
                        '".CCur($_REQUEST['fnqty_availableD0'][$j])."' AS fnqty_available, '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty_so,
                        '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty, '{$_REQUEST['ftuom_codeD0'][$j]}' AS ftuom_code, '{$dt}' AS  fdtrans_date,
                        '{$_REQUEST['ftdescriptionD0'][$j]}' AS ftdescription,
                        '".CCur($_REQUEST['fnqty_baseD0'][$j])."' AS fnqty_base, '".CCur($_REQUEST['ffformulaD0'][$j])."' AS ffformula,
                        '{$_SESSION['username']}' AS ftcreated_user, now() AS fdcreated_date,
                        '{$_SESSION['username']}' AS ftmodified_user, now() AS fdmodified_date";

            $rs=mysql_query("select ftitem_name,ftprint_name from tlstock_hdr where ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'");
            $r=mysql_fetch_assoc($rs);

            $strSQL["n"][]="update txdelivery_order_det set ftitem_name='".$r['ftitem_name']."',ftprint_name='".$r['ftprint_name']."' where ftdo_no='".$ftdo_no."' and ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'";

            $strSQL['Insert Stock Out Det'][]="
                     INSERT INTO txstock_out_det
                     (ftbranch_code, ftstock_out_no, fnindex, ftitem_code,
                     ftwarehouse_code, ftlocation_code, fnqty_available,
                     fnqty, ftuom_code, fnqty_base,ffformula,
                     ftcreated_user,
                     fdcreated_date, ftmodified_user)
                     SELECT
                     '{$_SESSION['strbranch']}' AS ftbranch_code,'{$ftstock_out_no}' AS ftstock_out_no, '{$j}' AS fnindex, '{$_REQUEST['ftitem_codeD0'][$j]}' AS ftitem_code,
                     '{$_REQUEST['ftwarehouse_codeD0'][$j]}' AS ftwarehouse_code, '{$_REQUEST['ftlocation_codeD0'][$j]}' AS ftlocation_code, '".CCur($_REQUEST['fnqty_availableD0'][$j])."' AS fnqty_available,
                     '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty, '{$_REQUEST['ftuom_codeD0'][$j]}' AS ftuom_code, '".CCur($_REQUEST['fnqty_baseD0'][$j])."' AS fnqty_base, '".CCur($_REQUEST['ffformulaD0'][$j])."' AS ffformula,
                     '{$_SESSION['username']}' AS ftcreated_user,
                     now() AS fdcreated_date, '{$_SESSION['username']}' AS ftmodified_user";

            $strSQL['Update txsales_order_det'][]="
                     UPDATE txsales_order_det
                     SET fnqty_accept_base=IFNULL(fnqty_accept_base,0)+ ".(double)CCur($_REQUEST['fnqty_baseD0'][$j]).",
                     fnqty_accept= IFNULL(fnqty_accept,0)+ ".(double)CCur($_REQUEST['fnqtyD0'][$j])."
                     WHERE ftso_no='{$_REQUEST['ftso_no']}' AND
                     ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}'";

            $m=SaveStock('-',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_codeD0'][$j],$_REQUEST['ftlocation_codeD0'][$j],'',CCur($_REQUEST['fnqty_baseD0'][$j]),CCur($_REQUEST['fnqty_baseD0'][$j]));
            $strSQL=array_merge_recursive($strSQL,$m);
        }

        if($m==0) {
            // DO
            unset($strSQL['Insert Stock Out Det']);
        }

        // SO
        $strSQL['Update SO Hdr'][]="UPDATE txsales_order_hdr
                 SET fnstatus=1,
                 ftdelivery_method='{$_REQUEST["ftdelivery_method"]}', ftreference='{$_REQUEST["ftreference"]}',
                 fdtrans_date='{$dt}', ftterm_payment='{$_REQUEST["ftterm_payment"]}',
                 fddue_date='{$dt2}', ftcurrency_code='{$_REQUEST["ftcurrency_code"]}',
                 fcrate='".CCur($_REQUEST["fcrate"])."', fntax_code='{$_REQUEST["fntax_code"]}',
                 fnpayment_status='{$_REQUEST["fnpayment_status"]}', fdvat_date='{$_REQUEST["fdvat_date"]}',
                 ftvat_no='{$_REQUEST["ftvat_no"]}', ftreff_no='{$_REQUEST["ftreff_no"]}',
                 ftnotes='{$_REQUEST["ftnotes"]}', fftotal_qty='".CCur($_REQUEST["fftotal_qty"])."',
                 fcsub_amount='".CCur($_REQUEST["fcsub_amount"])."', ffdiscount='".CCur($_REQUEST["ffdiscount"])."',
                 fcdiscount='".CCur($_REQUEST["fcdiscount"])."', fcother_cost='".CCur($_REQUEST["fcother_cost"])."',
                 ffvat='".CCur($_REQUEST["ffvat"])."', fcvat='".CCur($_REQUEST["fcvat"])."',
                 fcamount='".CCur($_REQUEST["fcamount"])."'
                 WHERE ftso_no='{$_REQUEST["ftso_no"]}'";

        // DO
        $strSQL['Update DO Hdr'][]="UPDATE txdelivery_order_hdr SET fnstatus=2 WHERE ftdo_no='{$ftdo_no}'";

        // SI
        $strSQL['Update SI Hdr'][]="UPDATE txsales_invoice_hdr SET fnstatus=1 WHERE ftdo_no='{$ftdo_no}'";

        break;
    }
    return $strSQL;
 }
*/
function SaveData() {

    global $objAxpDB;
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "simpan":
        // auto generate number when save
        $ftso_no=getAutoNumber($_SESSION['strbranch'],'sls001','txsales_order_hdr','ftso_no',$_REQUEST['fdtrans_date']);
        $_REQUEST['ftso_no'] = $ftso_no;

        // check and auto cancel for sales order more than 1 day
        $strSQL["Update txsales_order_hdr expire"][] ="
                        UPDATE txsales_order_hdr
                        SET fnstatus = 9, ftmodified_user = 'admin', fdmodified_date = now()
                        WHERE DATEDIFF(DATE_FORMAT(NOW(), '%Y-%m-%d'),fdtrans_date) > 1 and fnstatus = 1";

        break;

    }
    // die(print_r($strSQL));
    return $strSQL;
}

function SaveDataLast() {
    global $objAxpDB;
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "simpan":
        /*
        if ($_REQUEST['fntax_code']==0) {
            $ftmodule_code = "TN006";
        }
        else if ($_REQUEST['fntax_code']==1) {
            $ftmodule_code = "TN021";
        }
        else if ($_REQUEST['fntax_code']==2) {
            $ftmodule_code = "TN022";
        }

        $ftdo_no = getAutoNumber($_SESSION['strbranch'],'TN005','txdelivery_order_hdr','ftdo_no',$_REQUEST['fdtrans_date']);
        $ftsi_no = getAutoNumber($_SESSION['strbranch'],$ftmodule_code,'txsales_invoice_hdr','ftsales_invoice_no',$_REQUEST['fdtrans_date']);
        */

        // Penomoran SO, DO dan SI harus sama kecuali prefix nya saja.
        /*
        $ftso_no = $_REQUEST["ftso_no"];
        $ftso_code = RetRstField("SELECT ftcode FROM tstransaction_number WHERE ftmodule_code = 'sls001' and ftbranch_code = '{$_SESSION['strbranch']}'");

        $ftdo_code = RetRstField("SELECT ftcode FROM tstransaction_number WHERE ftmodule_code = 'TN005' and ftbranch_code = '{$_SESSION['strbranch']}'");
        $ftdo_no=getAutoNumber($_SESSION['strbranch'],'TN005','txdelivery_order_hdr','ftdo_no',$_REQUEST['fdtrans_date']);
        $ftdo_no = str_replace($ftso_code,$ftdo_code,$ftso_no);

        $ftsi_code = RetRstField("SELECT ftcode FROM tstransaction_number WHERE ftmodule_code = 'TN006' and ftbranch_code = '{$_SESSION['strbranch']}'");
        $ftsi_no=getAutoNumber($_SESSION['strbranch'],'TN006','txsales_invoice_hdr','ftsales_invoice_no',$_REQUEST['fdtrans_date']);
        ftsi_no = str_replace($ftso_code,$ftsi_code,$ftso_no);

        $ftstock_out_no=getAutoNumber($_SESSION['strbranch'],'inv005','txstock_out_hdr','ftstock_out_no',$_REQUEST['fdtrans_date']);

        $_REQUEST["fdtrans_date"]=getDateYmd($_REQUEST["fdtrans_date"]);

        $ssql = mysql_query("SELECT ftcustomer_name FROM tlcustomer WHERE ftcustomer_code='{$_REQUEST["ftcustomer_code"]}'");
        $r = mysql_fetch_assoc($ssql);

        $strSQL['Insert DO Hdr'][]="
              INSERT INTO txdelivery_order_hdr(
              ftbranch_code,
              ftdo_no, ftdo_type, ftso_no,
              ftcustomer_code, ftcustomer_name,ftship_address, fdtrans_date,
              ftdelivery_method, ftreference, ftnotes,
              fftotal_qty, fnstatus, ftsales_code,
              ftcreated_user, fdcreated_date
              ) SELECT
              '{$_SESSION['strbranch']}' AS ftbranch_code,
              '{$ftdo_no}' AS ftdo_no, '0' AS ftdo_type, '{$_REQUEST["ftso_no"]}' AS ftso_no,
              '{$_REQUEST["ftcustomer_code"]}' AS ftcustomer_code,'{$r['ftcustomer_name']}' AS ftcustomer_name,'{$_REQUEST["ftship_address"]}' AS ftship_address, '{$_REQUEST["fdtrans_date"]}' AS fdtrans_date,
              '{$_REQUEST["ftdelivery_method"]}' AS ftdelivery_method, '{$_REQUEST["ftreference"]}' AS ftreference, '{$_REQUEST["ftnotes"]}' AS ftnotes,
              '".CCur($_REQUEST["fftotal_qty"])."' AS fftotal_qty, '".CCur($_REQUEST["fnstatus"])."' AS fnstatus, '{$_REQUEST['ftsales_code']}',
              '{$_SESSION['username']}' AS ftcreated_user, now() AS fdcreated_date";

        $strSQL['Insert txsales_invoice_hdr'][]="
                 INSERT INTO txsales_invoice_hdr (
                 ftcurrency_code, fcrate,
                 ftbranch_code,ftsales_invoice_no,ftso_no,
                 fdtrans_date,ftdo_no,ftreff_no,ftvat_no,ftsales_code,
                 ftsales_type_code,ftwarehouse_code,ftcustomer_code,ftcustomer_name,
                 ftcontact_person,ftship_address, ftitem_type, ftphone,fddue_date,
                 fdwarranty_date,ftterm_payment,ftpayment_method,fnpayment_status,
                 ftdiscount_format,ffdiscount,fcdiscount,ffvat,
                 fcvat,fcother_cost,fcsub_amount,fcamount,
                 fcpayment_amount,fcoutstanding_amount,fcreturn_amount,ftnotes,
                 ftspecial_notes,fnstatus,fntotal,fnsaldo_awal,
                 ftprint_counter,fnprint_status,ftcreated_user,fdcreated_date,
                 ftmodified_user,fdmodified_date,fntax_code,fdvat_date)
                 VALUES (
                 '{$_REQUEST['ftcurrency_code']}', '".CCur($_REQUEST['fcrate'])."',
                 '{$_SESSION['strbranch']}','{$ftsi_no}','{$_REQUEST["ftso_no"]}',
                 '{$_REQUEST["fdtrans_date"]}','{$ftdo_no}','{$_REQUEST['ftreference']}','{$_REQUEST['ftvat_no']}','{$_REQUEST['ftsales_code']}',
                 '','','{$_REQUEST['ftcustomer_code']}','{$r['ftcustomer_name']}',
                 '','{$_REQUEST['ftship_address']}','1','','".Cdate($_REQUEST['fddue_date'])."',
                 now(),'{$_REQUEST['ftterm_payment']}','','1',
                 '','".CCur($_REQUEST['ffdiscount'])."','".CCur($_REQUEST['fcdiscount'])."','".CCur($_REQUEST['ffvat'])."',
                 '".CCur($_REQUEST['fcvat'])."','".CCur($_REQUEST['fcother_cost'])."','".CCur($_REQUEST['fcsub_amount'])."','".CCur($_REQUEST['fcamount'])."',
                 '".CCur($_REQUEST['fcpayment_amount'])."','".CCur($_REQUEST['fcoutstanding_amount'])."','".CCur($_REQUEST['fcreturn_amount'])."','{$_REQUEST['ftnotes']}',
                 '','{$_REQUEST['fnstatus']}','.".CCur($_REQUEST['fntotal'])."','0',
                 '','','{$_SESSION['username']}',now(),
                 '{$_SESSION['username']}',now(),'{$_REQUEST['fntax_code']}','".Cdate($_REQUEST['fdvat_date'])."')";

        $strSQL['Insert Stock Out Hdr'][]="
                INSERT INTO txstock_out_hdr
                (ftbranch_code, ftstock_out_no, ftcategory_code,ftreff_no,
                ftstock_out_company, ftdocument_no, fdtrans_date,
                fntotal_qty, ftnotes, fnstatus, ftcreated_user,
                fdcreated_date, ftmodified_user, fdmodified_date, ftstock_type_code)
                SELECT
                '{$_SESSION['strbranch']}' AS ftbranch_code, '{$ftstock_out_no}' AS ftstock_out_no, '4' AS ftcategory_code,'{$_REQUEST['ftreference']}',
                '{$_REQUEST['ftcustomer_code']}' AS  ftstock_out_company, '{$ftdo_no}' AS ftdocument_no,'{$_REQUEST['fdtrans_date']}' AS  fdtrans_date,
                '".CCur($_REQUEST['fftotal_qty'])."' AS fntotal_qty,'{$_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, 'inv' as ftstock_type_code";

        for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
          if((float)CCur($_REQUEST['fnqtyD0'][$j])>0){
                $rs=mysql_query("select ftitem_name,ftprint_name from tlstock_hdr where ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'");
                $r=mysql_fetch_assoc($rs);

                $strSQL['Insert DO Det'][]="
                       INSERT INTO txdelivery_order_det
                          (ftbranch_code, ftdo_no, fnindex,
                          ftitem_code, ftitem_name, ftprint_name,
                          ftwarehouse_code, ftlocation_code,
                          fnqty_available, fnqty_so,
                          fnqty, ftuom_code, fdtrans_date,
                          ftdescription,
                          fnqty_base,ffformula,
                          ftcreated_user, fdcreated_date,
                          ftmodified_user, fdmodified_date)
                        SELECT
                          '{$_SESSION['strbranch']}' AS ftbranch_code, '{$ftdo_no}' AS ftdo_no, '{$j}' AS fnindex,
                          '{$_REQUEST['ftitem_codeD0'][$j]}' AS ftitem_code, '{$r['ftitem_name']}' AS ftitem_name, '{$r['ftprint_name']}' AS ftprint_name,
                          '{$_REQUEST['ftwarehouse_codeD0'][$j]}' AS ftwarehouse_code, '{$_REQUEST['ftlocation_codeD0'][$j]}' AS ftlocation_code,
                          '".CCur($_REQUEST['fnqty_availableD0'][$j])."' AS fnqty_available, '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty_so,
                          '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty, '{$_REQUEST['ftuom_codeD0'][$j]}' AS ftuom_code, '{$_REQUEST["fdtrans_date"]}' AS  fdtrans_date,
                          '{$_REQUEST['ftdescriptionD0'][$j]}' AS ftdescription,
                          '".CCur($_REQUEST['fnqty_baseD0'][$j])."' AS fnqty_base, '".CCur($_REQUEST['ffformulaD0'][$j])."' AS ffformula,
                          '{$_SESSION['username']}' AS ftcreated_user, now() AS fdcreated_date,
                          '{$_SESSION['username']}' AS ftmodified_user, now() AS fdmodified_date";

                $strSQL['Insert txsales_invoice_det'][]="
                        INSERT INTO txsales_invoice_det  (
                        ftbranch_code,ftsales_invoice_no,fdtrans_date,
                        fnindex,ftitem_code,ftitem_name,ftstock_category_code,
                        ftbatch_code,ftwarehouse_code,ftlocation_code,fnqty_available,
                        fnqty_available_alt,fnqty,fnqty_base,fnqty_Retur,
                        fnqty_retur_base,ftuom_code,ftuom_code_alt,fcprice,
                        ffdiscount,fcdiscount,fcamount,fnwarranty_time,
                        fnwarranty_time_Code,fdwarranty_date,ffformula,ftcreated_user,
                        fdcreated_date,ftmodified_user,fdmodified_date,
                        ftprint_name)
                        VALUES (
                        '{$_SESSION['strbranch']}','{$ftsi_no}','{$_REQUEST['fdtrans_date']}',
                        '{$_REQUEST['fnindexD0'][$j]}','{$_REQUEST['ftitem_codeD0'][$j]}','{$r['ftitem_name']}','',
                        '','{$_REQUEST['ftwarehouse_codeD0'][$j]}','{$_REQUEST['ftlocation_codeD0'][$j]}','".CCur($_REQUEST['fnqty_availableD0'][$j])."',
                        '".CCur($_REQUEST['fnqty_available_altD0'][$j])."','".CCur($_REQUEST['fnqtyD0'][$j])."','".CCur($_REQUEST['fnqty_baseD0'][$j])."','',
                        '','{$_REQUEST['ftuom_codeD0'][$j]}','{$_REQUEST['ftuom_code_altD0'][$j]}','".CCur($_REQUEST['fcpriceD0'][$j])."',
                        '".CCur($_REQUEST['ffdiscountD0'][$j])."','".CCur($_REQUEST['fcdiscountD0'][$j])."','".CCur($_REQUEST['fcamountD0'][$j])."','',
                        '','','{$_REQUEST['ffformulaD0'][$j]}','{$_SESSION['username']}',
                        now(),'{$_SESSION['username']}',now(),'{$_REQUEST['ftprint_nameD'][$j]}')";

                $strSQL['Insert Stock Out Det'][]="
                     INSERT INTO txstock_out_det
                     (ftbranch_code, ftstock_out_no, fnindex, ftitem_code,
                     ftwarehouse_code, ftlocation_code, fnqty_available,
                     fnqty, ftuom_code, fnqty_base,ffformula,
                     ftcreated_user,
                     fdcreated_date, ftmodified_user)
                     SELECT
                     '{$_SESSION['strbranch']}' AS ftbranch_code,'{$ftstock_out_no}' AS ftstock_out_no, '{$j}' AS fnindex, '{$_REQUEST['ftitem_codeD0'][$j]}' AS ftitem_code,
                     '{$_REQUEST['ftwarehouse_codeD0'][$j]}' AS ftwarehouse_code, '{$_REQUEST['ftlocation_codeD0'][$j]}' AS ftlocation_code, '".CCur($_REQUEST['fnqty_availableD0'][$j])."' AS fnqty_available,
                     '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty, '{$_REQUEST['ftuom_codeD0'][$j]}' AS ftuom_code, '".CCur($_REQUEST['fnqty_baseD0'][$j])."' AS fnqty_base, '".CCur($_REQUEST['ffformulaD0'][$j])."' AS ffformula,
                     '{$_SESSION['username']}' AS ftcreated_user,
                     now() AS fdcreated_date, '{$_SESSION['username']}' AS ftmodified_user";

                $m=SaveStock('-',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_codeD0'][$j],$_REQUEST['ftlocation_codeD0'][$j],'',CCur($_REQUEST['fnqty_baseD0'][$j]),CCur($_REQUEST['fnqty_baseD0'][$j]));
                $strSQL=array_merge_recursive($strSQL,$m);

            }
        }

        $strSQL['Update txsles_order_hdr'][]="UPDATE txsales_order_hdr SET ftsales_invoice_no='{$ftsi_no}' WHERE ftso_no='{$_REQUEST['ftso_no']}'";
        */
        break;

    }

    return $strSQL;
 }


function UpdateData() {
    global $objAxpDB;
    $strSQL=array();
    switch($_REQUEST[mode]) {
        case "update":
            $strSQL['Update SO Hdr'][]="UPDATE txsales_order_hdr
                 SET fdvat_date='{$_REQUEST["fdvat_date"]}', ftvat_no='{$_REQUEST["ftvat_no"]}',
                     ftmodified_user = '{$_SESSION['username']}', fdmodified_date = now()
                 WHERE ftso_no='{$_REQUEST["ftso_no"]}'";
            break;

    }

    return $strSQL;
}


//close so
function UpdateDataLast() {

    global $objAxpDB;
    $strSQL=array();
    switch($_REQUEST[mode]) {
        case "close_so":
            // Update Detail
            $strSQL["Update txsales_order_hdr last"][] =
                        "UPDATE txsales_order_hdr
                            SET fnstatus = 5, ftmodified_user = '{$_SESSION['username']}', fdmodified_date = now()
                            WHERE ftso_no='{$_REQUEST['ftso_no']}' and fnstatus = 1";

        break;
    }
    return $strSQL;
}

function CancelData() {
    global $objAxpDB;
    //$objAxpDB->Allow_Cancel(false);
     $strSQL=array();
    switch($_REQUEST[mode]) {
    case "batal":
        $strSQL["Batal txsales_invoice_hdr"][]="UPDATE txsales_invoice_hdr SET fnstatus=9, ftmodified_user = '{$_SESSION['username']}', fdmodified_date = now() WHERE ftso_no='{$_REQUEST['ftso_no']}'";

        // KHUSUS ABRINDO, POSTED MASIH PENDING STOCK, APPROVED BARU POTONG STOCK
        if (($_REQUEST["fnstatus"] != 0) && ($_REQUEST["fnstatus"] != 1)) {
            for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
                $strSQL['Update txsales_order_det'][]="
                            UPDATE txsales_order_det
                            SET fnqty_accept_base=IFNULL(fnqty_accept_base,0)- ".(double)CCur($_REQUEST['fnqty_baseD0'][$j])."
                                ,fnqty_accept= IFNULL(fnqty_accept,0)- ".(double)CCur($_REQUEST['fnqtyD0'][$j])."
                                ,ftmodified_user = '{$_SESSION['username']}', fdmodified_date = now()
                            WHERE ftso_no='{$_REQUEST['ftso_no']}' AND ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}'  ";

                $m=SaveStock('+',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_codeD0'][$j],$_REQUEST['ftlocation_codeD0'][$j],'',CCur($_REQUEST['fnqty_baseD0'][$j]),CCur($_REQUEST['fnqty_baseD0'][$j]));
                $strSQL=array_merge_recursive($strSQL,$m);
            }
        }

        $ftdo_no = RetRstField("SELECT ftdo_no FROM txdelivery_order_hdr WHERE ftso_no = '{$_REQUEST['ftso_no']}' and ftbranch_code = '{$_SESSION['strbranch']}'");

        if ($ftdo_no != "") {
            $strSQL['Batal txstock_out_hdr'][]="UPDATE txstock_out_hdr SET fnstatus=9, ftmodified_user = '{$_SESSION['username']}', fdmodified_date = now() WHERE ftdocument_no='{$ftdo_no}' ";
        }

        $strSQL['Batal txdelivery_order_hdr'][]="UPDATE txdelivery_order_hdr SET fnstatus=9, ftmodified_user = '{$_SESSION['username']}', fdmodified_date = now() WHERE ftso_no='{$_REQUEST['ftso_no']}'";

        break;
    }

     return $strSQL;
 }


function CancelDataLast() {
    global $objAxpDB;
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "batal":

        $ssql="SELECT IFNULL(SUM(fnqty_accept),0) AS fnqty_accept
                FROM txsales_order_det WHERE ftso_no = '{$_REQUEST['ftso_no']}'
                GROUP BY ftso_no";
        $fnqty_accept = RetRstField($ssql);

        if ($fnqty_accept <= $_REQUEST['fftotal_qty']) {
            $fnreceive_status = 1;
        } else if ($fnqty_accept > $_REQUEST['fftotal_qty']){
            $fnreceive_status = 2;
        }

        $strSQL['Update txsales_order_hdr'][] = "UPDATE txsales_order_hdr set fnreceive_status = {$fnreceive_status} , ftmodified_user = '{$_SESSION['username']}', fdmodified_date = now()  WHERE ftso_no='{$_REQUEST['ftso_no']}'  ";

        break;

    }

    return $strSQL;
}

function FinalData() {
    global $objAxpDB;
    $objAxpDB->Allow_Final(false);
    $strSQL=array();
    switch($_REQUEST[mode]) {
        case "final":
            // temmporary solution for multiple click event
            $ftso_count = RetRstField("SELECT ftso_no FROM txdelivery_order_hdr WHERE ftso_no = '{$_REQUEST["ftso_no"]}' and ftbranch_code = '{$_SESSION['strbranch']}' and fnstatus !=3 and fnstatus != 9 ");

            if ($ftso_count == "" ) {

                $ftdo_code = RetRstField("SELECT ftcode FROM tstransaction_number WHERE ftmodule_code = 'TN005' and ftbranch_code = '{$_SESSION['strbranch']}'");
                $ftdo_no=getAutoNumber($_SESSION['strbranch'],'TN005','txdelivery_order_hdr','ftdo_no',$_REQUEST['fdtrans_date']);

                $ftsi_code = RetRstField("SELECT ftcode FROM tstransaction_number WHERE ftmodule_code = 'TN006' and ftbranch_code = '{$_SESSION['strbranch']}'");
                $ftsi_no=getAutoNumber($_SESSION['strbranch'],'TN006','txsales_invoice_hdr','ftsales_invoice_no',$_REQUEST['fdtrans_date']);

                $ftstock_out_no=getAutoNumber($_SESSION['strbranch'],'inv005','txstock_out_hdr','ftstock_out_no',$_REQUEST['fdtrans_date']);

                $_REQUEST["fdtrans_date"]=getDateYmd($_REQUEST["fdtrans_date"]);

                $ssql = mysql_query("SELECT ftcustomer_name FROM tlcustomer WHERE ftcustomer_code='{$_REQUEST["ftcustomer_code"]}'");
                $r = mysql_fetch_assoc($ssql);

                if ($_REQUEST['fntax_code'] == '0') { $fdvat_date = $_REQUEST["fdtrans_date"]; }

                $strSQL['Insert DO Hdr'][]="
                    INSERT INTO txdelivery_order_hdr(
                        ftbranch_code,
                        ftdo_no, ftdo_type, ftso_no,
                        ftcustomer_code, ftcustomer_name,ftship_address, fdtrans_date,
                        ftdelivery_method, ftreference, ftnotes,
                        fftotal_qty, fnstatus, ftsales_code,
                        ftcreated_user, fdcreated_date )
                    SELECT
                        '{$_SESSION['strbranch']}' AS ftbranch_code,
                        '{$ftdo_no}' AS ftdo_no, '0' AS ftdo_type, '{$_REQUEST["ftso_no"]}' AS ftso_no,
                        '".js_esc($_REQUEST["ftcustomer_code"])."' AS ftcustomer_code,'".js_esc($r['ftcustomer_name'])."' AS ftcustomer_name,'".js_esc($_REQUEST["ftship_address"])."' AS ftship_address, '{$_REQUEST["fdtrans_date"]}' AS fdtrans_date,
                        '{$_REQUEST["ftdelivery_method"]}' AS ftdelivery_method, '".js_esc($_REQUEST["ftreference"])."' AS ftreference, '".js_esc($_REQUEST["ftnotes"])."' AS ftnotes,
                        '".CCur($_REQUEST["fftotal_qty"])."' AS fftotal_qty, '".CCur($_REQUEST["fnstatus"])."' AS fnstatus, '{$_REQUEST['ftsales_code']}',
                        '{$_SESSION['username']}' AS ftcreated_user, now() AS fdcreated_date";

                $strSQL['Insert txsales_invoice_hdr'][]="
                        INSERT INTO txsales_invoice_hdr (
                            ftcurrency_code, fcrate,
                            ftbranch_code,ftsales_invoice_no,ftso_no,
                            fdtrans_date,ftdo_no,ftreff_no,ftvat_no,ftsales_code,
                            ftsales_type_code,ftwarehouse_code,ftcustomer_code,ftcustomer_name,
                            ftcontact_person,ftship_address, ftitem_type, ftphone,fddue_date,
                            fdwarranty_date,ftterm_payment,ftpayment_method,fnpayment_status,
                            ftdiscount_format,ffdiscount,fcdiscount,ffvat,
                            fcvat,fcother_cost,fcsub_amount,fcamount,
                            fcpayment_amount,fcoutstanding_amount,fcreturn_amount,ftnotes,
                            ftspecial_notes,fnstatus,fntotal,fnsaldo_awal,
                            ftprint_counter,fnprint_status,ftcreated_user,fdcreated_date,
                            ftmodified_user,fdmodified_date,fntax_code,fdvat_date)
                        VALUES (
                            '{$_REQUEST['ftcurrency_code']}', '".CCur($_REQUEST['fcrate'])."',
                            '{$_SESSION['strbranch']}','{$ftsi_no}','{$_REQUEST["ftso_no"]}',
                            '{$_REQUEST["fdtrans_date"]}','{$ftdo_no}','".js_esc($_REQUEST['ftreference'])."','{$_REQUEST['ftvat_no']}','{$_REQUEST['ftsales_code']}',
                            '','','{$_REQUEST['ftcustomer_code']}','".js_esc($r['ftcustomer_name'])."',
                            '','".js_esc($_REQUEST['ftship_address'])."','1','','".Cdate($_REQUEST['fddue_date'])."',
                            now(),'{$_REQUEST['ftterm_payment']}','','1',
                            '','".CCur($_REQUEST['ffdiscount'])."','".CCur($_REQUEST['fcdiscount'])."','".CCur($_REQUEST['ffvat'])."',
                            '".CCur($_REQUEST['fcvat'])."','".CCur($_REQUEST['fcother_cost'])."','".CCur($_REQUEST['fcsub_amount'])."','".CCur($_REQUEST['fcamount'])."',
                            '".CCur($_REQUEST['fcpayment_amount'])."','".CCur($_REQUEST['fcoutstanding_amount'])."','".CCur($_REQUEST['fcreturn_amount'])."','".js_esc($_REQUEST['ftnotes'])."',
                            '','{$_REQUEST['fnstatus']}','.".CCur($_REQUEST['fntotal'])."','0',
                            '','','{$_SESSION['username']}',now(),
                            '{$_SESSION['username']}',now(),'{$_REQUEST['fntax_code']}','{$fdvat_date}')";

                $strSQL['Insert Stock Out Hdr'][]="
                        INSERT INTO txstock_out_hdr
                            (ftbranch_code, ftstock_out_no, ftcategory_code,ftreff_no,
                            ftstock_out_company, ftdocument_no, fdtrans_date,
                            fntotal_qty, ftnotes, fnstatus, ftcreated_user,
                            fdcreated_date, ftmodified_user, fdmodified_date, ftstock_type_code)
                        SELECT
                            '{$_SESSION['strbranch']}' AS ftbranch_code, '{$ftstock_out_no}' AS ftstock_out_no, '4' AS ftcategory_code,'".js_esc($_REQUEST['ftreference'])."',
                            '{$_REQUEST['ftcustomer_code']}' AS  ftstock_out_company, '{$ftdo_no}' AS ftdocument_no,'{$_REQUEST['fdtrans_date']}' AS  fdtrans_date,
                            '".CCur($_REQUEST['fftotal_qty'])."' AS fntotal_qty,'".js_esc($_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, 'inv' as ftstock_type_code";

                for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                  if((float)CCur($_REQUEST['fnqtyD0'][$j])>0){
                        $rs=mysql_query("select ftitem_name from tlstock_hdr where ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'");
                        $r=mysql_fetch_assoc($rs);

                        $ftprint_name = $_REQUEST['ftprint_nameD0'][$j];

                        $strSQL['Insert DO Det'][]="
                                INSERT INTO txdelivery_order_det
                                    (ftbranch_code, ftdo_no, fnindex,
                                    ftitem_code, ftitem_name, ftprint_name,
                                    ftwarehouse_code, ftlocation_code,
                                    fnqty_available, fnqty_so,
                                    fnqty, ftuom_code, fdtrans_date,
                                    ftdescription,
                                    fnqty_base,ffformula,
                                    ftcreated_user, fdcreated_date,
                                    ftmodified_user, fdmodified_date)
                                SELECT
                                    '{$_SESSION['strbranch']}' AS ftbranch_code, '{$ftdo_no}' AS ftdo_no, '{$j}' AS fnindex,
                                    '".js_esc($_REQUEST['ftitem_codeD0'][$j])."' AS ftitem_code, '".js_esc($r['ftitem_name'])."' AS ftitem_name, '".js_esc($ftprint_name)."' AS ftprint_name,
                                    '{$_REQUEST['ftwarehouse_codeD0'][$j]}' AS ftwarehouse_code, '{$_REQUEST['ftlocation_codeD0'][$j]}' AS ftlocation_code,
                                    '".CCur($_REQUEST['fnqty_availableD0'][$j])."' AS fnqty_available, '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty_so,
                                    '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty, '{$_REQUEST['ftuom_codeD0'][$j]}' AS ftuom_code, '{$_REQUEST["fdtrans_date"]}' AS  fdtrans_date,
                                    '".js_esc($_REQUEST['ftdescriptionD0'][$j])."' AS ftdescription,
                                    '".CCur($_REQUEST['fnqty_baseD0'][$j])."' AS fnqty_base, '".CCur($_REQUEST['ffformulaD0'][$j])."' AS ffformula,
                                    '{$_SESSION['username']}' AS ftcreated_user, now() AS fdcreated_date,
                                    '{$_SESSION['username']}' AS ftmodified_user, now() AS fdmodified_date";

                        $strSQL['Insert txsales_invoice_det'][]="
                                INSERT INTO txsales_invoice_det  (
                                    ftbranch_code,ftsales_invoice_no,fdtrans_date,
                                    fnindex,ftitem_code,ftitem_name,ftstock_category_code,
                                    ftbatch_code,ftwarehouse_code,ftlocation_code,fnqty_available,
                                    fnqty_available_alt,fnqty,fnqty_base,fnqty_Retur,
                                    fnqty_retur_base,ftuom_code,ftuom_code_alt,fcprice,
                                    ffdiscount,fcdiscount,fcamount,fnwarranty_time,
                                    fnwarranty_time_Code,fdwarranty_date,ffformula,ftcreated_user,
                                    fdcreated_date,ftmodified_user,fdmodified_date,
                                    ftprint_name,ftdescription)
                                VALUES (
                                    '{$_SESSION['strbranch']}','{$ftsi_no}','{$_REQUEST['fdtrans_date']}',
                                    '{$_REQUEST['fnindexD0'][$j]}','{$_REQUEST['ftitem_codeD0'][$j]}','".js_esc($r['ftitem_name'])."','',
                                    '','{$_REQUEST['ftwarehouse_codeD0'][$j]}','{$_REQUEST['ftlocation_codeD0'][$j]}','".CCur($_REQUEST['fnqty_availableD0'][$j])."',
                                    '".CCur($_REQUEST['fnqty_available_altD0'][$j])."','".CCur($_REQUEST['fnqtyD0'][$j])."','".CCur($_REQUEST['fnqty_baseD0'][$j])."','',
                                    '','{$_REQUEST['ftuom_codeD0'][$j]}','{$_REQUEST['ftuom_code_altD0'][$j]}','".CCur($_REQUEST['fcpriceD0'][$j])."',
                                    '".CCur($_REQUEST['ffdiscountD0'][$j])."','".CCur($_REQUEST['fcdiscountD0'][$j])."','".CCur($_REQUEST['fcamountD0'][$j])."','',
                                    '','','{$_REQUEST['ffformulaD0'][$j]}','{$_SESSION['username']}',now(),'{$_SESSION['username']}',now(),
                                    '".js_esc($ftprint_name)."','".js_esc($_REQUEST['ftdescriptionD0'][$j])."')";

                        $strSQL['Insert Stock Out Det'][]="
                            INSERT INTO txstock_out_det
                                (ftbranch_code, ftstock_out_no, fnindex, ftitem_code,
                                ftwarehouse_code, ftlocation_code, fnqty_available,
                                fnqty, ftuom_code, fnqty_base,ffformula,
                                ftcreated_user, fdcreated_date, ftmodified_user, fdmodified_date,
                                ftprint_name,ftdescription)
                            SELECT
                                '{$_SESSION['strbranch']}' AS ftbranch_code,'{$ftstock_out_no}' AS ftstock_out_no, '{$j}' AS fnindex, '".js_esc($_REQUEST['ftitem_codeD0'][$j])."' AS ftitem_code,
                                '{$_REQUEST['ftwarehouse_codeD0'][$j]}' AS ftwarehouse_code, '{$_REQUEST['ftlocation_codeD0'][$j]}' AS ftlocation_code, '".CCur($_REQUEST['fnqty_availableD0'][$j])."' AS fnqty_available,
                                '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty, '{$_REQUEST['ftuom_codeD0'][$j]}' AS ftuom_code, '".CCur($_REQUEST['fnqty_baseD0'][$j])."' AS fnqty_base, '".CCur($_REQUEST['ffformulaD0'][$j])."' AS ffformula,
                                '{$_SESSION['username']}' AS ftcreated_user, now() AS fdcreated_date, '{$_SESSION['username']}' AS ftmodified_user, now() AS fdmodified_date,
                                '".js_esc($ftprint_name)."','".js_esc($_REQUEST['ftdescriptionD0'][$j])."'";

                        $m=SaveStock('-',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_codeD0'][$j],$_REQUEST['ftlocation_codeD0'][$j],'',CCur($_REQUEST['fnqty_baseD0'][$j]),CCur($_REQUEST['fnqty_baseD0'][$j]));
                        $strSQL=array_merge_recursive($strSQL,$m);

                    }
                }

                $strSQL['Update txsales_order_hdr'][]="UPDATE txsales_order_hdr SET fnstatus='2', ftsales_invoice_no='{$ftsi_no}', ftmodified_user = '{$_SESSION['username']}', fdmodified_date = now() WHERE ftso_no='{$_REQUEST['ftso_no']}'";
            }

            break;

    }

    return $strSQL;
}


function RevisiData() {

    global $objAxpDB;
    //$objAxpDB->Allow_Revisi(false);
    $strSQL=array();
    switch($_REQUEST[mode]) {
        case "simpan_revisi":
            $ssql1="SELECT IFNULL(MAX(ftso_no),'')  AS M
                    FROM txsales_order_hdr
                    WHERE ftso_no LIKE '{$_REQUEST['ftso_no']}-%'";
            $no=RetRstField($ssql1);
            if($no!="")
            {
                $no1=explode("-",$no);
                $no=(int)$no1[1]+1;
                $str1="-{$no}";
            }
            else
            {
                $str1="-1";
            }

            $ftso_no_new="{$_REQUEST['ftso_no']}{$str1}";

            $strSQL["Revisi"][]="UPDATE txsales_order_hdr
                                    SET fnstatus=3,ftso_no='{$ftso_no_new}',
                                        ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                                    WHERE ftso_no='{$_REQUEST['ftso_no']}'";

            $strSQL["Revisi"][]="UPDATE txsales_order_det
                                    SET ftso_no='{$ftso_no_new}',
                                        ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                                    where ftso_no='{$_REQUEST['ftso_no']}'";

            // KHUSUS ABRINDO (Revisi SO = Revisi SI = Revisi DO)
            // Auto revisi SI, Get No Revisi
            // $ftsi_no=RetRstField("SELECT ftsales_invoice_no FROM txsales_invoice_hdr WHERE ftso_no = '{$_REQUEST['ftso_no']}' ");
            //
            // if ($ftsi_no) {
            //
            //     $ssql1="SELECT IFNULL(MAX(ftsales_invoice_no),'')  AS M
            //             FROM txsales_invoice_hdr
            //             WHERE ftsales_invoice_no LIKE '{$ftsi_no}-%'";
            //
            //     $no=RetRstField($ssql1);
            //
            //     if($no!="")
            //     {
            //         $no1=explode("-",$no);
            //         $no=(int)$no1[1]+1;
            //         $str1="-{$no}";
            //     }
            //     else
            //     {
            //         $str1="-1";
            //     }
            //
            //     $ftsi_no_new="{$ftsi_no}{$str1}";
            //
            //     $strSQL["Revisi"][]="UPDATE txsales_invoice_hdr
            //                             SET fnstatus=3,ftsales_invoice_no='{$ftsi_no_new}',
            //                                 ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
            //                             WHERE ftsales_invoice_no='{$ftsi_no}'";
            //     $strSQL["Revisi"][]="UPDATE txsales_invoice_det
            //                             SET ftsales_invoice_no='{$ftsi_no_new}',
            //                                 ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
            //                             where ftsales_invoice_no='{$ftsi_no}'";

                // Auto Revisi DO, Get No Revisi
                // $ftdo_no=RetRstField("SELECT ftdo_no FROM txdelivery_order_hdr WHERE ftso_no = '{$_REQUEST['ftso_no']}' ");
                //
                // $ssql1="SELECT IFNULL(MAX(ftdo_no),'')  AS M
                //         FROM txdelivery_order_hdr
                //         WHERE ftdo_no LIKE '{$ftdo_no}-%'";
                // $no=RetRstField($ssql1);
                // if($no!="")
                // {
                //     $no1=explode("-",$no);
                //     $no=(int)$no1[1]+1;
                //     $str1="-{$no}";
                // }
                // else
                // {
                //     $str1="-1";
                // }
                //
                // $ftdo_no_new="{$ftdo_no}{$str1}";
                //
                // // Pengembalian Stock transaksi lama
                // $rs=mysql_query("SELECT * FROM txdelivery_order_det WHERE ftdo_no='{$ftdo_no}'");
                // while($r=mysql_fetch_assoc($rs)) {
                //     $m=SaveStock('+',$r['ftitem_code'],'',$r['ftwarehouse_code'],$r['ftlocation_code'],'',$r['fnqty_base'],$r['fnqty_base']);
                //     $strSQL=array_merge_recursive($strSQL,$m);
                // }
                //
                // $strSQL["Revisi"][]="UPDATE txdelivery_order_hdr
                //                         SET fnstatus=3,ftso_no='{$ftso_no_new}',ftdo_no = '{$ftdo_no_new}',
                //                             ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                //                         WHERE ftdo_no='{$ftdo_no}'";
                // $strSQL["Revisi"][]="UPDATE txdelivery_order_det
                //                         SET ftdo_no = '{$ftdo_no_new}',
                //                             ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                //                         WHERE ftdo_no='{$ftdo_no}'";

                // Auto Revisi Stock Out, Get No Revisi
                // $ftstock_out_no=RetRstField("SELECT ftstock_out_no FROM txstock_out_hdr WHERE ftdocument_no = '{$ftdo_no}'");
                //
                // if ($ftdo_no != "") {
                //
                //     $ssql1="SELECT IFNULL(MAX(ftstock_out_no),'')  AS M
                //             FROM txstock_out_hdr
                //             WHERE ftstock_out_no LIKE '{$ftstock_out_no}-%'";
                //     $no=RetRstField($ssql1);
                //     if($no!="")
                //     {
                //         $no1=explode("-",$no);
                //         $no=(int)$no1[1]+1;
                //         $str1="-{$no}";
                //     }
                //     else
                //     {
                //         $str1="-1";
                //     }
                //
                //     $ftstock_out_no_new="{$ftstock_out_no}{$str1}";
                //
                //     $strSQL["Revisi"][]="UPDATE txstock_out_hdr
                //                             SET fnstatus=3,ftstock_out_no='{$ftstock_out_no_new}',ftdocument_no = '{$ftdo_no_new}',
                //                                 ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                //                             WHERE ftstock_out_no='{$ftstock_out_no}'";
                //     $strSQL["Revisi"][]="UPDATE txstock_out_det
                //                             SET ftstock_out_no = '{$ftstock_out_no_new}',
                //                                 ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                //                             WHERE ftstock_out_no='{$ftstock_out_no}'";
                //
                // }

                // $fdtrans_date=getDateYmd($_REQUEST["fdtrans_date"]);
                //
                // if ($_REQUEST['fntax_code'] == '0') { $fdvat_date = $fdtrans_date; }
                //
                // $ssql = mysql_query("SELECT ftcustomer_name FROM tlcustomer WHERE ftcustomer_code='{$_REQUEST["ftcustomer_code"]}'");
                // $r = mysql_fetch_assoc($ssql);
                //
                // $strSQL['Insert DO Hdr'][]="
                //     INSERT INTO txdelivery_order_hdr(
                //         ftbranch_code,
                //         ftdo_no, ftdo_type, ftso_no,
                //         ftcustomer_code, ftcustomer_name,ftship_address, fdtrans_date,
                //         ftdelivery_method, ftreference, ftnotes,
                //         fftotal_qty, fnstatus, ftsales_code,
                //         ftcreated_user, fdcreated_date )
                //     SELECT
                //         '{$_SESSION['strbranch']}' AS ftbranch_code,
                //         '{$ftdo_no}' AS ftdo_no, '0' AS ftdo_type, '{$_REQUEST["ftso_no"]}' AS ftso_no,
                //         '".js_esc($_REQUEST["ftcustomer_code"])."' AS ftcustomer_code,'".js_esc($r['ftcustomer_name'])."' AS ftcustomer_name,'".js_esc($_REQUEST["ftship_address"])."' AS ftship_address, '{$fdtrans_date}' AS fdtrans_date,
                //         '{$_REQUEST["ftdelivery_method"]}' AS ftdelivery_method, '".js_esc($_REQUEST["ftreference"])."' AS ftreference, '".js_esc($_REQUEST["ftnotes"])."' AS ftnotes,
                //         '".CCur($_REQUEST["fftotal_qty"])."' AS fftotal_qty, '1' AS fnstatus, '{$_REQUEST['ftsales_code']}',
                //         '{$_SESSION['username']}' AS ftcreated_user, now() AS fdcreated_date";
                //
                // $strSQL['Insert txsales_invoice_hdr'][]="
                //          INSERT INTO txsales_invoice_hdr (
                //             ftcurrency_code, fcrate,
                //             ftbranch_code,ftsales_invoice_no,ftso_no,fntax_code,
                //             fdtrans_date,ftdo_no,ftreff_no,ftvat_no,ftsales_code,
                //             ftsales_type_code,ftwarehouse_code,ftcustomer_code,ftcustomer_name,
                //             ftcontact_person,ftship_address, ftitem_type, ftphone,fddue_date,
                //             fdwarranty_date,ftterm_payment,ftpayment_method,fnpayment_status,
                //             ftdiscount_format,ffdiscount,fcdiscount,ffvat,
                //             fcvat,fcother_cost,fcsub_amount,fcamount,
                //             fcpayment_amount,fcoutstanding_amount,fcreturn_amount,ftnotes,
                //             ftspecial_notes,fnstatus,fntotal,fnsaldo_awal,
                //             ftprint_counter,fnprint_status,ftcreated_user,fdcreated_date,
                //             ftmodified_user,fdmodified_date,fdvat_date)
                //         VALUES (
                //             '{$_REQUEST['ftcurrency_code']}',
                //             '".CCur($_REQUEST['fcrate'])."',
                //             '{$_SESSION['strbranch']}',
                //             '{$ftsi_no}','{$_REQUEST["ftso_no"]}',
                //             '{$_REQUEST["fntax_code"]}',
                //             '{$fdtrans_date}',
                //             '{$ftdo_no}',
                //             '".js_esc($_REQUEST['ftreference'])."',
                //             '{$_REQUEST['ftvat_no']}',
                //             '{$_REQUEST['ftsales_code']}',
                //             '','','{$_REQUEST['ftcustomer_code']}','".js_esc($r['ftcustomer_name'])."',
                //             '','".js_esc($_REQUEST['ftship_address'])."','1','','".Cdate($_REQUEST['fddue_date'])."',
                //             now(),'{$_REQUEST['ftterm_payment']}','','1',
                //             '','".CCur($_REQUEST['ffdiscount'])."','".CCur($_REQUEST['fcdiscount'])."','".CCur($_REQUEST['ffvat'])."',
                //             '".CCur($_REQUEST['fcvat'])."','".CCur($_REQUEST['fcother_cost'])."','".CCur($_REQUEST['fcsub_amount'])."','".CCur($_REQUEST['fcamount'])."',
                //             '".CCur($_REQUEST['fcpayment_amount'])."','".CCur($_REQUEST['fcoutstanding_amount'])."','".CCur($_REQUEST['fcreturn_amount'])."','".js_esc($_REQUEST['ftnotes'])."',
                //             '','1','.".CCur($_REQUEST['fntotal'])."','0',
                //             '','','{$_SESSION['username']}',now(),
                //             '{$_SESSION['username']}',now(),'{$fdvat_date}')";
                //
                // $strSQL['Insert Stock Out Hdr'][]="
                //         INSERT INTO txstock_out_hdr
                //             (ftbranch_code, ftstock_out_no, ftcategory_code,ftreff_no,
                //             ftstock_out_company, ftdocument_no, fdtrans_date,
                //             fntotal_qty, ftnotes, fnstatus, ftcreated_user,
                //             fdcreated_date, ftmodified_user, fdmodified_date, ftstock_type_code)
                //         SELECT
                //             '{$_SESSION['strbranch']}' AS ftbranch_code, '{$ftstock_out_no}' AS ftstock_out_no, '4' AS ftcategory_code,'".js_esc($_REQUEST['ftreference'])."',
                //             '{$_REQUEST['ftcustomer_code']}' AS  ftstock_out_company, '{$ftdo_no}' AS ftdocument_no,'{$fdtrans_date}' AS  fdtrans_date,
                //             '".CCur($_REQUEST['fftotal_qty'])."' AS fntotal_qty,'".js_esc($_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, 'inv' as ftstock_type_code";
                //
                // for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                //     if((float)CCur($_REQUEST['fnqtyD0'][$j])>0){
                //         $rs=mysql_query("select ftitem_name from tlstock_hdr where ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'");
                //         $r=mysql_fetch_assoc($rs);
                //
                //         $ftprint_name = $_REQUEST['ftprint_nameD0'][$j];
                //
                //         $strSQL['Insert DO Det'][]="
                //             INSERT INTO txdelivery_order_det
                //                 (ftbranch_code, ftdo_no, fnindex,
                //                 ftitem_code, ftitem_name, ftprint_name,
                //                 ftwarehouse_code, ftlocation_code,
                //                 fnqty_available, fnqty_so,
                //                 fnqty, ftuom_code, fdtrans_date,
                //                 ftdescription,
                //                 fnqty_base,ffformula,
                //                 ftcreated_user, fdcreated_date,
                //                 ftmodified_user, fdmodified_date)
                //             SELECT
                //                 '{$_SESSION['strbranch']}' AS ftbranch_code, '{$ftdo_no}' AS ftdo_no, '{$j}' AS fnindex,
                //                 '".js_esc($_REQUEST['ftitem_codeD0'][$j])."' AS ftitem_code, '".js_esc($r['ftitem_name'])."' AS ftitem_name, '".js_esc($ftprint_name)."' AS ftprint_name,
                //                 '{$_REQUEST['ftwarehouse_codeD0'][$j]}' AS ftwarehouse_code, '{$_REQUEST['ftlocation_codeD0'][$j]}' AS ftlocation_code,
                //                 '".CCur($_REQUEST['fnqty_availableD0'][$j])."' AS fnqty_available, '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty_so,
                //                 '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty, '{$_REQUEST['ftuom_codeD0'][$j]}' AS ftuom_code, '{$fdtrans_date}' AS  fdtrans_date,
                //                 '".js_esc($_REQUEST['ftdescriptionD0'][$j])."' AS ftdescription,
                //                 '".CCur($_REQUEST['fnqty_baseD0'][$j])."' AS fnqty_base, '".CCur($_REQUEST['ffformulaD0'][$j])."' AS ffformula,
                //                 '{$_SESSION['username']}' AS ftcreated_user, now() AS fdcreated_date,
                //                 '{$_SESSION['username']}' AS ftmodified_user, now() AS fdmodified_date";
                //
                //         $strSQL['Insert txsales_invoice_det'][]="
                //             INSERT INTO txsales_invoice_det  (
                //                 ftbranch_code,ftsales_invoice_no,fdtrans_date,
                //                 fnindex,ftitem_code,ftitem_name,ftstock_category_code,
                //                 ftbatch_code,ftwarehouse_code,ftlocation_code,fnqty_available,
                //                 fnqty_available_alt,fnqty,fnqty_base,fnqty_Retur,
                //                 fnqty_retur_base,ftuom_code,ftuom_code_alt,fcprice,
                //                 ffdiscount,fcdiscount,fcamount,fnwarranty_time,
                //                 fnwarranty_time_Code,fdwarranty_date,ffformula,ftcreated_user,
                //                 fdcreated_date,ftmodified_user,fdmodified_date,
                //                 ftprint_name,ftdescription)
                //             VALUES (
                //                 '{$_SESSION['strbranch']}','{$ftsi_no}','{$fdtrans_date}',
                //                 '{$_REQUEST['fnindexD0'][$j]}','{$_REQUEST['ftitem_codeD0'][$j]}','".js_esc($r['ftitem_name'])."','',
                //                 '','{$_REQUEST['ftwarehouse_codeD0'][$j]}','{$_REQUEST['ftlocation_codeD0'][$j]}','".CCur($_REQUEST['fnqty_availableD0'][$j])."',
                //                 '".CCur($_REQUEST['fnqty_available_altD0'][$j])."','".CCur($_REQUEST['fnqtyD0'][$j])."','".CCur($_REQUEST['fnqty_baseD0'][$j])."','',
                //                 '','{$_REQUEST['ftuom_codeD0'][$j]}','{$_REQUEST['ftuom_code_altD0'][$j]}','".CCur($_REQUEST['fcpriceD0'][$j])."',
                //                 '".CCur($_REQUEST['ffdiscountD0'][$j])."','".CCur($_REQUEST['fcdiscountD0'][$j])."','".CCur($_REQUEST['fcamountD0'][$j])."','',
                //                 '','','{$_REQUEST['ffformulaD0'][$j]}','{$_SESSION['username']}',now(),'{$_SESSION['username']}',now(),
                //                 '".js_esc($ftprint_name)."','".js_esc($_REQUEST['ftdescriptionD0'][$j])."')";
                //
                //         $strSQL['Insert Stock Out Det'][]="
                //             INSERT INTO txstock_out_det
                //                 (ftbranch_code, ftstock_out_no, fnindex, ftitem_code,
                //                 ftwarehouse_code, ftlocation_code, fnqty_available,
                //                 fnqty, ftuom_code, fnqty_base,ffformula,
                //                 ftcreated_user, fdcreated_date, ftmodified_user,fdmodified_date,
                //                 ftprint_name,ftdescription)
                //             SELECT
                //                 '{$_SESSION['strbranch']}' AS ftbranch_code,'{$ftstock_out_no}' AS ftstock_out_no, '{$j}' AS fnindex, '".js_esc($_REQUEST['ftitem_codeD0'][$j])."' AS ftitem_code,
                //                 '{$_REQUEST['ftwarehouse_codeD0'][$j]}' AS ftwarehouse_code, '{$_REQUEST['ftlocation_codeD0'][$j]}' AS ftlocation_code, '".CCur($_REQUEST['fnqty_availableD0'][$j])."' AS fnqty_available,
                //                 '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty, '{$_REQUEST['ftuom_codeD0'][$j]}' AS ftuom_code, '".CCur($_REQUEST['fnqty_baseD0'][$j])."' AS fnqty_base, '".CCur($_REQUEST['ffformulaD0'][$j])."' AS ffformula,
                //                 '{$_SESSION['username']}' AS ftcreated_user,now() AS fdcreated_date, '{$_SESSION['username']}' AS ftmodified_user, now() AS fdmodified_date,
                //                 '".js_esc($ftprint_name)."','".js_esc($_REQUEST['ftdescriptionD0'][$j])."'";
                //
                //     }
            //    }
            
            // }

            break;

    }

    return $strSQL;
}


function RevisiDataLast() {
    global $objAxpDB;
    //$objAxpDB->Allow_Revisi(false);
    $strSQL=array();
    switch($_REQUEST[mode]) {
        case "simpan_revisi":
            $ftsi_no=RetRstField("SELECT ftsales_invoice_no FROM txsales_invoice_hdr WHERE ftso_no = '{$_REQUEST['ftso_no']}' ");

            if ($ftsi_no) {
                $strSQL['Update txsales_order_hdr'][]="UPDATE txsales_order_hdr SET fnstatus='2', ftsales_invoice_no='{$ftsi_no}', ftmodified_user = '{$_SESSION['username']}', fdmodified_date = now() WHERE ftso_no='{$_REQUEST['ftso_no']}'";
            }

            break;

    }

    return $strSQL;
}


function RevisiDataAfter() {
    global $objAxpDB;
    $strSQL=array();

    switch($_REQUEST[mode]) {
        case "simpan_revisi":
            // KHUSUS ABRINDO (SI auto DO)
            $ftsi_no=RetRstField("SELECT ftsales_invoice_no FROM txsales_invoice_hdr WHERE ftso_no = '{$_REQUEST['ftso_no']}' ");

            if ($ftsi_no) {
                for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                    if((float)CCur($_REQUEST['fnqty_baseD0'][$j])>0){
                        $m=SaveStock('-',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_codeD0'][$j],$_REQUEST['ftlocation_codeD0'][$j],'',CCur($_REQUEST['fnqty_baseD0'][$j]),CCur($_REQUEST['fnqty_baseD0'][$j]));
                        $strSQL=array_merge_recursive($strSQL,$m);
                    }
                }
            }

            break;

    }

    return $strSQL;
}
