-
Notifications
You must be signed in to change notification settings - Fork 1
/
export-rli.sql
293 lines (274 loc) · 14.7 KB
/
export-rli.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
select
revenue_line_items.account_id AS rli_acct_id,
opportunities.id AS rli_opp_id,
CONCAT(
"https://instance.sugarondemand.com/#Opportunities/",
opportunities.id
) AS rli_opp_url,
opportunities.name AS rli_opp_name,
CONCAT(u2.first_name, " ", u2.last_name) AS rli_opp_owner,
revenue_line_items.id AS rli_id,
CONCAT(
"https://instance.sugarondemand.com/#RevenueLineItems/",
revenue_line_items.id
) AS rli_url,
revenue_line_items.product_type AS rli_type,
revenue_line_items.name AS rli_name,
product_types.name AS rli_product_type,
product_templates.name AS rli_product,
CONCAT(u1.first_name, " ", u1.last_name) AS rli_owner,
revenue_line_items.sales_stage AS rli_sales_stage,
revenue_line_items.probability AS rli_probability,
round(revenue_line_items.likely_case, 2) AS rli_likely_case,
IF (
revenue_line_items.currency_id = "-99",
"AUD",
currencies.iso4217
) AS rli_currency,
revenue_line_items.base_rate AS rli_xe_rate,
round(
revenue_line_items.likely_case / revenue_line_items.base_rate,
2
) AS rli_likely_aud,
IF(
revenue_line_items.renewal = 1,
round(
revenue_line_items.likely_case / revenue_line_items.base_rate,
2
),
round(
(
(
revenue_line_items.likely_case / revenue_line_items.base_rate
) / 100
) * revenue_line_items.probability,
2
)
) AS rli_likely_aud_weighted,
round(
(
(
revenue_line_items.likely_case / revenue_line_items.base_rate
) / revenue_line_items.service_duration_value
) * CASE WHEN revenue_line_items.service_duration_unit = "year" THEN 1 WHEN revenue_line_items.service_duration_unit = "month" THEN 12 WHEN revenue_line_items.service_duration_unit = "day" THEN 365 END,
2
) AS rli_likely_arr_aud,
IF(
revenue_line_items.renewal = 1,
round(
(
(
revenue_line_items.likely_case / revenue_line_items.base_rate
) / revenue_line_items.service_duration_value
) * CASE WHEN revenue_line_items.service_duration_unit = "year" THEN 1 WHEN revenue_line_items.service_duration_unit = "month" THEN 12 WHEN revenue_line_items.service_duration_unit = "day" THEN 365 END,
2
),
round(
(
(
(
(
revenue_line_items.likely_case / revenue_line_items.base_rate
) / revenue_line_items.service_duration_value
) * CASE WHEN revenue_line_items.service_duration_unit = "year" THEN 1 WHEN revenue_line_items.service_duration_unit = "month" THEN 12 WHEN revenue_line_items.service_duration_unit = "day" THEN 365 END
) / 100
) * revenue_line_items.probability,
2
)
) AS rli_likely_arr_aud_weighted,
CASE WHEN revenue_line_items.service = 0 THEN round(
(
revenue_line_items.likely_case / revenue_line_items.base_rate
),
2
) WHEN revenue_line_items.service = 1 THEN (
revenue_line_items.likely_case / revenue_line_items.base_rate
) / (
DATEDIFF(
revenue_line_items.service_end_date,
revenue_line_items.service_start_date
) + 1
) END AS rli_likely_drr_aud,
CASE WHEN revenue_line_items.service = 0 THEN round(
(
(
revenue_line_items.likely_case / revenue_line_items.base_rate
) / 100
) * revenue_line_items.probability,
2
) WHEN revenue_line_items.service = 1
AND revenue_line_items.renewal = 1 THEN (
revenue_line_items.likely_case / revenue_line_items.base_rate
) / (
DATEDIFF(
revenue_line_items.service_end_date,
revenue_line_items.service_start_date
) + 1
) WHEN revenue_line_items.service = 1
AND revenue_line_items.renewal = 0 THEN (
(
(
revenue_line_items.likely_case / revenue_line_items.base_rate
) / (
DATEDIFF(
revenue_line_items.service_end_date,
revenue_line_items.service_start_date
) + 1
)
) / 100
) * revenue_line_items.probability END AS rli_likely_drr_aud_weighted,
revenue_line_items.quantity AS rli_qty,
revenue_line_items.renewal AS rli_renewal,
revenue_line_items.service AS rli_service,
CASE WHEN revenue_line_items.service = 0
AND revenue_line_items_cstm.date_completion_c > revenue_line_items.date_closed THEN revenue_line_items_cstm.date_completion_c WHEN revenue_line_items.service = 0 THEN revenue_line_items.date_closed WHEN revenue_line_items.service = 1 THEN revenue_line_items.service_start_date END AS rli_service_start_date,
CASE WHEN revenue_line_items.service = 0
AND revenue_line_items_cstm.date_completion_c > revenue_line_items.date_closed THEN revenue_line_items_cstm.date_completion_c WHEN revenue_line_items.service = 0 THEN revenue_line_items.date_closed WHEN revenue_line_items.service = 1 THEN revenue_line_items.service_end_date END AS rli_service_end_date,
revenue_line_items.service_duration_value AS rli_duration_value,
revenue_line_items.service_duration_unit AS rli_duration_unit,
CASE
WHEN revenue_line_items.service = 1 AND revenue_line_items.renewal = 1 AND revenue_line_items.sales_stage NOT LIKE "Closed%" THEN "Pipeline Renewal"
WHEN revenue_line_items.service = 1 AND revenue_line_items.renewal = 0 AND revenue_line_items.product_type = "New Business" AND revenue_line_items.sales_stage NOT LIKE "Closed%" THEN "Pipeline New"
WHEN revenue_line_items.service = 1 AND revenue_line_items.renewal = 0 AND revenue_line_items.product_type = "Existing Business" AND revenue_line_items.sales_stage NOT LIKE "Closed%" THEN "Pipeline Existing"
WHEN product_templates.name = "Perpetual licence" AND revenue_line_items.sales_stage NOT LIKE "Closed%" THEN "Pipeline Perpetual"
WHEN revenue_line_items.service = 1 AND revenue_line_items.renewal = 1 AND revenue_line_items.sales_stage = "Closed Lost" THEN "Lost Renewal"
WHEN revenue_line_items.service = 1 AND revenue_line_items.renewal = 0 AND revenue_line_items.product_type = "New Business" AND revenue_line_items.sales_stage = "Closed Lost" THEN "Lost New"
WHEN revenue_line_items.service = 1 AND revenue_line_items.renewal = 0 AND revenue_line_items.product_type = "Existing Business" AND revenue_line_items.sales_stage = "Closed Lost" THEN "Lost Existing"
WHEN product_templates.name = "Perpetual licence" AND revenue_line_items.sales_stage = "Closed Lost" THEN "Lost Perpetual"
WHEN revenue_line_items.service = 1 AND revenue_line_items.renewal = 1 AND revenue_line_items.probability = 100 AND revenue_line_items.date_closed <= revenue_line_items.service_start_date THEN "Won Renewal"
WHEN revenue_line_items.service = 1 AND revenue_line_items.renewal = 1 AND revenue_line_items.probability = 100 AND revenue_line_items.date_closed > revenue_line_items.service_start_date THEN "Won Renewal (Late)"
WHEN revenue_line_items.service = 1 AND revenue_line_items.renewal = 0 AND revenue_line_items.product_type = "New Business" AND revenue_line_items.probability = 100 THEN "Won New"
WHEN revenue_line_items.service = 1 AND revenue_line_items.renewal = 0 AND revenue_line_items.product_type = "Existing Business" AND revenue_line_items.probability = 100 THEN "Won Existing"
WHEN product_templates.name = "Perpetual licence" AND revenue_line_items.probability = 100 THEN "Won Perpetual"
ELSE ""
END AS rli_arr_status,
CASE WHEN CURDATE() >= revenue_line_items.service_start_date
AND CURDATE() <= revenue_line_items.service_end_date THEN "Active" WHEN CURDATE() > revenue_line_items.service_end_date THEN "Past" WHEN CURDATE() < revenue_line_items.service_start_date THEN "Future" ELSE "" END AS rli_arr_active_status,
revenue_line_items.date_closed AS rli_date_closed,
revenue_line_items_cstm.date_completion_c AS rli_date_completion,
revenue_line_items_cstm.date_invoice_c AS rli_date_invoiced,
revenue_line_items_cstm.invoice_number_c AS rli_invoice_num,
revenue_line_items_cstm.invoice_url_c AS rli_invoice_url,
revenue_line_items_cstm.invoiceid_c AS rli_invoice_id,
revenue_line_items_cstm.po_number_c AS rli_po_num,
CASE
WHEN revenue_line_items.date_closed < "2022-07-01" AND product_templates.name = "Professional Services" THEN "opening balance"
WHEN revenue_line_items.date_closed < "2022-07-01" AND product_templates.name = "Engineering Services" THEN "opening balance"
WHEN revenue_line_items.date_closed < "2022-07-01" AND product_templates.name = "Engineering Services" THEN "opening balance"
WHEN revenue_line_items.date_closed < "2022-07-01" AND product_templates.name = "Professional Services" THEN "opening balance"
WHEN revenue_line_items.date_closed >= "2022-07-01" AND product_templates.name = "Professional Services" AND revenue_line_items.product_type = "New Business" THEN "Account Management"
WHEN revenue_line_items.date_closed >= "2022-07-01" AND product_templates.name = "Professional Services" AND revenue_line_items.product_type = "Existing Business" THEN "Account Management"
WHEN revenue_line_items.date_closed >= "2022-07-01" AND product_templates.name = "Engineering Services" AND revenue_line_items.product_type = "New Business" THEN "Account Management"
WHEN revenue_line_items.date_closed >= "2022-07-01" AND product_templates.name = "Engineering Services" AND revenue_line_items.product_type = "Existing Business" THEN "Account Management"
WHEN revenue_line_items.date_closed >= "2022-07-01" AND product_templates.name = "Professional Services" AND revenue_line_items.product_type = "Existing Business" THEN "Account Management"
WHEN revenue_line_items.date_closed >= "2022-07-01" AND product_templates.name = "Professional Services" AND revenue_line_items.product_type = "New Business" THEN "New Business"
WHEN revenue_line_items.date_closed >= "2022-07-01" AND product_templates.name = "Engineering Services" AND revenue_line_items.product_type = "Existing Business" THEN "Account Management"
WHEN revenue_line_items.date_closed >= "2022-07-01" AND product_templates.name = "Engineering Services" AND revenue_line_items.product_type = "New Business" THEN "New Business"
WHEN product_templates.name = "Tier 2 Mobilisation" AND revenue_line_items.product_type = "New Business" THEN "New Business"
WHEN product_templates.name = "Tier 2 Mobilisation" AND revenue_line_items.product_type = "Existing Business" THEN "Account Management"
ELSE ""
END AS rli_ps_category,
round (revenue_line_items_cstm.previousrenewal_c,2) AS rli_previous_renewal,
round(revenue_line_items_cstm.previousrenewal_c / revenue_line_items.base_rate,2) AS rli_previous_renewal_aud,
round( revenue_line_items_cstm.uplift_c,2) AS rli_uplift,
round ( revenue_line_items_cstm.uplift_c / revenue_line_items.base_rate, 2) AS rli_uplift_aud,
CASE
WHEN revenue_line_items.service = 0 THEN round (((revenue_line_items_cstm.uplift_c / revenue_line_items.base_rate)/100) * revenue_line_items.probability, 2)
WHEN revenue_line_items.service = 1 AND revenue_line_items.renewal = 0 THEN round (((revenue_line_items_cstm.uplift_c / revenue_line_items.base_rate)/100) * revenue_line_items.probability, 2)
WHEN revenue_line_items.service = 1 AND revenue_line_items.renewal = 1 THEN round ((revenue_line_items_cstm.uplift_c / revenue_line_items.base_rate),2)
END AS rli_uplift_aud_weighted,
round(
(
(
revenue_line_items_cstm.uplift_c / revenue_line_items.base_rate
) / revenue_line_items.service_duration_value
) * CASE WHEN revenue_line_items.service_duration_unit = "year" THEN 1 WHEN revenue_line_items.service_duration_unit = "month" THEN 12 WHEN revenue_line_items.service_duration_unit = "day" THEN 365 END,
2
) AS rli_uplift_arr_aud,
IF(
revenue_line_items.renewal = 1,
round(
(
(
revenue_line_items_cstm.uplift_c / revenue_line_items.base_rate
) / revenue_line_items.service_duration_value
) * CASE WHEN revenue_line_items.service_duration_unit = "year" THEN 1 WHEN revenue_line_items.service_duration_unit = "month" THEN 12 WHEN revenue_line_items.service_duration_unit = "day" THEN 365 END,
2
),
round(
(
(
(
(
revenue_line_items_cstm.uplift_c / revenue_line_items.base_rate
) / revenue_line_items.service_duration_value
) * CASE WHEN revenue_line_items.service_duration_unit = "year" THEN 1 WHEN revenue_line_items.service_duration_unit = "month" THEN 12 WHEN revenue_line_items.service_duration_unit = "day" THEN 365 END
) / 100
) * revenue_line_items.probability,
2
)
) AS rli_uplift_arr_aud_weighted,
CASE WHEN revenue_line_items.service = 0 THEN round(
(
revenue_line_items_cstm.uplift_c / revenue_line_items.base_rate
),
2
) WHEN revenue_line_items.service = 1 THEN (
revenue_line_items_cstm.uplift_c / revenue_line_items.base_rate
) / (
DATEDIFF(
revenue_line_items.service_end_date,
revenue_line_items.service_start_date
) + 1
) END AS rli_uplift_drr_aud,
CASE WHEN revenue_line_items.service = 0 THEN round(
(
(
revenue_line_items_cstm.uplift_c / revenue_line_items.base_rate
) / 100
) * revenue_line_items.probability,
2
) WHEN revenue_line_items.service = 1
AND revenue_line_items.renewal = 1 THEN (
revenue_line_items_cstm.uplift_c / revenue_line_items.base_rate
) / (
DATEDIFF(
revenue_line_items.service_end_date,
revenue_line_items.service_start_date
) + 1
) WHEN revenue_line_items.service = 1
AND revenue_line_items.renewal = 0 THEN (
(
(
revenue_line_items_cstm.uplift_c / revenue_line_items.base_rate
) / (
DATEDIFF(
revenue_line_items.service_end_date,
revenue_line_items.service_start_date
) + 1
)
) / 100
) * revenue_line_items.probability END AS rli_uplift_drr_aud_weighted,
CASE
WHEN revenue_line_items.sales_stage = "Closed Complete" OR revenue_line_items.sales_stage = "Closed Lost" THEN "Actual"
WHEN revenue_line_items.sales_stage = "Closed Won" THEN "Contracted"
ELSE "Pipe"
END AS rli_sales_status,
revenue_line_items.description
from
revenue_line_items
left join product_templates ON revenue_line_items.product_template_id = product_templates.id
left join product_types ON product_templates.type_id = product_types.id
left join users u1 ON revenue_line_items.assigned_user_id = u1.id
left join opportunities ON revenue_line_items.opportunity_id = opportunities.id
left join users u2 ON opportunities.assigned_user_id = u2.id
left join revenue_line_items_cstm ON revenue_line_items_cstm.id_c = revenue_line_items.id
left join accounts ON revenue_line_items.account_id = accounts.id
left join users u3 ON accounts.assigned_user_id = u3.id
left join currencies ON revenue_line_items.currency_id = currencies.id
where
revenue_line_items.deleted = 0
and (
revenue_line_items.date_closed >= "2021-07-01"
OR revenue_line_items.service_end_date >= "2021-07-01"
)