﻿	--IF EXISTS (SELECT * FROM sys.all_objects WHERE object_id = OBJECT_ID(N'[dbo].TMP123123123123123') AND type IN ('U'))
	--	DROP TABLE [dbo].TMP123123123123123
	--GO
	
	declare @tableName varchar(80)
	,@seqFid varchar(80)
	,@proMaterialId int
	,@materialId int
	,@FBILLNO varchar(80)
	,@FPPBOMBILLNO varchar(80)
	,@FSALEORDERNO varchar(80)
	,@LCID int

	SET @tableName = 'TMP123123123123123'
	SET @seqFid = 'ROW_NUMBER() OVER(ORDER BY t0.FID) AS FIDENTITYID'
	SET @proMaterialId = ''
	SET @materialId = ''
	SET @FBILLNO = ''
	SET @FPPBOMBILLNO = ''
	SET @FSALEORDERNO = ''
	SET @LCID = 2052
/***********************生产订单关联信息start****************************/
	SELECT 
		--ROW_NUMBER() OVER(ORDER BY t0.FID) AS FIDENTITYID--1.行号
		t0.FID
		,t0e.FSEQ
		,t0e.FENTRYID
		,t0.FDATE --2.单据日期
		,t0e.FPLANSTARTDATE--38.计划开工日期
		,t0e.FPLANFINISHDATE--39.计划完工日期
		,t0e.FSALEORDERNO --4.销售订单号
		,t0.FBILLNO--7.生产任务单号
		,t0e.FQTY--12.产品生产数量
		--,t0e.FSTOCKID--35.仓库
		--,t0e.FSTOCKLOCID--36.仓位
		,t0e.FYIELDRATE 
		,t0e.FROUTINGID
		,(CASE t0e.FPRODUCTTYPE	WHEN 1 THEN '主产品' WHEN 2 THEN '主产品' WHEN 3 THEN '主产品' ELSE '' END) AS 'PRODUCTTYPE'--43.生产类型
		,t0e.FMATERIALID AS 'ProMaterialId'
		,t0e.FUNITID AS 'PrdUnitId'
		--,(CASE t1.FMATERIALSRC WHEN 'A' THEN 'PLM' WHEN 'B' THEN 'ERP' ELSE '' END) AS 'ProMaterialsrc' --15.物料来源
		INTO #TMPMO_BASE_INFO
	FROM
		T_PRD_MO t0
		INNER JOIN T_PRD_MOENTRY t0e on t0e.FID = t0.FID
	WHERE 
		t0.FBILLNO = (CASE WHEN @FBILLNO != '' THEN @FBILLNO ELSE t0.FBILLNO END)
		AND t0e.FSALEORDERNO = (CASE WHEN @FSALEORDERNO != '' THEN @FSALEORDERNO ELSE t0e.FSALEORDERNO END)
		AND t0e.FMATERIALID = (CASE WHEN @proMaterialId <> 0 THEN @proMaterialId ELSE t0e.FMATERIALID END)

	CREATE INDEX #TMPMO_BASE_INFO_INDEX_FENTRYID ON #TMPMO_BASE_INFO(FENTRYID);

	SELECT tt.*
		,(CASE t0e_a.FSTATUS WHEN 6 THEN '已结案'ELSE '' END) AS 'StatusIs6'--45.生产任务单结案否
		,t1.FMNEMONICCODE AS 'ProMnemoniccode'--6.助记码
		,t1.FNUMBER AS 'ProNumber'--8.产品编码
		,t1_l.FNAME AS 'ProName'--9.产品名称
		,t1_l.FSPECIFICATION AS 'ProSpecification'--10.产品规格型号
		,t2_l.FNAME AS 'ProUnitName'--11.产品单位
		INTO #TmpTable0
	FROM #TMPMO_BASE_INFO tt
		INNER JOIN T_PRD_MOENTRY_A t0e_a on t0e_a.FENTRYID = tt.FENTRYID
		LEFT JOIN T_BD_MATERIAL t1 ON t1.FMATERIALID = tt.ProMaterialId
		LEFT JOIN T_BD_MATERIAL_L t1_l ON t1_l.FMATERIALID = tt.ProMaterialId AND t1_l.FLOCALEID = @LCID
		LEFT JOIN T_BD_UNIT_L t2_l ON t2_l.FUNITID = tt.PrdUnitId AND t2_l.FLOCALEID = @LCID

	DROP TABLE #TMPMO_BASE_INFO

	CREATE INDEX #TmpTable0_FENTRYID_INDEX ON #TmpTable0(FENTRYID);
/***********************生产订单关联信息end****************************/

/***********************生产用料清单关联信息start****************************/
	SELECT 
		tt.*
		,t0.FBILLNO AS 'FPPBOMBILLNO'--3.生产投料单号
		,t0e.FMATERIALID
		,t0e.FENTRYID AS 'PPBOMENTRYID'
		,(t0e.FNUMERATOR / t0e.FDENOMINATOR) AS 'FUnitNeedQty' --18.物料单位用量 
		,t0e.FNEEDQTY --22.物料总需求
		,t0e.FUNITID
		,t0e.FBOMENTRYID
		,t0e.FSTDQTY --标准用量 --29.标准数量
		,t0e.FMUSTQTY --应发数量 --30.应发数量
		,t0.FWORKSHOPID
		,(CASE t0.FDOCUMENTSTATUS WHEN 'A' THEN '创建' WHEN 'B' THEN '审核中' WHEN 'C' THEN '已审核' WHEN 'D' THEN '重新审核' WHEN 'Z' THEN '暂存' ELSE '' END) AS 'PPBOMStatus'--46.生产投料单状态
		INTO #TMPPPBOM_BASE_INFO
	FROM
		T_PRD_PPBOM t0  --生产用料清单
		INNER JOIN T_PRD_PPBOMENTRY t0e ON t0.FID = t0e.FID 
		INNER JOIN #TmpTable0 tt on t0.FMOENTRYID = tt.FENTRYID
	WHERE 
		T0.FBILLNO = (CASE WHEN @FPPBOMBILLNO != '' THEN @FPPBOMBILLNO ELSE T0.FBILLNO END)
		AND t0e.FMATERIALID = (CASE WHEN @materialId <> 0 THEN @materialId ELSE t0e.FMATERIALID END)

	CREATE INDEX #TMPPPBOM_BASE_INFO_PPBOMENTRYID_INDEX ON #TMPPPBOM_BASE_INFO(PPBOMENTRYID);

	SELECT tt.*
		,t0e_q.FBASEWIPQTY --23.当前在制品数量
		,t0e_q.FPICKEDQTY --27.已领数量
		,t0e_q.FNOPICKEDQTY --28.未领数量
		,t0e_l.FMEMO --31.备注
		,t0e_q.FPICKEDQTY * (100 - tt.FYIELDRATE) AS 'FLossQty'--32.损耗数量
		,t0e_q.FSCRAPQTY --33.报废数量
		,t0e_q.FREPICKEDQTY --37.补料数量
		,t0e_c.FSTOCKID
		,t0e_c.FSTOCKLOCID
		INTO #TmpTable1
	FROM #TMPPPBOM_BASE_INFO tt
		LEFT JOIN T_PRD_PPBOMENTRY_Q t0e_q ON t0e_q.FENTRYID = tt.PPBOMENTRYID --生产用料清单数量拆分表
		LEFT JOIN T_PRD_PPBOMENTRY_C t0e_c ON t0e_c.FENTRYID = tt.PPBOMENTRYID
		LEFT JOIN T_PRD_PPBOMENTRY_L t0e_l on t0e_l.FENTRYID = tt.PPBOMENTRYID AND t0e_l.FLOCALEID = @LCID

	DROP TABLE #TMPPPBOM_BASE_INFO

	SELECT 
		tt.*
		,t1.FNUMBER AS 'MaterialNumber'--8.物料编码
		,t1_l.FNAME AS 'MaterialName'--9.物料名称
		,t1_l.FSPECIFICATION AS 'MaterialSpecification'--10.物料规格型号
		,t2_l.FNAME AS 'MaterialUnitName'--11.物料单位
		,(CASE t1.FMATERIALSRC WHEN 'A' THEN 'PLM' WHEN 'B' THEN 'ERP' ELSE '' END) AS 'Materialsrc' --15.物料来源
		,t4_l.FNAME AS 'WORKSHOPNAME' --44.生产车间
		,t1.FMASTERID
		,t1.FUSEORGID
		,t5_l.FMEMO AS 'BOMMEMO' --42.BOM备注项
		,t3_l.FNAME AS 'FSTOCKNAME'
		into #TmpTable2
	FROM
		#TmpTable1 tt
		LEFT JOIN T_BD_MATERIAL t1 ON t1.FMATERIALID = tt.FMATERIALID
		LEFT JOIN T_BD_MATERIAL_L t1_l ON t1_l.FMATERIALID = tt.FMATERIALID AND t1_l.FLOCALEID = @LCID
		LEFT JOIN T_BD_UNIT_L t2_l ON t2_l.FUNITID = tt.FUNITID AND t2_l.FLOCALEID = @LCID
		LEFT JOIN T_BD_STOCK_L t3_l ON t3_l.FSTOCKID = tt.FSTOCKID AND t3_l.FLOCALEID =@LCID
		LEFT JOIN T_BD_DEPARTMENT_L t4_l on t4_l.FDEPTID = tt.FWORKSHOPID AND t4_l.FLOCALEID = @LCID
		LEFT JOIN T_ENG_BOMCHILD_L t5_l ON t5_l.FENTRYID = tt.FBOMENTRYID AND t5_L.FLOCALEID = @LCID

/***********************生产用料清单关联信息end****************************/
	SELECT
		t0.*
		,t1.FBASEQTY 
		,t2_l.FNAME AS 'ROUTENAME' 
		,t3e_A.FBASEINVQTY
		,t3e.FBASELACKQTY AS 'FBASELACKQTY'--24.欠料
		,t3e_A.FBASEONORDERQTY AS 'FBASEONORDERQTY' --25.物料在途数
		,6 AS 'LOWEST6'
		--INTO #TmpTable3
	FROM
		#TmpTable2 t0
		LEFT JOIN (
			SELECT 
				t0.FMATERIALID
				,t0.FSTOCKORGID
				,sum(t0.FBASEQTY) AS FBASEQTY
			FROM
				T_STK_INVENTORY t0
			GROUP BY
				t0.FMATERIALID
				,t0.FSTOCKORGID
		) t1 on t1.FMATERIALID = t0.FMASTERID AND t1.FSTOCKORGID = t0.FUSEORGID
		LEFT JOIN T_ENG_ROUTE_L t2_L on t2_L.FID = t0.FROUTINGID AND t2_L.FLOCALEID = @LCID
		LEFT JOIN T_PRD_PMPPBOMENTRY t3e on t3e.FMOID = t0.FID AND t3e.FMOENTRYID = t0.FENTRYID AND t3e.FMOENTRYSEQ = t0.FSEQ AND t3e.FMATERIALIDSUB = t0.FMATERIALID --AND t3e.FMATERIALID = t0.FMATERIALID
		LEFT JOIN T_PRD_PMPPBOMENTRY_A t3e_A ON t3e_A.FENTRYID = t3e.FENTRYID 


	--exec(N'
	--SELECT 
	--	t0.*
	--	,' + @seqFid+  '
	--	INTO ' + @tableName + '
	--FROM
	--	#TmpTable3 t0
	--')

	drop table #TmpTable0
	drop table #TmpTable1
	drop table #TmpTable2
	--drop table #TmpTable3
	
	--exec(N'SELECT * FROM '+ @tableName)