Skip to content

Commit

Permalink
Gjør utregn av skatteperiode smartere
Browse files Browse the repository at this point in the history
  • Loading branch information
hestad committed Jan 2, 2024
1 parent e5c602f commit 7523bc0
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,24 @@ fun YearRange.krympTilØvreGrense(øvreGrense: Year): YearRange {
}
}

fun YearRange.utvidNedreGrense(nedreGrense: Year): YearRange {
return if (nedreGrense > endInclusive) {
YearRange(nedreGrense, nedreGrense)
} else {
YearRange(nedreGrense, endInclusive)
}
}

fun YearRange.krymptTilNedreGrense(nedreGrense: Year): YearRange {
return if (nedreGrense > endInclusive) {
YearRange(nedreGrense, nedreGrense)
} else {
YearRange(max(start, nedreGrense), endInclusive)
}
}

fun min(y1: Year, y2: Year): Year = if (y1.value <= y2.value) y1 else y2
fun max(y1: Year, y2: Year): Year = if (y1.value > y2.value) y1 else y2

fun min(a: YearRange, b: YearRange): YearRange = when {
a.start < b.start -> a
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package no.nav.su.se.bakover.domain.skatt

import no.nav.su.se.bakover.common.tid.YearRange
import no.nav.su.se.bakover.common.tid.krympTilØvreGrense
import no.nav.su.se.bakover.common.tid.utvidNedreGrense
import no.nav.su.se.bakover.domain.søknadsbehandling.stønadsperiode.Stønadsperiode
import java.time.Clock
import java.time.Year

fun Stønadsperiode?.regnUtSkatteperiodeForStønadsperiode(clock: Clock): YearRange {
val iÅr = Year.now(clock)
val iForFjor = iÅr.minusYears(2)

return this
?.toYearRange()
?.krympTilØvreGrense(iÅr)
?.utvidNedreGrense(iForFjor)
?: YearRange(iForFjor, iÅr)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package no.nav.su.se.bakover.domain.skatt

import io.kotest.matchers.shouldBe
import no.nav.su.se.bakover.common.tid.YearRange
import no.nav.su.se.bakover.common.tid.periode.juli
import no.nav.su.se.bakover.common.tid.periode.juni
import no.nav.su.se.bakover.domain.søknadsbehandling.stønadsperiode.Stønadsperiode
import no.nav.su.se.bakover.test.fixedClock
import org.junit.jupiter.api.Test
import java.time.Year

internal class RegnUtSkatteperiodeForStønadsperiodeKtTest {
@Test
fun `should return range from two years ago to this year when stønadsperiode is null`() {
val expected = YearRange(Year.of(2022), Year.of(2024))
val actual = null.regnUtSkatteperiodeForStønadsperiode(fixedClock)

actual shouldBe expected
}

@Test
fun `should return same range as stønadsperiode when it's within bounds`() {
val stønadsperiode =
Stønadsperiode.create(juli(2023)..juni(2024))
val expected = YearRange(Year.of(2023), Year.of(2024))
val actual = stønadsperiode.regnUtSkatteperiodeForStønadsperiode(fixedClock)

actual shouldBe expected
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import no.nav.su.se.bakover.common.tid.YearRange
import no.nav.su.se.bakover.domain.skatt.Skattegrunnlag

interface SkatteService {
/**
* Ikke i bruk. TODO jah: Slett?
*/
fun hentSamletSkattegrunnlag(
fnr: Fnr,
saksbehandler: NavIdentBruker.Saksbehandler,
Expand All @@ -19,6 +22,9 @@ interface SkatteService {
yearRange: YearRange,
): Skattegrunnlag

/**
* Brukes for frioppslag på skattemelding.
*/
fun hentOgLagSkattePdf(request: FrioppslagSkattRequest): Either<KunneIkkeHenteOgLagePdfAvSkattegrunnlag, PdfA>
fun hentLagOgJournalførSkattePdf(request: FrioppslagSkattRequest): Either<KunneIkkeGenerereSkattePdfOgJournalføre, PdfA>
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import no.nav.su.se.bakover.common.persistence.SessionFactory
import no.nav.su.se.bakover.common.persistence.TransactionContext
import no.nav.su.se.bakover.common.person.Fnr
import no.nav.su.se.bakover.common.tid.YearRange
import no.nav.su.se.bakover.common.tid.krympTilØvreGrense
import no.nav.su.se.bakover.common.tid.toRange
import no.nav.su.se.bakover.domain.Sak
import no.nav.su.se.bakover.domain.behandling.BehandlingMetrics
import no.nav.su.se.bakover.domain.grunnlag.Bosituasjon.Companion.harEPS
Expand All @@ -29,6 +27,7 @@ import no.nav.su.se.bakover.domain.revurdering.vilkår.bosituasjon.LeggTilBositu
import no.nav.su.se.bakover.domain.sak.SakService
import no.nav.su.se.bakover.domain.sak.lagNyUtbetaling
import no.nav.su.se.bakover.domain.skatt.Skattegrunnlag
import no.nav.su.se.bakover.domain.skatt.regnUtSkatteperiodeForStønadsperiode
import no.nav.su.se.bakover.domain.statistikk.StatistikkEvent
import no.nav.su.se.bakover.domain.statistikk.StatistikkEventObserver
import no.nav.su.se.bakover.domain.statistikk.notify
Expand Down Expand Up @@ -98,7 +97,6 @@ import satser.domain.SatsFactory
import vilkår.formue.domain.FormuegrenserFactory
import økonomi.domain.utbetaling.UtbetalingsinstruksjonForEtterbetalinger
import java.time.Clock
import java.time.Year
import java.util.UUID
import kotlin.reflect.KClass

Expand Down Expand Up @@ -222,11 +220,11 @@ class SøknadsbehandlingServiceImpl(
søknadsbehandlingRepo.hent(behandlingId)
?: throw IllegalArgumentException("Søknadsbehandling send til attestering: Fant ikke søknadsbehandling med id $behandlingId. Avbryter handlingen.")
).let {
it as? KanSendesTilAttestering
?: return KunneIkkeSendeSøknadsbehandlingTilAttestering.UgyldigTilstand(
it::class,
).left()
}
it as? KanSendesTilAttestering
?: return KunneIkkeSendeSøknadsbehandlingTilAttestering.UgyldigTilstand(
it::class,
).left()
}
return søknadsbehandlingSomKanSendesTilAttestering.tilAttestering(
saksbehandler = request.saksbehandler,
fritekstTilBrev = request.fritekstTilBrev,
Expand Down Expand Up @@ -288,10 +286,10 @@ class SøknadsbehandlingServiceImpl(
søknadsbehandlingRepo.hent(request.behandlingId)
?: return KunneIkkeUnderkjenneSøknadsbehandling.FantIkkeBehandling.left()
).let {
it as? SøknadsbehandlingTilAttestering ?: return KunneIkkeUnderkjenneSøknadsbehandling.UgyldigTilstand(
it::class,
).left()
}
it as? SøknadsbehandlingTilAttestering ?: return KunneIkkeUnderkjenneSøknadsbehandling.UgyldigTilstand(
it::class,
).left()
}
return søknadsbehandling.tilUnderkjent(request.attestering).map { underkjent ->
val aktørId = personService.hentAktørId(underkjent.fnr).getOrElse {
log.error("Fant ikke aktør-id for sak: ${underkjent.id}")
Expand Down Expand Up @@ -787,7 +785,8 @@ class SøknadsbehandlingServiceImpl(
saksbehandler: NavIdentBruker.Saksbehandler,
samletSkattegrunnlag: (Fnr, NavIdentBruker.Saksbehandler, YearRange) -> Skattegrunnlag,
clock: Clock,
): Skattegrunnlag = samletSkattegrunnlag(fnr, saksbehandler, getYearRangeForSkatt(clock))
): Skattegrunnlag =
samletSkattegrunnlag(fnr, saksbehandler, stønadsperiode.regnUtSkatteperiodeForStønadsperiode(clock))

private fun Søknadsbehandling.hentSkattegrunnlagForEps(
saksbehandler: NavIdentBruker.Saksbehandler,
Expand All @@ -797,15 +796,9 @@ class SøknadsbehandlingServiceImpl(
samletSkattegrunnlag(
grunnlagsdata.bosituasjon.singleFullstendigEpsOrNull()!!.fnr,
saksbehandler,
getYearRangeForSkatt(clock),
stønadsperiode.regnUtSkatteperiodeForStønadsperiode(clock),
)
} else {
null
}

private fun Søknadsbehandling.getYearRangeForSkatt(clock: Clock): YearRange {
return Year.now(clock).minusYears(1).let {
stønadsperiode?.toYearRange()?.krympTilØvreGrense(it) ?: it.toRange()
}
}
}

0 comments on commit 7523bc0

Please sign in to comment.