@@ -124,22 +124,33 @@ module default {
124
124
);
125
125
126
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 (
127
+ when (exists __new__.mouStart and exists __new__.mouEnd)
128
+ do (
133
129
with
134
- interval := (select
135
- if __new__.financialReportPeriod = default::ReportPeriod.Monthly then '1' else '3'),
136
130
reportRanges := Project::create_periodic_report_ranges(
137
131
__new__.mouStart,
138
132
__new__.mouEnd,
139
- interval
133
+ '3'
134
+ ),
135
+ insertedFinancialReports := (
136
+ select
137
+ if __new__.financialReportPeriod = default::ReportPeriod.Monthly then (
138
+ with
139
+ financialReportRanges := Project::create_periodic_report_ranges(
140
+ __new__.mouStart,
141
+ __new__.mouEnd,
142
+ '1'
143
+ ),
144
+ financialReports := Project::insert_financial_reports(array_agg(financialReportRanges), __new__)
145
+ select financialReports
146
+ ) else if __new__.financialReportPeriod = default::ReportPeriod.Quarterly then (
147
+ select Project::insert_financial_reports(array_agg(reportRanges), __new__)
148
+ ) else (
149
+ select <default::FinancialReport>{}
150
+ )
140
151
),
141
- insertedReports := Project::insert_reports (array_agg(reportRanges), __new__)
142
- select insertedReports
152
+ insertedNarrativeAndProgressReports := Project::insert_narrative_and_progress_reports (array_agg(reportRanges), __new__)
153
+ select insertedFinancialReports union insertedNarrativeAndProgressReports
143
154
);
144
155
145
156
trigger addRemovePeriodicReports after update for each
@@ -154,59 +165,69 @@ module default {
154
165
oldMouStart := __old__.mouStart,
155
166
newMouEnd := __new__.mouEnd,
156
167
oldMouEnd := __old__.mouEnd,
157
- existingReports := (
158
- select FinancialReport union NarrativeReport
159
- filter .container.id = __old__.id
160
- ),
161
- interval := (
162
- select (if __new__.financialReportPeriod = default::ReportPeriod.Monthly then '1' else '3')
163
- ),
164
- requestedReportPeriods := Project::create_periodic_report_ranges(
168
+ monthlyReportRanges := Project::create_periodic_report_ranges(
165
169
__new__.mouStart,
166
170
__new__.mouEnd,
167
- interval
171
+ '1'
172
+ ),
173
+ quarterlyReportRanges := Project::create_periodic_report_ranges(
174
+ __new__.mouStart,
175
+ __new__.mouEnd,
176
+ '3'
177
+ ),
178
+ financialReports := (
179
+ select default::FinancialReport
180
+ filter .container.id = __old__.id
181
+ ),
182
+ financialReportsForDeletion := (
183
+ select financialReports
184
+ filter not exists .reportFile
185
+ ),
186
+ narrativeAndProgressReports := (
187
+ select NarrativeReport union ProgressReport
188
+ filter .container.id = __old__.id
168
189
),
169
- reportsForDeletion := (
170
- select existingReports
190
+ narrativeAndProgressReportsForDeletion := (
191
+ select narrativeAndProgressReports
171
192
filter not exists .reportFile
172
- )
193
+ )
173
194
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 (
178
- for report in reportsForDeletion
195
+ # start date or end date were deleted - delete all reports
196
+ if not exists __new__.mouStart or not exists __new__.mouEnd then (
197
+ for report in financialReportsForDeletion union narrativeAndProgressReportsForDeletion
179
198
union (
180
- delete report
199
+ delete report
181
200
)
182
- )
183
- # financial report period changes, start date is moved forward, or end date is moved backward
184
- else if __old__.financialReportPeriod ?!= __new__.financialReportPeriod
185
- or (__new__.mouStart > __old__.mouStart) ?? false
186
- or (__new__.mouEnd < __old__.mouEnd) ?? false then (
187
- with
188
- periodsForInsertion := Project::determine_requested_report_periods(
189
- array_agg(requestedReportPeriods),
190
- array_agg(existingReports)
191
- ),
192
- insertedReports := Project::insert_reports(array_agg(periodsForInsertion), __new__),
193
- deletedReports := (for report in reportsForDeletion
201
+ # financial report period was deleted - delete financial reports
202
+ ) else if not exists __new__.financialReportPeriod then (
203
+ for report in financialReportsForDeletion
204
+ union (
205
+ delete report
206
+ )
207
+ # start date is moved forward or end date is moved backward (contraction) - delete all
208
+ # reports that are not in the new range
209
+ ) else if (__new__.mouStart > __old__.mouStart) ?? false or (__new__.mouEnd < __old__.mouEnd) ?? false then (
210
+ for report in financialReportsForDeletion union narrativeAndProgressReportsForDeletion
194
211
union (
195
212
delete report
196
- filter report.period not in requestedReportPeriods
197
- ))
198
- select insertedReports
199
- )
200
- # start or end dates otherwise change
201
- else if newMouStart ?!= oldMouStart
202
- or newMouEnd ?!= oldMouEnd then (
213
+ filter report.period not in monthlyReportRanges union quarterlyReportRanges
214
+ )
215
+ # financial report period changes - delete all financial reports and insert new ones
216
+ ) else if __old__.financialReportPeriod ?!= __new__.financialReportPeriod then (
217
+ with
218
+ insertedFinancialReports := Project::insert_financial_reports(__new__, array_agg(financialReports),
219
+ array_agg(monthlyReportRanges), array_agg(quarterlyReportRanges))
220
+ for report in financialReportsForDeletion
221
+ union (
222
+ delete report
223
+ )
224
+ # start or end dates otherwise change (expansion) - insert all new reports
225
+ ) else if newMouStart ?!= oldMouStart or newMouEnd ?!= oldMouEnd then (
203
226
with
204
- periodsForInsertion := Project::determine_requested_report_periods(
205
- array_agg(requestedReportPeriods),
206
- array_agg(existingReports)
207
- ),
208
- insertedReports := Project::insert_reports(array_agg(periodsForInsertion), __new__)
209
- select insertedReports
227
+ insertedFinancialReports := Project::insert_financial_reports(__new__, array_agg(financialReports),
228
+ array_agg(monthlyReportRanges), array_agg(quarterlyReportRanges))
229
+ select Project::insert_narrative_and_progress_reports(__new__, array_agg(quarterlyReportRanges),
230
+ array_agg(narrativeAndProgressReports))
210
231
# nothing changes
211
232
) else (
212
233
select <PeriodicReport>{}
@@ -300,7 +321,8 @@ module Project {
300
321
};
301
322
}
302
323
303
- # creates the ranges for the given start and end dates based upon the given month interval
324
+ # creates the ranges for the given start and end dates based upon the given month interval,
325
+ # appending one additional range bound by matching end dates
304
326
function create_periodic_report_ranges(startDate: cal::local_date, endDate: cal::local_date,
305
327
monthInterval: str) -> set of range<cal::local_date>
306
328
using (
@@ -333,16 +355,21 @@ module Project {
333
355
select report.period
334
356
)
335
357
)
336
- select periodsForInsertion
358
+ select periodsForInsertion
337
359
);
338
360
339
- function insert_reports(requestedReportPeriodsForInsertion: array<range<cal::local_date>>,
340
- newProject: default::Project) -> set of default::PeriodicReport
361
+ function insert_narrative_and_progress_reports(newProject: default::Project,
362
+ quarterlyReportRanges: array<range<cal::local_date>>,
363
+ narrativeAndProgressReports: array<default::PeriodicReport>) -> set of default::PeriodicReport
341
364
using (
342
365
with
343
- financialReports := (for reportPeriod in array_unpack(requestedReportPeriodsForInsertion)
366
+ periodsForInsertion := Project::determine_requested_report_periods(
367
+ quarterlyReportRanges,
368
+ narrativeAndProgressReports
369
+ ),
370
+ narrativeReports := (for reportPeriod in periodsForInsertion
344
371
union (
345
- insert default::FinancialReport {
372
+ insert default::NarrativeReport {
346
373
createdAt := datetime_of_statement(),
347
374
modifiedAt := datetime_of_statement(),
348
375
createdBy := assert_exists(global default::currentActor),
@@ -353,9 +380,9 @@ module Project {
353
380
period := reportPeriod,
354
381
}
355
382
)),
356
- narrativeReports := (for reportPeriod in array_unpack(requestedReportPeriodsForInsertion)
383
+ progressReports := (for reportPeriod in periodsForInsertion
357
384
union (
358
- insert default::NarrativeReport {
385
+ insert default::ProgressReport {
359
386
createdAt := datetime_of_statement(),
360
387
modifiedAt := datetime_of_statement(),
361
388
createdBy := assert_exists(global default::currentActor),
@@ -366,6 +393,41 @@ module Project {
366
393
period := reportPeriod,
367
394
}
368
395
))
369
- select financialReports union narrativeReports
396
+ select narrativeReports union progressReports
370
397
);
398
+
399
+ function insert_financial_reports(newProject: default::Project,
400
+ financialReports: array<default::FinancialReport>,
401
+ monthlyReportRanges: array<range<cal::local_date>>,
402
+ quarterlyReportRanges: array<range<cal::local_date>>) -> set of default::PeriodicReport
403
+ using (
404
+ with
405
+ periodsForInsertion := (
406
+ select
407
+ if newProject.financialReportPeriod = default::ReportPeriod.Monthly then (
408
+ select Project::determine_requested_report_periods(
409
+ monthlyReportRanges,
410
+ financialReports
411
+ )
412
+ ) else (
413
+ select Project::determine_requested_report_periods(
414
+ quarterlyReportRanges,
415
+ financialReports
416
+ )
417
+ )
418
+ )
419
+ select (for reportPeriod in periodsForInsertion
420
+ union (
421
+ insert default::FinancialReport {
422
+ createdAt := datetime_of_statement(),
423
+ modifiedAt := datetime_of_statement(),
424
+ createdBy := assert_exists(global default::currentActor),
425
+ modifiedBy := assert_exists(global default::currentActor),
426
+ project := newProject,
427
+ projectContext := newProject.projectContext,
428
+ container := newProject,
429
+ period := reportPeriod,
430
+ }
431
+ ))
432
+ );
371
433
}
0 commit comments