with ls as (select LpuSection_id from v_LpuSection   where Lpu_id=$1)
		select
		MedProductCard.MedProductCard_id as id,
		MedProductCard.LpuSection_id as "lpuSection.id",
		MedProductCard.LpuUnit_id as "lpuUnit.id",
		MedProductCard.LpuBuilding_id as "lpuBuilding.id",
		MedProductClass.MedProductType_id as "medProductType.id",
		MedProductType.MedProductType_Code as "medProductType.code",
		MedProductType.MedProductType_Name as "medProductType.name",
		case when coalesce(MedProductCard.MedProductCard_IsRepair, 1)=2 or coalesce(MedProductCard.MedProductCard_IsSpisan , 1)=2 or MedProductCard.MedProductCauseType_id is not NULL then 2 else 1 end as "productStatus",
		(select   LpuSectionMedProductTypeLink_updDT from LpuSectionMedProductTypeLink link  
		where link.MedProductType_id = MedProductClass.MedProductType_id and link.LpuSection_id = MedProductCard.LpuSection_id  limit 1 ) as "productDate",
		(select count(veq.EvnQueue_id)   from v_EvnQueue veq   where veq.EvnQueueStatus_id=3 and veq.Resource_did in
		(select Resource_id from passport.v_MedProductCardResource   where MedProductCard_id = MedProductCard.MedProductCard_id)) as "evnQueueCount",
		(select max(veq.EvnQueue_updDT )  from v_EvnQueue veq   where veq.EvnQueueStatus_id=3 and veq.Resource_did in
		(select Resource_id from passport.v_MedProductCardResource   where MedProductCard_id = MedProductCard.MedProductCard_id)) as "evnUpdDt"
		from passport.v_MedProductCard MedProductCard  
		left join passport.v_MedProductClass MedProductClass   on MedProductCard.MedProductClass_id = MedProductClass.MedProductClass_id
		left join passport.v_MedProductType MedProductType   on MedProductClass.MedProductType_id = MedProductType.MedProductType_id
		 WHERE $2 = 2
			and (LpuSection_id in (select LpuSection_id from ls) or LpuBuilding_id in (select LpuBuilding_id from v_LpuSection where LpuSection_id in (select LpuSection_id from ls)))
			and ($3 != 2 or (coalesce(MedProductCard.MedProductCard_IsRepair, 1)=1 and coalesce(MedProductCard.MedProductCard_IsSpisan , 1)=1 and MedProductCard.MedProductCauseType_id is NULL))
			and ($4 = 0 or MedProductClass.MedProductType_id = $5)
