@@ -121,6 +121,41 @@ module default {
121
121
projectContext := __new__.projectContext,
122
122
}
123
123
);
124
+
125
+ trigger createPeriodicReportsOnInsert after insert for each do (
126
+ with
127
+ interval := (select
128
+ if __new__.financialReportPeriod = default::ReportPeriod.Monthly then '1' else '3'),
129
+ reportRanges := Project::create_periodic_report_ranges(
130
+ __new__.mouStart,
131
+ __new__.mouEnd,
132
+ 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
+ #receivedDate := __new__.financialReportReceivedAt, //TODO - what is this at the project level?
146
+ }),
147
+ (insert default::NarrativeReport {
148
+ createdAt := datetime_of_statement(),
149
+ modifiedAt := datetime_of_statement(),
150
+ createdBy := assert_exists(global currentActor),
151
+ modifiedBy := assert_exists(global currentActor),
152
+ project := __new__,
153
+ projectContext := __new__.projectContext,
154
+ container := __new__,
155
+ period := reportRange
156
+ })
157
+ )
158
+ )
124
159
}
125
160
126
161
abstract type TranslationProject extending Project {
@@ -208,4 +243,22 @@ module Project {
208
243
on target delete allow;
209
244
};
210
245
}
246
+
247
+ # creates the ranges for the given start and end dates based upon the given month interval
248
+ function create_periodic_report_ranges(startDate: cal::local_date, endDate: cal::local_date,
249
+ monthInterval: str) -> set of range<cal::local_date>
250
+ using (
251
+ with
252
+ reportingPeriod := range(<cal::local_date>startDate, <cal::local_date>endDate),
253
+ reportPeriodStartDates := range_unpack(reportingPeriod, <cal::date_duration>(monthInterval ++ ' month')),
254
+ reportPeriodRanges := (
255
+ for firstDayOfMonth in reportPeriodStartDates
256
+ union (
257
+ with
258
+ firstDayOfNextMonth := (select firstDayOfMonth + <cal::relative_duration>(monthInterval ++ ' month')),
259
+ lastDayOfMonth := firstDayOfNextMonth - <cal::relative_duration>'1 day'
260
+ select range(<cal::local_date>firstDayOfMonth, <cal::local_date>lastDayOfMonth)
261
+ ))
262
+ select reportPeriodRanges
263
+ )
211
264
}
0 commit comments