<?php


 function SaveData() {
    global $objAxpDB;
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "simpan":
     /*if($_REQUEST['fnitem_type']==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, '6' AS ftcategory_code, '{$_REQUEST['ftsupplier_code']}' AS ftstock_in_company, '{$_REQUEST['ftfpb_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, '6' AS ftcategory_code, '{$_REQUEST['ftsupplier_code']}' AS ftstock_in_company, '{$_REQUEST['ftfpb_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;
                     }
                }


            #===================================

      }*/

        break;

    }

    return $strSQL;
 }


 function CancelData() {
    global $objAxpDB;
    //$objAxpDB->Allow_Cancel(false);
     $strSQL=array();
    switch($_REQUEST[mode]) {
    case "batal":
        /*if($_REQUEST['fnitem_type']==1) {
              $strSQL['Batal txstock_in_hdr_setengah_jadi'][]="UPDATE txstock_in_hdr_setengah_jadi
                                                        SET fnstatus=9
                                                        WHERE ftdocument_no='{$_REQUEST['ftfpb_no']}' AND
                                                                ftcategory_code='6'";
             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 {
              $strSQL['Batal Stock Out Hdr'][]="UPDATE txstock_in_hdr_bahan_baku 
                                                        SET fnstatus=9
                                                        WHERE ftdocument_no='{$_REQUEST['ftfpb_no']}' AND
                                                                ftcategory_code='6'";

            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);
                
            }
        }*/

        break;
    }

     return $strSQL;
 }

function RevisiData() {
    global $objAxpDB;
    //$objAxpDB->Allow_Revisi(false);
     $strSQL=array();
    switch($_REQUEST[mode]) {
    case "simpan_revisi":
        if($_REQUEST['fnitem_type']==1) {

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

                         $ftfpb_no_new="{$_REQUEST['ftfpb_no']}{$str1}";

                       /* $strSQL['Batal Stock Out Hdr'][]="UPDATE txstock_in_hdr_setengah_jadi 
                                                        SET fnstatus=9,ftdocument_no='{$ftfpb_no_new}'
                                                        WHERE ftdocument_no='{$_REQUEST['ftfpb_no']}' AND
                                                                ftcategory_code='6'";

                        $rs=mysql_query("SELECT * FROM txpengembalian_barang_det WHERE ftfpb_no='{$_REQUEST['ftfpb_no']}'");
                        while($r=mysql_fetch_assoc($rs)) {
                             $m=SaveStock_setengah_jadi('-',$r['ftitem_code'],'',$r['ftwarehouse_code'],$r['ftlocation_code'],CCur($r['fnqty']),CCur($r['fnqty']));
                             $strSQL=array_merge_recursive($strSQL,$m);
                         }*/


                         $strSQL["Revisi"][]="update txpengembalian_barang_hdr
                                            set fdrevisi_date=now(),ftrevisi_user='{$_SESSION['username']}',
                                                 fnstatus=3,ftfpb_no='{$ftfpb_no_new}'
                                            where ftfpb_no='{$_REQUEST['ftfpb_no']}'";

                         $strSQL["Revisi"][]="update txpengembalian_barang_det
                                            set ftfpb_no='{$ftfpb_no_new}'
                                            where ftfpb_no='{$_REQUEST['ftfpb_no']}'";
                     //--------------------------------------------

                                /*  $strSQL['Batal txstock_in_hdr_setengah_jadi'][]="UPDATE txstock_in_hdr_setengah_jadi
                                                                    SET fnstatus=9,ftdocument_no='{$ftfpb_no_new}'
                                                                    WHERE ftdocument_no='{$_REQUEST['ftfpb_no']}'";

                                //############################
                                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, '6' AS ftcategory_code, '{$_REQUEST['ftsupplier_code']}' AS ftstock_in_company, '{$_REQUEST['ftfpb_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 {

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

                         $ftfpb_no_new="{$_REQUEST['ftfpb_no']}{$str1}";
                        
						/*$strSQL['Batal Stock Out Hdr'][]="UPDATE txstock_in_hdr_bahan_baku
                                                        SET fnstatus=9,ftdocument_no='{$ftfpb_no_new}'
                                                        WHERE ftdocument_no='{$_REQUEST['ftfpb_no']}' AND
                                                                ftcategory_code='6'";

                        $rs=mysql_query("SELECT * FROM txpengembalian_barang_det WHERE ftfpb_no='{$_REQUEST['ftfpb_no']}'");
                        while($r=mysql_fetch_assoc($rs)) {
                             $m=SaveStock_bahan_baku('-',$r['ftitem_code'],'',$r['ftwarehouse_code'],$r['ftlocation_code'],CCur($r['fnqty']),CCur($r['fnqty']));
                             $strSQL=array_merge_recursive($strSQL,$m);

                         }*/


                         $strSQL["Revisi"][]="update txpengembalian_barang_hdr
                                            set fdrevisi_date=now(),ftrevisi_user='{$_SESSION['username']}',
                                                 fnstatus=3,ftfpb_no='{$ftfpb_no_new}'
                                            where ftfpb_no='{$_REQUEST['ftfpb_no']}'";

                         $strSQL["Revisi"][]="update txpengembalian_barang_det
                                            set ftfpb_no='{$ftfpb_no_new}'
                                            where ftfpb_no='{$_REQUEST['ftfpb_no']}'";
                     //--------------------------------------------

                    /*              $strSQL['Batal txstock_in_hdr_bahan_baku'][]="UPDATE txstock_in_hdr_bahan_baku
                                                                    SET fnstatus=9,ftdocument_no='{$ftfpb_no_new}'
                                                                    WHERE ftdocument_no='{$_REQUEST['ftfpb_no']}'";

                     //############################
                     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, '6' AS ftcategory_code, '{$_REQUEST['ftsupplier_code']}' AS ftstock_in_company, '{$_REQUEST['ftfpb_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;
                             }
                        }*/


                    #===================================
                }

        break;
    }

     return $strSQL;
 }

//function RevisiDataLast() {
//    global $objAxpDB;
//    $strSQL=array();
//    switch($_REQUEST[mode]) {
//    case "simpan_revisi":
//        if($_REQUEST['fnitem_type']==1) {
//                for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
//                    $rs=mysql_query("select ftitem_name,ftprint_name from tlstock_hdr_setengah_jadi where ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'");
//                    $r=mysql_fetch_assoc($rs);
//                   $strSQL["n"][]="update txpengembalian_barang_det set ftitem_name='".$r['ftitem_name']."',ftprint_name='".$r['ftprint_name']."' where  ftfpb_no='".$_REQUEST['ftfpb_no']."' and ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'";
//                }
//                 $ssql="DELETE FROM  txpengembalian_barang_det WHERE fnqty=0 AND ftfpb_no='".$_REQUEST['ftfpb_no']."'  ";
//                  $strSQL["n"][]=$ssql;
//        }
//        else {
//              for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
//                    $rs=mysql_query("select ftitem_name,ftprint_name from tlstock_hdr_bahan_baku where ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'");
//                    $r=mysql_fetch_assoc($rs);
//                   $strSQL["n"][]="update txpengembalian_barang_det set ftitem_name='".$r['ftitem_name']."',ftprint_name='".$r['ftprint_name']."' where  ftfpb_no='".$_REQUEST['ftfpb_no']."' and ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'";
//                }
//                 $ssql="DELETE FROM  txpengembalian_barang_det WHERE fnqty=0 AND ftfpb_no='".$_REQUEST['ftfpb_no']."'  ";
//                  $strSQL["n"][]=$ssql;
//        }
//        break;
//
//    }
//
//    return $strSQL;
// }



?>
