<?php

    if($_REQUEST['mode']=="update_qty_limit") {
            $_REQUEST['mode']="update";
    }
    //echo ($_REQUEST['mode']);
    
    switch($_REQUEST[mode]) {
        case "simpan":
            if($_REQUEST['ftsupplier_code']=="") {
                echo "<script>
                        alert('Kode Supplier tidak valid. Harap dicek kembali.');
                        window.history.back();
                    </script>";
                die("");
            }
            break;
    
        case "revisi":
            $ssql="SELECT fnid FROM txstock_in_hdr a WHERE ftdocument_no='{$_REQUEST['ftpo_no']}' AND fnstatus !=3 AND fnstatus != 9  ";
            $rs=mysql_query($ssql);
            
            if(RetRstField($ssql)) {
                echo "<script>
                        alert('No. PO sudah terpakai pada stock masuk.');
                        window.history.back();
                    </script>";
                die("");
            }
            
            $ssql="SELECT fnid FROM txpurchase_invoice_hdr a WHERE ftpo_no='{$_REQUEST['ftpo_no']}'  AND fnstatus !=3 AND fnstatus != 9 ";
            $rs=mysql_query($ssql);
            
            if(RetRstField($ssql)) {
                echo "<script>
                       alert('No. PO sudah terpakai pada invoice.');
                        window.history.back();
                    </script>";
                die("");
            }
            break; 
    }

    function ValBatalPO() {
        // check apakah sudah ada stock masuk utk PO ini
        $ssql="select ftstock_in_no
                from txstock_in_hdr
                where fnstatus!=3 AND fnstatus!=9 and ftdocument_no='{$_REQUEST['ftpo_no']}'";

        $rs=mysql_query($ssql) or die ("query error {$ssql}");
        $ftstock_in_no="";
        while($r=mysql_fetch_assoc($rs)) {
           $ftstock_in_no.="- {$r['ftstock_in_no']}\\n";
        }
        
        if($ftstock_in_no!="") {
             $massage=" Stock masuk berikut harus dibatalkan terlebih dahulu \\n";
             $massage.="---------------------------------------------------------\\n";
             $massage.=$ftstock_in_no;
        }
        
        if($massage!="") {
          echo "<script>
                alert('{$massage}');
                window.history.back();
            </script>";
            die("");
        }

        // check apakah sudah ada stock keluar outsource utk PO ini
        $ssql="select ftstock_out_no from txstock_out_hdr where fnstatus!=3 AND fnstatus<>9 and ftdocument_no='{$_REQUEST['ftpo_no']}' union all
                select ftstock_out_no from txstock_out_hdr_bahan_baku where fnstatus!=3 AND fnstatus<>9 and ftdocument_no='{$_REQUEST['ftpo_no']}' union all
                select ftstock_out_no from txstock_out_hdr_setengah_jadi where fnstatus!=3 AND fnstatus<>9 and ftdocument_no='{$_REQUEST['ftpo_no']}' union all
                select ftstock_out_no from txstock_out_hdr_sparepart where fnstatus!=3 AND fnstatus<>9 and ftdocument_no='{$_REQUEST['ftpo_no']}'";

        $rs=mysql_query($ssql) or die ("query error {$ssql}");
        $ftstock_out_no="";
        $massage="";
        while($r=mysql_fetch_assoc($rs)) {
           $ftstock_out_no.="- {$r['ftstock_out_no']}\\n";

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

        // check apakah sudah ada purchase invoice utk PO ini
        $ssql="select ftpurchase_invoice_no
                from txpurchase_invoice_hdr
                where  fnstatus!=3 AND fnstatus<>9 and fnpo_type='0' and ftpo_no='{$_REQUEST['ftpo_no']}'";

        $rs=mysql_query($ssql) or die ("query error {$ssql}");
        $ftpurchase_invoice_no="";
        $massage="";
        while($r=mysql_fetch_assoc($rs)) {
           $ftpurchase_invoice_no.="- {$r['ftpurchase_invoice_no']}\\n";

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

    }
    
    function ValRevisiPO_1() {
        $ssql="select ftpurchase_invoice_no
                from txpurchase_invoice_hdr
                where fnstatus!=3 AND fnstatus<>9 and fnpo_type='0' and ftpo_no='{$_REQUEST['ftpo_no']}'";

        $rs=mysql_query($ssql) or die ("query error {$ssql}");
        $ftpurchasE_invoice_no="";
        $massage="";
        while($r=mysql_fetch_assoc($rs)) {
           $ftpurchasE_invoice_no.="- {$r['ftpurchasE_invoice_no']}\\n";

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

    function UpdateData() {
        global $objAxpDB;
        $strSQL=array();
        switch($_REQUEST[mode]) {
        case "update":
           $strSQL["Delete txpurchase_order_det"][] ="DELETE FROM txpurchase_order_det WHERE ftpo_no='{$_REQUEST['ftpo_no']}'";
           $strSQL["Delete txpurchase_order_schedule_delivery"][] ="DELETE FROM txpurchase_order_schedule_delivery WHERE ftpo_no='{$_REQUEST['ftpo_no']}'";

           for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
             $strSQL["Insert txpurchase_order_det"][] ="INSERT INTO txpurchase_order_det (
                      ftbranch_code, ftpo_no,
                      fnindex, ftitem_code,
                      ftwarehouse_code,ftlocation_code,
                      fnqty, ffformula,
                      fnqty_base, fnqty_limit,
                      fnqty_accept, fnqty_accept_base,
                      ftuom_code, fcprice,
                      ffdiscount, fcdiscount,
                      fcamount, fnvat,
                      ftdescription, fdclosing_date, fnstatus,
                      ftcreated_user,fdcreated_date,
                      ftmodified_user, fdmodified_date
                    ) SELECT
                      '{$_SESSION['strbranch']}' AS ftbranch_code, '{$_REQUEST['ftpo_no']}' AS ftpo_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['fnqtyD0'][$j])."' AS fnqty, '".CCur($_REQUEST['ffformulaD0'][$j])."' AS ffformula,
                      '".CCur($_REQUEST['fnqty_baseD0'][$j])."' AS fnqty_base, '".CCur($_REQUEST['fnqty_limitD0'][$j])."' AS fnqty_limit,
                      '".CCur(0)."' AS fnqty_accept, '".CCur(0)."' AS fnqty_accept_base,
                      '{$_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['fnvatD0'][$j])."' AS fnvat,
                      '{$_REQUEST['ftdescriptionD0'][$j]}' AS ftdescription, '".getDateYmd($_REQUEST['fdclosing_dateD0'][$j])."' AS fdclosing_date, '".CCur($_REQUEST['fnstatusD0'][$j])."' 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['fnindexD1']); $j++) {
              $strSQL["Insert txpurchase_order_schedule_delivery"][] ="INSERT INTO txpurchase_order_schedule_delivery (
                      ftbranch_code, ftpo_no,
                      fnindex, fdschedule_delivery_date, fnlock,
                      ftcreated_user,fdcreated_date,
                      ftmodified_user, fdmodified_date
                    ) SELECT
                      '{$_SESSION['strbranch']}' AS ftbranch_code, '{$_REQUEST['ftpo_no']}' AS ftpo_no,
                      '{$j}' AS fnindex, '".getDateYmd($_REQUEST['fdschedule_delivery_dateD1'][$j])."' AS fdschedule_delivery_date, '".CCur($_REQUEST['fnlockD1'][$j])."' AS fnlock,
                      '{$_SESSION['username']}' AS ftcreated_user, now() AS fdcreated_date,
                      '{$_SESSION['username']}' AS ftmodified_user, now() AS fdmodified_date";
           } */
           
           $strSQL["Update txpurchase_order_hdr"][] ="UPDATE txpurchase_order_hdr SET fddelivery_date=fdtrans_date where ftpo_no='{$_REQUEST['ftpo_no']}'";
			
        break;
        case "update_schedule_delivery":
            $strSQL["Delete txpurchase_order_schedule_delivery"][] ="DELETE FROM txpurchase_order_schedule_delivery WHERE ftpo_no='{$_REQUEST['ftpo_no']}'";

            for($j=1; $j<count($_REQUEST['fnindexD1']); $j++) {
                $strSQL["Insert txpurchase_order_schedule_delivery"][] =
                            "INSERT INTO txpurchase_order_schedule_delivery (
                              ftbranch_code, ftpo_no,
                              fnindex, fdschedule_delivery_date, fnlock,
                              ftcreated_user,fdcreated_date,
                              ftmodified_user, fdmodified_date
                            ) SELECT
                              '{$_SESSION['strbranch']}' AS ftbranch_code, '{$_REQUEST['ftpo_no']}' AS ftpo_no,
                              '{$j}' AS fnindex, '".getDateYmd($_REQUEST['fdschedule_delivery_dateD1'][$j])."' AS fdschedule_delivery_date, '".CCur($_REQUEST['fnlockD1'][$j])."' AS fnlock,
                              '{$_SESSION['username']}' AS ftcreated_user, now() AS fdcreated_date,
                              '{$_SESSION['username']}' AS ftmodified_user, now() AS fdmodified_date";

            }
        break;
        
        case "close_po":
           // Update Detail
           for($j=1; $j<count($_REQUEST['fnindexD1']); $j++) {
                $strSQL["Update txpurchase_order_det"][] =
                            "UPDATE txpurchase_order_det
                                SET fnstatus = 5, ftmodified_user = '{$_SESSION['username']}', fdmodified_date = now()
                                WHERE ftpo_no='{$_REQUEST['ftpo_no']}' and fnstatus = 1";
           }
           
        break;
        }
        return $strSQL;
    }

    function UpdateDataLast() {
        global $objAxpDB;
        $strSQL=array();
        switch($_REQUEST[mode]) {
            case "update":
                $strSQL["Update txpurchase_order_hdr 2"][] =
                            "UPDATE txpurchase_order_hdr SET fddelivery_date=fdtrans_date
                            WHERE ftpo_no='{$_REQUEST['ftpo_no']}'";

                // Status PR yang tidak jadi digunakan dikembalikan ke posted dan no_po di clear agar bisa digunakan di po lain
                $strSQL["Update txpurchase_request_hdr"][] =
                            "UPDATE txpurchase_request_det a
                                LEFT JOIN txpurchase_order_det b ON a.ftpo_no = b.ftpo_no AND a.ftitem_code = b.ftitem_code
                                SET a.fnstatus = 1, a.ftpo_no = ''
                                WHERE  a.fnstatus = 5 AND b.ftpo_no IS NULL AND a.ftpo_no IS NOT NULL AND a.ftpo_no <> ''";
                
            break;

            case "close_po":
                // Update Status PO menjadi close
                $strSQL["Update txpurchase_order_hdr 2"][] =
                                "UPDATE txpurchase_order_hdr
                                    SET fnstatus = 5, ftmodified_user = '{$_SESSION['username']}', fdmodified_date = now()
                                    WHERE ftpo_no='{$_REQUEST['ftpo_no']}'";
            break;
        }
        return $strSQL;
    }


function SaveData() {
    global $objAxpDB;
    $strSQL=array();
    switch($_REQUEST[mode]) {
        case "simpan":
            // Update Item status Purchase Request
            $strSQL['Update txpurchase_request_det'][]=
                    "UPDATE txpurchase_request_det
                        SET fnstatus=5, ftpo_no = '{$_REQUEST['ftpo_no']}',
                        ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                        WHERE (ftpr_no in ('0'{$_REQUEST['ftproses_pr']})) and (ftitem_code in ('0'{$_REQUEST['ftitem_code_pr']})) ";
            
            for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                if (!empty($_REQUEST['ftitem_codeD0'][$j])) {
                    $ssql_b = mysql_query("SELECT * FROM tlbuying_price WHERE ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}' AND ftcurrency_code='{$_REQUEST['ftcurrency_code']}'");
                    $r = mysql_num_rows($ssql_b);
                    
                    if ($r > 0) {
                        $strSQL['UPDATE tlbuying_price'][]="
                                UPDATE tlbuying_price 
                                SET fcold_buying_price=fcbuying_price,
                                    fcbuying_price='".CCur($_REQUEST['fcpriceD0'][$j])."',
                                    ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                                WHERE ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}' AND
                                    ftcurrency_code='{$_REQUEST['ftcurrency_code']}' AND
                                    fcbuying_price<>".CCur($_REQUEST['fcpriceD0'][$j])."";
                                
                        // Update IDR Price           
                        if ($_REQUEST['ftcurrency_code'] != 'IDR') {        
                            $strSQL['UPDATE tlbuying_price'][]="
                                    UPDATE tlbuying_price 
                                    SET fcold_buying_price=fcbuying_price,
                                        fcbuying_price='".CCur($_REQUEST['fcpriceD0'][$j]) * CCur($_REQUEST['fcrate'])."',
                                        ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                                    WHERE ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}' AND
                                        ftcurrency_code='IDR' AND
                                        fcbuying_price<>".CCur($_REQUEST['fcpriceD0'][$j]) * CCur($_REQUEST['fcrate'])."";   
                        }                
                    }
                    else {
                        if ($_REQUEST['fnpo_type3']=='2'){
                            $ssql_i = mysql_query("SELECT ftitem_name FROM tlstock_hdr_sparepart WHERE ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}'");
                            $d_i = mysql_fetch_assoc($ssql_i);
                            $ftstock_type_code = "invsp";
                        }
                        else if ($_REQUEST['fnpo_type3']=='3'){
                            $ssql_i = mysql_query("SELECT ftitem_name FROM tlstock_hdr WHERE ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}'");
                            $d_i = mysql_fetch_assoc($ssql_i);
                            $ftstock_type_code = "inv";
                        }
                        
                        $strSQL['INSERT tlbuying_price'][]="INSERT tlbuying_price (
                                ftbranch_code, ftitem_code, ftitem_name, 
                                ftsupplier_code, ftstock_type_code, ftuom_code, 
                                fcbuying_price, ftcurrency_code,
                                ftcreated_user, fdcreated_date, ftmodified_user, fdmodified_date)
                                VALUES (
                                '{$_SESSION['strbranch']}', '{$_REQUEST['ftitem_codeD0'][$j]}', '{$d_i['ftitem_name']}',
                                '{$_REQUEST['ftsupplier_code']}', '{$ftstock_type_code}', '{$_REQUEST['ftuom_code']}',
                                '".CCur($_REQUEST['fcpriceD0'][$j])."', '{$_REQUEST['ftcurrency_code']}',
                                '{$_SESSION['username']}', now(),'{$_SESSION['username']}', now())";
                             
                        // Update IDR Price           
                        if ($_REQUEST['ftcurrency_code'] != 'IDR') {  
                            $strSQL['INSERT tlbuying_price'][]="INSERT tlbuying_price (
                                    ftbranch_code, ftitem_code, ftitem_name, 
                                    ftsupplier_code, ftstock_type_code, ftuom_code, 
                                    fcbuying_price, ftcurrency_code,
                                    ftcreated_user, fdcreated_date, ftmodified_user, fdmodified_date)
                                    VALUES (
                                    '{$_SESSION['strbranch']}', '{$_REQUEST['ftitem_codeD0'][$j]}', '{$d_i['ftitem_name']}',
                                    '{$_REQUEST['ftsupplier_code']}', '{$ftstock_type_code}', '{$_REQUEST['ftuom_code']}',
                                    '".CCur($_REQUEST['fcpriceD0'][$j]) * CCur($_REQUEST['fcrate']) ."', 'IDR',
                                    '{$_SESSION['username']}', now(),'{$_SESSION['username']}', now())";
                        }                
                    }
                }
                
                // Update Harga Jual           
                /* $ssql="UPDATE tlselling_price SET
                        fcold_selling_price=fcselling_price,
                        fcselling_price='".CCur($_REQUEST['fcselling_priceD0'][$j])."',
                        ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                        WHERE ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}' AND
                        ftcurrency_code='IDR' AND
                        fcselling_price<>".CCur($_REQUEST['fcselling_priceD0'][$j])."";      
                $strSQL['UPDATE tlselling_price'][]=$ssql; */

            }
            break;
    }   
    return $strSQL;

}

function SaveDataLast() {
    global $objAxpDB;
    // $objAxpDB->Allow_Save(false);
    $strSQL=array();
    switch($_REQUEST[mode]) {
        case "simpan":
     
            $strSQL["Insert txpurchase_order_hdr Delivery"][] ="update txpurchase_order_hdr set fddelivery_date=fdtrans_date where ftpo_no='{$_REQUEST['ftpo_no']}'";
            $strSQL["Update txpurchase_order_det 1"][] ="update txpurchase_order_det set fdclosing_date='".CDate($_REQUEST['fdtrans_date'])."',fnstatus=1 where ftpo_no='{$_REQUEST['ftpo_no']}'";
            if($_REQUEST['fnpo_type3']=='1') {
                    for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                        $strSQL['Update DET'][]="
                                UPDATE txpurchase_order_det
                                    SET ftdescription2='{$_REQUEST['hidden_ftitem_codeD0'][$j]}'
                                    WHERE ftpo_no='{$_REQUEST['ftpo_no']}' AND
                                            fnindex='{$_REQUEST['fnindexD0'][$j]}'";
                    }
            }

            // Status PR yang tidak jadi digunakan dikembalikan ke posted dan no_po di clear agar bisa digunakan di po lain
            $strSQL["Update txpurchase_request_det last"][] ="
                    UPDATE txpurchase_request_det a
                    LEFT JOIN txpurchase_order_det b ON a.ftpo_no = b.ftpo_no AND a.ftitem_code = b.ftitem_code
                    SET a.fnstatus = 1, a.ftpo_no = ''
                    WHERE  a.fnstatus = 5 AND b.ftpo_no IS NULL AND a.ftpo_no IS NOT NULL AND a.ftpo_no <> ''";
            break;            
    }
    return $strSQL;
}

/* Tidak digunakan di ABRINDO 
function FinalData() {
    global $objAxpDB;
    $strSQL=array();
    switch($_REQUEST[mode]) {
      case "final":
       $strSQL["Delete txpurchase_order_det"][] ="DELETE FROM txpurchase_order_det WHERE ftpo_no='{$_REQUEST['ftpo_no']}'";
       $strSQL["Delete txpurchase_order_schedule_delivery"][] ="DELETE FROM txpurchase_order_schedule_delivery WHERE ftpo_no='{$_REQUEST['ftpo_no']}'";

       for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
         $strSQL["Insert txpurchase_order_det"][] ="INSERT INTO txpurchase_order_det (
                  ftbranch_code, ftpo_no,
                  fnindex, ftitem_code,
                  fnqty, ffformula,
                  fnqty_base, fnqty_limit,
                  fnqty_accept, fnqty_accept_base,
                  ftuom_code, fcprice,
                  ffdiscount, fcdiscount,
                  fcamount, fnvat,
                  ftdescription, fdclosing_date, fnstatus,
                  ftcreated_user,fdcreated_date,
                  ftmodified_user, fdmodified_date
                ) SELECT
                  '{$_SESSION['strbranch']}' AS ftbranch_code, '{$_REQUEST['ftpo_no']}' AS ftpo_no,
                  '{$j}' AS fnindex, '{$_REQUEST['ftitem_codeD0'][$j]}' AS ftitem_code,
                  '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty, '".CCur($_REQUEST['ffformulaD0'][$j])."' AS ffformula,
                  '".CCur($_REQUEST['fnqty_baseD0'][$j])."' AS fnqty_base, '".CCur($_REQUEST['fnqty_limitD0'][$j])."' AS fnqty_limit,
                  '".CCur(0)."' AS fnqty_accept, '".CCur(0)."' AS fnqty_accept_base,
                  '{$_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['fnvatD0'][$j])."' AS fnvat,
                  '{$_REQUEST['ftdescriptionD0'][$j]}' AS ftdescription, '".getDateYmd($_REQUEST['fdclosing_dateD0'][$j])."' AS fdclosing_date, '".CCur($_REQUEST['fnstatusD0'][$j])."' AS fnstatus,
                  '{$_SESSION['username']}' AS ftcreated_user, now() AS fdcreated_date,
                  '{$_SESSION['username']}' AS ftmodified_user, now() AS fdmodified_date";

         $strSQL['Update txpurchase_request_det'][]="UPDATE txpurchase_request_det a INNER JOIN txpurchase_request_hdr b ON a.ftpr_no=b.ftpr_no SET a.fnstatus=5 WHERE a.ftpr_no in ('0'{$_REQUEST['ftproses_pr']}) AND a.ftitem_code in ('0'{$_REQUEST['ftitem_code_pr']}) AND a.fnstatus=1 AND b.fnstatus IN (0, 1, 2)";
       }

      
       for($j=1; $j<count($_REQUEST['fnindexD1']); $j++) {
          $strSQL["Insert txpurchase_order_schedule_delivery"][] ="INSERT INTO txpurchase_order_schedule_delivery (
                  ftbranch_code, ftpo_no,
                  fnindex, fdschedule_delivery_date, fnlock,
                  ftcreated_user,fdcreated_date,
                  ftmodified_user, fdmodified_date
                ) SELECT
                  '{$_SESSION['strbranch']}' AS ftbranch_code, '{$_REQUEST['ftpo_no']}' AS ftpo_no,
                  '{$j}' AS fnindex, '".getDateYmd($_REQUEST['fdschedule_delivery_dateD1'][$j])."' AS fdschedule_delivery_date, '".CCur($_REQUEST['fnlockD1'][$j])."' AS fnlock,
                  '{$_SESSION['username']}' AS ftcreated_user, now() AS fdcreated_date,
                  '{$_SESSION['username']}' AS ftmodified_user, now() AS fdmodified_date";
       }

        $strSQL['Update txpurchase_order_hdr'][]="UPDATE txpurchase_order_hdr SET fnstatus=1 WHERE ftpo_no='{$_REQUEST["ftpo_no"]}'";

        // Status PR yang tidak jadi digunakan dikembalikan ke posted dan no_po di clear agar bisa digunakan di po lain
        $strSQL["Update txpurchase_request_det"][] =
                    "UPDATE txpurchase_request_det a
                        LEFT JOIN txpurchase_order_det b ON a.ftpo_no = b.ftpo_no AND a.ftitem_code = b.ftitem_code
                        SET a.fnstatus = 1, a.ftpo_no = ''
                        WHERE  a.fnstatus = 5 AND b.ftpo_no IS NULL AND a.ftpo_no IS NOT NULL AND a.ftpo_no <> ''";

        break;
    }
    return $strSQL;
}

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

        // Update Status PR ke Approve kalau semua item sudah status closed
        $ssql="SELECT b.ftpr_no, COUNT(a.fnid) AS total_item, COUNT(c.fnindex) AS total_item_closed
                    FROM txpurchase_request_det a INNER JOIN txpurchase_request_hdr b ON a.ftpr_no=b.ftpr_no
                    LEFT JOIN txpurchase_request_det c ON c.ftpr_no=a.ftpr_no AND c.fnindex=a.fnindex AND c.fnstatus = 5
                    WHERE b.fnstatus <> 2 AND b.fnstatus <> 9
                    GROUP BY b.ftpr_no
                    HAVING total_item = total_item_closed ";
        $mysql=mysql_query($ssql);

        while($r=mysql_fetch_assoc($mysql)){
            $strSQL['Update txpurchase_request_hdr'][]="UPDATE txpurchase_request_hdr SET fnstatus=2 WHERE ftpr_no='{$r["ftpr_no"]}'";
        }

        break;
    }
    return $strSQL;
}
*/

function CancelData() {
    ValBatalPO();
    ValRevisiPO_1();

    global $objAxpDB;
    $objAxpDB->Allow_Cancel(true);
    $strSQL=array();

    switch($_REQUEST[mode]) {
        case "batal":
            $strSQL["Final txpurchase_order_hdr"][] =
                        "UPDATE txpurchase_order_hdr
                                SET ftmodified_user='{$_SESSION['username']}',fdmodified_date=now(),
                                        fnreceipt_status=9,
                                        fnstatus=9 ,
                                        fdcancel_date='".CDate($_REQUEST['fdcancel_date'])."',
                                        ftmemo_no_cancel='{$_REQUEST['ftmemo_no_cancel']}',
                                        ftreason_cancel='{$_REQUEST['ftreason_cancel']}'
                                WHERE ftpo_no='{$_REQUEST['ftpo_no']}'";

            // Update Status PR ke Posted kalau ada item yg batal
            $strSQL['Update txpurchase_request_hdr'][]="UPDATE txpurchase_request_hdr a INNER JOIN txpurchase_request_det b ON a.ftpr_no=b.ftpr_no
                    SET a.fnstatus=1 , a.ftmodified_user='{$_SESSION['username']}', a.fdmodified_date=now()
                    WHERE a.fnstatus = 2 AND b.ftpo_no='{$_REQUEST['ftpo_no']}'";

            $strSQL["Batal txpurchase_request_det"][] =
                        "update txpurchase_request_det
                                set ftmodified_user='{$_SESSION['username']}',fdmodified_date=now(),
                                    fnstatus=1, ftpo_no=''
                                WHERE ftpo_no='{$_REQUEST['ftpo_no']}'";

        break;
    }
    return $strSQL;
}

function RevisiData() {
        ValBatalPO();
        ValRevisiPO_1();
        global $objAxpDB;
        //$objAxpDB->Allow_Revisi(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
            case "simpan_revisi":
                $ssql1="SELECT IFNULL(MAX(ftpo_no),'')  AS M
                        FROM txpurchase_order_hdr
                        WHERE ftpo_no LIKE '{$_REQUEST['ftpo_no']}-%'";
                $no=RetRstField($ssql1);
                if($no!="") 
                {
                        $no1=explode("-",$no);
                        $no=(int)$no1[1]+1;
                        $str1="-{$no}";
                }
                else 
                {
                        $str1="-1";
                }
                $ftpo_no_new="{$_REQUEST['ftpo_no']}{$str1}";
                
                $strSQL["Revisi"][]="UPDATE txpurchase_order_hdr
                                        SET fnstatus=3,ftpo_no='{$ftpo_no_new}',
                                            ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                                        WHERE ftpo_no='{$_REQUEST['ftpo_no']}'";
                $strSQL["Revisi"][]="UPDATE txpurchase_order_det
                                        SET ftpo_no='{$ftpo_no_new}',
                                            ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                                        where ftpo_no='{$_REQUEST['ftpo_no']}'";
                $strSQL["Revisi"][]="UPDATE txpurchase_order_schedule_delivery
                                        SET ftpo_no='{$ftpo_no_new}',
                                            ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                                        where ftpo_no='{$_REQUEST['ftpo_no']}'";
                              
                // Update Harga Beli & Harga Jual     
                for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {                        
                    $strSQL['UPDATE tlbuying_price'][]="
                            UPDATE tlbuying_price 
                            SET fcold_buying_price=fcbuying_price,
                                fcbuying_price='".CCur($_REQUEST['fcpriceD0'][$j])."',
                                ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                            WHERE ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}' AND
                                    ftcurrency_code='{$_REQUEST['ftcurrency_code']}' AND
                                    fcbuying_price<>".CCur($_REQUEST['fcpriceD0'][$j])."";
                                    
                    // Update IDR Price           
                    if ($_REQUEST['ftcurrency_code'] != 'IDR') {        
                        $strSQL['UPDATE tlbuying_price'][]="
                                UPDATE tlbuying_price 
                                SET fcold_buying_price=fcbuying_price,
                                    fcbuying_price='".CCur($_REQUEST['fcpriceD0'][$j]) * CCur($_REQUEST['fcrate'])."',
                                    ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                                WHERE ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}' AND
                                    ftcurrency_code='IDR' AND
                                    fcbuying_price<>".CCur($_REQUEST['fcpriceD0'][$j]) * CCur($_REQUEST['fcrate'])."";   
                    }
                    
                    // Update Harga Jual         
                    /*
                    $ssql="UPDATE tlselling_price SET
                            fcold_selling_price=fcselling_price,
                            fcselling_price='".CCur($_REQUEST['fcselling_priceD0'][$j])."',
                            ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                            WHERE ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}' AND
                            ftcurrency_code='IDR' AND
                            fcselling_price<>".CCur($_REQUEST['fcselling_priceD0'][$j])."";      
                    $strSQL['UPDATE tlselling_price'][]=$ssql; */

                }        
                                        
                // KHUSUS ABRINDO (Revisi PO = Revisi PB = Revisi PI)                
                // Auto revisi PB
                /* $ftsm_no=RetRstField("SELECT ftstock_in_no FROM txstock_in_hdr WHERE ftdocument_no = '{$_REQUEST['ftpo_no']}' AND fnstatus !=3 AND fnstatus != 9 ");                        
                
                $ssql1="SELECT IFNULL(MAX(ftstock_in_no),'')  AS M
                        FROM txstock_in_hdr
                        WHERE ftstock_in_no LIKE '{$ftsm_no}-%'";
                $no=RetRstField($ssql1);
                if($no!="") 
                {
                        $no1=explode("-",$no);
                        $no=(int)$no1[1]+1;
                        $str1="-{$no}";
                }
                else 
                {
                        $str1="-1";
                }
                
                $ftsm_no_new="{$ftsm_no}{$str1}";
                         
                if ($ftsm_no) {
                    // Pengembalian Stock transaksi lama              
                    $rs=mysql_query("SELECT * FROM txstock_in_det WHERE ftstock_in_no='{$ftsm_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 txstock_in_hdr
                                            SET fnstatus=3,ftdocument_no='{$ftpo_no_new}',ftstock_in_no = '{$ftsm_no_new}',
                                                ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                                            WHERE ftstock_in_no='{$ftsm_no}'"; 
                    $strSQL["Revisi"][]="UPDATE txstock_in_det
                                            SET ftstock_in_no = '{$ftsm_no_new}',
                                                ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                                            WHERE ftstock_in_no='{$ftsm_no}'";        

                    $ssql="INSERT INTO txstock_in_hdr
                                (
                                    ftbranch_code,ftstock_in_no, ftstock_type_code, ftcategory_code,
                                    ftstock_in_company,ftdocument_no,fdschedule_delivery_date,
                                    fdtrans_date, ftreff_no, 
                                    ftcurrency_code,fcrate,
                                    ftchecked_user, ftnotes,  fntotal_qty, fnstatus,ftcreated_user,
                                    fdcreated_date,ftmodified_user,fdmodified_date
                                )
                                    SELECT
                                    '{$_SESSION['strbranch']}' AS ftbranch_code,'{$ftsm_no}' AS ftstock_in_no,'inv' AS ftstock_type_code,'0' AS ftcategory_code,
                                    '{$_REQUEST['ftsupplier_code']}' AS ftstock_in_company,'{$_REQUEST['ftpo_no']}' AS ftdocument_no,'".CDate($_REQUEST['fdtrans_date'])."' AS fdschedule_delivery_date,
                                    '".CDate($_REQUEST['fdtrans_date'])."' AS fdtrans_date,'{$_REQUEST['ftreference']}' AS ftreff_no,
                                    '{$_REQUEST['ftcurrency_code']}' AS ftcurrency_code,'".CCur($_REQUEST['fcrate'])."' AS fcrate,
                                    '' AS ftchecked_user,'{$_REQUEST['ftnotes']}' AS ftnotes,'0' AS fntotal_qty,'{$_REQUEST['fnstatus']}'  AS fnstatus,'{$_SESSION['username']}' AS ftcreated_user,
                                    NOW() AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date
                            ";
                    $strSQL['INSERT txstock_in_hdr'][]=$ssql;
                    if($_REQUEST['DIRECT_EXECUTE_SQL']==1) {
                        mysql_query($ssql) or die("SQL ERROR :{$ssql}");
                    }

                    // Internal Setting
                    $PBAutoPI = RetRstField("SELECT ftvalue FROM tssys_setting WHERE ftsys_code = 'PUR01'");

                    // auto insert purchase invoice
                    if ($PBAutoPI) {
                        // Auto revisi PI
                        $ftpi_no=RetRstField("SELECT ftpurchase_invoice_no FROM txpurchase_invoice_hdr WHERE ftpo_no = '{$_REQUEST['ftpo_no']}' AND fnstatus !=3 AND fnstatus != 9 ");                        
                        
                        $ssql1="SELECT IFNULL(MAX(ftpurchase_invoice_no),'')  AS M
                                FROM txpurchase_invoice_hdr
                                WHERE ftpurchase_invoice_no LIKE '{$ftpi_no}-%'";
                        $no=RetRstField($ssql1);
                        if($no!="") 
                        {
                                $no1=explode("-",$no);
                                $no=(int)$no1[1]+1;
                                $str1="-{$no}";
                        }
                        else 
                        {
                                $str1="-1";
                        }

                        $ftpi_no_new="{$ftpi_no}{$str1}";

                        $strSQL["Revisi"][]="UPDATE txpurchase_invoice_hdr
                                                SET fnstatus=3,ftpo_no='{$ftpo_no_new}',ftpurchase_invoice_no = '{$ftpi_no_new}',
                                                    ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                                                WHERE ftpurchase_invoice_no='{$ftpi_no}'"; 
                        $strSQL["Revisi"][]="UPDATE txpurchase_invoice_det
                                                SET ftpurchase_invoice_no = '{$ftpi_no_new}',
                                                    ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                                                WHERE ftpurchase_invoice_no='{$ftpi_no}'";                           

                        $fddue_date=RetRstField("SELECT DATE_ADD('".CDate($_REQUEST['fdtrans_date'])."',INTERVAL {$_REQUEST['ftterm_payment']} DAY)");
                        $ftsupplier_name=RetRstField("select ftsupplier_name from tlsupplier where ftsupplier_code='".$_REQUEST['ftsupplier_code']."'");

                        $ssql="INSERT INTO txpurchase_invoice_hdr
                                    (
                                        ftbranch_code,ftpurchase_invoice_no, ftstock_in_no, ftpo_no, fnpo_type, 
                                        ftsupplier_code,ftsupplier_name,fntax_code,
                                        fdtrans_date, fddue_date, ftterm_payment, ftcurrency_code,fcrate,
                                        fcsub_amount,fcvat,fcamount,
                                        fcpayment_amount,fcoutstanding_amount,fcreturn_amount,
                                        ffvat, ftnotes, fnstatus,fnpayment_status,
                                        ftcreated_user,fdcreated_date,ftmodified_user,fdmodified_date
                                    )
                                        SELECT
                                        '{$_SESSION['strbranch']}' AS ftbranch_code,'{$ftpi_no}' AS ftpurchase_invoice_no,'{$ftsm_no}' AS ftstock_in_no,'{$_REQUEST['ftpo_no']}' AS ftpo_no,'{$_REQUEST['fnpo_type']}' AS fnpo_type,
                                        '{$_REQUEST['ftsupplier_code']}' AS ftsupplier_code,'{$ftsupplier_name}' AS ftsupplier_name,'{$_REQUEST['fntax_code']}' AS fntax_code,
                                        '".CDate($_REQUEST['fdtrans_date'])."' AS fdtrans_date,'{$fddue_date}' AS fddue_date,'{$_REQUEST['ftterm_payment']}' AS ftterm_payment,'{$_REQUEST['ftcurrency_code']}' AS ftcurrency_code,'".CCur($_REQUEST['fcrate'])."' AS fcrate,
                                        '".CCur($_REQUEST['fcsub_amount'])."' AS fcsub_amount,'".CCur($_REQUEST['fcvat'])."' AS fcvat,'".CCur($_REQUEST['fcamount'])."' AS fcamount,
                                        '0' AS fcpayment_amount,'".CCur($_REQUEST['fcamount'])."' AS fcoutstanding_amount,'0' AS fcreturn_amount,
                                        '".CCur($_REQUEST['ffvat'])."' AS ffvat,'{$_REQUEST['ftnotes']}' AS ftnotes,'{$_REQUEST['fnstatus']}'  AS fnstatus,'1' AS fnpayment_status,
                                        '{$_SESSION['username']}' AS ftcreated_user,NOW() AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date
                                ";

                        $strSQL['INSERT txpurchase_invoice_hdr'][]=$ssql;
                        if($_REQUEST['DIRECT_EXECUTE_SQL']==1) {
                            mysql_query($ssql) or die("SQL ERROR :{$ssql}");
                        }
                    } 

                    $jx=0;
                    for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                        $jx++;
                        $ftitem_name=RetRstField("select ftitem_name from tlstock_hdr where ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}'");

                        $ssql="INSERT INTO txstock_in_det
                                    (
                                        ftbranch_code,ftstock_in_no,fnindex, ftitem_code,ftitem_name,ftdescription,
                                        ftwarehouse_code, ftlocation_code, fnqtypo,fnqtypo_base,fnqtysisapo,
                                        fnqtysisapo_base, fnqty,fnqty_base, ftuom_code,ffformula,
                                        fnindex_po,fnqty_reject,fcprice,fcselling_price,ftcreated_user,fdcreated_date,
                                        ftmodified_user,fdmodified_date
                                    )
                                        SELECT  '{$_SESSION['strbranch']}' AS ftbranch_code,'{$ftsm_no}' AS ftstock_in_no,'{$jx}' AS fnindex,'{$_REQUEST['ftitem_codeD0'][$j]}' AS ftitem_code,'{$ftitem_name}' AS ftitem_name, '{$_REQUEST['ftdescriptionD0'][$j]}' AS ftdescription,
                                        '{$_REQUEST['ftwarehouse_codeD0'][$j]}' AS ftwarehouse_code,'{$_REQUEST['ftlocation_codeD0'][$j]}' AS ftlocation_code,'".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqtypo,'".CCur($_REQUEST['fnqty_baseD0'][$j])."' AS fnqtypo_base,'".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqtysisapo,
                                        '".CCur($_REQUEST['fnqty_baseD0'][$j])."' AS fnqtysisapo_base,'".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty,'".CCur($_REQUEST['fnqty_baseD0'][$j])."' AS fnqty_base,'{$_REQUEST['ftuom_codeD0'][$j]}' AS ftuom_code,'".CCur($_REQUEST['ffformulaD0'][$j])."' AS ffformula,
                                        '{$_REQUEST['fnindexD0'][$j]}' AS fnindex_po,'0' AS fnqty_reject,'".CCur($_REQUEST['fcpriceD0'][$j])."' AS fcprice,'".CCur($_REQUEST['fcselling_priceD0'][$j])."' AS fcselling_price,'{$_SESSION['username']}' AS ftcreated_user,now() AS fdcreated_date,
                                        '{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date";

                        $strSQL['INSERT txstock_in_det'][]=$ssql;

                        if($_REQUEST['DIRECT_EXECUTE_SQL']==1) {
                            mysql_query($ssql) or die("SQL ERROR :{$ssql}");
                        }

                        // auto insert txpurchase_invoice det
                        if ($PBAutoPI) {
                            $fcamount = CCur($_REQUEST['fnqtyD0'][$j]) * CCur($_REQUEST['fcpriceD0'][$j]);
                            $fcprice_cogs = CCur($_REQUEST['fcpriceD0'][$j]);

                            $ssql="INSERT INTO txpurchase_invoice_det
                                        (
                                            ftbranch_code,ftpurchase_invoice_no,ftstock_in_no,fnindex, ftitem_code,ftitem_name,
                                            fnqty, fnqty_base, ftuom_code,ffformula,fcprice, 
                                            ffdiscount, fcdiscount, fcamount, fcprice_cogs,
                                            ftcreated_user,fdcreated_date,ftmodified_user,fdmodified_date
                                        )
                                            SELECT  '{$_SESSION['strbranch']}' AS ftbranch_code,'{$ftpi_no}' AS ftpurchase_invoice_no,'{$ftsm_no}' AS ftstock_in_no,'{$jx}' AS fnindex,'{$_REQUEST['ftitem_codeD0'][$j]}' AS ftitem_code,'{$ftitem_name}' AS ftitem_name,
                                            '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty,'".CCur($_REQUEST['fnqty_baseD0'][$j])."' AS fnqty_base,'{$_REQUEST['ftuom_codeD0'][$j]}' AS ftuom_code,'".CCur($_REQUEST['ffformulaD0'][$j])."' AS ffformula,'{$_REQUEST['fcpriceD0'][$j]}' AS fcprice,
                                            '".CCur($_REQUEST['ffdiscountD0'][$j])."' AS ffdiscount,'".CCur($_REQUEST['fcdiscountD0'][$j])."' AS fcdiscount,'{$fcamount}' AS fcamount,'{$fcprice_cogs}' AS fcprice_cogs,
                                            '{$_SESSION['username']}' AS ftcreated_user,now() AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date";

                            $strSQL['INSERT txpurchase_invoice_det'][]=$ssql;

                            if($_REQUEST['DIRECT_EXECUTE_SQL']==1) {
                                mysql_query($ssql) or die("SQL ERROR :{$ssql}");
                            }
                        }

                        // Update Harga Jual           
                        $ssql="UPDATE tlselling_price SET
                                fcold_selling_price=fcselling_price,
                                fcselling_price='".CCur($_REQUEST['fcselling_priceD0'][$j])."',
                                ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                                WHERE ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}' AND
                                ftcurrency_code='IDR' AND
                                fcselling_price<>".CCur($_REQUEST['fcselling_priceD0'][$j])."";      
                        $strSQL['UPDATE tlselling_price'][]=$ssql;

                    }				

                    
                
                } */
                
                break;
        }
    return $strSQL;
}

function RevisiDataLast() {
    global $objAxpDB;
    $strSQL=array();
    
    switch($_REQUEST[mode]) {
        case "simpan_revisi":
            if($_REQUEST['fnreceipt_status']==1) {
                $strSQL["Insert txpurchase_order_hdr Delivery"][] ="update txpurchase_order_hdr set fddelivery_date=fdtrans_date where ftpo_no='{$_REQUEST['ftpo_no']}'";
                $strSQL["Update txpurchase_order_det 1"][] ="update txpurchase_order_det set fdclosing_date='".CDate($_REQUEST['fdtrans_date'])."',fnstatus=1 where ftpo_no='{$_REQUEST['ftpo_no']}'";
            
                
                /*
                if($_REQUEST['fnpo_type3']==1) {
                    for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                        $strSQL['Update DET'][]="UPDATE txpurchase_order_det
                                                    SET ftdescription2='{$_REQUEST['hidden_ftitem_codeD0'][$j]}',fnqty_accept='0',fnqty_accept_base='0'
                                                    WHERE ftpo_no='{$_REQUEST['ftpo_no']}' AND
                                                            fnindex='{$_REQUEST['fnindexD0'][$j]}'";
                    }
                }else{
                    for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                        $strSQL['Update DET'][]="UPDATE txpurchase_order_det
                                                    SET ftdescription2='{$_REQUEST['hidden_ftitem_codeD0'][$j]}',fnqty_accept='0',fnqty_accept_base='0'
                                                    WHERE ftpo_no='{$_REQUEST['ftpo_no']}' AND
                                                            fnindex='{$_REQUEST['fnindexD0'][$j]}'";
                    }
                } */
            }
            
            // KHUSUS ABRINDO
            /*
            $ftsm_no=RetRstField("SELECT ftstock_in_no FROM txstock_in_hdr WHERE ftdocument_no = '{$_REQUEST['ftpo_no']}' ");                        
            
            if($ftsm_no) {
                // update po qty accept
                for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
                    if(CCur($_REQUEST['fnqtyD0'][$j])>0){
                        $jj=$jj+1;
                        $ssql="UPDATE txpurchase_order_det
                                SET fnqty_accept_base=".CCur($_REQUEST['fnqty_baseD0'][$j]).",
                                    fnqty_accept=(".(double)CCur($_REQUEST['fnqty_baseD0'][$j])."/ffformula)
                                WHERE ftpo_no='{$_REQUEST['ftpo_no']}' AND
                                    ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}' AND
                                    fnindex='{$_REQUEST['fnindexD0'][$j]}' ";
                        $strSQL['Update txpurchase_order_det accept'][]=$ssql;

                        echo $ssql;

                        if($_REQUEST['DIRECT_EXECUTE_SQL']==1) {
                                mysql_query($ssql) or die("SQL ERROR :{$ssql}");
                        }
                    }
                }
            }
            */
            break;
    }
    return $strSQL;
}


function RevisiDataAfter() {
    global $objAxpDB;
    $strSQL=array();
    
    switch($_REQUEST[mode]) {
        case "simpan_revisi":
            // KHUSUS ABRINDO
            /* $ftsm_no=RetRstField("SELECT ftstock_in_no FROM txstock_in_hdr WHERE ftdocument_no = '{$_REQUEST['ftpo_no']}' ");                        
            
            if ($ftsm_no) {
                $_REQUEST['ftstock_type_code'] = "inv";
                $_REQUEST['ftcategory_code'] = "0";

                // update stock det
                if($_REQUEST['ftcategory_code']=='0') {
                    for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
                        if ($_REQUEST['ftstock_type_code'] == 'invbb') {
                            $m=SaveStock_bahan_baku('+',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_codeD0'][$j],$_REQUEST['ftlocation_codeD0'][$j],'',CCur($_REQUEST['fnqty_baseD0'][$j]),CCur($_REQUEST['fnqty_baseD0'][$j]));
                        } else if ($_REQUEST['ftstock_type_code'] == 'invsj') {
                            $m=SaveStock_setengah_jadi('+',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_codeD0'][$j],$_REQUEST['ftlocation_codeD0'][$j],'',CCur($_REQUEST['fnqty_baseD0'][$j]),CCur($_REQUEST['fnqty_baseD0'][$j]));
                        } else if ($_REQUEST['ftstock_type_code'] == 'invsp') {
                            $m=SaveStock_sparepart('+',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_codeD0'][$j],$_REQUEST['ftlocation_codeD0'][$j],'',CCur($_REQUEST['fnqty_baseD0'][$j]),CCur($_REQUEST['fnqty_baseD0'][$j]));
                        } else if ($_REQUEST['ftstock_type_code'] == 'inv') {
                            $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);
                    }

                    mysql_query("UPDATE txpurchase_order_det SET fnstatus=5 WHERE ftpo_no='{$_REQUEST['ftpo_no']}' AND fnqty_accept >= fnqty");

                    // set status terkirim
                    $ssql="UPDATE txpurchase_order_hdr a INNER JOIN ( 
                                SELECT b.ftpo_no, COUNT(a.fnid) AS total_item, COUNT(c.fnindex) AS total_item_closed, fnreceipt_status
                                FROM txpurchase_order_det a INNER JOIN txpurchase_order_hdr b ON a.ftpo_no=b.ftpo_no
                                LEFT JOIN txpurchase_order_det c ON c.ftpo_no=a.ftpo_no AND c.fnindex=a.fnindex AND c.fnstatus = 5
                                WHERE b.ftpo_no='{$_REQUEST['ftpo_no']}' GROUP BY b.ftpo_no
                                HAVING total_item = total_item_closed AND fnreceipt_status != 3 ) b 
                            SET a.fnreceipt_status = 3 WHERE a.ftpo_no = b.ftpo_no; ";
                    $strSQL["Update txpurchase_order_hdr Receipt1"][]=$ssql;

                    if($_REQUEST['DIRECT_EXECUTE_SQL']==1) {
                        mysql_query($ssql) or die("SQL ERROR :{$ssql}");
                        if($ssql2!="")
                           mysql_query($ssql2) or die("SQL ERROR :{$ssql}");
                    }

                    // set status sebagian
                    $ssql="UPDATE txpurchase_order_hdr a INNER JOIN ( 
                                SELECT b.ftpo_no, COUNT(a.fnid) AS total_item, COUNT(c.fnindex) AS total_item_closed, fnreceipt_status
                                FROM txpurchase_order_det a INNER JOIN txpurchase_order_hdr b ON a.ftpo_no=b.ftpo_no
                                LEFT JOIN txpurchase_order_det c ON c.ftpo_no=a.ftpo_no AND c.fnindex=a.fnindex AND c.fnstatus = 5
                                WHERE b.ftpo_no='{$_REQUEST['ftpo_no']}' GROUP BY b.ftpo_no
                                HAVING total_item != total_item_closed AND total_item_closed != 0 AND fnreceipt_status != 2 ) b
                            SET a.fnreceipt_status = 2 WHERE a.ftpo_no = b.ftpo_no; ";
                    $strSQL["Update txpurchase_order_hdr Receipt2"][]=$ssql;

                    if($_REQUEST['DIRECT_EXECUTE_SQL']==1) {
                        mysql_query($ssql) or die("SQL ERROR :{$ssql}");
                        if($ssql2!="")
                           mysql_query($ssql2) or die("SQL ERROR :{$ssql}");
                    }

                    // set status belum dikirim
                    $ssql="UPDATE txpurchase_order_hdr a INNER JOIN ( 
                                SELECT b.ftpo_no, COUNT(a.fnid) AS total_item, COUNT(c.fnindex) AS total_item_closed, fnreceipt_status
                                FROM txpurchase_order_det a INNER JOIN txpurchase_order_hdr b ON a.ftpo_no=b.ftpo_no
                                LEFT JOIN txpurchase_order_det c ON c.ftpo_no=a.ftpo_no AND c.fnindex=a.fnindex AND c.fnstatus = 5
                                WHERE b.ftpo_no='{$_REQUEST['ftpo_no']}' GROUP BY b.ftpo_no
                                HAVING total_item_closed = 0 AND fnreceipt_status != 1 ) b
                        SET a.fnreceipt_status = 1 WHERE a.ftpo_no = b.ftpo_no; ";
                    $strSQL["Update txpurchase_order_hdr Receipt3"][]=$ssql;

                    $ssql2="UPDATE txpurchase_order_schedule_delivery a INNER JOIN txpurchase_order_hdr b ON a.ftpo_no = b.ftpo_no
                                SET a.fnlock = 1 WHERE a.fnlock != 1 AND b.fnreceipt_status = 3; ";
                    $strSQL["Update txpurchase_order_hdr Receipt4"][]=$ssql2;

                    if($_REQUEST['DIRECT_EXECUTE_SQL']==1) {
                        mysql_query($ssql) or die("SQL ERROR :{$ssql}");
                        if($ssql2!="")
                           mysql_query($ssql2) or die("SQL ERROR :{$ssql}");
                    }
                }

                // update total qty PB
                $fntotal_qty=RetRstField("select sum(b.fnqty) AS fnqty from txstock_in_hdr a inner join txstock_in_det b ON a.ftstock_in_no = b.ftstock_in_no where a.ftdocument_no='{$_REQUEST['ftpo_no']}'");
                $strSQL["Update txstock_in_hdr"][] ="update txstock_in_hdr set fntotal_qty = '{$fntotal_qty}' where ftdocument_no='{$_REQUEST['ftpo_no']}'; ";

            }
            
            */
            
            break;
    }
	
    return $strSQL;

}