<?php

 function SaveData() {
        global $objAxpDB;
        $objAxpDB->Allow_Save(true);
        $strSQL=array();
        switch($_REQUEST[mode]) {
        case "simpan":
      

         #===================================
        /*if($_REQUEST['fnpo_type3']==1) {
              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);
                 $fntotal_qty=$fntotal_qty+(double)CCur($_REQUEST['fnqtyD0'][$j]);
            }
            $jj=0;
            $ftstock_in_no=getAutoNumber('sjtn001','txstock_in_hdr_setengah_jadi','ftstock_in_no',$_REQUEST['fdtrans_date']);

             $ssql="INSERT INTO txstock_in_hdr_setengah_jadi
                    (
                        ftstock_in_no, ftcategory_code, ftstock_in_company, ftdocument_no,
                        fdschedule_delivery_date, fdtrans_date, ftreff_no, ftchecked_user,
                        ftnotes,  fntotal_qty, fnstatus
                    )
                    SELECT
                        '{$ftstock_in_no}' AS ftstock_in_no, '1' AS ftcategory_code, '{$_REQUEST['ftsupplier_code']}' AS ftstock_in_company, '{$_REQUEST['ftstock_in_return_no']}' AS ftdocument_no,
                        '".CDate($_REQUEST['fdtrans_date'])."' AS fdschedule_delivery_date, '".CDate($_REQUEST['fdtrans_date'])."' AS fdtrans_date, '' AS ftreff_no, '' AS ftchecked_user,
                        '{$_REQUEST['ftnotes']}' AS ftnotes,  '{$fntotal_qty}' AS fntotal_qty, '1' AS fnstatus";

             $strSQL['Insert txstock_in_hdr_setengah_jadi'][]=$ssql;
                $index=0;
                for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
                     if(CCur($_REQUEST['fnqtyD0'][$j])>0) {
                         $index++;
                          $ssql="INSERT INTO txstock_in_det_setengah_jadi
                                    (
                                        ftstock_in_no, fnindex, ftitem_code, ftwarehouse_code, ftlocation_code, fnqtypo,
                                        fnqtysisapo, fnqty, ftuom_code
                                    )
                                    SELECT
                                        '{$ftstock_in_no}' AS ftstock_in_no, '{$index}' AS fnindex, '{$_REQUEST['ftitem_codeD0'][$j]}' AS ftitem_code, '{$_REQUEST['ftwarehouse_codeD0'][$j]}' AS ftwarehouse_code, '{$_REQUEST['ftlocation_codeD0'][$j]}' AS ftlocation_code, '' AS fnqtypo,
                                        '' AS fnqtysisapo, '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty, '{$_REQUEST['ftuom_codeD0'][$j]}' AS ftuom_code";
                             $strSQL['Insert txstock_in_hdr_bahan_baku'][]=$ssql;
                     }
                }


            #===================================
        }
        else {
              for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
                     $m=SaveStock_bahan_baku('+',$_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);
                     $fntotal_qty=$fntotal_qty+(double)CCur($_REQUEST['fnqtyD0'][$j]);
                }
        $jj=0;
            $ftstock_in_no=getAutoNumber('bbtn001','txstock_in_hdr_bahan_baku','ftstock_in_no',$_REQUEST['fdtrans_date']);

             $ssql="INSERT INTO txstock_in_hdr_bahan_baku
                    (
                        ftstock_in_no, ftcategory_code, ftstock_in_company, ftdocument_no,
                        fdschedule_delivery_date, fdtrans_date, ftreff_no, ftchecked_user,
                        ftnotes,  fntotal_qty, fnstatus
                    )
                    SELECT
                        '{$ftstock_in_no}' AS ftstock_in_no, '1' AS ftcategory_code, '{$_REQUEST['ftsupplier_code']}' AS ftstock_in_company, '{$_REQUEST['ftstock_in_return_no']}' AS ftdocument_no,
                        '".CDate($_REQUEST['fdtrans_date'])."' AS fdschedule_delivery_date, '".CDate($_REQUEST['fdtrans_date'])."' AS fdtrans_date, '' AS ftreff_no, '' AS ftchecked_user,
                        '{$_REQUEST['ftnotes']}' AS ftnotes,  '{$fntotal_qty}' AS fntotal_qty, '1' AS fnstatus";

             $strSQL['Insert txstock_in_hdr_bahan_baku'][]=$ssql;
                $index=0;
                for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
                     if(CCur($_REQUEST['fnqtyD0'][$j])>0) {
                         $index++;
                          $ssql="INSERT INTO txstock_in_det_bahan_baku
                                    (
                                        ftstock_in_no, fnindex, ftitem_code, ftwarehouse_code, ftlocation_code, fnqtypo,
                                        fnqtysisapo, fnqty, ftuom_code
                                    )
                                    SELECT
                                        '{$ftstock_in_no}' AS ftstock_in_no, '{$index}' AS fnindex, '{$_REQUEST['ftitem_codeD0'][$j]}' AS ftitem_code, '{$_REQUEST['ftwarehouse_codeD0'][$j]}' AS ftwarehouse_code, '{$_REQUEST['ftlocation_codeD0'][$j]}' AS ftlocation_code, '' AS fnqtypo,
                                        '' AS fnqtysisapo, '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty, '{$_REQUEST['ftuom_codeD0'][$j]}' AS ftuom_code";
                             $strSQL['Insert txstock_in_hdr_bahan_baku'][]=$ssql;
                     }
                }


            #===================================
        }*/
        
        
         for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
             if(CCur($_REQUEST['fnqtyD0'][$j])>0){
                 $jj=$jj+1;
                 $ssql="UPDATE txdelivery_order_return_det_bahan_baku
                        SET fnqty_stockin_return=IFNULL(fnqty_stockin_return,0)+ ".CCur($_REQUEST['fnqtyD0'][$j])."
                        WHERE fttrans_no='{$_REQUEST['ftreturn_no']}' AND
                                ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}' ";
                $strSQL['Update txdelivery_order_return_det_bahan_baku'][]=$ssql;
             }
        }

        break;
    }
    return $strSQL;
   }

    function SaveDataLast() {
        global $objAxpDB;
        $objAxpDB->Allow_Save(true);      
        $strSQL=array();
        switch($_REQUEST[mode]) {
        case "simpan":
       
         $ssql="UPDATE
                    txstock_in_return_hdr_bahan_baku a,txpurchase_order_hdr b
                SET a.fnpo_type3=b.fnpo_type3
                WHERE a.ftpo_no=b.ftpo_no AND
                        a.ftpo_no='{$_REQUEST['ftpo_no']}'";

             $strSQL["n"][]=$ssql;
             
        break;
    }
    return $strSQL;
   }

 function CancelData() {

        global $objAxpDB;
        $objAxpDB->Allow_Cancel(True);
      
        $strSQL=array();
        switch($_REQUEST[mode]) {
          case "batal":
            /*if($_REQUEST['fnpo_type3']==1) {
                       $ssql="UPDATE txstock_in_hdr_setengah_jadi 
                                SET fnstatus=9
                             WHERE ftdocument_no= '{$_REQUEST['ftstock_in_return_no']}' AND
                                    ftcategory_code='1'";
                    $strSQL['Batal txstock_in_hdr_bahan_baku'][]=$ssql;

                     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);
                     }
              }
            else {
                     $ssql="UPDATE txstock_in_hdr_bahan_baku
                                SET fnstatus=9
                             WHERE ftdocument_no= '{$_REQUEST['ftstock_in_return_no']}' AND
                                    ftcategory_code='1'";
                    $strSQL['Batal txstock_in_hdr_bahan_baku'][]=$ssql;
                      for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
                         $m=SaveStock_bahan_baku('-',$_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;
         for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
             if(CCur($_REQUEST['fnqtyD0'][$j])>0){
                 $jj=$jj+1;
                 $ssql="UPDATE txdelivery_order_return_det_bahan_baku
                        SET fnqty_stockin_return=IFNULL(fnqty_stockin_return,0)- ".CCur($_REQUEST['fnqtyD0'][$j])."
                        WHERE fttrans_no='{$_REQUEST['ftreturn_no']}' AND
                                ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}' ";

                $strSQL['Update txdelivery_order_return_det_bahan_baku'][]=$ssql;

             }
        }

        break;
    }
    return $strSQL;
   }

 function RevisiData() {

    global $objAxpDB;
   // $objAxpDB->Allow_Revisi(false);

     $strSQL=array();
    switch($_REQUEST[mode]) {
    case "simpan_revisi":
        /* for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
             $m=SaveStock_bahan_baku('-',$_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;
         $ssql="UPDATE txstock_in_hdr_bahan_baku
                    SET fnstatus=9
                 WHERE ftdocument_no= '{$_REQUEST['ftstock_in_return_no']}' AND
                        ftcategory_code='2'";
        $strSQL['Batal txstock_in_hdr_bahan_baku'][]=$ssql;
		*/
         for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
             if(CCur($_REQUEST['fnqtyD0'][$j])>0){
                 $jj=$jj+1;
                 $ssql="UPDATE txdelivery_order_return_det_bahan_baku
                        SET fnqty_stockin_return=IFNULL(fnqty_stockin_return,0)- ".CCur($_REQUEST['fnqtyD0'][$j])."
                        WHERE fttrans_no='{$_REQUEST['ftreturn_no']}' AND
                                ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}' ";

                $strSQL['Update txdelivery_order_return_det_bahan_baku'][]=$ssql;

             }
        }


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

             $ftstock_in_return_no_new="{$_REQUEST['ftstock_in_return_no']}{$str1}";
             $ssql="update txstock_in_return_hdr_bahan_baku
                                set fnstatus=3,ftrevisi_user='{$_SESSION['username']}',
                                     fdrevisi_date=now(),
                                     ftstock_in_return_no='{$ftstock_in_return_no_new}'
                                where ftstock_in_return_no='{$_REQUEST['ftstock_in_return_no']}'";

             $strSQL["revisi"][]=$ssql;

             $ssql="update txstock_in_return_det_bahan_baku
                                set ftstock_in_return_no='{$ftstock_in_return_no_new}'
                                where ftstock_in_return_no='{$_REQUEST['ftstock_in_return_no']}'";
             $strSQL["revisi"][]=$ssql;



        break;
    }

     return $strSQL;
 }

?>
