dbo.MG_MS_STO_DETAIL_ADD

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_STO_DETAIL_ADD
@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 
,@source_detail_id bigint 
,@quantity decimal(38,4) 
,@info varchar(255)
 
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)
,@source_detail_id=isnull(tempTable.item.value('source_detail_id[1]', 'bigint'),-1)
,@quantity=dbo.StrToDec4(isnull( tempTable.item.value('quantity[1]', 'varchar(255)'),'0'))
,@info=left(ltrim(rtrim(isnull(tempTable.item.value('info[1]', 'varchar(300)'),''))),255)
FROM @PARAMETER.nodes('param/field')  tempTable(item)
 
if @source_detail_id =-1
begin
 set @xStsno='02';
 set @xStsdes='Gagal, Pilih data yang akan di sto' ;
goto f
end 

declare 
  @check_ms_adj_sto_id bigint
, @check_doc_code varchar(255)
, @check_doc_date date
, @check_status_adj_sto_id int
, @check_status_adj_sto_name varchar(255)
, @check_ms_date_business_id int
, @check_is_move_business_date bit
, @check_ms_date_business_id_from int 
, @check_cancel_ms_cost_final_avg_id bigint
, @check_approve_ms_cost_final_avg_id bigint

SELECT  top (1)      
  @check_ms_adj_sto_id=ms_adj_sto_id
, @check_doc_code=doc_code
, @check_doc_date=doc_date
, @check_status_adj_sto_id=status_adj_sto_id
, @check_status_adj_sto_name=status_adj_sto_name
, @check_ms_date_business_id= ms_date_business_id
, @check_is_move_business_date=is_move_business_date
, @check_ms_date_business_id_from=ms_date_business_id_from
, @check_cancel_ms_cost_final_avg_id=cancel_ms_cost_final_avg_id
, @check_approve_ms_cost_final_avg_id=approve_ms_cost_final_avg_id
FROM            vw_ms_sto_header
WHERE        (ms_adj_sto_id = @header_id)


if @check_ms_adj_sto_id =-1
begin
 set @xStsno='02';
 set @xStsdes='Gagal, Pilih data header(2)' ;
goto f
end

if not(@check_status_adj_sto_id=0)
begin
 set @xStsno='02';
 set @xStsdes='Gagal, Dokumen dengan status ' + @check_status_adj_sto_name + ' tidak bisa di rubah ' ;
goto f 
end
 
if @quantity =0
begin
 set @xStsno='02';
 set @xStsdes='Gagal, Jumlah sto harus lebih besar atau kurang dari 0 ' ;
goto f
end

declare @check_souce_ms_in_item_detail_id bigint
 , @check_souce_mode_source_name_2 varchar(255)
 , @check_souce_bc_no varchar(255)
 , @check_souce_bc_date date
 , @check_souce_status_id int
 , @check_souce_status_name varchar(255)
 , @check_souce_quantity_pakai decimal(38,4)
 , @check_souce_quantity_sisa decimal(38,4)
 , @check_souce_part_no varchar(255)
 , @check_souce_part_name varchar(255)

 SELECT   top(1)    
   @check_souce_ms_in_item_detail_id= ms_in_item_detail_id
 , @check_souce_mode_source_name_2= mode_source_name_2
 , @check_souce_bc_no=bc_no
 , @check_souce_bc_date=bc_date
 , @check_souce_status_id=status_id
 , @check_souce_status_name=status_name
 , @check_souce_quantity_pakai=quantity_pakai
 , @check_souce_quantity_sisa=quantity_sisa
 , @check_souce_part_no=part_no
 , @check_souce_part_name=part_name
FROM            vw_ms_item_in_detail
WHERE        (ms_in_item_detail_id = @source_detail_id)

 
if not(@check_souce_status_id=1)
begin
 set @xStsno='02';
 set @xStsdes='Gagal, Dokumen ' + @check_souce_mode_source_name_2 +' dalam status ' + @check_souce_status_name + ' Check Status Dokumen ' + @check_souce_mode_source_name_2 
    ;
goto f
end

if @quantity<0 
begin 
 if abs(@quantity) >@check_souce_quantity_sisa
 begin
  set @xStsno='02';
  set @xStsdes='Gagal, Jumlah melebihi sisa stock dokumen ' + 
     @pemisah + ' SISA : ' + cast(@check_souce_quantity_sisa  as varchar(255))  + 
     @pemisah + ' QTY : ' + cast(@quantity  as varchar(255)) 
     ;
 goto f
 end 
end



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 
) 
 
BEGIN TRANSACTION;    

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 
    , info
    )
    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
 4--mode_source RETUR
, @header_id--id_source_header
, @source_detail_id -- id_source
, @quantity -- quantity
, @quantity-- quantity_source
, ms_item_id -- ms_item_id
, @quantity-- quantity_pakai
, @now
, @data_by 
, @now
, @data_by  
, @quantity * case when vw_ms_item_in_detail_for_bahan_baku.total_amont=0 or  vw_ms_item_in_detail_for_bahan_baku.JUMLAH =0 then 0 else   vw_ms_item_in_detail_for_bahan_baku.total_amont/ vw_ms_item_in_detail_for_bahan_baku.JUMLAH end
, @quantity * case when vw_ms_item_in_detail_for_bahan_baku.TOTAL_HARGA_RUPIAH=0 or  vw_ms_item_in_detail_for_bahan_baku.JUMLAH =0 then 0 else   vw_ms_item_in_detail_for_bahan_baku.TOTAL_HARGA_RUPIAH/ vw_ms_item_in_detail_for_bahan_baku.JUMLAH end
, @quantity * case when vw_ms_item_in_detail_for_bahan_baku.TOTAL_HARGA_USD=0 or  vw_ms_item_in_detail_for_bahan_baku.JUMLAH =0 then 0 else    vw_ms_item_in_detail_for_bahan_baku.TOTAL_HARGA_USD/ vw_ms_item_in_detail_for_bahan_baku.JUMLAH end
, @check_ms_date_business_id_from
, @check_is_move_business_date
,  case when mode_source =1 then 1 
  when mode_source=2 then case when  header_approve_ms_cost_final_avg_id =-1 then 0 else 1 end
  else 0
  end  as is_adjusment_avg
, @check_ms_date_business_id
,-1-- dbo.fn_ms_find_item_in_move_avg_id((SELECT TOP (1) ms_date_business
--FROM dt_ms_date_business
--WHERE (ms_date_business_id = @check_ms_date_business_id)),ms_item_id)
, vw_ms_item_in_detail_for_bahan_baku.ori_ms_in_item_detail_id
, @info 
FROM            vw_ms_item_in_detail_for_bahan_baku
WHERE        (ms_in_item_detail_id = @source_detail_id)
 
--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 @check_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=@check_is_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
--)


COMMIT TRANSACTION; 

declare @detail xml
--set @detail=(
-- SELECT
-- header_id AS rID
--, status_production_sto_name AS r1
--, sto_no AS r2
--, dbo.date_to_str(sto_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_sto_id AS r8
--, ms_production_sto_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_sto_header
-- where header_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

Used By

No items found