2023-04-14 08:45:47 +03:00

195 lines
5.8 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.

import * as createReactions from '../lib/create-reactions';
import * as CRMTypes from '@/graphql/crm.types';
import type { ProcessContext } from '@/process/types';
import { normalizeOptions } from '@/utils/entity';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import { reaction } from 'mobx';
import { sift } from 'radash';
dayjs.extend(utc);
export function common({ store, apolloClient }: ProcessContext) {
const { $calculation, $process } = store;
/**
* Заполняем selectDealerPerson
* На изменение Салон приобретения формируем список в поле ЮЛ поставщика - записи Контрагент,
* у которых статус = активный И Поставщик = Да И Тип поставщика = Юридическое лицо
* И связаны с карточкой Контрагент из поля "Салон приобретения" по связи Салон-ЮЛ
*/
reaction(
() => $calculation.element('selectDealer').getValue(),
async (dealerId) => {
if (!dealerId) {
$calculation.element('selectDealerPerson').reset();
return;
}
const {
data: { dealer },
} = await apolloClient.query({
query: CRMTypes.GetDealerDocument,
variables: {
dealerId,
},
});
if (dealer?.evo_return_leasing_dealer) {
return;
}
const {
data: { dealerPersons },
} = await apolloClient.query({
query: CRMTypes.GetDealerPersonsDocument,
variables: {
dealerId,
},
});
if (dealerPersons && dealerPersons.length > 0) {
$calculation.element('selectDealerPerson').setOptions(
normalizeOptions(
dealerPersons.map((dp) => ({
...dp,
label: `${dp?.label} ${sift([dp?.evo_inn, dp?.evo_kpp]).join(' | ')}`,
}))
)
);
const value = dealerPersons.at(0)?.value;
if (!$process.has('LoadKP') && value) {
$calculation.element('selectDealerPerson').setValue(value);
}
} else {
$calculation.element('selectDealerPerson').reset();
}
}
);
/**
* Заполняем selectDealerBroker
*/
reaction(
() => $calculation.element('selectDealerPerson').getValue(),
async (dealerPersonId) => {
if (!dealerPersonId) {
$calculation.element('selectDealerBroker').reset();
return;
}
const {
data: { dealer },
} = await apolloClient.query({
query: CRMTypes.GetDealerDocument,
variables: {
dealerId: dealerPersonId,
},
});
if (dealer?.evo_broker_accountid) {
const {
data: { agent: dealerBroker },
} = await apolloClient.query<CRMTypes.GetAgentQuery, CRMTypes.GetAgentQueryVariables>({
query: CRMTypes.GetAgentDocument,
variables: {
agentid: dealer?.evo_broker_accountid,
},
});
if (dealerBroker) {
$calculation.element('selectDealerBroker').setOptions(normalizeOptions([dealerBroker]));
if (!$process.has('LoadKP')) {
$calculation.element('selectDealerBroker').setValue(dealerBroker.value);
}
}
} else {
$calculation.element('selectDealerBroker').reset();
}
}
);
// Заполняем selectDealerRewardCondition
createReactions.fillAgentRewardReaction(store, apolloClient, {
agentField: 'selectDealerPerson',
rewardConditionField: 'selectDealerRewardCondition',
});
// Заполняем tbxDealerRewardSumm
createReactions.fillAgentRewardSummReaction(store, apolloClient, {
rewardConditionField: 'selectDealerRewardCondition',
rewardSummField: 'tbxDealerRewardSumm',
});
// Заполняем selectDealerBrokerRewardCondition
createReactions.fillAgentRewardReaction(store, apolloClient, {
agentField: 'selectDealerBroker',
rewardConditionField: 'selectDealerBrokerRewardCondition',
});
// Заполняем tbxDealerBrokerRewardSumm
createReactions.fillAgentRewardSummReaction(store, apolloClient, {
rewardConditionField: 'selectDealerBrokerRewardCondition',
rewardSummField: 'tbxDealerBrokerRewardSumm',
});
reaction(
() => $calculation.element('selectDealerRewardCondition').getValue(),
(dealerRewardConditionId) => {
if (dealerRewardConditionId) {
$calculation.element('selectDealerBroker').resetValue();
}
}
);
reaction(
() => $calculation.element('selectDealerBroker').getValue(),
(dealerBroker) => {
if (dealerBroker) {
$calculation.element('selectDealerRewardCondition').resetValue();
}
}
);
reaction(
() => $calculation.$values.getValues(['product', 'dealerPerson']),
async ({ product: productId, dealerPerson: dealerPersonId }) => {
let evo_baseproduct: CRMTypes.GetProductQuery['evo_baseproduct'] = null;
let dealerPerson: CRMTypes.GetDealerPersonQuery['account'] = null;
if (productId) {
const { data } = await apolloClient.query({
query: CRMTypes.GetProductDocument,
variables: { productId },
});
({ evo_baseproduct } = data);
}
if (dealerPersonId) {
const { data } = await apolloClient.query({
query: CRMTypes.GetDealerPersonDocument,
variables: { dealerPersonId },
});
({ account: dealerPerson } = data);
}
if (
evo_baseproduct?.evo_supplier_financing_accept &&
dealerPerson?.evo_supplier_financing_accept
) {
$calculation.element('cbxSupplierFinancing').unblock();
} else {
$calculation.element('cbxSupplierFinancing').setValue(false).block();
}
}
);
}