<?php
    if($_REQUEST['mode']=="update_siap_bayar") {
        $_REQUEST['mode']="update";
        $_REQUEST['mode1']="2";
    }

    switch($_REQUEST[mode]) {
        case "simpan_revisi":
            $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 purchase invoice utk PO ini
        $ssql="select ftpurchase_invoice_no
                from txpurchase_invoice_hdr
                where fnstatus<>3 and fnstatus<>9 and fnpo_type='1' 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='1' and ftpo_no='{$_REQUEST['ftpo_no']}'";

        $rs=mysql_query($ssql) or die ("query error {$ssql}");
        $ftpurchasE_invoice_no="";
        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, 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, '{$_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";
           }

            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":
        /*
        $_REQUEST['fddelivery_date']=getDateYmd($_REQUEST['fddelivery_date']);
        $_REQUEST['fdclosing_date']=getDateYmd($_REQUEST['fdclosing_date']);
        $_REQUEST['fdcancel_date']=getDateYmd($_REQUEST['fdcancel_date']);
        $strSQL["Insert txpurchase_order_hdr"][] ="
                INSERT INTO txpurchase_order_hdr(
                ftbranch_code,
                ftpo_no, fnpo_type3, fnpo_typelpb, ftsupplier_code, ftship_address, fnhdr_print,
                fnhdr_category, ftreference, ftdo_no, fdtrans_date, ftterm_payment,
                fddelivery_date, ftcurrency_code, fcrate, fnreceipt_status, fntax_code,
                ftnotes, fdclosing_date, fdcancel_date, ftmemo_no_cancel, ftreason_cancel,
                fcsub_amount, ffdiscount, fcdiscount, fcother_cost, ffvat,
                fcvat, fcamount, fnstatus, fnpo_type,
                ftcreated_user, fdcreated_date
                ) SELECT
                '{$_SESSION['strbranch']}' AS ftbranch_code,
                '{$_REQUEST['ftpo_no']}' AS ftpo_no, '{$_REQUEST['fnpo_type3']}' AS fnpo_type3, '{$_REQUEST['fnpo_typelpb']}' AS fnpo_typelpb,
                '{$_REQUEST['ftsupplier_code']}' AS ftsupplier_code, '{$_REQUEST['ftship_address']}' AS ftship_address, '{$_REQUEST['fnhdr_print']}' AS fnhdr_print,
                '{$_REQUEST['fnhdr_category']}' AS fnhdr_category, '{$_REQUEST['ftreference']}' AS ftreference, '{$_REQUEST['ftdo_no']}' AS ftdo_no, '{$_REQUEST['fdtrans_date']}' AS fdtrans_date, '{$_REQUEST['ftterm_payment']}' AS ftterm_payment,
                '{$_REQUEST['fddelivery_date']}' AS fddelivery_date, '{$_REQUEST['ftcurrency_code']}' AS ftcurrency_code, '{$_REQUEST['fcrate']}' AS fcrate, '{$_REQUEST['fnreceipt_status']}' AS fnreceipt_status, '{$_REQUEST['fntax_code']}' AS fntax_code,
                '{$_REQUEST['ftnotes']}' AS ftnotes, '{$_REQUEST['fdclosing_date']}' AS fdclosing_date, '{$_REQUEST['fdcancel_date']}' AS fdcancel_date, '{$_REQUEST['ftmemo_no_cancel']}' AS ftmemo_no_cancel, '{$_REQUEST['ftreason_cancel']}' AS ftreason_cancel,
                '".CCur($_REQUEST['fcsub_amount'])."' AS fcsub_amount, '".CCur($_REQUEST['ffdiscount'])."' AS ffdiscount, '".CCur($_REQUEST['fcdiscount'])."' AS fcdiscount, '".CCur($_REQUEST['fcother_cost'])."' AS fcother_cost, '".CCur($_REQUEST['ffvat'])."' AS ffvat,
                '".CCur($_REQUEST['fcvat'])."' AS fcvat, '".CCur($_REQUEST['fcamount'])."' AS fcamount, '{$_REQUEST['fnpo_type3']}' AS fnstatus, '1' AS fnpo_type,
                '{$_SESSION['username']}' AS ftcreated_user, now() AS fdcreated_date
                ";

        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, 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, '{$_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";
        } */
          
        // 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])."";
                    }
                    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())";
                    }
                }
            }
        break;
    }
//    die();
    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']}'";
        $ftpurchase_invoice_no=getAutoNumber('tn113','txpurchase_invoice_hdr','ftpurchase_invoice_no',$_REQUEST['fdtrans_date']);
         
        switch($_REQUEST['fnpo_type2']) {
            case "0":
                break;
            case "1":
            case "2":
            case "3":
                if($_REQUEST['ftterm_payment']=="") {
                    $_REQUEST['ftterm_payment']="0";
                }

                $strSQL["Insert txpurchase_invoice_hdr"][] ="
                            INSERT INTO txpurchase_invoice_hdr
                            (
                                       ftpurchase_invoice_no,ftstock_in_no, ftsupplier_code,ftpo_no,
                                            fdtrans_date,ftterm_payment,fddue_date,
                                            ftcurrency_code,fcrate,fnpayment_status, fntax_code,
                                    ftnotes,
                                            fcsub_amount,ffdiscount,fcdiscount,
                                            fcother_cost,ffvat,fcvat,
                                            fcamount,fnstatus,
                                             fnpo_type,fnpo_type2 ,
                                             fcpayment_amount,fcoutstanding_amount
                            )
                            SELECT '{$ftpurchase_invoice_no}' as ftpurchase_invoice_no, '' as ftstock_in_no, ftsupplier_code,'{$_REQUEST['ftpo_no']}' AS ftpo_no,
                                            fdtrans_date,ftterm_payment,'".CDate($_REQUEST['fddue_date'])."' as fddue_date,
                                            ftcurrency_code,fcrate,'1' AS fnpayment_status, fntax_code,
                                    ftnotes,
                                            fcsub_amount,ffdiscount,fcdiscount,
                                            fcother_cost,ffvat,fcvat,
                                            fcamount,fnstatus,
                                            '1' AS fnpo_type,'{$_REQUEST['fnpo_type2']}' AS fnpo_type2 ,
                                             fcpayment_amount,fcamount AS fcoutstanding_amount
                                    FROM txpurchase_order_hdr
                                    WHERE ftpo_no='{$_REQUEST['ftpo_no']}'";
                                    
                $strSQL["Insert txpurchase_invoice_det"][] ="
                             INSERT INTO txpurchase_invoice_det
                             (
                                  ftpurchase_invoice_no, fnindex, ftitem_code,fnqty, ftuom_code,ftwarehouse_code,
                                  ftlocation_code, fcprice, ffdiscount, fcdiscount, fcamount
                              )

                                SELECT
                                 '{$ftpurchase_invoice_no}' as   ftpurchase_invoice_no, fnindex, ftitem_code,fnqty, ftuom_code,'' as ftwarehouse_code,
                                    '' as ftlocation_code, fcprice, ffdiscount, fcdiscount, fcamount
                                    FROM txpurchase_order_det
                                WHERE ftpo_no='{$_REQUEST['ftpo_no']}'";

                break;
        }

        $ssql="update txpurchase_order_det  set  fnstatus=1 where  ftpo_no='{$_REQUEST['ftpo_no']}' ";
        $strSQL["n"][] =$ssql;

        // 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, 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, '{$_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 2"][] ="UPDATE txpurchase_order_hdr SET fnstatus=1, 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_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(false);
        $strSQL=array();
        
        switch($_REQUEST[mode]) {
            case "batal":
                $strSQL["Batal txpurchase_order_hdr"][] = "update txpurchase_order_hdr
                            set ftmodified_user='{$_SESSION['username']}',fdmodified_date=now(),
                            fnreceipt_status=9,
                            fnstatus=9
                            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(true);
    $strSQL=array();
    switch($_REQUEST[mode]) {
        case "simpan_revisi":
            //-----------------------------------------------------
            // batal -%
            $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']}',
                                     ftmodified_user='{$_SESSION['username']}',fdmodified_date=now(),
                                     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}',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']}'";

            $strSQL["Revisi"][]="update txpurchase_order_det_tracking
                                 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'])."";   
                }
            }                             
        break;
    }

    return $strSQL;
}

function RevisiDataLast() {
    global $objAxpDB;
    //$objAxpDB->Allow_Revisi(false);
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "simpan_revisi":
        
        $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']}'";
        // $ftpurchase_invoice_no=getAutoNumber($_SESSION['strbranch'],'tn113','txpurchase_invoice_hdr','ftpurchase_invoice_no',$_REQUEST['fdtrans_date']);
        
        switch($_REQUEST['fnpo_type2']) {
            case "0":
                break;
            case "1":
            case "2":
            case "3":
                if($_REQUEST['ftterm_payment']=="") {
                  $_REQUEST['ftterm_payment']="0";
                }
                /* 
                $strSQL["Insert txpurchase_invoice_hdr"][] ="
                                INSERT INTO txpurchase_invoice_hdr
                                (
                                           ftpurchase_invoice_no,ftstock_in_no, ftsupplier_code,ftpo_no,
                                                fdtrans_date,ftterm_payment,fddue_date,
                                                ftcurrency_code,fcrate,fnpayment_status, fntax_code,
                                        ftnotes,
                                                fcsub_amount,ffdiscount,fcdiscount,
                                                fcother_cost,ffvat,fcvat,
                                                fcamount,fnstatus,
                                                 fnpo_type,fnpo_type2 ,
                                                 fcpayment_amount,fcoutstanding_amount
                                )
                                SELECT '{$ftpurchase_invoice_no}' as ftpurchase_invoice_no, '' as ftstock_in_no, ftsupplier_code,'{$_REQUEST['ftpo_no']}' AS ftpo_no,
                                                fdtrans_date,ftterm_payment,'".CDate($_REQUEST['fddue_date'])."' as fddue_date,
                                                ftcurrency_code,fcrate,'1' AS fnpayment_status, fntax_code,
                                        ftnotes,
                                                fcsub_amount,ffdiscount,fcdiscount,
                                                fcother_cost,ffvat,fcvat,
                                                fcamount,fnstatus,
                                                '1' AS fnpo_type,'{$_REQUEST['fnpo_type2']}' AS fnpo_type2 ,
                                                 fcpayment_amount,fcamount AS fcoutstanding_amount
                                        FROM txpurchase_order_hdr
                                        WHERE ftpo_no='{$_REQUEST['ftpo_no']}'";
                                        
                $strSQL["Insert txpurchase_invoice_det"][] ="
                                 INSERT INTO txpurchase_invoice_det
                                 (
                                      ftpurchase_invoice_no, fnindex, ftitem_code,fnqty, ftuom_code,ftwarehouse_code,
                                      ftlocation_code, fcprice, ffdiscount, fcdiscount, fcamount
                                  )

                                    SELECT
                                     '{$ftpurchase_invoice_no}' as   ftpurchase_invoice_no, fnindex, ftitem_code,fnqty, ftuom_code,'' as ftwarehouse_code,
                                        '' as ftlocation_code, fcprice, ffdiscount, fcdiscount, fcamount
                                        FROM txpurchase_order_det
                                    WHERE ftpo_no='{$_REQUEST['ftpo_no']}'"; */

                break;
        }
            
        #------------------------
        $strSQL["Insert txpurchase_order_hdr Delivery"][] ="update txpurchase_order_hdr set fddelivery_date=fdtrans_date where ftpo_no='{$_REQUEST['ftpo_no']}'";
        $ssql="update txpurchase_order_det  set  fnstatus=1 where  ftpo_no='{$_REQUEST['ftpo_no']}' ";
        $strSQL["n"][] =$ssql;
        /*
        $strSQL["n"][] =
                        " update txpurchase_order_hdr
                                set fnsiap_bayar='{$_REQUEST['fnsiap_bayar']}',
                                    fdpayment_schedule_date='".CDate($_REQUEST['fdpayment_schedule_date'])."'
                                    WHERE ftpo_no='{$_REQUEST['ftpo_no']}'";

        $strSQL["n"][] =
                        " update txpurchase_invoice_hdr
                                set fnsiap_bayar='{$_REQUEST['fnsiap_bayar']}'
                                    WHERE ftpo_no='{$_REQUEST['ftpo_no']}'"; */

        break;
    }
    return $strSQL;
}