﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[_8DUpdateAuditScore]    Script Date: 11/21/2024 11:29:04 AM ******/
SET
	ANSI_NULLS ON
GO
SET
	QUOTED_IDENTIFIER ON
GO
	-- =============================================
	-- Author:		<Author,,Name>
	-- Create date: <Create Date,,>
	-- Description:	<Description,,>
	-- =============================================
	CREATE PROCEDURE [dbo].[_8DUpdateAuditScore] -- Add the parameters for the stored procedure here
	@AuditNo INT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
SET
	NOCOUNT ON;

DECLARE @NoOfMajorNonConformities INT DECLARE @NoOfMinorNonConformities INT DECLARE @AuditScore INT
SET
	@NoOfMajorNonConformities = (
		SELECT
			COUNT(*)
		FROM
			_8DAuditFinding
		WHERE
			FindingType = 'Major'
			AND AuditNo = @AuditNo
	)
SET
	@NoOfMinorNonConformities = (
		SELECT
			COUNT(*)
		FROM
			_8DAuditFinding
		WHERE
			FindingType = 'Minor'
			AND AuditNo = @AuditNo
	) -- IF Audit Type is either VDA or External the do not auto calculate the Audit Score
	-- 11/26/2018 TR - fix hard coded audit no
	IF (
		SELECT
			COUNT(*)
		FROM
			_8DAuditTypeByAudit
		WHERE
			(
				AuditTypeID = 4
				OR AuditTypeID = 8
			)
			AND AuditNo = @AuditNo
	) = 0 BEGIN
SET
	@AuditScore = 100 - (
		(@NoOfMajorNonConformities * 10) + (@NoOfMinorNonConformities)
	)
UPDATE
	_8DAudit
SET
	AuditScore = @AuditScore
WHERE
	AuditNo = @AuditNo
END
END
GO