@@ -177,19 +177,40 @@ class ReplacementInvoicesIntegrationTest : FullApplicationTest(resetDbBeforeEach
177
177
}
178
178
179
179
@Test
180
- fun `replacements are generated 12 months to the past` () {
180
+ fun `replacements are generated 12 months to the past from the last sent invoice` () {
181
+ val twoMonthsAgo = previousMonth.minusMonths(1 )
182
+
183
+ // Generate and send an unrelated invoice to make `twoMonthsAgo` the last invoiced month
184
+ val headOfFamily2 = DevPerson (ssn = " 010101-9998" )
185
+ val child2 = DevPerson ()
186
+ db.transaction { tx ->
187
+ tx.insert(headOfFamily2, DevPersonType .ADULT )
188
+ tx.insert(child2, DevPersonType .CHILD )
189
+ }
190
+ insertPlacementAndFeeDecision(
191
+ headOfFamily = headOfFamily2,
192
+ child = child2,
193
+ fee = 29500 ,
194
+ range = FiniteDateRange .ofMonth(twoMonthsAgo),
195
+ )
196
+ generateAndSendInvoices(twoMonthsAgo)
197
+
181
198
insertPlacementAndFeeDecision(
182
199
fee = 29500 ,
183
200
range =
184
201
FiniteDateRange (
185
- previousMonth.minusMonths(12 ).atDay(1 ),
186
- previousMonth .atEndOfMonth(),
202
+ previousMonth.minusMonths(13 ).atDay(1 ),
203
+ YearMonth .from(today) .atEndOfMonth(),
187
204
),
188
205
)
189
-
190
206
val replacements = generateReplacementDrafts()
191
207
192
208
assertEquals(replacements.size, 12 )
209
+ assertEquals(
210
+ twoMonthsAgo.minusMonths(11 ),
211
+ replacements.minBy { it.periodStart }.targetMonth(),
212
+ )
213
+ assertEquals(twoMonthsAgo, replacements.maxBy { it.periodStart }.targetMonth())
193
214
replacements.forEach { replacement ->
194
215
assertEquals(InvoiceStatus .REPLACEMENT_DRAFT , replacement.status)
195
216
assertEquals(null , replacement.replacedInvoiceId)
@@ -406,9 +427,9 @@ class ReplacementInvoicesIntegrationTest : FullApplicationTest(resetDbBeforeEach
406
427
}
407
428
}
408
429
409
- fun generateAndSendInvoices (): List <InvoiceDetailed > =
430
+ private fun generateAndSendInvoices (month : YearMonth = previousMonth ): List <InvoiceDetailed > =
410
431
db.transaction { tx ->
411
- invoiceGenerator.generateAllDraftInvoices(tx, previousMonth )
432
+ invoiceGenerator.generateAllDraftInvoices(tx, month )
412
433
invoiceService.sendInvoices(
413
434
tx,
414
435
AuthenticatedUser .SystemInternalUser .evakaUserId,
@@ -417,7 +438,6 @@ class ReplacementInvoicesIntegrationTest : FullApplicationTest(resetDbBeforeEach
417
438
null ,
418
439
null ,
419
440
)
420
-
421
441
tx.searchInvoices()
422
442
}
423
443
}
0 commit comments