/****** Object:  StoredProcedure [dbo].[GetPPBOMEntry]    Script Date: 2024/11/6 17:05:09 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[GetPPBOMEntry]
	@billlist varchar(max),
	@wh varchar(max)
AS
BEGIN
	SET NOCOUNT ON;
	CREATE TABLE #temp(FParentMaterial VARCHAR(255),
	FMainMaterial VARCHAR(255),
	FMaterial VARCHAR(255),
	FWorkshopID VARCHAR(255),
	FBOMID VARCHAR(255),
	FQty DECIMAL(38,10),
	FMOBillNO VARCHAR(255),
	FMOEntrySeq INT,
	FMOEntryID INT,
	FBaseQty DECIMAL(38,10),
	FMoId INT,
	FBaseUnitID VARCHAR(255),
	FMOType VARCHAR(255),
	FUnitID VARCHAR(255),
	FPrdOrgId VARCHAR(255),
	FNumerator DECIMAL(38,10),
	FDenominator DECIMAL(38,10),
	FParentOwnerTypeId VARCHAR(255),
	FParentOwnerId VARCHAR(255),
	FPPBOMNo VARCHAR(255),
	FPPBOMEntrySeq INT,
	FPPBOMId INT,
	FPPBOMEntryId INT,
	FReplaceGroup INT,
	FReplacePriority INT,
	ChildQty DECIMAL(38,10),
	BaseChildQty DECIMAL(38,10),
	FMaterialType int,
	FParentRowId VARCHAR(255),
	FRowId VARCHAR(255)
	)

	DECLARE @SQL varchar(max)='INSERT INTO #temp
SELECT * FROM (
SELECT M1.FNUMBER FParentMaterial,CASE PBOM.FMATERIALTYPE WHEN 3 THEN (SELECT M.FNUMBER FROM T_PRD_PPBOMENTRY_C T1 INNER JOIN T_PRD_PPBOMENTRY T2 ON T2.FID=T1.FID AND T2.FENTRYID=T1.FENTRYID
		INNER JOIN T_BD_MATERIAL M ON M.FMATERIALID=T2.FMATERIALID WHERE T1.FID=P.FID AND T1.FROWID=P3.FPARENTROWID) ELSE M2.FNUMBER END FMainMaterial
	,M2.FNUMBER FMaterial,D1.FNUMBER FWorkshopID,B1.FNUMBER FBOMID,P.FQTY FQty,P.FMOBillNO,P.FMOEntrySeq,P.FMOEntryID
	,P.FBaseQty,P.FMODIFIERID FMoId,U1.FNUMBER FBaseUnitID,B2.FNUMBER FMOType,U2.FNUMBER FUnitID,Org.FNUMBER FPrdOrgId,PBOM.FNumerator,PBOM.FDenominator
	,P.FParentOwnerTypeId FParentOwnerTypeId,Org.FNUMBER FParentOwnerId,P.FBILLNO FPPBOMNo,PBOM.FSEQ FPPBOMEntrySeq,PBOM.FID FPPBOMId,PBOM.FENTRYID FPPBOMEntryId,FREPLACEGROUP FReplaceGroup
	,ROW_NUMBER() OVER (PARTITION BY PBOM.FREPLACEGROUP ORDER BY PBOM.FSEQ) - 1 FReplacePriority
	,PBOM.FMustQty ChildQty,PBOM.FBaseMustQty BaseChildQty,PBOM.FMATERIALTYPE,P3.FParentRowId,p3.FRowId
FROM T_PRD_PPBOM P
	INNER JOIN T_PRD_PPBOMENTRY PBOM ON PBOM.FID=P.FID
	INNER JOIN T_PRD_PPBOMENTRY_C P3 ON P3.FID=PBOM.FID AND P3.FENTRYID=PBOM.FENTRYID
	INNER JOIN T_BD_DEPARTMENT D1 ON D1.FDEPTID=P.FWORKSHOPID
	INNER JOIN T_BD_MATERIAL M1 ON M1.FMATERIALID=P.FMATERIALID
	INNER JOIN T_ENG_BOM B1 ON B1.FID=P.FBOMID
	INNER JOIN T_BAS_BILLTYPE B2 ON B2.FBILLTYPEID=P.FMOTYPE
	INNER JOIN T_BD_UNIT U1 ON U1.FUNITID=P.FBASEUNITID
	INNER JOIN T_BD_MATERIALPRODUCE M4 ON M4.FMATERIALID=M1.FMATERIALID
	INNER JOIN T_BD_UNIT U2 ON U2.FUNITID=P.FUNITID
	INNER JOIN T_ORG_Organizations Org ON Org.FORGID=P.FPrdOrgId
	INNER JOIN T_BD_MATERIAL M2 ON M2.FMATERIALID=PBOM.FMATERIALID
WHERE 1=1 AND (' + @billlist + ') 
) A WHERE ' + @wh 
EXEC (@SQL)


SELECT * FROM #temp WHERE FPPBOMEntrySeq IN (SELECT MIN(FPPBOMEntrySeq) FROM #temp GROUP BY FPPBOMNo,FMainMaterial,FMaterial)
ORDER BY FPPBOMNo,FPPBOMEntrySeq
DROP TABLE #temp

END
