NJC__Documentation::DB

dbo.MG_MS_ADJUSTMENT_HEADER_APPROVE

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

Name Data Type Length Description
@PARAMETER xml -1

SQL Script

SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
create procedure dbo.MG_MS_ADJUSTMENT_HEADER_APPROVE
@PARAMETER xml='' 
---XencX---with encryption
as

SET TRANSACTION ISOLATION LEVEL  READ UNCOMMITTED

declare @pemisah varchar(255)
declare @pemisah_replace varchar(255)
set @pemisah_replace='<br />'
--set @pemisah=CHAR(13) + CHAR(10) + '
'

set @pemisah = '
'


declare @resultX varchar(max)
declare @resultX2 varchar(max)

declare @XmlSts xml
declare @raw xml
declare @xStsnoheader varchar(2),
  @xStsno varchar(2),
  @xStsdes varchar(max)
   
set @xStsnoheader='07'
set @xStsno='00';
set @xStsdes='sukses'
 
BEGIN TRY
SET NOCOUNT ON
DECLARE @NOW datetime
set @NOW =GETDATE() 

DECLARE 
@data_by varchar(50)
,@header_id bigint 
,@move_business_date bit 

SELECT 
@data_by=left(ltrim(rtrim(isnull(tempTable.item.value('data_by[1]', 'varchar(100)'),''))),50)
,@header_id=isnull(tempTable.item.value('header_id[1]', 'bigint'),-1)
,@move_business_date =isnull(tempTable.item.value('move_business_date[1]', 'bit'),0)
FROM @PARAMETER.nodes('param/field')  tempTable(item) 
 

declare  
@check_header_ms_adj_sto_id bigint, 
@check_header_doc_code varchar(255), 
@check_header_doc_date date , 
@check_header_status_adj_sto_id int , 
@check_header_status_adj_sto_name varchar(255), 
@check_header_ms_date_business_id bigint, 
@check_header_is_move_business_date bit , 
@check_header_ms_date_business_id_from bigint

SELECT        TOP (1) 
@check_header_ms_adj_sto_id=ms_adj_sto_id, 
@check_header_doc_code=doc_code, 
@check_header_doc_date=doc_date, 
@check_header_status_adj_sto_id=status_adj_sto_id, 
@check_header_status_adj_sto_name=status_adj_sto_name, 
@check_header_ms_date_business_id=ms_date_business_id, 
@check_header_is_move_business_date=is_move_business_date, 
@check_header_ms_date_business_id_from=ms_date_business_id_from
FROM            vw_ms_adjustment_header
WHERE        (ms_adj_sto_id = @header_id)
 

if @check_header_ms_adj_sto_id  is  null
begin
 set @xStsno='02';
 set @xStsdes='Pilih data yang akan di approve(2)' ;
goto f
end


if @check_header_status_adj_sto_id=-1  
begin
 set @xStsno='02';
 set @xStsdes='Gagal, Dokumen dengan status ' + @check_header_status_adj_sto_name + ' tidak bisa di Approve';
goto f
end

    
if @check_header_status_adj_sto_id=1 
begin
 set @xStsno='02';
 set @xStsdes='Gagal, Dokumen dengan status ' + @check_header_status_adj_sto_name + ' tidak bisa di Approve Lagi';
goto f
end
 

declare @check_ms_cost_final_avg_id  bigint
,@check_start_date date 
,@check_end_date date
,@check_info varchar(1000)
,@check_final_avg_name varchar(255)
SELECT  top (1)      
@check_ms_cost_final_avg_id=dt_ms_cost_final_avg.ms_cost_final_avg_id
, @check_start_date=dt_ms_cost_final_avg.start_date
, @check_end_date=dt_ms_cost_final_avg.end_date
, @check_info=dt_ms_cost_final_avg.info
, @check_final_avg_name=dt_ms_cost_final_avg.final_avg_name
FROM            dt_ms_cost_final_avg INNER JOIN
                         dt_ms_cost_final_avg_h_status ON dt_ms_cost_final_avg.ms_cost_final_avg_h_status_id = dt_ms_cost_final_avg_h_status.ms_cost_final_avg_h_status_id
WHERE        
(dt_ms_cost_final_avg_h_status.ms_cost_final_avg_status_id = 1) 
AND (dt_ms_cost_final_avg.start_date >=@check_header_doc_date) 
AND (dt_ms_cost_final_avg.end_date <=@check_header_doc_date)


declare  @business_date date =@check_header_doc_date  

if @check_ms_cost_final_avg_id is not null
begin
 if  @move_business_date=0 
 begin 
  set @xStsno='02';
  set @xStsdes='Gagal, Sudah Ada Posting FINAL AVG = ' + @check_final_avg_name   + @pemisah +
  'START DATE =' +  +dbo.date_to_str(@check_start_date)   + @pemisah +
  'END DATE =' +  +dbo.date_to_str(@check_end_date)   + @pemisah +
  'Untuk Tanggal Dokumen ' +dbo.date_to_str(@check_header_doc_date)  + '.'   + @pemisah +
  'Pilih Move Business Date untuk mencatat dalam posting FINAL selanjut nya' 
  goto f
 end
 else
 begin

  set @business_date=( SELECT   top(1)     dateadd(D ,1, dt_ms_cost_final_avg.end_date)
   FROM            dt_ms_cost_final_avg INNER JOIN
          dt_ms_cost_final_avg_h_status ON dt_ms_cost_final_avg.ms_cost_final_avg_h_status_id = dt_ms_cost_final_avg_h_status.ms_cost_final_avg_h_status_id
   WHERE        (dt_ms_cost_final_avg_h_status.ms_cost_final_avg_status_id = 1)
   ORDER BY dt_ms_cost_final_avg.end_date DESC 
  )
 end 
end  
else
begin 
set @move_business_date=0
end


 

declare @ms_date_business_id bigint 
SELECT    top (1) @ms_date_business_id =   ms_date_business_id
FROM            dt_ms_date_business
WHERE        (ms_date_business = @business_date)
 
if @ms_date_business_id is null
begin
 set @ms_date_business_id=cast(replace(convert(varchar(10),@business_date,121),'-','') as int)
 INSERT INTO dt_ms_date_business
        (ms_date_business_id, ms_date_business, ms_cost_final_avg_id)
 VALUES        (@ms_date_business_id,@business_date, - 1)  
end
   
declare @ms_date_business_id_from bigint 
SELECT    top (1) @ms_date_business_id_from =   ms_date_business_id
FROM            dt_ms_date_business
WHERE        (ms_date_business = @check_header_doc_date) 
 
if @ms_date_business_id_from is null
begin
 set @ms_date_business_id_from=cast(replace(convert(varchar(10),@check_header_doc_date,121),'-','') as int)
 INSERT INTO dt_ms_date_business
        (ms_date_business_id, ms_date_business, ms_cost_final_avg_id)
 VALUES        (@ms_date_business_id_from,@check_header_doc_date, - 1)  
end
declare @ms_adj_sto_h_status_id bigint



declare @ms_item_in_detail table(
  ms_in_item_detail_id bigint primary key
, ms_item_id bigint
, ms_item_in_move_avg_id bigint
, is_adjustment bit)


declare @new_daily_in_item table (
 ms_item_in_move_avg_id bigint 
,ms_date_business_id int 
,ms_item_id bigint 
) 
 

declare  @ndpbm_usd decimal(18,4)=dbo.get_rate_valuta( @business_date,'USD')

BEGIN TRANSACTION;    

INSERT INTO dt_ms_adj_sto_h_status
                         (ms_adj_sto_id, status_adj_sto_id, status_info, create_date, create_by)
VALUES        (@header_id, 1, '',@now,@data_by)
set @ms_adj_sto_h_status_id =SCOPE_IDENTITY ();

UPDATE       dt_ms_adj_sto
SET       ms_adj_sto_h_status_id=@ms_adj_sto_h_status_id   
,ms_date_business_id_from =@ms_date_business_id_from
,is_move_business_date =@move_business_date
,ms_date_business_id=@ms_date_business_id
WHERE        (ms_adj_sto_id = @header_id)
 
update dt_ms_item_in_detail
set ms_date_business_id_from  =@ms_date_business_id_from
, is_move_business_date =@move_business_date
, ms_date_business_id =@ms_date_business_id
, ms_item_in_move_avg_id=dbo.fn_ms_find_item_in_move_avg_id(@business_date,ms_item_id)  
where mode_source=3
and    (id_source_header = @header_id)
and quantity_pakai>0

declare @new_daily_out_item table (
 ms_item_out_move_avg_id bigint 
,ms_date_business_id int 
,ms_item_id bigint 
)

insert into @ms_item_in_detail (
ms_in_item_detail_id,ms_item_id,ms_item_in_move_avg_id,is_adjustment
)
select ms_in_item_detail_id,ms_item_id,ms_item_in_move_avg_id,is_adjustment 
from dt_ms_item_in_detail 
where mode_source=3 and quantity_pakai>0
and    (id_source_header = @header_id) 

---CHECK DAILY AVG IN UNTUK ITEM YANG JUMLAH LEBIH BESAR DARI 0
 
INSERT INTO dt_ms_item_daily_in_move_avg
    (ms_date_business_id, ms_item_id, total_qty_avg_move, price_avg_move_idr, price_avg_move_usd, ms_item_h_price_id,ms_cost_final_avg_id)
output inserted.ms_item_in_move_avg_id,inserted.ms_date_business_id,inserted.ms_item_id into @new_daily_in_item 
select @ms_date_business_id,ms_item_id , 0, 0, 0, - 1, - 1 from @ms_item_in_detail
where ms_item_in_move_avg_id=-1 or ms_item_in_move_avg_id is null 
group by ms_item_id   

update @ms_item_in_detail
set ms_item_in_move_avg_id=isnull((SELECT  top(1)   xnew.ms_item_in_move_avg_id from    @new_daily_in_item as xnew  where xnew.ms_item_id  = [@ms_item_in_detail].ms_item_id)
      ,-1) 
where ms_item_in_move_avg_id= -1 or ms_item_in_move_avg_id is null 

UPDATE       dt_ms_item_in_detail
SET                ori_ms_in_item_detail_id = data_detail.ms_in_item_detail_id
,ms_item_in_move_avg_id= data_detail.ms_item_in_move_avg_id
,is_move_business_date=@move_business_date
FROM            dt_ms_item_in_detail INNER JOIN
       @ms_item_in_detail AS data_detail ON dt_ms_item_in_detail.ms_in_item_detail_id = data_detail.ms_in_item_detail_id
where exists(
select * from @ms_item_in_detail as xfilter
where dt_ms_item_in_detail.ms_in_item_detail_id=xfilter.ms_in_item_detail_id
) 


---- CARI AVG MOVE OUT BERDASARKAN TANGGAL YANG SAMA
UPDATE       dt_ms_item_in_detail
SET                ms_item_out_move_avg_id = dt_ms_item_daily_out_move_avg.ms_item_out_move_avg_id
 ,is_move_business_date=@move_business_date
FROM            dt_ms_item_in_detail INNER JOIN
                         dt_ms_item_daily_out_move_avg ON dt_ms_item_in_detail.ms_item_id = dt_ms_item_daily_out_move_avg.ms_item_id
where mode_source=3 and quantity_pakai<0
and    (id_source_header = @header_id) 
 AND (dt_ms_item_daily_out_move_avg.ms_date_business_id = @ms_date_business_id)
  


INSERT INTO dt_ms_item_daily_out_move_avg
                         (ms_date_business_id, ms_item_in_move_avg_id, ms_item_id, ms_item_h_price_id,ms_cost_final_avg_id) 
 output inserted.ms_item_out_move_avg_id,inserted.ms_date_business_id,inserted.ms_item_id into  @new_daily_out_item
SELECT      
  @ms_date_business_id
 , ( SELECT        TOP (1) dt_ms_item_daily_in_move_avg.ms_item_in_move_avg_id
  FROM            dt_ms_item_daily_in_move_avg INNER JOIN
         dt_ms_date_business ON dt_ms_item_daily_in_move_avg.ms_date_business_id = dt_ms_date_business.ms_date_business_id
  WHERE        (dt_ms_item_daily_in_move_avg.ms_item_id = dt_ms_item_in_detail.ms_item_id ) AND (dt_ms_date_business.ms_date_business <= @business_date)
  ORDER BY dt_ms_date_business.ms_date_business DESC 
 ) 
,ms_item_id
,-1
,-1 
from dt_ms_item_in_detail
where mode_source=3 and quantity_pakai<0
and    (id_source_header = @header_id)  
and nullif(ms_item_out_move_avg_id,-1) is null
group by ms_item_id

         
if (select count(*) from @new_daily_out_item )>0
begin
 UPDATE       dt_ms_item_in_detail 
 SET                ms_item_out_move_avg_id = new_avg.ms_item_out_move_avg_id
 ,is_move_business_date=@move_business_date
 FROM            dt_ms_item_in_detail INNER JOIN
          @new_daily_out_item AS new_avg ON dt_ms_item_in_detail.ms_item_id = new_avg.ms_item_id
 WHERE        (dt_ms_item_in_detail.ms_item_out_move_avg_id = - 1 OR
        dt_ms_item_in_detail.ms_item_out_move_avg_id IS NULL) 
        and mode_source=3 and quantity_pakai<0
and    (id_source_header = @header_id) 
end







COMMIT TRANSACTION;  

declare @ms_item xml 
,@last_final_avg bigint
set @ms_item=(select ms_item_id as 'row/@id' from @ms_item_in_detail
where is_adjustment =1
group by ms_item_id for xml path('item')
) 

EXEC MG_CALCULATE_AVG_MOVE_BY_BUSINESS_DATE @ms_item,@business_date,@last_final_avg


  declare @detail xml
  set @detail =(
select
   ms_adj_sto_id AS rID 
, status_adj_sto_name AS r1 
, doc_code AS r2 
, dbo.date_to_str(doc_date ) AS r3 
, info AS r3a
, total_detail AS r4 
, total_item AS r4a 
, count_plus AS r4b 
, count_minus AS r4c 
, dbo.date_to_str(create_date ) AS r5 
, create_by AS r6 
, dbo.date_to_str(modify_date ) AS r7 
, modify_by AS r8 
, status_adj_sto_id AS r9 
, status_info AS r10 
, dbo.date_to_str(status_date ) AS r11 
, status_by AS r12 
from vw_ms_adjustment_header
 where ms_adj_sto_id =@header_id 

 for xml path('detail') )
f:

set nocount off

END TRY  
BEGIN CATCH
 IF (@@TRANCOUNT > 0)  ROLLBACK TRAN;
 
 set @xStsno='88'
 set @xStsdes= ERROR_MESSAGE() 
END CATCH

g:
set @XmlSts=(select @xStsnoheader + @xStsno as 'no', @xStsdes as 'des' for xml path('sts'))
 select  CAST( (select @XmlSts,isnull(@detail ,'') ,isnull(@raw ,'')   for xml path ('result')
)  as XML)  as dt

--GO

--EXEC MG_MS_ADJUSTMENT_HEADER_APPROVE
--'12json2::1ABA0127810007'
GO

Used By

No items found