128 lines
4.0 KiB
TypeScript
128 lines
4.0 KiB
TypeScript
/* eslint-disable @typescript-eslint/naming-convention */
|
||
import { gql } from '@apollo/client';
|
||
import dayjs from 'dayjs';
|
||
import utc from 'dayjs/plugin/utc';
|
||
import type * as CRMTypes from 'graphql/crm.types';
|
||
import { autorun, reaction } from 'mobx';
|
||
import type { ReactionsContext } from 'process/types';
|
||
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 default function commonReactions({ store, apolloClient, queryClient }: ReactionsContext) {
|
||
const { $calculation, $tables } = store;
|
||
|
||
reaction(
|
||
() => $calculation.element('selectLeasingWithoutKasko').getValue(),
|
||
(nextValue, prevValue) => {
|
||
if (prevValue && !nextValue) {
|
||
notification.warning({
|
||
key: NOTIFICATION_KEY,
|
||
message: 'Внимание',
|
||
description: 'Лизинг без КАСКО был сброшен',
|
||
});
|
||
}
|
||
}
|
||
);
|
||
|
||
/* eslint-disable max-len */
|
||
/**
|
||
* если "Лизинг без КАСКО" (SelectLeasingWithoutKasko) содержит данные,
|
||
* то в таблице страхования в строке ФинГАП: Страховая компания insCompany = null и закрыть для редактирования
|
||
*/
|
||
/* eslint-enable */
|
||
reaction(
|
||
() => $calculation.element('selectLeasingWithoutKasko').getValue(),
|
||
(leasingWithoutKasko) => {
|
||
if (leasingWithoutKasko) {
|
||
$tables.insurance.row('fingap').resetValue('insuranceCompany').block('insuranceCompany');
|
||
} else {
|
||
$tables.insurance.row('fingap').unblock('insuranceCompany');
|
||
}
|
||
}
|
||
);
|
||
|
||
autorun(async () => {
|
||
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 {
|
||
plPriceRub,
|
||
discountRub,
|
||
importProgramSum,
|
||
leasingPeriod,
|
||
addEquipmentPrice,
|
||
leaseObjectType,
|
||
firstPaymentPerc,
|
||
model: modelId,
|
||
} = $calculation.$values.values;
|
||
|
||
const options = evo_addproduct_types?.filter(
|
||
(x) =>
|
||
x?.evo_max_period &&
|
||
x.evo_max_period >= leasingPeriod &&
|
||
x?.evo_min_period &&
|
||
x.evo_min_period <= leasingPeriod &&
|
||
x?.evo_max_price &&
|
||
x.evo_max_price >= plPriceRub - discountRub - importProgramSum + addEquipmentPrice &&
|
||
x?.evo_min_price &&
|
||
x.evo_min_price <= plPriceRub - discountRub - importProgramSum + addEquipmentPrice &&
|
||
x.evo_leasingobject_types?.find(
|
||
(evo_leasingobject_type) =>
|
||
evo_leasingobject_type?.evo_leasingobject_typeid === leaseObjectType
|
||
) &&
|
||
x.evo_visible_calc &&
|
||
x.evo_min_first_payment_perc &&
|
||
x.evo_min_first_payment_perc <= firstPaymentPerc &&
|
||
x.evo_max_first_payment_perc &&
|
||
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));
|
||
});
|
||
}
|