<?php


    function SaveData() {
        global $objAxpDB;
        $objAxpDB->Allow_Save(true);
        $objAxpDB->SetDirectExecuteSql(true);
        $strSQL=array();
        switch($_REQUEST[mode]) {
        case "simpan":
        for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
             $m=SaveStock_setengah_jadi('+',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_codeD0'][$j],$_REQUEST['ftlocation_codeD0'][$j],CCur($_REQUEST['fnqtyD0'][$j]),CCur($_REQUEST['fnqtyD0'][$j]));
             $strSQL=array_merge_recursive($strSQL,$m);
        }
        $jj=0;
        if($_REQUEST['ftcategory_code']=='0') {
             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=IFNULL(fnqty_accept,0)+ ".CCur($_REQUEST['fnqtyD0'][$j])."
                            WHERE ftpo_no='{$_REQUEST['ftdocument_no']}' AND
                                    ftitem_code='{$_REQUEST['ftitem_code1D0'][$j]}'  ";
                    $strSQL['Update txpurchase_order_det'][]=$ssql;
                   if($_REQUEST['DIRECT_EXECUTE_SQL']==1) {
                        mysql_query($ssql) or die("SQL ERROR :{$ssql}");
                    }
                 }
            }
        }


        break;
    }
    return $strSQL;
   }

   function SaveDataLast() {
        global $objAxpDB;
        $objAxpDB->Allow_Save(true);

        $strSQL=array();
        switch($_REQUEST[mode]) {
        case "simpan":
        $ssql="DELETE FROM txstock_in_det_bahan_baku WHERE fnqty=0 AND ftstock_in_no='{$_REQUEST['ftstock_in_no']}' ";
        $strSQL['Stock Masuk Index'][]= $ssql;
         if($_REQUEST['DIRECT_EXECUTE_SQL']==1) {
                        mysql_query($ssql) or die("SQL ERROR :{$ssql}");
           }
        $ssql=" update
                        txstock_in_det_bahan_baku a,
                        (select c.fnid,(select ifnull(count(d.fnid),0)+1
                                        from txstock_in_det_bahan_baku d
                                        where d.fnid<c.fnid and d.ftstock_in_no='{$_REQUEST['ftstock_in_no']}') as m
                                from  txstock_in_det_bahan_baku c
                                where c.ftstock_in_no='{$_REQUEST['ftstock_in_no']}'
                                ) b
                        set a.fnindex=b.m
                        where a.ftstock_in_no='{$_REQUEST['ftstock_in_no']}' and a.fnid=b.fnid";

         $strSQL['Stock Masuk Index'][]= $ssql;
          if($_REQUEST['DIRECT_EXECUTE_SQL']==1) {
                        mysql_query($ssql) or die("SQL ERROR :{$ssql}");
           }
            //terkirim
            $ssql1="SELECT COUNT(*) AS jml
                    FROM txpurchase_order_det a, (
                            SELECT a.ftdocument_no AS ftpo_no ,b.ftitem_code1 as ftitem_code,b.fnindex_po,SUM(b.fnqty) AS fnqty
                            FROM txstock_in_hdr_bahan_baku a,
                                    txstock_in_det_bahan_baku b
                            WHERE a.fnstatus=1 AND
                                    a.ftcategory_code='0' AND
                                     a.ftstock_in_no=b.ftstock_in_no	 AND
                                    a.ftdocument_no='{$_REQUEST['ftdocument_no']}'
                            GROUP BY a.ftdocument_no,b.ftitem_code,b.fnindex_po 
                    ) b
                    WHERE a.ftpo_no=b.ftpo_no AND
                            a.fnqty<= b.fnqty  AND
                            a.ftitem_code=b.ftitem_code AND
                            a.ftpo_no='{$_REQUEST['ftdocument_no']}'  AND
                            a.fnstatus<>5 
                   ";
           $ssql2="SELECT COUNT(*) AS jml  FROM txpurchase_order_det WHERE fnstatus<>5 AND ftpo_no='{$_REQUEST['ftdocument_no']}' ";

            $ssql3="SELECT IFNULL(COUNT(*),0) AS jml
                    FROM txpurchase_order_det a, (
                            SELECT a.ftdocument_no AS ftpo_no ,b.ftitem_code1 as ftitem_code,b.fnindex_po,SUM(b.fnqty) AS fnqty
                            FROM txstock_in_hdr_bahan_baku a,
                                    txstock_in_det_bahan_baku b
                            WHERE a.fnstatus=1 AND
                                    a.ftcategory_code='0' AND
                                    a.ftstock_in_no=b.ftstock_in_no	  AND
                                    a.ftdocument_no='{$_REQUEST['ftdocument_no']}'
                            GROUP BY a.ftdocument_no,b.ftitem_code,b.fnindex_po 
                    ) b
                    WHERE a.ftpo_no=b.ftpo_no AND
                            a.ftitem_code=b.ftitem_code AND
                            a.ftpo_no='{$_REQUEST['ftdocument_no']}'
                    ";

          # die(($ssql3));
           if(RetRstField($ssql1)==RetRstField($ssql2)) {
               $ssql="UPDATE txpurchase_order_hdr SET fnreceipt_status=3 WHERE   ftpo_no='{$_REQUEST['ftdocument_no']}' ";
               $strSQL["Update txpurchase_order_hdr Receipt"][]=$ssql;
               $ssql2="UPDATE txpurchase_order_schedule_delivery SET fnlock=1 WHERE   ftpo_no='{$_REQUEST['ftdocument_no']}' ";

                $strSQL["Update txpurchase_order_hdr Receipt"][]=$ssql2;
           }
           //sebagian
           elseif(RetRstField($ssql3)>0) {

               $ssql="UPDATE txpurchase_order_hdr SET fnreceipt_status=2 WHERE   ftpo_no='{$_REQUEST['ftdocument_no']}' ";
               $strSQL["Update txpurchase_order_hdr Receipt"][]=$ssql;

           }
           else {
               $ssql="UPDATE txpurchase_order_hdr SET fnreceipt_status=1 WHERE   ftpo_no='{$_REQUEST['ftdocument_no']}' ";
               $strSQL["Update txpurchase_order_hdr Receipt"][]=$ssql;
                $ssql2="UPDATE txpurchase_order_schedule_delivery SET fnlock=0 WHERE ftpo_no='{$_REQUEST['ftdocument_no']}' ";
                 $strSQL["Update txpurchase_order_hdr Receipt"][]=$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}");
            }
            
            $ssql="update txstock_in_det_bahan_baku
                    set fnqty_1=fnqty,ftuom_code=ftuom_code_1
                    where ftstock_in_no='{$_REQUEST['ftstock_in_no']}' ";
               
            if($_REQUEST['DIRECT_EXECUTE_SQL']==1) {
                mysql_query($ssql) or die("SQL ERROR :{$ssql}");
                 $strSQL["n"][]=$ssql;
            }

        break;
    }
    return $strSQL;
   }




 function CancelData() {

        global $objAxpDB;
        //$objAxpDB->Allow_Cancel(false);
        $objAxpDB->SetDirectExecuteSql(true);
        $strSQL=array();
        switch($_REQUEST[mode]) {
          case "batal":
                for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
                     $m=SaveStock_setengah_jadi('-',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_codeD0'][$j],$_REQUEST['ftlocation_codeD0'][$j],CCur($_REQUEST['fnqtyD0'][$j]),CCur($_REQUEST['fnqtyD0'][$j]));
                     $strSQL=array_merge_recursive($strSQL,$m);
                }

              if($_REQUEST['ftcategory_code']=='0') {
                     for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
                         $ssql="UPDATE txpurchase_order_det
                                    SET fnqty_accept=IFNULL(fnqty_accept,0)- ".CCur($_REQUEST['fnqtyD0'][$j])."
                                    WHERE ftpo_no='{$_REQUEST['ftdocument_no']}' AND
                                            ftitem_code='{$_REQUEST['ftitem_code1D0'][$j]}' ";

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

        return $strSQL;
      }
   function CancelDataLast() {

        global $objAxpDB;
       // $objAxpDB->Allow_Cancel(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
          case "batal":
               //terkirim
            $ssql1="SELECT COUNT(*) AS jml
                    FROM txpurchase_order_det a, (
                            SELECT a.ftdocument_no AS ftpo_no ,b.ftitem_code1 as ftitem_code,b.fnindex_po,SUM(b.fnqty) AS fnqty
                            FROM txstock_in_hdr_bahan_baku a,
                                    txstock_in_det_bahan_baku b
                            WHERE a.fnstatus=1 AND
                                    a.ftcategory_code='0' AND
                                     a.ftstock_in_no=b.ftstock_in_no	 AND
                                    a.ftdocument_no='{$_REQUEST['ftdocument_no']}'
                            GROUP BY a.ftdocument_no,b.ftitem_code,b.fnindex_po 
                    ) b
                    WHERE a.ftpo_no=b.ftpo_no AND
                            a.fnqty<= b.fnqty  AND
                            a.ftitem_code=b.ftitem_code AND
                            a.ftpo_no='{$_REQUEST['ftdocument_no']}' 
                   ";
               
           $ssql2="SELECT COUNT(*) AS jml  FROM txpurchase_order_det WHERE fnstatus<>5 AND ftpo_no='{$_REQUEST['ftdocument_no']}' ";

            $ssql3="SELECT IFNULL(COUNT(*),0) AS jml
                    FROM txpurchase_order_det a, (
                            SELECT a.ftdocument_no AS ftpo_no , b.ftitem_code1 as  ftitem_code,b.fnindex_po, SUM(b.fnqty) AS fnqty
                            FROM txstock_in_hdr_bahan_baku a,
                                    txstock_in_det_bahan_baku b
                            WHERE a.fnstatus=1 AND
                                    a.ftcategory_code='0' AND
                                    a.ftstock_in_no=b.ftstock_in_no	  AND
                                    a.ftdocument_no='{$_REQUEST['ftdocument_no']}'
                            GROUP BY a.ftdocument_no,b.ftitem_code,b.fnindex_po 
                    ) b
                    WHERE a.ftpo_no=b.ftpo_no AND
                            a.ftitem_code=b.ftitem_code AND
                            a.ftpo_no='{$_REQUEST['ftdocument_no']}' 
                    ";

          # die(($ssql3));
           if(RetRstField($ssql1)==RetRstField($ssql2)) {
               $ssql="UPDATE txpurchase_order_hdr SET fnreceipt_status=3 WHERE   ftpo_no='{$_REQUEST['ftdocument_no']}' ";
               $strSQL["Update txpurchase_order_hdr Receipt"][]=$ssql;
                $ssql2="UPDATE txpurchase_order_schedule_delivery SET fnlock=1 WHERE   ftpo_no='{$_REQUEST['ftdocument_no']}' ";

                $strSQL["Update txpurchase_order_hdr Receipt"][]=$ssql2;
           }
           //sebagian
           elseif(RetRstField($ssql3)>0) {

               $ssql="UPDATE txpurchase_order_hdr SET fnreceipt_status=2 WHERE   ftpo_no='{$_REQUEST['ftdocument_no']}' ";
               $strSQL["Update txpurchase_order_hdr Receipt"][]=$ssql;

           }
           else {
               $ssql="UPDATE txpurchase_order_hdr SET fnreceipt_status=1 WHERE   ftpo_no='{$_REQUEST['ftdocument_no']}' ";
               $strSQL["Update txpurchase_order_hdr Receipt"][]=$ssql;
               $ssql2="UPDATE txpurchase_order_schedule_delivery SET fnlock=0 WHERE ftpo_no='{$_REQUEST['ftdocument_no']}' ";
                 $strSQL["Update txpurchase_order_hdr Receipt"][]=$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}");
            }

        break;
        }

        return $strSQL;
      }


 function RevisiData() {

    global $objAxpDB;
    $objAxpDB->Allow_Revisi(false);
      $objAxpDB->SetDirectExecuteSql(true);
     $strSQL=array();
    switch($_REQUEST[mode]) {
    case "revisi":

         for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
                     $m=SaveStock_setengah_jadi('-',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_codeD0'][$j],$_REQUEST['ftlocation_codeD0'][$j],CCur($_REQUEST['fnqtyD0'][$j]),CCur($_REQUEST['fnqtyD0'][$j]));
                     $strSQL=array_merge_recursive($strSQL,$m);
                }

          if($_REQUEST['ftcategory_code']=='0') {
                 for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
                     $ssql="UPDATE txpurchase_order_det
                                SET fnqty_accept=IFNULL(fnqty_accept,0)- ".CCur($_REQUEST['fnqtyD0'][$j])."
                                WHERE ftpo_no='{$_REQUEST['ftdocument_no']}' AND
                                        ftitem_code='{$_REQUEST['ftitem_code1D0'][$j]}' ";
                     
                     $strSQL['Update txpurchase_order_det'][]=$ssql;
                     if($_REQUEST['DIRECT_EXECUTE_SQL']==1) {
                             mysql_query($ssql) or die("SQL ERROR :{$ssql}");
                       }
                }
            }

             //-----------------------------------------------------
         // batal -%
         $ssql1="SELECT IFNULL(MAX(ftstock_in_no),'')  AS M
                      FROM txstock_in_hdr_bahan_baku
                      WHERE ftstock_in_no LIKE '{$_REQUEST['ftstock_in_no']}-%'";
            $no=RetRstField($ssql1);
            if($no!="") {
                $no1=explode("-",$no);
                $no=(int)$no1[1]+1;
                $str1="-{$no}";
            }
            else {
                $str1="-1";
            }

             $ftstock_in_no_new="{$_REQUEST['ftstock_in_no']}{$str1}";
             $ssql="update txstock_in_hdr_bahan_baku
                                set fnstatus=3,ftrevisi_user='{$_SESSION['username']}',
                                     ftstock_in_no='{$ftstock_in_no_new}'
                                where ftstock_in_no='{$_REQUEST['ftstock_in_no']}'";
             $strSQL["revisi"][]=$ssql;
                if($_REQUEST['DIRECT_EXECUTE_SQL']==1) {
                             mysql_query($ssql) or die("SQL ERROR :{$ssql}");
                       }
             $ssql="update txstock_in_det_bahan_baku
                                set ftstock_in_no='{$ftstock_in_no_new}'
                                where ftstock_in_no='{$_REQUEST['ftstock_in_no']}'";
             $strSQL["revisi"][]=$ssql;
             if($_REQUEST['DIRECT_EXECUTE_SQL']==1) {
                             mysql_query($ssql) or die("SQL ERROR :{$ssql}");
                       }


             //terkirim
            $ssql1="SELECT COUNT(*) AS jml
                    FROM txpurchase_order_det a, (
                            SELECT a.ftdocument_no AS ftpo_no ,b.ftitem_code1 as ftitem_code,b.fnindex_po,SUM(b.fnqty) AS fnqty
                            FROM txstock_in_hdr_bahan_baku a,
                                    txstock_in_det_bahan_baku b
                            WHERE a.fnstatus=1 AND
                                    a.ftcategory_code='0' AND
                                     a.ftstock_in_no=b.ftstock_in_no	 AND
                                    a.ftdocument_no='{$_REQUEST['ftdocument_no']}'
                            GROUP BY a.ftdocument_no,b.ftitem_code,b.fnindex_po 
                    ) b
                    WHERE a.ftpo_no=b.ftpo_no AND
                            a.fnqty<= b.fnqty  AND
                            a.ftitem_code=b.ftitem_code AND
                            a.ftpo_no='{$_REQUEST['ftdocument_no']}'  AND
                            a.fnstatus<>5 
                   ";
           $ssql2="SELECT COUNT(*) AS jml  FROM txpurchase_order_det WHERE fnstatus<>5 AND ftpo_no='{$_REQUEST['ftdocument_no']}' ";

            $ssql3="SELECT IFNULL(COUNT(*),0) AS jml
                    FROM txpurchase_order_det a, (
                            SELECT a.ftdocument_no AS ftpo_no ,b.ftitem_code1 as ftitem_code,b.fnindex_po,SUM(b.fnqty) AS fnqty
                            FROM txstock_in_hdr_bahan_baku a,
                                    txstock_in_det_bahan_baku b
                            WHERE a.fnstatus=1 AND
                                    a.ftcategory_code='0' AND
                                    a.ftstock_in_no=b.ftstock_in_no	  AND
                                    a.ftdocument_no='{$_REQUEST['ftdocument_no']}'
                            GROUP BY a.ftdocument_no,b.ftitem_code,b.fnindex_po 
                    ) b
                    WHERE a.ftpo_no=b.ftpo_no AND
                            a.ftitem_code=b.ftitem_code AND
                            a.ftpo_no='{$_REQUEST['ftdocument_no']}' 
                    ";

          # die(($ssql3));
           if(RetRstField($ssql1)==RetRstField($ssql2)) {
               $ssql="UPDATE txpurchase_order_hdr SET fnreceipt_status=3 WHERE   ftpo_no='{$_REQUEST['ftdocument_no']}' ";
               $strSQL["Update txpurchase_order_hdr Receipt"][]=$ssql;
                $ssql2="UPDATE txpurchase_order_schedule_delivery SET fnlock=1 WHERE   ftpo_no='{$_REQUEST['ftdocument_no']}' ";

                $strSQL["Update txpurchase_order_hdr Receipt"][]=$ssql2;
           }
           //sebagian
           elseif(RetRstField($ssql3)>0) {

               $ssql="UPDATE txpurchase_order_hdr SET fnreceipt_status=2 WHERE   ftpo_no='{$_REQUEST['ftdocument_no']}' ";
               $strSQL["Update txpurchase_order_hdr Receipt"][]=$ssql;

           }
           else {
               $ssql="UPDATE txpurchase_order_hdr SET fnreceipt_status=1 WHERE   ftpo_no='{$_REQUEST['ftdocument_no']}' ";
               $strSQL["Update txpurchase_order_hdr Receipt"][]=$ssql;
               $ssql2="UPDATE txpurchase_order_schedule_delivery SET fnlock=0 WHERE ftpo_no='{$_REQUEST['ftdocument_no']}' ";
                 $strSQL["Update txpurchase_order_hdr Receipt"][]=$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}");
            }



        break;
    }

     return $strSQL;
 }


?>
