Skip to content

Commit 7523bc0

Browse files
committed
Gjør utregn av skatteperiode smartere
1 parent e5c602f commit 7523bc0

File tree

5 files changed

+86
-20
lines changed

5 files changed

+86
-20
lines changed

common/domain/src/main/kotlin/no/nav/su/se/bakover/common/domain/tid/YearRange.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,24 @@ fun YearRange.krympTilØvreGrense(øvreGrense: Year): YearRange {
7070
}
7171
}
7272

73+
fun YearRange.utvidNedreGrense(nedreGrense: Year): YearRange {
74+
return if (nedreGrense > endInclusive) {
75+
YearRange(nedreGrense, nedreGrense)
76+
} else {
77+
YearRange(nedreGrense, endInclusive)
78+
}
79+
}
80+
81+
fun YearRange.krymptTilNedreGrense(nedreGrense: Year): YearRange {
82+
return if (nedreGrense > endInclusive) {
83+
YearRange(nedreGrense, nedreGrense)
84+
} else {
85+
YearRange(max(start, nedreGrense), endInclusive)
86+
}
87+
}
88+
7389
fun min(y1: Year, y2: Year): Year = if (y1.value <= y2.value) y1 else y2
90+
fun max(y1: Year, y2: Year): Year = if (y1.value > y2.value) y1 else y2
7491

7592
fun min(a: YearRange, b: YearRange): YearRange = when {
7693
a.start < b.start -> a
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package no.nav.su.se.bakover.domain.skatt
2+
3+
import no.nav.su.se.bakover.common.tid.YearRange
4+
import no.nav.su.se.bakover.common.tid.krympTilØvreGrense
5+
import no.nav.su.se.bakover.common.tid.utvidNedreGrense
6+
import no.nav.su.se.bakover.domain.søknadsbehandling.stønadsperiode.Stønadsperiode
7+
import java.time.Clock
8+
import java.time.Year
9+
10+
fun Stønadsperiode?.regnUtSkatteperiodeForStønadsperiode(clock: Clock): YearRange {
11+
val iÅr = Year.now(clock)
12+
val iForFjor = iÅr.minusYears(2)
13+
14+
return this
15+
?.toYearRange()
16+
?.krympTilØvreGrense(iÅr)
17+
?.utvidNedreGrense(iForFjor)
18+
?: YearRange(iForFjor, iÅr)
19+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package no.nav.su.se.bakover.domain.skatt
2+
3+
import io.kotest.matchers.shouldBe
4+
import no.nav.su.se.bakover.common.tid.YearRange
5+
import no.nav.su.se.bakover.common.tid.periode.juli
6+
import no.nav.su.se.bakover.common.tid.periode.juni
7+
import no.nav.su.se.bakover.domain.søknadsbehandling.stønadsperiode.Stønadsperiode
8+
import no.nav.su.se.bakover.test.fixedClock
9+
import org.junit.jupiter.api.Test
10+
import java.time.Year
11+
12+
internal class RegnUtSkatteperiodeForStønadsperiodeKtTest {
13+
@Test
14+
fun `should return range from two years ago to this year when stønadsperiode is null`() {
15+
val expected = YearRange(Year.of(2022), Year.of(2024))
16+
val actual = null.regnUtSkatteperiodeForStønadsperiode(fixedClock)
17+
18+
actual shouldBe expected
19+
}
20+
21+
@Test
22+
fun `should return same range as stønadsperiode when it's within bounds`() {
23+
val stønadsperiode =
24+
Stønadsperiode.create(juli(2023)..juni(2024))
25+
val expected = YearRange(Year.of(2023), Year.of(2024))
26+
val actual = stønadsperiode.regnUtSkatteperiodeForStønadsperiode(fixedClock)
27+
28+
actual shouldBe expected
29+
}
30+
31+
}

service/src/main/kotlin/no/nav/su/se/bakover/service/skatt/SkatteService.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import no.nav.su.se.bakover.common.tid.YearRange
88
import no.nav.su.se.bakover.domain.skatt.Skattegrunnlag
99

1010
interface SkatteService {
11+
/**
12+
* Ikke i bruk. TODO jah: Slett?
13+
*/
1114
fun hentSamletSkattegrunnlag(
1215
fnr: Fnr,
1316
saksbehandler: NavIdentBruker.Saksbehandler,
@@ -19,6 +22,9 @@ interface SkatteService {
1922
yearRange: YearRange,
2023
): Skattegrunnlag
2124

25+
/**
26+
* Brukes for frioppslag på skattemelding.
27+
*/
2228
fun hentOgLagSkattePdf(request: FrioppslagSkattRequest): Either<KunneIkkeHenteOgLagePdfAvSkattegrunnlag, PdfA>
2329
fun hentLagOgJournalførSkattePdf(request: FrioppslagSkattRequest): Either<KunneIkkeGenerereSkattePdfOgJournalføre, PdfA>
2430
}

service/src/main/kotlin/no/nav/su/se/bakover/service/søknadsbehandling/SøknadsbehandlingServiceImpl.kt

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import no.nav.su.se.bakover.common.persistence.SessionFactory
1313
import no.nav.su.se.bakover.common.persistence.TransactionContext
1414
import no.nav.su.se.bakover.common.person.Fnr
1515
import no.nav.su.se.bakover.common.tid.YearRange
16-
import no.nav.su.se.bakover.common.tid.krympTilØvreGrense
17-
import no.nav.su.se.bakover.common.tid.toRange
1816
import no.nav.su.se.bakover.domain.Sak
1917
import no.nav.su.se.bakover.domain.behandling.BehandlingMetrics
2018
import no.nav.su.se.bakover.domain.grunnlag.Bosituasjon.Companion.harEPS
@@ -29,6 +27,7 @@ import no.nav.su.se.bakover.domain.revurdering.vilkår.bosituasjon.LeggTilBositu
2927
import no.nav.su.se.bakover.domain.sak.SakService
3028
import no.nav.su.se.bakover.domain.sak.lagNyUtbetaling
3129
import no.nav.su.se.bakover.domain.skatt.Skattegrunnlag
30+
import no.nav.su.se.bakover.domain.skatt.regnUtSkatteperiodeForStønadsperiode
3231
import no.nav.su.se.bakover.domain.statistikk.StatistikkEvent
3332
import no.nav.su.se.bakover.domain.statistikk.StatistikkEventObserver
3433
import no.nav.su.se.bakover.domain.statistikk.notify
@@ -98,7 +97,6 @@ import satser.domain.SatsFactory
9897
import vilkår.formue.domain.FormuegrenserFactory
9998
import økonomi.domain.utbetaling.UtbetalingsinstruksjonForEtterbetalinger
10099
import java.time.Clock
101-
import java.time.Year
102100
import java.util.UUID
103101
import kotlin.reflect.KClass
104102

@@ -222,11 +220,11 @@ class SøknadsbehandlingServiceImpl(
222220
søknadsbehandlingRepo.hent(behandlingId)
223221
?: throw IllegalArgumentException("Søknadsbehandling send til attestering: Fant ikke søknadsbehandling med id $behandlingId. Avbryter handlingen.")
224222
).let {
225-
it as? KanSendesTilAttestering
226-
?: return KunneIkkeSendeSøknadsbehandlingTilAttestering.UgyldigTilstand(
227-
it::class,
228-
).left()
229-
}
223+
it as? KanSendesTilAttestering
224+
?: return KunneIkkeSendeSøknadsbehandlingTilAttestering.UgyldigTilstand(
225+
it::class,
226+
).left()
227+
}
230228
return søknadsbehandlingSomKanSendesTilAttestering.tilAttestering(
231229
saksbehandler = request.saksbehandler,
232230
fritekstTilBrev = request.fritekstTilBrev,
@@ -288,10 +286,10 @@ class SøknadsbehandlingServiceImpl(
288286
søknadsbehandlingRepo.hent(request.behandlingId)
289287
?: return KunneIkkeUnderkjenneSøknadsbehandling.FantIkkeBehandling.left()
290288
).let {
291-
it as? SøknadsbehandlingTilAttestering ?: return KunneIkkeUnderkjenneSøknadsbehandling.UgyldigTilstand(
292-
it::class,
293-
).left()
294-
}
289+
it as? SøknadsbehandlingTilAttestering ?: return KunneIkkeUnderkjenneSøknadsbehandling.UgyldigTilstand(
290+
it::class,
291+
).left()
292+
}
295293
return søknadsbehandling.tilUnderkjent(request.attestering).map { underkjent ->
296294
val aktørId = personService.hentAktørId(underkjent.fnr).getOrElse {
297295
log.error("Fant ikke aktør-id for sak: ${underkjent.id}")
@@ -787,7 +785,8 @@ class SøknadsbehandlingServiceImpl(
787785
saksbehandler: NavIdentBruker.Saksbehandler,
788786
samletSkattegrunnlag: (Fnr, NavIdentBruker.Saksbehandler, YearRange) -> Skattegrunnlag,
789787
clock: Clock,
790-
): Skattegrunnlag = samletSkattegrunnlag(fnr, saksbehandler, getYearRangeForSkatt(clock))
788+
): Skattegrunnlag =
789+
samletSkattegrunnlag(fnr, saksbehandler, stønadsperiode.regnUtSkatteperiodeForStønadsperiode(clock))
791790

792791
private fun Søknadsbehandling.hentSkattegrunnlagForEps(
793792
saksbehandler: NavIdentBruker.Saksbehandler,
@@ -797,15 +796,9 @@ class SøknadsbehandlingServiceImpl(
797796
samletSkattegrunnlag(
798797
grunnlagsdata.bosituasjon.singleFullstendigEpsOrNull()!!.fnr,
799798
saksbehandler,
800-
getYearRangeForSkatt(clock),
799+
stønadsperiode.regnUtSkatteperiodeForStønadsperiode(clock),
801800
)
802801
} else {
803802
null
804803
}
805-
806-
private fun Søknadsbehandling.getYearRangeForSkatt(clock: Clock): YearRange {
807-
return Year.now(clock).minusYears(1).let {
808-
stønadsperiode?.toYearRange()?.krympTilØvreGrense(it) ?: it.toRange()
809-
}
810-
}
811804
}

0 commit comments

Comments
 (0)