2024-07-15 11:04:47 +03:00

399 lines
13 KiB
TypeScript

/* eslint-disable sonarjs/cognitive-complexity */
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 { getCurrentDateString } from '@/utils/date';
import { normalizeOptions } from '@/utils/entity';
import { debouncedReaction } from '@/utils/mobx';
import { reaction, toJS } from 'mobx';
import { max } from 'radash';
export default function reactions({ store, apolloClient }: ProcessContext) {
const { $calculation, $tables, $process } = store;
reaction(
() => $calculation.$values.getValues(['leasingPeriod', 'leaseObjectType', 'maxMass']),
async ({ leasingPeriod, leaseObjectType, maxMass }) => {
if (maxMass >= 40_000) {
$calculation.element('selectTechnicalCard').setOptions([]);
return;
}
const currentDate = getCurrentDateString();
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
)
)
);
const currentTechnicalCardId = $calculation.element('selectTechnicalCard').getValue();
$calculation.element('selectTechnicalCard').setOptions(normalizeOptions(options));
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,
}
);
/**
* Устанавливаем самую дорогую техническую карточку по умолчанию первый раз при формировании списка карт
*/
{
const dispose = reaction(
() => {
const technicalCards = $calculation.element('selectTechnicalCard').getOptions();
const isLoadKP = $process.has('LoadKP');
return { isLoadKP, technicalCards };
},
async ({ technicalCards, isLoadKP }) => {
if (isLoadKP) {
dispose();
return;
}
const currentTechnicalCard = $calculation.element('selectTechnicalCard').getValue();
if (technicalCards.length && !currentTechnicalCard) {
const evo_addproduct_types = await Promise.all(
technicalCards.map(async (x) => {
const {
data: { evo_addproduct_type },
} = await apolloClient.query({
query: CRMTypes.GetAddProductTypeDocument,
variables: { addproductTypeId: x.value },
});
return evo_addproduct_type;
})
);
const maxPriceTechnicalCard = max(evo_addproduct_types, (x) => x?.evo_graph_price ?? 0);
if (maxPriceTechnicalCard) {
$calculation
.element('selectTechnicalCard')
.setValue(maxPriceTechnicalCard?.evo_addproduct_typeid);
}
dispose();
}
}
);
}
reaction(
() => $calculation.$values.getValues(['leasingPeriod', 'leaseObjectType']),
async ({ leasingPeriod, leaseObjectType }) => {
const currentDate = getCurrentDateString();
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 = getCurrentDateString();
const {
data: { evo_addproduct_types },
} = await apolloClient.query({
context: {
disableModify: true,
},
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', 'engineType']),
async ({ recalcWithRevision, leaseObjectType: leaseObjectTypeId, engineType }) => {
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 (engineType === 100_000_003) {
$calculation.element('selectRequirementTelematic').setValue(100_000_003).block();
} else {
$calculation.element('selectRequirementTelematic').unblock();
if (leaseObjectTypeId) {
const {
data: { evo_leasingobject_type },
} = await apolloClient.query({
query: CRMTypes.GetLeaseObjectTypeDocument,
variables: { leaseObjectTypeId },
});
if (evo_leasingobject_type?.evo_id === '8') {
$calculation
.element('selectRequirementTelematic')
.setOptions(
selectRequirementTelematic.filter((x) =>
[100_000_000, 100_000_001].includes(x.value)
)
);
}
if (evo_leasingobject_type?.evo_id === '11') {
$calculation.element('selectRequirementTelematic').setValue(100_000_002).block();
} else {
$calculation.element('selectRequirementTelematic').unblock();
}
}
}
} else {
$calculation.element('selectRequirementTelematic').resetOptions();
}
},
{
fireImmediately: true,
}
);
reaction(
() =>
$calculation.$values.getValues([
'requirementTelematic',
'recalcWithRevision',
'leaseObjectType',
'engineType',
]),
async ({
requirementTelematic,
recalcWithRevision,
leaseObjectType: leaseObjectTypeId,
engineType,
}) => {
const currentDate = getCurrentDateString();
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
);
if (engineType === 100_000_003) {
filteredTelematicTypes = telematicTypes?.filter(
(x) =>
x?.evo_controls_program?.includes(100_000_003) &&
x.label?.toLowerCase().includes('аво') &&
!x.label?.toLowerCase().includes('pro')
);
$calculation.element('selectTracker').resetValue().block();
$calculation.element('selectTelematic').block();
} else {
$calculation.element('selectTracker').unblock();
$calculation.element('selectTelematic').unblock();
if (leaseObjectTypeId) {
const {
data: { evo_leasingobject_type },
} = await apolloClient.query({
query: CRMTypes.GetLeaseObjectTypeDocument,
variables: { leaseObjectTypeId },
});
if (evo_leasingobject_type?.evo_id === '11') {
filteredTelematicTypes = telematicTypes?.filter(
(x) =>
x?.evo_controls_program?.includes(100_000_002) &&
x.label?.toLowerCase().includes('delta') &&
!x.label?.toLowerCase().includes('pro')
);
}
}
}
}
$calculation.element('selectTracker').setOptions(normalizeOptions(filteredTrackerTypes));
$calculation.element('selectTelematic').setOptions(normalizeOptions(filteredTelematicTypes));
},
{
fireImmediately: true,
}
);
}
export const validation = createValidationReaction(createValidationSchema);