2024-04-15 10:18:13 +03:00

309 lines
10 KiB
TypeScript

/* eslint-disable sonarjs/cognitive-complexity */
import { createValidationReaction } from '../tools';
import type { ProcessContext } from '../types';
import { createValidationSchema } from './validation';
import { selectLeaseObjectUseFor } from '@/config/default-options';
import * as CRMTypes from '@/graphql/crm.types';
import { normalizeOptions } from '@/utils/entity';
import { debouncedReaction, disposableReaction } from '@/utils/mobx';
import { reaction } from 'mobx';
export function common({ store, apolloClient }: ProcessContext) {
const { $calculation, $tables, $process } = store;
reaction(
() => $calculation.element('selectGPSBrand').getValue(),
async (gpsBrandId) => {
if (!gpsBrandId) {
$calculation.element('selectGPSModel').resetValue();
return;
}
const {
data: { evo_gps_models },
} = await apolloClient.query({
query: CRMTypes.GetGpsModelsDocument,
variables: {
gpsBrandId,
},
});
if (evo_gps_models) {
$calculation.element('selectGPSModel').setOptions(normalizeOptions(evo_gps_models));
}
}
);
reaction(
() => $calculation.element('selectLeaseObjectCategory').getValue(),
(leaseObjectCategory) => {
switch (leaseObjectCategory) {
case 100_000_000: {
$calculation
.element('selectLeaseObjectUseFor')
.setOptions(
selectLeaseObjectUseFor.filter((x) => [100_000_000, 100_000_002].includes(x.value))
);
break;
}
case 100_000_001: {
$calculation
.element('selectLeaseObjectUseFor')
.setOptions(
selectLeaseObjectUseFor.filter((x) =>
[
100_000_000, 100_000_001, 100_000_002, 100_000_003, 100_000_004, 100_000_005,
100_000_006, 100_000_007, 100_000_008, 100_000_009, 100_000_010, 100_000_011,
100_000_012, 100_000_013,
].includes(x.value)
)
);
break;
}
case 100_000_002: {
$calculation
.element('selectLeaseObjectUseFor')
.setOptions(
selectLeaseObjectUseFor.filter((x) =>
[
100_000_002, 100_000_004, 100_000_005, 100_000_006, 100_000_009, 100_000_010,
100_000_020,
].includes(x.value)
)
);
break;
}
case 100_000_003: {
$calculation
.element('selectLeaseObjectUseFor')
.setOptions(
selectLeaseObjectUseFor.filter((x) =>
[
100_000_002, 100_000_004, 100_000_006, 100_000_007, 100_000_008, 100_000_011,
100_000_012, 100_000_013,
].includes(x.value)
)
);
break;
}
case 100_000_004: {
$calculation
.element('selectLeaseObjectUseFor')
.setOptions(
selectLeaseObjectUseFor.filter((x) =>
[100_000_002, 100_000_009, 100_000_020].includes(x.value)
)
);
break;
}
default: {
$calculation
.element('selectLeaseObjectUseFor')
.setOptions(
selectLeaseObjectUseFor.filter((x) =>
[
100_000_014, 100_000_015, 100_000_016, 100_000_017, 100_000_018, 100_000_019,
].includes(x.value)
)
);
break;
}
}
},
{
fireImmediately: true,
}
);
debouncedReaction(
() =>
$calculation.$values.getValues([
'leasingPeriod',
'leasingWithoutKasko',
'dealer',
'leaseObjectCategory',
]),
async ({ leasingPeriod, leasingWithoutKasko, dealer: dealerId, leaseObjectCategory }) => {
const {
data: { accounts },
} = await apolloClient.query({
query: CRMTypes.GetInsuranceCompaniesDocument,
});
if (leasingWithoutKasko) {
const otherInsuranceCompany = accounts?.find(
(x) => x?.evo_type_ins_policy === null && x.label?.includes('ПРОЧИЕ')
);
if (otherInsuranceCompany) {
$tables.insurance
.row('kasko')
.column('insuranceCompany')
.setOptions(normalizeOptions([otherInsuranceCompany]))
.setValue(otherInsuranceCompany.value)
.block();
$tables.insurance.row('kasko').column('insCost').resetValue().block();
$tables.insurance.row('kasko').column('insTerm').setValue(100_000_000).block();
$tables.insurance.row('kasko').column('insured').setValue(100_000_000).block();
}
} else {
const defaultKaskoOptions = accounts?.filter((x) =>
x?.evo_type_ins_policy?.includes(100_000_000)
);
$tables.insurance
.row('kasko')
.setOptions('insuranceCompany', normalizeOptions(defaultKaskoOptions))
.unblock('insuranceCompany')
.unblock('insCost');
if (leasingPeriod < 12) {
$tables.insurance.row('osago').column('insured').setValue(100_000_000).block();
$tables.insurance.row('kasko').column('insured').setValue(100_000_000).block();
$tables.insurance.row('kasko').column('insTerm').setValue(100_000_000).block();
} else if (leasingPeriod === 12 || leasingPeriod > 16) {
$tables.insurance.row('osago').column('insured').unblock();
$tables.insurance.row('kasko').column('insTerm').setValue(100_000_000).block();
$tables.insurance.row('kasko').column('insured').unblock();
} else if (leasingPeriod > 12 && leasingPeriod < 16) {
$tables.insurance.row('osago').column('insured').unblock();
$tables.insurance.row('kasko').column('insTerm').setValue(100_000_001).block();
$tables.insurance.row('kasko').column('insured').unblock();
} else {
$tables.insurance.row('osago').column('insured').unblock();
$tables.insurance.row('kasko').column('insTerm').block();
$tables.insurance.row('kasko').column('insured').unblock();
}
/**
* @see 'apps\web\process\supplier-agent\reactions\leaseback.ts'
*/
if (dealerId) {
const {
data: { dealer },
} = await apolloClient.query({
query: CRMTypes.GetDealerDocument,
variables: {
dealerId,
},
});
if (dealer?.evo_return_leasing_dealer === true) {
$tables.insurance.row('kasko').column('insured').setValue(100_000_000).block();
}
// объединили реакцию для прицепа и возвратного лизинга
const isTrailer = leaseObjectCategory === 100_000_004;
if (isTrailer || dealer?.evo_return_leasing_dealer === true) {
$tables.insurance.row('osago').column('insured').setValue(100_000_000).block();
}
}
}
},
{
delay: 1,
fireImmediately: true,
wait: 100,
}
);
debouncedReaction(
() => $calculation.$values.getValues(['leaseObjectCategory', 'leasingWithoutKasko']),
async ({ leaseObjectCategory, leasingWithoutKasko }) => {
const {
data: { accounts },
} = await apolloClient.query({
query: CRMTypes.GetInsuranceCompaniesDocument,
});
const isTrailer = leaseObjectCategory === 100_000_004;
if (isTrailer) {
const otherInsuranceCompany = accounts?.find(
(x) => x?.evo_type_ins_policy === null && x.label?.includes('ПРОЧИЕ')
);
if (otherInsuranceCompany) {
$tables.insurance
.row('osago')
.column('insuranceCompany')
.setOptions(normalizeOptions([otherInsuranceCompany]))
.setValue(otherInsuranceCompany.value)
.block();
$tables.insurance.row('osago').column('insCost').resetValue().block();
}
} else {
$tables.insurance.row('osago').column('insCost').unblock();
$tables.insurance.row('osago').column('insuranceCompany').unblock();
const defaultOsagoOptions = accounts?.filter((x) =>
x?.evo_type_ins_policy?.includes(100_000_001)
);
if (leasingWithoutKasko) {
const evoKaskoInsuranceOptions = defaultOsagoOptions?.filter(
(x) => x?.evo_evokasko_access === true
);
$tables.insurance
.row('osago')
.column('insuranceCompany')
.setOptions(normalizeOptions(evoKaskoInsuranceOptions));
} else {
$tables.insurance
.row('osago')
.column('insuranceCompany')
.setOptions(normalizeOptions(defaultOsagoOptions));
}
}
},
{
delay: 1,
wait: 100,
}
);
disposableReaction(
() => $process.has('LoadKP'),
() => $calculation.element('selectBrand').getValue(),
async (brandId) => {
if (!brandId) {
$tables.insurance.row('kasko').column('insuranceCompany').resetValue().unblock();
return;
}
const {
data: { accounts },
} = await apolloClient.query({
query: CRMTypes.GetInsuranceCompaniesDocument,
});
const defaultKaskoOptions = accounts?.filter((x) =>
x?.evo_type_ins_policy?.includes(100_000_000)
);
const {
data: { evo_brand },
} = await apolloClient.query({
query: CRMTypes.GetBrandDocument,
variables: { brandId },
});
if (evo_brand?.evo_id === 'BRAND49') {
const renessansCompany = defaultKaskoOptions?.find((x) => x?.evo_inn === '7725497022');
if (renessansCompany)
$tables.insurance
.row('kasko')
.column('insuranceCompany')
.setValue(renessansCompany?.value)
.block();
} else {
$tables.insurance.row('kasko').column('insuranceCompany').resetValue().unblock();
}
}
);
}
export const validation = createValidationReaction(createValidationSchema);