Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Katkaistaan myös määräaikainen päällekkäinen tulotieto automaattisesti uutta luotaessa #6249

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions frontend/src/e2e-test/specs/5_employee/income.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,15 @@ describe('Income', () => {

it('Overlapping incomes error', async () => {
await incomesSection.openNewIncomeForm()
await incomesSection.fillIncomeStartDate('1.1.2020')
await incomesSection.fillIncomeStartDate('1.2.2020')
await incomesSection.fillIncomeEndDate('31.3.2020')
await incomesSection.confirmRetroactive.check()
await incomesSection.chooseIncomeEffect('MAX_FEE_ACCEPTED')
await incomesSection.save()

await incomesSection.openNewIncomeForm()
await incomesSection.fillIncomeStartDate('1.2.2020')
await incomesSection.fillIncomeEndDate('30.4.2020')
await incomesSection.fillIncomeStartDate('1.1.2020')
await incomesSection.fillIncomeEndDate('15.2.2020')
await incomesSection.confirmRetroactive.check()
await incomesSection.chooseIncomeEffect('MAX_FEE_ACCEPTED')
await incomesSection.saveFailing()
Expand All @@ -216,6 +216,34 @@ describe('Income', () => {
)
})

it('Overlapping income may be automatically ended', async () => {
await incomesSection.openNewIncomeForm()
await incomesSection.fillIncomeStartDate('1.1.2020')
await incomesSection.fillIncomeEndDate('31.3.2020')
await incomesSection.confirmRetroactive.check()
await incomesSection.chooseIncomeEffect('MAX_FEE_ACCEPTED')
await incomesSection.save()

await incomesSection.openNewIncomeForm()
await incomesSection.fillIncomeStartDate('1.2.2020')
await incomesSection.fillIncomeEndDate('15.6.2020')
await incomesSection.confirmRetroactive.check()
await incomesSection.chooseIncomeEffect('MAX_FEE_ACCEPTED')
await incomesSection.save()

await waitUntilEqual(() => incomesSection.incomeListItemCount(), 2)
await incomesSection.incomeListItems
.nth(0)
.assertText((s) =>
s.includes('Tulotiedot ajalle 01.02.2020 - 15.06.2020')
)
await incomesSection.incomeListItems
.nth(1)
.assertText((s) =>
s.includes('Tulotiedot ajalle 01.01.2020 - 31.01.2020')
)
})

it('Attachments can be added.', async () => {
await incomesSection.openNewIncomeForm()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,14 @@ class IncomeIntegrationTest : FullApplicationTest(resetDbBeforeEach = true) {
}

@Test
fun `createIncome splits earlier indefinite income`() {
fun `createIncome ends earlier income without end date`() {
val firstIncome = testIncomeRequest().copy(validTo = null)
val firstIncomeId =
db.transaction { tx ->
tx.insertIncome(clock.now(), firstIncome, financeUser.evakaUserId)
}

val secondIncome = firstIncome.copy(validFrom = firstIncome.validFrom.plusMonths(1))
val secondIncome = firstIncome.copy(validFrom = firstIncome.validFrom.plusDays(1))
createIncome(secondIncome)

val incomes = getPersonIncomes(testIncomeRequest().personId)
Expand All @@ -194,14 +194,42 @@ class IncomeIntegrationTest : FullApplicationTest(resetDbBeforeEach = true) {
}

@Test
fun `createIncome throws with partly overlapping date range`() {
fun `createIncome ends earlier income with end date`() {
val firstIncome = testIncomeRequest()
val firstIncomeId =
db.transaction { tx ->
tx.insertIncome(clock.now(), firstIncome, financeUser.evakaUserId)
}

val secondIncome =
firstIncome.copy(
validFrom = firstIncome.validFrom.plusDays(1),
validTo = firstIncome.validTo!!.plusDays(10),
)
createIncome(secondIncome)

val incomes = getPersonIncomes(testIncomeRequest().personId)

assertEquals(2, incomes.size)

val firstIncomeResult = incomes.find { it.id == firstIncomeId }!!
assertEquals(firstIncome.validFrom, firstIncomeResult.validFrom)
assertEquals(secondIncome.validFrom.minusDays(1), firstIncomeResult.validTo)

val secondIncomeResult = incomes.find { it.id != firstIncomeId }!!
assertEquals(secondIncome.validFrom, secondIncomeResult.validFrom)
assertEquals(secondIncome.validTo, secondIncomeResult.validTo)
}

@Test
fun `createIncome throws if new range overlaps start of old range`() {
db.transaction { tx ->
tx.insertIncome(clock.now(), testIncomeRequest(), financeUser.evakaUserId)
}

val overlappingIncome =
testIncomeRequest().let {
it.copy(validFrom = it.validFrom.plusDays(10), validTo = null)
it.copy(validFrom = it.validFrom.minusDays(1), validTo = it.validTo!!.minusDays(1))
}
assertThrows<Conflict> { createIncome(overlappingIncome) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import fi.espoo.evaka.AuditId
import fi.espoo.evaka.attachment.AttachmentParent
import fi.espoo.evaka.attachment.associateOrphanAttachments
import fi.espoo.evaka.invoicing.data.deleteIncome
import fi.espoo.evaka.invoicing.data.endEarlierOverlappingIncome
import fi.espoo.evaka.invoicing.data.getIncome
import fi.espoo.evaka.invoicing.data.getIncomesForPerson
import fi.espoo.evaka.invoicing.data.insertIncome
import fi.espoo.evaka.invoicing.data.splitEarlierIncome
import fi.espoo.evaka.invoicing.data.updateIncome
import fi.espoo.evaka.invoicing.domain.Income
import fi.espoo.evaka.invoicing.domain.IncomeCoefficient
Expand Down Expand Up @@ -129,7 +129,12 @@ class IncomeController(
val now = clock.now()
val incomeTypes = incomeTypesProvider.get()
val validIncome = validateIncome(income, incomeTypes)
tx.splitEarlierIncome(now, validIncome.personId, period, user.evakaUserId)
tx.endEarlierOverlappingIncome(
now,
validIncome.personId,
period,
user.evakaUserId,
)
val id = tx.insertIncome(now, validIncome, user.evakaUserId)
tx.associateOrphanAttachments(
user.evakaUserId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ fun Database.Transaction.deleteIncome(incomeId: IncomeId) {
handlingExceptions { update.execute() }
}

fun Database.Transaction.splitEarlierIncome(
fun Database.Transaction.endEarlierOverlappingIncome(
now: HelsinkiDateTime,
personId: PersonId,
period: DateRange,
Expand All @@ -230,7 +230,7 @@ fun Database.Transaction.splitEarlierIncome(
WHERE
person_id = ${bind(personId)}
AND valid_from < ${bind(period.start)}
AND valid_to IS NULL
AND (valid_to IS NULL OR valid_to >= ${bind(period.start)})
"""
)
}
Expand Down
Loading