|
| 1 | +package no.nav.ung.sak.domene.behandling.steg.uttak; |
| 2 | + |
| 3 | +import jakarta.enterprise.context.Dependent; |
| 4 | +import jakarta.inject.Inject; |
| 5 | +import no.nav.fpsak.tidsserie.LocalDateSegment; |
| 6 | +import no.nav.fpsak.tidsserie.LocalDateTimeline; |
| 7 | +import no.nav.fpsak.tidsserie.StandardCombinators; |
| 8 | +import no.nav.ung.sak.domene.arbeidsforhold.InntektArbeidYtelseTjeneste; |
| 9 | +import no.nav.ung.sak.domene.behandling.steg.uttak.regler.InntektType; |
| 10 | +import no.nav.ung.sak.domene.behandling.steg.uttak.regler.RapportertInntekt; |
| 11 | +import no.nav.ung.sak.domene.iay.modell.InntektArbeidYtelseGrunnlag; |
| 12 | +import no.nav.ung.sak.domene.iay.modell.OppgittOpptjening; |
| 13 | +import org.jetbrains.annotations.NotNull; |
| 14 | + |
| 15 | +import java.time.LocalDateTime; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.Comparator; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Set; |
| 20 | + |
| 21 | +@Dependent |
| 22 | +public class RapportertInntektMapper { |
| 23 | + |
| 24 | + private InntektArbeidYtelseTjeneste inntektArbeidYtelseTjeneste; |
| 25 | + |
| 26 | + @Inject |
| 27 | + public RapportertInntektMapper(InntektArbeidYtelseTjeneste inntektArbeidYtelseTjeneste) { |
| 28 | + this.inntektArbeidYtelseTjeneste = inntektArbeidYtelseTjeneste; |
| 29 | + } |
| 30 | + |
| 31 | + public LocalDateTimeline<Set<RapportertInntekt>> map(Long behandlingId) { |
| 32 | + // Henter iay-grunnlag (kall til abakus) |
| 33 | + final var iayGrunnlag = inntektArbeidYtelseTjeneste.hentGrunnlag(behandlingId); |
| 34 | + // Finner rapporterte inntekter pr journalpost sortert på mottattdato med siste mottatt journalpost først |
| 35 | + final var sorterteInntekttidslinjerPåMottattdato = finnSorterteInntektstidslinjer(iayGrunnlag); |
| 36 | + |
| 37 | + var resultatTidslinje = new LocalDateTimeline<Set<RapportertInntekt>>(List.of()); |
| 38 | + for (InntektForMottattidspunkt journalpostMedInntekttidslinje : sorterteInntekttidslinjerPåMottattdato) { |
| 39 | + // Dersom vi ikke allerede har lagt til en rapportert inntekt for perioden legger vi den til i resultattidslinjen |
| 40 | + if (!resultatTidslinje.intersects(journalpostMedInntekttidslinje.tidslinje())) { |
| 41 | + // Trenger ikkje å håndtere overlapp sidan vi aldri går inn her med overlapp |
| 42 | + resultatTidslinje = resultatTidslinje.crossJoin(journalpostMedInntekttidslinje.tidslinje()); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + return resultatTidslinje; |
| 47 | + } |
| 48 | + |
| 49 | + private List<InntektForMottattidspunkt> finnSorterteInntektstidslinjer(InntektArbeidYtelseGrunnlag iayGrunnlag) { |
| 50 | + return iayGrunnlag.getOppgittOpptjeningAggregat() |
| 51 | + .stream() |
| 52 | + .flatMap(o -> o.getOppgitteOpptjeninger().stream().map(RapportertInntektMapper::finnInntekterPrMottattidspunkt)) |
| 53 | + .sorted(Comparator.reverseOrder()) |
| 54 | + .toList(); |
| 55 | + } |
| 56 | + |
| 57 | + private static InntektForMottattidspunkt finnInntekterPrMottattidspunkt(OppgittOpptjening o) { |
| 58 | + final var res = new ArrayList<LocalDateSegment<Set<RapportertInntekt>>>(); |
| 59 | + res.addAll(finnArbeidOgFrilansSegmenter(o)); |
| 60 | + res.addAll(finnNæringssegmenter(o)); |
| 61 | + return new InntektForMottattidspunkt(o.getInnsendingstidspunkt(), new LocalDateTimeline<>(res, StandardCombinators::union)); |
| 62 | + } |
| 63 | + |
| 64 | + private static List<LocalDateSegment<Set<RapportertInntekt>>> finnNæringssegmenter(OppgittOpptjening o) { |
| 65 | + return o.getEgenNæring().stream() |
| 66 | + .map(it -> new LocalDateSegment<>( |
| 67 | + it.getPeriode().toLocalDateInterval(), |
| 68 | + Set.of(new RapportertInntekt( |
| 69 | + InntektType.SELVSTENDIG_NÆRINGSDRIVENDE, |
| 70 | + it.getBruttoInntekt()) |
| 71 | + ))).toList(); |
| 72 | + } |
| 73 | + |
| 74 | + private static List<LocalDateSegment<Set<RapportertInntekt>>> finnArbeidOgFrilansSegmenter(OppgittOpptjening o) { |
| 75 | + return o.getOppgittArbeidsforhold().stream() |
| 76 | + .map(it -> new LocalDateSegment<>( |
| 77 | + it.getPeriode().toLocalDateInterval(), |
| 78 | + Set.of(new RapportertInntekt( |
| 79 | + InntektType.ARBEIDSTAKER_ELLER_FRILANSER, |
| 80 | + it.getInntekt()) |
| 81 | + ))).toList(); |
| 82 | + } |
| 83 | + |
| 84 | + private record InntektForMottattidspunkt(LocalDateTime mottattTidspunkt, |
| 85 | + LocalDateTimeline<Set<RapportertInntekt>> tidslinje |
| 86 | + ) implements Comparable<InntektForMottattidspunkt> { |
| 87 | + |
| 88 | + @Override |
| 89 | + public int compareTo(@NotNull InntektForMottattidspunkt o) { |
| 90 | + return this.mottattTidspunkt.compareTo(o.mottattTidspunkt); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments