2023-02-28 09:46:31 +03:00

140 lines
4.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* eslint-disable canonical/sort-keys */
/* eslint-disable @typescript-eslint/naming-convention */
import type * as CRMTypes from '@/graphql/crm.types';
import type { ProcessContext } from '@/process/types';
import { gql } from '@apollo/client';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import { reaction } from 'mobx';
import { uid } from 'radash';
import { normalizeOptions } from 'tools';
import notification from 'ui/elements/notification';
dayjs.extend(utc);
const NOTIFICATION_KEY = uid(7);
const QUERY_GET_LEASING_WITHOUT_KASKO_OPTIONS = gql`
query GetLeasingWithoutKaskoOptions($currentDate: DateTime) {
evo_addproduct_types(
statecode: 0
evo_product_type: 100000007
evo_datefrom_param: { lte: $currentDate }
evo_dateto_param: { gte: $currentDate }
) {
label: evo_name
value: evo_addproduct_typeid
evo_product_type
evo_min_period
evo_max_period
evo_min_price
evo_max_price
evo_leasingobject_types {
evo_leasingobject_typeid
}
evo_visible_calc
evo_min_first_payment_perc
evo_max_first_payment_perc
evo_models {
evo_modelid
}
}
}
`;
export function common({ store, apolloClient }: ProcessContext) {
const { $calculation, $tables } = store;
reaction(
() => $calculation.element('selectLeasingWithoutKasko').getValue(),
(nextValue, prevValue) => {
if (prevValue && !nextValue) {
notification.warning({
key: NOTIFICATION_KEY,
message: 'Внимание',
description: 'Лизинг без КАСКО был сброшен',
});
}
}
);
/**
* если "Лизинг без КАСКО" (SelectLeasingWithoutKasko) содержит данные,
* то в таблице страхования в строке ФинГАП: Страховая компания insCompany = null и закрыть для редактирования
*/
reaction(
() => $calculation.element('selectLeasingWithoutKasko').getValue(),
(leasingWithoutKasko) => {
if (leasingWithoutKasko) {
$tables.insurance.row('fingap').resetValue('insuranceCompany').block('insuranceCompany');
} else {
$tables.insurance.row('fingap').unblock('insuranceCompany');
}
}
);
reaction(
() =>
$calculation.$values.getValues([
'plPriceRub',
'discountRub',
'importProgramSum',
'leasingPeriod',
'addEquipmentPrice',
'leaseObjectType',
'firstPaymentPerc',
'model',
]),
async ({
plPriceRub,
discountRub,
importProgramSum,
leasingPeriod,
addEquipmentPrice,
leaseObjectType,
firstPaymentPerc,
model: modelId,
}) => {
const currentDate = dayjs().utc(false).toISOString();
const {
data: { evo_addproduct_types },
} = await apolloClient.query<
CRMTypes.GetLeasingWithoutKaskoOptionsQuery,
CRMTypes.GetLeasingWithoutKaskoOptionsQueryVariables
>({
query: QUERY_GET_LEASING_WITHOUT_KASKO_OPTIONS,
variables: {
currentDate,
},
});
const price = plPriceRub - discountRub - importProgramSum + addEquipmentPrice;
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 >= price) &&
Boolean(x.evo_min_price !== null && x.evo_min_price <= price) &&
x.evo_leasingobject_types?.find(
(evo_leasingobject_type) =>
evo_leasingobject_type?.evo_leasingobject_typeid === leaseObjectType
) &&
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
) &&
!x.evo_models?.map((evo_model) => evo_model?.evo_modelid).includes(modelId)
);
$calculation.element('selectLeasingWithoutKasko').setOptions(normalizeOptions(options));
}
);
}