NJC__Documentation::DB

dbo.MG_MS_PRODUCTION_RETUR_HEADER_CANCEL

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_CANCEL
@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 
,@info varchar(255)

SELECT 
@data_by=left(ltrim(rtrim(isnull(tempTable.item.value('data_by[1]', 'varchar(100)'),''))),50)
,@info=left(ltrim(rtrim(isnull(tempTable.item.value('info[1]', 'varchar(300)'),''))),255)
,@ms_production_retur_id=isnull(tempTable.item.value('ms_production_retur_id[1]', 'bigint'),-1)
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 
, @check_ms_date_business date
, @check_ms_date_business_id bigint
, @check_is_move_business_date bit 
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
, @check_ms_date_business=ms_date_business
, @check_is_move_business_date=is_move_business_date 
, @check_ms_date_business_id=ms_date_business_id
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 sudah dalam status ' + @check_status_production_retur_name + ' tidak bisa di Approve';
goto f
end

 
 

--- CHECK APAKAH TERPAKAI SEBAGAI SUPPLY
if @check_status_production_retur_id=1
begin
 declare @check_avg_final_avg_name varchar(255)
 ,@check_avg_start_date varchar(255)
 ,@check_avg_end_date varchar(255)

 SELECT      top 1  
   @check_avg_final_avg_name=dt_ms_cost_final_avg.final_avg_name
 , @check_avg_start_date=dt_ms_cost_final_avg.start_date
 , @check_avg_end_date=dt_ms_cost_final_avg.end_date
 FROM            dt_ms_date_business INNER JOIN
        dt_ms_cost_final_avg ON dt_ms_date_business.ms_cost_final_avg_id = dt_ms_cost_final_avg.ms_cost_final_avg_id 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_date_business.ms_date_business_id =@check_ms_date_business_id) 

 if @check_avg_final_avg_name is not null
 begin 
  set @xStsno='02';
  set @xStsdes='Gagal, Dokumen sudah terkait FINAL AVG ' + @check_avg_final_avg_name ;
  goto f
 end 

 declare @result varchar(max)
 SET @resultX= ( 
 SELECT       
   'type : '+ mode_out_name + 
  +' ,Doc no: ' +  doc_code
  +' ,Doc Date: ' +  isnull(dbo.date_to_str(doc_date),'')
  +' ,Status : ' +  status_name
 + @pemisah 
 AS [text()] 
 FROM  
 ( 
  SELECT       
     ms_in_item_detail_id
   , id_source_header
   , mode_source
   , mode_out_id
   , mode_out_name
   , qty
   , doc_code
   , doc_date
   , info
   , ms_item_id
   , status_id
   , status_name
  FROM            vw_ms_item_in_out_to_memo_detail
  where not(status_id =-1) and (id_source_header=@ms_production_retur_id) and (mode_source=5)
 UNION ALL
  SELECT       
     ms_in_item_detail_id
   , id_source_header
   , mode_source
   , mode_out_id
   , mode_out_name
   , qty
   , doc_code
   , doc_date
   , info
   , ms_item_id
   , status_id
   , status_name
  FROM            vw_ms_item_in_out_to_car_line_detail
  where not(status_id =-1) and (id_source_header=@ms_production_retur_id) and (mode_source=5)
  UNION ALL
  SELECT       
     ms_in_item_detail_id
   , id_source_header
   , mode_source
   , mode_out_id
   , mode_out_name
   , qty
   , doc_code
   , doc_date
   , info
   , ms_item_id
   , status_id
   , status_name
  FROM            vw_ms_item_in_out_to_sto_detail
  where not(status_id =-1) and (id_source_header=@ms_production_retur_id) and (mode_source=5)
  UNION ALL
  SELECT       
     ms_in_item_detail_id
   , id_source_header
   , mode_source
   , mode_out_id
   , mode_out_name
   , qty
   , doc_code
   , doc_date
   , info
   , ms_item_id
   , status_id
   , status_name
  FROM            vw_ms_item_in_out_to_adjustment_detail
  where not(status_id =-1) and (id_source_header=@ms_production_retur_id) and (mode_source=5)
 )data
  
 For XML PATH (''))     
 SET @resultX= REPLACE(@resultX,@pemisah_replace,@pemisah);
 IF @resultX is not null
 BEGIN 
    SET @xStsno='17';
    SET @xStsdes='Gagal , Data Terpakai di sebagai supply dokumen : ' + @pemisah+  @pemisah+ @resultX + @pemisah+  @pemisah+ ', Cancel Terlebih dahulu dokumen terkait supply' ;
   GOto f
 end    
end     


declare @ms_production_retur_h_status_id bigint 

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, @info,@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   
WHERE        (ms_production_retur_id = @ms_production_retur_id)

  
COMMIT TRANSACTION;  
 
if @check_status_production_retur_id=1
begin 
 declare @ms_item xml 
 ,@last_final_avg bigint
 set @ms_item=(select ms_item_id as 'row/@id' from dt_ms_item_in_detail
 where mode_source =5 and id_source_header=@ms_production_retur_id
 group by ms_item_id for xml path('item')
 )  
 EXEC MG_CALCULATE_AVG_MOVE_BY_BUSINESS_DATE @ms_item,@check_ms_date_business,@last_final_avg
end 

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_CANCEL
--'12json2::1ABA0127810007'
GO

Used By

No items found