<?php

 function SaveData() {
    global $objAxpDB;
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "simpan":
      /*if($_REQUEST['fnitem_type']==1) {
            $ftstock_out_no=getAutoNumber('bbtn002','txstock_out_hdr_setengah_jadi','ftstock_out_no',$_REQUEST['fdtrans_date']);
            $dt=getDateYmd($_REQUEST['fdtrans_date']);

                $strSQL['Insert Stock Out Hdr'][]="
                          INSERT INTO txstock_out_hdr_setengah_jadi
                              (ftbranch_code, ftstock_out_no, ftcategory_code,
                              ftstock_out_company, ftdocument_no, fdtrans_date,
                              fntotal_qty, ftnotes, fnstatus, ftcreated_user,
                              fdcreated_date, ftmodified_user)
                          SELECT
                         '{$_SESSION['strbranch']}' AS ftbranch_code, '{$ftstock_out_no}' AS ftstock_out_no, '1' AS ftcategory_code,
                             '{$_REQUEST['ftsupplier_code']}' AS  ftstock_out_company, '{$_REQUEST['ftdo_no']}' AS ftdocument_no,'{$dt}' AS  fdtrans_date,
                              '".CCur($_REQUEST['fftotal_qty'])."' AS fntotal_qty,'{$_REQUEST['ftnotes']}' AS  ftnotes, '1' AS fnstatus,  '{$_SESSION['username']}' AS ftcreated_user,
                              now() AS fdcreated_date,  '{$_SESSION['username']}' AS ftmodified_user";


                        $m=0;
                        for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
    //                        if($_REQUEST['ftstock_category_codeD0'][$j]=="inv") {
                                 $m=1;
                                 $strSQL['Insert Stock Out Det'][]="
                                 INSERT INTO txstock_out_det_setengah_jadi
                                      (ftstock_out_no, fnindex, ftitem_code,
                                      ftwarehouse_code, ftlocation_code, fnqty_available,
                                      fnqty, ftuom_code, ftcreated_user,
                                      fdcreated_date, ftmodified_user
                                      )
                                      SELECT
                                      '{$ftstock_out_no}' AS ftstock_out_no, '{$j}' AS fnindex, '{$_REQUEST['ftitem_codeD0'][$j]}' AS ftitem_code,
                                      '{$_REQUEST['ftwarehouse_codeD0'][$j]}' AS ftwarehouse_code, '{$_REQUEST['ftlocation_codeD0'][$j]}' AS ftlocation_code, '".CCur($_REQUEST['fnqty_availableD0'][$j])."' AS fnqty_available,
                                      '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty,  '{$_REQUEST['ftuom_codeD0'][$j]}' AS ftuom_code,  '{$_SESSION['username']}' AS ftcreated_user,
                                      now() AS fdcreated_date,  '{$_SESSION['username']}' AS ftmodified_user";

                                  $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($m==0) {
                        unset($strSQL['Insert Stock Out Det']);
                    }
      }
      else {
              $ftstock_out_no=getAutoNumber('bbtn002','txstock_out_hdr_bahan_baku','ftstock_out_no',$_REQUEST['fdtrans_date']);
            $dt=getDateYmd($_REQUEST['fdtrans_date']);

                $strSQL['Insert Stock Out Hdr'][]="
                          INSERT INTO txstock_out_hdr_bahan_baku
                              (ftbranch_code, ftstock_out_no, ftcategory_code,
                              ftstock_out_company, ftdocument_no, fdtrans_date,
                              fntotal_qty, ftnotes, fnstatus, ftcreated_user,
                              fdcreated_date, ftmodified_user)
                          SELECT
                         '{$_SESSION['strbranch']}' AS ftbranch_code, '{$ftstock_out_no}' AS ftstock_out_no, '1' AS ftcategory_code,
                             '{$_REQUEST['ftsupplier_code']}' AS  ftstock_out_company, '{$_REQUEST['ftdo_no']}' AS ftdocument_no,'{$dt}' AS  fdtrans_date,
                              '".CCur($_REQUEST['fftotal_qty'])."' AS fntotal_qty,'{$_REQUEST['ftnotes']}' AS  ftnotes, '1' AS fnstatus,  '{$_SESSION['username']}' AS ftcreated_user,
                              now() AS fdcreated_date,  '{$_SESSION['username']}' AS ftmodified_user";

                
                        $m=0;
                        for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
    //                        if($_REQUEST['ftstock_category_codeD0'][$j]=="inv") {
                                 $m=1;
                                 $strSQL['Insert Stock Out Det'][]="
                                 INSERT INTO txstock_out_det_bahan_baku
                                      (ftstock_out_no, fnindex, ftitem_code,
                                      ftwarehouse_code, ftlocation_code, fnqty_available,
                                      fnqty, ftuom_code, ftcreated_user,
                                      fdcreated_date, ftmodified_user
                                      )
                                      SELECT
                                      '{$ftstock_out_no}' AS ftstock_out_no, '{$j}' AS fnindex, '{$_REQUEST['ftitem_codeD0'][$j]}' AS ftitem_code,
                                      '{$_REQUEST['ftwarehouse_codeD0'][$j]}' AS ftwarehouse_code, '{$_REQUEST['ftlocation_codeD0'][$j]}' AS ftlocation_code, '".CCur($_REQUEST['fnqty_availableD0'][$j])."' AS fnqty_available,
                                      '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty,  '{$_REQUEST['ftuom_codeD0'][$j]}' AS ftuom_code,  '{$_SESSION['username']}' AS ftcreated_user,
                                      now() AS fdcreated_date,  '{$_SESSION['username']}' AS ftmodified_user";

                                  $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 SaveDataLast() {
    global $objAxpDB;
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "simpan":
    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 txdelivery_order_det_bahan_baku set ftitem_name='".$r['ftitem_name']."',ftprint_name='".$r['ftprint_name']."' where  ftdo_no='".$_REQUEST['ftdo_no']."' and ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'";
                    
					$ssql="UPDATE txpurchase_order_det
                            SET fnqty_sj=IFNULL(fnqty_sj,0)+ ".CCur($_REQUEST['fnqtyD0'][$j])."
                            WHERE ftpo_no='{$_REQUEST['ftpo_no']}' AND
                                    ftitem_code='{$_REQUEST['ftitem_code1D0'][$j]}' ";

                    $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 txdelivery_order_det_bahan_baku set ftitem_name='".$r['ftitem_name']."',ftprint_name='".$r['ftprint_name']."' where  ftdo_no='".$_REQUEST['ftdo_no']."' and ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'";
                    
					$ssql="UPDATE txpurchase_order_det
                            SET fnqty_sj=IFNULL(fnqty_sj,0)+ ".CCur($_REQUEST['fnqtyD0'][$j])."
                            WHERE ftpo_no='{$_REQUEST['ftpo_no']}' AND
                                    ftitem_code='{$_REQUEST['ftitem_code1D0'][$j]}' ";

                    $strSQL["n"][]=$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 Stock Out Hdr'][]="UPDATE txstock_out_hdr_setengah_jadi
                                                        SET fnstatus=9
                                                        WHERE ftdocument_no='{$_REQUEST['ftdo_no']}' AND
                                                                ftcategory_code='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);
                 
				 $ssql="UPDATE txpurchase_order_det
                        SET fnqty_sj=IFNULL(fnqty_sj,0)- ".CCur($_REQUEST['fnqtyD0'][$j])."
                        WHERE ftpo_no='{$_REQUEST['ftpo_no']}' AND
                                ftitem_code='{$_REQUEST['ftitem_code1D0'][$j]}' ";

                 $strSQL["n"][]=$ssql;
            }
        }
        else {
              /*$strSQL['Batal Stock Out Hdr'][]="UPDATE txstock_out_hdr_bahan_baku
                                                        SET fnstatus=9
                                                        WHERE ftdocument_no='{$_REQUEST['ftdo_no']}' AND
                                                                ftcategory_code='1'";*/
      
            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);
                 
				 $ssql="UPDATE txpurchase_order_det
                        SET fnqty_sj=IFNULL(fnqty_sj,0)- ".CCur($_REQUEST['fnqtyD0'][$j])."
                        WHERE ftpo_no='{$_REQUEST['ftpo_no']}' AND
                                ftitem_code='{$_REQUEST['ftitem_code1D0'][$j]}' ";

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

        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(ftdo_no),'')  AS M
                                  FROM txdelivery_order_hdr_bahan_baku
                                  WHERE ftdo_no LIKE '{$_REQUEST['ftdo_no']}-%'";
                        $no=RetRstField($ssql1);
                        if($no!="") {
                            $no1=explode("-",$no);
                            $no=(int)$no1[1]+1;
                            $str1="-{$no}";
                        }
                        else {
                            $str1="-1";
                        }

                        $ftdo_no_new="{$_REQUEST['ftdo_no']}{$str1}";
                    
							
                        $strSQL["Revisi"][]="update txdelivery_order_hdr_bahan_baku
                                            set fdrevisi_date=now(),ftrevisi_user='{$_SESSION['username']}',
                                                 fnstatus=3,ftdo_no='{$ftdo_no_new}'
                                            where ftdo_no='{$_REQUEST['ftdo_no']}'";						 
								
                        $strSQL["Revisi"][]="update txdelivery_order_det_bahan_baku
                                            set ftdo_no='{$ftdo_no_new}'
                                            where ftdo_no='{$_REQUEST['ftdo_no']}'";
                     
          }
                   
                else {
                     
                     //-----------------------------------------------------
                     // batal -%
                     $ssql1="SELECT IFNULL(MAX(ftdo_no),'')  AS M
                                  FROM txdelivery_order_hdr_bahan_baku 
                                  WHERE ftdo_no LIKE '{$_REQUEST['ftdo_no']}-%'";
                        $no=RetRstField($ssql1);
                        if($no!="") {
                            $no1=explode("-",$no);
                            $no=(int)$no1[1]+1;
                            $str1="-{$no}";
                        }
                        else {
                            $str1="-1";
                        }

                        $ftdo_no_new="{$_REQUEST['ftdo_no']}{$str1}";					

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

                        $strSQL["Revisi"][]="update txdelivery_order_det_bahan_baku
                                            set ftdo_no='{$ftdo_no_new}'
                                            where ftdo_no='{$_REQUEST['ftdo_no']}'";
                     //--------------------------------------------

                        /*$strSQL['Batal txstock_out_hdr_bahan_baku'][]="UPDATE txstock_out_hdr_bahan_baku 
                                                                    SET fnstatus=9,ftdocument_no='{$ftdo_no_new}'
                                                                    WHERE ftdocument_no='{$_REQUEST['ftdo_no']}'";

                                //############################
                        $ftstock_out_no=getAutoNumber('TN008','txstock_out_hdr_bahan_baku','ftstock_out_no',$_REQUEST['fdtrans_date']);
                        $dt=getDateYmd($_REQUEST['fdtrans_date']);
                                $strSQL['Insert Stock Out Hdr'][]="
                                          INSERT INTO txstock_out_hdr_bahan_baku
                                              (ftbranch_code, ftstock_out_no, ftcategory_code,
                                              ftstock_out_company, ftdocument_no, fdtrans_date,
                                              fntotal_qty, ftnotes, fnstatus, ftcreated_user,
                                              fdcreated_date, ftmodified_user)
                                          SELECT
                                         '{$_SESSION['strbranch']}' AS ftbranch_code, '{$ftstock_out_no}' AS ftstock_out_no, '1' AS ftcategory_code,
                                             '{$_REQUEST['ftcustomer_code']}' AS  ftstock_out_company, '{$_REQUEST['ftdo_no']}' AS ftdocument_no,'{$dt}' AS  fdtrans_date,
                                              '".CCur($_REQUEST['fftotal_qty'])."' AS fntotal_qty,'{$_REQUEST['ftnotes']}' AS  ftnotes, '1' AS fnstatus,  '{$_SESSION['username']}' AS ftcreated_user,
                                              now() AS fdcreated_date,  '{$_SESSION['username']}' AS ftmodified_user";

                                $m=0;
                                for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
            //                        if($_REQUEST['ftstock_category_codeD0'][$j]=="inv") {
                                         $m=1;
                                         $strSQL['Insert Stock Out Det'][]="
                                         INSERT INTO txstock_out_det_bahan_baku
                                              (ftstock_out_no, fnindex, ftitem_code,
                                              ftwarehouse_code, ftlocation_code, fnqty_available,
                                              fnqty, ftuom_code, ftcreated_user,
                                              fdcreated_date, ftmodified_user
                                              )
                                              SELECT
                                              '{$ftstock_out_no}' AS ftstock_out_no, '{$j}' AS fnindex, '{$_REQUEST['ftitem_codeD0'][$j]}' AS ftitem_code,
                                              '{$_REQUEST['ftwarehouse_codeD0'][$j]}' AS ftwarehouse_code, '{$_REQUEST['ftlocation_codeD0'][$j]}' AS ftlocation_code, '".CCur($_REQUEST['fnqty_availableD0'][$j])."' AS fnqty_available,
                                              '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty,  '{$_REQUEST['ftuom_codeD0'][$j]}' AS ftuom_code,  '{$_SESSION['username']}' AS ftcreated_user,
                                              now() AS fdcreated_date,  '{$_SESSION['username']}' AS ftmodified_user";

                                          $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);
                                    }*/
            //                    }
                                //########################################33
                }
         
        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 txdelivery_order_det_bahan_baku set ftitem_name='".$r['ftitem_name']."',ftprint_name='".$r['ftprint_name']."' where  ftdo_no='".$_REQUEST['ftdo_no']."' and ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'";
                }
                 $ssql="DELETE FROM  txdelivery_order_det_bahan_baku WHERE fnqty=0 AND ftdo_no='".$_REQUEST['ftdo_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 txdelivery_order_det_bahan_baku set ftitem_name='".$r['ftitem_name']."',ftprint_name='".$r['ftprint_name']."' where  ftdo_no='".$_REQUEST['ftdo_no']."' and ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'";
                }
                 $ssql="DELETE FROM  txdelivery_order_det_bahan_baku WHERE fnqty=0 AND ftdo_no='".$_REQUEST['ftdo_no']."'  ";
                 $strSQL["n"][]=$ssql;
        }
        break;

    }

    return $strSQL;
 }


?>
