@@ -59,6 +59,7 @@ module default {
59
59
status := Project::statusFromStep(.step);
60
60
latestWorkflowEvent := (select .workflowEvents order by .at desc limit 1);
61
61
workflowEvents := .<project[is Project::WorkflowEvent];
62
+
62
63
trigger assertMatchingLatestWorkflowEvent after insert, update for each do (
63
64
assert(
64
65
__new__.latestWorkflowEvent.to ?= __new__.step
@@ -122,38 +123,23 @@ module default {
122
123
}
123
124
);
124
125
125
- trigger createPeriodicReports after insert for each do (
126
+ trigger createPeriodicReports after insert for each
127
+ when (
128
+ exists __new__.financialReportPeriod
129
+ and exists __new__.mouStart
130
+ and exists __new__.mouEnd
131
+ )
132
+ do (
126
133
with
127
134
interval := (select
128
135
if __new__.financialReportPeriod = default::ReportPeriod.Monthly then '1' else '3'),
129
136
reportRanges := Project::create_periodic_report_ranges(
130
137
__new__.mouStart,
131
138
__new__.mouEnd,
132
139
interval
133
- )
134
- for reportRange in reportRanges
135
- union (
136
- (insert default::FinancialReport {
137
- createdAt := datetime_of_statement(),
138
- modifiedAt := datetime_of_statement(),
139
- createdBy := assert_exists(global currentActor),
140
- modifiedBy := assert_exists(global currentActor),
141
- project := __new__,
142
- projectContext := __new__.projectContext,
143
- container := __new__,
144
- period := reportRange
145
- }),
146
- (insert default::NarrativeReport {
147
- createdAt := datetime_of_statement(),
148
- modifiedAt := datetime_of_statement(),
149
- createdBy := assert_exists(global currentActor),
150
- modifiedBy := assert_exists(global currentActor),
151
- project := __new__,
152
- projectContext := __new__.projectContext,
153
- container := __new__,
154
- period := reportRange
155
- })
156
- )
140
+ ),
141
+ insertedReports := Project::insertReports(array_agg(reportRanges), __new__)
142
+ select insertedReports
157
143
);
158
144
159
145
trigger addRemovePeriodicReports after update for each
@@ -184,86 +170,44 @@ module default {
184
170
select existingReports
185
171
filter not exists .reportFile
186
172
)
187
- select if not exists __new__.mouStart or not exists __new__.mouEnd then (
173
+ select
174
+ # start date, end date, or financial report period were deleted
175
+ if not exists __new__.mouStart
176
+ or not exists __new__.mouEnd
177
+ or not exists __new__.financialReportPeriod then (
188
178
for report in reportsForDeletion
189
179
union (
190
180
delete report
191
181
)
192
182
)
183
+ # financial report period changes, start date is moved forward, or end date is moved backward
193
184
else if __old__.financialReportPeriod ?!= __new__.financialReportPeriod
194
185
or (__new__.mouStart > __old__.mouStart) ?? false
195
186
or (__new__.mouEnd < __old__.mouEnd) ?? false then (
196
187
with
197
- requestedReportPeriodsForInsertion := (
198
- select requestedReportPeriods
199
- filter requestedReportPeriods not in existingReports.period
188
+ periodsForInsertion := Project::determineRequestedReportPeriods (
189
+ array_agg( requestedReportPeriods),
190
+ array_agg( existingReports)
200
191
),
201
- financialReports := (for reportPeriod in requestedReportPeriodsForInsertion
192
+ insertedReports := Project::insertReports(array_agg(periodsForInsertion), __new__),
193
+ deletedReports := (for report in reportsForDeletion
202
194
union (
203
- insert default::FinancialReport {
204
- createdAt := datetime_of_statement(),
205
- modifiedAt := datetime_of_statement(),
206
- createdBy := assert_exists(global currentActor),
207
- modifiedBy := assert_exists(global currentActor),
208
- project := __new__,
209
- projectContext := __new__.projectContext,
210
- container := __new__,
211
- period := reportPeriod,
212
- }
213
- )),
214
- narrativeReports := (for reportPeriod in requestedReportPeriodsForInsertion
215
- union (
216
- insert default::NarrativeReport {
217
- createdAt := datetime_of_statement(),
218
- modifiedAt := datetime_of_statement(),
219
- createdBy := assert_exists(global currentActor),
220
- modifiedBy := assert_exists(global currentActor),
221
- project := __new__,
222
- projectContext := __new__.projectContext,
223
- container := __new__,
224
- period := reportPeriod,
225
- }
195
+ delete report
196
+ filter report.period not in requestedReportPeriods
226
197
))
227
- for report in reportsForDeletion
228
- union (
229
- delete report
230
- filter report.period not in requestedReportPeriods
231
- )
232
- )
198
+ select insertedReports
199
+ )
200
+ # start or end dates otherwise change
233
201
else if newMouStart ?!= oldMouStart
234
202
or newMouEnd ?!= oldMouEnd then (
235
203
with
236
- requestedReportPeriodsForInsertion := (
237
- select requestedReportPeriods
238
- filter requestedReportPeriods not in existingReports.period
239
- ),
240
- financialReports := (for reportPeriod in requestedReportPeriodsForInsertion
241
- union (
242
- insert default::FinancialReport {
243
- createdAt := datetime_of_statement(),
244
- modifiedAt := datetime_of_statement(),
245
- createdBy := assert_exists(global currentActor),
246
- modifiedBy := assert_exists(global currentActor),
247
- project := __new__,
248
- projectContext := __new__.projectContext,
249
- container := __new__,
250
- period := reportPeriod,
251
- }
252
- )),
253
- narrativeReports := (for reportPeriod in requestedReportPeriodsForInsertion
254
- union (
255
- insert default::NarrativeReport {
256
- createdAt := datetime_of_statement(),
257
- modifiedAt := datetime_of_statement(),
258
- createdBy := assert_exists(global currentActor),
259
- modifiedBy := assert_exists(global currentActor),
260
- project := __new__,
261
- projectContext := __new__.projectContext,
262
- container := __new__,
263
- period := reportPeriod,
264
- }
265
- ))
266
- select financialReports union narrativeReports
204
+ periodsForInsertion := Project::determineRequestedReportPeriods(
205
+ array_agg(requestedReportPeriods),
206
+ array_agg(existingReports)
207
+ ),
208
+ insertedReports := Project::insertReports(array_agg(periodsForInsertion), __new__)
209
+ select insertedReports
210
+ # nothing changes
267
211
) else (
268
212
select <PeriodicReport>{}
269
213
)
@@ -367,47 +311,58 @@ module Project {
367
311
for firstDayOfMonth in reportPeriodStartDates
368
312
union (
369
313
with
370
- firstDayOfNextMonth := (select firstDayOfMonth + <cal::relative_duration>(monthInterval ++ ' month')),
371
- lastDayOfMonth := firstDayOfNextMonth - <cal::relative_duration>'1 day'
372
- select range(<cal::local_date>firstDayOfMonth, <cal::local_date>lastDayOfMonth )
373
- ) )
314
+ firstDayOfNextPeriod := (select firstDayOfMonth + <cal::relative_duration>(monthInterval ++ ' month')),
315
+ select range(<cal::local_date>firstDayOfMonth, <cal::local_date>firstDayOfNextPeriod)
316
+ )
317
+ )
374
318
select reportPeriodRanges
375
319
);
376
320
377
- # function insertReports(requestedReportPeriods: set of range<cal::local_date>,
378
- # existingReports: array<PeriodicReport>, newProject: default::Project) -> optional str
379
- # using (
380
- # with
381
- # requestedReportPeriodsForInsertion := (
382
- # select requestedReportPeriods
383
- # filter requestedReportPeriods not in existingReports.period
384
- # ),
385
- # financialReports := (for reportPeriod in requestedReportPeriodsForInsertion
386
- # union (
387
- # insert default::FinancialReport {
388
- # createdAt := datetime_of_statement(),
389
- # modifiedAt := datetime_of_statement(),
390
- # createdBy := assert_exists(global default::currentActor),
391
- # modifiedBy := assert_exists(global default::currentActor),
392
- # project := __new__,
393
- # projectContext := __new__.projectContext,
394
- # container := __new__,
395
- # period := reportPeriod,
396
- # }
397
- # )),
398
- # narrativeReports := (for reportPeriod in requestedReportPeriodsForInsertion
399
- # union (
400
- # insert default::NarrativeReport {
401
- # createdAt := datetime_of_statement(),
402
- # modifiedAt := datetime_of_statement(),
403
- # createdBy := assert_exists(global default::currentActor),
404
- # modifiedBy := assert_exists(global default::currentActor),
405
- # project := __new__,
406
- # projectContext := __new__.projectContext,
407
- # container := __new__,
408
- # period := reportPeriod,
409
- # }
410
- # ))
411
- # select financialReports union narrativeReports
412
- # )
321
+ function determine_requested_report_periods(requestedReportPeriods: array<range<cal::local_date>>,
322
+ existingReports: array<default::PeriodicReport>) -> set of range<cal::local_date>
323
+ using (
324
+ with
325
+ distinctRequestedReportPeriods := distinct array_unpack(requestedReportPeriods),
326
+ periodsForInsertion := (
327
+ select distinctRequestedReportPeriods
328
+ filter distinctRequestedReportPeriods not in (
329
+ for report in array_unpack(existingReports)
330
+ select report.period
331
+ )
332
+ )
333
+ select periodsForInsertion
334
+ );
335
+
336
+ function insert_reports(requestedReportPeriodsForInsertion: array<range<cal::local_date>>,
337
+ newProject: default::Project) -> set of default::PeriodicReport
338
+ using (
339
+ with
340
+ financialReports := (for reportPeriod in array_unpack(requestedReportPeriodsForInsertion)
341
+ union (
342
+ insert default::FinancialReport {
343
+ createdAt := datetime_of_statement(),
344
+ modifiedAt := datetime_of_statement(),
345
+ createdBy := assert_exists(global default::currentActor),
346
+ modifiedBy := assert_exists(global default::currentActor),
347
+ project := newProject,
348
+ projectContext := newProject.projectContext,
349
+ container := newProject,
350
+ period := reportPeriod,
351
+ }
352
+ )),
353
+ narrativeReports := (for reportPeriod in array_unpack(requestedReportPeriodsForInsertion)
354
+ union (
355
+ insert default::NarrativeReport {
356
+ createdAt := datetime_of_statement(),
357
+ modifiedAt := datetime_of_statement(),
358
+ createdBy := assert_exists(global default::currentActor),
359
+ modifiedBy := assert_exists(global default::currentActor),
360
+ project := newProject,
361
+ projectContext := newProject.projectContext,
362
+ container := newProject,
363
+ period := reportPeriod,
364
+ }
365
+ ))
366
+ select financialReports union narrativeReports
367
+ );
413
368
}
0 commit comments