diff --git a/apps/web/process/payments/get-kp-data.ts b/apps/web/process/payments/get-kp-data.ts index 7d403be..69823ef 100644 --- a/apps/web/process/payments/get-kp-data.ts +++ b/apps/web/process/payments/get-kp-data.ts @@ -1,10 +1,12 @@ import type { GetQuoteInputData, GetQuoteProcessData } from '../load-kp/types'; +import { generateDegressionRows } from './lib/degression-tools'; import initializeApollo from '@/apollo/client'; import defaultValues from '@/config/default-values'; import * as CRMTypes from '@/graphql/crm.types'; import { gql } from '@apollo/client'; import { sort } from 'radash'; +// eslint-disable-next-line @typescript-eslint/no-unused-vars const QUERY_GET_QUOTE_PAYMENTS_DATA = gql` query GetQuotePaymentsData($quoteId: Uuid!) { quote(quoteId: $quoteId) { @@ -27,6 +29,7 @@ const QUERY_GET_QUOTE_PAYMENTS_DATA = gql` } `; +// eslint-disable-next-line sonarjs/cognitive-complexity export async function getKPData({ values: { quote: quoteId, recalcWithRevision }, }: GetQuoteInputData): Promise { @@ -45,7 +48,13 @@ export async function getKPData({ ? Math.min(quote?.evo_period ?? 0, quote?.evo_accept_period ?? 0) : quote?.evo_period ?? 0; + const graphType = quote?.evo_graph_type ?? defaultValues.graphType; + const firstPaymentPerc = quote?.evo_first_payment_perc ?? defaultValues.firstPaymentPerc; + const lastPaymentPerc = quote?.evo_last_payment_perc ?? defaultValues.lastPaymentPerc; + const seasonType = quote?.evo_seasons_type; + let paymentsValues: number[] = []; + if (quote?.evo_graphs) { paymentsValues = sort(quote?.evo_graphs, (evo_graph) => Date.parse(evo_graph?.createdon ?? '0')) @@ -55,6 +64,19 @@ export async function getKPData({ .map((payment) => payment?.evo_payment_ratio || 0) || []; } + const isDegression = + graphType === 100_000_001 && + seasonType !== null && + seasonType !== undefined && + seasonType !== 100_000_007; + + if (recalcWithRevision && isDegression) { + paymentsValues = generateDegressionRows({ + leasingPeriod, + seasonType, + }).map((x) => x.value); + } + return { payments: { values: [ @@ -64,14 +86,14 @@ export async function getKPData({ ], }, values: { - firstPaymentPerc: quote?.evo_first_payment_perc ?? defaultValues.firstPaymentPerc, - graphType: quote?.evo_graph_type ?? defaultValues.graphType, + firstPaymentPerc, + graphType, highSeasonStart: quote?.evo_high_season, - lastPaymentPerc: quote?.evo_last_payment_perc ?? defaultValues.lastPaymentPerc, + lastPaymentPerc, leasingPeriod, parmentsDecreasePercent: quote?.evo_payments_decrease_perc ?? defaultValues.parmentsDecreasePercent, - seasonType: quote?.evo_seasons_type, + seasonType, }, }; } diff --git a/apps/web/process/payments/lib/degression-tools.ts b/apps/web/process/payments/lib/degression-tools.ts new file mode 100644 index 0000000..846b69c --- /dev/null +++ b/apps/web/process/payments/lib/degression-tools.ts @@ -0,0 +1,75 @@ +import type { CalculationValues } from '@/stores/calculation/values/types'; +import type { Row } from '@/stores/tables/payments/types'; + +const degressionSteps: { [key: number]: number[] } = { + 100_000_003: [100, 50, 25], + 100_000_004: [100, 30, 10], + 100_000_005: [100, 70, 40], + 100_000_006: [100, 7, 3], +}; + +export function generateDegressionRows({ + seasonType: degressionType, + leasingPeriod, +}: Pick) { + let middlePayments: Row[] = []; + + switch (degressionType) { + case 100_000_007: { + const editablePayments: Row[] = Array.from( + { + length: leasingPeriod - 3, + }, + () => ({ + status: 'Default', + value: 100, + }) + ); + + middlePayments = [ + { + status: 'Disabled', + value: 100, + }, + ...editablePayments, + ]; + + break; + } + case 100_000_003: + case 100_000_004: + case 100_000_005: + case 100_000_006: { + const [step1, step2, step3] = degressionSteps[degressionType]; + const paymentsInStep = Math.ceil((leasingPeriod - 2) / 3); + + middlePayments = Array.from( + { + length: leasingPeriod - 2, + }, + (_v, i) => { + let value = step3; + + if (i <= paymentsInStep * 2 - 1) { + value = step2; + } + + if (i <= paymentsInStep - 1) { + value = step1; + } + + return { + status: 'Disabled', + value, + }; + } + ); + break; + } + default: { + break; + } + } + + return middlePayments; +} diff --git a/apps/web/process/payments/reactions/common.ts b/apps/web/process/payments/reactions/common.ts index 310cd4b..92719be 100644 --- a/apps/web/process/payments/reactions/common.ts +++ b/apps/web/process/payments/reactions/common.ts @@ -1,3 +1,4 @@ +import * as degressionTools from '../lib/degression-tools'; import * as seasonsConstants from '../lib/seasons-constants'; import * as seasonsTools from '../lib/seasons-tools'; import { selectHighSeasonStart } from '@/config/default-options'; @@ -244,108 +245,39 @@ export default function reactions({ store }: ProcessContext) { // } // ); - const degressionSteps: { [key: number]: number[] } = { - 100_000_003: [100, 50, 25], - 100_000_004: [100, 30, 10], - 100_000_005: [100, 70, 40], - 100_000_006: [100, 7, 3], - }; + makeDisposable( + () => + reaction( + () => $calculation.$values.getValues(['leasingPeriod', 'seasonType']), + ({ seasonType, leasingPeriod }) => { + const middlePayments: Row[] = degressionTools.generateDegressionRows({ + leasingPeriod, + seasonType, + }); - reaction( - () => { - const degressionType = $calculation.element('selectSeasonType').getValue(); - const leasingPeriod = $calculation.element('tbxLeasingPeriod').getValue(); - const graphType = $calculation.element('radioGraphType').getValue(); + const firstPaymentPerc = $calculation.element('tbxFirstPaymentPerc').getValue(); + const lastPaymentPerc = $calculation.element('tbxLastPaymentPerc').getValue(); - return { - degressionType, - graphType, - leasingPeriod, - }; - }, - ({ degressionType, leasingPeriod, graphType }) => { - if (graphType === 100_000_001) { - let middlePayments: Row[] = []; + const rows: Row[] = [ + { + status: 'Disabled', + value: firstPaymentPerc, + }, + ...middlePayments, + { + status: 'Disabled', + value: lastPaymentPerc, + }, + ]; - switch (degressionType) { - case 100_000_007: { - const editablePayments: Row[] = Array.from( - { - length: leasingPeriod - 3, - }, - () => ({ - status: 'Default', - value: 100, - }) - ); - - middlePayments = [ - { - status: 'Disabled', - value: 100, - }, - ...editablePayments, - ]; - - break; + if (!$process.has('LoadKP')) { + $tables.payments.setValues(rows.map((row) => row.value)); } - case 100_000_003: - case 100_000_004: - case 100_000_005: - case 100_000_006: { - const [step1, step2, step3] = degressionSteps[degressionType]; - const paymentsInStep = Math.ceil((leasingPeriod - 2) / 3); - middlePayments = Array.from( - { - length: leasingPeriod - 2, - }, - (_v, i) => { - let value = step3; - - if (i <= paymentsInStep * 2 - 1) { - value = step2; - } - - if (i <= paymentsInStep - 1) { - value = step1; - } - - return { - status: 'Disabled', - value, - }; - } - ); - break; - } - default: { - break; - } + $tables.payments.setStatuses(rows.map((row) => row.status)); } - - const firstPaymentPerc = $calculation.element('tbxFirstPaymentPerc').getValue(); - const lastPaymentPerc = $calculation.element('tbxLastPaymentPerc').getValue(); - - const rows: Row[] = [ - { - status: 'Disabled', - value: firstPaymentPerc, - }, - ...middlePayments, - { - status: 'Disabled', - value: lastPaymentPerc, - }, - ]; - - if (!$process.has('LoadKP')) { - $tables.payments.setValues(rows.map((row) => row.value)); - } - - $tables.payments.setStatuses(rows.map((row) => row.status)); - } - } + ), + () => $calculation.element('radioGraphType').getValue() !== 100_000_001 ); makeDisposable(