@@ -132,22 +132,14 @@ module default {
132
132
);
133
133
134
134
trigger createPeriodicReports after insert for each
135
- when (
136
- exists __new__.financialReportPeriod
137
- and exists __new__.mouStart
138
- and exists __new__.mouEnd
139
- )
140
- do (
135
+ when (exists __new__.mouStart and exists __new__.mouEnd)
136
+ do (
141
137
with
142
- interval := (select
143
- if __new__.financialReportPeriod = default::ReportPeriod.Monthly then '1' else '3'),
144
- reportRanges := Project::create_periodic_report_ranges(
145
- __new__.mouStart,
146
- __new__.mouEnd,
147
- interval
148
- ),
149
- insertedReports := Project::insert_reports(array_agg(reportRanges), __new__)
150
- select insertedReports
138
+ insertedFinancialReports := Project::insert_financial_reports(__new__,
139
+ <array<default::FinancialReport>>{}),
140
+ insertedNarrativeAndProgressReports := Project::insert_narrative_and_progress_reports(__new__,
141
+ <array<default::PeriodicReport>>{}),
142
+ select insertedFinancialReports union insertedNarrativeAndProgressReports
151
143
);
152
144
153
145
trigger addRemovePeriodicReports after update for each
@@ -162,59 +154,93 @@ module default {
162
154
oldMouStart := __old__.mouStart,
163
155
newMouEnd := __new__.mouEnd,
164
156
oldMouEnd := __old__.mouEnd,
165
- existingReports := (
166
- select FinancialReport union NarrativeReport
167
- filter .container.id = __old__.id
157
+ monthlyReportRanges := (
158
+ select
159
+ if not exists __new__.mouStart or not exists __new__.mouEnd then (
160
+ select <range<cal::local_date>>{}
161
+ ) else (
162
+ Project::create_periodic_report_ranges(
163
+ assert_exists(__new__.mouStart),
164
+ assert_exists(__new__.mouEnd),
165
+ '1'
166
+ )
167
+ )
168
+ ),
169
+ quarterlyReportRanges := (
170
+ select
171
+ if not exists __new__.mouStart or not exists __new__.mouEnd then (
172
+ select <range<cal::local_date>>{}
173
+ ) else (
174
+ Project::create_periodic_report_ranges(
175
+ assert_exists(__new__.mouStart),
176
+ assert_exists(__new__.mouEnd),
177
+ '3'
178
+ )
179
+ )
168
180
),
169
- interval := (
170
- select (if __new__.financialReportPeriod = default::ReportPeriod.Monthly then '1' else '3')
181
+ financialReports := (
182
+ select default::FinancialReport
183
+ filter .container.id = __old__.id
171
184
),
172
- requestedReportPeriods := Project::create_periodic_report_ranges(
173
- __new__.mouStart,
174
- __new__.mouEnd,
175
- interval
185
+ allReports := (
186
+ select PeriodicReport
187
+ filter .container.id = __old__.id
176
188
),
177
- reportsForDeletion := (
178
- select existingReports
189
+ narrativeAndProgressReports := (
190
+ select PeriodicReport
191
+ filter .container.id = __old__.id
192
+ and .id not in (
193
+ for report in financialReports
194
+ select report.id
195
+ )
196
+ ),
197
+ financialReportsForDeletion := (
198
+ select financialReports
179
199
filter not exists .reportFile
180
- )
200
+ ),
201
+ allReportsForDeletion := (
202
+ select allReports
203
+ filter not exists .reportFile
204
+ )
205
+ # debug := default::log_it("In addRemovePeriodicReports - monthlyReportRanges", <json>array_agg(monthlyReportRanges)),
206
+ # debugTwo := default::log_it("In addRemovePeriodicReports - quarterlyReportRanges", <json>array_agg(quarterlyReportRanges)),
207
+ # debugThree := default::log_it("In addRemovePeriodicReports - unioned", <json>array_agg(monthlyReportRanges union quarterlyReportRanges))
181
208
select
182
- # start date, end date, or financial report period were deleted
183
- if not exists __new__.mouStart
184
- or not exists __new__.mouEnd
185
- or not exists __new__.financialReportPeriod then (
186
- for report in reportsForDeletion
209
+ # start or end date was deleted - delete all reports
210
+ if not exists __new__.mouStart or not exists __new__.mouEnd then (
211
+ for report in allReportsForDeletion
187
212
union (
188
- delete report
213
+ delete report
189
214
)
190
- )
191
- # financial report period changes, start date is moved forward, or end date is moved backward
192
- else if __old__.financialReportPeriod ?!= __new__.financialReportPeriod
193
- or (__new__.mouStart > __old__.mouStart) ?? false
194
- or (__new__.mouEnd < __old__.mouEnd) ?? false then (
195
- with
196
- periodsForInsertion := Project::determine_requested_report_periods(
197
- array_agg(requestedReportPeriods),
198
- array_agg(existingReports)
199
- ),
200
- insertedReports := Project::insert_reports(array_agg(periodsForInsertion), __new__),
201
- deletedReports := (for report in reportsForDeletion
215
+ # financial report period was deleted - delete financial reports
216
+ ) else if not exists __new__.financialReportPeriod then (
217
+ for report in financialReportsForDeletion
218
+ union (
219
+ delete report
220
+ )
221
+ # start date is moved forward or end date is moved backward (contraction) - delete all
222
+ # reports that are not in the new range
223
+ ) else if (__new__.mouStart > __old__.mouStart) ?? false or (__new__.mouEnd < __old__.mouEnd) ?? false then (
224
+ for report in allReportsForDeletion
202
225
union (
203
226
delete report
204
- filter report.period not in requestedReportPeriods
205
- ))
206
- select insertedReports
207
- )
208
- # start or end dates otherwise change
209
- else if newMouStart ?!= oldMouStart
210
- or newMouEnd ?!= oldMouEnd then (
227
+ filter report.period not in (monthlyReportRanges union quarterlyReportRanges)
228
+ )
229
+ # financial report period changes - delete all financial reports and insert new ones
230
+ ) else if __old__.financialReportPeriod ?!= __new__.financialReportPeriod then (
231
+ with
232
+ deletedFinancialReports := (
233
+ for report in financialReportsForDeletion
234
+ union (
235
+ delete report
236
+ )),
237
+ insertedFinancialReports := Project::insert_financial_reports(__new__, array_agg(financialReports))
238
+ select insertedFinancialReports
239
+ # start or end dates otherwise change (expansion) - insert all new reports
240
+ ) else if newMouStart ?!= oldMouStart or newMouEnd ?!= oldMouEnd then (
211
241
with
212
- periodsForInsertion := Project::determine_requested_report_periods(
213
- array_agg(requestedReportPeriods),
214
- array_agg(existingReports)
215
- ),
216
- insertedReports := Project::insert_reports(array_agg(periodsForInsertion), __new__)
217
- select insertedReports
242
+ insertedFinancialReports := Project::insert_financial_reports(__new__, array_agg(financialReports))
243
+ select Project::insert_narrative_and_progress_reports(__new__, array_agg(narrativeAndProgressReports))
218
244
# nothing changes
219
245
) else (
220
246
select <PeriodicReport>{}
@@ -308,11 +334,13 @@ module Project {
308
334
};
309
335
}
310
336
311
- # creates the ranges for the given start and end dates based upon the given month interval
337
+ # creates the ranges for the given start and end dates based upon the given month interval,
338
+ # appending one additional range bound by matching end dates
312
339
function create_periodic_report_ranges(startDate: cal::local_date, endDate: cal::local_date,
313
340
monthInterval: str) -> set of range<cal::local_date>
314
341
using (
315
342
with
343
+ # debug := default::log_it("In create_periodic_report_ranges - input", <json>array_agg({<str>startDate, <str>endDate, monthInterval})),
316
344
reportingPeriod := range(<cal::local_date>startDate, <cal::local_date>endDate),
317
345
reportPeriodStartDates := range_unpack(reportingPeriod, <cal::date_duration>(monthInterval ++ ' month')),
318
346
reportPeriodRanges := (
@@ -326,54 +354,139 @@ module Project {
326
354
additionalReportPeriodRange := (
327
355
select range(<cal::local_date>endDate, <cal::local_date>endDate, inc_upper := true)
328
356
)
357
+ # debugTwo := default::log_it("In create_periodic_report_ranges - reportPeriodRanges", <json>array_agg(reportPeriodRanges)),
358
+ # debugThree := default::log_it("In create_periodic_report_ranges - additionalReportPeriodRange", <json>array_agg(additionalReportPeriodRange))
329
359
select reportPeriodRanges union additionalReportPeriodRange
330
360
);
331
361
332
- function determine_requested_report_periods(requestedReportPeriods: array<range<cal::local_date>>,
333
- existingReports: array<default::PeriodicReport>) -> set of range<cal::local_date>
362
+ function determine_requested_report_periods(monthInterval: str, newProject: default::Project,
363
+ existingReports: optional array<default::PeriodicReport>) -> set of range<cal::local_date> {
364
+ volatility := 'Modifying';
334
365
using (
335
- with
336
- distinctRequestedReportPeriods := distinct array_unpack(requestedReportPeriods),
337
- periodsForInsertion := (
366
+ select
367
+ if not exists newProject.mouStart or not exists newProject.mouEnd or not exists monthInterval then (
368
+ select <range<cal::local_date>>{}
369
+ ) else (
370
+ with
371
+ requestedReportPeriods := Project::create_periodic_report_ranges(
372
+ assert_exists(newProject.mouStart), assert_exists(newProject.mouEnd), monthInterval
373
+ ),
374
+ distinctRequestedReportPeriods := distinct requestedReportPeriods,
375
+ # debug := default::log_it("In determine_requested_report_periods - interval", <json>monthInterval),
376
+ # debugTwo := default::log_it("In determine_requested_report_periods - existingReports", <json>existingReports),
377
+ # debugThree := default::log_it("In determine_requested_report_periods - distinctRequestedReportPeriods", <json>array_agg(distinctRequestedReportPeriods))
378
+ select
379
+ if exists existingReports then (
380
+ with
381
+ something := (
382
+ select distinctRequestedReportPeriods
383
+ # filter out report periods that already exist in current reports
384
+ filter distinctRequestedReportPeriods not in (
385
+ for report in array_unpack(existingReports)
386
+ select report.period
387
+ )
388
+ ),
389
+ # debug := default::log_it("In determine_requested_report_periods - something", <json>array_agg(something)),
390
+ select (something)
391
+ ) else (
338
392
select distinctRequestedReportPeriods
339
- filter distinctRequestedReportPeriods not in (
340
- for report in array_unpack(existingReports)
341
- select report.period
342
- )
343
393
)
344
- select periodsForInsertion
345
- );
394
+ )
395
+ );
396
+ }
346
397
347
- function insert_reports(requestedReportPeriodsForInsertion: array<range<cal::local_date>> ,
348
- newProject: default::Project ) -> set of default::PeriodicReport
398
+ function insert_narrative_and_progress_reports(newProject: default::Project ,
399
+ narrativeAndProgressReports: optional array< default::PeriodicReport> ) -> set of default::PeriodicReport
349
400
using (
350
401
with
351
- financialReports := (for reportPeriod in array_unpack(requestedReportPeriodsForInsertion)
352
- union (
353
- insert default::FinancialReport {
354
- createdAt := datetime_of_statement(),
355
- modifiedAt := datetime_of_statement(),
356
- createdBy := assert_exists(global default::currentActor ),
357
- modifiedBy := assert_exists(global default::currentActor),
358
- project := newProject,
359
- projectContext := newProject.projectContext,
360
- container := newProject,
361
- period := reportPeriod ,
362
- }
363
- ) ),
364
- narrativeReports := (for reportPeriod in array_unpack(requestedReportPeriodsForInsertion)
365
- union (
366
- insert default::NarrativeReport {
367
- createdAt := datetime_of_statement() ,
368
- modifiedAt := datetime_of_statement() ,
369
- createdBy := assert_exists(global default::currentActor),
370
- modifiedBy := assert_exists(global default::currentActor),
371
- project := newProject ,
372
- projectContext := newProject.projectContext,
373
- container := newProject,
374
- period := reportPeriod,
402
+ # debug := default::log_it("In insert_narrative_and_progress_reports", <json>{}),
403
+ periodsForInsertion := Project::determine_requested_report_periods (
404
+ '3',
405
+ newProject,
406
+ narrativeAndProgressReports
407
+ ),
408
+ narrativeReports := (
409
+ for reportPeriod in periodsForInsertion
410
+ union (
411
+ insert default::NarrativeReport {
412
+ createdAt := datetime_of_statement() ,
413
+ modifiedAt := datetime_of_statement(),
414
+ createdBy := assert_exists(global default::currentActor ),
415
+ modifiedBy := assert_exists(global default::currentActor),
416
+ project := newProject,
417
+ projectContext := newProject.projectContext,
418
+ container := newProject ,
419
+ period := reportPeriod ,
420
+ }
421
+ )
422
+ ) ,
423
+ projectWithEngagements := (
424
+ select newProject {
425
+ engagements := .<project[is default::LanguageEngagement]
375
426
}
376
- ))
377
- select financialReports union narrativeReports
427
+ ),
428
+ progressReports := (
429
+ for reportPeriod in periodsForInsertion
430
+ union (
431
+ for engagement in projectWithEngagements.engagements
432
+ union (
433
+ insert default::ProgressReport {
434
+ createdAt := datetime_of_statement(),
435
+ modifiedAt := datetime_of_statement(),
436
+ createdBy := assert_exists(global default::currentActor),
437
+ modifiedBy := assert_exists(global default::currentActor),
438
+ project := newProject,
439
+ projectContext := newProject.projectContext,
440
+ engagement := engagement,
441
+ container := engagement,
442
+ period := reportPeriod,
443
+ }
444
+ )
445
+ )
446
+ )
447
+ select narrativeReports
448
+ );
449
+
450
+ function insert_financial_reports(newProject: default::Project,
451
+ financialReports: optional array<default::FinancialReport>) -> set of default::FinancialReport
452
+ using (
453
+ select
454
+ if exists newProject.financialReportPeriod then (
455
+ with
456
+ periodsForInsertion := (
457
+ select
458
+ if newProject.financialReportPeriod ?= default::ReportPeriod.Monthly then (
459
+ select Project::determine_requested_report_periods(
460
+ '1',
461
+ newProject,
462
+ financialReports
463
+ )
464
+ ) else (
465
+ select Project::determine_requested_report_periods(
466
+ '3',
467
+ newProject,
468
+ financialReports
469
+ )
470
+ )
471
+ )
472
+ # debug := default::log_it("In insert_financial_reports - periodsForInsertion", <json>array_agg(periodsForInsertion)),
473
+ select (
474
+ for reportPeriod in periodsForInsertion
475
+ union (
476
+ insert default::FinancialReport {
477
+ createdAt := datetime_of_statement(),
478
+ modifiedAt := datetime_of_statement(),
479
+ createdBy := assert_exists(global default::currentActor),
480
+ modifiedBy := assert_exists(global default::currentActor),
481
+ project := newProject,
482
+ projectContext := newProject.projectContext,
483
+ container := newProject,
484
+ period := reportPeriod,
485
+ }
486
+ )
487
+ )
488
+ ) else (
489
+ select <default::FinancialReport>{}
490
+ )
378
491
);
379
492
}
0 commit comments