﻿IF EXISTS(SELECT * FROM SYS.PROCEDURES WHERE NAME='PROC_INQUIRPLMTMPRTP1111111111')
	DROP  PROCEDURE  dbo.PROC_INQUIRPLMTMPRTP1111111111;
GO
	
CREATE PROCEDURE PROC_INQUIRPLMTMPRTP1111111111
	@FRECORDDATE DATETIME
	,@FRECORDYEAR INT
	,@FRECORDMONTH INT
AS
BEGIN
	SELECT
		AE.FSRCBILLNO
		,AE.FMATERIALID
		,AE.FSALEORDERENTRYID
		,AE.FSALEORDERENTRYSEQ
		,SUM(AE.FQTY) AS FQTY 
		,SUM(B.FFINISHQTY) AS FFINISHQTY --已入库数量
	FROM
		T_PRD_MO A
		LEFT JOIN T_PRD_MOENTRY AE ON A.FID = AE.FID
		LEFT JOIN T_PRD_MOENTRY_A AE_A ON AE_A.FENTRYID = AE.FENTRYID
		LEFT JOIN (SELECT 
				A.FBILLTYPE --50fa7c2eda7947b89fab5431bf25d48e 入库汇报 e52b554423704173b7306554708a8b05 生产汇报
				,AE.FMOENTRYID
				,FFINISHQTY
			FROM
				T_PRD_MORPT A
				LEFT JOIN T_PRD_MORPTENTRY AE ON AE.FID = A.FID
			WHERE A.FDOCUMENTSTATUS='C'
		) B on B.FMOENTRYID = AE.FENTRYID 
		RIGHT JOIN (
			SELECT 
				A.FMATERIALID
				,A.FNUMBER
				,B.FSTOCKORGID
				,B.FLOT
				,SUM(B.FBASEQTY) AS FBASEQTY
				,@FRECORDDATE AS FRECORDDATE
				,@FRECORDYEAR as FRECORDYEAR
				,@FRECORDMONTH as FRECORDMONTH
				,AP.FLOTYIELD -- '每批产量'
				,AP.FPACKUNITCONVERRATIO -- '包装规格'
				,AP.FPACKUNITID -- '单位'
				--INTO #TMPRECORD
			FROM 
				T_BD_MATERIAL A 
				INNER JOIN T_BD_MATERIALPRODUCE AP ON AP.FMATERIALID = A.FMATERIALID
				INNER JOIN T_STK_INVENTORY B ON A.FMASTERID = B.FMATERIALID
			WHERE
				B.FSTOCKORGID = 100038 And A.FDOCUMENTSTATUS = 'C'
			GROUP BY
				A.FMATERIALID
				,A.FNUMBER
				,B.FSTOCKORGID
				,B.FLOT
				,AP.FLOTYIELD -- '每批产量'
				,AP.FPACKUNITCONVERRATIO -- '包装规格'
				,AP.FPACKUNITID -- '单位'
		) C on C.FMATERIALID = AE.FMATERIALID
	WHERE 
		AE.FSRCBILLTYPE ='PLN_PLANORDER' AND AE_A.FSTATUS <> 6 and AE.FSALEORDERENTRYID <> 0
	GROUP BY
		AE.FSRCBILLNO
		,AE.FMATERIALID
		,AE.FSALEORDERENTRYID
		,AE.FSALEORDERENTRYSEQ


	--ALTER TABLE #TMPRECORD ADD FRECORDDATE DATETIME
	--UPDATE #TMPRECORD SET FRECORDDATE = @FRECORDDATE 

	--ALTER TABLE #TMPRECORD ADD FRECORDYEAR int DEFAULT 0
	--UPDATE #TMPRECORD SET FRECORDYEAR = @FRECORDYEAR

	--ALTER TABLE #TMPRECORD ADD FRECORDMONTH int DEFAULT 0
	--UPDATE #TMPRECORD SET FRECORDMONTH = @FRECORDMONTH

	SELECT * FROM #TMPRECORD
	DROP TABLE #TMPRECORD
END