<?php

if($_REQUEST['mode']=="update_schedule_delivery") {
    $_REQUEST['mode']="update";
}

    function ValBatalPO() {
        $ssql="select ftstock_in_no
                from txstock_in_hdr_bahan_baku
                where fnstatus<>9 and ftcategory_code='0' 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("");
        }

    }

//if($_REQUEST['mode']=="update_schedule_delivery") {
//    $_REQUEST['mode']="update";
//}
 function UpdateData() {
        global $objAxpDB;
        $objAxpDB->Allow_Update(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
        case "update":
              for($j=1; $j<count($_REQUEST['fnindexD1']); $j++) {
                 $strSQL["Update txpurchase_order_schedule_delivery"][] =
                                " update txpurchase_order_schedule_delivery
                                        set fnlock='{$_REQUEST['fnlockD1'][$j]}',
                                            fdschedule_delivery_date='".CDate($_REQUEST['fdschedule_delivery_dateD1'][$j])."'
                                            WHERE ftpo_no='{$_REQUEST['ftpo_no']}' and
                                                fnindex='{$_REQUEST['fnindexD1'][$j]}'";
            }



        break;
    }

    return $strSQL;

   }

    function SaveData() {
        global $objAxpDB;
//        $objAxpDB->Allow_Save(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
        case "simpan":
            for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                $strSQL['Update Harga Beli Mata Uang'][]="  UPDATE tlbuying_price SET
                                                            ftcurrency_code='{$_REQUEST['ftcurrency_code']}'
                                                             WHERE ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}' AND
                                                                ftcurrency_code<>'{$_REQUEST['ftcurrency_code']}'";


                        $strSQL['Update Harga Beli Price'][]="  UPDATE tlbuying_price SET
                                                            fcold_buying_price=fcbuying_price,
                                                            fcbuying_price='".CCur($_REQUEST['fcpriceD0'][$j])."'
                                                            WHERE ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}' AND
                                                                fcbuying_price<>".CCur($_REQUEST['fcpriceD0'][$j]);
            }





        break;
    }
    return $strSQL;

   }

    function SaveDataLast() {

        global $objAxpDB;
//        $objAxpDB->Allow_Save(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
        case "simpan":
           #die("mmmm:".$_REQUEST['fnpo_type2']);
           $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']}'";
           
        }
         return $strSQL;
    }

    function FinalData() {
        global $objAxpDB;
        $objAxpDB->Allow_Final(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
        case "final":
             for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                    if($_REQUEST['fnstatusD0'][$j]=='5') {
                        $strSQL["Update txpurchase_order_det 1"][] ="
                                       update txpurchase_order_det
                                            set
                                        fdclosing_date='".CDate($_REQUEST['fdclosing_dateD0'][$j])."',fnstatus=5
                                        where ftpo_no='{$_REQUEST['ftpo_no']}' AND
                                            ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}' AND
                                            fnstatus<>5";
                    }
          
            }

           $strSQL["Update txpurchase_order_det 1"][] =" UPDATE
                        txpurchase_order_hdr
                        SET ftfinal_user='{$_SESSION['username']}',
                        fdfinal_date=now(),
                        fnreceipt_status=3 ,
                        fdclosing_date='".CDate($_REQUEST['fdclosing_date'])."'   
                    WHERE not exists(
                        SELECT b.fnid
                        FROM txpurchase_order_det b
                        WHERE txpurchase_order_hdr.ftpo_no=b.ftpo_no AND
                            (b.fnstatus<>5 or ifnull(b.fnqty_accept,0)>=b.fnqty) and
                            b.ftpo_no='{$_REQUEST['ftpo_no']}')
                       and ftpo_no='{$_REQUEST['ftpo_no']}'";


//             $strSQL["Update txpurchase_order_det 1"][] ="UPDATE
//                                                            txpurchase_order_hdr a,txpurchase_order_det b
//                                                            SET a.ftfinal_user='{$_SESSION['username']}',
//                                                                a.fdfinal_date=now(),
//                                                                a.fnreceipt_status=3 ,
//                                                                a.fdclosing_date='".CDate($_REQUEST['fdclosing_date'])."'
//                                                        WHERE a.ftpo_no=b.ftpo_no and
//                                                            (b.fnstatus=5 or ifnull(b.fnqty_accept,0)>=b.fnqty) and
//                                                            a.ftpo_no='{$_REQUEST['ftpo_no']}'
//                                                              ";

//             $strSQL["Update txpurchase_order_det 1"][] =
//                    "update txpurchase_order_hdr
//                            set ftfinal_user='{$_SESSION['username']}',fdfinal_date=now(),
//                                fnreceipt_status=3 ,
//                                 fdclosing_date='".CDate($_REQUEST['fdclosing_date'])."'
//                                WHERE ftpo_no='{$_REQUEST['ftpo_no']}'";


        break;
    }

    return $strSQL;

   }





 function CancelData() {
        ValBatalPO();
        global $objAxpDB;
        $objAxpDB->Allow_Cancel(false);
        $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'])."'
                                WHERE ftpo_no='{$_REQUEST['ftpo_no']}'";



        break;
        }

        return $strSQL;
      }

  function RevisiData() {
//      ValBatalPO();

    global $objAxpDB;
    if($_REQUEST['fnreceipt_status']==1) {
         $objAxpDB->Allow_Revisi(true);
    }
    else {
         $objAxpDB->Allow_Revisi(false);
    }
     $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']}',
                                     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']}'";

               if($_REQUEST['fnreceipt_status']!=1) {
                  $strSQL["Revisi"][]="
                            INSERT INTO txpurchase_order_hdr
                            (
                            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 '{$_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, fnqty_accept, ftuom_code, fcprice,
                                 ffdiscount, fcdiscount, fcamount, fnvat,ftdescription
                                 ,fdclosing_date,fnstatus 
                            )
                             SELECT '{$_REQUEST['ftpo_no']}' AS ftpo_no, fnindex, ftitem_code,fnqty, fnqty_accept, 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
                            (
                                 ftpo_no, fnindex,fdschedule_delivery_date, fnlock
                            )
                             SELECT '{$_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]}'";


                     }

               }
              else {
                       $strSQL["Revisi"][]="update txpurchase_order_det_tracking
                                set ftpo_no='{$ftpo_no_new}'
                                where ftpo_no='{$_REQUEST['ftpo_no']}'";

                       $strSQL["Revisi"][]="update txpurchase_invoice_hdr
                                        set ftpo_no='{$ftpo_no_new}'
                                        where ftpo_no='{$_REQUEST['ftpo_no']}'";

                       $strSQL["Revisi"][]="update txstock_in_hdr_bahan_baku
                                        set ftdocument_no='{$ftpo_no_new}'
                                        where ftcategory_code='0' AND
                                                 ftdocument_no='{$_REQUEST['ftpo_no']}'";

             }






               for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                $strSQL['Update Harga Beli Mata Uang'][]="  UPDATE tlbuying_price SET
                                                            ftcurrency_code='{$_REQUEST['ftcurrency_code']}'
                                                             WHERE ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}' AND
                                                                ftcurrency_code<>'{$_REQUEST['ftcurrency_code']}'";


                        $strSQL['Update Harga Beli Price'][]="  UPDATE tlbuying_price SET
                                                            fcold_buying_price=fcbuying_price,
                                                            fcbuying_price='".CCur($_REQUEST['fcpriceD0'][$j])."'
                                                            WHERE ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}' AND
                                                                fcbuying_price<>".CCur($_REQUEST['fcpriceD0'][$j]);
            }



        break;
    }




//    $_REQUEST[mode]="simpan";

     return $strSQL;
 }

  function RevisiDataLast() {

        global $objAxpDB;
//        $objAxpDB->Allow_Save(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
        case "simpan_revisi":
           #die("mmmm:".$_REQUEST['fnpo_type2']);
           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']}'";       
           }
           
        }
         return $strSQL;
    }

?>
