
select * from rpt.han_pan_evn_pg(
$1::timestamp, 
$2::timestamp,
$3::bigint,
$4::bigint,
$5::bigint,
$6::bigint,
$7::bigint,
$8::bigint,
$9::integer,
$10::bigint
)

/*with prm as (
select to_date('01.06.2021','dd.mm.yyyy') as p_paramBegDate,
       to_date('10.10.2021','dd.mm.yyyy') as p_paramEndDate,
       10101000000001::bigint as p_paramlpu,
       null::bigint  as p_paramLpuBuilding,
       null::bigint  as p_paramLpuUnit,
       null::bigint  as p_paramLpuSection,  
       null::bigint as p_parampaytype,       
       null::bigint  as paramMedStaffFact,
       coalesce(1,1)::integer as paramYesNo,
       null::bigint  as paramMedStaffSisterPolka
)


, pl as (
			select 1 as flg,
			       v.medstafffact_id,                   
                   mid_fio,
                   mid_post_id, 
                   pl.evnpl_id as case_id,
                   v.evnvizitpl_id as step_id      
			  from prm 
              join v_evnPL pl on pl.lpu_id = p_paramLpu and pl.evnpl_setdate between p_paramBegDate and p_paramEndDate and pl.evnclass_id  in (3,6)
			  join v_evnvizitpl v on v.evnvizitpl_pid = pl.evnpl_id and v.evnvizitpl_setdate between p_paramBegDate and p_paramEndDate                
                and (paramMedStaffFact is null or  v.medpersonal_id = paramMedStaffFact)
                and (paramMedStaffSisterPolka is null or  v.medpersonal_sid = paramMedStaffSisterPolka)                
                and (paramYesNo = 1 or paramYesNo = 2 and v.medpersonal_sid is not null)			    
                and (p_paramLpuSection is null or v.lpusection_id = p_paramLpuSection)
			    and (p_paramPayType is null or v.paytype_id = p_paramPayType)    
			  join v_LpuSection ls on ls.lpusection_id = v.lpusection_id
			    and (p_paramLpuBuilding is null or ls.LpuBuilding_id = p_paramLpuBuilding)
			    and (p_paramLpuUnit is null or ls.lpuunit_id = p_paramLpuUnit)	
              left join lateral (
                   select act.person_fio as mid_fio,
                          case when act.lpusection_id = v.lpusection_id then act.Post_id end as mid_post_id
                     from v_medstafffact act where act.medpersonal_id = v.medpersonal_sid and act.PostKind_id = 6 order by act.lpusection_id != v.lpusection_id ,act.medstafffact_upddt limit 1)  as l_act on true)
            
, ps as (
			select 2 as flg,                    
                   s.medstafffact_id,
                   null::text  as mid_fio,
                   null::bigint as mid_post_id,
			       ps.evnps_id as case_id,
			       s.evnsection_id as step_id       
			  from prm
              join v_evnPS ps on ps.lpu_id = p_paramLpu and ps.evnps_setdate between p_paramBegDate and p_paramEndDate
			  join v_EvnSection s on s.evnsection_pid = ps.evnps_id and s.evnsection_setdate between p_paramBegDate and p_paramEndDate
                and (paramMedStaffFact is null or  s.medpersonal_id = paramMedStaffFact)
                and (paramMedStaffSisterPolka is null and paramYesNo = 1)
			    and (p_paramLpuSection is null or s.lpusection_id = p_paramLpuSection)     
			    and (p_paramPayType is null or s.paytype_id = p_paramPayType)
			  join v_LpuSection ls on ls.lpusection_id = s.lpusection_id
			    and (p_paramLpuBuilding is null or ls.LpuBuilding_id = p_paramLpuBuilding)
			    and (p_paramLpuUnit is null or ls.lpuunit_id = p_paramLpuUnit))

, pl_ps as (
			select * from pl
			union all  
			select * from ps)
              
 select l.org_nick,
        act.person_fio as doc_fio,
        p.name as dolgnost_name,
        mid_fio,
        pos.name as mid_post,
       count(distinct case when flg=1 then case_id end) as pl_case,
       count(distinct case when flg=1 then step_id end) as pl_viz,
       count(distinct case when flg=2 then case_id end) as ps_case,
       count(distinct case when flg=2 then step_id end) as ps_step
  from pl_ps pp
  join v_medstafffact act on act.medstafffact_id = pp.medstafffact_id
  join v_lpu l on l.lpu_id = act.lpu_id
  left join persis.post p on p.id = act.post_id
  left join persis.post pos on pos.id = mid_post_id  
group by 1,2,3,4,5
order by 1,2*/
