NJC__Documentation::DB

dbo.MG_MS_OUT_DISAMBLE_PART_LIST_HEADER_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_OUT_DISAMBLE_PART_LIST_HEADER_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 @detail 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)
,@disamble_no varchar(255)
,@disamble_date date
,@disamble_assets_list_id bigint
,@disamble_assets_list_name varchar(100)
,@disamble_car_line_no bigint

SELECT 
 @data_by=left(ltrim(rtrim(isnull(tempTable.item.value('data_by[1]', 'varchar(100)'),''))),50)
,@disamble_no=left(ltrim(rtrim(isnull(tempTable.item.value('disamble_no[1]', 'varchar(255)'),''))),255)
,@disamble_date=dbo.str_to_date(isnull(tempTable.item.value('disamble_date[1]', 'varchar(255)'),''))
,@disamble_assets_list_id=isnull(tempTable.item.value('disamble_assets_list_id[1]', 'bigint'),-1)
,@disamble_assets_list_name=left(ltrim(rtrim(isnull(tempTable.item.value('disamble_assets_list_name[1]', 'varchar(100)'),''))),1000)
,@disamble_car_line_no=isnull(tempTable.item.value('disamble_car_line_no[1]', 'bigint'),-1)
FROM @PARAMETER.nodes('param/field')  tempTable(item)

select @disamble_car_line_no

if @disamble_no =''
 begin
  set @xStsno='02';
  set @xStsdes='Gagal, Nomor Disamble tidak boleh kosong' ;
  goto f
 end

if @disamble_date is null
 begin
  set @xStsno='02';
  set @xStsdes='Gagal, Tanggal Disamble tidak boleh kosong' ;
  goto f
 end

if @disamble_assets_list_id  = 0
 begin
  set @xStsno='02';
  set @xStsdes='Gagal, Assets List tidak boleh kosong' ;
  goto f
 end

if @disamble_car_line_no= 0
 begin
  set @xStsno='02';
  set @xStsdes='Gagal,Car line No wajib' ;
 goto f
 end

if exists(select * from vw_part_list_disamble_header where  ms_part_list_disamble_no = @disamble_no and not(ms_part_list_disamble_status_id =-1))
 begin
  set @xStsno='02';
  set @xStsdes='Gagal, Nomor Production Sudah Ada' ;
  goto f
 end

if @disamble_assets_list_id=-1
 begin
  set @xStsno='02';
  set @xStsdes='Gagal, Assets List No wajib di isi' ;
  goto f
 end

declare @check_ms_item_id bigint ,
  @check_ms_part_list_production_id bigint

SELECT  top(1) @check_ms_item_id = ms_item_id, @check_ms_part_list_production_id = ms_part_list_production_id
FROM            dt_ms_assets_list
WHERE        (assets_list_id = @disamble_assets_list_id) 

declare @check_quantity decimal(18,4)
SELECT  top(1) @check_quantity = quantity 
FROM            dt_ms_part_list_production_header
WHERE        (ms_part_list_production_id = @check_ms_part_list_production_id) 
 
if not(@xStsno='00')
 begin
  goto f
 end

declare @ms_part_list_disamble_id bigint
declare @ms_part_list_disamble_h_status_id bigint

DECLARE @temp_table table(
 id bigint
 , ms_item_id bigint
)

BEGIN TRANSACTION;    

 INSERT INTO dt_ms_part_list_disamble_header
  (ms_part_list_disamble_no, ms_part_list_disamble_date, ms_part_list_production_id, ms_item_id, assets_list_id, ms_car_line_no, create_date, create_by, modify_date, modify_by)
 VALUES(@disamble_no, @disamble_date, @check_ms_part_list_production_id, @check_ms_item_id, @disamble_assets_list_id, @disamble_car_line_no, @NOW, @data_by, @NOW, @data_by)

 SET @ms_part_list_disamble_id=SCOPE_IDENTITY();

 INSERT INTO dt_ms_part_list_disamble_h_status(ms_part_list_disamble_id, ms_part_list_disamble_status_id, status_info, create_date, create_by)
 VALUES        (@ms_part_list_disamble_id, 0, N'',@now,@data_by)

 SET @ms_part_list_disamble_h_status_id=SCOPE_IDENTITY();
 
 UPDATE dt_ms_part_list_disamble_header
 SET ms_part_list_disamble_h_status_id=@ms_part_list_disamble_h_status_id
 WHERE  ms_part_list_disamble_id=@ms_part_list_disamble_id

  --UPDATE DETAIL
 INSERT INTO dt_ms_part_list_disamble_detail(ms_part_list_disamble_id, ms_item_id, quantity, create_date, create_by, modify_date, modify_by)
 OUTPUT  INSERTED.ms_part_list_disamble_detail_id, INSERTED.ms_item_id
 INTO @temp_table
 SELECT
 @ms_part_list_disamble_id, c.ms_item_id, sum(ISNULL(a.quantity_total,0) - ISNULL(a.quantity_used,0)), @NOW, @data_by, @NOW, @data_by
 FROM dt_ms_assets_list_detail a
  Left Join dt_ms_part_list_out b ON a.ms_part_list_out_id = b.ms_part_list_out_id
  Left Join dt_ms_part_list_production_detail c ON b.ms_part_list_process_detail_id = c.ms_part_list_production_detail_id
 WHERE a.assets_list_id = @disamble_assets_list_id
 GROUP BY c.ms_item_id

 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)
 SELECT 
  b.ms_item_out_detail_id
  , b.ms_trans_type_id
  , d.id
  , -1
  , sum(isnull(a.quantity_total,0)) - sum(isnull(a.quantity_used,0)) as quantity
  , case when sum(ISNULL(a.quantity,0)) > 0 then sum(ISNULL(a.quantity,0)) - sum(ISNULL(a.quantity_used,0)) end as qty_new_part
  , case when sum(ISNULL(a.quantity_recycle,0)) > 0 then sum(ISNULL(a.quantity_recycle,0)) - sum(ISNULL(a.quantity_used,0)) end as qty_recycle
  , @NOW
  , @data_by
  , @NOW
  , @data_by 
 FROM dt_ms_assets_list_detail a
 LEFT OUTER JOIN dt_ms_part_list_out b ON a.ms_part_list_out_id = b.ms_part_list_out_id
 LEFT OUTER JOIN dt_ms_part_list_production_detail c ON b.ms_part_list_process_detail_id = c.ms_part_list_production_detail_id
 LEFT OUTER JOIN @temp_table d ON c.ms_item_id = d.ms_item_id
 WHERE a.assets_list_id = @disamble_assets_list_id
 AND (isnull(a.quantity,0) + isnull(a.quantity_recycle,0)) - isnull(a.quantity_used,0) > 0
 GROUP BY c.ms_item_id, b.ms_item_out_detail_id, b.ms_trans_type_id, d.id, b.ms_process_id
 ORDER BY b.ms_trans_type_id

 --select * from dt_ms_part_list_out
 --select * from vw_part_list_disamble_detail

commit TRANSACTION; 

set  @detail=(
SELECT        
   ms_part_list_disamble_id AS rID 
 , ms_part_list_disamble_status_name AS r1 
 , @disamble_no AS r2 
 , dbo.date_to_str(@disamble_date ) AS r3 
 , status_info AS r4  
FROM            vw_part_list_disamble_header
WHERE ms_part_list_disamble_id=@ms_part_list_disamble_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