NJC__Documentation::DB

dbo.MG_MS_PRODUCTION_RETUR_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_PRODUCTION_RETUR_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)
,@ms_production_retur_id bigint 
,@allow_move_ms_date_business bit  
SELECT 
@data_by=left(ltrim(rtrim(isnull(tempTable.item.value('data_by[1]', 'varchar(100)'),''))),50)
,@ms_production_retur_id=isnull(tempTable.item.value('ms_production_retur_id[1]', 'bigint'),-1)
,@allow_move_ms_date_business =isnull(tempTable.item.value('move_business_date[1]', 'bit'),0)
FROM @PARAMETER.nodes('param/field')  tempTable(item) 
 
declare 
@check_ms_production_retur_id bigint
, @check_status_production_retur_id int
, @check_status_production_retur_name varchar(255) 
, @check_retur_no  varchar(255)
, @check_retur_date  date

SELECT        TOP (1) 
@check_ms_production_retur_id=ms_production_retur_id
, @check_status_production_retur_id=status_production_retur_id
, @check_status_production_retur_name=status_production_retur_name
, @check_retur_no=retur_no
, @check_retur_date=retur_date
FROM            vw_ms_production_retur_header
where ms_production_retur_id=@ms_production_retur_id
   

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


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

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

declare @result_date_business_id bigint,
  @result_date_business date,
  @result_date_business_id_from bigint,
  @result_date_business_from date,
  @is_move_date_business bit,
  @business_date date=@check_retur_date

EXEC  [dbo].[MG_MS_CHECK_BUSINESS_DATE_AND_AVG]
  @business_date = @business_date,
  @allow_move_ms_date_business = @allow_move_ms_date_business,
  @result_date_business_id = @result_date_business_id OUTPUT,
  @result_date_business = @result_date_business OUTPUT,
  @result_date_business_id_from = @result_date_business_id_from OUTPUT,
  @result_date_business_from = @result_date_business_from OUTPUT,
  @is_move_date_business = @is_move_date_business OUTPUT,
  @xStsno = @xStsno OUTPUT,
  @xStsdes = @xStsdes OUTPUT
   
   
if not(@xStsno='00')
begin
 goto f
end
 

declare @ms_production_retur_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_production_retur_h_status
                         (ms_production_retur_id, status_production_retur_id, status_info, create_date, create_by)
VALUES        (@ms_production_retur_id, 1, '',@now,@data_by)
set @ms_production_retur_h_status_id =SCOPE_IDENTITY ();

UPDATE       dt_ms_production_retur
SET       ms_production_retur_h_status_id=@ms_production_retur_h_status_id  
,receive_ndpbm_usd = @ndpbm_usd 
,ms_date_business_id_from =@result_date_business_id_from
,is_move_business_date =@is_move_date_business
,ms_date_business_id=@result_date_business_id
WHERE        (ms_production_retur_id = @ms_production_retur_id)

 
/* dt_ms_production_retur data sumber out date tidak boleh lebih besar dari doc date retur receive date retur tidak boleh lebih besar dari doc date retur kodisi: jika sumber out sudah terdaftar di posting final maka akan menjadi adjustment di avg berjalan */


INSERT INTO dt_ms_item_in_detail
    (
     mode_source
    , id_source_header
    , id_source
    , quantity
    , quantity_source
    , ms_item_id
    , quantity_pakai
    , create_date
    , create_by
    , modify_date
    , modify_by 
    , total_amont
    , total_amont_idr
    , total_amont_usd 
    , ms_date_business_id_from  
    , is_move_business_date  
    , is_adjustment
    , ms_date_business_id 
    , ms_item_in_move_avg_id
    , ori_ms_in_item_detail_id 
    )
output inserted.ms_in_item_detail_id,inserted.ms_item_id,inserted.ms_item_in_move_avg_id,inserted.is_adjustment  into  @ms_item_in_detail 
select
 5--mode_source RETUR
, @ms_production_retur_id--id_source_header
, ms_production_retur_detail_id -- id_source
, qty_retur -- quantity
, qty_retur-- quantity_source
, ms_item_id -- ms_item_id
, qty_retur-- quantity_pakai
, @now
, @data_by 
, @now
, @data_by  
, qty_retur * price_usd --total_amont
, qty_retur * price_usd * @ndpbm_usd   
, qty_retur * price_usd
, @result_date_business_id_from
, @is_move_date_business
, is_adjusment_avg
, @result_date_business_id
, dbo.fn_ms_find_item_in_move_avg_id(@result_date_business,ms_item_id) 
, vw_ms_production_retur_detail.ori_ms_in_item_detail_id
FROM            vw_ms_production_retur_detail
WHERE        (ms_production_retur_id = @ms_production_retur_id)
 
  
  
---CHECK DAILY AVG IN
 
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 @result_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=@is_move_date_business
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
) 

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,@result_date_business,@last_final_avg


declare @detail xml
set  @detail=(
 SELECT        
   ms_production_retur_id AS rID 
, status_production_retur_name AS r1 
, retur_no AS r2 
, dbo.date_to_str(retur_date ) AS r3 
, receive_no AS r4 
, dbo.date_to_str(receive_date ) AS r5 
, count_item AS r6 
, count_adjusment_avg AS r6a 
, max_doc_date  AS r6m 
, status_info AS r7 
, status_production_retur_id AS r8 
, ms_production_retur_h_status_id AS r9 
, dbo.date_to_str(status_date ) AS r10 
, status_by AS r11 
, dbo.date_to_str(create_date ) AS r12 
, create_by AS r13 
, dbo.date_to_str(modify_date ) AS r14 
, modify_by AS r15 
 FROM            vw_ms_production_retur_header
 where ms_production_retur_id=@ms_production_retur_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_PRODUCTION_RETUR_HEADER_APPROVE
--'12json2::1ABA0127810007'
GO

Used By

No items found