<?php


    function SaveData() {
        global $objAxpDB;
        $objAxpDB->Allow_Save(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
        case "simpan":
            
            $strSQL['Update qty retur txstock_in_det_bahan_baku'][]="
                    INSERT INTO txdelivery_order_return_hdr_bahan_baku
                            (
                                ftstock_in_return_no,fntype_reff,fttrans_no, ftstock_in_no, ftpo_no, ftsupplier_code,ftsj_no_supplier, fdtrans_date,
                                ftnotes, fftotal_qty, fnstatus,fntype,fntypesj,
                                fdcreated_date,fdmodified_date,ftcreated_user, ftmodified_user
                            )
                                VALUES (
                                '{$_REQUEST['ftstock_in_return_no']}', '{$_REQUEST['fntype_reff']}','{$_REQUEST['fttrans_no']}', '{$_REQUEST['ftstock_in_no']}', '{$_REQUEST['ftpo_no']}', '{$_REQUEST['ftsupplier_code']}','{$_REQUEST['ftsj_no_supplier']}', '".CDate($_REQUEST['fdtrans_date'])."',
                                '{$_REQUEST['ftnotes']}',  '".CCur($_REQUEST['fftotal_qty'])."', '{$_REQUEST['fnstatus']}',0 ,'{$_REQUEST['fntypesj']}',
                                NOW(),NOW(),'{$_SESSION['username']}', '{$_SESSION['username']}')";
            $index=0;
             for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                 if(CCur($_REQUEST['fnqtyD0'][$j])>0) {
                     $index++;
                     $strSQL['Insert Stock Out Det'][]="
                            INSERT INTO txdelivery_order_return_det_bahan_baku
                            (
                                fttrans_no, fnindex, ftitem_code, ftwarehouse_code, ftlocation_code,
                                fnqty_available, fnqty, fnqty_in, fnqty_outstanding_in, ftuom_code,ftdescription,
                                ftcreated_user, fdcreated_date, ftmodified_user
                            )
                            VALUES
                            (
                                 '{$_REQUEST['fttrans_no']}', '{$index}', '{$_REQUEST['ftitem_codeD0'][$j]}', '{$_REQUEST['ftwarehouse_codeD0'][$j]}', '{$_REQUEST['ftlocation_codeD0'][$j]}',
                                 '".CCur($_REQUEST['fnqty_availableD0'][$j])."',  '".CCur($_REQUEST['fnqtyD0'][$j])."','".CCur($_REQUEST['fnqty_inD0'][$j])."', '".CCur($_REQUEST['fnqty_outstanding_inD0'][$j])."', '{$_REQUEST['ftuom_codeD0'][$j]}', '{$_REQUEST['ftdescriptionD0'][$j]}',
                                '{$_SESSION['username']}', NOW(), '{$_SESSION['username']}')";
                 }

            }
            if($_REQUEST['fntype_reff']==1) {           
                    //update qty retur di stock in
                    //-------------------------------
                    /*for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                         $strSQL['Update qty retur txstock_in_det_bahan_baku'][]="UPDATE txstock_in_det_bahan_baku
                                                                    SET fnqty_return=IFNULL(fnqty_return,0)+".CCur($_REQUEST['fnqtyD0'][$j])."
                                                                    WHERE ftstock_in_no='{$_REQUEST['ftstock_in_no']}' AND
                                                                            ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}'";
                    }*/
             }
            else {
                //update qty retur di stock in
                    //-------------------------------
                    for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                         $strSQL['Update qty retur txstock_in_return_det_bahan_baku'][]="UPDATE txstock_in_return_det_bahan_baku
                                                                    SET fnqty_return2=IFNULL(fnqty_return2,0)+".CCur($_REQUEST['fnqtyD0'][$j])."
                                                                    WHERE ftstock_in_return_no='{$_REQUEST['ftstock_in_return_no']}' AND
                                                                            ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}'";
                    }
            }


            // transaksi stock keluar
            /*if($_REQUEST['fnpo_type3']==1) {
                  $ftstock_out_no=getAutoNumber('bbtn002','txstock_out_hdr_setengah_jadi','ftstock_out_no',$_REQUEST['fdtrans_date']);
                    $dt=getDateYmd($_REQUEST['fdtrans_date']);
                    $strSQL['Insert txstock_out_hdr_setengah_jadi'][]="
                              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, '2' AS ftcategory_code,
                                 '{$_REQUEST['ftsupplier_code']}' AS  ftstock_out_company, '{$_REQUEST['fttrans_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";


                    for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                         if(CCur($_REQUEST['fnqtyD0'][$j])>0) {
                             $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";
                         }

                    }
                      $strSQL['Insert Stock Out Det'][]="UPDATE  txstock_out_hdr_setengah_jadi 
                                                            SET fntotal_qty=(SELECT SUM(fnqty) AS fnqty FROM txstock_out_det_bahan_baku  WHERE ftstock_out_no='{$ftstock_out_no}')
                                                          WHERE ftstock_out_no='{$ftstock_out_no}'";

                      //update qty
                     for($j=1; $j<count($_REQUEST['fnindexD0']); $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 {
                    $ftstock_out_no=getAutoNumber('bbtn002','txstock_out_hdr_bahan_baku','ftstock_out_no',$_REQUEST['fdtrans_date']);
                    $dt=getDateYmd($_REQUEST['fdtrans_date']);
                    $strSQL['Insert txstock_out_hdr_bahan_baku'][]="
                              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, '2' AS ftcategory_code,
                                 '{$_REQUEST['ftsupplier_code']}' AS  ftstock_out_company, '{$_REQUEST['fttrans_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";


                    for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                         if(CCur($_REQUEST['fnqtyD0'][$j])>0) {
                             $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";
                         }

                    }
                      $strSQL['Insert Stock Out Det'][]="UPDATE  txstock_out_hdr_bahan_baku
                                                            SET fntotal_qty=(SELECT SUM(fnqty) AS fnqty FROM txstock_out_det_bahan_baku  WHERE ftstock_out_no='{$ftstock_out_no}')
                                                          WHERE ftstock_out_no='{$ftstock_out_no}'";

                      //update qty
                     for($j=1; $j<count($_REQUEST['fnindexD0']); $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 SaveDataLast() {
        global $objAxpDB;
        $objAxpDB->Allow_Save(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
        case "simpan":
             if($_REQUEST['fnpo_type3']==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_return_det_bahan_baku set ftitem_name='".$r['ftitem_name']."',ftprint_name='".$r['ftprint_name']."' where  fttrans_no='".$_REQUEST['fttrans_no']."' and ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'";
                    }
                     $ssql="UPDATE
                            txdelivery_order_return_hdr_bahan_baku a,txstock_in_hdr_bahan_baku b
                            SET a.fnpo_type3=b.fnpo_type3
                            WHERE a.ftstock_in_no=b.ftstock_in_no AND
                                    a.ftstock_in_no='{$_REQUEST['ftstock_in_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_return_det_bahan_baku set ftitem_name='".$r['ftitem_name']."',ftprint_name='".$r['ftprint_name']."' where  fttrans_no='".$_REQUEST['fttrans_no']."' and ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'";
                    }
                     $ssql="UPDATE
                            txdelivery_order_return_hdr_bahan_baku a,txstock_in_hdr_bahan_baku b
                            SET a.fnpo_type3=b.fnpo_type3
                            WHERE a.ftstock_in_no=b.ftstock_in_no AND
                                    a.ftstock_in_no='{$_REQUEST['ftstock_in_no']}'
                            ";
                     $strSQL["n"][]=$ssql;
             }
      
           
             
            break;
    }
    return $strSQL;

   }

 function CancelData() {
     
        global $objAxpDB;
        $objAxpDB->Allow_Cancel(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
          case "batal":
            $strSQL["Batal txdelivery_order_return_hdr_bahan_baku"][] =
                    "update txdelivery_order_return_hdr_bahan_baku
                            set ftmodified_user='{$_SESSION['username']}',fdmodified_date=now(), 
                                fnstatus=9
                                WHERE fttrans_no='{$_REQUEST['fttrans_no']}'";


            if($_REQUEST['fntype_reff']==1) {
                    //update qty retur di stock in
                    //-------------------------------
                    /*for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                         $strSQL['Update qty retur txstock_in_det_bahan_baku'][]="UPDATE txstock_in_det_bahan_baku
                                                                    SET fnqty_return=IFNULL(fnqty_return,0)-".CCur($_REQUEST['fnqtyD0'][$j])."
                                                                    WHERE ftstock_in_no='{$_REQUEST['ftstock_in_no']}' AND
                                                                            ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}'";
                    }*/
             }
            else {
                //update qty retur di stock in
                    //-------------------------------
                    for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                         $strSQL['Update qty retur txstock_in_return_det_bahan_baku'][]="UPDATE txstock_in_return_det_bahan_baku
                                                                    SET fnqty_return2=IFNULL(fnqty_return2,0)-".CCur($_REQUEST['fnqtyD0'][$j])."
                                                                    WHERE ftstock_in_return_no='{$_REQUEST['ftstock_in_return_no']}' AND
                                                                            ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}'";
                    }
            }
            /*if($_REQUEST['fnpo_type3']==1) {
                  $strSQL['Insert Stock Out Det'][]="UPDATE  txstock_out_hdr_setengah_jadi 
                                                        SET fnstatus='9'
                                                      WHERE ftdocument_no='{$_REQUEST['fttrans_no']}' AND
                                                                ftcategory_code='2'";
                                                      
                 for($j=1; $j<count($_REQUEST['fnindexD0']); $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['Insert Stock Out Det'][]="UPDATE  txstock_out_hdr_bahan_baku
                                                    SET fnstatus='9'
                                                  WHERE ftdocument_no='{$_REQUEST['fttrans_no']}'  AND
                                                      ftcategory_code='2'";
                 //update qty
                 for($j=1; $j<count($_REQUEST['fnindexD0']); $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['fntype_reff']==1) {
                    //update qty retur di stock in
                    //-------------------------------
                    /*for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                         $strSQL['Update qty retur txstock_in_det_bahan_baku'][]="UPDATE txstock_in_det_bahan_baku
                                                                    SET fnqty_return=IFNULL(fnqty_return,0)-".CCur($_REQUEST['fnqtyD0'][$j])."
                                                                    WHERE ftstock_in_no='{$_REQUEST['ftstock_in_no']}' AND
                                                                            ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}'";
                    }*/
             }
            else {
                //update qty retur di stock in
                    //-------------------------------
                    for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                         $strSQL['Update qty retur txstock_in_return_det_bahan_baku'][]="UPDATE txstock_in_return_det_bahan_baku
                                                                    SET fnqty_return2=IFNULL(fnqty_return2,0)-".CCur($_REQUEST['fnqtyD0'][$j])."
                                                                    WHERE ftstock_in_return_no='{$_REQUEST['ftstock_in_return_no']}' AND
                                                                            ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}'";
                    }
            }
            
             if($_REQUEST['fnpo_type3']==1) {
                 /*$strSQL['Insert Stock Out Det'][]="UPDATE  txstock_out_hdr_setengah_jadi 
                                                        SET fnstatus='9'
                                                      WHERE ftdocument_no='{$_REQUEST['fttrans_no']}'  AND
                                                                ftcategory_code='2'";;

                 //update qty
                 for($j=1; $j<count($_REQUEST['fnindexD0']); $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['Insert Stock Out Det'][]="UPDATE  txstock_out_hdr_bahan_baku
                                                        SET fnstatus='9'
                                                      WHERE ftdocument_no='{$_REQUEST['fttrans_no']}'  AND
                                                                ftcategory_code='2'";

                 //update qty
                 for($j=1; $j<count($_REQUEST['fnindexD0']); $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);
                }*/
             }
         // batal -%
         $ssql1="SELECT IFNULL(MAX(fttrans_no),'')  AS M
                      FROM txdelivery_order_return_hdr_bahan_baku
                      WHERE fttrans_no LIKE '{$_REQUEST['fttrans_no']}-%'";
            $no=RetRstField($ssql1);
            if($no!="") {
                $no1=explode("-",$no);
                $i=count($no1)-1;
                $no=(int)$no1[$i]+1;
                $str1="-{$no}";
            }
            else {
                $str1="-1";
            }

             $fttrans_no_new="{$_REQUEST['fttrans_no']}{$str1}";
             $ssql="update txdelivery_order_return_hdr_bahan_baku
                                set fnstatus=3,ftrevisi_user='{$_SESSION['username']}',
                                     fdrevisi_date=now(), 
                                     fttrans_no='{$fttrans_no_new}'
                                where fttrans_no='{$_REQUEST['fttrans_no']}'";
             
             $strSQL["revisi"][]=$ssql;
               
             $ssql="update txdelivery_order_return_det_bahan_baku
                                set fttrans_no='{$fttrans_no_new}'
                                where fttrans_no='{$_REQUEST['fttrans_no']}'";
             $strSQL["revisi"][]=$ssql;
          


        break;
    }

     return $strSQL;
 }
 
function RevisiDataLast() {
    global $objAxpDB;
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "simpan_revisi":
	
            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_return_det_bahan_baku set ftitem_name='".$r['ftitem_name']."',ftprint_name='".$r['ftprint_name']."' where  fttrans_no='".$_REQUEST['fttrans_no']."' and ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'";
			}
             $ssql="UPDATE
                    txdelivery_order_return_hdr_bahan_baku a,txstock_in_hdr_bahan_baku b
                    SET a.fnpo_type3=b.fnpo_type3 , a.fntype='0'
                    WHERE a.ftstock_in_no=b.ftstock_in_no AND
                            a.ftstock_in_no='{$_REQUEST['ftstock_in_no']}'
                    ";
             $strSQL["n"][]=$ssql;
			 
            break;
    }
    return $strSQL;

   }


?>