select  /*переделать выборку версий через group by*/
			/* dayscale.dt
			,a.contract_id
			,a.agreement_version
			,a.deal_uid
			,a.probability_perc
			,siv.dt_from			
			,siv.dt_to			
			,siv.quantity				
			,siv.price*/		
			DATE_TRUNC('month', siv.dt_from)::date AS month_install
			,sum(siv.price*siv.quantity*a.probability_perc/100) as cost_install
			--,si.pricing_model_id
			
		from 
		(SELECT dd::date as dt	FROM generate_series
			( '2024-01-01'::timestamp
			, '2029-12-31'::timestamp
			, '1 day'::interval) dd) dayscale
		join specification_item_version siv on (dayscale.dt=siv.dt_from)
		join specification_item si on (siv.specification_item_uid=si.specification_item_uid)
		join specification s on (si.specification_id=s.specification_id)
		join agreement a on (s.contract_id=a.contract_id AND siv.agreement_version=a.agreement_version AND a.agreement_version=(select max(iiv.agreement_version) from specification_item_version iiv 
				join specification_item ii on (iiv.specification_item_uid=ii.specification_item_uid)
				join specification isp on (ii.specification_id=isp.specification_id)
				join agreement ia on (isp.contract_id=ia.contract_id AND iiv.agreement_version=ia.agreement_version)
				where iiv.specification_item_uid=si.specification_item_uid
				AND ia.is_actual
			))
		join contract d on s.contract_id=d.contract_id
		join contragent k on d.contragent_id=k.contragent_id
		join svc on (si.svc_id=svc.svc_id)
		where si.pricing_model_id=1
		group by DATE_TRUNC('month', siv.dt_from)
		--order by ds.dt
	union all	
		
select 
			/*dayscale.dt
			,a.contract_id
			,a.agreement_version
			,a.deal_uid
			,a.probability_perc
			,siv.dt_from			
			,siv.quantity				
			,siv.price	
			,si.pricing_model_id*/
			DATE_TRUNC('month', dayscale.dt)::date AS month_i
			,sum(siv.price*siv.quantity*a.probability_perc/100/dayscale.days) as cost_install
			
			
		from 
		(SELECT dd::date as dt, DATE_PART('days', 
		        DATE_TRUNC('month', dd) + '1 MONTH'::INTERVAL - '1 DAY'::INTERVAL
		    ) days	FROM generate_series
			( '2025-01-01'::timestamp
			, '2025-12-31'::timestamp
			, '1 day'::interval) dd) dayscale
		join specification_item_version siv on (dayscale.dt >= siv.dt_from /**/)
		join specification_item si on (siv.specification_item_uid=si.specification_item_uid)
		join specification s on (si.specification_id=s.specification_id AND si.pricing_model_id in (2,3))
		join agreement a on (s.contract_id=a.contract_id AND siv.agreement_version=a.agreement_version AND a.agreement_version=(select max(iiv.agreement_version) from specification_item_version iiv 
				join specification_item ii on (iiv.specification_item_uid=ii.specification_item_uid)
				join specification isp on (ii.specification_id=isp.specification_id)
				join agreement ia on (isp.contract_id=ia.contract_id AND iiv.agreement_version=ia.agreement_version)
				where iiv.specification_item_uid=si.specification_item_uid
				AND ia.is_actual
			))
		join contract d on s.contract_id=d.contract_id
		join contragent k on d.contragent_id=k.contragent_id
		join svc on (si.svc_id=svc.svc_id)

		group by DATE_TRUNC('month', dayscale.dt)
		order by DATE_TRUNC('month', dayscale.dt)
		
				select 		
			DATE_TRUNC('month', dayscale.dt)::date AS month_start
			,sum(siv.price*siv.quantity*a.probability_perc/100) as income			
		from 
		(SELECT dd::date as dt	FROM generate_series
			( '2023-01-01'::timestamp
			, '2027-12-31'::timestamp
			, '1 day'::interval) dd) dayscale
		join specification_item_version siv on (dayscale.dt=siv.dt_from)
		join specification_item si on (siv.specification_item_uid=si.specification_item_uid AND si.pricing_model_id in (1))
		join specification s on (si.specification_id=s.specification_id)
		join agreement a on (s.contract_id=a.contract_id AND siv.agreement_version=a.agreement_version AND a.agreement_version=(select max(iiv.agreement_version) from specification_item_version iiv 
				join specification_item ii on (iiv.specification_item_uid=ii.specification_item_uid)
				join specification isp on (ii.specification_id=isp.specification_id)
				join agreement ia on (isp.contract_id=ia.contract_id AND iiv.agreement_version=ia.agreement_version)
				where iiv.specification_item_uid=si.specification_item_uid
				AND ia.is_actual
			))
		join contract d on s.contract_id=d.contract_id
		join contragent k on d.contragent_id=k.contragent_id
		join svc on (si.svc_id=svc.svc_id)
		where si.pricing_model_id=1
		group by DATE_TRUNC('month', dayscale.dt)

	union all	
		
select 
		 DATE_TRUNC('month', dayscale.dt)::date 
		,sum(siv.price*siv.quantity*a.probability_perc/100/dayscale.days)				
		from 
		(SELECT dd::date as dt, DATE_PART('days', 
		        DATE_TRUNC('month', dd) + '1 MONTH'::INTERVAL - '1 DAY'::INTERVAL
		    ) days	FROM generate_series
			( '2023-01-01'::timestamp
			, '2027-12-31'::timestamp
			, '1 day'::interval) dd) dayscale
		join specification_item_version siv on (dayscale.dt >= siv.dt_from AND (siv.dt_to >= dayscale.dt OR siv.dt_to IS NULL))
		join specification_item si on (siv.specification_item_uid=si.specification_item_uid AND si.pricing_model_id in (2,3))
		join specification s on (si.specification_id=s.specification_id )
		join agreement a on (s.contract_id=a.contract_id AND siv.agreement_version=a.agreement_version AND a.agreement_version=(select max(iiv.agreement_version) from specification_item_version iiv 
				join specification_item ii on (iiv.specification_item_uid=ii.specification_item_uid)
				join specification isp on (ii.specification_id=isp.specification_id)
				join agreement ia on (isp.contract_id=ia.contract_id AND iiv.agreement_version=ia.agreement_version)
				where iiv.specification_item_uid=si.specification_item_uid
				AND ia.is_actual
			))
		join contract d on s.contract_id=d.contract_id
		join contragent k on d.contragent_id=k.contragent_id
		join svc on (si.svc_id=svc.svc_id)

		group by DATE_TRUNC('month', dayscale.dt)
		order by 1
		
		
			select 		
		 DATE_TRUNC('month', dayscale.dt)::date AS month_start
		,sum(siv.price*siv.quantity*a.probability_perc/100) as income			
	from (SELECT dd::date as dt FROM generate_series( 
		 '2023-01-01'::timestamp
		, '2027-12-31'::timestamp
		, '1 day'::interval) dd
		) dayscale
	join specification_item_version siv on (dayscale.dt=siv.dt_from)
	join specification_item si on (siv.specification_item_uid=si.specification_item_uid AND si.pricing_model_id in (1))
	join specification s on (si.specification_id=s.specification_id)
	join agreement a on (s.contract_id=a.contract_id AND siv.agreement_version=a.agreement_version AND a.agreement_version=(select max(iiv.agreement_version) from specification_item_version iiv 
		join specification_item ii on (iiv.specification_item_uid=ii.specification_item_uid)
		join specification isp on (ii.specification_id=isp.specification_id)
		join agreement ia on (isp.contract_id=ia.contract_id AND iiv.agreement_version=ia.agreement_version)
		where iiv.specification_item_uid=si.specification_item_uid
		AND ia.is_actual
	))
	join contract d on s.contract_id=d.contract_id
	join contragent k on d.contragent_id=k.contragent_id
	join svc on (si.svc_id=svc.svc_id)
	where si.pricing_model_id=1
	group by DATE_TRUNC('month', dayscale.dt)

	union all	
		
	select 
		 DATE_TRUNC('month', dayscale.dt)::date 
		,sum(siv.price*siv.quantity*a.probability_perc/100/dayscale.days)				
	from (SELECT dd::date as dt
			, DATE_PART('days', DATE_TRUNC('month', dd) + '1 MONTH'::INTERVAL - '1 DAY'::INTERVAL) days 
			FROM generate_series( 
			'2023-01-01'::timestamp
			, '2027-12-31'::timestamp
			, '1 day'::interval) dd
		) dayscale
	join specification_item_version siv on (dayscale.dt >= siv.dt_from AND (siv.dt_to >= dayscale.dt OR siv.dt_to IS NULL))
	join specification_item si on (siv.specification_item_uid=si.specification_item_uid AND si.pricing_model_id in (2,3))
	join specification s on (si.specification_id=s.specification_id )
	join agreement a on (s.contract_id=a.contract_id AND siv.agreement_version=a.agreement_version AND a.agreement_version=(select max(iiv.agreement_version) from specification_item_version iiv 
		join specification_item ii on (iiv.specification_item_uid=ii.specification_item_uid)
		join specification isp on (ii.specification_id=isp.specification_id)
		join agreement ia on (isp.contract_id=ia.contract_id AND iiv.agreement_version=ia.agreement_version)
		where iiv.specification_item_uid=si.specification_item_uid
		AND ia.is_actual
	))
	join contract d on s.contract_id=d.contract_id
	join contragent k on d.contragent_id=k.contragent_id
	join svc on (si.svc_id=svc.svc_id)

	group by DATE_TRUNC('month', dayscale.dt)
	order by 1
	
	