Files
payg/crm2cloud.cfm
T
2026-04-29 12:42:04 +03:00

460 lines
17 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<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:prepare_ls entity="crm2cloud" settingsKey="crm2cloud" accessObject="" pageInfoOut="pageInfo" trackOut="tr"/>
<!--- *** нуждается в оптимизации, чтобы фильтр по контрагенту ускорял селект --->
<cfparam name="dt_report" default=#Now()#/>
<cfquery name="qRead" cachedWithin=#createTimeSpan(0, 0, 25, 0)#>
WITH
status(status, status_id, probability_pc) as
(
VALUES
('Закрыта неуспешно',7,0) ,
('Закрыта успешно',6,100) ,
('Договор подписан',5,100) ,
('Договор на подписании',15,90) ,
('Договор на согласовании',12,70),
('Тестирование',8,50) ,
('Отправлено ТКП',10,20) ,
('Внутреннее согласование',37,10),
('Проработка решения',4,10) ,
('Сбор потребностей',1,0)
)
-----------------------------
,line AS (
SELECT siv.__id AS siv_uid
,siv.discount
,siv.type AS line_pricing_model_id
,siv.HASH AS line_key
,siv.is_actual AS siv_is_actual
,siv.version::NUMERIC AS line_version
,d.__index AS deal_index
,d._companies
,a.INDEX AS agreement_version
,siv.date_nop::DATE AS dt_from
,siv.date_end::DATE AS dt_to
,siv.is_easy AS is_simple
,siv.cnt
,s.probability_pc
,a.__id AS agreement_uid
,a.contract_uid
,a.is_actual AS agreement_is_actual
,a.deal_uid
,d.__name AS deal
,d.__status_status AS deal_status_id
,siv.user_description
,siv.params
FROM elma.deals_services siv
JOIN elma.deals d ON (d.__id = ANY (siv.deal))
JOIN STATUS s ON (d.__status_status = s.status_id)
LEFT JOIN elma.additional_agreements a ON (a.__id = ANY (siv.additional_agreement))
WHERE siv.is_actual
AND siv."__deletedAt" IS NULL
AND d."__deletedAt" IS NULL
AND a."__deletedAt" IS NULL
AND s.probability_pc >= 50
--and a.is_actual
AND siv.type IN (2, 3)
)
-----------------------------
,component AS (
SELECT
-- price*cnt*(100.0-COALESCE(discount,0))/100.0 as cost ,
-- price ,
cnt
,code
,__id
FROM elma.service_parametrs
WHERE "__deletedAt" IS NULL
)
------------------------------
,stair AS (
SELECT *
FROM (
SELECT *
,MIN(dt_from) OVER (
PARTITION BY line_key ORDER BY deal_index DESC
) running_min_dt
,COALESCE((
LEAD(dt_from) OVER (
PARTITION BY line_key ORDER BY dt_from
,deal_index
)
), (
<cfqueryparam cfsqltype="cf_sql_timestamp" value=#dt_report+1#/>
<!--- CURRENT_TIMESTAMP + INTERVAL '1 day'--->
)) dt_next
FROM line
WHERE probability_pc > 0
) t
WHERE t.dt_from = t.running_min_dt
<!--- AND (t.dt_to > <cfqueryparam cfsqltype="cf_sql_timestamp" value=#dt_report#/> OR t.dt_to IS NULL) --->
)
,line_expanded
AS (
SELECT line.*
,unnest(line.params) AS param_id
FROM line
)
-----------------------------
,alloc AS (
SELECT c."WZ" AS wz
,round(sum(c."VM_RAM_Allocated")) AS ram
,round(sum(CASE WHEN "VM_is_ON" > 0 THEN c."VM_RAM_Allocated" ELSE 0 END)) AS ram_on
,round(sum(c."VM_vCPU_count")) AS cpu
,round(sum(CASE WHEN "VM_is_ON" > 0 THEN c."VM_vCPU_count" ELSE 0 END)) AS cpu_on
FROM vmreports.COMPUTE c
WHERE ts = (
SELECT max(ts)
FROM vmreports.COMPUTE
)
GROUP BY c."WZ"
)
,disk_alloc AS (
SELECT c."WZ" AS wz
,round(sum(c."Disk_Allocated")) AS disk_alloc
FROM vmreports.storage c
WHERE ts = (
SELECT max(ts)
FROM vmreports.COMPUTE
)
GROUP BY c."WZ"
)
-----------------------------
,sold_ram AS (
SELECT z.id_klienta AS WZ
,z.__name AS client
,z.__id AS company_uid
,round(sum(c.cnt * o.cnt)) AS quantity
,count(*) AS lines
,STRING_AGG(DISTINCT c.code, ', ' ORDER BY c.code) AS codes
FROM stair o
JOIN elma.companies z ON o._companies = z.__id
--LEFT JOIN component c on (c.__id = ANY (o.params))
LEFT JOIN line_expanded e ON o.siv_uid = e.siv_uid
LEFT JOIN component c ON c.__id = e.param_id
WHERE o.dt_from <= <cfqueryparam cfsqltype="cf_sql_timestamp" value=#dt_report#/>
AND (<cfqueryparam cfsqltype="cf_sql_timestamp" value=#dt_report#/> < o.dt_next)
--c.code SIMILAR TO 'iaas.(ngc|dcp).%.ram-_'
AND (c.code LIKE 'iaas.ngc.%.ram-_' OR c.code LIKE 'iaas.dcp.%.ram-_')
AND c.cnt <> 0
AND o.cnt <> 0
GROUP BY z.id_klienta
,z.__id
,z.__name
)
------------------------------
,sold_cpu AS (
SELECT z.id_klienta AS WZ
,z.__name AS client
,z.__id AS company_uid
,round(sum(c.cnt * o.cnt)) AS quantity
,count(*) AS lines
,STRING_AGG(DISTINCT c.code, ', ' ORDER BY c.code) AS codes
FROM stair o
JOIN elma.companies z ON (o._companies = z.__id)
LEFT JOIN line_expanded e ON o.siv_uid = e.siv_uid
LEFT JOIN component c ON (c.__id = e.param_id)
WHERE o.dt_from <= <cfqueryparam cfsqltype="cf_sql_timestamp" value=#dt_report#/>
AND (<cfqueryparam cfsqltype="cf_sql_timestamp" value=#dt_report#/> < o.dt_next)
--c.code SIMILAR TO 'iaas.(ngc|dcp).%.vcpu%-_'
AND (
c.code LIKE 'iaas.ngc.%.vcpu%-_'
OR c.code LIKE 'iaas.dcp.%.vcpu%-_'
)
AND c.cnt <> 0
AND o.cnt <> 0
GROUP BY z.id_klienta
,z.__id
,z.__name
)
---------------------------------
,sold_disk AS (
SELECT z.id_klienta AS WZ
,z.__name AS client
,z.__id AS company_uid
,round(sum(c.cnt * o.cnt)) AS quantity
,count(*) AS lines
,STRING_AGG(DISTINCT c.code, ', ' ORDER BY c.code) AS codes
FROM stair o
JOIN elma.companies z ON (o._companies = z.__id)
--LEFT JOIN component c ON c.__id = o.param_id
--LEFT JOIN component c on (c.__id = ANY (o.params))
LEFT JOIN line_expanded e ON o.siv_uid = e.siv_uid
LEFT JOIN component c ON c.__id = e.param_id
WHERE o.dt_from <= <cfqueryparam cfsqltype="cf_sql_timestamp" value=#dt_report#/>
AND (<cfqueryparam cfsqltype="cf_sql_timestamp" value=#dt_report#/> < o.dt_next)
--c.code SIMILAR TO 'iaas.(ngc|dcp).%.(ssd|fstssd|sata)-_'
AND (
c.code LIKE 'iaas.ngc.%.ssd-_'
OR c.code LIKE 'iaas.dcp.%.ssd-_'
OR c.code LIKE 'iaas.ngc.%.fstssd-_'
OR c.code LIKE 'iaas.dcp.%.fstssd-_'
OR c.code LIKE 'iaas.ngc.%.sata-_'
OR c.code LIKE 'iaas.dcp.%.sata-_'
)
AND c.cnt <> 0
AND o.cnt <> 0
GROUP BY z.id_klienta
,z.__id
,z.__name
)
--------------------------------
,sold_gpu AS (
SELECT z.id_klienta AS WZ
,z.__name AS client
,z.__id AS company_uid
,round(sum(c.cnt * o.cnt)) AS quantity
,count(*) AS lines
,STRING_AGG(DISTINCT c.code, ', ' ORDER BY c.code) AS codes
FROM stair o
JOIN elma.companies z ON (o._companies = z.__id)
--LEFT JOIN component c ON c.__id = o.param_id
--LEFT JOIN component c on (c.__id = ANY (o.params))
LEFT JOIN line_expanded e ON o.siv_uid = e.siv_uid
LEFT JOIN component c ON c.__id = e.param_id
WHERE o.dt_from <= <cfqueryparam cfsqltype="cf_sql_timestamp" value=#dt_report#/>
AND (<cfqueryparam cfsqltype="cf_sql_timestamp" value=#dt_report#/> < o.dt_next)
--c.code SIMILAR TO 'iaas.(ngc|dcp).%.vcpu%-_'
AND (
c.code LIKE 'iaas.ngc.%.gpu%-_'
OR c.code LIKE 'iaas.dcp.%.gpu%-_'
)
AND c.cnt <> 0
AND o.cnt <> 0
GROUP BY z.id_klienta
,z.__id
,z.__name
)
-------------------------------
,gpu_alloc AS (
SELECT c."ClientID" AS wz
,
-- json_agg("GPU") as gpu,
string_agg("GPU"::TEXT || ' ' || "OrgStatus", ', ') AS gpu
,count(*) AS gpu_alloc
FROM vmreports.vm_gpu c
WHERE ts = (
SELECT max(ts)
FROM vmreports.vm_gpu
)
GROUP BY c."ClientID"
)
--------------------------------
,deal_stat AS (
SELECT STRING_AGG(DISTINCT deal_status_id::TEXT, ', ' ORDER BY deal_status_id::TEXT) AS status_ids
,ARRAY_AGG(DISTINCT deal_status_id ORDER BY deal_status_id) AS ar_status_ids
,STRING_AGG(DISTINCT line_pricing_model_id::TEXT, ', ' ORDER BY line_pricing_model_id::TEXT) AS line_pricing_model_ids
,ARRAY_AGG(DISTINCT line_pricing_model_id ORDER BY line_pricing_model_id) AS ar_line_pricing_model_id
,z.id_klienta AS WZ
FROM stair d
--LEFT JOIN LATERAL unnest(d.params) AS arr(param_id) ON TRUE
--JOIN component p ON (p.__id = arr.param_id)
JOIN elma.companies z ON (d._companies = z.__id)
WHERE d.dt_from <= <cfqueryparam cfsqltype="cf_sql_timestamp" value=#dt_report#/>
AND (<cfqueryparam cfsqltype="cf_sql_timestamp" value=#dt_report#/> < d.dt_next)
GROUP BY z.id_klienta
)
select
<d:field_set titleMapOut="titleMap" lengthOut="fieldCount">
<d:field title="WZ Elma">r.wz as wz_sold</d:field>
<d:field title="Клиент">r.client as client_sold</d:field>
<d:field>r.company_uid</d:field>
<d:field title="WZ Cloud">a.wz as wz_alloc</d:field>
<d:field title="Статусы">ds.status_ids</d:field>
<d:field title="Модели оплаты">ds.line_pricing_model_ids</d:field>
<d:field title="Коды RAM">r.codes as ram_codes</d:field>
<d:field title="RAM GB Elma">r.quantity as ram_sold</d:field>
<d:field title="RAM Cloud">a.ram as ram_alloc</d:field>
<d:field title="RAM Cloud ON">a.ram_on as ram_alloc_on</d:field>
<d:field title="Коды CPU">c.codes as cpu_codes</d:field>
<d:field title="CPU Elma">c.quantity as cpu_sold</d:field>
<d:field title="CPU Cloud">a.cpu as cpu_alloc</d:field>
<d:field title="CPU Cloud ON">a.cpu_on as cpu_alloc_on</d:field>
<d:field title="Коды Дисков">d.codes as disk_codes</d:field>
<d:field title="Диски GB Elma">d.quantity as disk_sold</d:field>
<d:field title="Диски Cloud">da.disk_alloc</d:field>
<d:field title="Коды GPU">g.codes as gpu_codes</d:field>
<d:field title="GPU Elma">g.quantity as gpu_sold</d:field>
<d:field title="GPU Cloud">ga.gpu_alloc</d:field>
<d:field title="GPU">ga.gpu</d:field>
<d:field>z.__name as alloc_company</d:field>
<d:field>z.__id as alloc_company_uid</d:field>
<d:field>ds.ar_status_ids</d:field>
</d:field_set>
from sold_ram r
join deal_stat ds on (lower(r.wz)=lower(ds.wz))
join sold_cpu c on (lower(r.wz)=lower(c.wz))
left join sold_disk d on (lower(r.wz)=lower(d.wz))
left join sold_gpu g on (lower(r.wz)=lower(g.wz))
full join alloc a on (lower(r.wz)=lower(a.wz))
left join disk_alloc da on (a.wz=da.wz)
left join gpu_alloc ga on (a.wz=ga.wz)
left join elma.companies z on (lower(a.wz)=lower(z.id_klienta))
where 1=1 <m:filter_build filter=#pageInfo.settings.filter#/>
order by <m:order_build sortArray=#pageInfo.settings.sort.sortArray# fieldCount=#fieldCount#/>
</cfquery>
<!--- <cfdump var=#qRead#> --->
<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>Сверка CRM - Cloud</b></cfoutput>
</layout:attribute>
</layout:page>
<!--- <cfdump var=#qRead# abort=true/> --->
<!---<cfoutput> <form method="post" action="">
Период с <input type="text" name="dt_start" value="#dateFormat(dt_start,'YYYY-MM-DD')#"/>
по <input type="text" name="dt_finish" value="#dateFormat(dt_finish,'YYYY-MM-DD')#"/>
<input type="submit" style="cursor:pointer;"/>
<input type="submit" name="DEBUG" value="DEBUG" style="cursor:pointer;"/>
</form> --->
<!--- Часов в периоде: <b>#hours#</b> Строк в отчете: <b>#qCharge.recordCount#</b><br/>
Актуальность данных:
VCD Computing: <b>#dateFormat(qComputingAge.dt_load,'YYYY-MM-DD')# #timeFormat(qComputingAge.dt_load,'HH:MM:SS')#</b>
VCD Storage: <b>#dateFormat(qStorageAge.dt_load,'YYYY-MM-DD')# #timeFormat(qStorageAge.dt_load,'HH:MM:SS')#</b>
S3 хранение: <b>#dateFormat(qS3VolAge.dt_load,'YYYY-MM-DD')# #timeFormat(qS3VolAge.dt_load,'HH:MM:SS')#</b>
S3 операции и трафик: <b>#dateFormat(qS3OpsTrfAge.dt_load,'YYYY-MM-DD')# #timeFormat(qS3OpsTrfAge.dt_load,'HH:MM:SS')#</b>
--->
<!--- <br/>
<a href="#request.thisPage#?output_xls" title="экспорт в Excel" style="margin-left:.5em; height:100%;" target="_blank"><img src="img/xls.gif" style="vertical-align:text-bottom;"/></a>
<a href="#request.thisPage#?output_json" title="экспорт в json" style="margin-left:.5em; height:100%;" target="_blank"><img src="img/json.svg" style="vertical-align:text-bottom;" width="13" height="13"/></a></cfoutput>
<br/> --->
<layout:grid_summary
recordCount=#qRead.recordCount#
footerOut="gridFooter"
excelLink="Yes"
jsonLink="Yes"
/>
<table class="worktable">
<thead>
<layout:grid_head titleMap=#titleMap# sortArray=#pageInfo.settings.sort.sortArray#>
<th width="5%"><layout:column_head name="wz_sold"/></th>
<th width="10%"><layout:column_head name="client_sold"/></th>
<th width="5%"><layout:column_head name="wz_alloc"/></th>
<th width="3%"><layout:column_head name="status_ids"/></th>
<th width="3%"><layout:column_head name="line_pricing_model_ids"/></th>
<th width="10%"><layout:column_head name="cpu_codes"/></th>
<th width="3%"><layout:column_head name="cpu_sold"/></th>
<th width="3%"><layout:column_head name="cpu_alloc"/></th>
<th width="3%"><layout:column_head name="cpu_alloc_on"/></th>
<th width="10%"><layout:column_head name="ram_codes"/></th>
<th width="3%"><layout:column_head name="ram_sold"/></th>
<th width="3%"><layout:column_head name="ram_alloc"/></th>
<th width="3%"><layout:column_head name="ram_alloc_on"/></th>
<th width="10%"><layout:column_head name="disk_codes"/></th>
<th width="3%"><layout:column_head name="disk_sold"/></th>
<th width="3%"><layout:column_head name="disk_alloc"/></th>
<th width="10%"><layout:column_head name="gpu_codes"/></th>
<th width="3%"><layout:column_head name="gpu_sold"/></th>
<th width="3%"><layout:column_head name="gpu_alloc"/></th>
<th width="10%"><layout:column_head name="gpu"/></th>
<!--- <th width="3%"><layout:column_head name="disk_sold"/></th> --->
</layout:grid_head>
</thead>
<cfflush/>
<cfoutput query="qRead">
<tr>
<td>#wz_sold#</td>
<td>
<cfif len(wz_sold)>
<a href="contragent_rpt.cfm?__id=#company_uid#&#tr.fwx#">#client_sold#</a>
<cfelse>
<a href="contragent_rpt.cfm?__id=#alloc_company_uid#&#tr.fwx#">#alloc_company#</a>
</cfif>
</td>
<td>#wz_alloc#</td>
<td>#status_ids#</td>
<td>#line_pricing_model_ids#</td>
<td>#cpu_codes#</td>
<td class="r"<cfif cpu_sold GT ram_alloc> style="background:lightgreen;"</cfif>>#cpu_sold#</td>
<td class="r"<cfif cpu_sold LT cpu_alloc> style="background:lightcoral;"</cfif>>#cpu_alloc#</td>
<td class="r"<cfif cpu_sold LT cpu_alloc_on> style="background:lightcoral;"</cfif>>#cpu_alloc_on#</td>
<td>#ram_codes#</td>
<td class="r"<cfif ram_sold GT ram_alloc> style="background:lightgreen;"</cfif>>#ram_sold#</td>
<td class="r"<cfif ram_sold LT ram_alloc> style="background:lightcoral;"</cfif>>#ram_alloc#</td>
<td class="r"<cfif ram_sold LT ram_alloc_on> style="background:lightcoral;"</cfif>>#ram_alloc_on#</td>
<td>#disk_codes#</td>
<td class="r"<cfif disk_sold GT disk_alloc> style="background:lightgreen;"</cfif>>#disk_sold#</td>
<td class="r"<cfif disk_sold LT disk_alloc> style="background:lightcoral;"</cfif>>#disk_alloc#</td>
<td>#gpu_codes#</td>
<td class="r"<cfif gpu_sold GT gpu_alloc> style="background:lightgreen;"</cfif>>#gpu_sold#</td>
<td class="r"<cfif gpu_sold LT gpu_alloc> style="background:lightcoral;"</cfif>>#gpu_alloc#</td>
<td>#gpu#</td>
</tr>
</cfoutput>
<cfquery name="qTotal" dbtype="query">
select
sum(cpu_sold) as cpu_sold
,sum(cpu_alloc) as cpu_alloc
,sum(cpu_alloc_on) as cpu_alloc_on
,sum(ram_sold) as ram_sold
,sum(ram_alloc) as ram_alloc
,sum(ram_alloc_on) as ram_alloc_on
,sum(disk_sold) as disk_sold
,sum(disk_alloc) as disk_alloc
,sum(gpu_sold) as gpu_sold
from qRead
</cfquery>
<cfoutput query="qTotal">
<tr class="b r">
<td colspan="6">Итого</td>
<td class="r"<cfif cpu_sold GT ram_alloc> style="background:lightgreen;"</cfif>>#cpu_sold#</td>
<td class="r"<cfif cpu_sold LT cpu_alloc> style="background:lightcoral;"</cfif>>#cpu_alloc#</td>
<td class="r"<cfif cpu_sold LT cpu_alloc_on> style="background:lightcoral;"</cfif>>#cpu_alloc_on#</td>
<td></td>
<td class="r"<cfif ram_sold GT ram_alloc> style="background:lightgreen;"</cfif>>#ram_sold#</td>
<td class="r"<cfif ram_sold LT ram_alloc> style="background:lightcoral;"</cfif>>#ram_alloc#</td>
<td class="r"<cfif ram_sold LT ram_alloc_on> style="background:lightcoral;"</cfif>>#ram_alloc_on#</td>
<td></td>
<td class="r"<cfif disk_sold GT disk_alloc> style="background:lightgreen;"</cfif>>#disk_sold#</td>
<td class="r"<cfif disk_sold LT disk_alloc> style="background:lightcoral;"</cfif>>#disk_alloc#</td>
<td></td>
<td class="r">#gpu_sold#</td>
</tr>
</cfoutput>
</table>
<layout:page section="footer"/>