416 lines
18 KiB
Plaintext
416 lines
18 KiB
Plaintext
<cfsilent>
|
||
<cfimport prefix="m" taglib="lib"/>
|
||
<cfimport prefix="c" taglib="lib/controls"/>
|
||
<cfimport prefix="d" taglib="lib/data"/>
|
||
<cfimport prefix="layout" taglib="layout"/>
|
||
</cfsilent><m:silent silent="No">
|
||
|
||
<cffunction name="hideNonPositive">
|
||
<cfargument name="a"/>
|
||
<cfreturn (a GT 0)? a : ""/>
|
||
</cffunction>
|
||
|
||
<cffunction name="myNumFmt">
|
||
<cfargument name="a"/>
|
||
<cfreturn (isNumeric(a))? numberFormat(a,",.00") : ""/>
|
||
</cffunction>
|
||
|
||
<m:prepare_ls entity="specification_item" accessObject="" pageInfoOut="pageInfo" trackOut="tr"/>
|
||
|
||
<!--- <m:filter_settings target="#pageInfo.entity#_ls">
|
||
<m:filterparam filter=#filter# param="quickfilter" ftype="string" prefix="%" suffix="%" expression="((p.project like ?) OR (p.customer like ?) OR (p.customer_alias like ?) OR (p.specification_item_class_type like ?) OR (p.division like ?) OR (p.performer_short like ?))" default=""/>
|
||
</m:filter_settings> --->
|
||
<!--- <cfset pageInfo.settings.filter=#filter#/> --->
|
||
|
||
|
||
<cfquery name="qRead" datasource="#request.DS#">
|
||
<!--- select
|
||
<d:field_set titleMapOut="titleMap" lengthOut="fieldCount">
|
||
<d:field title="Месяц" cfSqlType="CF_SQL_TIMESTAMP">m</d:field>
|
||
<d:field title="Дата" cfSqlType="CF_SQL_TIMESTAMP">dt</d:field>
|
||
<d:field title="Инсталл" cfSqlType="CF_SQL_NUMERIC">income_install</d:field>
|
||
<d:field title="Инсталл" cfSqlType="CF_SQL_NUMERIC">inst1</d:field>
|
||
<d:field title="Инсталл" cfSqlType="CF_SQL_NUMERIC">inst0</d:field>
|
||
<d:field title="Ежемес" cfSqlType="CF_SQL_NUMERIC">income_recurring</d:field>
|
||
<d:field title="Ежемес+" cfSqlType="CF_SQL_NUMERIC">rec1+rec0 as rec</d:field>
|
||
<d:field title="Ежемес" cfSqlType="CF_SQL_NUMERIC">rec1</d:field>
|
||
<d:field title="Ежемес" cfSqlType="CF_SQL_NUMERIC">rec0</d:field>
|
||
<d:field title="Выручка" cfSqlType="CF_SQL_NUMERIC">income_install+income_recurring as income</d:field>
|
||
<d:field title="cnt" cfSqlType="CF_SQL_NUMERIC">cnt</d:field>
|
||
</d:field_set>
|
||
from ( --->
|
||
select
|
||
--install
|
||
DATE_TRUNC('month', dayscale.dt)::date as m
|
||
,dayscale.dt::date
|
||
,siv.price as price_i
|
||
,siv.quantity as quantity_i
|
||
,siv0.price as price0_i
|
||
,siv0.quantity as quantity0_i
|
||
,a.is_actual as is_actual_i
|
||
,a.probability_perc as p_i
|
||
,(100-COALESCE(a.probability_perc,0))/100 as p_i_rest
|
||
,siv.specification_item_uid as si_uid_i
|
||
,siv.agreement_version as av_i
|
||
,siv0.specification_item_uid as si_uid0_i
|
||
,siv0.agreement_version as av0_i
|
||
,null as price_f
|
||
,null as quantity_f
|
||
,null as price0_f
|
||
,null as quantity0_f
|
||
,null as is_actual_f
|
||
,null as p_f
|
||
,null as p_f_rest
|
||
,null as sum_f
|
||
,null as dt_from_f
|
||
,null as dt_to_f
|
||
,null as dt_from0_f
|
||
,null as dt_to0_f
|
||
,null as si_uid_f
|
||
,null as av_f
|
||
,null as si_uid0_f
|
||
,null as av0_f
|
||
|
||
from (SELECT dd::date as dt FROM generate_series(
|
||
'2023-01-01'::timestamp
|
||
,'2029-12-31'::timestamp
|
||
,'1 day'::interval) dd
|
||
) dayscale
|
||
left outer join specification_item_version siv on (dayscale.dt=siv.dt_from)
|
||
left outer join specification_item si on (siv.specification_item_uid=si.specification_item_uid AND si.pricing_model_id in (1))
|
||
left outer join specification s on (si.specification_id=s.specification_id)
|
||
left outer 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)
|
||
left outer join agreement a0 on (s.contract_id=a0.contract_id AND a0.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.agreement_version < a.agreement_version
|
||
AND ia.is_actual
|
||
))
|
||
left outer join specification_item_version siv0 on (siv0.specification_item_uid=si.specification_item_uid
|
||
AND siv0.agreement_version=a0.agreement_version AND (dayscale.dt=siv0.dt_from))
|
||
where 1=1 <m:filter_build filter=#pageInfo.settings.filter#/>
|
||
|
||
union all
|
||
|
||
--fix and payg
|
||
select
|
||
DATE_TRUNC('month', dayscale.dt)::date as m
|
||
,dayscale.dt::date
|
||
,null
|
||
,null
|
||
,null
|
||
,null
|
||
,null
|
||
,null
|
||
,null
|
||
,null
|
||
,null
|
||
,null
|
||
,null
|
||
,case when (siv.dt_to > dayscale.dt OR siv.dt_to IS NULL) then
|
||
siv.price
|
||
/*до окончания свежей сделки она суммируется с прошлой актуальной по правилу вероятностей, считая вероятность актуальной за сделки за 1*/
|
||
else 0 /*по окончании свежей сделки она дает нулевой вклад*/
|
||
end
|
||
,case when (siv.dt_to > dayscale.dt OR siv.dt_to IS NULL) then
|
||
siv.quantity
|
||
/*до окончания свежей сделки она суммируется с прошлой актуальной по правилу вероятностей, считая вероятность актуальной за сделки за 1*/
|
||
else 0 /*по окончании свежей сделки она дает нулевой вклад*/
|
||
end
|
||
,case when (siv0.dt_to > dayscale.dt OR siv0.dt_to IS NULL) then
|
||
siv0.price
|
||
/*до окончания свежей сделки она суммируется с прошлой актуальной по правилу вероятностей, считая вероятность актуальной за сделки за 1*/
|
||
else 0 /*по окончании свежей сделки она дает нулевой вклад*/
|
||
end
|
||
,case when (siv0.dt_to > dayscale.dt OR siv0.dt_to IS NULL) then
|
||
siv0.quantity
|
||
/*до окончания свежей сделки она суммируется с прошлой актуальной по правилу вероятностей, считая вероятность актуальной за сделки за 1*/
|
||
else 0 /*по окончании свежей сделки она дает нулевой вклад*/
|
||
end
|
||
,a.is_actual as is_actual_f
|
||
,a.probability_perc as p_f
|
||
,(100-COALESCE(a.probability_perc,0))/100 as p_f_rest
|
||
|
||
,case when (siv.dt_to > dayscale.dt OR siv.dt_to IS NULL) then
|
||
siv.price/dayscale.days*siv.quantity*a.probability_perc/100
|
||
else 0 /*по окончании свежей сделки она дает нулевой вклад*/
|
||
end
|
||
+
|
||
COALESCE(case when (siv0.dt_to > dayscale.dt OR siv0.dt_to IS NULL) then
|
||
siv.price/dayscale.days*siv.quantity*(100- a.probability_perc)/100
|
||
else 0 /*по окончании свежей сделки она дает нулевой вклад*/
|
||
end,0)
|
||
,siv.dt_from
|
||
,siv.dt_to
|
||
,siv0.dt_from
|
||
,siv0.dt_to
|
||
,siv.specification_item_uid
|
||
,siv.agreement_version
|
||
,siv0.specification_item_uid
|
||
,siv0.agreement_version
|
||
|
||
|
||
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
|
||
,'2029-12-31'::timestamp
|
||
,'1 day'::interval) dd
|
||
) dayscale
|
||
left outer join specification_item_version siv on (dayscale.dt >= siv.dt_from /*AND (siv.dt_to > dayscale.dt OR siv.dt_to IS NULL)*/
|
||
AND siv.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=siv.specification_item_uid
|
||
AND (dayscale.dt >= iiv.dt_from /*AND (iiv.dt_to > dayscale.dt OR iiv.dt_to IS NULL)*/)))
|
||
/*AND ia.is_actual*/ /*свежее соглашение не обязано быть действующим*/
|
||
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 a.agreement_version=siv.agreement_version)
|
||
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)
|
||
-- находим предыдущее действующее соглашение
|
||
-- если свежее соглашение действует, то впр
|
||
-- нам нужно, чтобы в прошлом (до dt_from свежего соглашения) свежак был прозрачен,
|
||
-- а в будущем (после dt_to) факт перетирался свежим соглашением
|
||
left outer join agreement a0 on (s.contract_id=a0.contract_id AND a0.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.agreement_version < siv.agreement_version
|
||
AND ia.is_actual /*именно действующее соглашение, принимаемое за факт*/
|
||
)
|
||
)
|
||
left outer join specification_item_version siv0 on (siv0.specification_item_uid=si.specification_item_uid
|
||
AND siv0.agreement_version=a0.agreement_version AND (dayscale.dt >= siv0.dt_from /*AND (siv0.dt_to > dayscale.dt OR siv0.dt_to IS NULL)*/))
|
||
where 1=1 <m:filter_build filter=#pageInfo.settings.filter#/>
|
||
|
||
order by dt
|
||
|
||
</cfquery>
|
||
<!--- <cfdump var=#pageInfo.settings.filter#/> --->
|
||
|
||
|
||
|
||
|
||
<!--- <cfquery name="qCountTotal" datasource="#request.DS#">
|
||
select count(*) as cnt from specification_item where 1=1
|
||
</cfquery> --->
|
||
|
||
</m:silent><!---
|
||
-----------------------------------------------------------------------------------------
|
||
-----------------------------------------------------------------------------------------
|
||
-----------------------------------------------------------------------------------------
|
||
---><cfif isDefined("output_xls")>
|
||
<layout:xml qRead=#qRead# titleMap=#titleMap# filename="#pageInfo.entity#.xml"/>
|
||
<cfabort/>
|
||
</cfif><cfif isDefined("output_json")>
|
||
<layout:json qRead=#qRead# titleMap=#titleMap# filename="#pageInfo.entity#.json"/>
|
||
<cfabort/>
|
||
</cfif><!---
|
||
---><layout:page section="header" pageInfo=#pageInfo#>
|
||
|
||
<layout:attribute name="title">
|
||
<cfoutput><b>Выручка по дням 2023-01-01 .. 2029-12-31</b></cfoutput>
|
||
</layout:attribute>
|
||
<!--- <layout:attribute name="controls">
|
||
skip filter link, filter is not implemented
|
||
<layout:language_switch/>
|
||
</layout:attribute> --->
|
||
</layout:page>
|
||
|
||
<cfif pageInfo.readPermitted() AND !pageInfo.status.errorState>
|
||
|
||
<!--- <cfdump var=#qRead#/><cfabort/> --->
|
||
|
||
|
||
<div style="display:inline-block; vertical-align:top;">
|
||
<cfoutput><b>#qRead.recordCount#</b> записей</cfoutput>
|
||
<a href="?output_xls" title="экспорт в Excel" style="margin:.5em; height:100%;" target="_blank"><img src="img/xls.gif" style="vertical-align:text-bottom;"/></a>
|
||
<a href="?output_json" title="экспорт в json" style="margin:.5em; height:100%;" target="_blank"><img src="img/json.svg" style="vertical-align:text-bottom;" width="13" height="13"/></a>
|
||
<br/>
|
||
Дни и месяцы без данных не показаны.
|
||
Инсталл и ежемес могут генерировать отдельные строки на один день.
|
||
Вариант с локальной рабочей версией строки (инстанса)
|
||
<table class="worktable">
|
||
|
||
<thead>
|
||
<tr>
|
||
<th>Дата</th>
|
||
<!--- <th>Инсталл</th> --->
|
||
<th>Инсталл рабочая</th>
|
||
<th>Инсталл базовая</th>
|
||
<!--- <th>Ежемес</th> --->
|
||
<th>Ежемес рабочая</th>
|
||
<th>Ежемес базовая</th>
|
||
<th>Ежемес</th>
|
||
<!--- <th>Выручка</th>
|
||
<th>cnt</th> --->
|
||
</tr>
|
||
</thead>
|
||
|
||
<cfoutput query="qRead" group="dt">
|
||
<tr>
|
||
<td class="c">#dateFormat(dt,"YYYY-MM-DD")#</td>
|
||
<!--- <td class="r">#myNumFmt(income_install)#</td> --->
|
||
<td class="l">
|
||
<cfoutput>
|
||
<a href="specification_item_version.cfm?specification_item_uid=#si_uid_i#&agreement_version=#av_i#&#tr.fwx#">
|
||
#myNumFmt(price_i)#*#quantity_i#*#p_i#(#p_i_rest#)
|
||
</a>
|
||
</cfoutput>
|
||
</td>
|
||
<td class="l">
|
||
<cfoutput>
|
||
<a href="specification_item_version.cfm?specification_item_uid=#si_uid0_i#&agreement_version=#av0_i#&#tr.fwx#">
|
||
#myNumFmt(price0_i)#*#quantity0_i#
|
||
</a>
|
||
</cfoutput>
|
||
</td>
|
||
<td class="l">
|
||
<cfoutput>
|
||
<a href="specification_item_version.cfm?specification_item_uid=#si_uid_f#&agreement_version=#av_f#&#tr.fwx#">
|
||
#myNumFmt(price_f)#*#quantity_f#*#p_f#(#p_f_rest#)
|
||
#dateFormat(dt_from_f,'YYYY-MM-DD')# - #dateFormat(dt_to_f,'YYYY-MM-DD')#
|
||
</a>
|
||
</cfoutput>
|
||
</td>
|
||
<td class="l">
|
||
<cfoutput>
|
||
<a href="specification_item_version.cfm?specification_item_uid=#si_uid0_f#&agreement_version=#av0_f#&#tr.fwx#">
|
||
#myNumFmt(price0_f)#*#quantity0_f#
|
||
#dateFormat(dt_from0_f,'YYYY-MM-DD')# - #dateFormat(dt_to0_f,'YYYY-MM-DD')#
|
||
</a>
|
||
</cfoutput>
|
||
</td>
|
||
<td class="r"><cfoutput>#myNumFmt(sum_f)#</cfoutput></td>
|
||
</tr>
|
||
</cfoutput>
|
||
|
||
</table>
|
||
</div>
|
||
|
||
<cfabort/>
|
||
|
||
|
||
<div style="display:inline-block; vertical-align:top;">
|
||
|
||
<cfquery dbtype="query" name="qMonthly">
|
||
select m
|
||
, sum(income_install) as income_install
|
||
, sum(income_recurring) as income_recurring
|
||
, sum(income) as income
|
||
from qRead
|
||
group by m
|
||
order by m
|
||
</cfquery>
|
||
<br/>
|
||
Суммировано по месяцам:
|
||
<table class="worktable">
|
||
|
||
<thead>
|
||
<tr>
|
||
<th>Месяц</th>
|
||
<th>Инсталл</th>
|
||
<th>Ежемес</th>
|
||
<th>Выручка</th>
|
||
</tr>
|
||
</thead>
|
||
|
||
<cfoutput query="qMonthly">
|
||
<tr>
|
||
<td class="c">#dateFormat(m,"MM.YYYY")#</td>
|
||
<td class="r">#myNumFmt(income_install)#</td>
|
||
<td class="r">#myNumFmt(income_recurring)#</td>
|
||
<td class="r">#myNumFmt(income)#</td>
|
||
</tr>
|
||
</cfoutput>
|
||
</table>
|
||
</div>
|
||
|
||
</cfif>
|
||
<layout:page section="footer"/>
|
||
|
||
|
||
<!---
|
||
|
||
-- select *
|
||
select
|
||
--DATE_TRUNC('month', dayscale.dt)::date as m,
|
||
|
||
dayscale.dt::date
|
||
--,a.agreement_version as v
|
||
,siv.agreement_version
|
||
,siv.dt_from::date
|
||
,siv.dt_to::date
|
||
,siv.quantity
|
||
,case when siv.dt_to >=dayscale.dt OR siv.dt_to IS NULL then siv.quantity else -100500 end as q
|
||
|
||
,a0.agreement_version as v0
|
||
,siv0.dt_from::date
|
||
,siv0.dt_to::date
|
||
,siv0.quantity
|
||
--,0 ,0 ,0
|
||
/*
|
||
,COALESCE(sum(siv.price*siv.quantity*COALESCE(a.probability_perc,0)/100/dayscale.days),0)
|
||
+ COALESCE(sum(siv0.price*siv0.quantity*(100-COALESCE(a.probability_perc,0))/100/dayscale.days),0)
|
||
,sum(siv.price*siv.quantity*COALESCE(a.probability_perc,0)/100/dayscale.days)
|
||
,sum(siv0.price*siv0.quantity*(100-COALESCE(a.probability_perc,0))/100/dayscale.days)
|
||
,count(*)
|
||
*/
|
||
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
|
||
,'2029-12-31'::timestamp
|
||
,'1 day'::interval) dd
|
||
) dayscale
|
||
left outer join specification_item_version siv on (dayscale.dt >= siv.dt_from /*AND (siv.dt_to >= dayscale.dt OR siv.dt_to IS NULL)*/
|
||
AND siv.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=siv.specification_item_uid
|
||
AND (dayscale.dt >= iiv.dt_from /*AND (iiv.dt_to >= dayscale.dt OR iiv.dt_to IS NULL)*/)))
|
||
/*AND ia.is_actual*/ /*свежее соглашение не обязано быть действующим*/
|
||
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 a.agreement_version=siv.agreement_version)
|
||
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)
|
||
-- находим предыдущее действующее соглашение
|
||
-- если свежее соглашение действует, то впр
|
||
-- нам нужно, чтобы в прошлом (до dt_from свежего соглашения) свежак был прозрачен,
|
||
-- а в будущем (после dt_to) факт перетирался свежим соглашением
|
||
left outer join agreement a0 on (s.contract_id=a0.contract_id AND a0.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.agreement_version < siv.agreement_version
|
||
AND ia.is_actual /*именно действующее соглашение, принимаемое за факт*/
|
||
)
|
||
)
|
||
left outer join specification_item_version siv0 on (siv0.specification_item_uid=si.specification_item_uid
|
||
AND siv0.agreement_version=a0.agreement_version AND (dayscale.dt >= siv0.dt_from /*AND (siv0.dt_to >= dayscale.dt OR siv0.dt_to IS NULL)*/)
|
||
)
|
||
where d.contragent_id=4
|
||
--group by dayscale.dt
|
||
---> |