520 lines
18 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 sonarjs/no-identical-functions */
import * as createReactions from '../lib/create-reactions';
import * as fillAgentsFromLead from '../lib/fill-agents-from-lead';
import * as CRMTypes from '@/graphql/crm.types';
import type { ProcessContext } from '@/process/types';
import { disposableReaction } from '@/utils/mobx';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import { reaction } from 'mobx';
const { fillIndAgent, fillCalcBroker, fillCalcDoubleAgent, fillFinDepartment } = fillAgentsFromLead;
const { fillAgentRewardReaction, fillAgentRewardSummReaction } = createReactions;
dayjs.extend(utc);
class Helper {
public enabled: boolean;
/**
*
*/
constructor() {
this.enabled = false;
}
public set = (value: boolean) => {
this.enabled = value;
};
}
export function common({ store, apolloClient }: ProcessContext) {
const { $calculation, $process } = store;
/**
* Заполняем агентов из Интереса
*/
disposableReaction(
() => $process.has('LoadKP'),
() => $calculation.element('selectLead').getValue(),
(leadid) => {
fillIndAgent(store, apolloClient, leadid);
fillCalcDoubleAgent(store, apolloClient, leadid);
fillCalcBroker(store, apolloClient, leadid);
fillFinDepartment(store, apolloClient, leadid);
}
);
/**
* IndAgent
*/
// Заполняем selectIndAgentRewardCondition
fillAgentRewardReaction(store, apolloClient, {
agentField: 'selectIndAgent',
rewardConditionField: 'selectIndAgentRewardCondition',
});
// Заполняем tbxIndAgentRewardSumm
fillAgentRewardSummReaction(store, apolloClient, {
rewardConditionField: 'selectIndAgentRewardCondition',
rewardSummField: 'tbxIndAgentRewardSumm',
});
/**
* CalcDoubleAgent
*/
// Заполняем selectCalcDoubleAgentRewardCondition
fillAgentRewardReaction(store, apolloClient, {
agentField: 'selectCalcDoubleAgent',
rewardConditionField: 'selectCalcDoubleAgentRewardCondition',
});
// Заполняем tbxCalcDoubleAgentRewardSumm
fillAgentRewardSummReaction(store, apolloClient, {
rewardConditionField: 'selectCalcDoubleAgentRewardCondition',
rewardSummField: 'tbxCalcDoubleAgentRewardSumm',
});
/**
* CalcBroker
*/
// Заполняем selectCalcBrokerRewardCondition
fillAgentRewardReaction(store, apolloClient, {
agentField: 'selectCalcBroker',
rewardConditionField: 'selectCalcBrokerRewardCondition',
});
// Заполняем tbxCalcBrokerRewardSum
fillAgentRewardSummReaction(store, apolloClient, {
rewardConditionField: 'selectCalcBrokerRewardCondition',
rewardSummField: 'tbxCalcBrokerRewardSum',
});
/**
* CalcFinDepartment
*/
// Заполняем selectFinDepartmentRewardCondtion
fillAgentRewardReaction(store, apolloClient, {
agentField: 'selectCalcFinDepartment',
rewardConditionField: 'selectFinDepartmentRewardCondtion',
});
// Заполняем tbxCalcBrokerRewardSum
fillAgentRewardSummReaction(store, apolloClient, {
rewardConditionField: 'selectFinDepartmentRewardCondtion',
rewardSummField: 'tbxFinDepartmentRewardSumm',
});
/**
* Добавить реакцию на изменение списка в поле selectDealerRewardCondition :
* Если в списке selectDealerRewardCondition есть запись,
* у которой evo_reward_condition.evo_agency_agreementid.
* Выплата без других агентов (evo_reward_without_other_agent) = True,
* то сбрасывают значение и закрываются для выбора поля:
*
* selectDealerBroker
* selectIndAgent
* selectCalcDoubleAgent
* selectCalcBroker
* selectFinDepartment
*/
{
const helper = new Helper();
reaction(
() => $calculation.element('selectDealerRewardCondition').getValue(),
async (rewardConditionId) => {
function fillAgents() {
const leadid = $calculation.element('selectLead').getValue();
fillIndAgent(store, apolloClient, leadid);
fillCalcDoubleAgent(store, apolloClient, leadid);
fillCalcBroker(store, apolloClient, leadid);
fillFinDepartment(store, apolloClient, leadid);
}
if (!rewardConditionId) {
if (helper.enabled === true) {
helper.set(false);
fillAgents();
}
return;
}
const {
data: { evo_reward_condition },
} = await apolloClient.query({
query: CRMTypes.GetRewardConditionDocument,
variables: {
conditionId: rewardConditionId,
},
});
if (evo_reward_condition?.evo_agency_agreementidData?.evo_reward_without_other_agent) {
helper.set(true);
$calculation.element('selectDealerBroker').resetValue();
$calculation.element('selectIndAgent').reset();
$calculation.element('selectCalcDoubleAgent').reset();
$calculation.element('selectCalcBroker').reset();
$calculation.element('selectCalcFinDepartment').reset();
} else {
helper.set(false);
fillAgents();
}
}
);
}
/**
* Добавить реакцию на изменение списка в поле selectDealerBrokerRewardCondition :
*
* Если в списке selectDealerBrokerRewardCondition есть запись,
* у которой evo_reward_condition.evo_agency_agreementid.
* Выплата без других агентов (evo_reward_without_other_agent) = True,
* то сбрасывают значение и закрываются для выбора поля:
*
* selectDealerPerson (не обнуляем)
* selectIndAgent
* selectCalcDoubleAgent
* selectCalcBroker
* selectFinDepartment
* иначе
*
* selectDealerPerson открываем для редактирования
* selectIndAgent заполняется значением из Интереса
* selectCalcDoubleAgent заполняется значением из Интереса
* selectCalcBrokerRewardCondition заполняется значением из Интереса
* selectFinDepartmentRewardCondtion заполняется значением из Интереса
*/
{
const helper = new Helper();
reaction(
() => $calculation.element('selectDealerBrokerRewardCondition').getValue(),
async (rewardConditionId) => {
function fillAgents() {
const leadid = $calculation.element('selectLead').getValue();
fillIndAgent(store, apolloClient, leadid);
fillCalcDoubleAgent(store, apolloClient, leadid);
fillCalcBroker(store, apolloClient, leadid);
fillFinDepartment(store, apolloClient, leadid);
}
if (!rewardConditionId) {
if (helper.enabled === true) {
helper.set(false);
fillAgents();
}
return;
}
const {
data: { evo_reward_condition },
} = await apolloClient.query({
query: CRMTypes.GetRewardConditionDocument,
variables: {
conditionId: rewardConditionId,
},
});
if (evo_reward_condition?.evo_agency_agreementidData?.evo_reward_without_other_agent) {
helper.set(true);
$calculation.element('selectDealerPerson').block();
$calculation.element('selectIndAgent').reset();
$calculation.element('selectCalcDoubleAgent').reset();
$calculation.element('selectCalcBroker').reset();
$calculation.element('selectCalcFinDepartment').reset();
} else {
helper.set(false);
$calculation.element('selectDealerPerson').unblock();
fillAgents();
}
}
);
}
/**
* Добавить реакцию на изменение списка в поле selectIndAgentRewardCondition :
*
* Если в списке selectIndAgentRewardCondition есть запись,
* у которой evo_reward_condition.evo_agency_agreementid.
* Выплата без других агентов (evo_reward_without_other_agent) = True,
* то сбрасывают значение и закрываются для выбора поля:
*
* selectDealerPerson (не обнуляем)
* selectDealerBroker
* selectCalcDoubleAgent
* selectCalcBroker
* selectFinDepartment
* иначе
*
* selectDealerPerson открываем для редактирования
* selectDealerBroker открываем для редактирования
* selectCalcDoubleAgent заполняется значением из Интереса
* selectCalcBroker заполняется значением из Интереса
* selectCalcFinDepartment заполняется значением из Интереса
*/
{
const helper = new Helper();
reaction(
() => $calculation.element('selectIndAgentRewardCondition').getValue(),
async (rewardConditionId) => {
function fillAgents() {
const leadid = $calculation.element('selectLead').getValue();
fillCalcDoubleAgent(store, apolloClient, leadid);
fillCalcBroker(store, apolloClient, leadid);
fillFinDepartment(store, apolloClient, leadid);
}
if (!rewardConditionId) {
if (helper.enabled === true) {
helper.set(false);
fillAgents();
}
return;
}
const {
data: { evo_reward_condition },
} = await apolloClient.query({
query: CRMTypes.GetRewardConditionDocument,
variables: {
conditionId: rewardConditionId,
},
});
if (evo_reward_condition?.evo_agency_agreementidData?.evo_reward_without_other_agent) {
helper.set(true);
$calculation.element('selectDealerPerson').block();
$calculation.element('selectDealerBroker').resetValue();
$calculation.element('selectCalcDoubleAgent').reset();
$calculation.element('selectCalcBroker').reset();
$calculation.element('selectCalcFinDepartment').reset();
} else {
helper.set(false);
$calculation.element('selectDealerPerson').unblock();
fillAgents();
}
}
);
}
/**
* Добавить реакцию на изменение списка в поле selectCalcDoubleAgentRewardCondition :
*
* Если в списке selectCalcDoubleAgentRewardCondition есть запись,
* у которой evo_reward_condition.evo_agency_agreementid.
* Выплата без других агентов (evo_reward_without_other_agent) = True,
* то сбрасывают значение и закрываются для выбора поля:
*
* selectDealerPerson (не обнуляем)
* selectDealerBroker
* selectIndAgent
* selectCalcBroker
* selectCalcFinDepartment
* иначе
*
* selectDealerPerson открываем для редактирования
* selectDealerBroker открываем для редактирования
* selectIndAgent заполняется значением из Интереса
* selectCalcBroker заполняется значением из Интереса
* selectCalcFinDepartment заполняется значением из Интереса
*/
{
const helper = new Helper();
reaction(
() => $calculation.element('selectCalcDoubleAgentRewardCondition').getValue(),
async (rewardConditionId) => {
function fillAgents() {
const leadid = $calculation.element('selectLead').getValue();
fillIndAgent(store, apolloClient, leadid);
fillCalcBroker(store, apolloClient, leadid);
fillFinDepartment(store, apolloClient, leadid);
}
if (!rewardConditionId) {
if (helper.enabled === true) {
helper.set(false);
fillAgents();
}
return;
}
const {
data: { evo_reward_condition },
} = await apolloClient.query({
query: CRMTypes.GetRewardConditionDocument,
variables: {
conditionId: rewardConditionId,
},
});
if (evo_reward_condition?.evo_agency_agreementidData?.evo_reward_without_other_agent) {
helper.set(true);
$calculation.element('selectDealerPerson').block();
$calculation.element('selectDealerBroker').resetValue();
$calculation.element('selectIndAgent').reset();
$calculation.element('selectCalcBroker').reset();
$calculation.element('selectCalcFinDepartment').reset();
} else {
helper.set(false);
$calculation.element('selectDealerPerson').unblock();
$calculation.element('selectDealerBroker').unblock();
fillAgents();
}
}
);
}
/**
* @description
* Добавить реакцию на изменение списка в поле selectCalcBrokerRewardCondition:
*
* Если в списке selectCalcBrokerRewardCondition есть запись,
* у которой evo_reward_condition.evo_agency_agreementid.
* Выплата без других агентов (evo_reward_without_other_agent) = True,
* то сбрасывают значение и закрываются для выбора поля:
*
* selectDealerPerson (не обнуляем)
* selectDealerBroker
* selectIndAgent
* selectCalcDoubleAgent
* selectCalcFinDepartment
* иначе
*
* selectDealerPerson открываем для редактирования
* selectDealerBroker открываем для редактирования
* selectIndAgent заполняется значением из Интереса
* selectCalcDoubleAgent заполняется значением из Интереса
* selectCalcFinDepartment заполняется значением из Интереса
*/
{
const helper = new Helper();
reaction(
() => $calculation.element('selectCalcBrokerRewardCondition').getValue(),
async (rewardConditionId) => {
function fillAgents() {
const leadid = $calculation.element('selectLead').getValue();
fillIndAgent(store, apolloClient, leadid);
fillCalcDoubleAgent(store, apolloClient, leadid);
fillFinDepartment(store, apolloClient, leadid);
}
if (!rewardConditionId) {
if (helper.enabled === true) {
helper.set(false);
fillAgents();
}
return;
}
const {
data: { evo_reward_condition },
} = await apolloClient.query({
query: CRMTypes.GetRewardConditionDocument,
variables: {
conditionId: rewardConditionId,
},
});
if (evo_reward_condition?.evo_agency_agreementidData?.evo_reward_without_other_agent) {
helper.set(true);
$calculation.element('selectDealerPerson').block();
$calculation.element('selectDealerBroker').resetValue();
$calculation.element('selectIndAgent').reset();
$calculation.element('selectCalcDoubleAgent').reset();
$calculation.element('selectCalcFinDepartment').reset();
} else {
helper.set(false);
$calculation.element('selectDealerPerson').unblock();
$calculation.element('selectDealerBroker').unblock();
fillAgents();
}
}
);
}
/**
* Добавить реакцию на изменение списка в поле selectFinDepartmentRewardCondtion:
*
* Если в списке selectFinDepartmentRewardCondtion есть запись,
* у которой evo_reward_condition.evo_agency_agreementid.
* Выплата без других агентов (evo_reward_without_other_agent) = True,
* то сбрасывают значение и закрываются для выбора поля:
*
* selectDealerPerson (не обнуляем)
* selectDealerBroker
* selectIndAgent
* selectCalcDoubleAgent
* selectCalcBroker
* иначе
*
* selectDealerPerson открываем для редактирования
* selectDealerBroker открываем для редактирования
* selectIndAgent заполняется значением из Интереса
* selectCalcDoubleAgent заполняется значением из Интереса
* selectCalcBroker заполняется значением из Интереса
*/
{
const helper = new Helper();
reaction(
() => $calculation.element('selectFinDepartmentRewardCondtion').getValue(),
async (rewardConditionId) => {
function fillAgents() {
const leadid = $calculation.element('selectLead').getValue();
fillIndAgent(store, apolloClient, leadid);
fillCalcDoubleAgent(store, apolloClient, leadid);
fillFinDepartment(store, apolloClient, leadid);
}
if (!rewardConditionId) {
if (helper.enabled === true) {
helper.set(false);
fillAgents();
}
return;
}
const {
data: { evo_reward_condition },
} = await apolloClient.query({
query: CRMTypes.GetRewardConditionDocument,
variables: {
conditionId: rewardConditionId,
},
});
if (evo_reward_condition?.evo_agency_agreementidData?.evo_reward_without_other_agent) {
helper.set(true);
$calculation.element('selectDealerPerson').block();
$calculation.element('selectDealerBroker').resetValue();
$calculation.element('selectIndAgent').reset();
$calculation.element('selectCalcDoubleAgent').reset();
$calculation.element('selectCalcBroker').reset();
} else {
helper.set(false);
$calculation.element('selectDealerPerson').unblock();
$calculation.element('selectDealerBroker').unblock();
fillAgents();
}
}
);
}
}