VIEW "CT_PF_GetUnsynchronizedMOR_WOR" AS 

with 
MorWorHr as (
	select
		mor."DocEntry"													as "MorHrDocEntry", 
		mor."U_Status"													as "MorHrU_Status",
		mor."U_ItemCode"												as "MorHrU_ItemCode", 
		mor."U_Project"													as "MorHrU_Project", 
		round((mor."U_Quantity" - mor."U_ActualQty"),adm."QtyDec")		as "MorHrPlannedQtyCALC",
		mor."U_Warehouse"												as "MorHrU_Warehouse",
		mor."U_RequiredDate"											as "MorHrU_RequiredDate",
		mor."U_PlannedStartDate"										as "MorHrU_PlannedStartDate",
		mor."U_Remarks"													as "MorHrU_Remarks",
		wor."DueDate"													as "WorHrDueDate",
		wor."StartDate"													as "WorHrStartDate",
		wor."ItemCode"													as "WorHrItemCode",
		wor."Project"													as "WorHrProject",
		wor."PlannedQty"												as "WorHrPlannedQtyCALC",
		wor."Status"													as "WorHrStatus",
		wor."Warehouse"													as "WorHrWarehouse",
		wor."DocEntry"													as "WorHrDocEntry",
		wor."U_MnfOrdDocEntry"											as "WorHrU_MnfOrdDocEntry",
		wor."Comments"													as "WorHrComments",
		adm."QtyDec"													as "admQtyDec",
		adm."MltpBrnchs"												as "admMltpBrnchs",
		cfg."U_MrpHrItemCode"											as "cfgU_MrpHrItemCode",
		cfg."U_MrpCoProdItemCode"										as "cfgU_MrpCoProdItemCode",
	--	Expected WOR DueDate
		case
			when cfg."U_SynchDueDate" = 'S' and mor."U_RequiredDate" is		null and mor."U_PlannedStartDate" is	 null then wor."PostDate"
			when cfg."U_SynchDueDate" = 'S' and mor."U_RequiredDate" is		null and mor."U_PlannedStartDate" is not null then mor."U_PlannedStartDate"
			when cfg."U_SynchDueDate" = 'S' and mor."U_RequiredDate" is not null and mor."U_PlannedStartDate" is	 null then mor."U_RequiredDate"
			when cfg."U_SynchDueDate" = 'S' and mor."U_RequiredDate" is not null and mor."U_PlannedStartDate" is not null then mor."U_PlannedStartDate"
			when cfg."U_SynchDueDate" = 'R' and mor."U_RequiredDate" is		null and mor."U_PlannedStartDate" is	 null then wor."PostDate"
			when cfg."U_SynchDueDate" = 'R' and mor."U_RequiredDate" is		null and mor."U_PlannedStartDate" is not null then wor."PostDate"
			when cfg."U_SynchDueDate" = 'R' and mor."U_RequiredDate" is not null and mor."U_PlannedStartDate" is	 null then mor."U_RequiredDate"
			when cfg."U_SynchDueDate" = 'R' and mor."U_RequiredDate" is not null and mor."U_PlannedStartDate" is not null then mor."U_RequiredDate"

			when cfg."U_SynchDueDate" = 'E' and mor."U_RequiredDate" is		null and mor."U_PlannedEndDate"   is	 null then wor."PostDate"
			when cfg."U_SynchDueDate" = 'E' and mor."U_RequiredDate" is		null and mor."U_PlannedEndDate"   is not null then mor."U_PlannedEndDate"
			when cfg."U_SynchDueDate" = 'E' and mor."U_RequiredDate" is not null and mor."U_PlannedEndDate"   is	 null then mor."U_RequiredDate"
			when cfg."U_SynchDueDate" = 'E' and mor."U_RequiredDate" is not null and mor."U_PlannedEndDate"   is not null then mor."U_PlannedEndDate"

			else null
		end																as "ExpWorHrDueDate",
	--	Expected WOR StartDate
		case
			when cfg."U_SynchDueDate" = 'S' and mor."U_RequiredDate" is		null and mor."U_PlannedStartDate" is	 null then wor."PostDate"
			when cfg."U_SynchDueDate" = 'S' and mor."U_RequiredDate" is		null and mor."U_PlannedStartDate" is not null then mor."U_PlannedStartDate"
			when cfg."U_SynchDueDate" = 'S' and mor."U_RequiredDate" is not null and mor."U_PlannedStartDate" is	 null then mor."U_RequiredDate"
			when cfg."U_SynchDueDate" = 'S' and mor."U_RequiredDate" is not null and mor."U_PlannedStartDate" is not null then mor."U_PlannedStartDate"
			when cfg."U_SynchDueDate" = 'R' and mor."U_RequiredDate" is		null and mor."U_PlannedStartDate" is	 null then wor."PostDate"
			when cfg."U_SynchDueDate" = 'R' and mor."U_RequiredDate" is		null and mor."U_PlannedStartDate" is not null then wor."PostDate"
			when cfg."U_SynchDueDate" = 'R' and mor."U_RequiredDate" is not null and mor."U_PlannedStartDate" is	 null then mor."U_RequiredDate"
			when cfg."U_SynchDueDate" = 'R' and mor."U_RequiredDate" is not null and mor."U_PlannedStartDate" is not null then 
																																case
																																	when mor."U_RequiredDate" <= mor."U_PlannedStartDate" then mor."U_RequiredDate"
																																	else mor."U_PlannedStartDate"
																																end

			when cfg."U_SynchDueDate" = 'E' and mor."U_RequiredDate" is		null and mor."U_PlannedStartDate" is	 null then wor."PostDate"
			when cfg."U_SynchDueDate" = 'E' and mor."U_RequiredDate" is		null and mor."U_PlannedStartDate" is not null then wor."PostDate"
			when cfg."U_SynchDueDate" = 'E' and mor."U_RequiredDate" is not null and mor."U_PlannedStartDate" is	 null then mor."U_RequiredDate"
			when cfg."U_SynchDueDate" = 'E' and mor."U_RequiredDate" is not null and mor."U_PlannedStartDate" is not null then 
																																case
																																	when mor."U_RequiredDate" <= mor."U_PlannedStartDate" then mor."U_RequiredDate"
																																	else mor."U_PlannedStartDate"
																																end																															
			else null
		end																as "ExpWorHrStartDate",
	--	Expected WOR Status
		case
			when mor."U_Status" = 'NS' then 'P'
			when mor."U_Status" = 'SC' then 'P'
			when mor."U_Status" = 'RL' then 'R'
			when mor."U_Status" = 'ST' then 'R'
			when mor."U_Status" = 'FI' then 'R'
			when mor."U_Status" = 'CL' then 'L'
			else null
		end																as "ExpWorHrStatus"
	from 
		"@CT_PF_OMOR" as mor
		left join
			"OWOR" as wor
			on	mor."DocEntry" = wor."U_MnfOrdDocEntry"
			and	wor."Status" not in ('C', 'L')
			cross join
				"@CT_PF_CONFIG" as cfg
				cross join
					"OADM" as adm
),
MorLn as (
	select
		"MorLnTbl_ItemType"					as "MorLnTbl_ItemType",	
		"MorLnDocEntry"						as "MorLnDocEntry", 
		"MorLnU_LineNum"					as "MorLnU_LineNum",
		"MorLnU_ItemCode"					as "MorLnU_ItemCode", 
		"MorLnU_Project"					as "MorLnU_Project",
		round(("MorLnU_Result" - "MorLnU_ActualQty"),MorWorHr."admQtyDec")	
											as "MorLnPlannedQtyCALC",
		"MorLnU_WhsCode"					as "MorLnU_WhsCode",
		"MorLnU_StartDate"					as "MorLnU_StartDate",
		"MorLnU_EndDate"					as "MorLnU_EndDate",
		MorWorHr."MorHrU_Status"			as "MorHrU_Status",
		MorWorHr."WorHrStatus"				as "WorHrStatus"
	from
		(
		select
			'IT' 								as "MorLnTbl_ItemType",	
			mor3."DocEntry"						as "MorLnDocEntry", 
			mor3."U_LineNum"					as "MorLnU_LineNum",
			mor3."U_ItemCode"					as "MorLnU_ItemCode", 
			mor3."U_Project"					as "MorLnU_Project",
			mor3."U_Result"						as "MorLnU_Result", 
			mor3."U_ActualQty"					as "MorLnU_ActualQty", 
			mor3."U_WhsCode"					as "MorLnU_WhsCode",
			mor3."U_StartDate"					as "MorLnU_StartDate",
			mor3."U_EndDate"					as "MorLnU_EndDate"
		from
			"@CT_PF_MOR3" as mor3
		union all
		select
			'CP'								as "MorLnTbl_ItemType",
			mor1."DocEntry"						as "MorLnDocEntry",
			mor1."U_LineNum"					as "MorLnU_LineNum",
			mor1."U_ItemCode"					as "MorLnU_ItemCode", 
			mor1."U_Project"					as "MorLnU_Project",
			mor1."U_Result"						as "MorLnU_Result", 
			mor1."U_ActualQty"					as "MorLnU_ActualQty", 
			mor1."U_WhsCode"					as "MorLnU_WhsCode",
			mor1."U_StartDate"					as "MorLnU_StartDate",
			mor1."U_EndDate"					as "MorLnU_EndDate"
		from
			"@CT_PF_MOR1" as mor1
		union all
		select
			'SC'								as "MorLnTbl_ItemType",
			mor4."DocEntry"						as "MorLnDocEntry",
			mor4."U_LineNum"					as "MorLnU_LineNum",
			mor4."U_ItemCode"					as "MorLnU_ItemCode", 
			mor4."U_Project"					as "MorLnU_Project",
			mor4."U_Result"						as "MorLnU_Result", 
			mor4."U_ActualQty"					as "MorLnU_ActualQty", 
			mor4."U_WhsCode"					as "MorLnU_WhsCode",
			mor4."U_StartDate"					as "MorLnU_StartDate",
			mor4."U_EndDate"					as "MorLnU_EndDate"
		from
			"@CT_PF_MOR4" as mor4
		) as v
		inner join 
			MorWorHr
			on	v."MorLnDocEntry" = MorWorHr."MorHrDocEntry"
),
MorLnMorHr as (
	select
		MorLn."MorLnTbl_ItemType"			as "MorLnTbl_ItemType",
		MorLn."MorLnDocEntry"				as "MorLnDocEntry",
		MorLn."MorLnU_LineNum"				as "MorLnU_LineNum",
		MorLn."MorLnU_StartDate"			as "MorLnU_StartDate",
		MorLn."MorLnU_EndDate"				as "MorLnU_EndDate",
		MorLn."MorLnPlannedQtyCALC"			as "MorLnPlannedQtyCALC",
		MorLn."MorLnU_ItemCode"				as "MorLnU_ItemCode",
		MorLn."MorLnU_Project"				as "MorLnU_Project",
		MorLn."MorLnU_WhsCode"				as "MorLnU_WhsCode",
		MorWorHr."ExpWorHrStartDate"		as "ExpWorHrStartDate",
		MorWorHr."ExpWorHrDueDate"			as "ExpWorHrDueDate",
		MorWorHr."MorHrU_Status"			as "MorHrU_Status",
		MorWorHr."WorHrStatus"				as "WorHrStatus",
	--	Expected WOR Line StartDate
		case
			when MorLn."MorLnU_StartDate" between MorWorHr."ExpWorHrStartDate" and MorWorHr."ExpWorHrDueDate" then MorLn."MorLnU_StartDate"
			else MorWorHr."ExpWorHrStartDate"
		end								as "ExpWorLnStartDate",
	--	Expected WOR Line EndDate
		case
			when
				MorLn."MorLnU_EndDate" between 
											case
												when MorLn."MorLnU_StartDate" between MorWorHr."ExpWorHrStartDate" and MorWorHr."ExpWorHrDueDate" then MorLn."MorLnU_StartDate"
												else MorWorHr."ExpWorHrStartDate"
											end
										and
											MorWorHr."ExpWorHrDueDate" then MorLn."MorLnU_EndDate"
			else MorWorHr."ExpWorHrDueDate"
		end								as "ExpWorLnEndDate"
	from
		MorLn
		inner join
			MorWorHr
			on	MorLn."MorLnDocEntry" = MorWorHr."MorHrDocEntry"
),
WorLn as (
		select
			wor1."DocEntry"						as "WorLnDocEntry",
			wor1."ItemCode"						as "WorLnItemCode",
			wor1."Project"						as "WorLnProject",
			wor1."PlannedQty" * (case when wor1."PlannedQty" < 0 then -1 else 1 end)
												as "WorLnPlannedQtyCALC",
			wor1."PlannedQty"					as "WorLnPlannedQty",
			wor1."wareHouse"					as "WorLnwareHouse",
			wor1."StartDate"					as "WorLnStartDate",
			wor1."EndDate"						as "WorLnEndDate",
			wor."WorHrU_MnfOrdDocEntry"			as "WorHrU_MnfOrdDocEntry",
			wor1."U_LineNum"					as "WorLnU_LineNum",
			wor1."U_ItemType"					as "WorLnU_ItemType",
			wor."MorHrU_Status"					as "MorHrU_Status",
			wor."WorHrStatus"					as "WorHrStatus"
		from 
			"WOR1" as wor1
			inner join
				MorWorHr as wor
				on	wor1."DocEntry" = wor."WorHrDocEntry"
),
ViewAll as (
---========================================================================================================================================================================
---========================================================================================================================================================================
----	HR
----	"MorHrPlannedQtyCALC" > 0 / "U_MrpHrItemCode" - / MOR -> WOR
	select
		"MorHrDocEntry" as "MORXDocEntry"
		--,'HR diff' "Reason"
		--,'HR'
		,'"MorHrPlannedQtyCALC" > 0 / "U_MrpHrItemCode" - / MOR -> WOR' as "Reason"
	from
        MorWorHr
	where
		MorWorHr."MorHrU_Status" not in ('CL')	--	nie ma tego w dokumentacji
	and	MorWorHr."WorHrStatus" not in  ('L','C')
	and	"MorHrPlannedQtyCALC" > 0
	and	(
	--	dates
			coalesce("ExpWorHrDueDate", '1900-01-01') <> coalesce("WorHrDueDate", '1900-01-01')
		or	coalesce("ExpWorHrStartDate", '1900-01-01') <> coalesce("WorHrStartDate", '1900-01-01')

	--	items
		or	coalesce("MorHrU_ItemCode", '') <> coalesce("WorHrItemCode", '')

	--	project
		or	coalesce("MorHrU_Project", '') <> coalesce("WorHrProject", '')

	--	qty
		or	coalesce("MorHrPlannedQtyCALC", 0) <> coalesce("WorHrPlannedQtyCALC", 0)

	--	status
		or	coalesce("ExpWorHrStatus", '') <> coalesce("WorHrStatus", '')

	--	Warehouse
		or	coalesce("MorHrU_Warehouse",'') <> coalesce("WorHrWarehouse",'')
		)
---===========================================================================================
----	HR
----	"MorHrPlannedQtyCALC" <= 0 / "U_MrpHrItemCode" = null / MOR -> WOR
	union all
	select
		"MorHrDocEntry" as "MORXDocEntry"
		--,'HR diff' "Reason"
		--,'HR'
		,'"MorHrPlannedQtyCALC" <= 0 / "U_MrpHrItemCode" = null / MOR -> WOR'
	from
		MorWorHr
		left join
			(
			select
				whs."WhsCode"				as "WhsCode",
				bpl."U_DefaultComp"			as "U_MrpCoProdItemCode",
				bpl."U_DefaultFinalComp"	as "U_MrpHrItemCode"
			from
				"OWHS" as whs
				inner join
					"OBPL" as bpl
					on	whs."BPLid" = bpl."BPLId"
			) as cfg2
			on	MorWorHr."MorHrU_Warehouse" = cfg2."WhsCode"
	where
		MorWorHr."MorHrU_Status" not in ('CL')	--	nie ma tego w dokumentacji
	and	MorWorHr."WorHrStatus" not in  ('L','C')
	and	MorWorHr."MorHrPlannedQtyCALC" <= 0
	and	case 
			when MorWorHr."admMltpBrnchs" = N'Y' then cfg2."U_MrpHrItemCode"
			else MorWorHr."cfgU_MrpHrItemCode"
		end is null
	and	(
	--	dates
			coalesce(MorWorHr."ExpWorHrDueDate", '1900-01-01') <> coalesce(MorWorHr."WorHrDueDate", '1900-01-01')
		or	coalesce(MorWorHr."ExpWorHrStartDate", '1900-01-01') <> coalesce(MorWorHr."WorHrStartDate", '1900-01-01')

	--	items
		or	coalesce(MorWorHr."MorHrU_ItemCode", '') <> coalesce(MorWorHr."WorHrItemCode", '')

	--	project
		or	coalesce(MorWorHr."MorHrU_Project", '') <> coalesce(MorWorHr."WorHrProject", '')

	--	qty
		or	1 <> coalesce(MorWorHr."WorHrPlannedQtyCALC", 0)

	--	status
		or	coalesce(MorWorHr."ExpWorHrStatus", '') <> coalesce(MorWorHr."WorHrStatus", '')

	--	Warehouse
		or	coalesce(MorWorHr."MorHrU_Warehouse",'') <> coalesce(MorWorHr."WorHrWarehouse",'')
		)
---===========================================================================================
--	HR
--	"MorHrPlannedQtyCALC" <= 0 / "U_MrpHrItemCode" != null / MOR -> WOR
	union all
	select
		"MorHrDocEntry" as "MORXDocEntry"
		--,'HR diff' "Reason"
		--,'HR'
		,'"MorHrPlannedQtyCALC" <= 0 / "U_MrpHrItemCode" != null / MOR -> WOR'
	from
		MorWorHr
		left join
			(
			select
				whs."WhsCode"				as "WhsCode",
				bpl."U_DefaultComp"			as "U_MrpCoProdItemCode",
				bpl."U_DefaultFinalComp"	as "U_MrpHrItemCode"
			from
				"OWHS" as whs
				inner join
					"OBPL" as bpl
					on	whs."BPLid" = bpl."BPLId"
			) as cfg2
			on	MorWorHr."MorHrU_Warehouse" = cfg2."WhsCode"
	where
		MorWorHr."MorHrU_Status" not in ('CL')	--	nie ma tego w dokumentacji
	and	MorWorHr."WorHrStatus" not in  ('L','C')
	and	MorWorHr."MorHrPlannedQtyCALC" <= 0
	and	case 
			when MorWorHr."admMltpBrnchs" = N'Y' then cfg2."U_MrpHrItemCode"
			else MorWorHr."cfgU_MrpHrItemCode"
		end is not null
	and	(
	--	dates
			coalesce(MorWorHr."ExpWorHrDueDate", '1900-01-01') <> coalesce(MorWorHr."WorHrDueDate", '1900-01-01')
		or	coalesce(MorWorHr."ExpWorHrStartDate", '1900-01-01') <> coalesce(MorWorHr."WorHrStartDate", '1900-01-01')

	--	items
		or	coalesce(
			case 
				when MorWorHr."admMltpBrnchs" = N'Y' then cfg2."U_MrpHrItemCode"
				else MorWorHr."cfgU_MrpHrItemCode"
			end
		, '') <> coalesce(MorWorHr."WorHrItemCode", '')

	--	qty
		or	1 <> coalesce(MorWorHr."WorHrPlannedQtyCALC", 0)

	--	status
		or	 coalesce(MorWorHr."ExpWorHrStatus", '') <> coalesce(MorWorHr."WorHrStatus", '')

	--	Warehouse
		or	coalesce(MorWorHr."MorHrU_Warehouse",'') <> coalesce(MorWorHr."WorHrWarehouse",'')
		)
---========================================================================================================================================================================
---========================================================================================================================================================================
----	IT CP SC
----	"MorLnPlannedQtyCALC" > 0 / "U_MrpCoProdItemCode" - / MOR -> WOR
	union all 
	select
		MorLnMorHr."MorLnDocEntry" as "MORXDocEntry"
		--,'IT diff' "Reason"
		--,'IT'
		,'"MorLnPlannedQtyCALC" > 0 / "U_MrpCoProdItemCode" - / MOR -> WOR'
	from
		MorLnMorHr
		inner join
			WorLn
			on	MorLnMorHr."MorLnTbl_ItemType" = WorLn."WorLnU_ItemType"
			and	MorLnMorHr."MorLnDocEntry" = WorLn."WorHrU_MnfOrdDocEntry"
			and MorLnMorHr."MorLnU_LineNum" = WorLn."WorLnU_LineNum"
	where
		MorLnMorHr."MorHrU_Status" not in ('CL')	--	nie ma tego w dokumentacji
	and	MorLnMorHr."WorHrStatus" not in  ('L','C')
	and	MorLnMorHr."MorLnPlannedQtyCALC" > 0
	and	(
	--	dates
			coalesce(MorLnMorHr."ExpWorLnStartDate", '1900-01-01') <> coalesce(WorLn."WorLnStartDate", '1900-01-01')
		or	coalesce(MorLnMorHr."ExpWorLnEndDate", '1900-01-01') <> coalesce(WorLn."WorLnEndDate", '1900-01-01')

	--	items
		or	coalesce(MorLnMorHr."MorLnU_ItemCode", '') <> coalesce(WorLn."WorLnItemCode", '')

	--	projects
		or	coalesce(MorLnMorHr."MorLnU_Project", '') <> coalesce(WorLn."WorLnProject", '')

	--	qty
		or	coalesce(MorLnMorHr."MorLnPlannedQtyCALC", 0) <> coalesce(WorLn."WorLnPlannedQtyCALC", 0)

	--	Warehouse
		or	coalesce(MorLnMorHr."MorLnU_WhsCode",'') <> coalesce(WorLn."WorLnwareHouse",'')
		)
---========================================================================================================================================================================
----	IT CP SC
----	"MorLnPlannedQtyCALC" <= 0 / "U_MrpCoProdItemCode" - / MOR -> WOR
	union all
	select
		MorLnMorHr."MorLnDocEntry" as "MORXDocEntry"
		--,'IT diff' "Reason"
		--,'IT'
		,'"MorLnPlannedQtyCALC" <= 0 / "U_MrpCoProdItemCode" - / MOR -> WOR'
	from
		MorLnMorHr
		inner join
			WorLn
			on	MorLnMorHr."MorLnTbl_ItemType" = WorLn."WorLnU_ItemType"
			and	MorLnMorHr."MorLnDocEntry" = WorLn."WorHrU_MnfOrdDocEntry"
			and MorLnMorHr."MorLnU_LineNum" = WorLn."WorLnU_LineNum"
	where
		MorLnMorHr."MorHrU_Status" not in ('CL')	--	nie ma tego w dokumentacji
	and	MorLnMorHr."WorHrStatus" not in  ('L','C')
	and	MorLnMorHr."MorLnPlannedQtyCALC" <= 0
---========================================================================================================================================================================
----	IT CP SC
----	Wor Line not exist / MOR -> WOR
	union all
	select
		MorLn."MorLnDocEntry" as "MORXDocEntry"
		--,'IT diff' "Reason"
		--,'IT'
		,'Wor Line not exist / MOR -> WOR'
	from
		MorLn
		left join
			WorLn
			on	MorLn."MorLnTbl_ItemType" = WorLn."WorLnU_ItemType"
			and	MorLn."MorLnDocEntry" = WorLn."WorHrU_MnfOrdDocEntry"
			and MorLn."MorLnU_LineNum" = WorLn."WorLnU_LineNum"
			and	MorLn."WorHrStatus" not in  ('L','C')
	where
		MorLn."MorHrU_Status" not in ('CL')	--	nie ma tego w dokumentacji
	and	MorLn."MorLnPlannedQtyCALC" > 0
	and	WorLn."WorLnU_ItemType" is null
---========================================================================================================================================================================
----	IT CP SC
----	Wor Line exist and should not / MOR <- WOR				---	PFDEV-6141
	union all
	select
		WorLn."WorHrU_MnfOrdDocEntry" as "MORXDocEntry"
		--,'IT diff' "Reason"
		--,'IT'
		,'Wor Line exist and should not / MOR <- WOR // --- PFDEV-6141'
	from
		MorLnMorHr
		right join
			WorLn
			on	MorLnMorHr."MorLnTbl_ItemType" = WorLn."WorLnU_ItemType"
			and	MorLnMorHr."MorLnDocEntry" = WorLn."WorHrU_MnfOrdDocEntry"
			and MorLnMorHr."MorLnU_LineNum" = WorLn."WorLnU_LineNum"
			and	MorLnMorHr."MorHrU_Status" not in ('CL')
			inner join
				MorWorHr
				on	MorWorHr."MorHrDocEntry" = WorLn."WorHrU_MnfOrdDocEntry"
				left join
					(
					select
						whs."WhsCode"				as "WhsCode",
						bpl."U_DefaultComp"			as "U_MrpCoProdItemCode",
						bpl."U_DefaultFinalComp"	as "U_MrpHrItemCode"
					from
						"OWHS" as whs
						inner join
							"OBPL" as bpl
							on	whs."BPLid" = bpl."BPLId"
					) as cfg2
					on	MorWorHr."MorHrU_Warehouse" = cfg2."WhsCode"
	where
		WorLn."WorHrStatus" not in  ('L','C')
	and	MorLnMorHr."MorLnDocEntry" is null
	and	WorLn."WorLnItemCode" != 
			case 
				when MorWorHr."admMltpBrnchs" = N'Y' then cfg2."U_MrpCoProdItemCode"
				else MorWorHr."cfgU_MrpCoProdItemCode"
			end
	--and	WorLn."WorHrU_MnfOrdDocEntry" != 0
	--and	WorLn."WorHrU_MnfOrdDocEntry" is not null
---========================================================================================================================================================================
---========================================================================================================================================================================
---========================================================================================================================================================================
	union all
	select
		MorWorHr."MorHrDocEntry" as "MORXDocEntry"
		,'MOR closed, WOR open'
	from
		MorWorHr
	where
		MorWorHr."MorHrU_Status" in ('CL')
	and	MorWorHr."WorHrStatus" not in ('C','L')
---========================================================================================================================================================================
	union all
	select
		MorWorHr."MorHrDocEntry" as "MORXDocEntry"
		,'MOR open, No open WOR'
	from
		MorWorHr
	where	
		MorWorHr."MorHrU_Status" not in ('CL')
	and	(
			MorWorHr."WorHrStatus" in ('C','L')
		or	MorWorHr."WorHrStatus" is null
		)
---========================================================================================================================================================================
	union all
	select
		MorWorHr."MorHrDocEntry" as "MORXDocEntry"
		,'MOR open, Multiple open WOR'
	from
		MorWorHr
	where
		MorWorHr."MorHrU_Status" not in ('CL')
	and	MorWorHr."WorHrStatus" not in ('C','L')
	group by
		MorWorHr."MorHrDocEntry"
	having
		count(MorWorHr."MorHrDocEntry") > 1
---========================================================================================================================================================================
	union all
	select
		MorWorHr."MorHrDocEntry" as "MORXDocEntry"
		,'Remarks'
	from
		MorWorHr
	where
		MorWorHr."MorHrU_Status" not in ('CL')
	and	MorWorHr."WorHrStatus" not in ('C','L')
	and CAST(COALESCE(MorWorHr."MorHrU_Remarks", '') as NVARCHAR(254)) <> CAST(COALESCE(MorWorHr."WorHrComments", '') as NVARCHAR(254))
)
select
	distinct
	"MORXDocEntry"
from
	ViewAll with (nolock)
