<?php

    if($_REQUEST['mode']=="update_qty_limit") {
            $_REQUEST['mode']="update";
    }
    //echo ($_REQUEST['mode']);
    
    switch($_REQUEST[mode]) {
        case "revisi":
            $ssql="SELECT fnid FROM txstock_in_hdr a WHERE ftdocument_no='{$_REQUEST['ftpo_no']}'";
            $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']}'";
            $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<>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<>9 and ftdocument_no='{$_REQUEST['ftpo_no']}' union all
                select ftstock_out_no from txstock_out_hdr_bahan_baku where fnstatus<>9 and ftdocument_no='{$_REQUEST['ftpo_no']}' union all
                select ftstock_out_no from txstock_out_hdr_setengah_jadi where fnstatus<>9 and ftdocument_no='{$_REQUEST['ftpo_no']}' union all
                select ftstock_out_no from txstock_out_hdr_sparepart where 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<>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<>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,
                      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";

           }

           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']}'";
			/*for
                        //  Update oleh Jansen - 05.07.2012
                        $strSQL["Update txpurchase_order_hdr"][] =
						"UPDATE txpurchase_order_hdr
                                                    SET fnreceipt_status= 2
						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())";

                        }        
                    }
                }
            }
            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;
}

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 fdrevisi_date=now(),ftrevisi_user='{$_SESSION['username']}',
                                        fnstatus=3,ftpo_no='{$ftpo_no_new}'
                                        WHERE ftpo_no='{$_REQUEST['ftpo_no']}'";
                $strSQL["Revisi"][]="UPDATE txpurchase_order_det
                                        SET ftpo_no='{$ftpo_no_new}'
                                        where ftpo_no='{$_REQUEST['ftpo_no']}'";

                $strSQL["Revisi"][]="UPDATE txpurchase_order_schedule_delivery
                                        SET ftpo_no='{$ftpo_no_new}'
                                        where ftpo_no='{$_REQUEST['ftpo_no']}'";
                
                $strSQL["Revisi"][]="
                        INSERT INTO txpurchase_order_hdr
                        (
                                ftbranch_code, ftpo_no, ftsupplier_code,ftship_address, ftreference,
                                fdtrans_date,ftterm_payment,fddelivery_date,
                                ftcurrency_code,fcrate,fnreceipt_status, fntax_code,
                                ftnotes, fdclosing_date, fdcancel_date,
                                fcsub_amount,ffdiscount,fcdiscount,
                                fcother_cost,ffvat,fcvat,
                                fcamount,fnstatus,
                                fnpo_type,fnpo_type3 ,fnpo_typelpb
                         )
                        SELECT ftbranch_code,'{$_REQUEST['ftpo_no']}' AS ftpo_no, ftsupplier_code,ftship_address, ftreference,
                                fdtrans_date,ftterm_payment,fddelivery_date,
                                ftcurrency_code,fcrate,fnreceipt_status, fntax_code,
                                ftnotes, fdclosing_date, fdcancel_date,
                                fcsub_amount,ffdiscount,fcdiscount,
                                fcother_cost,ffvat,fcvat,
                                fcamount,'1' as fnstatus,
                                fnpo_type,fnpo_type3, fnpo_typelpb
                        FROM  txpurchase_order_hdr
                        WHERE ftpo_no='{$ftpo_no_new}'";

                $strSQL["Revisi"][]="
                        INSERT INTO txpurchase_order_det
                        (
                            ftpo_no, fnindex, ftitem_code,fnqty,ffformula,fnqty_base, fnqty_accept,fnqty_accept_base, ftuom_code, fcprice,
                            ffdiscount, fcdiscount, fcamount, fnvat,ftdescription,
                            fdclosing_date,fnstatus
                        )
                        SELECT '{$_REQUEST['ftpo_no']}' AS ftpo_no, fnindex, ftitem_code,fnqty,ffformula,fnqty_base, fnqty_accept,fnqty_accept_base, ftuom_code, fcprice,
                            ffdiscount, fcdiscount, fcamount, fnvat,ftdescription,
                            fdclosing_date,fnstatus
                        FROM txpurchase_order_det
                        WHERE ftpo_no='{$ftpo_no_new}'";

                $strSQL["Revisi"][]="
                        INSERT INTO txpurchase_order_schedule_delivery
                        (
                            ftbranch_code,ftpo_no, fnindex,fdschedule_delivery_date, fnlock
                        )
                        SELECT ftbranch_code,'{$_REQUEST['ftpo_no']}' AS ftpo_no, fnindex,fdschedule_delivery_date, fnlock
                        FROM txpurchase_order_schedule_delivery
                        WHERE ftpo_no='{$ftpo_no_new}'";

                $strSQL["Revisi"][]="
                        UPDATE txpurchase_order_hdr
                        SET ftcurrency_code='{$_REQUEST['ftcurrency_code']}',
                            fcrate='".CCur($_REQUEST['fcrate'])."',
                            ftnotes='{$_REQUEST['ftnotes']}',
                            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 ftpo_no='{$_REQUEST['ftpo_no']}';
                        ";

                for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                        $strSQL["Revisi"][]="
                                UPDATE txpurchase_order_det SET
                                        fcprice='".CCur($_REQUEST['fcpriceD0'][$j])."',
                                        ffdiscount='".CCur($_REQUEST['ffdiscountD0'][$j])."',
                                        fcdiscount='".CCur($_REQUEST['fcdiscountD0'][$j])."',
                                        fcamount='".CCur($_REQUEST['fcamountD0'][$j])."',
                                        fnvat='".CCur($_REQUEST['fnvatD0'][$j])."',
                                        ftdescription='".($_REQUEST['ftdescriptionD0'][$j])."'
                                WHERE ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}' AND
                                        ftpo_no='{$_REQUEST['ftpo_no']}'  AND
                                        fnindex='{$_REQUEST['fnindexD0'][$j]}'";
                }  

                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]}'";
                    }
                }
            }
            break;
    }
    return $strSQL;
}