236 lines
6.1 KiB
TypeScript
236 lines
6.1 KiB
TypeScript
import helper from '../lib/helper';
|
|
import type { Elements } from '@/Components/Calculation/config/map/values';
|
|
import * as CRMTypes from '@/graphql/crm.types';
|
|
import type { ProcessContext } from '@/process/types';
|
|
import { normalizeOptions } from '@/utils/entity';
|
|
import { disposableDebouncedReaction, disposableReaction } from '@/utils/mobx';
|
|
import dayjs from 'dayjs';
|
|
import utc from 'dayjs/plugin/utc';
|
|
import { comparer, reaction } from 'mobx';
|
|
|
|
dayjs.extend(utc);
|
|
|
|
export default function valuesReactions({ store, apolloClient, trpcClient }: ProcessContext) {
|
|
let abortController = new AbortController();
|
|
const { $calculation, $process } = store;
|
|
|
|
const { getRates, getPriceChange } = helper({ apolloClient });
|
|
|
|
disposableDebouncedReaction(
|
|
() => $process.has('LoadKP'),
|
|
() =>
|
|
$calculation.$values.getValues([
|
|
'product',
|
|
'leasingPeriod',
|
|
'leaseObjectUsed',
|
|
'deliveryTime',
|
|
'firstPaymentPerc',
|
|
'lastPaymentPerc',
|
|
'leaseObjectType',
|
|
'floatingRate',
|
|
'partialVAT',
|
|
]),
|
|
async (values) => {
|
|
$calculation.element('selectTarif').resetOptions().resetValue();
|
|
try {
|
|
if (abortController) abortController.abort();
|
|
abortController = new AbortController();
|
|
|
|
$process.add('Tarif');
|
|
const { evo_tarif } = await trpcClient.getTarif.query(values, {
|
|
signal: abortController.signal,
|
|
});
|
|
|
|
if (evo_tarif) {
|
|
$calculation.element('selectTarif').setOptions(normalizeOptions([evo_tarif]));
|
|
$calculation.element('selectTarif').setValue(evo_tarif.evo_tarifid);
|
|
}
|
|
|
|
$process.delete('Tarif');
|
|
} catch {
|
|
$process.delete('Tarif');
|
|
$calculation.element('selectTarif').resetOptions();
|
|
}
|
|
},
|
|
{
|
|
delay: 10,
|
|
equals: comparer.shallow,
|
|
fireImmediately: true,
|
|
wait: 100,
|
|
}
|
|
);
|
|
|
|
disposableReaction(
|
|
() => $process.has('LoadKP'),
|
|
() => $calculation.$values.getValue('tarif'),
|
|
async (tarifId) => {
|
|
if (!tarifId) {
|
|
$calculation.element('tbxIRR_Perc').resetValue();
|
|
|
|
return;
|
|
}
|
|
|
|
const {
|
|
data: { evo_tarif },
|
|
} = await apolloClient.query({
|
|
fetchPolicy: 'network-only',
|
|
query: CRMTypes.GetTarifDocument,
|
|
variables: {
|
|
tarifId,
|
|
},
|
|
});
|
|
|
|
if (evo_tarif?.evo_irr) {
|
|
$calculation.element('tbxIRR_Perc').setValue(evo_tarif?.evo_irr);
|
|
} else {
|
|
$calculation.element('tbxIRR_Perc').resetValue();
|
|
}
|
|
},
|
|
{
|
|
delay: 20,
|
|
}
|
|
);
|
|
|
|
reaction(
|
|
() => $calculation.element('selectTarif').getValue(),
|
|
async (tarif) => {
|
|
if (!tarif) {
|
|
$calculation.element('selectRate').resetOptions();
|
|
|
|
return;
|
|
}
|
|
|
|
const { evo_rates } = await getRates({ tarif });
|
|
|
|
$calculation.element('selectRate').setOptions(normalizeOptions(evo_rates));
|
|
}
|
|
);
|
|
|
|
disposableReaction(
|
|
() => $process.has('LoadKP'),
|
|
() => $calculation.element('selectTarif').getValue(),
|
|
async (tarif) => {
|
|
if (!tarif) {
|
|
$calculation.element('selectRate').resetValue();
|
|
|
|
return;
|
|
}
|
|
|
|
const { evo_rate } = await getRates({ tarif });
|
|
|
|
$calculation.element('selectRate').setValue(evo_rate?.evo_rateid || null);
|
|
}
|
|
);
|
|
|
|
reaction(
|
|
() => $calculation.element('selectRate').getValue(),
|
|
async (rateId) => {
|
|
if (!rateId) {
|
|
$calculation.element('tbxCreditRate').resetValue();
|
|
|
|
return;
|
|
}
|
|
|
|
const {
|
|
data: { evo_rate },
|
|
} = await apolloClient.query({
|
|
fetchPolicy: 'network-only',
|
|
query: CRMTypes.GetRateDocument,
|
|
variables: {
|
|
rateId,
|
|
},
|
|
});
|
|
|
|
if (evo_rate?.evo_base_rate === null || evo_rate?.evo_base_rate === undefined) {
|
|
$calculation.element('tbxCreditRate').resetValue();
|
|
|
|
return;
|
|
}
|
|
|
|
$calculation.element('tbxCreditRate').setValue(evo_rate?.evo_base_rate);
|
|
}
|
|
);
|
|
|
|
disposableReaction(
|
|
() => $process.has('LoadKP'),
|
|
() =>
|
|
$calculation.$values.getValues([
|
|
'discountRub',
|
|
'addEquipmentPrice',
|
|
'importProgramSum',
|
|
'plPriceRub',
|
|
'leaseObjectType',
|
|
'recalcWithRevision',
|
|
]),
|
|
async (values) => {
|
|
const { maxPriceChange, minPriceChange } = await getPriceChange(values);
|
|
|
|
$calculation.element('tbxMaxPriceChange').setValue(maxPriceChange);
|
|
$calculation.element('tbxMinPriceChange').setValue(minPriceChange);
|
|
},
|
|
{
|
|
delay: 5,
|
|
equals: comparer.shallow,
|
|
}
|
|
);
|
|
|
|
reaction(
|
|
() => $calculation.element('selectProduct').getValue(),
|
|
async (productId) => {
|
|
if (!productId) {
|
|
$calculation.element('cbxPartialVAT').unblock();
|
|
$calculation.element('cbxPartialVAT').unblock();
|
|
|
|
return;
|
|
}
|
|
|
|
const {
|
|
data: { evo_baseproduct },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetProductDocument,
|
|
variables: { productId },
|
|
});
|
|
|
|
// eslint-disable-next-line no-negated-condition
|
|
if (!evo_baseproduct?.evo_product_properties?.includes(100_000_001)) {
|
|
$calculation.element('cbxFloatingRate').setValue(false).block();
|
|
} else {
|
|
$calculation.element('cbxFloatingRate').unblock();
|
|
}
|
|
|
|
// eslint-disable-next-line no-negated-condition
|
|
if (!evo_baseproduct?.evo_sale_without_nds) {
|
|
$calculation.element('cbxPartialVAT').setValue(false).block();
|
|
} else {
|
|
$calculation.element('cbxPartialVAT').unblock();
|
|
}
|
|
}
|
|
);
|
|
|
|
(
|
|
[
|
|
'selectProduct',
|
|
'selectLeaseObjectType',
|
|
'selectBrand',
|
|
'selectModel',
|
|
'selectConfiguration',
|
|
'selectTracker',
|
|
'selectTelematic',
|
|
'selectTechnicalCard',
|
|
'selectFuelCard',
|
|
'selectRegistration',
|
|
'selectTownRegistration',
|
|
'radioCalcType',
|
|
] as Elements[]
|
|
).forEach((elementName) => {
|
|
reaction(
|
|
() => $calculation.element(elementName).getOptions(),
|
|
(options) => {
|
|
if (options.length === 1) {
|
|
$calculation.element(elementName).setValue(options[0]?.value);
|
|
}
|
|
}
|
|
);
|
|
});
|
|
}
|