428 lines
12 KiB
TypeScript
428 lines
12 KiB
TypeScript
import * as CRMTypes from '@/graphql/crm.types';
|
|
import type { ProcessContext } from '@/process/types';
|
|
import { normalizeOptions } from '@/utils/entity';
|
|
import { reaction } from 'mobx';
|
|
import { intersects } from 'radash';
|
|
|
|
export default function reactions({ store, apolloClient }: ProcessContext) {
|
|
const { $calculation } = store;
|
|
|
|
reaction(
|
|
() => $calculation.$values.getValues(['brand', 'importProgram', 'leaseObjectType', 'subsidy']),
|
|
async ({
|
|
brand: brandId,
|
|
importProgram: importProgramId,
|
|
leaseObjectType: leaseObjectTypeId,
|
|
subsidy: subsidyId,
|
|
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
}) => {
|
|
if (!brandId) {
|
|
$calculation.element('selectModel').reset();
|
|
|
|
return;
|
|
}
|
|
|
|
const {
|
|
data: { evo_models },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetModelsDocument,
|
|
variables: { brandId },
|
|
});
|
|
|
|
let models = evo_models;
|
|
|
|
if (!models) {
|
|
$calculation.element('selectModel').reset();
|
|
|
|
return;
|
|
}
|
|
|
|
if (leaseObjectTypeId) {
|
|
const {
|
|
data: { evo_leasingobject_type },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetLeaseObjectTypeDocument,
|
|
variables: { leaseObjectTypeId },
|
|
});
|
|
|
|
models = models?.filter((model) => {
|
|
if (
|
|
model?.evo_vehicle_type &&
|
|
evo_leasingobject_type?.evo_vehicle_type?.includes(model.evo_vehicle_type)
|
|
) {
|
|
return model;
|
|
}
|
|
|
|
return false;
|
|
});
|
|
}
|
|
|
|
if (subsidyId) {
|
|
const {
|
|
data: { evo_subsidy: subsidy },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetSubsidyDocument,
|
|
variables: { subsidyId },
|
|
});
|
|
|
|
models = models?.filter((model) => {
|
|
if (
|
|
model &&
|
|
(!subsidy?.evo_models?.length ||
|
|
subsidy?.evo_models?.filter(
|
|
(subsidy_evo_model) => subsidy_evo_model?.evo_modelid === model.evo_modelid
|
|
)?.length)
|
|
) {
|
|
return model;
|
|
}
|
|
|
|
return false;
|
|
});
|
|
}
|
|
|
|
if (importProgramId) {
|
|
const {
|
|
data: { importProgram },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetImportProgramDocument,
|
|
variables: { importProgramId },
|
|
});
|
|
|
|
models = models?.filter((model) => {
|
|
if (
|
|
model &&
|
|
(!importProgram?.evo_models?.length ||
|
|
importProgram?.evo_models?.filter(
|
|
(importProgram_evo_model) =>
|
|
importProgram_evo_model?.evo_modelid === model.evo_modelid
|
|
)?.length)
|
|
) {
|
|
return model;
|
|
}
|
|
|
|
return false;
|
|
});
|
|
}
|
|
|
|
$calculation.element('selectModel').setOptions(normalizeOptions(models));
|
|
}
|
|
);
|
|
|
|
reaction(
|
|
() =>
|
|
$calculation.$values.getValues(['importProgram', 'leaseObjectType', 'product', 'subsidy']),
|
|
async ({
|
|
importProgram: importProgramId,
|
|
leaseObjectType: leaseObjectTypeId,
|
|
product: productId,
|
|
subsidy: subsidyId,
|
|
}) => {
|
|
const {
|
|
data: { evo_brands },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetBrandsDocument,
|
|
});
|
|
|
|
let brands = evo_brands;
|
|
|
|
if (!brands) {
|
|
$calculation.element('selectBrand').reset();
|
|
|
|
return;
|
|
}
|
|
|
|
if (productId) {
|
|
const {
|
|
data: { evo_baseproduct },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetProductDocument,
|
|
variables: { productId },
|
|
});
|
|
|
|
brands = brands?.filter(
|
|
(brand) =>
|
|
!evo_baseproduct?.evo_brands?.length ||
|
|
evo_baseproduct.evo_brands.filter(
|
|
(baseproduct_evo_brand) => baseproduct_evo_brand?.evo_brandid === brand?.evo_brandid
|
|
)?.length
|
|
);
|
|
}
|
|
|
|
if (leaseObjectTypeId) {
|
|
const {
|
|
data: { evo_leasingobject_type },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetLeaseObjectTypeDocument,
|
|
variables: { leaseObjectTypeId },
|
|
});
|
|
|
|
brands = brands?.filter(
|
|
(brand) =>
|
|
brand?.evo_vehicle_type?.length &&
|
|
evo_leasingobject_type?.evo_vehicle_type &&
|
|
intersects(
|
|
brand.evo_vehicle_type?.filter((x) => x > 0),
|
|
evo_leasingobject_type?.evo_vehicle_type
|
|
)
|
|
);
|
|
}
|
|
|
|
if (subsidyId) {
|
|
const {
|
|
data: { evo_subsidy: subsidy },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetSubsidyDocument,
|
|
variables: { subsidyId },
|
|
});
|
|
|
|
brands = brands?.filter(
|
|
(brand) =>
|
|
!subsidy?.evo_brands?.length ||
|
|
subsidy?.evo_brands?.filter(
|
|
(subsidy_evo_brand) => subsidy_evo_brand?.evo_brandid === brand?.evo_brandid
|
|
)?.length
|
|
);
|
|
}
|
|
|
|
if (importProgramId) {
|
|
const {
|
|
data: { importProgram },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetImportProgramDocument,
|
|
variables: { importProgramId },
|
|
});
|
|
|
|
brands = brands?.filter(
|
|
(brand) =>
|
|
!importProgram?.evo_brands?.length ||
|
|
importProgram?.evo_brands?.filter(
|
|
(importProgram_evo_brand) =>
|
|
importProgram_evo_brand?.evo_brandid === brand?.evo_brandid
|
|
)?.length
|
|
);
|
|
}
|
|
|
|
$calculation.element('selectBrand').setOptions(normalizeOptions(brands));
|
|
}
|
|
);
|
|
|
|
reaction(
|
|
() => $calculation.element('selectLeaseObjectType').getValue(),
|
|
(leaseObjectTypeId) => {
|
|
if (!leaseObjectTypeId) {
|
|
$calculation.element('selectBrand').resetValue();
|
|
}
|
|
}
|
|
);
|
|
|
|
reaction(
|
|
() => $calculation.element('selectModel').getValue(),
|
|
async (modelId) => {
|
|
if (!modelId) {
|
|
$calculation.element('selectConfiguration').reset();
|
|
|
|
return;
|
|
}
|
|
|
|
const {
|
|
data: { evo_equipments },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetConfigurationsDocument,
|
|
variables: { modelId },
|
|
});
|
|
|
|
$calculation.element('selectConfiguration').setOptions(normalizeOptions(evo_equipments));
|
|
}
|
|
);
|
|
|
|
reaction(
|
|
() => $calculation.element('cbxLeaseObjectUsed').getValue(),
|
|
(leaseObjectUsed) => {
|
|
if (leaseObjectUsed) {
|
|
$calculation.element('tbxLeaseObjectCount').setValue(1).block();
|
|
} else {
|
|
$calculation.element('tbxLeaseObjectCount').unblock();
|
|
}
|
|
}
|
|
);
|
|
|
|
reaction(
|
|
() => $calculation.element('selectLeaseObjectType').getValue(),
|
|
async (leaseObjectTypeId) => {
|
|
if (!leaseObjectTypeId) {
|
|
$calculation.element('cbxWithTrailer').resetValue();
|
|
|
|
return;
|
|
}
|
|
|
|
const {
|
|
data: { evo_leasingobject_type },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetLeaseObjectTypeDocument,
|
|
variables: { leaseObjectTypeId },
|
|
});
|
|
|
|
$calculation.element('cbxWithTrailer').setValue(evo_leasingobject_type?.evo_id === '7');
|
|
}
|
|
);
|
|
|
|
reaction(
|
|
() => $calculation.$values.getValues(['model']),
|
|
async ({ model: modelId }) => {
|
|
let evo_model: CRMTypes.GetModelQuery['evo_model'] = null;
|
|
|
|
if (modelId) {
|
|
const { data } = await apolloClient.query({
|
|
query: CRMTypes.GetModelDocument,
|
|
variables: { modelId },
|
|
});
|
|
|
|
evo_model = data.evo_model;
|
|
}
|
|
|
|
if (evo_model?.evo_impairment_groupidData?.evo_name) {
|
|
$calculation
|
|
.element('labelDepreciationGroup')
|
|
.setValue(evo_model?.evo_impairment_groupidData?.evo_name);
|
|
} else {
|
|
$calculation.element('labelDepreciationGroup').resetValue();
|
|
}
|
|
}
|
|
);
|
|
|
|
reaction(
|
|
() => $calculation.element('selectLeaseObjectCategory').getValue(),
|
|
(leaseObjectCategory) => {
|
|
if (
|
|
leaseObjectCategory !== null &&
|
|
[100_000_001, 100_000_003].includes(leaseObjectCategory)
|
|
) {
|
|
$calculation.element('cbxWithTrailer').setValue(false).block();
|
|
} else {
|
|
$calculation.element('cbxWithTrailer').unblock();
|
|
}
|
|
}
|
|
);
|
|
|
|
reaction(
|
|
() => $calculation.$values.getValues(['brand', 'model', 'leaseObjectUsed']),
|
|
async ({ brand: brandId, model: modelId, leaseObjectUsed }) => {
|
|
const tbxImporterRewardPerc = $calculation.element('tbxImporterRewardPerc');
|
|
const tbxImporterRewardRub = $calculation.element('tbxImporterRewardRub');
|
|
|
|
if (leaseObjectUsed) {
|
|
tbxImporterRewardPerc.resetValue();
|
|
tbxImporterRewardRub.resetValue();
|
|
|
|
return;
|
|
}
|
|
|
|
let evo_importer_reward_perc = 0;
|
|
let evo_importer_reward_rub = 0;
|
|
|
|
if (modelId) {
|
|
const {
|
|
data: { evo_model },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetModelDocument,
|
|
variables: { modelId },
|
|
});
|
|
|
|
if (evo_model?.evo_importer_reward_perc) {
|
|
evo_importer_reward_perc = evo_model?.evo_importer_reward_perc;
|
|
}
|
|
|
|
if (evo_model?.evo_importer_reward_rub) {
|
|
evo_importer_reward_rub = evo_model?.evo_importer_reward_rub;
|
|
}
|
|
}
|
|
|
|
if (brandId && evo_importer_reward_perc === 0 && evo_importer_reward_rub === 0) {
|
|
const {
|
|
data: { evo_brand },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetBrandDocument,
|
|
variables: { brandId },
|
|
});
|
|
|
|
if (evo_brand?.evo_importer_reward_perc) {
|
|
evo_importer_reward_perc = evo_brand?.evo_importer_reward_perc;
|
|
}
|
|
|
|
if (evo_brand?.evo_importer_reward_rub) {
|
|
evo_importer_reward_rub = evo_brand?.evo_importer_reward_rub;
|
|
}
|
|
}
|
|
|
|
tbxImporterRewardPerc.setValue(evo_importer_reward_perc);
|
|
tbxImporterRewardRub.setValue(evo_importer_reward_rub);
|
|
}
|
|
);
|
|
|
|
reaction(
|
|
() => $calculation.element('selectLeaseObjectType').getValue(),
|
|
async (leaseObjectTypeId) => {
|
|
if (!leaseObjectTypeId) {
|
|
$calculation.element('selectLeaseObjectCategory').resetValue();
|
|
|
|
return;
|
|
}
|
|
|
|
const {
|
|
data: { evo_leasingobject_type },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetLeaseObjectTypeDocument,
|
|
variables: {
|
|
leaseObjectTypeId,
|
|
},
|
|
});
|
|
|
|
if (evo_leasingobject_type?.evo_category) {
|
|
$calculation
|
|
.element('selectLeaseObjectCategory')
|
|
.setValue(evo_leasingobject_type?.evo_category);
|
|
} else {
|
|
$calculation.element('selectLeaseObjectCategory').resetValue();
|
|
}
|
|
}
|
|
);
|
|
|
|
reaction(
|
|
() =>
|
|
$calculation.$values.getValues([
|
|
'leaseObjectType',
|
|
'engineVolume',
|
|
'engineType',
|
|
'leaseObjectMotorPower',
|
|
]),
|
|
async ({ leaseObjectType: leaseObjectTypeId }) => {
|
|
if (!leaseObjectTypeId) {
|
|
$calculation.element('selectEngineType').unblock();
|
|
$calculation.element('tbxEngineVolume').unblock();
|
|
$calculation.element('tbxLeaseObjectMotorPower').unblock();
|
|
|
|
return;
|
|
}
|
|
|
|
const {
|
|
data: { evo_leasingobject_type },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetLeaseObjectTypeDocument,
|
|
variables: {
|
|
leaseObjectTypeId,
|
|
},
|
|
});
|
|
|
|
if (evo_leasingobject_type?.evo_id === '8') {
|
|
$calculation.element('selectEngineType').setValue(null).block();
|
|
$calculation.element('tbxEngineVolume').resetValue().block();
|
|
$calculation.element('tbxLeaseObjectMotorPower').resetValue().block();
|
|
} else {
|
|
$calculation.element('selectEngineType').unblock();
|
|
$calculation.element('tbxEngineVolume').unblock();
|
|
$calculation.element('tbxLeaseObjectMotorPower').unblock();
|
|
}
|
|
}
|
|
);
|
|
}
|