Skip to content

Commit da2e09b

Browse files
authored
Merge pull request #6047 from espoon-voltti/last-invoiced-month
Korjataan viimeisen laskutetun kuukauden päättely
2 parents e87b3e4 + 8de4642 commit da2e09b

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

service/src/integrationTest/kotlin/fi/espoo/evaka/invoicing/service/ReplacementInvoicesIntegrationTest.kt

+27-7
Original file line numberDiff line numberDiff line change
@@ -177,19 +177,40 @@ class ReplacementInvoicesIntegrationTest : FullApplicationTest(resetDbBeforeEach
177177
}
178178

179179
@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+
181198
insertPlacementAndFeeDecision(
182199
fee = 29500,
183200
range =
184201
FiniteDateRange(
185-
previousMonth.minusMonths(12).atDay(1),
186-
previousMonth.atEndOfMonth(),
202+
previousMonth.minusMonths(13).atDay(1),
203+
YearMonth.from(today).atEndOfMonth(),
187204
),
188205
)
189-
190206
val replacements = generateReplacementDrafts()
191207

192208
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())
193214
replacements.forEach { replacement ->
194215
assertEquals(InvoiceStatus.REPLACEMENT_DRAFT, replacement.status)
195216
assertEquals(null, replacement.replacedInvoiceId)
@@ -406,9 +427,9 @@ class ReplacementInvoicesIntegrationTest : FullApplicationTest(resetDbBeforeEach
406427
}
407428
}
408429

409-
fun generateAndSendInvoices(): List<InvoiceDetailed> =
430+
private fun generateAndSendInvoices(month: YearMonth = previousMonth): List<InvoiceDetailed> =
410431
db.transaction { tx ->
411-
invoiceGenerator.generateAllDraftInvoices(tx, previousMonth)
432+
invoiceGenerator.generateAllDraftInvoices(tx, month)
412433
invoiceService.sendInvoices(
413434
tx,
414435
AuthenticatedUser.SystemInternalUser.evakaUserId,
@@ -417,7 +438,6 @@ class ReplacementInvoicesIntegrationTest : FullApplicationTest(resetDbBeforeEach
417438
null,
418439
null,
419440
)
420-
421441
tx.searchInvoices()
422442
}
423443
}

service/src/main/kotlin/fi/espoo/evaka/invoicing/data/InvoiceQueries.kt

+6
Original file line numberDiff line numberDiff line change
@@ -620,3 +620,9 @@ WHERE id = ${bind(replacedInvoiceId)}
620620
.updateExactlyOne()
621621
}
622622
}
623+
624+
fun Database.Read.getLastInvoicedMonth(): YearMonth? =
625+
createQuery { sql("SELECT MAX(invoice_date) AS month FROM invoice WHERE status = 'SENT'") }
626+
.exactlyOneOrNull<YearMonth>()
627+
// Invoices of month M are sent in month M+1
628+
?.minusMonths(1)

service/src/main/kotlin/fi/espoo/evaka/invoicing/service/InvoiceGenerator.kt

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import fi.espoo.evaka.children.getChildIdsByHeadsOfFamily
1111
import fi.espoo.evaka.invoicing.data.deleteDraftInvoices
1212
import fi.espoo.evaka.invoicing.data.feeDecisionQuery
1313
import fi.espoo.evaka.invoicing.data.getFeeThresholds
14+
import fi.espoo.evaka.invoicing.data.getLastInvoicedMonth
1415
import fi.espoo.evaka.invoicing.data.getSentInvoicesOfMonth
1516
import fi.espoo.evaka.invoicing.data.insertDraftInvoices
1617
import fi.espoo.evaka.invoicing.domain.ChildWithDateOfBirth
@@ -130,7 +131,10 @@ class InvoiceGenerator(
130131
}
131132

132133
val monthsToGenerate = 12L
133-
val latestMonth = YearMonth.of(today.year, today.month).minusMonths(1)
134+
135+
val latestMonth: YearMonth =
136+
dbc.read { tx -> tx.getLastInvoicedMonth() }
137+
?: YearMonth.of(today.year, today.month).minusMonths(1)
134138
val earliestMonth =
135139
maxOf(replacementInvoicesStart, latestMonth.minusMonths(monthsToGenerate - 1))
136140

0 commit comments

Comments
 (0)