2024-05-22 13:06:31 +03:00

171 lines
5.0 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 { reaction } from 'mobx';
import { sift } from 'radash';
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(' | ')}`,
}))
)
);
} else {
$calculation.element('selectDealerPerson').reset();
}
}
);
/**
* Заполняем selectDealerBroker
*/
reaction(
() => $calculation.element('selectDealerPerson').getValue(),
async (dealerPersonId) => {
if (!dealerPersonId) {
$calculation.element('selectDealerBroker').reset();
return;
}
const {
data: { dealer_person },
} = await apolloClient.query({
query: CRMTypes.GetDealerPersonDocument,
variables: {
dealerPersonId,
},
});
if (dealer_person?.evo_broker_accountid) {
const {
data: { agent: dealerBroker },
} = await apolloClient.query<CRMTypes.GetAgentQuery, CRMTypes.GetAgentQueryVariables>({
query: CRMTypes.GetAgentDocument,
variables: {
agentid: dealer_person?.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']),
async ({ product: productId }) => {
let evo_baseproduct: CRMTypes.GetProductQuery['evo_baseproduct'] = null;
if (productId) {
const { data } = await apolloClient.query({
query: CRMTypes.GetProductDocument,
variables: { productId },
});
({ evo_baseproduct } = data);
}
$calculation
.element('cbxSupplierFinancing')
.setValue(Boolean(evo_baseproduct?.evo_supplier_financing_accept));
}
);
}