NJC__Documentation::DB

dbo.MG_MS_OUT_CHANGE_PART_LIST_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_OUT_CHANGE_PART_LIST_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_part_list_change_part_header_id bigint

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


if @ms_part_list_change_part_header_id =-1
begin
 set @xStsno='02';
 set @xStsdes='Gagal, Pilih data yang akan di edit' ;
goto f
end

declare  @check_ms_part_list_change_part_id bigint
  ,@check_ms_part_list_change_part_no varchar(255)
  ,@check_ms_part_list_change_part_date date
  ,@check_status_info  varchar(1000)
  ,@check_ms_part_list_change_part_status_id int
  ,@check_ms_part_list_change_part_status_name  varchar(255)
  ,@check_ms_item_id bigint
  ,@check_assets_list_id bigint

SELECT  top (1)     
  @check_ms_part_list_change_part_id=ms_part_list_change_part_id
 ,@check_ms_part_list_change_part_no= ms_part_list_change_part_no
 ,@check_ms_part_list_change_part_date= ms_part_list_change_part_date
 ,@check_status_info= status_info
 ,@check_ms_part_list_change_part_status_id= ms_part_list_change_part_status_id
 ,@check_ms_part_list_change_part_status_name= ms_part_list_change_part_status_name
 ,@check_ms_item_id = ms_item_id
 ,@check_assets_list_id = assets_list_id
FROM            vw_part_list_change_part_header
WHERE        (ms_part_list_change_part_id = @ms_part_list_change_part_header_id)

declare @check_assets_list_detail bigint
 
select top(1)
 @check_assets_list_detail = assets_list_detail_id
from dt_ms_assets_list_detail 
where assets_list_id = @check_assets_list_id


if @check_ms_part_list_change_part_id =-1
begin
 set @xStsno='02';
 set @xStsdes='Gagal, Pilih data yang akan di edit(2)' ;
goto f
end

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

if (SELECT        COUNT(*) AS Expr1
FROM            dt_ms_part_list_change_part_detail
WHERE        (ms_part_list_change_part_id = @ms_part_list_change_part_header_id))=0
begin
 set @xStsno='02';
 set @xStsdes='Gagal, Tidak ada detail , masukan detail item ' ;
goto f 
end 
   
if not(@xStsno='00')
begin
 goto f
end

declare @ms_part_list_change_part_h_status_id bigint

BEGIN TRANSACTION;  

 INSERT INTO dt_ms_part_list_change_part_h_status
   (ms_part_list_change_part_id, ms_part_list_change_part_status_id, status_info, create_date, create_by)
 VALUES  (@ms_part_list_change_part_header_id, 1, N'', @NOW, @data_by)

 set @ms_part_list_change_part_h_status_id=SCOPE_IDENTITY();

 update dt_ms_part_list_change_part_header
 set ms_part_list_change_part_h_status_id=@ms_part_list_change_part_h_status_id 
 where  ms_part_list_change_part_id=@ms_part_list_change_part_header_id 

 DELETE FROM dt_ms_assets_list_detail WHERE assets_list_id = @check_assets_list_id

 DECLARE @cur_ms_part_list_change_part_detail_id bigint
  , @cur_ms_item_id bigint
  , @cur_quantity decimal(38,4) 

 IF CURSOR_STATUS('global','loopchecking')>=-1
 BEGIN
  DEALLOCATE loopchecking
 END

 DECLARE loopchecking CURSOR FOR 
  SELECT
   ms_part_list_change_part_detail_id 
   , ms_item_id
   , sum(isnull(quantity_scrap,0))
  FROM dt_ms_part_list_out a
  INNER JOIN dt_ms_part_list_change_part_detail b ON a.ms_part_list_process_detail_id = b.ms_part_list_change_part_detail_id
  WHERE b.ms_part_list_change_part_id = @check_ms_part_list_change_part_id AND a.ms_process_id = -2
  GROUP BY ms_part_list_change_part_detail_id, ms_item_id
 OPEN loopchecking
 FETCH NEXT FROM loopchecking INTO @cur_ms_part_list_change_part_detail_id, @cur_ms_item_id, @cur_quantity
 WHILE @@FETCH_STATUS = 0 
 BEGIN
  
  DECLARE @ms_production_in_detail_id bigint
  , @doc_date date
  , @quantity decimal(18,4) 
  , @is_sumber int 

  DECLARE production_cursor2 CURSOR FOR  
   SELECT 
     ms_production_in_detail_id
   , doc_date
   , quantity
   , is_sumber
   FROM  dbo.fn_ms_auto_supply_production_part_list(@cur_ms_item_id, 0, @check_ms_part_list_change_part_date, @cur_quantity)
  OPEN production_cursor2  
  FETCH NEXT FROM production_cursor2 INTO @ms_production_in_detail_id, @doc_date, @quantity, @is_sumber
  WHILE @@FETCH_STATUS = 0  
  BEGIN  
 
  IF @is_sumber = 0
   begin
    INSERT INTO dt_ms_part_list_out (ms_item_out_detail_id, ms_trans_type_id, ms_part_list_process_detail_id, ms_process_id, quantity, quantity_new_part, quantity_recycle, create_date, create_by, modify_date, modify_by)
    VALUES (@ms_production_in_detail_id,1,@cur_ms_part_list_change_part_detail_id,2,@quantity,@quantity,0, @NOW, @data_by, @NOW, @data_by)
   end
  ELSE
   begin
    INSERT INTO dt_ms_part_list_out (ms_item_out_detail_id, ms_trans_type_id, ms_part_list_process_detail_id, ms_process_id, quantity, quantity_new_part, quantity_recycle, create_date, create_by, modify_date, modify_by)
    VALUES (@ms_production_in_detail_id,2,@cur_ms_part_list_change_part_detail_id,2,@quantity,0,@quantity, @NOW, @data_by, @NOW, @data_by)
   end

  FETCH NEXT FROM production_cursor2 INTO @ms_production_in_detail_id, @doc_date, @quantity, @is_sumber
  END
  CLOSE production_cursor2  
  DEALLOCATE production_cursor2

  FETCH NEXT FROM loopchecking INTO @cur_ms_part_list_change_part_detail_id, @cur_ms_item_id, @cur_quantity
 END
 CLOSE loopchecking    
 DEALLOCATE loopchecking

 ---------------------------------------------------------------------------------------------------------------

 --SELECT
 -- a.ms_part_list_process_detail_id
 -- , b.ms_item_id
 -- , sum(isnull(a.quantity_scrap,0)) as qty_scrap
 -- --, a.ms_part_list_out_id
 --FROM dt_ms_part_list_out a
 --INNER JOIN dt_ms_part_list_change_part_detail b ON a.ms_part_list_process_detail_id = b.ms_part_list_change_part_detail_id
 --INNER JOIN dt_ms_part_list_change_part_header c ON b.ms_part_list_change_part_id = c.ms_part_list_change_part_id
 --WHERE a.ms_process_id = -2 and b.ms_part_list_change_part_detail_id = @ms_part_list_change_part_header_id
 --GROUP BY a.ms_part_list_process_detail_id, b.ms_item_id

 DECLARE 
   @cur2_ms_part_list_process_detail_id bigint
 , @cur2_ms_item_id bigint
 , @cur2_quantity_scrap decimal(38,4)
 , @cur2_ms_part_list_out_id bigint

 IF CURSOR_STATUS('global','loopchecking3')>=-1
 BEGIN
  DEALLOCATE loopchecking3
 END

 DECLARE loopchecking3 CURSOR FOR
  SELECT
     a.ms_part_list_process_detail_id 
   , b.ms_item_id
   , sum(isnull(a.quantity_scrap,0)) as qty_scrap
   --, a.ms_part_list_out_id
  FROM dt_ms_part_list_out a 
  INNER JOIN dt_ms_part_list_change_part_detail b ON a.ms_part_list_process_detail_id = b.ms_part_list_change_part_detail_id
  INNER JOIN dt_ms_part_list_change_part_header c ON b.ms_part_list_change_part_id = c.ms_part_list_change_part_id
  WHERE a.ms_process_id = -2 and b.ms_part_list_change_part_detail_id = @ms_part_list_change_part_header_id
  GROUP BY a.ms_part_list_process_detail_id, b.ms_item_id
 OPEN loopchecking3
 FETCH NEXT FROM loopchecking3 INTO @cur2_ms_part_list_process_detail_id ,@cur2_ms_item_id, @cur2_quantity_scrap 
 WHILE @@FETCH_STATUS = 0 
 BEGIN

  IF @cur2_quantity_scrap > 0
  BEGIN 
   insert into dt_ms_part_list_scrap (ms_item_id,ms_part_list_disamble_detail_id,quantity,quantity_used, trans_type_id, create_date, create_by, modify_date, modify_by) 
   VALUES (@cur_ms_item_id, @cur2_ms_part_list_process_detail_id, @cur2_quantity_scrap, 0, -2, @NOW, @data_by, @NOW, @data_by) 
  END
   
  FETCH NEXT FROM loopchecking3 INTO @cur2_ms_part_list_process_detail_id ,@cur2_ms_item_id, @cur2_quantity_scrap
 END
 CLOSE loopchecking3    
 DEALLOCATE loopchecking3

  --UPDATE BOM PART LIST
 insert into dt_ms_assets_list_detail
 (
  assets_list_id,
  ms_part_list_out_id,
  quantity,
  quantity_recycle,
  quantity_total,
  quantity_used,
  as_change_part
 )
 SELECT 
    b.assets_list_id
  , c.ms_part_list_out_id
  , isnull(c.quantity_new_part,0)
  , isnull(c.quantity_recycle,0)
  , isnull(c.quantity_new_part,0) + isnull(c.quantity_recycle,0) as quantity_total
  , quantity_used
  , 1 as as_change_part
 FROM dt_ms_part_list_change_part_detail a
 INNER JOIN dt_ms_part_list_change_part_header b on b.ms_part_list_change_part_id = a.ms_part_list_change_part_id
 INNER JOIN dt_ms_part_list_out c on a.ms_part_list_change_part_detail_id = c.ms_part_list_process_detail_id
 WHERE  (b.assets_list_id = @check_assets_list_id) AND (c.ms_process_id = 2 or c.ms_process_id = -2)

 --select * from dt_ms_part_list_out
 --select * from dt_ms_assets_list_detail
 --select * from dt_ms_part_list_scrap

commit TRANSACTION; 
DECLARE @detail xml

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