diff --git a/config/default-options.ts b/config/default-options.ts index 097cf84..17bca62 100644 --- a/config/default-options.ts +++ b/config/default-options.ts @@ -1,6 +1,41 @@ import { alphabetical } from 'radash/dist/array'; import type { CalculationOptions } from 'stores/calculation/options/types'; +export const selectSeasonType = [ + { + label: '6/6', + value: 100_000_000, + }, + { + label: '8/4', + value: 100_000_001, + }, + { + label: '4/4/4', + value: 100_000_002, + }, + { + label: 'Гибкая дегрессия', + value: 100_000_007, + }, + { + label: '100.50.25', + value: 100_000_003, + }, + { + label: '100.30.10', + value: 100_000_004, + }, + { + label: '100.70.40', + value: 100_000_005, + }, + { + label: '100.7.3', + value: 100_000_006, + }, +]; + const defaultOptions: CalculationOptions = { radioLastPaymentRule: [ { @@ -51,40 +86,7 @@ const defaultOptions: CalculationOptions = { }, ], - selectSeasonType: [ - { - label: '6/6', - value: 100_000_000, - }, - { - label: '8/4', - value: 100_000_001, - }, - { - label: '4/4/4', - value: 100_000_002, - }, - { - label: 'Гибкая дегрессия', - value: 100_000_007, - }, - { - label: '100.50.25', - value: 100_000_003, - }, - { - label: '100.30.10', - value: 100_000_004, - }, - { - label: '100.70.40', - value: 100_000_005, - }, - { - label: '100.7.3', - value: 100_000_006, - }, - ], + selectSeasonType, selectHighSeasonStart: Array.from( { diff --git a/config/default-values.ts b/config/default-values.ts index 16308b6..2db3c13 100644 --- a/config/default-values.ts +++ b/config/default-values.ts @@ -23,7 +23,7 @@ const defaultValues: CalculationValues = { balanceHolder: 100_000_001, graphType: 100_000_000, parmentsDecreasePercent: 94, - seasonType: 100_000_000, + seasonType: null, highSeasonStart: 100_000_000, comissionPerc: 0, comissionRub: 0, diff --git a/process/payments/reactions.ts b/process/payments/reactions.ts index dcc4fb1..9dd1841 100644 --- a/process/payments/reactions.ts +++ b/process/payments/reactions.ts @@ -1,5 +1,8 @@ +/* eslint-disable function-paren-newline */ +/* eslint-disable implicit-arrow-linebreak */ import type { ApolloClient } from '@apollo/client'; import type { QueryClient } from '@tanstack/react-query'; +import { selectSeasonType } from 'config/default-options'; import { reaction, toJS } from 'mobx'; import { sort } from 'radash/dist/array'; import type RootStore from 'stores/root'; @@ -207,4 +210,131 @@ export default function paymentsReactions( } else if (removeError) removeError(); } ); + + /** + * Дегрессия + */ + const mapGraphTypeToSeasonTypes: { [key: number]: Array } = { + 100_000_001: [100_000_003, 100_000_004, 100_000_005, 100_000_006, 100_000_007], + 100_000_003: [100_000_000, 100_000_001, 100_000_002], + }; + + reaction( + () => { + const graphType = $calculation.getElementValue('radioGraphType'); + + return graphType; + }, + (graphType) => { + if (!graphType) { + $calculation.setElementOptions('selectSeasonType', []); + + return; + } + + const allowedSeasonTypes = mapGraphTypeToSeasonTypes[graphType]; + const selectSeasonTypeOptions = selectSeasonType.filter((option) => + allowedSeasonTypes.includes(option.value) + ); + $calculation.setElementOptions('selectSeasonType', selectSeasonTypeOptions); + } + ); + + const degressionSteps: { [key: number]: Array } = { + 100_000_003: [100, 50, 25], + 100_000_004: [100, 30, 10], + 100_000_005: [100, 70, 40], + 100_000_006: [100, 7, 3], + }; + + 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[] = []; + + 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([ + { + value: firstPaymentPerc, + status: 'Disabled', + }, + ...payments, + { + value: lastPaymentPerc, + status: 'Disabled', + }, + ]); + } + } + ); } diff --git a/stores/calculation/options/index.ts b/stores/calculation/options/index.ts index 0043b38..3b2811c 100644 --- a/stores/calculation/options/index.ts +++ b/stores/calculation/options/index.ts @@ -34,7 +34,7 @@ export default class OptionsStore { if ( // eslint-disable-next-line operator-linebreak !this.options[elementName]?.length || - this.options[elementName].some((x) => x.value === value) + !this.options[elementName].some((x) => x.value === value) ) { this.root.$calculation.resetElementValue(elementName); }