2023-03-16 15:35:44 +03:00

216 lines
5.9 KiB
TypeScript

import type { Elements } from '@/Components/Calculation/config/map/values';
import * as CRMTypes from '@/graphql/crm.types';
import type { ProcessContext } from '@/process/types';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import { reaction } from 'mobx';
import { first, sort } from 'radash';
import { makeDisposable, normalizeOptions } from 'tools';
dayjs.extend(utc);
export default function valuesReactions({ store, apolloClient }: ProcessContext) {
const { $calculation, $process } = store;
reaction(
() =>
$calculation.$values.getValues([
'product',
'leasingPeriod',
'leaseObjectUsed',
'deliveryTime',
'firstPaymentPerc',
'lastPaymentPerc',
]),
async ({
product,
leasingPeriod,
leaseObjectUsed,
deliveryTime,
firstPaymentPerc,
lastPaymentPerc,
}) => {
const currentDate = dayjs().utc(false).format('YYYY-MM-DD');
let {
data: { evo_tarifs = [] },
} = await apolloClient.query({
fetchPolicy: 'network-only',
query: CRMTypes.GetTarifsDocument,
variables: {
currentDate,
},
});
$calculation.element('selectTarif').setOptions(normalizeOptions(evo_tarifs));
if (product && leasingPeriod && deliveryTime && evo_tarifs) {
evo_tarifs = evo_tarifs?.filter(
(tarif) =>
tarif &&
tarif.evo_baseproductid === product &&
tarif.evo_min_period !== null &&
tarif.evo_min_period <= leasingPeriod &&
tarif.evo_max_period !== null &&
tarif.evo_max_period >= leasingPeriod &&
tarif.evo_delivery_time?.includes(deliveryTime) &&
tarif.evo_min_first_payment !== null &&
tarif.evo_min_first_payment <= firstPaymentPerc &&
tarif.evo_max_first_payment !== null &&
tarif.evo_max_first_payment >= firstPaymentPerc &&
tarif.evo_min_last_payment !== null &&
tarif.evo_min_last_payment <= lastPaymentPerc &&
tarif.evo_max_last_payment !== null &&
tarif.evo_max_last_payment >= lastPaymentPerc
);
$calculation.element('selectTarif').setValue(evo_tarifs?.at(0)?.evo_tarifid || null);
} else if (leaseObjectUsed === true && evo_tarifs) {
evo_tarifs = evo_tarifs?.filter((tarif) => {
if (leaseObjectUsed === true) {
return tarif?.evo_used;
}
return true;
});
$calculation.element('selectTarif').setValue(evo_tarifs?.at(0)?.evo_tarifid || null);
} else {
$calculation.element('selectTarif').resetValue();
}
},
{
delay: 10,
fireImmediately: true,
}
);
makeDisposable(
() =>
reaction(
() => $calculation.element('selectTarif').getValue(),
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();
}
}
),
() => $process.has('LoadKP')
);
reaction(
() => $calculation.element('selectTarif').getValue(),
async (tarifId) => {
if (!tarifId) {
$calculation.element('selectRate').resetValue();
return;
}
const currentDate = dayjs().utc(false).format('YYYY-MM-DD');
const {
data: { evo_rates },
} = await apolloClient.query({
fetchPolicy: 'network-only',
query: CRMTypes.GetRatesDocument,
variables: {
currentDate,
},
});
$calculation.element('selectRate').setOptions(normalizeOptions(evo_rates));
const {
data: { evo_tarif },
} = await apolloClient.query({
query: CRMTypes.GetTarifDocument,
variables: { tarifId },
});
const targetRate =
evo_tarif?.evo_rates &&
first(sort(evo_tarif?.evo_rates, (x) => dayjs(x?.evo_datefrom).date()));
if (targetRate) {
$calculation.element('selectRate').setValue(targetRate?.evo_rateid);
} else {
const baseRate = evo_rates?.find((x) => x?.evo_id === 'BASE');
if (baseRate) $calculation.element('selectRate').setValue(baseRate?.value);
}
}
);
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);
}
);
(
[
'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);
}
}
);
});
}