2023-04-18 16:39:02 +03:00

289 lines
9.1 KiB
TypeScript

import { createValidationReaction } from '../tools';
import type { ProcessContext } from '../types';
import { createValidationSchema } from './validation';
import { selectRequirementTelematic } from '@/config/default-options';
import * as CRMTypes from '@/graphql/crm.types';
import { normalizeOptions } from '@/utils/entity';
import { debouncedReaction } from '@/utils/mobx';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import { reaction, toJS } from 'mobx';
dayjs.extend(utc);
export default function reactions({ store, apolloClient }: ProcessContext) {
const { $calculation, $tables } = store;
reaction(
() => $calculation.$values.getValues(['leasingPeriod', 'leaseObjectType', 'maxMass']),
async ({ leasingPeriod, leaseObjectType, maxMass }) => {
if (maxMass >= 40_000) {
$calculation.element('selectTechnicalCard').setOptions([]);
return;
}
const currentDate = dayjs().utc(false).toISOString();
const {
data: { evo_addproduct_types },
} = await apolloClient.query({
query: CRMTypes.GetTechnicalCardsDocument,
variables: { currentDate },
});
const options = evo_addproduct_types?.filter(
(evo_addproduct_type) =>
Boolean(
evo_addproduct_type?.evo_min_period &&
evo_addproduct_type.evo_min_period <= leasingPeriod
) &&
Boolean(
evo_addproduct_type?.evo_max_period &&
evo_addproduct_type.evo_max_period >= leasingPeriod
) &&
Boolean(
leaseObjectType &&
evo_addproduct_type?.evo_leasingobject_types?.find(
(x) => x?.evo_leasingobject_typeid === leaseObjectType
)
)
);
$calculation.element('selectTechnicalCard').setOptions(normalizeOptions(options));
const currentTechnicalCardId = $calculation.element('selectTechnicalCard').getValue();
if (currentTechnicalCardId) {
const {
data: { evo_addproduct_type },
} = await apolloClient.query({
query: CRMTypes.GetAddProductTypeDocument,
variables: { addproductTypeId: currentTechnicalCardId },
});
const nextTechnicalCard = options?.find(
(x) => x?.evo_helpcard_type === evo_addproduct_type?.evo_helpcard_type
);
if (nextTechnicalCard) {
$calculation.element('selectTechnicalCard').setValue(nextTechnicalCard?.value);
} else {
$calculation.element('selectTechnicalCard').resetValue();
}
}
},
{
fireImmediately: true,
}
);
reaction(
() => $calculation.$values.getValues(['leasingPeriod', 'leaseObjectType']),
async ({ leasingPeriod, leaseObjectType }) => {
const currentDate = dayjs().utc(false).toISOString();
const {
data: { evo_addproduct_types },
} = await apolloClient.query({
query: CRMTypes.GetFuelCardsDocument,
variables: { currentDate },
});
const options = evo_addproduct_types?.filter(
(evo_addproduct_type) =>
Boolean(
evo_addproduct_type?.evo_min_period &&
evo_addproduct_type.evo_min_period <= leasingPeriod
) &&
Boolean(
evo_addproduct_type?.evo_max_period &&
evo_addproduct_type.evo_max_period >= leasingPeriod
) &&
Boolean(
leaseObjectType &&
evo_addproduct_type?.evo_leasingobject_types?.find(
(x) => x?.evo_leasingobject_typeid === leaseObjectType
)
)
);
$calculation.element('selectFuelCard').setOptions(normalizeOptions(options));
},
{
fireImmediately: true,
}
);
reaction(
() => {
const values = $calculation.$values.getValues(['leasingPeriod', 'leasingWithoutKasko']);
const kaskoInsured = toJS($tables.insurance.row('kasko').getValue('insured'));
const fingapInsured = toJS($tables.insurance.row('fingap').getValue('insured'));
return {
...values,
fingapInsured,
kaskoInsured,
};
},
({ leasingPeriod, fingapInsured, kaskoInsured, leasingWithoutKasko }) => {
if (
leasingPeriod < 12 ||
leasingWithoutKasko ||
kaskoInsured === 100_000_001 ||
(fingapInsured === 100_000_001 && leasingPeriod < 36)
) {
$calculation.element('selectInsNSIB').resetValue().block();
} else {
$calculation.element('selectInsNSIB').unblock();
}
},
{
delay: 10,
fireImmediately: true,
}
);
debouncedReaction(
() =>
$calculation.$values.getValues([
'leasingPeriod',
'leaseObjectType',
'plPriceRub',
'discountRub',
'addEquipmentPrice',
'importProgramSum',
'firstPaymentPerc',
]),
async ({
addEquipmentPrice,
discountRub,
firstPaymentPerc,
importProgramSum,
leaseObjectType: leaseObjectTypeId,
leasingPeriod,
plPriceRub,
}) => {
const currentDate = dayjs().utc(false).toISOString();
const {
data: { evo_addproduct_types },
} = await apolloClient.query({
query: CRMTypes.GetInsNsibTypesDocument,
variables: { currentDate },
});
const options = evo_addproduct_types?.filter(
(x) =>
x &&
Boolean(x?.evo_max_period !== null && x.evo_max_period >= leasingPeriod) &&
Boolean(x?.evo_min_period !== null && x.evo_min_period <= leasingPeriod) &&
Boolean(
x?.evo_max_price !== null &&
x.evo_max_price >= plPriceRub - discountRub - importProgramSum + addEquipmentPrice
) &&
Boolean(
x?.evo_min_price !== null &&
x.evo_min_price <= plPriceRub - discountRub - importProgramSum + addEquipmentPrice
) &&
x.evo_leasingobject_types?.find(
(evo_leasingobject_type) =>
evo_leasingobject_type?.evo_leasingobject_typeid === leaseObjectTypeId
) &&
x.evo_visible_calc &&
Boolean(
x.evo_min_first_payment_perc !== null &&
x.evo_min_first_payment_perc <= firstPaymentPerc
) &&
Boolean(
x.evo_max_first_payment_perc !== null &&
x.evo_max_first_payment_perc >= firstPaymentPerc
)
);
$calculation.element('selectInsNSIB').setOptions(normalizeOptions(options));
},
{
delay: 10,
wait: 100,
}
);
reaction(
() => $calculation.$values.getValues(['recalcWithRevision', 'leaseObjectType']),
async ({ recalcWithRevision, leaseObjectType: leaseObjectTypeId }) => {
if (recalcWithRevision === false) {
$calculation
.element('selectRequirementTelematic')
.setOptions(
selectRequirementTelematic.filter((x) =>
[100_000_000, 100_000_001, 100_000_002, 100_000_003].includes(x.value)
)
);
if (leaseObjectTypeId) {
const {
data: { evo_leasingobject_type },
} = await apolloClient.query({
query: CRMTypes.GetLeaseObjectTypeDocument,
variables: { leaseObjectTypeId },
});
if (evo_leasingobject_type?.evo_id === '11') {
$calculation.element('selectRequirementTelematic').setValue(100_000_000).block();
} else {
$calculation.element('selectRequirementTelematic').unblock();
}
}
} else {
$calculation.element('selectRequirementTelematic').resetOptions();
}
},
{
fireImmediately: true,
}
);
reaction(
() => $calculation.$values.getValues(['requirementTelematic', 'recalcWithRevision']),
async ({ requirementTelematic, recalcWithRevision }) => {
const currentDate = dayjs().utc(false).toISOString();
const {
data: { evo_addproduct_types: trackerTypes },
} = await apolloClient.query({
query: CRMTypes.GetTrackerTypesDocument,
variables: { currentDate },
});
const {
data: { evo_addproduct_types: telematicTypes },
} = await apolloClient.query({
query: CRMTypes.GetTelematicTypesDocument,
variables: { currentDate },
});
let filteredTrackerTypes = trackerTypes?.filter(
(x) => requirementTelematic && x?.evo_controls_program?.includes(requirementTelematic)
);
let filteredTelematicTypes = telematicTypes?.filter(
(x) => requirementTelematic && x?.evo_controls_program?.includes(requirementTelematic)
);
if (!recalcWithRevision) {
filteredTrackerTypes = filteredTrackerTypes?.filter((x) => x?.evo_visible_calc === true);
filteredTelematicTypes = filteredTelematicTypes?.filter(
(x) => x?.evo_visible_calc === true
);
}
$calculation.element('selectTracker').setOptions(normalizeOptions(filteredTrackerTypes));
$calculation.element('selectTelematic').setOptions(normalizeOptions(filteredTelematicTypes));
},
{
fireImmediately: true,
}
);
}
export const validation = createValidationReaction(createValidationSchema);