USE [AIS202410070030]
GO
/****** Object:  UserDefinedFunction [dbo].[GetWPLAN]    Script Date: 2025/4/22 16:19:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================REMARK
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<推送与查询条件相同时 fformidtypeid in(9,12)>
-- =============================================
ALTER FUNCTION [dbo].[GetWPLAN] 
(	
	@ERP_MO varchar(255)
	,@D varchar(255)
	,@defaultorgid int=1
)
RETURNS TABLE 
AS
RETURN 
(
	SELECT DISTINCT MY.FID MO_ID,MY.FENTRYID MO_ENTRYID,MO.FBILLNO ERP_MO,MY.FSEQ MO_SEQ,M.FNUMBER PROD_ID,MY.FQTY P_QTY,'' AS 'FACT_ID' ,'Y' AS 'IS_IMS_MO'
	,CONVERT(varchar(10),MY.FPLANSTARTDATE,23) PB_DATE,CONVERT(varchar(10),MY.FPLANFINISHDATE,23) PE_DATE,MY.FFORECASTGROUP BOM_LEVEL
	,S.FBILLNO SO_ID,SO.FSEQ SO_SEQ,Cust.FNUMBER AS CUST_ID,CASE WHEN MO.FTRUSTTEED=1 THEN 'Y' ELSE 'N' END 'IS_OUTS',D.FNUMBER DEPT_ID   
	,CASE WHEN MO.FISREWORK=1 THEN 4 ELSE CASE WHEN MO.FTRUSTTEED=1 THEN 7 ELSE 1 END END MO_TYPE,BOM.FNUMBER BOM_VER,P.FBILLNO MOBOM_ID
	,'' AS 'SUP_ID',MY.FNOTE2 REMARK,Y.FNUMBER ERP_MO_TYPE,'' AS PO_ID,0 AS PO_SEQ
	FROM T_PRD_MO MO
	INNER JOIN T_PRD_MOENTRY MY ON MY.FID=MO.FID   
	INNER JOIN T_PRD_MOENTRY_A MA ON MA.FID=MY.FID AND MA.FENTRYID=MY.FENTRYID   
	INNER JOIN T_BD_MATERIAL M ON M.FMATERIALID=MY.FMATERIALID   
	INNER JOIN T_BD_DEPARTMENT D ON D.FDEPTID=MY.FWORKSHOPID   
	INNER JOIN T_BAS_BILLTYPE Y ON Y.FBILLTYPEID=MO.FBILLTYPE AND Y.FBILLFORMID='PRD_MO'   
	INNER JOIN T_BAS_BILLTYPE_L B ON B.FBILLTYPEID=Y.FBILLTYPEID AND B.FLOCALEID=2052    
	INNER JOIN T_ENG_BOM BOM ON BOM.FID=MY.FBOMID
	LEFT JOIN T_SAL_ORDER S ON S.FID=MY.FSALEORDERID   
	LEFT JOIN T_SAL_ORDERENTRY SO ON SO.FID=MY.FSALEORDERID AND SO.FENTRYID=MY.FSALEORDERENTRYID  
	LEFT JOIN T_PRD_PPBOM P ON P.FMOID=MO.FID AND P.FMOENTRYID=MY.FENTRYID
	--LEFT JOIN T_BD_CUSTOMER C ON C.FCUSTID=S.FCUSTID
	LEFT JOIN T_BD_CUSTOMER Cust ON Cust.FCUSTID=MO.F_PKGR_Customer
	WHERE MO.FDOCUMENTSTATUS='C' AND MO.FCANCELSTATUS='A' AND MA.FSTATUS BETWEEN 3 AND 5 
		 AND MO.FBILLNO=isnull(@ERP_MO,MO.FBILLNO)
		 AND MA.FCONVEYDATE>=isnull(@D,MA.FCONVEYDATE)
		 AND MO.FPrdOrgId=@defaultorgid
	UNION
	SELECT DISTINCT MY.FID MO_ID,MY.FENTRYID MO_ENTRYID,MO.FBILLNO ERP_MO,MY.FSEQ MO_SEQ,M.FNUMBER PROD_ID,MY.FQTY P_QTY,'' AS 'FACT_ID' ,'Y' AS 'IS_IMS_MO'
	,CONVERT(varchar(10),MY.FPLANSTARTDATE,23) PB_DATE,CONVERT(varchar(10),MY.FPLANFINISHDATE,23) PE_DATE,MY.FFORECASTGROUP BOM_LEVEL
	,S.FBILLNO SO_ID,SO.FSEQ SO_SEQ,C.FNUMBER AS CUST_ID,'Y' 'IS_OUTS','' DEPT_ID,CASE WHEN MO.FBILLTYPE = '6801fa8a05c5cb' THEN 4 ELSE 7 END MO_TYPE,ISNULL(BOM.FNUMBER,'') BOM_VER,P.FBILLNO MOBOM_ID 
	,D1.FNUMBER SUP_ID,ML.FDESCRIPTION REMARK,Y.FNUMBER ERP_MO_TYPE,PO.FBillNo PO_ID,PE.FSeq PO_SEQ
	FROM T_SUB_REQORDER MO    
	INNER JOIN T_SUB_REQORDERENTRY MY ON MY.FID=MO.FID   
	LEFT JOIN T_SUB_REQORDERENTRY_L ML ON ML.FPKID=MY.FID AND ML.FENTRYID=MY.FENTRYID
	INNER JOIN T_SUB_REQORDERENTRY_A MA ON MA.FID=MY.FID AND MA.FENTRYID=MY.FENTRYID   
	INNER JOIN T_BD_MATERIAL M ON M.FMATERIALID=MY.FMATERIALID   
	INNER JOIN T_BAS_BILLTYPE Y ON Y.FBILLTYPEID=MO.FBILLTYPE AND Y.FBILLFORMID='SUB_SUBREQORDER'  
	INNER JOIN T_BAS_BILLTYPE_L B ON B.FBILLTYPEID=Y.FBILLTYPEID AND B.FLOCALEID=2052    
	LEFT JOIN T_ENG_BOM BOM ON BOM.FID=MY.FBOMID 
	INNER JOIN T_PUR_POORDERENTRY PE ON PE.FID=MA.FPurOrderId and PE.FENTRYID=MA.FPurOrderEntryId   
	INNER JOIN T_PUR_POORDER PO ON PO.FID=PE.FID
	inner join T_BD_SUPPLIER D1 ON D1.FSupplierId=MY.FSupplierId
	LEFT JOIN T_SAL_ORDER S ON S.FID=MA.FSALEORDERID   
	LEFT JOIN T_SAL_ORDERENTRY SO ON SO.FID=MA.FSALEORDERID AND SO.FENTRYID=MA.FSALEORDERENTRYID  
	LEFT JOIN T_PRD_PPBOM P ON P.FMOID=MO.FID AND P.FMOENTRYID=MY.FENTRYID
	LEFT JOIN T_BD_CUSTOMER C ON C.FCUSTID=S.FCUSTID
	WHERE PO.FDocumentStatus='C' AND PO.FCancelStatus='A'  AND PO.FCloseStatus='A' 
		 AND MO.FBILLNO=isnull(@ERP_MO,MO.FBILLNO)
		 AND PO.FAPPROVEDATE>=isnull(@D,PO.FAPPROVEDATE)
		 AND MO.FSubOrgId=@defaultorgid
)

