-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtableVirtualRegionPartner.sql
86 lines (85 loc) · 3.53 KB
/
tableVirtualRegionPartner.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
SELECT
rp.display_name AS name,
region.name AS region,
rp.id AS partner_id,
rp.administrative_dependency AS adm_dep,
crl.season AS season,
COALESCE(
CASE
WHEN ru.login IS NULL OR ru.login = '' OR position('.' IN ru.login) = 0 OR position('@' IN ru.login) = 0 THEN '(no asignado)'
ELSE CONCAT(
substring(ru.login FROM 1 FOR position('.' IN ru.login) - 1),
' ',
substring(ru.login FROM position('.' IN ru.login) + 1 FOR position('@' IN ru.login) - position('.' IN ru.login) - 1)
)
END,
'(no asignado)'
) AS name_saler,
MAX(CASE
WHEN EXISTS (
SELECT 1
FROM lead_cali_adoption_info lca
LEFT JOIN product_template pt ON lca.cali_product_id = pt.id
LEFT JOIN product_category pci ON pt.categ_id = pci.id
WHERE crl.id = lca.lead_id
AND pt.id IS NOT NULL
AND pci.name IN (
SELECT pc.name
FROM product_category pc
JOIN product_template pt ON pt.categ_id = pc.id
WHERE 1=1
{% for filter in get_filters('category', remove_filter=True) %}
{% if filter.get('op') == 'IN' %}
AND pc.name IN ({{ "'" + "','".join(filter.get('val'))|replace("'", "''") + "'" }})
{% elif filter.get('op') == 'LIKE' %}
AND pc.name LIKE {{ "'" + filter.get('val')|replace("'", "''") + "'" }}
{% endif %}
{% endfor %}
)
) THEN 1 ELSE 0
END) AS tiene_adoption,
MAX(CASE
WHEN EXISTS (
SELECT 1
FROM lead_cali_adoption_info lca
LEFT JOIN product_template pt ON lca.cali_product_id = pt.id
LEFT JOIN product_category pci ON pt.categ_id = pci.id
WHERE crl.id = lca.lead_id
AND pt.id IS NOT NULL
AND pci.name IN (
SELECT pc.name
FROM product_category pc
JOIN product_template pt ON pt.categ_id = pc.id
WHERE 1=1
{% for filter in get_filters('category', remove_filter=True) %}
{% if filter.get('op') == 'IN' %}
AND pc.name IN ({{ "'" + "','".join(filter.get('val'))|replace("'", "''") + "'" }})
{% elif filter.get('op') == 'LIKE' %}
AND pc.name LIKE {{ "'" + filter.get('val')|replace("'", "''") + "'" }}
{% endif %}
{% endfor %}
)
) OR EXISTS (
SELECT 1
FROM lead_competitor_adoption_info lcai
WHERE crl.id = lcai.lead_id
AND lcai.lead_id IS NOT NULL
) THEN 1 ELSE 0
END) AS tiene_info
FROM
res_partner rp
JOIN
crm_lead crl ON rp.id = crl.partner_id
LEFT JOIN
res_country_state comuna ON rp.state_id = comuna.id
LEFT JOIN
res_country_state ciudad ON comuna.parent_id = ciudad.id
LEFT JOIN
res_country_state region ON ciudad.parent_id = region.id
RIGHT JOIN
res_users ru ON rp.user_id = ru.id
WHERE
crl.season = CAST((SELECT crm_user_authorized_season FROM res_company) AS INT)
OR crl.season = CAST((SELECT crm_user_authorized_season - 1 FROM res_company) AS INT)
GROUP BY
rp.display_name, region.name, rp.id, crl.season, ru.login