CREATE PROC PROC_MATERIAL_STOCK_STATU(
	@sDate varchar(80)
	,@eDate varchar(80)
)
AS 
BEGIN
SELECT t0.FMATERIALID
	,ISNULL(t3.sumQTY,0) AS 即时库存
	,ISNULL(t4.sumBASENOPICKEDQTY,0) AS 未发数
	,ISNULL(t5.sumNoStockInQty,0) AS 在制数
	,CASE WHEN CHARINDEX('TC',t0.FNUMBER) = 1 THEN 0 ELSE ISNULL(t1.数量 - t1.未发数量,0) END AS '受订量'
	,ISNULL(t2.数量 - t2.入库数量,0) AS '在途量'
	,(ISNULL(t3.sumQTY,0) - ISNULL(t4.sumBASENOPICKEDQTY,0) + ISNULL(t5.sumNoStockInQty,0) - ISNULL(t1.数量 - t1.未发数量,0) + ISNULL(t2.数量 - t2.入库数量,0)) AS '可用库存'
	,ISNULL(t6.进检量,0) AS '进检量'
	--INTO #Material_Stock_Statu
FROM T_BD_MATERIAL t0
	LEFT JOIN (
		SELECT 
			t0e.FMATERIALID,SUM(t0e.FQTY) AS '数量'
			,SUM((t0e_r.FBASEREMAINOUTQTY + t0e_r.FBASERETURNQTY)) AS '未发数量'
		FROM T_SAL_ORDER t0
			INNER JOIN T_SAL_ORDERENTRY t0e on t0.FID = t0e.FID
			INNER JOIN T_SAL_ORDERENTRY_R t0e_r on t0e.FID = t0e_r.FID AND t0e_r.FENTRYID = t0e.FENTRYID
		WHERE t0.FDOCUMENTSTATUS = 'C'
			AND t0.FCANCELSTATUS = 'A'
			AND t0.FCLOSESTATUS =  'A'
			AND t0.FDATE between @sDate AND @eDate
		GROUP BY t0e.FMATERIALID
	) t1 on t1.FMATERIALID = t0.FMATERIALID 
	LEFT JOIN (
		SELECT 
			t0e.FMATERIALID,SUM(t0e.FQTY) AS '数量'
			,SUM(t0e_r.FREMAINSTOCKINQTY) AS '剩余入库数量'
			,SUM(t0e_r.FBASESTOCKINQTY) AS '入库数量'
		FROM T_PUR_POORDER t0
			INNER JOIN T_PUR_POORDERENTRY t0e on t0.FID = t0e.FID AND t0.FBUSINESSTYPE != 'ZCCG' AND t0.FBUSINESSTYPE != 'FYCG'
			INNER JOIN T_PUR_POORDERENTRY_R t0e_r on t0e.FID = t0e_r.FID AND t0e_r.FENTRYID = t0e.FENTRYID
		WHERE t0.FDOCUMENTSTATUS = 'C'
			AND t0.FCANCELSTATUS = 'A'
			AND t0.FCLOSESTATUS =  'A' 
			AND t0.FDATE BETWEEN @sDate AND @eDate
		GROUP BY t0e.FMATERIALID
	) t2 on t2.FMATERIALID = t0.FMATERIALID
	LEFT JOIN (
		SELECT tt0.FMATERIALID,tt0.FSTOCKORGID,SUM(tt0.FBASEQTY) AS sumQTY
		FROM T_STK_INVENTORY tt0
		GROUP BY tt0.FMATERIALID,tt0.FSTOCKORGID
	) t3 on t3.FMATERIALID = t0.FMASTERID AND t3.FSTOCKORGID = t0.FUSEORGID
	LEFT JOIN (
		SELECT t0e.FMATERIALID,SUM(t0e_q.FNOPICKEDQTY) sumBASENOPICKEDQTY --未发量
		FROM T_PRD_PPBOMENTRY t0e
			INNER JOIN T_PRD_PPBOMENTRY_Q t0e_q on t0e.FENTRYID = t0e_q.FENTRYID
			INNER JOIN T_PRD_MOENTRY_A t0e_a on t0e_a.FENTRYID = t0e.FMOENTRYID
		WHERE t0e_a.FSTATUS = 4
		GROUP BY t0e.FMATERIALID
	) t4 on t4.FMATERIALID = t0.FMATERIALID 
	LEFT JOIN (
		SELECT t0e.FMATERIALID,SUM(t0e_q.FNOSTOCKINQTY) AS sumNoStockInQty
		FROM T_PRD_MOENTRY t0e
			INNER JOIN T_PRD_MOENTRY_Q t0e_q on t0e.FENTRYID = t0e_q.FENTRYID
			INNER JOIN T_PRD_MOENTRY_A t0e_a on t0e_a.FENTRYID = t0e.FENTRYID
		WHERE t0e_a.FSTATUS = 4
		GROUP BY t0e.FMATERIALID
	) t5 on t5.FMATERIALID = t0.FMATERIALID
	LEFT JOIN (
		SELECT t0e.FMATERIALID,SUM(FBASEUNITQTY-FINSTOCKBASEQTY + FRETURNBASEQTY) AS '进检量'
		FROM T_PUR_RECEIVE t0
			INNER JOIN T_PUR_RECEIVEENTRY t0e on t0.FID = t0e.FID
			INNER JOIN T_PUR_RECEIVEENTRY_R t0e_r on t0e.FID = t0e_r.FID AND t0e.FENTRYID = t0e_r.FENTRYID
			INNER JOIN T_PUR_RECEIVEENTRY_S t0e_s on t0e.FID = t0e_s.FID AND t0e.FENTRYID = t0e_s.FENTRYID
		WHERE t0.FDOCUMENTSTATUS = 'C'
			AND t0.FCANCELSTATUS = 'A'
			AND t0.FCLOSESTATUS =  'A' 
			AND t0.FDATE BETWEEN @sDate AND @eDate
		GROUP BY t0e.FMATERIALID
	) t6 on t6.FMATERIALID = t0.FMATERIALID
WHERE t0.FDOCUMENTSTATUS = 'C'
	AND t0.FFORBIDSTATUS = 'A'

	--RETURN #Material_Stock_Statu
END