215 lines
6.6 KiB
TypeScript
215 lines
6.6 KiB
TypeScript
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));
|
||
}
|
||
);
|
||
|
||
{
|
||
// eslint-disable-next-line no-inner-declarations
|
||
function unblock() {
|
||
$calculation.element('cbxPartialVAT').unblock();
|
||
$calculation.element('tbxVATInLeaseObjectPrice').unblock();
|
||
$calculation.element('cbxInsDecentral').unblock();
|
||
$calculation.element('selectDealerPerson').unblock();
|
||
$calculation.element('selectDealerRewardCondition').unblock();
|
||
$calculation.element('selectDealerBroker').unblock();
|
||
$calculation.element('selectDealerBrokerRewardCondition').unblock();
|
||
}
|
||
|
||
reaction(
|
||
() => $calculation.element('selectDealerPerson').getValue(),
|
||
async (dealerPersonId) => {
|
||
if (!dealerPersonId) {
|
||
unblock();
|
||
|
||
return;
|
||
}
|
||
|
||
const { data } = await apolloClient.query({
|
||
query: CRMTypes.GetDealerPersonDocument,
|
||
variables: {
|
||
dealerPersonId,
|
||
},
|
||
});
|
||
|
||
if (data?.dealer_person?.evo_legal_form === 100_000_004) {
|
||
$calculation.element('cbxPartialVAT').setValue(true).block();
|
||
$calculation.element('tbxVATInLeaseObjectPrice').resetValue().block();
|
||
$calculation.element('cbxInsDecentral').setValue(false).block();
|
||
$calculation.element('selectDealerRewardCondition').block();
|
||
$calculation.element('tbxDealerRewardSumm').resetValue().block();
|
||
$calculation.element('selectDealerBroker').resetValue().block();
|
||
$calculation.element('selectDealerBrokerRewardCondition').resetValue().block();
|
||
$calculation.element('tbxDealerBrokerRewardSumm').resetValue().block();
|
||
} else {
|
||
unblock();
|
||
}
|
||
}
|
||
);
|
||
}
|
||
}
|