diff --git a/process/payments/reactions.ts b/process/payments/reactions.ts index 4bf4937..db4d627 100644 --- a/process/payments/reactions.ts +++ b/process/payments/reactions.ts @@ -9,6 +9,7 @@ import type { CalculationOptions } from 'stores/calculation/options/types'; import type RootStore from 'stores/root'; import type { Row } from 'stores/tables/payments/types'; import { difference } from 'tools/array'; +import { makeDisposable } from 'tools/mobx'; import * as seasonsConstants from './lib/seasons-constants'; import * as seasonsTools from './lib/seasons-tools'; import validatePaymentsTable from './validation'; @@ -18,7 +19,7 @@ export default function paymentsReactions( apolloClient: ApolloClient, queryClient: QueryClient ) { - const { $calculation, $tables } = store; + const { $calculation, $tables, $process } = store; reaction( () => $calculation.getElementValue('tbxFirstPaymentPerc'), @@ -38,155 +39,169 @@ export default function paymentsReactions( /** * Аннуитет */ - reaction( - () => { - const graphType = $calculation.getElementValue('radioGraphType'); - const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod'); + makeDisposable( + () => + reaction( + () => { + const graphType = $calculation.getElementValue('radioGraphType'); + const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod'); - return { - graphType, - leasingPeriod, - }; - }, - ({ graphType, leasingPeriod }) => { - if (graphType === 100_000_000) { - const middlePayments: Row[] = Array.from( - { - length: leasingPeriod - 2, - }, - () => ({ - value: 100, - status: 'Disabled', - }) - ); + return { + graphType, + leasingPeriod, + }; + }, + ({ graphType, leasingPeriod }) => { + if (graphType === 100_000_000) { + const middlePayments: Row[] = Array.from( + { + length: leasingPeriod - 2, + }, + () => ({ + value: 100, + status: 'Disabled', + }) + ); - const firstPaymentPerc = $calculation.getElementValue('tbxFirstPaymentPerc'); - const lastPaymentPerc = $calculation.getElementValue('tbxLastPaymentPerc'); + const firstPaymentPerc = $calculation.getElementValue('tbxFirstPaymentPerc'); + const lastPaymentPerc = $calculation.getElementValue('tbxLastPaymentPerc'); - $tables.payments.setRows([ - { - value: firstPaymentPerc, - status: 'Disabled', - }, - ...middlePayments, - { - value: lastPaymentPerc, - status: 'Disabled', - }, - ]); - } - }, - { - fireImmediately: true, - } + $tables.payments.setRows([ + { + value: firstPaymentPerc, + status: 'Disabled', + }, + ...middlePayments, + { + value: lastPaymentPerc, + status: 'Disabled', + }, + ]); + } + }, + { + fireImmediately: true, + } + ), + () => $process.has('LoadKP') ); /** * Равноубывающий */ - reaction( - () => { - const graphType = $calculation.getElementValue('radioGraphType'); - const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod'); - const parmentsDecreasePercent = $calculation.getElementValue('tbxParmentsDecreasePercent'); + makeDisposable( + () => + reaction( + () => { + const graphType = $calculation.getElementValue('radioGraphType'); + const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod'); + const parmentsDecreasePercent = $calculation.getElementValue( + 'tbxParmentsDecreasePercent' + ); - return { - graphType, - leasingPeriod, - parmentsDecreasePercent, - }; - }, - ({ graphType, leasingPeriod, parmentsDecreasePercent }) => { - if (graphType === 100_000_002) { - const middlePayments: Row[] = Array.from( - { - length: leasingPeriod - 2, - }, - (_, k) => { - const payment = 100 * (parmentsDecreasePercent / 100) ** k; + return { + graphType, + leasingPeriod, + parmentsDecreasePercent, + }; + }, + ({ graphType, leasingPeriod, parmentsDecreasePercent }) => { + if (graphType === 100_000_002) { + const middlePayments: Row[] = Array.from( + { + length: leasingPeriod - 2, + }, + (_, k) => { + const payment = 100 * (parmentsDecreasePercent / 100) ** k; - return { - value: Number(payment.toFixed(2)), - status: 'Disabled', - }; + return { + value: Number(payment.toFixed(2)), + status: 'Disabled', + }; + } + ); + + const firstPaymentPerc = $calculation.getElementValue('tbxFirstPaymentPerc'); + const lastPaymentPerc = $calculation.getElementValue('tbxLastPaymentPerc'); + + $tables.payments.setRows([ + { + value: firstPaymentPerc, + status: 'Disabled', + }, + ...middlePayments, + { + value: lastPaymentPerc, + status: 'Disabled', + }, + ]); } - ); - - const firstPaymentPerc = $calculation.getElementValue('tbxFirstPaymentPerc'); - const lastPaymentPerc = $calculation.getElementValue('tbxLastPaymentPerc'); - - $tables.payments.setRows([ - { - value: firstPaymentPerc, - status: 'Disabled', - }, - ...middlePayments, - { - value: lastPaymentPerc, - status: 'Disabled', - }, - ]); - } - } + } + ), + () => $process.has('LoadKP') ); /** * Легкий старт */ - reaction( - () => { - const graphType = $calculation.getElementValue('radioGraphType'); - const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod'); + makeDisposable( + () => + reaction( + () => { + const graphType = $calculation.getElementValue('radioGraphType'); + const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod'); - return { - graphType, - leasingPeriod, - }; - }, - ({ graphType, leasingPeriod }) => { - if (graphType === 100_000_004) { - const editablePayments: Row[] = [ - { - value: 25, - status: 'Default', - }, - { - value: 50, - status: 'Default', - }, - { - value: 75, - status: 'Default', - }, - ]; + return { + graphType, + leasingPeriod, + }; + }, + ({ graphType, leasingPeriod }) => { + if (graphType === 100_000_004) { + const editablePayments: Row[] = [ + { + value: 25, + status: 'Default', + }, + { + value: 50, + status: 'Default', + }, + { + value: 75, + status: 'Default', + }, + ]; - const payments: Row[] = Array.from( - { - length: leasingPeriod - 5, - }, - () => ({ - value: 100, - status: 'Disabled', - }) - ); + const payments: Row[] = Array.from( + { + length: leasingPeriod - 5, + }, + () => ({ + value: 100, + status: 'Disabled', + }) + ); - const firstPaymentPerc = $calculation.getElementValue('tbxFirstPaymentPerc'); - const lastPaymentPerc = $calculation.getElementValue('tbxLastPaymentPerc'); + const firstPaymentPerc = $calculation.getElementValue('tbxFirstPaymentPerc'); + const lastPaymentPerc = $calculation.getElementValue('tbxLastPaymentPerc'); - $tables.payments.setRows([ - { - value: firstPaymentPerc, - status: 'Disabled', - }, - ...editablePayments, - ...payments, - { - value: lastPaymentPerc, - status: 'Disabled', - }, - ]); - } - } + $tables.payments.setRows([ + { + value: firstPaymentPerc, + status: 'Disabled', + }, + ...editablePayments, + ...payments, + { + value: lastPaymentPerc, + status: 'Disabled', + }, + ]); + } + } + ), + () => $process.has('LoadKP') ); /** @@ -227,133 +242,141 @@ export default function paymentsReactions( 100_000_006: [100, 7, 3], }; - reaction( - () => { - const degressionType = $calculation.getElementValue('selectSeasonType'); - const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod'); - const graphType = $calculation.getElementValue('radioGraphType'); + makeDisposable( + () => + reaction( + () => { + const degressionType = $calculation.getElementValue('selectSeasonType'); + const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod'); + const graphType = $calculation.getElementValue('radioGraphType'); - return { - degressionType, - leasingPeriod, - graphType, - }; - }, - ({ degressionType, leasingPeriod, graphType }) => { - if (graphType === 100_000_001) { - let payments: Row[] = []; + return { + degressionType, + leasingPeriod, + graphType, + }; + }, + ({ degressionType, leasingPeriod, graphType }) => { + if (graphType === 100_000_001) { + let payments: Row[] = []; - switch (degressionType) { - case 100_000_007: { - const editablePayments: Row[] = Array.from( + switch (degressionType) { + case 100_000_007: { + const editablePayments: Row[] = Array.from( + { + length: leasingPeriod - 3, + }, + () => ({ + value: 100, + status: 'Default', + }) + ); + + payments = [ + { + value: 100, + status: 'Disabled', + }, + ...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); + + payments = Array.from( + { + length: leasingPeriod - 2, + }, + (_v, i) => { + let value = step3; + + if (i <= paymentsInStep * 2 - 1) { + value = step2; + } + + if (i <= paymentsInStep - 1) { + value = step1; + } + + return { + value, + status: 'Disabled', + }; + } + ); + break; + } + default: { + break; + } + } + + const firstPaymentPerc = $calculation.getElementValue('tbxFirstPaymentPerc'); + const lastPaymentPerc = $calculation.getElementValue('tbxLastPaymentPerc'); + + $tables.payments.setRows([ { - length: leasingPeriod - 3, - }, - () => ({ - value: 100, - status: 'Default', - }) - ); - - payments = [ - { - value: 100, + value: firstPaymentPerc, status: 'Disabled', }, - ...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); - - payments = Array.from( + ...payments, { - length: leasingPeriod - 2, + value: lastPaymentPerc, + status: 'Disabled', }, - (_v, i) => { - let value = step3; - - if (i <= paymentsInStep * 2 - 1) { - value = step2; - } - - if (i <= paymentsInStep - 1) { - value = step1; - } - - return { - value, - status: 'Disabled', - }; - } - ); - break; - } - default: { - break; + ]); } } - - const firstPaymentPerc = $calculation.getElementValue('tbxFirstPaymentPerc'); - const lastPaymentPerc = $calculation.getElementValue('tbxLastPaymentPerc'); - - $tables.payments.setRows([ - { - value: firstPaymentPerc, - status: 'Disabled', - }, - ...payments, - { - value: lastPaymentPerc, - status: 'Disabled', - }, - ]); - } - } + ), + () => $process.has('LoadKP') ); - reaction( - () => { - const graphType = $calculation.getElementValue('radioGraphType'); - const payments = toJS($tables.payments.values); - const degressionType = $calculation.getElementValue('selectSeasonType'); + makeDisposable( + () => + reaction( + () => { + const graphType = $calculation.getElementValue('radioGraphType'); + const payments = toJS($tables.payments.values); + const degressionType = $calculation.getElementValue('selectSeasonType'); - return { - graphType, - payments, - degressionType, - }; - }, - (nextParams, prevParams) => { - if (nextParams.graphType === 100_000_001 && nextParams.degressionType === 100_000_007) { - const changes = difference(nextParams.payments, prevParams.payments); + return { + graphType, + payments, + degressionType, + }; + }, + (nextParams, prevParams) => { + if (nextParams.graphType === 100_000_001 && nextParams.degressionType === 100_000_007) { + const changes = difference(nextParams.payments, prevParams.payments); - if (changes === null || changes.length > 1) return; + if (changes === null || changes.length > 1) return; - const [changeIndex] = changes; - const value = nextParams.payments[changeIndex]; - const payments = nextParams.payments.slice(1, -1).map((payment, i) => { - if (i <= changeIndex - 2) return payment; + const [changeIndex] = changes; + const value = nextParams.payments[changeIndex]; + const payments = nextParams.payments.slice(1, -1).map((payment, i) => { + if (i <= changeIndex - 2) return payment; - return value; - }); + return value; + }); - const firstPaymentPerc = $calculation.getElementValue('tbxFirstPaymentPerc'); - const lastPaymentPerc = $calculation.getElementValue('tbxLastPaymentPerc'); + const firstPaymentPerc = $calculation.getElementValue('tbxFirstPaymentPerc'); + const lastPaymentPerc = $calculation.getElementValue('tbxLastPaymentPerc'); - $tables.payments.setValues([firstPaymentPerc, ...payments, lastPaymentPerc]); - } - }, - { - delay: 50, - equals: comparer.structural, - } + $tables.payments.setValues([firstPaymentPerc, ...payments, lastPaymentPerc]); + } + }, + { + delay: 50, + equals: comparer.structural, + } + ), + () => $process.has('LoadKP') ); /** @@ -418,109 +441,117 @@ export default function paymentsReactions( return rows; } - reaction( - () => { - const seasonType = $calculation.getElementValue('selectSeasonType'); - const highSeasonStartOption = $calculation.getElementOption('selectHighSeasonStart'); - const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod'); + makeDisposable( + () => + reaction( + () => { + const seasonType = $calculation.getElementValue('selectSeasonType'); + const highSeasonStartOption = $calculation.getElementOption('selectHighSeasonStart'); + const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod'); - return { - seasonType, - highSeasonStartOption, - leasingPeriod, - }; - }, - ({ seasonType, highSeasonStartOption, leasingPeriod }) => { - const graphType = $calculation.getElementValue('radioGraphType'); - if (graphType !== 100_000_003) return; - - if (!seasonType || !highSeasonStartOption) { - $tables.payments.setValues([]); - - return; - } - - const seasons = seasonsTools.generateSeasons( - seasonType, - seasonsConstants.DEFAULT_SEASONS_VALUES - ); - const shiftNumber = Number.parseInt(highSeasonStartOption.label, 10) - 2; - const payments = seasonsTools.generateSeasonsPayments( - leasingPeriod, - shift(seasons, shiftNumber) - ); - const rows: Row[] = generateSeasonPaymentsRows(seasonType, shiftNumber, payments); - const firstPaymentPerc = $calculation.getElementValue('tbxFirstPaymentPerc'); - const lastPaymentPerc = $calculation.getElementValue('tbxLastPaymentPerc'); - - $tables.payments.setRows([ - { - value: firstPaymentPerc, - status: 'Disabled', + return { + seasonType, + highSeasonStartOption, + leasingPeriod, + }; }, - ...rows, - { - value: lastPaymentPerc, - status: 'Disabled', - }, - ]); - } + ({ seasonType, highSeasonStartOption, leasingPeriod }) => { + const graphType = $calculation.getElementValue('radioGraphType'); + if (graphType !== 100_000_003) return; + + if (!seasonType || !highSeasonStartOption) { + $tables.payments.setValues([]); + + return; + } + + const seasons = seasonsTools.generateSeasons( + seasonType, + seasonsConstants.DEFAULT_SEASONS_VALUES + ); + const shiftNumber = Number.parseInt(highSeasonStartOption.label, 10) - 2; + const payments = seasonsTools.generateSeasonsPayments( + leasingPeriod, + shift(seasons, shiftNumber) + ); + const rows: Row[] = generateSeasonPaymentsRows(seasonType, shiftNumber, payments); + const firstPaymentPerc = $calculation.getElementValue('tbxFirstPaymentPerc'); + const lastPaymentPerc = $calculation.getElementValue('tbxLastPaymentPerc'); + + $tables.payments.setRows([ + { + value: firstPaymentPerc, + status: 'Disabled', + }, + ...rows, + { + value: lastPaymentPerc, + status: 'Disabled', + }, + ]); + } + ), + () => $process.has('LoadKP') ); - reaction( - () => { - const payments = toJS($tables.payments.values); - const seasons = payments.slice(1, seasonsConstants.SEASONS_PERIOD_NUMBER + 1); + makeDisposable( + () => + reaction( + () => { + const payments = toJS($tables.payments.values); + const seasons = payments.slice(1, seasonsConstants.SEASONS_PERIOD_NUMBER + 1); - return seasons; - }, - (nextSeasons, prevSeasons) => { - const graphType = $calculation.getElementValue('radioGraphType'); - if (graphType !== 100_000_003) return; - - const seasonType = $calculation.getElementValue('selectSeasonType'); - const highSeasonStartOption = $calculation.getElementOption('selectHighSeasonStart'); - if (!seasonType || !highSeasonStartOption) return; - - const shiftNumber = Number.parseInt(highSeasonStartOption.label, 10) - 2; - const unshiftedNextSeasons = shift(nextSeasons, -shiftNumber); - const unshiftedPrevSeasons = shift(prevSeasons, -shiftNumber); - - const changes = difference(unshiftedNextSeasons, unshiftedPrevSeasons); - if (changes === null || changes.length > 1) return; - - const [changeIndex] = changes; - const positionIndex = seasonsTools.getPositionIndex(seasonType, changeIndex); - - const values = seasonsTools.getSeasonsValues(seasonType, unshiftedNextSeasons); - values[positionIndex] = unshiftedNextSeasons[changeIndex]; - - const seasons = seasonsTools.generateSeasons(seasonType, values); - const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod'); - const payments = seasonsTools.generateSeasonsPayments( - leasingPeriod, - shift(seasons, shiftNumber) - ); - const rows: Row[] = generateSeasonPaymentsRows(seasonType, shiftNumber, payments); - const firstPaymentPerc = $calculation.getElementValue('tbxFirstPaymentPerc'); - const lastPaymentPerc = $calculation.getElementValue('tbxLastPaymentPerc'); - - $tables.payments.setRows([ - { - value: firstPaymentPerc, - status: 'Disabled', + return seasons; }, - ...rows, - { - value: lastPaymentPerc, - status: 'Disabled', + (nextSeasons, prevSeasons) => { + const graphType = $calculation.getElementValue('radioGraphType'); + if (graphType !== 100_000_003) return; + + const seasonType = $calculation.getElementValue('selectSeasonType'); + const highSeasonStartOption = $calculation.getElementOption('selectHighSeasonStart'); + if (!seasonType || !highSeasonStartOption) return; + + const shiftNumber = Number.parseInt(highSeasonStartOption.label, 10) - 2; + const unshiftedNextSeasons = shift(nextSeasons, -shiftNumber); + const unshiftedPrevSeasons = shift(prevSeasons, -shiftNumber); + + const changes = difference(unshiftedNextSeasons, unshiftedPrevSeasons); + if (changes === null || changes.length > 1) return; + + const [changeIndex] = changes; + const positionIndex = seasonsTools.getPositionIndex(seasonType, changeIndex); + + const values = seasonsTools.getSeasonsValues(seasonType, unshiftedNextSeasons); + values[positionIndex] = unshiftedNextSeasons[changeIndex]; + + const seasons = seasonsTools.generateSeasons(seasonType, values); + const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod'); + const payments = seasonsTools.generateSeasonsPayments( + leasingPeriod, + shift(seasons, shiftNumber) + ); + const rows: Row[] = generateSeasonPaymentsRows(seasonType, shiftNumber, payments); + const firstPaymentPerc = $calculation.getElementValue('tbxFirstPaymentPerc'); + const lastPaymentPerc = $calculation.getElementValue('tbxLastPaymentPerc'); + + $tables.payments.setRows([ + { + value: firstPaymentPerc, + status: 'Disabled', + }, + ...rows, + { + value: lastPaymentPerc, + status: 'Disabled', + }, + ]); }, - ]); - }, - { - delay: 50, - equals: comparer.structural, - } + { + delay: 50, + equals: comparer.structural, + } + ), + () => $process.has('LoadKP') ); /**