<?php

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

            $strSQL['Update qty retur txpurchase_invoice_det'][]="
                    INSERT INTO txdelivery_order_return_hdr_bahan_baku
                            (
                                fttrans_no, ftstock_in_no, ftpo_no, ftsupplier_code,ftsj_no_supplier, fdtrans_date,
                                ftnotes, fftotal_qty, fnstatus,fntype,fntypesj,fntype_reff, 
                                fdcreated_date,fdmodified_date,ftcreated_user, ftmodified_user
                            )
                                VALUES ('{$_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']}','{$_REQUEST['fntype_reff']}',
                                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']}')";
                 }

            }
            //update qty retur di pi
            //-------------------------------
            /*for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                 $strSQL['Update qty retur txpurchase_invoice_det'][]="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]}'";
            }*/

          
            // transaksi stock keluar
            /*$ftstock_out_no=getAutoNumber('bbtn001','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, '0' 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":

//        $ssql="DELETE FROM txpurchase_return_det WHERE fnqty=0 AND ftpurchase_return_no='{$_REQUEST['ftpurchase_return_no']}' ";
//        $strSQL['txpurchase_return_det Index'][]= $ssql;
//
//        $ssql=" update
//                        txpurchase_return_det a,
//                        (select c.fnid,(select ifnull(count(d.fnid),0)+1
//                                        from txpurchase_return_det d
//                                        where d.fnid<c.fnid and d.ftpurchase_return_no='{$_REQUEST['ftpurchase_return_no']}') as m
//                                from  txpurchase_return_det c
//                                where c.ftpurchase_return_no='{$_REQUEST['ftpurchase_return_no']}'
//                                ) b
//                        set a.fnindex=b.m
//                        where a.ftpurchase_return_no='{$_REQUEST['ftpurchase_return_no']}' and a.fnid=b.fnid";
//
//         $strSQL['txpurchase_return_det Index'][]= $ssql;
        
            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']}'";


               //update qty retur di pi
            //-------------------------------
            /*for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                 $strSQL['Update qty retur txpurchase_invoice_det'][]="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]}'";
            }

              $strSQL['Batal Stock Out Det'][]="UPDATE  txstock_out_hdr_bahan_baku
                                                    SET fnstatus='9'
                                                  WHERE ftdocument_no='{$_REQUEST['fttrans_no']}'  AND
                                                                ftcategory_code='0'";

              
             //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(true);
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "simpan_revisi":

        /* 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]}'";
            }

              $strSQL['Batal Stock Out Det'][]="UPDATE  txstock_out_hdr_bahan_baku
                                                    SET fnstatus='9'
                                                  WHERE ftdocument_no='{$_REQUEST['fttrans_no']}'   AND
                                                                ftcategory_code='0'";

             //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;

   }

?>
