with zzz as
(
		SELECT
	lu.LpuUnit_Phone as MO_Phone,
	lu.LpuUnit_id AS MO_Id,
			coalesce (LpuUnit_FRMOUnitID,FRMOUnit.FRMOUnit_OID,FRMOSection.FRMOUnit_OID) AS MO_OID,
			CASE WHEN LpuUnit_FRMOid IS NOT NULL THEN lu.LpuUnit_Name || ', ' || Org.Org_Nick
			ELSE coalesce(FRMOUnit.FRMOUnit_Name,FRMOSection.FRMOUnit_Name) || ', ' || Org.Org_Nick end AS MO_Name,
			dbo.ConcatPunkt(v_address.Address_Zip, ', ','',3,0) ||
	dbo.ConcatPunkt(KLAreaRegion.KLArea_FullName,', ','',3,0)  ||
	coalesce(KLCity.KLSocr_Nick ||'. ' ||KLCity.KLCity_Name,KLTown.KLSocr_Nick || '. ' || KLTown.KLTown_Name) || 
	dbo.ConcatPunkt(',',  KLStreet.KLStreet_FullName,' ',3,0) ||		dbo.ConcatPunkt(', д.', v_address.Address_House,' ',3,0) AS MO_Address,	
	coalesce(msf.Person_Snils || '.' || CAST(msf.MedStaffFact_id AS VARCHAR(20)),'00000000000.' || CAST(msf.MedStaffFact_id AS VARCHAR(20))) 	AS Resource_id,	
	Person_Fio as Resource_Name, 
	lrs.LpuRegionStreet_HouseSet AS LpuRegionStreet_HouseSet,
	LpuRegion_updDT,lr.LpuRegion_id,lu.LpuUnit_id,msf.MedStaffFact_id
	FROM dbo.v_KLStreet ks
	INNER JOIN dbo.LpuRegionStreet lrs ON lrs.KLStreet_id = ks.KLStreet_id 
	INNER JOIN dbo.v_LpuRegion lr ON lr.LpuRegion_id = lrs.LpuRegion_id
	INNER JOIN dbo.v_LpuSection ls   ON ls.LpuSection_id = lr.LpuSection_id
	LEFT JOIN v_lpuunit lu   ON lu.LpuUnit_id = ls.LpuUnit_id 
	left JOIN nsi.FRMOUnit   ON FRMOUnit.FRMOUnit_id = lu.FRMOUnit_id
			LEFT JOIN LATERAL (SELECT FRMOSection.FRMOUnit_OID, FRMOUnit_Name FROM dbo.v_LpuSection
			INNER JOIN nsi.FRMOSection ON FRMOSection.FRMOSection_id = v_LpuSection.FRMOSection_id and FRMOSection.FRMOUnit_OID IS NOT null AND v_LpuSection.LpuUnit_id=lu.LpuUnit_id
			INNER JOIN nsi.FRMOUnit ON FRMOUnit.FRMOUnit_OID = FRMOSection.FRMOUnit_OID
			limit 1) FRMOSection ON true
	INNER JOIN dbo.v_Lpu Lpu ON Lpu.Lpu_id =lu.Lpu_id and coalesce (Lpu.Lpu_IsTest,5)!=2
	INNER JOIN fed.PassportToken Tokens ON Tokens.Lpu_id = lu.Lpu_id
	INNER JOIN dbo.v_Org Org ON	Org.Org_id =Lpu.Org_id
	INNER JOIN dbo.v_Address as v_address	ON	v_address.Address_id = lu.Address_id
	INNER JOIN dbo.v_KLArea	as KLAreaRegion ON KLAreaRegion.KLArea_id = Lpu.Region_id
	LEFT JOIN dbo.v_KLCity as KLCity ON KLCity.KLCity_id =	v_address.KLCity_id
	LEFT JOIN dbo.v_KLTown as KLTown ON kltown.KLTown_id=v_address.KLTown_id
	INNER JOIN dbo.v_KLStreet as KLStreet ON  KLStreet.KLStreet_id = v_address.KLStreet_id
	INNER JOIN dbo.v_MedStaffRegion AS msr ON msr.LpuRegion_id = lr.LpuRegion_id
	inner JOIN dbo.v_MedStaffFact AS msf ON msf.MedStaffFact_id = msr.MedStaffFact_id AND coalesce(msf.WorkData_endDate,dbo.tzGetDate()+ interval '1 day') > dbo.tzGetDate()
	inner join nsi.v_PostLink PostLink  on msf.Post_id =	PostLink.Post_lid
	inner join nsi.v_Post Post  on PostLink.Post_id = Post.Post_id
	INNER JOIN dbo.v_DataStorage AS dataS ON dataS.lpu_id=Lpu.Lpu_id and DataStorage_Name = 'homevizit_isallowed' and DataStorage_Value = '1' 
	where
	ks.KLAdr_Code like $1 ||'%' 
	AND 
	lr.lpu_id <> 101 
	AND (case when dbo.age((SELECT Person_BirthDay FROM v_personstate WHERE Person_id= $2), GETDATE()) >= 18
			then lr.LpuRegionType_SysNick = 'ter'
			else lr.LpuRegionType_SysNick = 'ped'
			end
	or lr.LpuRegionType_SysNick='vop')
	AND MedStaffRegion_isMain=2 
	AND lu.LpuUnitType_id=2
	and coalesce(LpuUnit_FRMOUnitID,FRMOUnit.FRMOUnit_OID,FRMOSection.FRMOUnit_OID) IS NOT NULL
	order by ks.kladr_code, LpuRegionType_id, LpuRegion_updDT desc	
)

	SELECT MO_Phone,MO_Id,MO_OID,MO_Name,MO_Address,Resource_id,
	Resource_Name,LpuRegion_id,LpuUnit_id,MedStaffFact_id
	from zzz
	WHERE dbo.GetHouse(coalesce(LpuRegionStreet_HouseSet,'  '), $3) = 1
	limit 1
