﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[ECNReassignOriginator]    Script Date: 11/21/2024 11:29:04 AM ******/
SET
	ANSI_NULLS ON
GO
SET
	QUOTED_IDENTIFIER ON
GO
	CREATE PROCEDURE [dbo].[ECNReassignOriginator] @ECNNumber int,
	@NewOriginatorID int,
	@Comments varchar(500),
	@UserID int AS BEGIN
SET
	NOCOUNT ON;

SET
	XACT_ABORT ON;

BEGIN TRANSACTION
INSERT INTO
	EventLog(
		[IssueID],
		[SysDocumentID],
		[UserID],
		[DocumentType],
		[OperationType],
		[Comments],
		[InsertTimeStamp]
	)
SELECT
	@ECNNumber,
	NULL,
	@UserID,
	'ECN',
	'Reassign Originator',
	LEFT(
		'Originator changed from ' + ISNULL(
			old.LoginID,
			CONVERT(varchar(20), ECN.OriginatorID)
		) + ' to ' + ISNULL(
			new.LoginID,
			CONVERT(varchar(20), @NewOriginatorID)
		) + ' by ' + ISNULL(me.LoginID, CONVERT(varchar(20), @UserID)) + ' comments: ' + @Comments,
		500
	),
	GETDATE()
FROM
	ECN
	LEFT OUTER JOIN Users old ON old.UserID = ECN.OriginatorID
	LEFT OUTER JOIN Users new ON new.UserID = @NewOriginatorID
	LEFT OUTER JOIN Users me ON me.UserID = @UserID
WHERE
	ECNNumber = @ECNNumber
UPDATE
	ECN
SET
	OriginatorID = @NewOriginatorID
WHERE
	ECNNumber = @ECNNumber COMMIT TRANSACTION
END
GO