﻿--DECLARE @date date,@startDate datetime ,@endDate datetime
--set @date = '2023-07-15'
--select 
--	@startDate = dateadd(month, datediff(month, 0, @date), 0)
--	,@endDate = dateadd(ms,-3, dateadd(month, datediff(month, 0, dateadd(month, 1, @date)), 0))
--select
--@startDate,@endDate

declare @LCID int 
set @LCID = 2052
SELECT
	A.FMATERIALID
	,a.FBILLNO
	,ee.FSRCBILLNO
	--,B_L.FNAME AS '产品名称'
	,(CASE
			FEXPUNIT 
			WHEN 'Y' THEN
			CONVERT ( VARCHAR, BA.FEXPPERIOD ) + '年' 
			WHEN 'M' THEN
			CONVERT ( VARCHAR, ba.FEXPPERIOD) + '月' 
			WHEN 'D' THEN
			CONVERT ( VARCHAR, ba.FEXPPERIOD ) + '日' ELSE '' 
		END) as '有效期'

	,'' as '有效期'
	,'' as '标准批量'
	,'' as '每批产量（件）'
	,'' as '包装规格'
	,'' as '包装规格'
	,a.FUNITID
	,'' as '单位'
	--,C_L.FNAME AS '单位'
	,'' as '安全库存量（件）'
	,BA.FSAFESTOCK AS '安全库存数量'
	--,DE.M01 AS '本月预计需求量'
	--,DE.S01 AS '前三月发货量'
	--,DE.S02 AS '前二月发货量'
	--,DE.S03 AS '前一月发货量'
	,EE_A.FSTOCKINQUAAUXQTY as '已入库量'
	,'' as '在生产量'
	,(EE_A.FSTOCKINQUAAUXQTY + 0) as '合计库存量'
	,'' as '计划生产批数'
	,DE.FQTY AS '计划生产量'
	,DE.FREMARK AS '备注'	
FROM
	T_PLN_PLANORDER A --计划订单
	LEFT JOIN T_BD_MATERIAL B ON A.FMATERIALID = B.FMATERIALID --物料
--	LEFT JOIN T_BD_MATERIAL_L B_L ON B_L.FMATERIALID = B.FMATERIALID
	LEFT JOIN T_BD_MATERIALSTOCK BA ON BA.FMATERIALID = B.FMATERIALID
--	LEFT JOIN T_BD_MATERIALBASE BB ON BB.FMATERIALID = B.FMATERIALID
	--LEFT JOIN T_BD_UNIT_L C_L ON C_L.FUNITID = a.FUNITID
	LEFT JOIN T_PLN_FORECASTENTRY DE ON DE.FMATERIALID = B.FMATERIALID --预测单
	left join T_PLN_FORECAST d on de.FID = d.FID
	LEFT JOIN T_PRD_MOENTRY EE ON  EE.FMATERIALID = B.FMATERIALID --生产订单
	LEFT JOIN T_PRD_MOENTRY_A EE_A ON  EE_A.FENTRYID = EE.FENTRYID AND EE.FID =EE_A.FID --生产订单
WHERE
	A.FRELEASETYPE = 1 
	--AND B_L.FLOCALEID = @LCID 
	--AND C_L.FLOCALEID = @LCID