import helper from '../lib/helper'; import type { ProcessContext } from '@/process/types'; import { disposableReaction } from '@/utils/mobx'; import { comparer, reaction } from 'mobx'; export default function reactions({ store, apolloClient, queryClient }: ProcessContext) { const { getFingapRisks } = helper({ apolloClient, queryClient }); const { $calculation, $tables, $process } = store; // Расчет итоговой суммы ФинГАП и запись в таблицу страхования disposableReaction( () => $process.has('LoadKP'), () => $tables.fingap.totalSum, (totalSum) => { $tables.insurance.row('fingap').setValue('insCost', totalSum); }, { delay: 50, } ); /** * Реакция на изменение Страховой компании у ФинГАП и Срока лизинга: * * Если Страхованя компания = Не выбрано, то * * Плательщик insuredFinGAP = ЛП (100 000 000) и закрыто для редактирования, * Стоимость за первый период inscostFinGAP = 0 * Срок страхования insTermFinGAP = 12 мес (100 000 000) и закрыто для редактирования * * иначе * * Плательщик insuredFinGAP = открыто для редактирования, * Стоимость за первый период inscostFinGAP = 0 * Срок страхования insTermFinGAP = Если срок лизинга tbxLeasingPeriod < 13, * то указываем Срок страхования insTermFinGAP = 12 мес * и закрываем для редактирования, иначе открыто для редактирования */ reaction( () => { const finGAPInsuranceCompany = $tables.insurance.row('fingap').getValue('insuranceCompany'); const leasingPeriod = $calculation.element('tbxLeasingPeriod').getValue(); return { finGAPInsuranceCompany, leasingPeriod, }; }, ({ finGAPInsuranceCompany, leasingPeriod }) => { // eslint-disable-next-line no-negated-condition if (!finGAPInsuranceCompany) { $tables.insurance.row('fingap').column('insured').setValue(100_000_000).block(); $tables.insurance.row('fingap').column('insCost').setValue(0); $tables.insurance.row('fingap').column('insTerm').setValue(100_000_000); } else { $tables.insurance.row('fingap').column('insured').unblock(); if (leasingPeriod < 16) { $tables.insurance.row('fingap').column('insTerm').setValue(100_000_001); } else { $tables.insurance.row('fingap').column('insTerm').setValue(100_000_000); } } }, { equals: comparer.shallow, fireImmediately: true, } ); // Очищаем таблицу ФинГАП, если не выбрана страховая компания reaction( () => $tables.insurance.row('fingap').getValue('insuranceCompany'), (finGAPInsuranceCompany) => { if (!finGAPInsuranceCompany) { $tables.fingap.clear(); } } ); // Заполнение таблицы рисков ФинГАП + Запрос расчета финГАП в мозжечок disposableReaction( () => $process.has('Calculate'), () => { const finGAPInsuranceCompany = $tables.insurance.row('fingap').getValue('insuranceCompany'); const paymentsValues = $tables.payments.getValues; const plPriceRub = $calculation.$values.getValue('plPriceRub'); const discountRub = $calculation.$values.getValue('discountRub'); const firstPaymentRub = $calculation.element('tbxFirstPaymentRub').getValue(); const leasingPeriod = $calculation.element('tbxLeasingPeriod').getValue(); const hasPaymentsErrors = $tables.payments.validation.hasErrors; return { discountRub, finGAPInsuranceCompany, firstPaymentRub, hasPaymentsErrors, leasingPeriod, paymentsValues, plPriceRub, }; }, ({ finGAPInsuranceCompany, paymentsValues, plPriceRub, discountRub, firstPaymentRub, leasingPeriod, hasPaymentsErrors, }) => { if (!finGAPInsuranceCompany || hasPaymentsErrors) { $tables.fingap.clear(); return; } getFingapRisks( { discountRub, firstPaymentRub, leasingPeriod, plPriceRub, }, { fingap: { insuranceCompany: finGAPInsuranceCompany } }, { values: paymentsValues } ) .then((risks) => { if (risks?.length) { $tables.fingap.setRisks(risks); } else { $tables.fingap.clear(); } }) .catch(() => { $tables.fingap.clear(); }); }, { // Important: delay prohibits multiple reaction invocation delay: 100, equals: comparer.structural, } ); }