﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[SubmitForApproval_OLD]    Script Date: 11/21/2024 11:29:05 AM ******/
SET
	ANSI_NULLS ON
GO
SET
	QUOTED_IDENTIFIER ON
GO
	-- =============================================
	-- Author:		<Author,,Name>
	-- Create date: <Create Date,,>
	-- Description:	<Description,,>
	-- =============================================
	CREATE PROCEDURE [dbo].[SubmitForApproval_OLD] @IssueID INT,
	@UserID INT,
	@PERequired BIT,
	@MRBRequired BIT,
	@DocumentTypeID INT,
	@AppoverCount INT OUT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
SET
	NOCOUNT ON;

CREATE TABLE #TempApprovals (
[IssueID] [int] NULL,
[RoleName] [nvarchar](50) NULL,
[SubRole] [nvarchar](50) NOT NULL,
[UserID] [int] NOT NULL,
[SubRoleID] [int] NOT NULL,
[ItemStatus] [int] NULL,
[Step] [int] NULL,
[NotifyDate] [datetime] NULL,
[AssignedDate] [datetime] NULL,
[RoleAssignedDate] [datetime] NULL,
[ApprovalType] [tinyint] NULL
) DECLARE @CurrentDate DATETIME
SET
	@CurrentDate = GETDATE()
UPDATE
	LotDisposition
SET
	CurrentStep = 1,
	SubmitedDate = GETDATE()
WHERE
	IssueID = @IssueID -- make sure to delete any existing approval items before sumbitting/re-submitting an issue
DELETE FROM
	Approval
WHERE
	IssueID = @IssueID -- UPDATE THE APPROVAL LOG
INSERT INTO
	ApprovalLog (
		IssueID,
		UserID,
		OperationType,
		OperationLog,
		DocumentTypeID
	)
VALUES
	(
		@IssueID,
		@UserID,
		'Submit',
		'Submitted the document',
		@DocumentTypeID
	) -- UPDATE THE APPROVAL LOG for a system initiated transaction
INSERT INTO
	ApprovalLog (
		IssueID,
		UserID,
		OperationType,
		OperationLog,
		DocumentTypeID
	)
VALUES
	(
		@IssueID,
		0,
		'Delete',
		'Delete Existing Approval',
		@DocumentTypeID
	) DECLARE @RH_RowCount INT DECLARE @MA_RowCount INT
SET
	@RH_RowCount = (
		SELECT
			COUNT(*)
		FROM
			LotDisposition LD
			INNER JOIN Lot L ON LD.IssueID = L.IssueID
		WHERE
			(L.ProductFamily = 'RH')
			AND LD.IssueID = @IssueID
	)
SET
	@MA_RowCount = (
		SELECT
			COUNT(*)
		FROM
			LotDisposition LD
			INNER JOIN Lot L ON LD.IssueID = L.IssueID
		WHERE
			(L.ProductFamily = 'MA')
			AND LD.IssueID = @IssueID
	) IF (
		@PERequired = 1
		OR @MRBRequired = 1
	) BEGIN IF @RH_RowCount > 0 -- If RadHard part, make sure that users are ITAR compliant
	BEGIN
INSERT INTO
	#TempApprovals
	(
		IssueID,
		RoleName,
		SubRole,
		UserID,
		SubRoleID,
		ItemStatus,
		Step,
		AssignedDate,
		NotifyDate,
		RoleAssignedDate,
		ApprovalType
	) (
		SELECT
			DISTINCT R.*
		FROM
			FnGetStep1Roles_PERequired(@IssueID, @CurrentDate) R
			INNER JOIN Users U ON R.UserID = U.UserID
			INNER JOIN dbo.fnGetITARUsers() IT ON U.LoginID = IT.UserID
	) --  Add roles for the  RH-ALL
INSERT INTO
	#TempApprovals
	(
		IssueID,
		RoleName,
		SubRole,
		UserID,
		SubRoleID,
		ItemStatus,
		Step,
		AssignedDate,
		NotifyDate,
		RoleAssignedDate,
		ApprovalType
	) (
		SELECT
			DISTINCT R.*
		FROM
			fnGetStep1RolesRH_YE_PE(@IssueID, @CurrentDate) R
			INNER JOIN Users U ON R.UserID = U.UserID
			INNER JOIN dbo.fnGetITARUsers() IT ON U.LoginID = IT.UserID
	) --  Add roles for the  MA-ALL
	IF @MA_RowCount > 0 BEGIN
INSERT INTO
	#TempApprovals
	(
		IssueID,
		RoleName,
		SubRole,
		UserID,
		SubRoleID,
		ItemStatus,
		Step,
		AssignedDate,
		NotifyDate,
		RoleAssignedDate,
		ApprovalType
	) (
		SELECT
			DISTINCT R.*
		FROM
			fnGetStep1RolesMA_YE_PE(@IssueID, @CurrentDate) R
			INNER JOIN Users U ON R.UserID = U.UserID
			INNER JOIN dbo.fnGetITARUsers() IT ON U.LoginID = IT.UserID
	)
END
END
ELSE -- NON - ITAR
BEGIN
INSERT INTO
	#TempApprovals
	(
		IssueID,
		RoleName,
		SubRole,
		UserID,
		SubRoleID,
		ItemStatus,
		Step,
		AssignedDate,
		NotifyDate,
		RoleAssignedDate,
		ApprovalType
	) (
		SELECT
			DISTINCT *
		FROM
			FnGetStep1Roles_PERequired(@IssueID, @CurrentDate)
	)
END --  Add roles for the MA-ALL 
IF @MA_RowCount > 0 BEGIN
INSERT INTO
	#TempApprovals
	(
		IssueID,
		RoleName,
		SubRole,
		UserID,
		SubRoleID,
		ItemStatus,
		Step,
		AssignedDate,
		NotifyDate,
		RoleAssignedDate,
		ApprovalType
	) (
		SELECT
			DISTINCT R.*
		FROM
			fnGetStep1RolesMA_YE_PE(@IssueID, @CurrentDate) R
	)
END
END
ELSE BEGIN -- Yield Engineers Only
IF @RH_RowCount > 0 -- If RadHard part, make sure that users are ITAR compliant
BEGIN
INSERT INTO
	#TempApprovals
	(
		IssueID,
		RoleName,
		SubRole,
		UserID,
		SubRoleID,
		ItemStatus,
		Step,
		AssignedDate,
		NotifyDate,
		RoleAssignedDate,
		ApprovalType
	) (
		SELECT
			DISTINCT R.*
		FROM
			FnGetStep1Roles(@IssueID, @CurrentDate) R
			INNER JOIN Users U ON R.UserID = U.UserID
			INNER JOIN dbo.fnGetITARUsers() IT ON U.LoginID = IT.UserID
	) --  Add roles for the  RH-ALL
INSERT INTO
	#TempApprovals
	(
		IssueID,
		RoleName,
		SubRole,
		UserID,
		SubRoleID,
		ItemStatus,
		Step,
		AssignedDate,
		NotifyDate,
		RoleAssignedDate,
		ApprovalType
	) (
		SELECT
			DISTINCT R.*
		FROM
			fnGetStep1RolesRH_YE(@IssueID, @CurrentDate) R
			INNER JOIN Users U ON R.UserID = U.UserID
			INNER JOIN dbo.fnGetITARUsers() IT ON U.LoginID = IT.UserID
	) IF @MA_RowCount > 0 BEGIN
INSERT INTO
	#TempApprovals
	(
		IssueID,
		RoleName,
		SubRole,
		UserID,
		SubRoleID,
		ItemStatus,
		Step,
		AssignedDate,
		NotifyDate,
		RoleAssignedDate,
		ApprovalType
	) (
		SELECT
			DISTINCT R.*
		FROM
			fnGetStep1RolesMA_YE(@IssueID, @CurrentDate) R
			INNER JOIN Users U ON R.UserID = U.UserID
			INNER JOIN dbo.fnGetITARUsers() IT ON U.LoginID = IT.UserID
	)
END
END
ELSE BEGIN
INSERT INTO
	#TempApprovals
	(
		IssueID,
		RoleName,
		SubRole,
		UserID,
		SubRoleID,
		ItemStatus,
		Step,
		AssignedDate,
		NotifyDate,
		RoleAssignedDate,
		ApprovalType
	) (
		SELECT
			DISTINCT *
		FROM
			FnGetStep1Roles(@IssueID, @CurrentDate)
	) --  Add roles for the MA-ALL 
	IF @MA_RowCount > 0 BEGIN
INSERT INTO
	#TempApprovals
	(
		IssueID,
		RoleName,
		SubRole,
		UserID,
		SubRoleID,
		ItemStatus,
		Step,
		AssignedDate,
		NotifyDate,
		RoleAssignedDate,
		ApprovalType
	) (
		SELECT
			DISTINCT R.*
		FROM
			fnGetStep1RolesMA_YE(@IssueID, @CurrentDate) R
	)
END
END
END
SET
	@AppoverCount = (
		SELECT
			COUNT(*)
		FROM
			#TempApprovals)
			IF @AppoverCount > 0 BEGIN -- INSEERT INTO THE ACTUAL TABLE
		INSERT INTO
			Approval (
				IssueID,
				RoleName,
				SubRole,
				UserID,
				SubRoleID,
				ItemStatus,
				Step,
				AssignedDate,
				NotifyDate,
				RoleAssignedDate,
				ApprovalType,
				DocumentTypeID
			) (
				SELECT
					DISTINCT *,
					@DocumentTypeID AS DocumentTypeID
				FROM
					#TempApprovals)
			END DROP TABLE #TempApprovals
	END
GO