2023-04-13 20:06:44 +03:00

169 lines
5.2 KiB
TypeScript

import type { ProcessContext } from '../types';
import { createValidationSchema } from './validation';
import type { Elements } from '@/Components/Calculation/config/map/values';
import * as CRMTypes from '@/graphql/crm.types';
import { createValidationReaction } from '@/process/tools';
import { disposableReaction } from '@/utils/mobx';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import { reaction } from 'mobx';
dayjs.extend(utc);
export function common({ store, apolloClient }: ProcessContext) {
const { $calculation, $tables } = store;
reaction(
() => $calculation.element('cbxRecalcWithRevision').getValue(),
() => $calculation.element('selectQuote').resetValue()
);
disposableReaction(
() => $calculation.element('cbxRecalcWithRevision').getValue() === true,
() =>
$calculation.$values.getValues([
'discountRub',
'addEquipmentPrice',
'importProgramSum',
'plPriceRub',
'leaseObjectType',
]),
async ({
discountRub,
importProgramSum,
addEquipmentPrice,
plPriceRub,
leaseObjectType: leaseObjectTypeId,
}) => {
let evo_leasingobject_type: CRMTypes.GetLeaseObjectTypeQuery['evo_leasingobject_type'] = null;
if (leaseObjectTypeId) {
const { data } = await apolloClient.query({
query: CRMTypes.GetLeaseObjectTypeDocument,
variables: { leaseObjectTypeId },
});
({ evo_leasingobject_type } = data);
}
const price = plPriceRub + addEquipmentPrice - importProgramSum;
const maxPriceChange =
price - discountRub < 800_000
? price - discountRub + 50_000
: (price - discountRub) *
(evo_leasingobject_type?.evo_vehicle_type?.includes(100_000_001) ||
evo_leasingobject_type?.evo_vehicle_type?.includes(100_000_005)
? 1.01
: 1.05);
$calculation.element('tbxMaxPriceChange').setValue(maxPriceChange);
const minPriceChange =
price - discountRub < 800_000 ? price - discountRub - 50_000 : (price - discountRub) * 0.95;
$calculation.element('tbxMinPriceChange').setValue(minPriceChange);
}
);
reaction(
() => $calculation.$values.getValues(['leaseObjectUsed', 'recalcWithRevision']),
({ leaseObjectUsed, recalcWithRevision }) => {
if (recalcWithRevision && leaseObjectUsed) {
$calculation.element('tbxLeaseObjectYear').block();
} else {
$calculation.element('tbxLeaseObjectYear').unblock();
}
}
);
{
const elements: Elements[] = [
'cbxLeaseObjectUsed',
'radioGraphType',
'selectBrand',
'selectCalcBroker',
'selectCalcFinDepartment',
'selectClientRisk',
'selectClientType',
'selectConfiguration',
'selectDealer',
'selectDealerPerson',
'selectHighSeasonStart',
'selectImportProgram',
'selectIndAgent',
'selectLead',
'selectLeaseObjectCategory',
'selectLeaseObjectType',
'selectModel',
'selectOpportunity',
'selectRate',
'selectRequirementTelematic',
'selectSeasonType',
'selectSubsidy',
'selectTarif',
'selectTelematic',
'selectTracker',
'tbxAddEquipmentPrice',
'tbxCreditRate',
'tbxEngineHours',
'tbxImporterRewardPerc',
'tbxImporterRewardRub',
'tbxLeasingPeriod',
'tbxMaxPriceChange',
'tbxMileage',
'tbxParmentsDecreasePercent',
'tbxVIN',
];
reaction(
() => $calculation.element('cbxRecalcWithRevision').getValue(),
(recalcWithRevision) => {
if (recalcWithRevision) {
elements.forEach((elementName) =>
$calculation.$status.overrideStatus(elementName, 'Disabled')
);
$tables.payments.overrideStatuses('Disabled');
} else {
elements.forEach((elementName) => $calculation.$status.clearOverridedStatus(elementName));
$tables.payments.clearOverridedStatuses();
}
}
);
const agents: Elements[] = [
'selectCalcBroker',
'selectCalcBrokerRewardCondition',
'selectCalcDoubleAgent',
'selectCalcDoubleAgentRewardCondition',
'selectCalcFinDepartment',
'selectDealerBroker',
'selectDealerBrokerRewardCondition',
'selectDealerPerson',
'selectDealerRewardCondition',
'selectFinDepartmentRewardCondtion',
'selectIndAgent',
'selectIndAgentRewardCondition',
'tbxCalcBrokerRewardSum',
'tbxCalcDoubleAgentRewardSumm',
'tbxDealerBrokerRewardSumm',
'tbxDealerRewardSumm',
'tbxFinDepartmentRewardSumm',
'tbxIndAgentRewardSumm',
];
reaction(
() => $calculation.$values.getValues(['firstPaymentPerc', 'recalcWithRevision']),
({ firstPaymentPerc, recalcWithRevision }) => {
if (firstPaymentPerc === 0 && recalcWithRevision === true) {
agents.forEach((elementName) =>
$calculation.$status.overrideStatus(elementName, 'Disabled')
);
} else {
agents.forEach((elementName) => $calculation.$status.clearOverridedStatus(elementName));
}
}
);
}
}
export const validation = createValidationReaction(createValidationSchema);