﻿SELECT
	'' AS '行号',
	'' AS '单据日期',
	D.FBILLNO AS '生产投料单号',
	'' AS '销售订单号',
	'' AS '购货单位代码',
	C.FMNEMONICCODE AS '助记码',
	A.FBILLNO AS '生产任务单号',
	C.FNUMBER AS '产品代码',
	C_L.FNAME AS '产品名称',
	C_L.FSPECIFICATION AS '产品规格型号',
	E_L.FNAME AS '产品单位',
	AE.FQTY AS '产品生产数量',
	I.FNUMBER AS '物料代码',
	I.FNAME AS '物料名称',
	( CASE I.FMATERIALSRC WHEN 'A' THEN 'PLM' WHEN 'B' THEN 'ERP' ELSE '' END ) AS '物料来源',
	I.FSPECIFICATION AS '物料规格型号',
	'' AS '物料技术标准',
	I.FSTDQTY AS '物料单位用量',
	I.FUNITID AS '物料单位',
	I.FINVENTORYQTY AS '物料库存量',
	'' AS '物料待检数',
	I.FNEEDQTY AS '物料总需求',
	'' AS '当前在制品数量',
	'' AS '欠料',
	'' AS '物料在途数',
	'' AS '计划投料数量',
	I.FPICKEDQTY AS '已领数量',
	I.FNOPICKEDQTY AS '未领数量',
	'' AS '标准数量',
	I.FMUSTQTY AS '应发数量',
	'' AS '备注',
	'' AS '损耗数量',
	I.FSCRAPQTY AS '报废数量',
	'' AS '计划发料日期',
	'' AS '仓库',
	'' AS '仓位',
	I.FREPICKEDQTY AS '补料数量',
	AE.FPLANSTARTDATE AS '计划开工日期',
	AE.FPLANFINISHDATE AS '计划完工日期',
	'' AS '工位',
	'' AS '其他备注项',
	'' AS 'BOM备注项',
	A.FBILLTYPE AS '生产类型',
	AE.FWORKSHOPID AS '生产车间',
	( CASE AE_A.FSTATUS WHEN 6 THEN '已结案' ELSE '' END ) AS '生产任务单结案否',
	D.FDOCUMENTSTATUS AS '生产投料单状态',
	'' AS 'PR已审未转PO数量' 
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
	INNER JOIN T_BD_MATERIAL C ON AE.FMATERIALID = C.FMATERIALID --物料表
	INNER JOIN T_BD_MATERIAL_L C_L ON C.FMATERIALID = C_L.FMATERIALID
	LEFT JOIN T_PRD_PPBOM D ON D.FMOENTRYID = AE_A.FENTRYID --生产用料清单
	LEFT JOIN T_BD_UNIT_L E_L ON AE.FUNITID = E_L.FUNITID
	LEFT JOIN (
	SELECT
		A.FMATERIALID,
		A.FMASTERID,
		A.FNUMBER,
		B.FUNITID,
		AL.FNAME ,
		AL.FSPECIFICATION ,
		A.FMATERIALSRC,
		B.FENTRYID,
		B.FID,
		B.FMOID,
		B.FMOENTRYID,
		B.FMOTYPE,
		B.FMOBILLNO,
		B.FNEEDQTY,
		B.FMUSTQTY,
		B_Q.FPICKEDQTY,
		B_Q.FNOPICKEDQTY,
		B.FSTDQTY,
		B_Q.FINVENTORYQTY,
		B_Q.FSCRAPQTY,
		B_Q.FREPICKEDQTY
	FROM
		T_BD_MATERIAL A
		LEFT JOIN T_BD_MATERIAL_L AL ON A.FMATERIALID = AL.FMATERIALID
		RIGHT JOIN T_PRD_PPBOMENTRY B ON B.FMATERIALID = A.FMATERIALID 
		RIGHT JOIN T_PRD_PPBOMENTRY_Q B_Q ON B.FENTRYID = B_Q.FENTRYID
	WHERE
		AL.FLOCALEID = 2052 
	) I ON D.FID = I.FID 
WHERE
	C_L.FLOCALEID = 2052 
	AND E_L.FLOCALEID = 2052