with params as(
	select
		p_LpuList,
		p_LpuFilialList,
		p_LpuBuilding,
		p_LpuUnit_id,
		p_LpuSection_id,
		p_LpuSectionProfile_id,
		p_Order413,
		p_IsGUZOnly,
		p_region,
		p_begdate 
			+ interval'1day'*(1 - coalesce(p_Order413, 2))
			as p_Stac_begDate,
		p_enddate
			+ interval'1day'*(2 - coalesce(p_Order413, 2))
			as p_Stac_endDate,
		p_begdate 
			+ interval'1day'*(1 - coalesce(p_Order413, 2))
			+ coalesce(
				p_BegTime,
				case when p_region in (58,50) then '08:00' else '09:00' end::time)
			as p_Stac_begDT,
		p_enddate
			+ interval'1day'*(2 - coalesce(p_Order413, 2))
			+ coalesce(
				p_BegTime,
				case when p_region in (58,50) then '08:00' else '09:00' end::time)
			as p_Stac_endDT
	from(
		select
			$1 as p_LpuList,
			$2 as p_LpuFilialList,
			cast($3 as bigint) as p_LpuBuilding,
			cast($4 as bigint) as p_LpuUnit_id,
			cast($5 as bigint) as p_LpuSection_id,
			cast($6 as bigint) as p_LpuSectionProfile_id,
			cast($7 as timestamp) as p_begdate,
			cast($8 as timestamp) as p_enddate, 
			cast($9 as bigint) as p_Order413,
			cast($10 as bigint) as p_IsGUZOnly,
			cast($11 as time)p_BegTime,
			cast($12 as bigint) as p_region
	)sub
),
LpuP as(
	select
		L.Lpu_id,
		params.*
	from params
		inner join lateral(
			select L.Lpu_id
			from rpt.GetLpuListFromStr_pg(p_LpuList,p_Stac_begDate, p_Stac_endDate, 1, 2)L
				inner join Org as O on L.Org_id = O.Org_id
			where case
					when p_region = 59 and L.Lpu_id in (13002556,10011362) or O.Okfs_id in (2,4,5)
						then 2
					else 1 end >= coalesce(p_IsGUZOnly,1)
		)L on true
),
LS as(
    select
        LS.LpuSection_id,
        LS.LpuSectionBedProfile_id,
        LS.LpuSectionProfile_id,
        LSP.LpuSectionProfile_Code
    from LpuP 
        inner join LpuBuilding as LB on LB.Lpu_id = LpuP.Lpu_id
        inner join LpuUnit as LU on LU.LpuBuilding_id = LB.LpuBuilding_id
        inner join LpuUnitType as LUT on LU.LpuUnitType_id = LUT.LpuUnitType_id
        inner join LpuSection as LS on LS.LpuUnit_id = LU.LpuUnit_id
        inner join LpuSectionProfile as LSP on LS.LpuSectionProfile_id = LSP.LpuSectionProfile_id
    where (coalesce(LS.LpuSection_deleted,1) = 1)
        and (LS.LpuSection_id = p_LpuSection_id or p_LpuSection_id is null)
        and LS.LpuSectionProfile_id!= 75
        and (LS.LpuSectionProfile_id = p_LpuSectionProfile_id or p_LpuSectionProfile_id is null)
        and (LS.LpuUnit_id = p_LpuUnit_id or p_LpuUnit_id is null)
        and (LB.LpuBuilding_id = p_lpubuilding or p_lpubuilding is null)
        and LUT.LpuUnitType_Sysnick = 'stac'
        and (LB.LpuFilial_id in(select id from rpt.GetIDs_FromStrList(p_LpuFilialList))
            or coalesce(p_LpuFilialList,'') = '')
    union
    select
        LS.LpuSection_id,
        LS.LpuSectionBedProfile_id,
        LSLSP.LpuSectionProfile_id,
        LSP.LpuSectionProfile_Code
    from LpuP 
        inner join LpuBuilding as LB on LB.Lpu_id = LpuP.Lpu_id
        inner join LpuUnit as LU on LU.LpuBuilding_id = LB.LpuBuilding_id
        inner join LpuUnitType as LUT on LU.LpuUnitType_id = LUT.LpuUnitType_id
        inner join LpuSection as LS on LS.LpuUnit_id = LU.LpuUnit_id
        inner join LpuSectionLpuSectionProfile as LSLSP on LSLSP.LpuSection_id = LS.LpuSection_id
        inner join LpuSectionProfile as LSP on LSLSP.LpuSectionProfile_id = LSP.LpuSectionProfile_id
    where (coalesce(LS.LpuSection_deleted,1) = 1)
        and (LS.LpuSection_id = p_LpuSection_id or p_LpuSection_id is null)
        and LS.LpuSectionProfile_id!= 75
        and (LSLSP.LpuSectionProfile_id = p_LpuSectionProfile_id or p_LpuSectionProfile_id is null)
        and (LS.LpuUnit_id = p_LpuUnit_id or p_LpuUnit_id is null)
        and (LB.LpuBuilding_id = p_lpubuilding or p_lpubuilding is null)
        and LUT.LpuUnitType_Sysnick = 'stac'
        and (LB.LpuFilial_id in(select id from rpt.GetIDs_FromStrList(p_LpuFilialList))
            or coalesce(p_LpuFilialList,'') = '')
)
select 
    count(distinct
        case
            when EvnUsluga.EvnUsluga_disDT is not null 
                    and datediff('day', EvnUsluga.EvnUsluga_setDT, EvnUsluga.EvnUsluga_disDT) = 0 
                then EvnPS.EvnPS_id end) as "ColAll_1",
    count(distinct
        case
            when EvnUsluga.EvnUsluga_disDT is not null 
                    and datediff('day', EvnUsluga.EvnUsluga_setDT, EvnUsluga.EvnUsluga_disDT) between 0 and 2
                then EvnPS.EvnPS_id end) as "ColAll_3",
    count(distinct
        case
            when EvnUsluga.EvnUsluga_setDT is not null
                    and (EvnUsluga.EvnUsluga_disDT is null 
                        or datediff('day', EvnUsluga.EvnUsluga_setDT, EvnUsluga.EvnUsluga_disDT) >=  30) 
                then EvnPS.EvnPS_id end) as "ColAll_30", 
    count(distinct
        case
            when LeaveType.LeaveType_SysNick in ('die','psumer','ksdie','dsdie','kslet','ksletitar')
                    and EvnUsluga.EvnUsluga_setDT is not null 
                    and datediff('hour', EvnUsluga.EvnUsluga_setDT, Evnsection.EvnSection_disDT) between 0 and 1  
                then EvnPS.EvnPS_id end) as "ColDie_Min0",  
    count(distinct
        case
            when LeaveType.LeaveType_SysNick in ('die','psumer','ksdie','dsdie','kslet','ksletitar')
                    and EvnUsluga.EvnUsluga_setDT is not null 
                    and datediff('day', EvnUsluga.EvnUsluga_setDT, Evnsection.EvnSection_disDT) <=  1  
                then EvnPS.EvnPS_id end) as "ColDie_Min1"   
from LpuP 
    inner join v_EvnPS as EvnPS on EvnPS.Lpu_id = LpuP.Lpu_id
    inner join v_EvnSection as EvnSection on EvnPS.EvnPS_id = EvnSection.EvnSection_pid
    inner join LS on LS.LpuSection_id = EvnSection.LpuSection_id and (LS.LpuSectionProfile_id = EvnSection.LpuSectionProfile_id or p_lpusectionprofile_id is null)
    left join v_LeaveType as LeaveType on LeaveType.LeaveType_id = EvnPS.LeaveType_id
    inner join v_EvnUsluga as EvnUsluga on EvnUsluga.EvnUsluga_pid = EvnSection.EvnSection_id
    inner join v_UslugaComplex as UslugaComplex 
        on UslugaComplex.UslugaComplex_id = EvnUsluga.UslugaComplex_id
where EvnPS.EvnPS_disDT >=  LpuP.p_Stac_begDT 
    and EvnPS.EvnPS_disDT < LpuP.p_Stac_endDT
    and UslugaComplex.UslugaComplex_Code in ('A14.08.001','A16.09.011','A16.09.011.001','A16.09.011.002',
    'A16.09.011.003','A16.09.012','A16.12.030','A18.05.001','A18.05.002','A18.05.002.001','A18.05.006',
    'A16.09.011.004')
