@vrplatform/kysely 1.3.45-stage.339 → 1.3.45-stage.344
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -51,12 +51,6 @@ export async function up(db: Kysely<unknown>): Promise<void> {
|
|
|
51
51
|
end;
|
|
52
52
|
$$
|
|
53
53
|
`.execute(db);
|
|
54
|
-
await sql`
|
|
55
|
-
create trigger initialize_reservation_journal_state
|
|
56
|
-
after insert on public.reservation
|
|
57
|
-
for each row execute function accounting.initialize_reservation_journal_state()
|
|
58
|
-
`.execute(db);
|
|
59
|
-
|
|
60
54
|
await sql`
|
|
61
55
|
create table accounting.reservation_journal_invalidation (
|
|
62
56
|
id uuid primary key default gen_random_uuid(),
|
|
@@ -125,253 +119,10 @@ export async function up(db: Kysely<unknown>): Promise<void> {
|
|
|
125
119
|
invalidation_id
|
|
126
120
|
)
|
|
127
121
|
`.execute(db);
|
|
128
|
-
|
|
129
|
-
await sql`
|
|
130
|
-
insert into accounting.reservation_journal_state (
|
|
131
|
-
tenant_id,
|
|
132
|
-
reservation_id,
|
|
133
|
-
calculated_at
|
|
134
|
-
)
|
|
135
|
-
select reservation.tenant_id, reservation.id, now()
|
|
136
|
-
from public.reservation as reservation
|
|
137
|
-
where reservation.tenant_id is not null
|
|
138
|
-
on conflict (reservation_id) do nothing
|
|
139
|
-
`.execute(db);
|
|
140
|
-
|
|
141
|
-
await sql`
|
|
142
|
-
with outstanding as (
|
|
143
|
-
select
|
|
144
|
-
effect.tenant_id,
|
|
145
|
-
effect.entity_id::uuid as reservation_id,
|
|
146
|
-
effect.action_id,
|
|
147
|
-
effect.id as effect_id,
|
|
148
|
-
effect.root_entity_type,
|
|
149
|
-
effect.root_entity_id,
|
|
150
|
-
effect.created_at,
|
|
151
|
-
effect.completed_at,
|
|
152
|
-
effect.status,
|
|
153
|
-
(
|
|
154
|
-
select attempt.status
|
|
155
|
-
from audit.effect_attempt as attempt
|
|
156
|
-
where attempt.tenant_id = effect.tenant_id
|
|
157
|
-
and attempt.effect_id = effect.id
|
|
158
|
-
order by attempt.attempt_num desc
|
|
159
|
-
limit 1
|
|
160
|
-
) as latest_attempt_status,
|
|
161
|
-
row_number() over (
|
|
162
|
-
partition by effect.tenant_id, effect.entity_id
|
|
163
|
-
order by effect.created_at, effect.id
|
|
164
|
-
) as revision
|
|
165
|
-
from audit.effect as effect
|
|
166
|
-
inner join public.reservation as reservation
|
|
167
|
-
on reservation.id = effect.entity_id
|
|
168
|
-
and reservation.tenant_id = effect.tenant_id
|
|
169
|
-
where effect.effect_type = 'REFRESH_RESERVATION_JOURNAL'
|
|
170
|
-
and effect.entity_type = 'reservation'
|
|
171
|
-
)
|
|
172
|
-
insert into accounting.reservation_journal_invalidation (
|
|
173
|
-
tenant_id,
|
|
174
|
-
reservation_id,
|
|
175
|
-
revision,
|
|
176
|
-
action_id,
|
|
177
|
-
effect_id,
|
|
178
|
-
source_resource_type,
|
|
179
|
-
source_resource_id,
|
|
180
|
-
applied_at,
|
|
181
|
-
covered_by_effect_id,
|
|
182
|
-
created_at
|
|
183
|
-
)
|
|
184
|
-
select
|
|
185
|
-
outstanding.tenant_id,
|
|
186
|
-
outstanding.reservation_id,
|
|
187
|
-
outstanding.revision,
|
|
188
|
-
outstanding.action_id,
|
|
189
|
-
outstanding.effect_id,
|
|
190
|
-
case outstanding.root_entity_type
|
|
191
|
-
when 'recurringFee' then 'recurring-fee'::core.operation_resource_type
|
|
192
|
-
when 'reservation' then 'reservation'::core.operation_resource_type
|
|
193
|
-
when 'listing' then 'listing'::core.operation_resource_type
|
|
194
|
-
when 'ownerStatement' then 'statement'::core.operation_resource_type
|
|
195
|
-
else 'team'::core.operation_resource_type
|
|
196
|
-
end,
|
|
197
|
-
case
|
|
198
|
-
when outstanding.root_entity_type in (
|
|
199
|
-
'recurringFee',
|
|
200
|
-
'reservation',
|
|
201
|
-
'listing',
|
|
202
|
-
'ownerStatement'
|
|
203
|
-
) then outstanding.root_entity_id
|
|
204
|
-
else outstanding.tenant_id
|
|
205
|
-
end,
|
|
206
|
-
case
|
|
207
|
-
when outstanding.status = 'completed'
|
|
208
|
-
and outstanding.latest_attempt_status is distinct from 'skipped_locked'
|
|
209
|
-
then outstanding.completed_at
|
|
210
|
-
else null
|
|
211
|
-
end,
|
|
212
|
-
case
|
|
213
|
-
when outstanding.status = 'completed'
|
|
214
|
-
and outstanding.latest_attempt_status is distinct from 'skipped_locked'
|
|
215
|
-
then outstanding.effect_id
|
|
216
|
-
else null
|
|
217
|
-
end,
|
|
218
|
-
outstanding.created_at
|
|
219
|
-
from outstanding
|
|
220
|
-
on conflict (effect_id) do nothing
|
|
221
|
-
`.execute(db);
|
|
222
|
-
|
|
223
|
-
await sql`
|
|
224
|
-
update accounting.reservation_journal_state as state
|
|
225
|
-
set
|
|
226
|
-
desired_revision = revisions.desired_revision,
|
|
227
|
-
applied_revision = revisions.applied_revision,
|
|
228
|
-
stale_since = revisions.stale_since,
|
|
229
|
-
calculated_at = coalesce(revisions.calculated_at, state.calculated_at),
|
|
230
|
-
updated_at = now()
|
|
231
|
-
from (
|
|
232
|
-
select
|
|
233
|
-
tenant_id,
|
|
234
|
-
reservation_id,
|
|
235
|
-
max(revision) as desired_revision,
|
|
236
|
-
coalesce(
|
|
237
|
-
min(revision) filter (where applied_at is null) - 1,
|
|
238
|
-
max(revision)
|
|
239
|
-
) as applied_revision,
|
|
240
|
-
min(created_at) filter (where applied_at is null) as stale_since,
|
|
241
|
-
max(applied_at) as calculated_at
|
|
242
|
-
from accounting.reservation_journal_invalidation
|
|
243
|
-
group by tenant_id, reservation_id
|
|
244
|
-
) as revisions
|
|
245
|
-
where state.tenant_id = revisions.tenant_id
|
|
246
|
-
and state.reservation_id = revisions.reservation_id
|
|
247
|
-
`.execute(db);
|
|
248
|
-
|
|
249
122
|
await sql`
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
invalidation.tenant_id,
|
|
254
|
-
reservation.id as reservation_id,
|
|
255
|
-
listing_scope.listing_id,
|
|
256
|
-
coalesce(reservation.currency, tenant.default_currency) as currency,
|
|
257
|
-
least(
|
|
258
|
-
reservation.effective_check_in,
|
|
259
|
-
reservation.effective_check_out,
|
|
260
|
-
reservation.booked_at,
|
|
261
|
-
reservation.general_ledger_posting_at,
|
|
262
|
-
reservation.created_at,
|
|
263
|
-
line_bounds.minimum_at,
|
|
264
|
-
entry_bounds.minimum_at
|
|
265
|
-
) as minimum_at,
|
|
266
|
-
greatest(
|
|
267
|
-
reservation.effective_check_in,
|
|
268
|
-
reservation.effective_check_out,
|
|
269
|
-
reservation.booked_at,
|
|
270
|
-
reservation.general_ledger_posting_at,
|
|
271
|
-
reservation.created_at,
|
|
272
|
-
line_bounds.maximum_at,
|
|
273
|
-
entry_bounds.maximum_at
|
|
274
|
-
) as maximum_at
|
|
275
|
-
from accounting.reservation_journal_invalidation as invalidation
|
|
276
|
-
inner join public.reservation as reservation
|
|
277
|
-
on reservation.id = invalidation.reservation_id
|
|
278
|
-
and reservation.tenant_id = invalidation.tenant_id
|
|
279
|
-
inner join public.tenant as tenant
|
|
280
|
-
on tenant.id = invalidation.tenant_id
|
|
281
|
-
inner join lateral (
|
|
282
|
-
select coalesce(listing.parent_id, reservation.listing_id) as listing_id
|
|
283
|
-
from public.listing as listing
|
|
284
|
-
where listing.id = reservation.listing_id
|
|
285
|
-
union
|
|
286
|
-
select distinct coalesce(entry_listing.parent_id, entry.listing_id)
|
|
287
|
-
from accounting.journal_entry as entry
|
|
288
|
-
left join public.listing as entry_listing
|
|
289
|
-
on entry_listing.id = entry.listing_id
|
|
290
|
-
where entry.tenant_id = invalidation.tenant_id
|
|
291
|
-
and entry.reservation_id = invalidation.reservation_id
|
|
292
|
-
and entry.listing_id is not null
|
|
293
|
-
) as listing_scope on true
|
|
294
|
-
left join lateral (
|
|
295
|
-
select
|
|
296
|
-
min(line.general_ledger_posting_at) as minimum_at,
|
|
297
|
-
max(line.general_ledger_posting_at) as maximum_at
|
|
298
|
-
from public.payment_line as line
|
|
299
|
-
where line.tenant_id = invalidation.tenant_id
|
|
300
|
-
and line.reservation_id = invalidation.reservation_id
|
|
301
|
-
) as line_bounds on true
|
|
302
|
-
left join lateral (
|
|
303
|
-
select
|
|
304
|
-
min(entry.txn_at) as minimum_at,
|
|
305
|
-
max(entry.txn_at) as maximum_at
|
|
306
|
-
from accounting.journal_entry as entry
|
|
307
|
-
where entry.tenant_id = invalidation.tenant_id
|
|
308
|
-
and entry.reservation_id = invalidation.reservation_id
|
|
309
|
-
) as entry_bounds on true
|
|
310
|
-
where invalidation.applied_at is null
|
|
311
|
-
and listing_scope.listing_id is not null
|
|
312
|
-
), currencies as (
|
|
313
|
-
select
|
|
314
|
-
scope.invalidation_id,
|
|
315
|
-
scope.tenant_id,
|
|
316
|
-
scope.listing_id,
|
|
317
|
-
scope.minimum_at,
|
|
318
|
-
scope.maximum_at,
|
|
319
|
-
scope.currency
|
|
320
|
-
from reservation_scope as scope
|
|
321
|
-
where scope.currency is not null
|
|
322
|
-
union
|
|
323
|
-
select
|
|
324
|
-
scope.invalidation_id,
|
|
325
|
-
scope.tenant_id,
|
|
326
|
-
scope.listing_id,
|
|
327
|
-
scope.minimum_at,
|
|
328
|
-
scope.maximum_at,
|
|
329
|
-
entry.currency
|
|
330
|
-
from reservation_scope as scope
|
|
331
|
-
inner join accounting.journal_entry as entry
|
|
332
|
-
on entry.tenant_id = scope.tenant_id
|
|
333
|
-
and entry.reservation_id = scope.reservation_id
|
|
334
|
-
), months as (
|
|
335
|
-
select
|
|
336
|
-
currencies.*,
|
|
337
|
-
month::date as month
|
|
338
|
-
from currencies
|
|
339
|
-
cross join lateral generate_series(
|
|
340
|
-
date_trunc('month', currencies.minimum_at),
|
|
341
|
-
date_trunc('month', currencies.maximum_at),
|
|
342
|
-
interval '1 month'
|
|
343
|
-
) as month
|
|
344
|
-
where currencies.minimum_at is not null
|
|
345
|
-
and currencies.maximum_at is not null
|
|
346
|
-
)
|
|
347
|
-
insert into accounting.reservation_journal_statement_impact (
|
|
348
|
-
invalidation_id,
|
|
349
|
-
tenant_id,
|
|
350
|
-
listing_id,
|
|
351
|
-
listing_ownership_period_id,
|
|
352
|
-
month,
|
|
353
|
-
currency
|
|
354
|
-
)
|
|
355
|
-
select distinct
|
|
356
|
-
months.invalidation_id,
|
|
357
|
-
months.tenant_id,
|
|
358
|
-
months.listing_id,
|
|
359
|
-
ownership.id,
|
|
360
|
-
months.month,
|
|
361
|
-
months.currency
|
|
362
|
-
from months
|
|
363
|
-
inner join public.listing_ownership_period as ownership
|
|
364
|
-
on ownership.tenant_id = months.tenant_id
|
|
365
|
-
and ownership.listing_id = months.listing_id
|
|
366
|
-
and (
|
|
367
|
-
ownership.start_at is null
|
|
368
|
-
or ownership.start_at < months.month + interval '1 month'
|
|
369
|
-
)
|
|
370
|
-
and (
|
|
371
|
-
ownership.end_at is null
|
|
372
|
-
or ownership.end_at > months.month
|
|
373
|
-
)
|
|
374
|
-
on conflict do nothing
|
|
123
|
+
create trigger initialize_reservation_journal_state
|
|
124
|
+
after insert on public.reservation
|
|
125
|
+
for each row execute function accounting.initialize_reservation_journal_state()
|
|
375
126
|
`.execute(db);
|
|
376
127
|
}
|
|
377
128
|
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
import type { Kysely } from 'kysely';
|
|
2
|
+
import { sql } from 'kysely';
|
|
3
|
+
|
|
4
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
5
|
+
await sql`
|
|
6
|
+
insert into accounting.reservation_journal_state (
|
|
7
|
+
tenant_id,
|
|
8
|
+
reservation_id,
|
|
9
|
+
calculated_at
|
|
10
|
+
)
|
|
11
|
+
select reservation.tenant_id, reservation.id, now()
|
|
12
|
+
from public.reservation as reservation
|
|
13
|
+
where reservation.tenant_id is not null
|
|
14
|
+
on conflict (reservation_id) do nothing
|
|
15
|
+
`.execute(db);
|
|
16
|
+
|
|
17
|
+
await sql`
|
|
18
|
+
with outstanding as (
|
|
19
|
+
select
|
|
20
|
+
effect.tenant_id,
|
|
21
|
+
effect.entity_id::uuid as reservation_id,
|
|
22
|
+
effect.action_id,
|
|
23
|
+
effect.id as effect_id,
|
|
24
|
+
effect.root_entity_type,
|
|
25
|
+
effect.root_entity_id,
|
|
26
|
+
effect.created_at,
|
|
27
|
+
effect.completed_at,
|
|
28
|
+
effect.status,
|
|
29
|
+
(
|
|
30
|
+
select attempt.status
|
|
31
|
+
from audit.effect_attempt as attempt
|
|
32
|
+
where attempt.tenant_id = effect.tenant_id
|
|
33
|
+
and attempt.effect_id = effect.id
|
|
34
|
+
order by attempt.attempt_num desc
|
|
35
|
+
limit 1
|
|
36
|
+
) as latest_attempt_status,
|
|
37
|
+
row_number() over (
|
|
38
|
+
partition by effect.tenant_id, effect.entity_id
|
|
39
|
+
order by effect.created_at, effect.id
|
|
40
|
+
) as revision
|
|
41
|
+
from audit.effect as effect
|
|
42
|
+
inner join public.reservation as reservation
|
|
43
|
+
on reservation.id = effect.entity_id
|
|
44
|
+
and reservation.tenant_id = effect.tenant_id
|
|
45
|
+
where effect.effect_type = 'REFRESH_RESERVATION_JOURNAL'
|
|
46
|
+
and effect.entity_type = 'reservation'
|
|
47
|
+
)
|
|
48
|
+
insert into accounting.reservation_journal_invalidation (
|
|
49
|
+
tenant_id,
|
|
50
|
+
reservation_id,
|
|
51
|
+
revision,
|
|
52
|
+
action_id,
|
|
53
|
+
effect_id,
|
|
54
|
+
source_resource_type,
|
|
55
|
+
source_resource_id,
|
|
56
|
+
applied_at,
|
|
57
|
+
covered_by_effect_id,
|
|
58
|
+
created_at
|
|
59
|
+
)
|
|
60
|
+
select
|
|
61
|
+
outstanding.tenant_id,
|
|
62
|
+
outstanding.reservation_id,
|
|
63
|
+
outstanding.revision,
|
|
64
|
+
outstanding.action_id,
|
|
65
|
+
outstanding.effect_id,
|
|
66
|
+
case outstanding.root_entity_type
|
|
67
|
+
when 'recurringFee' then 'recurring-fee'::core.operation_resource_type
|
|
68
|
+
when 'reservation' then 'reservation'::core.operation_resource_type
|
|
69
|
+
when 'listing' then 'listing'::core.operation_resource_type
|
|
70
|
+
when 'ownerStatement' then 'statement'::core.operation_resource_type
|
|
71
|
+
else 'team'::core.operation_resource_type
|
|
72
|
+
end,
|
|
73
|
+
case
|
|
74
|
+
when outstanding.root_entity_type in (
|
|
75
|
+
'recurringFee',
|
|
76
|
+
'reservation',
|
|
77
|
+
'listing',
|
|
78
|
+
'ownerStatement'
|
|
79
|
+
) then outstanding.root_entity_id
|
|
80
|
+
else outstanding.tenant_id
|
|
81
|
+
end,
|
|
82
|
+
case
|
|
83
|
+
when outstanding.status = 'completed'
|
|
84
|
+
and outstanding.latest_attempt_status is distinct from 'skipped_locked'
|
|
85
|
+
then outstanding.completed_at
|
|
86
|
+
else null
|
|
87
|
+
end,
|
|
88
|
+
case
|
|
89
|
+
when outstanding.status = 'completed'
|
|
90
|
+
and outstanding.latest_attempt_status is distinct from 'skipped_locked'
|
|
91
|
+
then outstanding.effect_id
|
|
92
|
+
else null
|
|
93
|
+
end,
|
|
94
|
+
outstanding.created_at
|
|
95
|
+
from outstanding
|
|
96
|
+
on conflict (effect_id) do nothing
|
|
97
|
+
`.execute(db);
|
|
98
|
+
|
|
99
|
+
await sql`
|
|
100
|
+
update accounting.reservation_journal_state as state
|
|
101
|
+
set
|
|
102
|
+
desired_revision = revisions.desired_revision,
|
|
103
|
+
applied_revision = revisions.applied_revision,
|
|
104
|
+
stale_since = revisions.stale_since,
|
|
105
|
+
calculated_at = coalesce(revisions.calculated_at, state.calculated_at),
|
|
106
|
+
updated_at = now()
|
|
107
|
+
from (
|
|
108
|
+
select
|
|
109
|
+
tenant_id,
|
|
110
|
+
reservation_id,
|
|
111
|
+
max(revision) as desired_revision,
|
|
112
|
+
coalesce(
|
|
113
|
+
min(revision) filter (where applied_at is null) - 1,
|
|
114
|
+
max(revision)
|
|
115
|
+
) as applied_revision,
|
|
116
|
+
min(created_at) filter (where applied_at is null) as stale_since,
|
|
117
|
+
max(applied_at) as calculated_at
|
|
118
|
+
from accounting.reservation_journal_invalidation
|
|
119
|
+
group by tenant_id, reservation_id
|
|
120
|
+
) as revisions
|
|
121
|
+
where state.tenant_id = revisions.tenant_id
|
|
122
|
+
and state.reservation_id = revisions.reservation_id
|
|
123
|
+
`.execute(db);
|
|
124
|
+
|
|
125
|
+
await sql`
|
|
126
|
+
with reservation_scope as (
|
|
127
|
+
select
|
|
128
|
+
invalidation.id as invalidation_id,
|
|
129
|
+
invalidation.tenant_id,
|
|
130
|
+
reservation.id as reservation_id,
|
|
131
|
+
listing_scope.listing_id,
|
|
132
|
+
coalesce(reservation.currency, tenant.default_currency) as currency,
|
|
133
|
+
least(
|
|
134
|
+
reservation.effective_check_in,
|
|
135
|
+
reservation.effective_check_out,
|
|
136
|
+
reservation.booked_at,
|
|
137
|
+
reservation.general_ledger_posting_at,
|
|
138
|
+
reservation.created_at,
|
|
139
|
+
line_bounds.minimum_at,
|
|
140
|
+
entry_bounds.minimum_at
|
|
141
|
+
) as minimum_at,
|
|
142
|
+
greatest(
|
|
143
|
+
reservation.effective_check_in,
|
|
144
|
+
reservation.effective_check_out,
|
|
145
|
+
reservation.booked_at,
|
|
146
|
+
reservation.general_ledger_posting_at,
|
|
147
|
+
reservation.created_at,
|
|
148
|
+
line_bounds.maximum_at,
|
|
149
|
+
entry_bounds.maximum_at
|
|
150
|
+
) as maximum_at
|
|
151
|
+
from accounting.reservation_journal_invalidation as invalidation
|
|
152
|
+
inner join public.reservation as reservation
|
|
153
|
+
on reservation.id = invalidation.reservation_id
|
|
154
|
+
and reservation.tenant_id = invalidation.tenant_id
|
|
155
|
+
inner join public.tenant as tenant
|
|
156
|
+
on tenant.id = invalidation.tenant_id
|
|
157
|
+
inner join lateral (
|
|
158
|
+
select coalesce(listing.parent_id, reservation.listing_id) as listing_id
|
|
159
|
+
from public.listing as listing
|
|
160
|
+
where listing.id = reservation.listing_id
|
|
161
|
+
union
|
|
162
|
+
select distinct coalesce(entry_listing.parent_id, entry.listing_id)
|
|
163
|
+
from accounting.journal_entry as entry
|
|
164
|
+
left join public.listing as entry_listing
|
|
165
|
+
on entry_listing.id = entry.listing_id
|
|
166
|
+
where entry.tenant_id = invalidation.tenant_id
|
|
167
|
+
and entry.reservation_id = invalidation.reservation_id
|
|
168
|
+
and entry.listing_id is not null
|
|
169
|
+
) as listing_scope on true
|
|
170
|
+
left join lateral (
|
|
171
|
+
select
|
|
172
|
+
min(line.general_ledger_posting_at) as minimum_at,
|
|
173
|
+
max(line.general_ledger_posting_at) as maximum_at
|
|
174
|
+
from public.payment_line as line
|
|
175
|
+
where line.tenant_id = invalidation.tenant_id
|
|
176
|
+
and line.reservation_id = invalidation.reservation_id
|
|
177
|
+
) as line_bounds on true
|
|
178
|
+
left join lateral (
|
|
179
|
+
select
|
|
180
|
+
min(entry.txn_at) as minimum_at,
|
|
181
|
+
max(entry.txn_at) as maximum_at
|
|
182
|
+
from accounting.journal_entry as entry
|
|
183
|
+
where entry.tenant_id = invalidation.tenant_id
|
|
184
|
+
and entry.reservation_id = invalidation.reservation_id
|
|
185
|
+
) as entry_bounds on true
|
|
186
|
+
where invalidation.applied_at is null
|
|
187
|
+
and listing_scope.listing_id is not null
|
|
188
|
+
), currencies as (
|
|
189
|
+
select
|
|
190
|
+
scope.invalidation_id,
|
|
191
|
+
scope.tenant_id,
|
|
192
|
+
scope.listing_id,
|
|
193
|
+
scope.minimum_at,
|
|
194
|
+
scope.maximum_at,
|
|
195
|
+
scope.currency
|
|
196
|
+
from reservation_scope as scope
|
|
197
|
+
where scope.currency is not null
|
|
198
|
+
union
|
|
199
|
+
select
|
|
200
|
+
scope.invalidation_id,
|
|
201
|
+
scope.tenant_id,
|
|
202
|
+
scope.listing_id,
|
|
203
|
+
scope.minimum_at,
|
|
204
|
+
scope.maximum_at,
|
|
205
|
+
entry.currency
|
|
206
|
+
from reservation_scope as scope
|
|
207
|
+
inner join accounting.journal_entry as entry
|
|
208
|
+
on entry.tenant_id = scope.tenant_id
|
|
209
|
+
and entry.reservation_id = scope.reservation_id
|
|
210
|
+
), months as (
|
|
211
|
+
select
|
|
212
|
+
currencies.*,
|
|
213
|
+
month::date as month
|
|
214
|
+
from currencies
|
|
215
|
+
cross join lateral generate_series(
|
|
216
|
+
date_trunc('month', currencies.minimum_at),
|
|
217
|
+
date_trunc('month', currencies.maximum_at),
|
|
218
|
+
interval '1 month'
|
|
219
|
+
) as month
|
|
220
|
+
where currencies.minimum_at is not null
|
|
221
|
+
and currencies.maximum_at is not null
|
|
222
|
+
)
|
|
223
|
+
insert into accounting.reservation_journal_statement_impact (
|
|
224
|
+
invalidation_id,
|
|
225
|
+
tenant_id,
|
|
226
|
+
listing_id,
|
|
227
|
+
listing_ownership_period_id,
|
|
228
|
+
month,
|
|
229
|
+
currency
|
|
230
|
+
)
|
|
231
|
+
select distinct
|
|
232
|
+
months.invalidation_id,
|
|
233
|
+
months.tenant_id,
|
|
234
|
+
months.listing_id,
|
|
235
|
+
ownership.id,
|
|
236
|
+
months.month,
|
|
237
|
+
months.currency
|
|
238
|
+
from months
|
|
239
|
+
inner join public.listing_ownership_period as ownership
|
|
240
|
+
on ownership.tenant_id = months.tenant_id
|
|
241
|
+
and ownership.listing_id = months.listing_id
|
|
242
|
+
and (
|
|
243
|
+
ownership.start_at is null
|
|
244
|
+
or ownership.start_at < months.month + interval '1 month'
|
|
245
|
+
)
|
|
246
|
+
and (
|
|
247
|
+
ownership.end_at is null
|
|
248
|
+
or ownership.end_at > months.month
|
|
249
|
+
)
|
|
250
|
+
on conflict do nothing
|
|
251
|
+
`.execute(db);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export async function down(_db: Kysely<unknown>): Promise<void> {}
|