console 1

  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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
WITH elec_tariff AS (
SELECT stm.id, stm_svc.charge
FROM "Statement" stm
JOIN "StatementService" stm_svc ON stm.id = stm_svc."billId"
WHERE stm_svc."utilityType" = 'electric'
), gas_tariff AS (
SELECT stm.id, stm_svc.charge
FROM "Statement" stm
JOIN "StatementService" stm_svc ON stm.id = stm_svc."billId"
WHERE stm_svc."utilityType" = 'gas'
), statements AS (
SELECT
stm."houseContractId",
stm."tenantContractId",
date_trunc('month',stm."statementDate") AS "statementDate",
jsonb_agg(stm ORDER BY date_trunc('month', stm."statementDate"), stm.version DESC) AS "statements"
FROM "Statement" stm
WHERE stm.type='bill'
GROUP BY stm."tenantContractId", stm."houseContractId", date_trunc('month',stm."statementDate")
)
SELECT
distinct on (hc.id, stm_lvv."tenantContractId", date_trunc('month', stm."statementDate"))
row_number() OVER () as id,
hc.id as "houseContractId",
stm_lvv."tenantContractId",
et.charge as "elecTariffCharge",
gt.charge as "gasTariffCharge",
stm_cv.balance,
date_trunc('month', stm."statementDate") as "billDate",
statements,
COALESCE(ss.manual, false) AS "allowManualGeneration",
COALESCE((statements->0->>'scheduled')::bool, false) AS scheduled,
COALESCE((statements->0->>'emailed')::bool, false) AS emailed,
COALESCE((statements->0->>'paid')::bool, false) AS paid
FROM "HouseContract" hc
JOIN "StatementLastVersionView" stm_lvv ON stm_lvv."houseContractId" = hc.id
JOIN elec_tariff et ON et.id = stm_lvv.id
JOIN gas_tariff gt ON gt.id = stm_lvv.id
JOIN "StatementCalculatedView" stm_cv ON stm_cv.id = stm_lvv.id
JOIN "Statement" lstm ON lstm.id = stm_lvv.id
JOIN "Statement" stm ON stm."houseContractId" = hc.id
LEFT JOIN "StatementSettings" ss ON (ss."houseContractId" = hc.id
AND ss."statementType"='bill'
AND (ss."tenantContractId"=stm_lvv."tenantContractId" OR ss."tenantContractId" ISNULL))
JOIN statements ON (statements."houseContractId"=hc.id
AND statements."statementDate"=date_trunc('month', stm."statementDate")
AND (statements."tenantContractId"=stm_lvv."tenantContractId"))
OR (statements."houseContractId"=hc.id
AND statements."statementDate"=date_trunc('month', stm."statementDate")
AND (statements."tenantContractId" IS NULL AND stm_lvv."tenantContractId" ISNULL));
SELECT * FROM "StatementLastVersionView";
SELECT * FROM "StatementSettings";
WITH stm_agg AS (
SELECT fs."tenantContractId",
jsonb_agg(jsonb_build_object('id', fs.id, 'version', fs.version, 'url', u.url)
ORDER BY fs.version DESC) AS statements
FROM ("Statement" fs
LEFT JOIN "Uploads" u ON (((u."statementId" = fs.id) AND (u."resourceType" = 'STATEMENT'::text))))
WHERE (fs."tenantContractId" IS NOT NULL)
GROUP BY fs."tenantContractId"
)
SELECT tc.id,
tc."tenantId",
tc."houseContractId",
hc."houseId",
stm.id AS "statementId",
stm.type
FROM (((((((((("TenantContract" tc
JOIN "HouseContract" hc ON ((hc.id = tc."houseContractId")))
JOIN "House" h ON ((h.id = hc."houseId")))
JOIN "Address" a ON ((a.id = h."addressId")))
JOIN "Tenant" t ON ((t.id = tc."tenantId")))
LEFT JOIN "StatementSettings" ss ON (((ss."statementType" = 'tenant'::"StatementType") AND
(ss."tenantContractId" = tc.id))))
LEFT JOIN stm_agg ON ((stm_agg."tenantContractId" = tc.id)))
LEFT JOIN "StatementLastVersionView" stl ON ((stl."tenantContractId" = tc.id AND stl.type='tenant')))
LEFT JOIN "Statement" stm ON ((stm.id = stl.id AND stm.type='tenant')))
LEFT JOIN "StatementCalculatedView" scv ON ((scv.id = stl.id)))
LEFT JOIN "StatementTotalsView" stt ON ((stt.id = stl.id)));
WITH stm_agg AS (
SELECT fs."tenantContractId",
jsonb_agg(jsonb_build_object('id', fs.id, 'version', fs.version, 'url', u.url)
ORDER BY fs.version DESC) AS statements
FROM ("Statement" fs
LEFT JOIN "Uploads" u ON (((u."statementId" = fs.id) AND (u."resourceType" = 'STATEMENT'::text))))
WHERE (fs."tenantContractId" IS NOT NULL)
GROUP BY fs."tenantContractId"
)
SELECT tc.id,
tc."tenantId",
concat_ws(' '::text, t."firstName", t."lastName") AS name,
tc."houseContractId",
tc."endDate" AS "tenantContractEndDate",
tc."paymentComments" AS "tenantComments",
hc."houseComments",
hc."houseId",
stt."electricPlan",
stt."electricBill",
stt."electricWarnings",
stt."electricErrors",
stt."gasPlan",
stt."gasBill",
stt."gasWarnings",
stt."gasErrors",
stt."waterPlan",
stt."waterBill",
stt."waterWarnings",
stt."waterErrors",
stt."broadbandPlan",
stt."broadbandBill",
stt."broadbandWarnings",
stt."broadbandErrors",
stt."tvPlan",
stt."tvBill",
stt."tvWarnings",
stt."tvErrors",
stm.id AS "statementId",
stm.imported,
scv.balance,
scv.warnings,
scv.errors,
stm_agg.statements,
(hc."endDate" = tc."endDate") AS "equalEndDate",
h."agencyId" AS "_agencyId",
a."cityId" AS "_cityId",
tc."huddleStatus" AS "_huddleStatus",
COALESCE(ss.manual, false) AS "allowManualGeneration",
COALESCE(stm.scheduled, false) AS scheduled,
COALESCE(stm.emailed, false) AS emailed,
COALESCE(stm.paid, false) AS paid,
stm."dayToBePaid"
FROM (((((((((("TenantContract" tc
JOIN "HouseContract" hc ON ((hc.id = tc."houseContractId")))
JOIN "House" h ON ((h.id = hc."houseId")))
JOIN "Address" a ON ((a.id = h."addressId")))
JOIN "Tenant" t ON ((t.id = tc."tenantId")))
LEFT JOIN "StatementSettings" ss ON (((ss."statementType" = 'tenant'::"StatementType") AND
(ss."tenantContractId" = tc.id))))
LEFT JOIN stm_agg ON ((stm_agg."tenantContractId" = tc.id)))
LEFT JOIN "StatementLastVersionView" stl ON ((stl."tenantContractId" = tc.id)))
LEFT JOIN "Statement" stm ON ((stm.id = stl.id)))
LEFT JOIN "StatementCalculatedView" scv ON ((scv.id = stl.id)))
LEFT JOIN "StatementTotalsView" stt ON ((stt.id = stl.id)));
with ss as (SELECT
"houseContractId",
array_agg("startDate") as "startDate"
FROM "Statement"
GROUP BY "houseContractId")
select * from ss WHERE '2018-12-01T00:00:00.000Z'::date != ALL (ss."startDate")
SELECT
"houseContractId",
array_agg("startDate") as "startDate"
FROM "Statement"
GROUP BY "houseContractId"
WITH
electricTariffNames as (
SELECT hc.id, t."tariffName"
FROM "HouseContract" hc
JOIN "Service" s ON s."houseContractId" = hc.id
JOIN "Tariff" t ON s."tariffId" = t.id
WHERE s."utilityType"='electric'
),
gasTariffNames as (
SELECT hc.id, t."tariffName"
FROM "HouseContract" hc
JOIN "Service" s ON s."houseContractId" = hc.id
JOIN "Tariff" t ON s."tariffId" = t.id
WHERE s."utilityType"='gas'
),
lastStatement as (
SELECT
-- DISTINCT ON ("statementDate")
stm.*
FROM "Statement" stm
WHERE stm.type='bill'
ORDER BY "statementDate" ASC
),
statementsDates as (
SELECT
"houseContractId",
array_agg("startDate") as "startDate"
FROM "Statement"
GROUP BY "houseContractId"
)
SELECT
DISTINCT ON (hc.id)
hc.id,
hc."signUpDate",
hc."endDate",
a."formatedAddress",
etf."tariffName" as "electricTariffName",
gtf."tariffName" as "gasTariffName",
lstm.id as "statementId",
lstm."statementDate" as "lastBillDate",
stmd."startDate" as "statementsDates",
hc."houseId"
FROM "HouseContract" hc
JOIN "House" h ON h.id = hc."houseId"
JOIN "view_get_formatted_address" a ON h."addressId" = a."addressId"
JOIN "Service" s ON s."houseContractId" = hc.id
JOIN view_get_house_contract_service_info hcsi ON hcsi."houseContractId" = hc.id
JOIN "Tariff" t ON s."tariffId" = t.id
JOIN electricTariffNames etf ON etf.id = hc.id
JOIN gasTariffNames gtf ON gtf.id = hc.id
LEFT JOIN lastStatement lstm ON lstm."houseContractId"=hc.id
LEFT JOIN statementsDates stmd ON stmd."houseContractId"=hc.id
WHERE hc."billingType" = 'BILL'
AND s."utilityType" IN ('gas', 'electric')
AND hc."signUpDate" <= now() AND hc."endDate" >= now()
AND NOT hc."isPortfolio"
AND hc."status" <> 'Closed'
AND hcsi."liveEnergy";
WITH elec_tariff AS (
SELECT stm.id, stm_svc.charge
FROM "Statement" stm
JOIN "StatementService" stm_svc ON stm.id = stm_svc."billId"
WHERE stm_svc."utilityType" = 'electric'
), gas_tariff AS (
SELECT stm.id, stm_svc.charge
FROM "Statement" stm
JOIN "StatementService" stm_svc ON stm.id = stm_svc."billId"
WHERE stm_svc."utilityType" = 'gas'
), statements AS (
SELECT
stm."houseContractId",
stm."tenantContractId",
date_trunc('month',stm."statementDate") AS "statementDate",
jsonb_agg(stm ORDER BY date_trunc('month', stm."statementDate"), stm.version DESC) AS "statements"
FROM "Statement" stm
WHERE stm.type='bill'
GROUP BY stm."tenantContractId", stm."houseContractId", date_trunc('month',stm."statementDate")
)
SELECT
distinct on (hc.id, stm_lvv."tenantContractId", date_trunc('month', stm."statementDate"))
row_number() OVER () as id,
hc.id as "houseContractId",
stm_lvv."tenantContractId",
et.charge as "elecTariffCharge",
gt.charge as "gasTariffCharge",
stm_cv.balance,
date_trunc('month', stm."statementDate") as "billDate",
statements,
COALESCE(ss.manual, false) as "allowManualGeneration",
COALESCE((statements->0->>'scheduled')::bool, false) as scheduled,
COALESCE((statements->0->>'emailed')::bool, false) as emailed,
COALESCE((statements->0->>'paid')::bool, false) as paid
FROM "HouseContract" hc
JOIN "StatementLastVersionView" stm_lvv ON stm_lvv."houseContractId" = hc.id
LEFT JOIN elec_tariff et ON et.id = stm_lvv.id
LEFT JOIN gas_tariff gt ON gt.id = stm_lvv.id
JOIN "StatementCalculatedView" stm_cv ON stm_cv.id = stm_lvv.id
JOIN "Statement" lstm ON lstm.id = stm_lvv.id
JOIN "Statement" stm ON stm."houseContractId" = hc.id
LEFT JOIN "StatementSettings" ss ON (ss."houseContractId" = hc.id
AND ss."statementType"='bill'
AND (ss."tenantContractId"=stm_lvv."tenantContractId" OR ss."tenantContractId" ISNULL))
JOIN statements ON ((statements."houseContractId"=hc.id
AND statements."statementDate"=date_trunc('month', stm."statementDate")
AND (statements."tenantContractId"=stm_lvv."tenantContractId"))
OR (statements."houseContractId"=hc.id
AND statements."statementDate"=date_trunc('month', stm."statementDate")
AND (statements."tenantContractId" IS NULL AND stm_lvv."tenantContractId" ISNULL)));
WITH elec_tariff AS (
SELECT stm.id, stm_svc.charge
FROM "Statement" stm
JOIN "StatementService" stm_svc ON stm.id = stm_svc."billId"
WHERE stm_svc."utilityType" = 'electric'
), gas_tariff AS (
SELECT stm.id, stm_svc.charge
FROM "Statement" stm
JOIN "StatementService" stm_svc ON stm.id = stm_svc."billId"
WHERE stm_svc."utilityType" = 'gas'
), statements AS (
SELECT
stm."houseContractId",
stm."tenantContractId",
date_trunc('month',stm."statementDate") AS "statementDate",
jsonb_agg(jsonb_build_object('id', stm.id, 'version', stm.version, 'url', u.url)
ORDER BY date_trunc('month', stm."statementDate"), stm.version DESC) AS "statements"
FROM "Statement" stm
LEFT JOIN "Uploads" u ON (((u."statementId" = stm.id) AND (u."resourceType" = 'STATEMENT'::text)))
WHERE stm.type='bill'
GROUP BY stm."tenantContractId", stm."houseContractId", date_trunc('month',stm."statementDate")
)
SELECT
distinct on (hc.id, stm_lvv."tenantContractId", date_trunc('month', stm."statementDate"))
row_number() OVER () as id,
hc.id as "houseContractId",
stm_lvv."tenantContractId",
et.charge as "elecTariffCharge",
gt.charge as "gasTariffCharge",
stm_cv.balance,
date_trunc('month', stm."statementDate") as "billDate",
statements,
COALESCE(ss.manual, false) as "allowManualGeneration",
COALESCE((statements->0->>'scheduled')::bool, false) as scheduled,
COALESCE((statements->0->>'emailed')::bool, false) as emailed,
COALESCE((statements->0->>'paid')::bool, false) as paid,
stm_cv.warnings,
stm_cv.errors,
stt."electricPlan",
stt."electricBill",
stt."electricWarnings",
stt."electricErrors",
stt."gasPlan",
stt."gasBill",
stt."gasWarnings",
stt."gasErrors"
FROM "HouseContract" hc
JOIN "StatementLastVersionView" stm_lvv ON stm_lvv."houseContractId" = hc.id
LEFT JOIN elec_tariff et ON et.id = stm_lvv.id
LEFT JOIN gas_tariff gt ON gt.id = stm_lvv.id
JOIN "StatementCalculatedView" stm_cv ON stm_cv.id = stm_lvv.id
JOIN "Statement" lstm ON lstm.id = stm_lvv.id
JOIN "Statement" stm ON stm."houseContractId" = hc.id
LEFT JOIN "StatementSettings" ss ON (ss."houseContractId" = hc.id
AND ss."statementType"='bill'
AND (ss."tenantContractId"=stm_lvv."tenantContractId" OR ss."tenantContractId" ISNULL))
JOIN statements ON ((statements."houseContractId"=hc.id
AND statements."statementDate"=date_trunc('month', stm."statementDate")
AND (statements."tenantContractId"=stm_lvv."tenantContractId"))
OR (statements."houseContractId"=hc.id
AND statements."statementDate"=date_trunc('month', stm."statementDate")
AND (statements."tenantContractId" IS NULL AND stm_lvv."tenantContractId" ISNULL)))
LEFT JOIN "StatementTotalsView" stt ON ((stt.id = stm_lvv.id));