@@ -135,7 +135,7 @@ module default {
135
135
when (exists __new__.mouStart and exists __new__.mouEnd)
136
136
do (
137
137
with
138
- insertedFinancialReports := Project::insert_financial_reports (__new__,
138
+ insertedFinancialReports := Project::create_financial_reports (__new__,
139
139
<array<default::FinancialReport>>{}),
140
140
insertedNarrativeAndProgressReports := Project::insert_narrative_and_progress_reports(__new__,
141
141
<array<default::PeriodicReport>>{}),
@@ -202,9 +202,6 @@ module default {
202
202
select allReports
203
203
filter not exists .reportFile
204
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))
208
205
select
209
206
# start or end date was deleted - delete all reports
210
207
if not exists __new__.mouStart or not exists __new__.mouEnd then (
@@ -218,14 +215,27 @@ module default {
218
215
union (
219
216
delete report
220
217
)
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
225
- union (
226
- delete report
227
- filter report.period not in (monthlyReportRanges union quarterlyReportRanges)
228
- )
218
+ # start date is moved forward (contraction) - delete all reports that are not in the new range
219
+ ) else if (__new__.mouStart > __old__.mouStart) ?? false then (
220
+ for report in allReportsForDeletion
221
+ union (
222
+ delete report
223
+ filter report.period not in (monthlyReportRanges union quarterlyReportRanges)
224
+ )
225
+ # end date is moved backward (contraction) - delete all reports that are not in the new range
226
+ # and add an additional report period range
227
+ ) else if (__new__.mouEnd < __old__.mouEnd) ?? false then (
228
+ with
229
+ deletedReports := (
230
+ for report in allReportsForDeletion
231
+ union (
232
+ delete report
233
+ filter report.period not in (monthlyReportRanges union quarterlyReportRanges)
234
+ )),
235
+ additionalReportPeriodRange := (select range(<cal::local_date>newMouEnd, <cal::local_date>newMouEnd, inc_upper := true)),
236
+ financialReports := Project::insert_financial_reports(__new__, array_agg(additionalReportPeriodRange)),
237
+ narrativeReports := Project::insert_narrative_reports(__new__, array_agg(additionalReportPeriodRange))
238
+ select Project::insert_progress_reports(__new__, array_agg(additionalReportPeriodRange))
229
239
# financial report period changes - delete all financial reports and insert new ones
230
240
) else if __old__.financialReportPeriod ?!= __new__.financialReportPeriod then (
231
241
with
@@ -234,12 +244,11 @@ module default {
234
244
union (
235
245
delete report
236
246
)),
237
- insertedFinancialReports := Project::insert_financial_reports(__new__, array_agg(financialReports))
238
- select insertedFinancialReports
247
+ select Project::create_financial_reports(__new__, array_agg(financialReports))
239
248
# start or end dates otherwise change (expansion) - insert all new reports
240
249
) else if newMouStart ?!= oldMouStart or newMouEnd ?!= oldMouEnd then (
241
250
with
242
- insertedFinancialReports := Project::insert_financial_reports (__new__, array_agg(financialReports))
251
+ insertedFinancialReports := Project::create_financial_reports (__new__, array_agg(financialReports))
243
252
select Project::insert_narrative_and_progress_reports(__new__, array_agg(narrativeAndProgressReports))
244
253
# nothing changes
245
254
) else (
@@ -335,12 +344,12 @@ module Project {
335
344
}
336
345
337
346
# 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
347
+ # and creates a single additional range that is bound by the given end date, with the upper
348
+ # bound inclusive
339
349
function create_periodic_report_ranges(startDate: cal::local_date, endDate: cal::local_date,
340
350
monthInterval: str) -> set of range<cal::local_date>
341
351
using (
342
352
with
343
- # debug := default::log_it("In create_periodic_report_ranges - input", <json>array_agg({<str>startDate, <str>endDate, monthInterval})),
344
353
reportingPeriod := range(<cal::local_date>startDate, <cal::local_date>endDate),
345
354
reportPeriodStartDates := range_unpack(reportingPeriod, <cal::date_duration>(monthInterval ++ ' month')),
346
355
reportPeriodRanges := (
@@ -351,11 +360,8 @@ module Project {
351
360
select range(<cal::local_date>firstDayOfMonth, <cal::local_date>firstDayOfNextPeriod)
352
361
)
353
362
),
354
- additionalReportPeriodRange := (
355
- select range(<cal::local_date>endDate, <cal::local_date>endDate, inc_upper := true)
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))
363
+ additionalReportPeriodRange := (select range(<cal::local_date>endDate, <cal::local_date>endDate, inc_upper := true)),
364
+ debug := default::log_it("In create_periodic_report_ranges", <json>array_agg(additionalReportPeriodRange)),
359
365
select reportPeriodRanges union additionalReportPeriodRange
360
366
);
361
367
@@ -372,9 +378,6 @@ module Project {
372
378
assert_exists(newProject.mouStart), assert_exists(newProject.mouEnd), monthInterval
373
379
),
374
380
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
381
select
379
382
if exists existingReports then (
380
383
with
@@ -386,47 +389,104 @@ module Project {
386
389
select report.period
387
390
)
388
391
),
389
- # debug := default::log_it("In determine_requested_report_periods - something", <json>array_agg(something)),
390
392
select (something)
391
393
) else (
392
394
select distinctRequestedReportPeriods
393
395
)
394
396
)
395
397
);
396
- }
398
+ }
397
399
398
400
function insert_narrative_and_progress_reports(newProject: default::Project,
399
401
narrativeAndProgressReports: optional array<default::PeriodicReport>) -> set of default::PeriodicReport
400
402
using (
401
403
with
402
- # debug := default::log_it("In insert_narrative_and_progress_reports", <json>{}),
403
404
periodsForInsertion := Project::determine_requested_report_periods(
404
405
'3',
405
406
newProject,
406
407
narrativeAndProgressReports
407
408
),
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
- ),
409
+ narrativeReports := Project::insert_narrative_reports(newProject, array_agg(periodsForInsertion)),
410
+ progressReports := Project::insert_progress_reports(newProject, array_agg(periodsForInsertion))
411
+ select narrativeReports
412
+ );
413
+
414
+ function create_financial_reports(newProject: default::Project,
415
+ financialReports: optional array<default::FinancialReport>) -> set of default::FinancialReport
416
+ using (
417
+ select
418
+ if exists newProject.financialReportPeriod then (
419
+ with
420
+ periodsForInsertion := (
421
+ select
422
+ if newProject.financialReportPeriod ?= default::ReportPeriod.Monthly then (
423
+ select Project::determine_requested_report_periods(
424
+ '1',
425
+ newProject,
426
+ financialReports
427
+ )
428
+ ) else (
429
+ select Project::determine_requested_report_periods(
430
+ '3',
431
+ newProject,
432
+ financialReports
433
+ )
434
+ )
435
+ ),
436
+ project := newProject,
437
+ select Project::insert_financial_reports(project, array_agg(periodsForInsertion))
438
+ ) else (
439
+ select <default::FinancialReport>{}
440
+ )
441
+ );
442
+
443
+ function insert_financial_reports(newProject: default::Project,
444
+ periodsForInsertion: array<range<cal::local_date>>) -> set of default::FinancialReport
445
+ using (
446
+ for reportPeriod in array_unpack(periodsForInsertion)
447
+ union (
448
+ insert default::FinancialReport {
449
+ createdAt := datetime_of_statement(),
450
+ modifiedAt := datetime_of_statement(),
451
+ createdBy := assert_exists(global default::currentActor),
452
+ modifiedBy := assert_exists(global default::currentActor),
453
+ project := newProject,
454
+ projectContext := newProject.projectContext,
455
+ container := newProject,
456
+ period := reportPeriod,
457
+ }
458
+ )
459
+ );
460
+
461
+ function insert_narrative_reports(newProject: default::Project,
462
+ periodsForInsertion: array<range<cal::local_date>>) -> set of default::NarrativeReport
463
+ using (
464
+ for reportPeriod in array_unpack(periodsForInsertion)
465
+ union (
466
+ insert default::NarrativeReport {
467
+ createdAt := datetime_of_statement(),
468
+ modifiedAt := datetime_of_statement(),
469
+ createdBy := assert_exists(global default::currentActor),
470
+ modifiedBy := assert_exists(global default::currentActor),
471
+ project := newProject,
472
+ projectContext := newProject.projectContext,
473
+ container := newProject,
474
+ period := reportPeriod,
475
+ }
476
+ )
477
+ );
478
+
479
+ function insert_progress_reports(newProject: default::Project,
480
+ periodsForInsertion: array<range<cal::local_date>>) -> set of default::ProgressReport
481
+ using (
482
+ with
423
483
projectWithEngagements := (
424
484
select newProject {
425
485
engagements := .<project[is default::LanguageEngagement]
426
486
}
427
487
),
428
488
progressReports := (
429
- for reportPeriod in periodsForInsertion
489
+ for reportPeriod in array_unpack( periodsForInsertion)
430
490
union (
431
491
for engagement in projectWithEngagements.engagements
432
492
union (
@@ -444,49 +504,6 @@ module Project {
444
504
)
445
505
)
446
506
)
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
- )
507
+ select progressReports
491
508
);
492
509
}
0 commit comments