Skip to content

Commit

Permalink
Merge pull request #6047 from espoon-voltti/last-invoiced-month
Browse files Browse the repository at this point in the history
Korjataan viimeisen laskutetun kuukauden päättely
  • Loading branch information
akheron authored Nov 28, 2024
2 parents e87b3e4 + 8de4642 commit da2e09b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,40 @@ class ReplacementInvoicesIntegrationTest : FullApplicationTest(resetDbBeforeEach
}

@Test
fun `replacements are generated 12 months to the past`() {
fun `replacements are generated 12 months to the past from the last sent invoice`() {
val twoMonthsAgo = previousMonth.minusMonths(1)

// Generate and send an unrelated invoice to make `twoMonthsAgo` the last invoiced month
val headOfFamily2 = DevPerson(ssn = "010101-9998")
val child2 = DevPerson()
db.transaction { tx ->
tx.insert(headOfFamily2, DevPersonType.ADULT)
tx.insert(child2, DevPersonType.CHILD)
}
insertPlacementAndFeeDecision(
headOfFamily = headOfFamily2,
child = child2,
fee = 29500,
range = FiniteDateRange.ofMonth(twoMonthsAgo),
)
generateAndSendInvoices(twoMonthsAgo)

insertPlacementAndFeeDecision(
fee = 29500,
range =
FiniteDateRange(
previousMonth.minusMonths(12).atDay(1),
previousMonth.atEndOfMonth(),
previousMonth.minusMonths(13).atDay(1),
YearMonth.from(today).atEndOfMonth(),
),
)

val replacements = generateReplacementDrafts()

assertEquals(replacements.size, 12)
assertEquals(
twoMonthsAgo.minusMonths(11),
replacements.minBy { it.periodStart }.targetMonth(),
)
assertEquals(twoMonthsAgo, replacements.maxBy { it.periodStart }.targetMonth())
replacements.forEach { replacement ->
assertEquals(InvoiceStatus.REPLACEMENT_DRAFT, replacement.status)
assertEquals(null, replacement.replacedInvoiceId)
Expand Down Expand Up @@ -406,9 +427,9 @@ class ReplacementInvoicesIntegrationTest : FullApplicationTest(resetDbBeforeEach
}
}

fun generateAndSendInvoices(): List<InvoiceDetailed> =
private fun generateAndSendInvoices(month: YearMonth = previousMonth): List<InvoiceDetailed> =
db.transaction { tx ->
invoiceGenerator.generateAllDraftInvoices(tx, previousMonth)
invoiceGenerator.generateAllDraftInvoices(tx, month)
invoiceService.sendInvoices(
tx,
AuthenticatedUser.SystemInternalUser.evakaUserId,
Expand All @@ -417,7 +438,6 @@ class ReplacementInvoicesIntegrationTest : FullApplicationTest(resetDbBeforeEach
null,
null,
)

tx.searchInvoices()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -620,3 +620,9 @@ WHERE id = ${bind(replacedInvoiceId)}
.updateExactlyOne()
}
}

fun Database.Read.getLastInvoicedMonth(): YearMonth? =
createQuery { sql("SELECT MAX(invoice_date) AS month FROM invoice WHERE status = 'SENT'") }
.exactlyOneOrNull<YearMonth>()
// Invoices of month M are sent in month M+1
?.minusMonths(1)
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import fi.espoo.evaka.children.getChildIdsByHeadsOfFamily
import fi.espoo.evaka.invoicing.data.deleteDraftInvoices
import fi.espoo.evaka.invoicing.data.feeDecisionQuery
import fi.espoo.evaka.invoicing.data.getFeeThresholds
import fi.espoo.evaka.invoicing.data.getLastInvoicedMonth
import fi.espoo.evaka.invoicing.data.getSentInvoicesOfMonth
import fi.espoo.evaka.invoicing.data.insertDraftInvoices
import fi.espoo.evaka.invoicing.domain.ChildWithDateOfBirth
Expand Down Expand Up @@ -130,7 +131,10 @@ class InvoiceGenerator(
}

val monthsToGenerate = 12L
val latestMonth = YearMonth.of(today.year, today.month).minusMonths(1)

val latestMonth: YearMonth =
dbc.read { tx -> tx.getLastInvoicedMonth() }
?: YearMonth.of(today.year, today.month).minusMonths(1)
val earliestMonth =
maxOf(replacementInvoicesStart, latestMonth.minusMonths(monthsToGenerate - 1))

Expand Down

0 comments on commit da2e09b

Please sign in to comment.