From ade824355f43a7d1d6713a2b33bbd6ef43b9a0fd Mon Sep 17 00:00:00 2001 From: Chika Date: Tue, 8 Dec 2020 19:13:19 +0300 Subject: [PATCH] loadkp reaction --- package.json | 1 + src/client/hooks/Calculation/useValue.js | 10 +- src/client/services/CrmService/client.ts | 2 - src/client/services/CrmService/index.ts | 1 + .../stores/CalculationStore/Data/values.js | 3 + .../CalculationStore/Effects/reaction.ts | 3359 ----------------- .../Effects/reactions/index.ts | 11 + .../Effects/reactions/loadKpReaction/index.ts | 359 ++ .../loadKpReaction/mapKpToValues.ts} | 15 - .../Effects/reactions/otherReactions.ts | 1708 +++++++++ .../Effects/reactions/requestReactions.ts | 1162 ++++++ .../Effects/reactions/tablesReactions.ts | 716 ++++ .../config/initialTables/tableInsurance.ts | 26 +- .../config/initialTables/tablePayments.ts | 12 +- src/client/stores/CalculationStore/index.ts | 20 +- src/client/stores/index.js | 3 +- src/core/types/Calculation/Requests.ts | 9 +- src/core/types/Calculation/Store/effect.ts | 8 +- src/core/types/Calculation/Store/index.ts | 1 + src/core/types/Calculation/Store/process.ts | 4 + src/core/types/Calculation/Store/tables.ts | 2 + src/core/types/Entities/crmEntities.ts | 20 + 22 files changed, 4058 insertions(+), 3394 deletions(-) delete mode 100644 src/client/stores/CalculationStore/Effects/reaction.ts create mode 100644 src/client/stores/CalculationStore/Effects/reactions/index.ts create mode 100644 src/client/stores/CalculationStore/Effects/reactions/loadKpReaction/index.ts rename src/client/{constants/Calculation/kpToValues.ts => stores/CalculationStore/Effects/reactions/loadKpReaction/mapKpToValues.ts} (85%) create mode 100644 src/client/stores/CalculationStore/Effects/reactions/otherReactions.ts create mode 100644 src/client/stores/CalculationStore/Effects/reactions/requestReactions.ts create mode 100644 src/client/stores/CalculationStore/Effects/reactions/tablesReactions.ts create mode 100644 src/core/types/Calculation/Store/process.ts diff --git a/package.json b/package.json index 5ef591e..4f7348e 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "ts-loader": "^8.0.2", "typescript": "3.9.7", "use-debounce": "^3.4.3", + "uuid": "^8.3.1", "validator": "^13.1.1" }, "devDependencies": { diff --git a/src/client/hooks/Calculation/useValue.js b/src/client/hooks/Calculation/useValue.js index a9cac99..8f4536e 100644 --- a/src/client/hooks/Calculation/useValue.js +++ b/src/client/hooks/Calculation/useValue.js @@ -36,7 +36,7 @@ export const useStoreValue = ({ valueName }) => { }; export const useTableValue = ({ tableName, rowIndex, propName }) => { - const { calculationStore } = useStores(); + const { calculationStore, calculationProcess } = useStores(); const [currentValue, setCurrentValue] = useState(undefined); const [debouncedValue] = useDebounce(currentValue, DEFAULT_DEBOUNCE_DELAY); @@ -66,7 +66,13 @@ export const useTableValue = ({ tableName, rowIndex, propName }) => { useEffect(() => { if (cellCallBack) { - cellCallBack(calculationStore, tableName, rowIndex, debouncedValue); + cellCallBack( + calculationStore, + calculationProcess, + tableName, + rowIndex, + debouncedValue, + ); } }, [debouncedValue, cellCallBack]); diff --git a/src/client/services/CrmService/client.ts b/src/client/services/CrmService/client.ts index 37ef0ae..bd4343a 100644 --- a/src/client/services/CrmService/client.ts +++ b/src/client/services/CrmService/client.ts @@ -5,6 +5,4 @@ import { CRM_PROXY_URL } from 'core/constants/urls'; export default new ApolloClient({ uri: getServerUrl('/proxy', CRM_PROXY_URL), cache: new InMemoryCache(), - - }); diff --git a/src/client/services/CrmService/index.ts b/src/client/services/CrmService/index.ts index ab01bc7..c85941e 100644 --- a/src/client/services/CrmService/index.ts +++ b/src/client/services/CrmService/index.ts @@ -40,6 +40,7 @@ export default class { resEntities[targetName]; if (toOptions) + //@ts-ignore if (toOptions.includes(targetName)) { if (Array.isArray(targetEnt)) { let optionatedEntities: (TCRMEntity & IBaseOption)[] = []; diff --git a/src/client/stores/CalculationStore/Data/values.js b/src/client/stores/CalculationStore/Data/values.js index 84bc694..45e9b4f 100644 --- a/src/client/stores/CalculationStore/Data/values.js +++ b/src/client/stores/CalculationStore/Data/values.js @@ -18,6 +18,9 @@ const valuesActions = { setValue(sourceValueName, newValue) { this.values[sourceValueName] = newValue; }, + setValues(values){ + this.values = values; + }, getStatus(elementName) { return this.statuses[elementName]; diff --git a/src/client/stores/CalculationStore/Effects/reaction.ts b/src/client/stores/CalculationStore/Effects/reaction.ts deleted file mode 100644 index 27882be..0000000 --- a/src/client/stores/CalculationStore/Effects/reaction.ts +++ /dev/null @@ -1,3359 +0,0 @@ -import { gql } from '@apollo/client'; -import { openNotification } from 'client/Elements/Notification'; -import CrmService from 'client/services/CrmService'; -import { currentDate } from 'client/tools/date'; -import { shift, shiftRight } from 'core/tools/array'; -import { IReactionEffect } from 'core/types/Calculation/Store/effect'; -import { ITableCell, TableProps } from 'core/types/Calculation/Store/tables'; -import { ElementStatus } from 'core/types/statuses'; -import { toJS } from 'mobx'; -import { calcPrice, calculatePerc, calculateRub } from './lib/tools'; - -const reactionEffects: IReactionEffect[] = [ - calculationStore => ({ - expression: () => { - const { values } = calculationStore; - return values.lead; - }, - effect: async leadId => { - if (!leadId) { - calculationStore.setValue('opportunity', null); - calculationStore.setFilter('selectOpportunity', undefined); - calculationStore.setValue('quote', null); - calculationStore.setOptions('selectQuote', []); - calculationStore.setOptions('selectIndAgent', []); - calculationStore.setValue('indAgent', null); - calculationStore.setOptions('selectCalcDoubleAgent', []); - calculationStore.setValue('calcDoubleAgent', null); - calculationStore.setOptions('selectCalcBroker', []); - calculationStore.setValue('calcBroker', null); - calculationStore.setOptions('selectCalcFinDepartment', []); - calculationStore.setValue('calcFinDepartment', null); - return; - } - const lead = calculationStore.options.selectLead?.find( - x => x.leadid === leadId, - ); - - if (lead) { - if (lead.evo_opportunityid) { - calculationStore.setValue('opportunity', lead.evo_opportunityid); - calculationStore.setFilter('selectOpportunity', opportunities => - opportunities.filter( - x => x.opportunityid === lead.evo_opportunityid, - ), - ); - } else { - calculationStore.setFilter('selectOpportunity', () => []); - calculationStore.setValue('opportunity', null); - } - - if (lead.leadid) { - CrmService.crmgqlquery({ - query: gql` - query($leadid: Uuid) { - quote: quotes(evo_leadid: $leadid) { - quoteid - name - evo_quotename - quotenumber - evo_recalc_limit - evo_approved_first_payment - evo_statuscode: evo_statuscodeidData { - evo_id - } - } - } - `, - variables: { - leadid: leadId, - }, - toOptions: ['quote'], - }).then(({ entities }) => { - if ( - entities.quote && - Array.isArray(entities.quote) && - entities.quote.length > 0 - ) { - calculationStore.setOptions('selectQuote', entities.quote); - calculationStore.setValue('quote', null); - } else { - calculationStore.setOptions('selectQuote', []); - calculationStore.setValue('quote', null); - } - }); - } else { - calculationStore.setOptions('selectQuote', []); - calculationStore.setValue('quote', null); - } - - if (lead.evo_agent_accountid) { - CrmService.crmgqlquery({ - query: gql` - query($evo_agent_accountid: Uuid!) { - account(accountid: $evo_agent_accountid) { - accountid - name - } - } - `, - variables: { - evo_agent_accountid: lead.evo_agent_accountid, - }, - toOptions: ['account'], - }).then(({ entities }) => { - if (entities.account) { - if (!Array.isArray(entities.account)) { - calculationStore.setOptions('selectIndAgent', [ - entities.account, - ]); - calculationStore.setValue( - 'indAgent', - entities.account.accountid, - ); - } - } else { - calculationStore.setValue('indAgent', null); - calculationStore.setOptions('selectIndAgent', []); - } - }); - } else { - calculationStore.setValue('indAgent', null); - calculationStore.setOptions('selectIndAgent', []); - } - - if (lead.evo_double_agent_accountid) { - CrmService.crmgqlquery({ - query: gql` - query($evo_double_agent_accountid: Uuid!) { - account(accountid: $evo_double_agent_accountid) { - accountid - name - } - } - `, - variables: { - evo_double_agent_accountid: lead.evo_double_agent_accountid, - }, - toOptions: ['account'], - }).then(({ entities }) => { - if (entities.account) { - if (!Array.isArray(entities.account)) { - calculationStore.setOptions('selectCalcDoubleAgent', [ - entities.account, - ]); - calculationStore.setValue( - 'calcDoubleAgent', - entities.account.accountid, - ); - } - } else { - calculationStore.setValue('calcDoubleAgent', null); - calculationStore.setOptions('selectCalcDoubleAgent', []); - } - }); - } else { - calculationStore.setValue('calcDoubleAgent', null); - calculationStore.setOptions('selectCalcDoubleAgent', []); - } - - if (lead.evo_broker_accountid) { - CrmService.crmgqlquery({ - query: gql` - query($evo_broker_accountid: Uuid!) { - account(accountid: $evo_broker_accountid) { - accountid - name - } - } - `, - variables: { - evo_broker_accountid: lead.evo_broker_accountid, - }, - toOptions: ['account'], - }).then(({ entities }) => { - if (entities.account) { - if (!Array.isArray(entities.account)) { - calculationStore.setOptions('selectCalcBroker', [ - entities.account, - ]); - calculationStore.setValue( - 'calcBroker', - entities.account.accountid, - ); - } - } - }); - } else { - calculationStore.setValue('calcBroker', null); - calculationStore.setOptions('selectCalcBroker', []); - } - - if (lead.evo_fin_department_accountid) { - CrmService.crmgqlquery({ - query: gql` - query($evo_fin_department_accountid: Uuid!) { - account(accountid: $evo_fin_department_accountid) { - accountid - name - } - } - `, - variables: { - evo_fin_department_accountid: lead.evo_fin_department_accountid, - }, - toOptions: ['account'], - }).then(({ entities }) => { - if (entities.account) { - if (!Array.isArray(entities.account)) { - calculationStore.setOptions('selectCalcFinDepartment', [ - entities.account, - ]); - calculationStore.setValue( - 'calcFinDepartment', - entities.account.accountid, - ); - } - } - }); - } else { - calculationStore.setValue('calcFinDepartment', null); - calculationStore.setOptions('selectCalcFinDepartment', []); - } - } - }, - }), - - // calculationStore => ({ - // expression: () => { - // const { options } = calculationStore; - // return options.selectQuote; - // }, - // effect: quotes => { - // if (quotes.length > 0) { - // calculationStore.setStatus('tbxQuoteName', Status.Disabled); - // } else { - // calculationStore.setStatus('tbxQuoteName', Status.Default); - // } - // }, - // }), - - calculationStore => ({ - expression: () => { - const { opportunity } = calculationStore.values; - return opportunity; - }, - effect: opportunityId => { - const opportunity = calculationStore.options.selectOpportunity?.find( - x => x.opportunityid === opportunityId, - ); - - if (opportunity) { - calculationStore.setValue('lead', opportunity.evo_leadid); - } - }, - }), - - calculationStore => ({ - expression: () => { - const { lead, opportunity } = calculationStore.values; - return { leadid: lead, opportunityid: opportunity }; - }, - effect: ({ leadid, opportunityid }) => { - if (opportunityid) { - const opportunity = calculationStore.options.selectOpportunity?.find( - x => x.opportunityid === opportunityid, - ); - if (opportunity) { - if (opportunity.evo_client_riskid) { - calculationStore.setValue( - 'clientRisk', - opportunity.evo_client_riskid, - ); - } else { - if (opportunity.parentaccountid) - CrmService.crmgqlquery({ - query: gql` - query($accountid: Uuid!) { - account(accountid: $accountid) { - evo_client_riskid - } - } - `, - variables: { accountid: opportunity.parentaccountid }, - }).then(({ entities }) => { - if ( - entities.account && - !Array.isArray(entities.account) && - entities.account.evo_client_riskid - ) - calculationStore.setValue( - 'clientRisk', - entities.account.evo_client_riskid, - ); - }); - } - } - } else if (leadid && !opportunityid) { - const lead = calculationStore.options.selectLead?.find( - x => x.leadid === leadid, - ); - if (lead) { - if (lead.customerid) { - CrmService.crmgqlquery({ - query: gql` - query($customerid: Uuid!) { - account(accountid: $customerid) { - evo_client_riskid - } - } - `, - variables: { customerid: lead.customerid }, - }).then(({ entities }) => { - if ( - entities.account && - !Array.isArray(entities.account) && - entities.account.evo_client_riskid - ) - calculationStore.setValue( - 'clientRisk', - entities.account.evo_client_riskid, - ); - }); - } - } - } else { - calculationStore.setValue('clientRisk', null); - } - }, - }), - - calculationStore => ({ - expression: () => { - const { agent } = calculationStore.values; - return agent; - }, - effect: agentid => { - if (!agentid) { - calculationStore.setStatus('selectDoubleAgent', ElementStatus.Disabled); - } else { - calculationStore.setStatus('selectDoubleAgent', ElementStatus.Default); - } - }, - }), - - calculationStore => ({ - expression: () => { - const { channel } = calculationStore.values; - return channel; - }, - effect: channel => { - switch (channel) { - case 100000000: - calculationStore.setStatus('selectSupplier', ElementStatus.Default); - calculationStore.setStatus('selectAgent', ElementStatus.Default); - - calculationStore.setStatus('selectFinDepartment', ElementStatus.Disabled); - calculationStore.setValue('finDepartment', null); - - calculationStore.setStatus('selectBroker', ElementStatus.Disabled); - calculationStore.setValue('broker', null); - break; - - case 100000001: - calculationStore.setStatus('selectSupplier', ElementStatus.Default); - calculationStore.setStatus('selectAgent', ElementStatus.Default); - - calculationStore.setStatus('selectFinDepartment', ElementStatus.Default); - - calculationStore.setStatus('selectBroker', ElementStatus.Disabled); - calculationStore.setValue('broker', null); - break; - case 100000002: - calculationStore.setStatus('selectSupplier', ElementStatus.Disabled); - calculationStore.setValue('supplier', null); - - calculationStore.setStatus('selectAgent', ElementStatus.Default); - - calculationStore.setStatus('selectFinDepartment', ElementStatus.Disabled); - calculationStore.setValue('finDepartment', null); - - calculationStore.setStatus('selectBroker', ElementStatus.Disabled); - calculationStore.setValue('broker', null); - break; - case 100000003: - calculationStore.setStatus('selectSupplier', ElementStatus.Disabled); - calculationStore.setValue('supplier', null); - - calculationStore.setStatus('selectAgent', ElementStatus.Default); - calculationStore.setValue('agent', null); - - calculationStore.setStatus('selectFinDepartment', ElementStatus.Disabled); - calculationStore.setValue('finDepartment', null); - - calculationStore.setStatus('selectBroker', ElementStatus.Default); - break; - case 100000004: - default: - calculationStore.setStatus('selectSupplier', ElementStatus.Disabled); - calculationStore.setValue('supplier', null); - - calculationStore.setStatus('selectAgent', ElementStatus.Disabled); - calculationStore.setValue('agent', null); - - calculationStore.setStatus('selectFinDepartment', ElementStatus.Disabled); - calculationStore.setValue('finDepartment', null); - - calculationStore.setStatus('selectBroker', ElementStatus.Disabled); - calculationStore.setValue('broker', null); - break; - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { newClient } = calculationStore.values; - return newClient; - }, - effect: newClient => { - if (newClient && newClient.length > 0) { - calculationStore.setValue('account', null); - calculationStore.setValue('contactClient', null); - calculationStore.setStatus('selectAccount', ElementStatus.Disabled); - calculationStore.setStatus('selectContactClient', ElementStatus.Disabled); - } else { - calculationStore.setStatus('selectAccount', ElementStatus.Default); - calculationStore.setStatus('selectContactClient', ElementStatus.Default); - } - }, - }), - - calculationStore => ({ - expression: () => { - const { account } = calculationStore.values; - return account; - }, - effect: account => { - if (account && account.length > 0) { - calculationStore.setStatus('tbxNewClient', ElementStatus.Disabled); - calculationStore.setValue('newClient', null); - } else { - calculationStore.setStatus('tbxNewClient', ElementStatus.Default); - } - }, - }), - - calculationStore => ({ - expression: () => { - const { indAgent } = calculationStore.values; - return indAgent; - }, - effect: indAgentId => { - if (!indAgentId) { - calculationStore.setValue('indAgentRewardCondition', null); - calculationStore.setStatus( - 'selectIndAgentRewardCondition', - ElementStatus.Disabled, - ); - } else { - calculationStore.setValue('indAgentRewardCondition', null); - calculationStore.setStatus( - 'selectIndAgentRewardCondition', - ElementStatus.Default, - ); - - if (indAgentId) - CrmService.crmgqlquery({ - query: gql` - query( - $statecode: Int - $currentDate: DateTime - $evo_agent_accountid: Uuid - ) { - evo_reward_condition: evo_reward_conditions( - evo_agent_accountid: $evo_agent_accountid - evo_datefrom_param: { lte: $currentDate } - evo_dateto_param: { gte: $currentDate } - statecode: $statecode - ) { - evo_reward_conditionid - evo_name - evo_double_agent_accountid - evo_reward_summ - evo_reduce_reward - } - } - `, - variables: { - evo_agent_accountid: indAgentId, - currentDate, - statecode: 0, - }, - toOptions: ['evo_reward_condition'], - }).then(({ entities: { evo_reward_condition } }) => { - if (evo_reward_condition && Array.isArray(evo_reward_condition)) - calculationStore.setOptions( - 'selectIndAgentRewardCondition', - evo_reward_condition, - ); - }); - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { calcDoubleAgent } = calculationStore.values; - return calcDoubleAgent; - }, - effect: doubleAgentId => { - if (!doubleAgentId) { - calculationStore.setValue('calcDoubleAgentRewardCondition', null); - calculationStore.setStatus( - 'selectCalcDoubleAgentRewardCondition', - ElementStatus.Disabled, - ); - } else { - calculationStore.setValue('calcDoubleAgentRewardCondition', null); - calculationStore.setStatus( - 'selectCalcDoubleAgentRewardCondition', - ElementStatus.Default, - ); - - if (doubleAgentId) - CrmService.crmgqlquery({ - query: gql` - query( - $statecode: Int - $currentDate: DateTime - $evo_agent_accountid: Uuid - ) { - evo_reward_condition: evo_reward_conditions( - evo_agent_accountid: $evo_agent_accountid - evo_datefrom_param: { lte: $currentDate } - evo_dateto_param: { gte: $currentDate } - statecode: $statecode - ) { - evo_reward_conditionid - evo_name - evo_reward_summ - evo_reduce_reward - } - } - `, - variables: { - evo_agent_accountid: doubleAgentId, - currentDate, - statecode: 0, - }, - toOptions: ['evo_reward_condition'], - }).then(({ entities: { evo_reward_condition } }) => { - if (evo_reward_condition && Array.isArray(evo_reward_condition)) - calculationStore.setOptions( - 'selectCalcDoubleAgentRewardCondition', - evo_reward_condition, - ); - }); - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { calcFinDepartment } = calculationStore.values; - return calcFinDepartment; - }, - effect: calcFinDepartmentId => { - if (!calcFinDepartmentId) { - calculationStore.setValue('finDepartmentRewardCondtion', null); - calculationStore.setStatus( - 'selectFinDepartmentRewardCondtion', - ElementStatus.Disabled, - ); - } else { - calculationStore.setValue('finDepartmentRewardCondtion', null); - calculationStore.setStatus( - 'selectFinDepartmentRewardCondtion', - ElementStatus.Default, - ); - if (calcFinDepartmentId) - CrmService.crmgqlquery({ - query: gql` - query( - $statecode: Int - $currentDate: DateTime - $evo_agent_accountid: Uuid - ) { - evo_reward_condition: evo_reward_conditions( - evo_agent_accountid: $evo_agent_accountid - evo_datefrom_param: { lte: $currentDate } - evo_dateto_param: { gte: $currentDate } - statecode: $statecode - ) { - evo_reward_conditionid - evo_name - evo_reward_summ - evo_reduce_reward - } - } - `, - variables: { - evo_agent_accountid: calcFinDepartmentId, - currentDate, - statecode: 0, - }, - toOptions: ['evo_reward_condition'], - }).then(({ entities: { evo_reward_condition } }) => { - if (evo_reward_condition && Array.isArray(evo_reward_condition)) - calculationStore.setOptions( - 'selectFinDepartmentRewardCondtion', - evo_reward_condition, - ); - }); - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { calcBroker } = calculationStore.values; - return calcBroker; - }, - effect: calcBrokerId => { - if (!calcBrokerId) { - calculationStore.setValue('calcBrokerRewardCondition', null); - calculationStore.setStatus( - 'selectCalcBrokerRewardCondition', - ElementStatus.Disabled, - ); - } else { - calculationStore.setValue('calcBrokerRewardCondition', null); - calculationStore.setStatus( - 'selectCalcBrokerRewardCondition', - ElementStatus.Default, - ); - - if (calcBrokerId) - CrmService.crmgqlquery({ - query: gql` - query( - $statecode: Int - $currentDate: DateTime - $evo_agent_accountid: Uuid - ) { - evo_reward_condition: evo_reward_conditions( - evo_agent_accountid: $evo_agent_accountid - evo_datefrom_param: { lte: $currentDate } - evo_dateto_param: { gte: $currentDate } - statecode: $statecode - ) { - evo_reward_conditionid - evo_name - evo_reward_summ - evo_reduce_reward - } - } - `, - variables: { - evo_agent_accountid: calcBrokerId, - currentDate, - statecode: 0, - }, - toOptions: ['evo_reward_condition'], - }).then(({ entities: { evo_reward_condition } }) => { - if (evo_reward_condition && Array.isArray(evo_reward_condition)) - calculationStore.setOptions( - 'selectCalcBrokerRewardCondition', - evo_reward_condition, - ); - }); - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { indAgentRewardCondition } = calculationStore.values; - return indAgentRewardCondition; - }, - effect: indAgentRewardConditionId => { - if (!indAgentRewardConditionId) { - calculationStore.setValue('indAgentRewardSumm', null); - calculationStore.setStatus('tbxIndAgentRewardSumm', ElementStatus.Disabled); - - const leadId = calculationStore.values.lead; - if (leadId) { - const lead = calculationStore.options.selectLead?.find( - x => x.leadid === leadId, - ); - if (lead && !lead.evo_double_agent_accountid) { - calculationStore.setValue('calcDoubleAgent', null); - calculationStore.setStatus( - 'selectCalcDoubleAgent', - ElementStatus.Disabled, - ); - } - } - } else { - const indAgentRewardCondition = calculationStore.options.selectIndAgentRewardCondition?.find( - x => x.evo_reward_conditionid === indAgentRewardConditionId, - ); - if (indAgentRewardCondition) { - calculationStore.setValue( - 'indAgentRewardSumm', - indAgentRewardCondition.evo_reward_summ || 0, - ); - calculationStore.setStatus('tbxIndAgentRewardSumm', ElementStatus.Default); - if (indAgentRewardCondition?.evo_double_agent_accountid) { - calculationStore.setStatus('selectCalcDoubleAgent', ElementStatus.Default); - const doubleAgent = calculationStore.options.selectCalcDoubleAgent?.find( - x => - x.accountid === - indAgentRewardCondition?.evo_double_agent_accountid, - ); - if (doubleAgent) { - calculationStore.setOptions('selectCalcDoubleAgent', [ - doubleAgent, - ]); - calculationStore.setValue( - 'calcDoubleAgent', - doubleAgent.accountid, - ); - } - } else { - calculationStore.setValue('calcDoubleAgent', null); - calculationStore.setStatus( - 'selectCalcDoubleAgent', - ElementStatus.Disabled, - ); - } - } - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { calcDoubleAgentRewardCondition } = calculationStore.values; - return calcDoubleAgentRewardCondition; - }, - effect: calcDoubleAgentRewardConditionId => { - if (!calcDoubleAgentRewardConditionId) { - calculationStore.setValue('calcDoubleAgentRewardSumm', 0); - calculationStore.setStatus( - 'tbxCalcDoubleAgentRewardSumm', - ElementStatus.Disabled, - ); - } else { - const calcDoubleAgentRewardCondition = calculationStore.options.selectCalcDoubleAgentRewardCondition?.find( - x => x.evo_reward_conditionid === calcDoubleAgentRewardConditionId, - ); - if (calcDoubleAgentRewardCondition) { - if (calcDoubleAgentRewardCondition.evo_reward_summ) { - calculationStore.setValue( - 'calcDoubleAgentRewardSumm', - calcDoubleAgentRewardCondition.evo_reward_summ, - ); - calculationStore.setStatus( - 'tbxCalcDoubleAgentRewardSumm', - ElementStatus.Default, - ); - } - } - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { calcBrokerRewardCondition } = calculationStore.values; - return calcBrokerRewardCondition; - }, - effect: calcBrokerRewardConditionId => { - if (!calcBrokerRewardConditionId) { - calculationStore.setValue('calcBrokerRewardSum', 0); - calculationStore.setStatus('tbxCalcBrokerRewardSum', ElementStatus.Disabled); - } else { - const calcBrokerRewardCondition = calculationStore.options.selectCalcBrokerRewardCondition?.find( - x => x.evo_reward_conditionid === calcBrokerRewardConditionId, - ); - if (calcBrokerRewardCondition) { - if (calcBrokerRewardCondition.evo_reward_summ) { - calculationStore.setValue( - 'calcBrokerRewardSum', - calcBrokerRewardCondition.evo_reward_summ, - ); - calculationStore.setStatus( - 'tbxCalcBrokerRewardSum', - ElementStatus.Default, - ); - } - } - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { finDepartmentRewardCondtion } = calculationStore.values; - return finDepartmentRewardCondtion; - }, - effect: finDepartmentRewardCondtionId => { - if (!finDepartmentRewardCondtionId) { - calculationStore.setValue('finDepartmentRewardSumm', 0); - calculationStore.setStatus( - 'tbxFinDepartmentRewardSumm', - ElementStatus.Disabled, - ); - } else { - const finDepartmentRewardCondtion = calculationStore.options.selectFinDepartmentRewardCondtion?.find( - x => x.evo_reward_conditionid === finDepartmentRewardCondtionId, - ); - if (finDepartmentRewardCondtion) { - if (finDepartmentRewardCondtion.evo_reward_summ) { - calculationStore.setValue( - 'finDepartmentRewardSumm', - finDepartmentRewardCondtion.evo_reward_summ, - ); - calculationStore.setStatus( - 'tbxFinDepartmentRewardSumm', - ElementStatus.Default, - ); - } - } - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { indAgentRewardSumm } = calculationStore.values; - return indAgentRewardSumm; - }, - effect: indAgentRewardSumm => { - const indAgentRewardConditionId = - calculationStore.values.indAgentRewardCondition; - if (indAgentRewardConditionId) { - const indAgentRewardCondition = calculationStore.options.selectIndAgentRewardCondition?.find( - x => x.evo_reward_conditionid === indAgentRewardConditionId, - ); - if (indAgentRewardCondition) { - if (indAgentRewardCondition.evo_reward_summ) { - if ( - parseFloat(indAgentRewardSumm) > - indAgentRewardCondition.evo_reward_summ - ) { - calculationStore.setValidation('tbxIndAgentRewardSumm', false); - openNotification({ - type: 'error', - title: 'Ошибка', - description: - 'Вознаграждение агента ФЛ указано больше условия по агентскому договору! \nЗначение установлено по условию договора.', - })(); - return; - } else if ( - !indAgentRewardCondition.evo_reduce_reward && - indAgentRewardCondition.evo_reward_summ - ) { - if ( - parseFloat(indAgentRewardSumm) < - indAgentRewardCondition.evo_reward_summ - ) { - calculationStore.setValidation('tbxIndAgentRewardSumm', false); - openNotification({ - type: 'error', - title: 'Ошибка', - description: - 'Вознаграждение агента ФЛ указано меньше условия по агентскому договору! \nЗначение установлено по условию договора.', - })(); - return; - } - } - calculationStore.setValidation('tbxIndAgentRewardSumm', true); - } - } - } - }, - }), - - calculationStore => ({ - expression: () => { - const { calcDoubleAgentRewardSumm } = calculationStore.values; - return calcDoubleAgentRewardSumm; - }, - effect: calcDoubleAgentRewardSumm => { - const calcDoubleAgentRewardConditionId = - calculationStore.values.calcDoubleAgentRewardCondition; - if (calcDoubleAgentRewardConditionId) { - const selectCalcDoubleAgentRewardCondition = calculationStore.options.selectCalcDoubleAgentRewardCondition?.find( - x => x.evo_reward_conditionid === calcDoubleAgentRewardConditionId, - ); - if (selectCalcDoubleAgentRewardCondition) { - if (selectCalcDoubleAgentRewardCondition.evo_reward_summ) { - if ( - parseFloat(calcDoubleAgentRewardSumm) > - selectCalcDoubleAgentRewardCondition.evo_reward_summ - ) { - calculationStore.setValidation( - 'tbxCalcDoubleAgentRewardSumm', - false, - ); - openNotification({ - type: 'error', - title: 'Ошибка', - description: - 'Вознаграждение двойного агента указано больше условия по агентскому договору! \nЗначение установлено по условию договора.', - })(); - return; - } else if ( - !selectCalcDoubleAgentRewardCondition.evo_reduce_reward && - selectCalcDoubleAgentRewardCondition.evo_reward_summ - ) { - if ( - parseFloat(calcDoubleAgentRewardSumm) < - selectCalcDoubleAgentRewardCondition.evo_reward_summ - ) { - calculationStore.setValidation( - 'tbxCalcDoubleAgentRewardSumm', - false, - ); - openNotification({ - type: 'error', - title: 'Ошибка', - description: - 'Вознаграждение двойного агента указано меньше условия по агентскому договору! \nЗначение установлено по условию договора.', - })(); - return; - } - } - calculationStore.setValidation( - 'tbxCalcDoubleAgentRewardSumm', - true, - ); - } - } - } - }, - }), - - calculationStore => ({ - expression: () => { - const { calcBrokerRewardSum } = calculationStore.values; - return calcBrokerRewardSum; - }, - effect: calcBrokerRewardSum => { - const calcBrokerRewardConditionId = - calculationStore.values.calcBrokerRewardCondition; - if (calcBrokerRewardConditionId) { - const selectCalcBrokerRewardCondition = calculationStore.options.selectCalcBrokerRewardCondition?.find( - x => x.evo_reward_conditionid === calcBrokerRewardConditionId, - ); - if (selectCalcBrokerRewardCondition) { - if (selectCalcBrokerRewardCondition.evo_reward_summ) { - if ( - parseFloat(calcBrokerRewardSum) > - selectCalcBrokerRewardCondition.evo_reward_summ - ) { - calculationStore.setValidation('tbxCalcBrokerRewardSum', false); - openNotification({ - type: 'error', - title: 'Ошибка', - description: - 'Вознаграждение брокера указано больше условия по агентскому договору! \nЗначение установлено по условию договора.', - })(); - return; - } else if ( - !selectCalcBrokerRewardCondition.evo_reduce_reward && - selectCalcBrokerRewardCondition.evo_reward_summ - ) { - if ( - parseFloat(calcBrokerRewardSum) < - selectCalcBrokerRewardCondition.evo_reward_summ - ) { - calculationStore.setValidation('tbxCalcBrokerRewardSum', false); - openNotification({ - type: 'error', - title: 'Ошибка', - description: - 'Вознаграждение брокера указано меньше условия по агентскому договору! \nЗначение установлено по условию договора.', - })(); - return; - } - } - calculationStore.setValidation('tbxCalcBrokerRewardSum', true); - } - } - } - }, - }), - - calculationStore => ({ - expression: () => { - const { finDepartmentRewardSumm } = calculationStore.values; - return finDepartmentRewardSumm; - }, - effect: finDepartmentRewardSumm => { - const finDepartmentRewardCondtionid = - calculationStore.values.finDepartmentRewardCondtion; - if (finDepartmentRewardCondtionid) { - const selectFinDepartmentRewardCondtion = calculationStore.options.selectFinDepartmentRewardCondtion?.find( - x => x.evo_reward_conditionid === finDepartmentRewardCondtionid, - ); - if (selectFinDepartmentRewardCondtion) { - if (selectFinDepartmentRewardCondtion.evo_reward_summ) { - if ( - parseFloat(finDepartmentRewardSumm) > - selectFinDepartmentRewardCondtion.evo_reward_summ - ) { - calculationStore.setValidation( - 'tbxFinDepartmentRewardSumm', - false, - ); - openNotification({ - type: 'error', - title: 'Ошибка', - description: - 'Вознаграждение финотдела указано больше условия по агентскому договору! \nЗначение установлено по условию договора.', - })(); - return; - } else if ( - !selectFinDepartmentRewardCondtion.evo_reduce_reward && - selectFinDepartmentRewardCondtion.evo_reward_summ - ) { - if ( - parseFloat(finDepartmentRewardSumm) < - selectFinDepartmentRewardCondtion.evo_reward_summ - ) { - calculationStore.setValidation( - 'tbxFinDepartmentRewardSumm', - false, - ); - openNotification({ - type: 'error', - title: 'Ошибка', - description: - 'Вознаграждение финотдела указано меньше условия по агентскому договору! \nЗначение установлено по условию договора.', - })(); - return; - } - } - calculationStore.setValidation('tbxFinDepartmentRewardSumm', true); - } - } - } - }, - }), - - calculationStore => ({ - expression: () => { - const { leaseObjectUsed } = calculationStore.values; - return leaseObjectUsed; - }, - effect: leaseObjectUsed => { - if (leaseObjectUsed) { - calculationStore.setValue('leaseObjectCount', 1); - calculationStore.setStatus('tbxLeaseObjectCount', ElementStatus.Disabled); - } else { - calculationStore.setStatus('tbxLeaseObjectCount', ElementStatus.Default); - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { insUnlimitDrivers } = calculationStore.values; - return insUnlimitDrivers; - }, - effect: insUnlimitDrivers => { - if (insUnlimitDrivers) { - calculationStore.setStatus('tbxInsAgeDrivers', ElementStatus.Disabled); - calculationStore.setStatus('tbxInsExpDrivers', ElementStatus.Disabled); - calculationStore.setStatus('btnDriversApplication', ElementStatus.Disabled); - } else { - calculationStore.setStatus('tbxInsAgeDrivers', ElementStatus.Default); - calculationStore.setStatus('tbxInsExpDrivers', ElementStatus.Default); - calculationStore.setStatus('btnDriversApplication', ElementStatus.Default); - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { insFranchise } = calculationStore.values; - return insFranchise; - }, - effect: insFranchise => { - if (!insFranchise || parseInt(insFranchise) === 0) { - calculationStore.setStatus('btnFranschise', ElementStatus.Disabled); - } else { - calculationStore.setStatus('btnFranschise', ElementStatus.Default); - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { lastPaymentRule } = calculationStore.values; - return lastPaymentRule; - }, - effect: lastPaymentRule => { - if (lastPaymentRule) { - if (lastPaymentRule === 100000000) { - calculationStore.setStatus('tbxLastPaymentPerc', ElementStatus.Disabled); - calculationStore.setStatus('tbxLastPaymentRub', ElementStatus.Default); - } else { - calculationStore.setStatus('tbxLastPaymentPerc', ElementStatus.Default); - calculationStore.setStatus('tbxLastPaymentRub', ElementStatus.Disabled); - } - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { lastPaymentPerc, balanceHolder } = calculationStore.values; - return { - lastPaymentPerc, - balanceHolder, - }; - }, - effect: ({ lastPaymentPerc, balanceHolder }) => { - if (balanceHolder && balanceHolder === 100000001) { - if (!lastPaymentPerc || parseFloat(lastPaymentPerc) < 1) { - calculationStore.setValidation('tbxLastPaymentPerc', false); - openNotification({ - type: 'error', - title: 'Ошибка', - description: - 'При балансе лизингодатель последний платеж не может быть меньше 1%! Увеличьте значение.', - })(); - return; - } - } else { - if (parseFloat(lastPaymentPerc) === 0) { - calculationStore.setValidation('tbxLastPaymentPerc', false); - openNotification({ - type: 'error', - title: 'Ошибка', - description: - 'Последний платеж не может быть равен 0. Увеличьте значение', - })(); - return; - } - } - calculationStore.setValidation('tbxLastPaymentPerc', true); - }, - }), - - calculationStore => ({ - expression: () => { - const { - lastPaymentRub, - lastPaymentPerc, - insNSIB, - } = calculationStore.values; - return { lastPaymentRub, lastPaymentPerc, insNSIB }; - }, - effect: ({ lastPaymentRub, insNSIB }) => { - if (insNSIB && lastPaymentRub && lastPaymentRub < 3500) { - calculationStore.setValidation('tbxLastPaymentRub', false); - openNotification({ - type: 'error', - title: 'Ошибка', - description: - 'Последний платеж меньше 3500 руб. не может быть при наличии НСИБ, укажите большее значение', - })(); - return; - } else if (lastPaymentRub && lastPaymentRub < 1000) { - calculationStore.setValidation('tbxLastPaymentRub', false); - openNotification({ - type: 'error', - title: 'Ошибка', - description: - 'Последний платеж меньше 1000 руб. не может быть, укажите большее значение', - })(); - return; - } - calculationStore.setValidation('tbxLastPaymentRub', true); - }, - }), - - calculationStore => ({ - expression: () => { - const { graphType } = calculationStore.values; - return graphType; - }, - effect: graphType => { - if (graphType) { - switch (graphType) { - case 100000002: { - calculationStore.setStatus('radioSeasonType', ElementStatus.Disabled); - calculationStore.setStatus( - 'tbxParmentsDecreasePercent', - ElementStatus.Default, - ); - calculationStore.setStatus( - 'selectHighSeasonStart', - ElementStatus.Disabled, - ); - break; - } - - case 100000003: { - calculationStore.setStatus('radioSeasonType', ElementStatus.Default); - calculationStore.setStatus( - 'tbxParmentsDecreasePercent', - ElementStatus.Disabled, - ); - calculationStore.setStatus('selectHighSeasonStart', ElementStatus.Default); - break; - } - - default: { - calculationStore.setStatus('radioSeasonType', ElementStatus.Disabled); - calculationStore.setStatus( - 'tbxParmentsDecreasePercent', - ElementStatus.Disabled, - ); - calculationStore.setStatus( - 'selectHighSeasonStart', - ElementStatus.Disabled, - ); - break; - } - } - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { seasonType } = calculationStore.values; - return seasonType; - }, - effect: seasonType => { - if (seasonType) { - switch (seasonType) { - case 100000001: - case 100000002: { - calculationStore.setFilter('selectHighSeasonStart', seasons => { - return seasons.filter( - season => season.value && season.value <= 100000004, - ); - }); - break; - } - default: - calculationStore.setFilter('selectHighSeasonStart', undefined); - break; - } - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { leasingPeriod } = calculationStore.values; - return leasingPeriod; - }, - effect: leasingPeriod => { - if (leasingPeriod) { - if (parseInt(leasingPeriod) < 12) { - calculationStore.setStatus('radioBalanceHolder', ElementStatus.Disabled); - calculationStore.setValue('balanceHolder', 100000000); - calculationStore.setValue('insNSIB', null); - calculationStore.setStatus('selectInsNSIB', ElementStatus.Disabled); - } else { - calculationStore.setStatus('radioBalanceHolder', ElementStatus.Default); - calculationStore.setStatus('selectInsNSIB', ElementStatus.Default); - } - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { balanceHolder } = calculationStore.values; - return balanceHolder; - }, - effect: balanceHolder => { - if (balanceHolder) { - if (balanceHolder === 100000001) { - calculationStore.setStatus( - 'cbxLastPaymentRedemption', - ElementStatus.Disabled, - ); - calculationStore.setValue('lastPaymentRedemption', true); - } else { - calculationStore.setStatus( - 'cbxLastPaymentRedemption', - ElementStatus.Default, - ); - } - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { dealer } = calculationStore.values; - return dealer; - }, - effect: dealerId => { - if (dealerId) { - const dealer = calculationStore.options.selectDealer?.find( - x => x.accountid === dealerId, - ); - if (dealer) { - CrmService.crmgqlquery({ - query: gql` - query($statecode: Int, $salonaccountid: Uuid!) { - account: salon_providers( - statecode: $statecode - salonaccountid: $salonaccountid - ) { - accountid - name - evo_broker_accountid - } - } - `, - toOptions: ['account'], - variables: { - salonaccountid: dealerId, - statecode: 0, - }, - }).then(({ entities }) => { - if ( - entities.account && - Array.isArray(entities.account) && - entities.account.length > 0 - ) { - calculationStore.setOptions( - 'selectDealerPerson', - entities.account, - ); - calculationStore.setValue( - 'dealerPerson', - entities.account[0].accountid, - ); - calculationStore.setStatus('selectDealerPerson', ElementStatus.Default); - } - }); - } - } - - calculationStore.setOptions('selectDealerPerson', []); - calculationStore.setValue('dealerPerson', null); - calculationStore.setStatus('selectDealerPerson', ElementStatus.Disabled); - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { dealerPerson } = calculationStore.values; - return dealerPerson; - }, - effect: dealerPersonId => { - calculationStore.setOptions('selectDealerBroker', []); - calculationStore.setValue('dealerBroker', null); - calculationStore.setStatus('selectDealerBroker', ElementStatus.Disabled); - if (dealerPersonId) { - const dealerPerson = calculationStore.options.selectDealerPerson?.find( - x => x.accountid === dealerPersonId, - ); - if (dealerPerson && dealerPerson.evo_broker_accountid) { - CrmService.crmgqlquery({ - query: gql` - query($accountid: Uuid!) { - account(accountid: $accountid) { - accountid - name - } - } - `, - toOptions: ['account'], - variables: { - accountid: dealerPerson.evo_broker_accountid, - }, - }).then(({ entities }) => { - if (entities.account && !Array.isArray(entities.account)) { - calculationStore.setOptions('selectDealerBroker', [ - entities.account, - ]); - calculationStore.setValue( - 'dealerBroker', - entities.account.accountid, - ); - calculationStore.setStatus('selectDealerBroker', ElementStatus.Default); - } - }); - } - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { dealerBroker } = calculationStore.values; - return dealerBroker; - }, - effect: dealerBrokerId => { - if (dealerBrokerId) { - const dealerBroker = calculationStore.options.selectDealerBroker?.find( - x => x.accountid === dealerBrokerId, - ); - if (dealerBroker) { - calculationStore.setStatus( - 'selectDealerBrokerRewardCondition', - ElementStatus.Default, - ); - - CrmService.crmgqlquery({ - query: gql` - query( - $statecode: Int - $currentDate: DateTime - $evo_agent_accountid: Uuid - ) { - evo_reward_condition: evo_reward_conditions( - evo_agent_accountid: $evo_agent_accountid - evo_datefrom_param: { lte: $currentDate } - evo_dateto_param: { gte: $currentDate } - statecode: $statecode - ) { - evo_reward_conditionid - evo_name - evo_reward_summ - evo_reduce_reward - } - } - `, - variables: { - evo_agent_accountid: dealerBrokerId, - currentDate, - statecode: 0, - }, - toOptions: ['evo_reward_condition'], - }).then(({ entities: { evo_reward_condition } }) => { - if (evo_reward_condition && Array.isArray(evo_reward_condition)) - calculationStore.setOptions( - 'selectDealerBrokerRewardCondition', - evo_reward_condition, - ); - }); - } - } else { - calculationStore.setValue('dealerBrokerRewardCondition', null); - calculationStore.setStatus( - 'selectDealerBrokerRewardCondition', - ElementStatus.Disabled, - ); - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { dealerBroker, dealerPerson } = calculationStore.values; - return [dealerBroker, dealerPerson]; - }, - effect: ([dealerBrokerId, dealerPersonId]) => { - if (dealerPersonId && !dealerBrokerId) { - calculationStore.setStatus( - 'selectDealerRewardCondition', - ElementStatus.Default, - ); - - CrmService.crmgqlquery({ - query: gql` - query( - $statecode: Int - $currentDate: DateTime - $evo_agent_accountid: Uuid - ) { - evo_reward_condition: evo_reward_conditions( - evo_agent_accountid: $evo_agent_accountid - evo_datefrom_param: { lte: $currentDate } - evo_dateto_param: { gte: $currentDate } - statecode: $statecode - ) { - evo_reward_conditionid - evo_name - evo_reward_summ - evo_reduce_reward - } - } - `, - variables: { - evo_agent_accountid: dealerPersonId, - currentDate, - statecode: 0, - }, - toOptions: ['evo_reward_condition'], - }).then(({ entities: { evo_reward_condition } }) => { - if (evo_reward_condition && Array.isArray(evo_reward_condition)) - calculationStore.setOptions( - 'selectDealerRewardCondition', - evo_reward_condition, - ); - }); - } else { - calculationStore.setValue('dealerRewardCondition', null); - calculationStore.setStatus( - 'selectDealerRewardCondition', - ElementStatus.Disabled, - ); - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { dealerRewardCondition } = calculationStore.values; - return dealerRewardCondition; - }, - effect: dealerRewardConditionId => { - if (!dealerRewardConditionId) { - calculationStore.setValue('dealerRewardSumm', 0); - calculationStore.setStatus('tbxDealerRewardSumm', ElementStatus.Disabled); - } else { - const dealerRewardContition = calculationStore.options.selectDealerRewardCondition?.find( - x => x.evo_reward_conditionid === dealerRewardConditionId, - ); - if (dealerRewardContition) { - if (dealerRewardContition.evo_reward_summ) { - calculationStore.setValue( - 'dealerRewardSumm', - dealerRewardContition.evo_reward_summ, - ); - calculationStore.setStatus('tbxDealerRewardSumm', ElementStatus.Default); - } - } - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { dealerBrokerRewardCondition } = calculationStore.values; - return dealerBrokerRewardCondition; - }, - effect: dealerBrokerRewardConditionId => { - if (!dealerBrokerRewardConditionId) { - calculationStore.setValue('dealerBrokerRewardSumm', 0); - calculationStore.setStatus( - 'tbxDealerBrokerRewardSumm', - ElementStatus.Disabled, - ); - } else { - const dealerBrokerRewardContition = calculationStore.options.selectDealerBrokerRewardCondition?.find( - x => x.evo_reward_conditionid === dealerBrokerRewardConditionId, - ); - if (dealerBrokerRewardContition) { - if (dealerBrokerRewardContition.evo_reward_summ) { - calculationStore.setValue( - 'dealerBrokerRewardSumm', - dealerBrokerRewardContition.evo_reward_summ, - ); - calculationStore.setStatus( - 'tbxDealerBrokerRewardSumm', - ElementStatus.Default, - ); - } - } - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { dealerRewardSumm } = calculationStore.values; - return dealerRewardSumm; - }, - effect: dealerRewardSumm => { - const dealerRewardConditionId = - calculationStore.values.dealerRewardCondition; - if (dealerRewardConditionId) { - const dealerRewardCondition = calculationStore.options.selectDealerRewardCondition?.find( - x => x.evo_reward_conditionid === dealerRewardConditionId, - ); - if (dealerRewardCondition) { - if (dealerRewardCondition.evo_reward_summ) { - if ( - parseFloat(dealerRewardSumm) > - dealerRewardCondition.evo_reward_summ - ) { - calculationStore.setValidation('tbxDealerRewardSumm', false); - openNotification({ - type: 'error', - title: 'Ошибка', - description: - 'Вознаграждение ЮЛ поставщика указано больше условия по агентскому договору! \nЗначение установлено по условию договора.', - })(); - return; - } else if ( - !dealerRewardCondition.evo_reduce_reward && - dealerRewardCondition.evo_reward_summ - ) { - if ( - parseFloat(dealerRewardSumm) < - dealerRewardCondition.evo_reward_summ - ) { - calculationStore.setValidation('tbxDealerRewardSumm', false); - openNotification({ - type: 'error', - title: 'Ошибка', - description: - 'Вознаграждение ЮЛ поставщика указано меньше условия по агентскому договору! \nЗначение установлено по условию договора.', - })(); - return; - } - } - calculationStore.setValidation('tbxDealerRewardSumm', true); - } - } - } - }, - }), - - calculationStore => ({ - expression: () => { - const { dealerBrokerRewardSumm } = calculationStore.values; - return dealerBrokerRewardSumm; - }, - effect: dealerBrokerRewardSumm => { - const dealerBrokerRewardConditionId = - calculationStore.values.dealerBrokerRewardCondition; - if (dealerBrokerRewardConditionId) { - const dealerBrokerRewardCondition = calculationStore.options.selectDealerBrokerRewardCondition?.find( - x => x.evo_reward_conditionid === dealerBrokerRewardConditionId, - ); - if (dealerBrokerRewardCondition) { - if (dealerBrokerRewardCondition.evo_reward_summ) { - if ( - parseFloat(dealerBrokerRewardSumm) > - dealerBrokerRewardCondition.evo_reward_summ - ) { - calculationStore.setValidation( - 'tbxDealerBrokerRewardSumm', - false, - ); - openNotification({ - type: 'error', - title: 'Ошибка', - description: - 'Вознаграждение брокера поставщика указано больше условия по агентскому договору! \nЗначение установлено по условию договора.', - })(); - return; - } - } - if ( - !dealerBrokerRewardCondition.evo_reduce_reward && - dealerBrokerRewardCondition.evo_reward_summ - ) { - if ( - parseFloat(dealerBrokerRewardSumm) < - dealerBrokerRewardCondition.evo_reward_summ - ) { - calculationStore.setValidation( - 'tbxDealerBrokerRewardSumm', - false, - ); - openNotification({ - type: 'error', - title: 'Ошибка', - description: - 'Вознаграждение брокера поставщика указано меньше условия по агентскому договору! \nЗначение установлено по условию договора.', - })(); - return; - } - } - calculationStore.setValidation('tbxDealerBrokerRewardSumm', true); - } - } - }, - }), - - calculationStore => ({ - expression: () => { - const { brand } = calculationStore.values; - return brand; - }, - effect: brandId => { - calculationStore.setStatus('selectModel', ElementStatus.Disabled); - calculationStore.setOptions('selectModel', []); - calculationStore.setValue('model', null); - - if (brandId) { - const brand = calculationStore.options.selectBrand?.find( - x => x.evo_brandid === brandId, - ); - if (brand) { - CrmService.crmgqlquery({ - query: gql` - query($statecode: Int, $evo_brandid: Uuid) { - evo_model: evo_models( - statecode: $statecode - evo_brandid: $evo_brandid - ) { - evo_name - evo_modelid - evo_leasingobject_risk - evo_importer_reward_perc - evo_importer_reward_rub - evo_impairment_groupid - } - } - `, - variables: { - statecode: 0, - evo_brandid: brandId, - }, - toOptions: ['evo_model'], - }).then(({ entities }) => { - if ( - entities.evo_model && - Array.isArray(entities.evo_model) && - entities.evo_model.length > 0 - ) { - calculationStore.setStatus('selectModel', ElementStatus.Default); - calculationStore.setOptions('selectModel', entities.evo_model); - calculationStore.setValue('model', null); - } - }); - } - } - }, - options: { fireImmediately: true }, - }), - - calculationStore => ({ - expression: () => { - const { model } = calculationStore.values; - return model; - }, - effect: modelId => { - calculationStore.setStatus('selectConfiguration', ElementStatus.Disabled); - calculationStore.setOptions('selectConfiguration', []); - calculationStore.setValue('configuration', null); - - if (modelId) { - const model = calculationStore.options.selectModel?.find( - x => x.evo_modelid === modelId, - ); - if (model) { - CrmService.crmgqlquery({ - query: gql` - query($statecode: Int, $evo_modelid: Uuid) { - evo_equipment: evo_equipments( - statecode: $statecode - evo_modelid: $evo_modelid - ) { - evo_equipmentid - evo_name - evo_impairment_groupid - evo_leasingobject_risk - evo_start_production_year - } - } - `, - toOptions: ['evo_equipment'], - variables: { - statecode: 0, - evo_modelid: modelId, - }, - }).then(({ entities }) => { - if ( - entities.evo_equipment && - Array.isArray(entities.evo_equipment) && - entities.evo_equipment.length > 0 - ) { - calculationStore.setStatus('selectConfiguration', ElementStatus.Default); - calculationStore.setOptions( - 'selectConfiguration', - entities.evo_equipment, - ); - } - }); - } - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { configuration } = calculationStore.values; - const { selectConfiguration } = calculationStore.options; - return [configuration, selectConfiguration]; - }, - effect: ([configurationId, selectConfigurationOptions]) => { - if (selectConfigurationOptions && selectConfigurationOptions.length) { - if (!configurationId) { - calculationStore.setValidation('selectConfiguration', false); - return; - } - } - calculationStore.setValidation('selectConfiguration', true); - }, - }), - - calculationStore => ({ - expression: () => { - const { model, configuration } = calculationStore.values; - return [model, configuration]; - }, - effect: ([modelId, configurationId]) => { - if (configurationId) { - const configuration = calculationStore.options.selectConfiguration?.find( - x => x.evo_equipmentid === configurationId, - ); - if (configuration) { - if (configuration.evo_impairment_groupid) { - const evo_impairment_groups = calculationStore.getStaticData( - 'evo_impairment_group', - ); - const evo_impairment_group = evo_impairment_groups.find( - x => - x.evo_impairment_groupid === - configuration.evo_impairment_groupid, - ); - calculationStore.setValue( - 'depreciationGroup', - evo_impairment_group ? evo_impairment_group.evo_name : '', - ); - return; - } - } - } - const model = calculationStore.options.selectModel?.find( - x => x.evo_modelid === modelId, - ); - if (model) - if (model.evo_impairment_groupid) { - const evo_impairment_groups = calculationStore.getStaticData( - 'evo_impairment_group', - ); - const evo_impairment_group = evo_impairment_groups.find( - x => x.evo_impairment_groupid === model.evo_impairment_groupid, - ); - calculationStore.setValue( - 'depreciationGroup', - evo_impairment_group ? evo_impairment_group.evo_name : '', - ); - return; - } - calculationStore.setValue('depreciationGroup', null); - }, - }), - - calculationStore => ({ - expression: () => { - const { leaseObjectPrice, supplierDiscountRub } = calculationStore.values; - return [leaseObjectPrice, supplierDiscountRub]; - }, - effect: ([leaseObjectPrice, supplierDiscountRub]) => { - if (supplierDiscountRub === undefined) { - return; - } - - calculationStore.setValue( - 'supplierDiscountPerc', - (supplierDiscountRub / leaseObjectPrice) * 100, - ); - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { - leaseObjectPrice, - supplierDiscountPerc, - } = calculationStore.values; - return [leaseObjectPrice, supplierDiscountPerc]; - }, - effect: ([leaseObjectPrice, supplierDiscountPerc]) => { - if (supplierDiscountPerc === undefined) { - return; - } - - calculationStore.setValue( - 'supplierDiscountRub', - (supplierDiscountPerc * leaseObjectPrice) / 100, - ); - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { firstPaymentRub } = calculationStore.values; - return firstPaymentRub; - }, - effect: firstPaymentRub => { - if (firstPaymentRub === undefined) { - return; - } - const { supplierCurrency, leaseObjectPrice } = calculationStore.values; - calculatePerc(calculationStore)( - supplierCurrency, - leaseObjectPrice, - firstPaymentRub, - 'firstPaymentPerc', - ); - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { - supplierCurrency, - leaseObjectPrice, - firstPaymentPerc, - } = calculationStore.values; - return [supplierCurrency, leaseObjectPrice, firstPaymentPerc]; - }, - effect: ([supplierCurrencyId, leaseObjectPrice, firstPaymentPerc]) => { - if (firstPaymentPerc === undefined) { - return; - } - calculateRub(calculationStore)( - supplierCurrencyId, - leaseObjectPrice, - firstPaymentPerc, - 'firstPaymentRub', - ); - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { - supplierCurrency, - leaseObjectPrice, - comissionPerc, - } = calculationStore.values; - return [supplierCurrency, leaseObjectPrice, comissionPerc]; - }, - effect: ([supplierCurrencyId, leaseObjectPrice, comissionPerc]) => { - if (comissionPerc === undefined) { - return; - } - calculateRub(calculationStore)( - supplierCurrencyId, - leaseObjectPrice, - comissionPerc, - 'comissionRub', - ); - }, - options: { - fireImmediately: true, - }, - }), - calculationStore => ({ - expression: () => { - const { comissionRub } = calculationStore.values; - return comissionRub; - }, - effect: comissionRub => { - if (comissionRub === undefined) { - return; - } - const { supplierCurrency, leaseObjectPrice } = calculationStore.values; - calculatePerc(calculationStore)( - supplierCurrency, - leaseObjectPrice, - comissionRub, - 'comissionPerc', - ); - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { - supplierCurrency, - leaseObjectPrice, - lastPaymentPerc, - lastPaymentRule, - } = calculationStore.values; - return [ - supplierCurrency, - leaseObjectPrice, - lastPaymentPerc, - lastPaymentRule, - ]; - }, - effect: ([ - supplierCurrencyId, - leaseObjectPrice, - lastPaymentPerc, - lastPaymentRule, - ]) => { - if (lastPaymentPerc === undefined || lastPaymentRule !== 100000001) { - return; - } - calculateRub(calculationStore)( - supplierCurrencyId, - leaseObjectPrice, - lastPaymentPerc, - 'lastPaymentRub', - ); - }, - options: { - fireImmediately: true, - }, - }), - calculationStore => ({ - expression: () => { - const { - supplierCurrency, - leaseObjectPrice, - lastPaymentRub, - lastPaymentRule, - } = calculationStore.values; - return [ - supplierCurrency, - leaseObjectPrice, - lastPaymentRub, - lastPaymentRule, - ]; - }, - effect: ([ - supplierCurrencyId, - leaseObjectPrice, - lastPaymentRub, - lastPaymentRule, - ]) => { - if (lastPaymentRub === undefined || lastPaymentRule !== 100000000) { - return; - } - calculatePerc(calculationStore)( - supplierCurrencyId, - leaseObjectPrice, - lastPaymentRub, - 'lastPaymentPerc', - ); - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { - recalcWithRevision, - supplierCurrency, - supplierDiscountRub, - supplierDiscountPerc, - leaseObjectPrice, - } = calculationStore.values; - return [ - recalcWithRevision, - supplierCurrency, - supplierDiscountRub, - supplierDiscountPerc, - leaseObjectPrice, - ]; - }, - effect: ([ - recalcWithRevision, - supplierCurrencyId, - supplierDiscountRub, - supplierDiscountPerc, - leaseObjectPrice, - ]) => { - if (recalcWithRevision === true) { - return; - } - const supplierCurrency = calculationStore - .getOptions('selectSupplierCurrency') - .find(x => x.transactioncurrencyid === supplierCurrencyId); - const evo_currencychange = calculationStore - .getStaticData('evo_currencychange') - ?.find(x => x.evo_ref_transactioncurrency === supplierCurrencyId); - const evo_currencychangeValue = - (evo_currencychange && evo_currencychange.evo_currencychange) || 0; - let price = calcPrice( - supplierCurrency?.isocurrencycode, - leaseObjectPrice, - evo_currencychangeValue, - ); - - let maxPriceChange = - price - supplierDiscountRub < 800000 - ? price - supplierDiscountRub + 50000 - : (price - supplierDiscountRub) * 1.05; - - calculationStore.setValue('maxPriceChange', maxPriceChange); - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { recalcWithRevision } = calculationStore.values; - return recalcWithRevision; - }, - effect: recalcWithRevision => { - if (recalcWithRevision === true) { - calculationStore.setStatus('selectLead', ElementStatus.Disabled); - calculationStore.setStatus('selectOpportunity', ElementStatus.Disabled); - calculationStore.setStatus('selectQuote', ElementStatus.Disabled); - } else { - calculationStore.setStatus('selectLead', ElementStatus.Default); - calculationStore.setStatus('selectOpportunity', ElementStatus.Default); - calculationStore.setStatus('selectQuote', ElementStatus.Default); - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { - supplierDiscountRub, - leaseObjectPrice, - supplierCurrency, - } = calculationStore.values; - return [supplierDiscountRub, leaseObjectPrice, supplierCurrency]; - }, - effect: ([supplierDiscountRub, leaseObjectPrice, supplierCurrencyId]) => { - const { recalcWithRevision, maxPriceChange } = calculationStore.values; - if (recalcWithRevision === false) { - return; - } - - const supplierCurrency = calculationStore - .getOptions('selectSupplierCurrency') - .find(x => x.transactioncurrencyid === supplierCurrencyId); - const evo_currencychange = calculationStore - .getStaticData('evo_currencychange') - ?.find(x => x.evo_ref_transactioncurrency === supplierCurrencyId); - const evo_currencychangeValue = - (evo_currencychange && evo_currencychange.evo_currencychange) || 0; - let price = calcPrice( - supplierCurrency?.isocurrencycode, - leaseObjectPrice, - evo_currencychangeValue, - ); - - if (price - supplierDiscountRub > maxPriceChange) { - calculationStore.setValidation('tbxLeaseObjectPrice', false); - openNotification({ - type: 'error', - title: 'Ошибка', - description: - 'Указанная стоимость предмета лизинга больше возмножного изменения стоимости предмета лизинга при пересчете без пересмотра.' + - 'Уменьшите стоимость предмета лизинга', - })(); - } else { - calculationStore.setValidation('tbxLeaseObjectPrice', true); - } - }, - }), - - calculationStore => ({ - expression: () => { - const { leaseObjectUsed } = calculationStore.values; - return leaseObjectUsed; - }, - effect: leaseObjectUsed => { - if (leaseObjectUsed === true) { - calculationStore.setValue('deliveryTime', 100000000); - calculationStore.setStatus('radioDeliveryTime', ElementStatus.Disabled); - } else { - calculationStore.setStatus('radioDeliveryTime', ElementStatus.Default); - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { leaseObjectCategory } = calculationStore.values; - return leaseObjectCategory; - }, - effect: leaseObjectCategory => { - if ( - leaseObjectCategory === 100000001 || - leaseObjectCategory === 100000001 - ) { - calculationStore.setValue('withTrailer', null); - calculationStore.setStatus('selectWithTrailer', ElementStatus.Disabled); - } else { - calculationStore.setStatus('selectWithTrailer', ElementStatus.Default); - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { quote } = calculationStore.values; - return quote; - }, - effect: quoteId => { - calculationStore.setStatus('cbxRecalcWithRevision', ElementStatus.Disabled); - if (quoteId) { - const quote = calculationStore.options.selectQuote?.find( - x => x.quoteid === quoteId, - ); - if (quote) { - if (quote.evo_recalc_limit && quote.evo_recalc_limit > 0) { - if (quote.evo_statuscode?.evo_id === '2.3') { - calculationStore.setStatus( - 'cbxRecalcWithRevision', - ElementStatus.Default, - ); - } - } - } - } - }, - }), - - calculationStore => ({ - expression: () => { - const { firstPaymentPerc } = calculationStore.values; - return firstPaymentPerc; - }, - effect: firstPaymentPerc => { - const { recalcWithRevision, quote: quoteId } = calculationStore.values; - if (recalcWithRevision) { - const quote = calculationStore.options.selectQuote?.find( - x => x.quoteid === quoteId, - ); - if (quote && quote.evo_approved_first_payment) { - if (firstPaymentPerc < quote.evo_approved_first_payment) { - calculationStore.setValidation('tbxFirstPaymentPerc', false); - openNotification({ - type: 'error', - title: 'Ошибка', - description: - 'Указанный первый платеж меньше одобренного. При пересчете без пересмотра изменение первого платежа возможно только в большую сторону от одобренного значения', - })(); - return; - } - } - } - calculationStore.setValidation('tbxFirstPaymentPerc', undefined); - }, - }), - - calculationStore => ({ - expression: () => { - const { brand, model, leaseObjectUsed } = calculationStore.values; - return [brand, model, leaseObjectUsed]; - }, - effect: ([brandId, modelId, leaseObjectUsed]) => { - if (leaseObjectUsed === false) { - const model = calculationStore.options.selectModel?.find( - x => x.evo_modelid === modelId, - ); - const brand = calculationStore.options.selectBrand?.find( - x => x.evo_brandid === brandId, - ); - - if ( - model && - ((model.evo_importer_reward_perc && - model.evo_importer_reward_perc > 0) || - (model.evo_importer_reward_rub && - model.evo_importer_reward_rub > 0)) - ) { - calculationStore.setValue( - 'importerRewardPerc', - model.evo_importer_reward_perc || 0, - ); - calculationStore.setValue( - 'importerRewardRub', - model.evo_importer_reward_rub || 0, - ); - return; - } - if ( - brand && - ((brand.evo_importer_reward_perc && - brand.evo_importer_reward_perc > 0) || - (brand.evo_importer_reward_rub && - brand.evo_importer_reward_rub > 0)) - ) { - calculationStore.setValue( - 'importerRewardPerc', - brand.evo_importer_reward_perc || 0, - ); - calculationStore.setValue( - 'importerRewardRub', - brand.evo_importer_reward_rub || 0, - ); - return; - } - } - calculationStore.setValue('importerRewardPerc', 0); - calculationStore.setValue('importerRewardRub', 0); - }, - options: { fireImmediately: true }, - }), - - calculationStore => ({ - expression: () => { - const { rows: tableRows } = calculationStore.tables.tableInsurance; - const kaskoRowIndex = tableRows.findIndex( - x => x.policyType?.value === 'КАСКО', - ); - - const kaskoValues = calculationStore.getTableRowValues( - 'tableInsurance', - kaskoRowIndex, - 'value', - ); - - return { - ...kaskoValues, - }; - }, - effect: ({ insuranceCompany, insTerm, insured }) => { - if (insTerm) { - const { rows: tableRows } = calculationStore.tables.tableInsurance; - const dgoRowIndex = tableRows.findIndex( - x => x.policyType?.value === 'ДГО', - ); - if (dgoRowIndex && dgoRowIndex >= 0) - calculationStore.setTableRows( - 'tableInsurance', - dgoRowIndex, - )([ - { - insuranceCompany: { - value: insuranceCompany, - }, - insTerm: { - value: insTerm, - }, - insured: { - value: insured, - }, - }, - { - insuranceCompany: { - value: insuranceCompany, - }, - insTerm: { - value: insTerm, - }, - insured: { - value: insured, - }, - }, - ]); - } - }, - }), - - calculationStore => ({ - expression: () => { - const { rows: tableRows } = calculationStore.tables.tableInsurance; - const kaskoRowIndex = tableRows.findIndex( - x => x.policyType?.value === 'КАСКО', - ); - const kaskoRow = tableRows[kaskoRowIndex]; - const { leasingPeriod } = calculationStore.values; - - return { - insTerm: kaskoRow.insTerm?.value, - leasingPeriod, - }; - }, - effect: ({ insTerm, leasingPeriod }) => { - const { rows: tableRows } = calculationStore.tables.tableInsurance; - const kaskoRowIndex = tableRows.findIndex( - x => x.policyType?.value === 'КАСКО', - ); - if (insTerm === 100000001 && leasingPeriod > 15 && kaskoRowIndex) { - if (kaskoRowIndex >= 0) - calculationStore.setTableRow( - 'tableInsurance', - kaskoRowIndex, - )({ - insured: { value: 100000001, status: ElementStatus.Disabled }, - }); - } else { - if (kaskoRowIndex >= 0) - calculationStore.setTableRow( - 'tableInsurance', - kaskoRowIndex, - )({ - insured: { status: ElementStatus.Default }, - }); - } - }, - }), - - calculationStore => ({ - expression: () => { - const { leasingPeriod } = calculationStore.values; - return leasingPeriod; - }, - effect: leasingPeriod => { - const { rows: tableRows } = calculationStore.tables.tableInsurance; - const kaskoRowIndex = tableRows.findIndex( - x => x.policyType?.value === 'КАСКО', - ); - const osagoRowIndex = tableRows.findIndex( - x => x.policyType?.value === 'ОСАГО', - ); - - if (leasingPeriod) { - if (leasingPeriod < 12) { - calculationStore.setTableRow( - 'tableInsurance', - kaskoRowIndex, - )({ - insured: { value: 100000000, status: ElementStatus.Disabled }, - }); - calculationStore.setTableRow( - 'tableInsurance', - osagoRowIndex, - )({ - insured: { value: 100000000, status: ElementStatus.Disabled }, - }); - } else { - calculationStore.setTableRow( - 'tableInsurance', - kaskoRowIndex, - )({ - insured: { value: 100000000, status: ElementStatus.Default }, - }); - calculationStore.setTableRow( - 'tableInsurance', - osagoRowIndex, - )({ - insured: { status: ElementStatus.Default }, - }); - } - - if (leasingPeriod === 12) { - calculationStore.setTableRow( - 'tableInsurance', - kaskoRowIndex, - )({ - insTerm: { value: 100000000, status: ElementStatus.Disabled }, - }); - return; - } else if (leasingPeriod > 12 && leasingPeriod < 16) { - calculationStore.setTableRow( - 'tableInsurance', - kaskoRowIndex, - )({ - insTerm: { value: 100000001, status: ElementStatus.Disabled }, - }); - return; - } else { - calculationStore.setTableRow( - 'tableInsurance', - kaskoRowIndex, - )({ - insured: { status: ElementStatus.Default }, - insTerm: { status: ElementStatus.Default }, - }); - } - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { rows: tableRows } = calculationStore.tables.tableInsurance; - const kaskoRowIndex = tableRows.findIndex( - x => x.policyType?.value === 'КАСКО', - ); - const dgoRowIndex = tableRows.findIndex( - x => x.policyType?.value === 'ДГО', - ); - const nsRowIndex = tableRows.findIndex(x => x.policyType?.value === 'НС'); - - const kaskoValues = calculationStore.getTableRowValues( - 'tableInsurance', - kaskoRowIndex, - 'value', - ); - const dgoValues = calculationStore.getTableRowValues( - 'tableInsurance', - dgoRowIndex, - 'value', - ); - const nsValues = calculationStore.getTableRowValues( - 'tableInsurance', - nsRowIndex, - 'value', - ); - - return { - kaskoRow: kaskoValues, - dgoRow: dgoValues, - nsRow: nsValues, - }; - }, - effect: ({ kaskoRow, dgoRow, nsRow }) => { - const { rows: tableRows } = calculationStore.tables.tableInsurance; - const kaskoRowIndex = tableRows.findIndex( - x => x.policyType?.value === 'КАСКО', - ); - - if ( - kaskoRow && - kaskoRow.insCost === 0 && - ((dgoRow && dgoRow.insCost > 0) || (nsRow && nsRow.insCost > 0)) - ) { - calculationStore.setTableRow( - 'tableInsurance', - 1, - )({ - insCost: { - validation: false, - }, - }); - - openNotification({ - type: 'error', - title: 'Ошибка во время расчета графика', - description: 'Укажите стоимость КАСКО', - })(); - } else { - calculationStore.setTableRow( - 'tableInsurance', - kaskoRowIndex, - )({ - insCost: { - validation: true, - }, - }); - } - }, - }), - - calculationStore => ({ - expression: () => { - const { rows: tableRows } = calculationStore.tables.tableInsurance; - const kaskoRowIndex = tableRows.findIndex( - x => x.policyType?.value === 'КАСКО', - ); - const kaskoValues = calculationStore.getTableRowValues( - 'tableInsurance', - kaskoRowIndex, - 'value', - ); - return { - kaskoRow: kaskoValues, - }; - }, - effect: ({ kaskoRow }) => { - const { rows: tableRows } = calculationStore.tables.tableInsurance; - const kaskoRowIndex = tableRows.findIndex( - x => x.policyType?.value === 'КАСКО', - ); - - if ( - kaskoRow && - kaskoRow.insured === 100000001 && - kaskoRow.insCost === 0 - ) { - calculationStore.setTableRow( - 'tableInsurance', - kaskoRowIndex, - )({ - insCost: { - validation: false, - }, - }); - openNotification({ - type: 'error', - title: 'Ошибка во время расчета графика', - description: 'Укажите стоимость КАСКО, включаемую в график', - })(); - } else { - calculationStore.setTableRow( - 'tableInsurance', - kaskoRowIndex, - )({ - insCost: { - validation: true, - }, - }); - } - }, - }), - - calculationStore => ({ - expression: () => { - const { rows: tableRows } = calculationStore.tables.tableInsurance; - const osagoRowIndex = tableRows.findIndex( - x => x.policyType?.value === 'ОСАГО', - ); - const osagoValues = calculationStore.getTableRowValues( - 'tableInsurance', - osagoRowIndex, - 'value', - ); - return { - osagoRow: osagoValues, - }; - }, - effect: ({ osagoRow }) => { - const { rows: tableRows } = calculationStore.tables.tableInsurance; - const osagoRowIndex = tableRows.findIndex( - x => x.policyType?.value === 'ОСАГО', - ); - - if ( - osagoRow && - osagoRow.insured === 100000001 && - osagoRow.insCost === 0 - ) { - calculationStore.setTableRow( - 'tableInsurance', - osagoRowIndex, - )({ - insCost: { - validation: false, - }, - }); - openNotification({ - type: 'error', - title: 'Ошибка во время расчета графика', - description: 'Укажите стоимость ОСАГО, включаемую в график', - })(); - } else { - calculationStore.setTableRow( - 'tableInsurance', - osagoRowIndex, - )({ - insCost: { - validation: true, - }, - }); - } - }, - }), - - calculationStore => ({ - expression: () => { - const { graphType, leasingPeriod } = calculationStore.values; - return { graphType, leasingPeriod }; - }, - effect: ({ graphType, leasingPeriod }) => { - if (graphType === 100000003 && leasingPeriod < 14) { - calculationStore.setValue('leasingPeriod', 14); - openNotification({ - type: 'warning', - title: 'Внимание', - description: - 'При сезонном графике срок лизинга должен быть больше 14 месяцев', - })(); - } - }, - }), - - calculationStore => ({ - expression: () => { - const { - leasingPeriod, - graphType, - parmentsDecreasePercent, - seasonType, - highSeasonStart: highSeasonStartId, - firstPaymentPerc, - lastPaymentPerc, - } = calculationStore.values; - - const highSeasonStart = calculationStore.options.selectHighSeasonStart?.find( - x => x.value === highSeasonStartId, - ); - - return { - leasingPeriod, - graphType, - parmentsDecreasePercent, - seasonType, - highSeasonStart: parseInt(highSeasonStart?.name || '2'), - firstPaymentPerc, - lastPaymentPerc, - }; - }, - effect: async (nextParams, prevParams) => { - const { - leasingPeriod, - graphType, - parmentsDecreasePercent, - seasonType, - highSeasonStart, - firstPaymentPerc, - lastPaymentPerc, - } = nextParams; - - const prevValues = toJS(calculationStore.tables.tablePayments.rows).map( - x => x.paymentRelation?.value, - ); - let payments: TableProps[] = [ - { - paymentRelation: { - value: firstPaymentPerc, - status: ElementStatus.Disabled, - }, - }, - ]; - - calculationStore.cleanTable('tablePayments'); - - switch (graphType) { - case 100000000: { - const middleRows = Array.from({ length: leasingPeriod - 2 }, () => ({ - paymentRelation: { - value: 100, - status: ElementStatus.Disabled, - }, - })); - payments = [...payments, ...middleRows]; - - break; - } - - case 100000001: { - const middleRows = Array.from({ length: leasingPeriod - 3 }, () => ({ - paymentRelation: { - value: 100, - status: ElementStatus.Default, - }, - })); - payments = [ - ...payments, - { - paymentRelation: { - value: 100, - status: ElementStatus.Disabled, - }, - }, - ...middleRows, - ]; - - break; - } - - case 100000002: { - const middleRows = Array.from( - { length: leasingPeriod - 3 }, - (v, i) => ({ - paymentRelation: { - value: 100, - status: ElementStatus.Disabled, - }, - }), - ); - for (let i in middleRows) { - const currRow = middleRows[parseInt(i)]; - const prevRow = middleRows[parseInt(i) - 1]; - currRow.paymentRelation.value = parseFloat( - ( - ((prevRow ? prevRow.paymentRelation.value : 100) * - parmentsDecreasePercent) / - 100 - ).toFixed(2), - ); - } - payments = [ - ...payments, - { - paymentRelation: { - value: 100, - status: ElementStatus.Disabled, - }, - }, - ...middleRows, - ]; - - break; - } - - case 100000003: { - let HIGH = 100, - MIDDLE = 75, - LOW = 50; - - if ( - prevParams.graphType === nextParams.graphType - // && nextParams.graphType === 100000003 - ) { - /** - * FIND PREV HIGH, MIDDLE, LOW - */ - const { - leasingPeriod: prevLeasingPeriod, - seasonType: prevSeasonType, - highSeasonStart: prevHighSeasonStart, - } = prevParams; - - const prevPeriodsNumber = - prevLeasingPeriod <= 14 ? prevLeasingPeriod - 2 : 12; - const prevShiftNumber = prevHighSeasonStart - 2; - - let middleRows = prevValues.slice(1, prevPeriodsNumber + 1); - if (middleRows.length < 12) { - middleRows = [ - ...middleRows, - ...Array.from({ length: 12 - middleRows.length }, v => 0), - ]; - } - if (prevShiftNumber > 0) - middleRows = shiftRight(middleRows, prevShiftNumber); - - switch (prevSeasonType) { - // 6/6 - case 100000000: { - HIGH = middleRows[0]; - LOW = middleRows[6]; - break; - } - // 8/4 - case 100000001: { - HIGH = middleRows[0]; - LOW = middleRows[8]; - - break; - } - // 4/4/4 - case 100000002: { - HIGH = middleRows[0]; - MIDDLE = middleRows[4]; - LOW = middleRows[8]; - break; - } - } - /** */ - } - - /** - * GENERATE PERIODS - */ - - const { - leasingPeriod: nextLeasingPeriod, - seasonType: nextSeasonType, - highSeasonStart: nextHighSeasonStart, - } = nextParams; - - const nextPeriodsNumber = - nextLeasingPeriod <= 14 ? nextLeasingPeriod - 2 : 12; - const nextShiftNumber = nextHighSeasonStart - 2; - - let nextPeriods: number[] = []; - - switch (nextSeasonType) { - // 6/6 - case 100000000: { - nextPeriods = Array.from({ length: 12 }, (v, i) => - i < 6 ? HIGH : LOW, - ); - break; - } - // 8/4 - case 100000001: { - nextPeriods = Array.from({ length: 12 }, (v, i) => - i < 8 ? HIGH : LOW, - ); - break; - } - // 4/4/4 - case 100000002: { - nextPeriods = Array.from( - { length: 12 }, - (v, i) => (i < 4 && HIGH) || (i < 8 && MIDDLE) || LOW, - ); - break; - } - } - if (nextShiftNumber > 0) { - nextPeriods = shift(nextPeriods, nextShiftNumber); - } - nextPeriods.length = nextPeriodsNumber; - - const middleRows = Array.from( - { length: nextLeasingPeriod - 2 }, - (v, i) => { - return { - paymentRelation: { - value: - nextPeriods[ - i - nextPeriodsNumber * Math.floor(i / nextPeriodsNumber) - ], - status: - i < nextPeriodsNumber ? ElementStatus.Default : ElementStatus.Disabled, - }, - }; - }, - ); - - payments = [...payments, ...middleRows]; - - break; - } - - case 100000004: { - const middleRows = Array.from({ length: leasingPeriod - 5 }, () => ({ - paymentRelation: { - value: 100, - status: ElementStatus.Disabled, - }, - })); - - payments = [ - ...payments, - { - paymentRelation: { - value: 25, - status: ElementStatus.Default, - }, - }, - { - paymentRelation: { - value: 50, - status: ElementStatus.Default, - }, - }, - { - paymentRelation: { - value: 75, - status: ElementStatus.Default, - }, - }, - ...middleRows, - ]; - break; - } - - default: { - break; - } - } - - payments = [ - ...payments, - { - paymentRelation: { - value: lastPaymentPerc, - status: ElementStatus.Disabled, - }, - }, - ]; - - calculationStore.setTableRows('tablePayments', 0)(payments); - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { supplier } = calculationStore.values; - return supplier; - }, - effect: supplierId => { - calculationStore.setOptions('selectFinDepartment', []); - calculationStore.setValue('finDepartment', null); - if (supplierId) { - const supplier = calculationStore.options.selectSupplier?.find( - x => x.accountid === supplierId, - ); - if (supplier && supplier.evo_fin_department_accountid) - CrmService.crmgqlquery({ - query: gql` - query($accountid: Uuid!) { - account(accountid: $accountid) { - accountid - name - } - } - `, - toOptions: ['account'], - variables: { - accountid: supplier.evo_fin_department_accountid, - }, - }).then(({ entities }) => { - if (entities.account && !Array.isArray(entities.account)) { - calculationStore.setOptions('selectFinDepartment', [ - entities.account, - ]); - } - }); - } - }, - }), - calculationStore => ({ - expression: () => { - const { supplier, channel } = calculationStore.values; - return { supplierId: supplier, channelId: channel }; - }, - //TODO: add $ownerid: Uuid - effect: ({ supplierId, channelId }) => { - calculationStore.setOptions('selectAgent', []); - calculationStore.setValue('agent', null); - if (channelId === 100000002) { - CrmService.crmgqlquery({ - query: gql` - query( - $statecode: Int - $evo_account_type: [Int!] - $evo_legal_form: Int - ) { - account: accounts( - statecode: $statecode - evo_account_type: $evo_account_type - evo_legal_form: $evo_legal_form - ) { - accountid - name - } - } - `, - toOptions: ['account'], - variables: { - statecode: 0, - evo_account_type: [100000005], - evo_legal_form: 100000004, - }, - }).then(({ entities }) => { - if (entities.account && Array.isArray(entities.account)) { - calculationStore.setOptions('selectAgent', entities.account); - } - }); - } else { - if (supplierId) { - CrmService.crmgqlquery({ - query: gql` - query($statecode: Int, $salonaccountid: Uuid!) { - account: salon_agents( - statecode: $statecode - salonaccountid: $salonaccountid - ) { - accountid - name - } - } - `, - toOptions: ['account'], - variables: { - statecode: 0, - salonaccountid: supplierId, - }, - }).then(({ entities }) => { - if (entities.account && Array.isArray(entities.account)) { - calculationStore.setOptions('selectAgent', entities.account); - } - }); - } - } - }, - }), - //TODO - // calculationStore => ({ - // expression: () => { - // const { product } = calculationStore.values; - // return product; - // }, - // effect: productId => { - // if (productId) { - // const product = calculationStore.options.selectProduct?.find( - // x => x.evo_id === productId, - // ); - // if ( - // product && - // product.evo_leasingobject_types && - // Array.isArray(product.evo_leasingobject_types) - // ) { - // calculationStore.setOptions( - // 'selectLeaseObjectType', - // product.evo_leasingobject_types, - // ); - // } - // } - // }, - // }), - - calculationStore => ({ - expression: () => { - const { product } = calculationStore.values; - return product; - }, - effect: productId => { - if (productId) { - const product = calculationStore.options.selectProduct?.find( - x => x.evo_id === productId, - ); - if (product) { - const registraction_2020_3 = calculationStore.options.selectRegistration?.find( - x => x.evo_id === '2020_3', - ); - if (registraction_2020_3) { - calculationStore.setValue( - 'registration', - registraction_2020_3.evo_addproduct_typeid, - ); - } - } - } - }, - }), - - calculationStore => ({ - expression: () => { - const { GPSBrand } = calculationStore.values; - return GPSBrand; - }, - effect: GPSBrandId => { - calculationStore.setStatus('selectGPSModel', ElementStatus.Disabled); - calculationStore.setOptions('selectGPSModel', []); - calculationStore.setValue('GPSModel', null); - - if (GPSBrandId) { - const gpsBrand = calculationStore.options.selectGPSBrand?.find( - x => x.evo_gps_brandid === GPSBrandId, - ); - if (gpsBrand) { - CrmService.crmgqlquery({ - query: gql` - query($statecode: Int, $evo_gps_brandid: Uuid) { - evo_gps_model: evo_gps_models( - statecode: $statecode - evo_gps_brandid: $evo_gps_brandid - ) { - evo_name - evo_gps_modelid - } - } - `, - variables: { - statecode: 0, - evo_gps_brandid: GPSBrandId, - }, - toOptions: ['evo_gps_model'], - }).then(({ entities }) => { - if ( - entities.evo_gps_model && - Array.isArray(entities.evo_gps_model) && - entities.evo_gps_model.length > 0 - ) { - calculationStore.setStatus('selectGPSModel', ElementStatus.Default); - calculationStore.setOptions( - 'selectGPSModel', - entities.evo_gps_model, - ); - calculationStore.setValue('GPSModel', null); - } - }); - } - } - }, - options: { fireImmediately: true }, - }), - - calculationStore => ({ - expression: () => { - const { regionRegistration } = calculationStore.values; - return regionRegistration; - }, - effect: regionRegistrationId => { - calculationStore.setStatus('selectTownRegistration', ElementStatus.Disabled); - calculationStore.setOptions('selectTownRegistration', []); - calculationStore.setValue('townRegistration', null); - - if (regionRegistrationId) { - const regionRegistration = calculationStore.options.selectRegionRegistration?.find( - x => x.evo_regionid === regionRegistrationId, - ); - if (regionRegistration) { - CrmService.crmgqlquery({ - query: gql` - query($statecode: Int, $evo_regionid: Uuid) { - evo_town: evo_towns( - statecode: $statecode - evo_regionid: $evo_regionid - ) { - evo_name - evo_townid - } - } - `, - variables: { - statecode: 0, - evo_regionid: regionRegistrationId, - }, - toOptions: ['evo_town'], - }).then(({ entities }) => { - if ( - entities.evo_town && - Array.isArray(entities.evo_town) && - entities.evo_town.length > 0 - ) { - calculationStore.setStatus( - 'selectTownRegistration', - ElementStatus.Default, - ); - calculationStore.setOptions( - 'selectTownRegistration', - entities.evo_town, - ); - calculationStore.setValue('townRegistration', null); - } - }); - } - } - }, - options: { fireImmediately: true }, - }), - - calculationStore => ({ - expression: () => { - const { product, leasingPeriod } = calculationStore.values; - return { product_evo_id: product, leasingPeriod }; - }, - effect: ({ product_evo_id, leasingPeriod }) => { - if (product_evo_id && leasingPeriod) { - const product = calculationStore.options.selectProduct?.find( - x => x.evo_id === product_evo_id, - ); - - if (product) { - const tarif = calculationStore.options.selectTarif?.find( - x => - x.evo_baseproductid === product.evo_baseproductid && - x.evo_min_period && - x.evo_min_period <= leasingPeriod && - x.evo_max_period && - x.evo_max_period >= leasingPeriod, - ); - calculationStore.setValue('tarif', tarif?.evo_tarifid); - } - } else { - calculationStore.setValue('tarif', null); - } - }, - }), - - calculationStore => ({ - expression: () => { - const { tarif } = calculationStore.values; - return tarif; - }, - effect: tarif_evo_id => { - const tarif = calculationStore.options.selectTarif?.find( - x => x.evo_tarifid === tarif_evo_id, - ); - - if (tarif) { - calculationStore.setValue('IRR_Perc', tarif.evo_irr); - } - }, - }), -]; - -export default reactionEffects; diff --git a/src/client/stores/CalculationStore/Effects/reactions/index.ts b/src/client/stores/CalculationStore/Effects/reactions/index.ts new file mode 100644 index 0000000..d3b6753 --- /dev/null +++ b/src/client/stores/CalculationStore/Effects/reactions/index.ts @@ -0,0 +1,11 @@ +import requestReactions from './requestReactions'; +import otherReactions from './otherReactions'; +import tablesReactions from './tablesReactions'; +import loadKpReaction from './loadKpReaction'; + +export default [ + ...otherReactions, + ...requestReactions, + ...tablesReactions, + loadKpReaction, +]; diff --git a/src/client/stores/CalculationStore/Effects/reactions/loadKpReaction/index.ts b/src/client/stores/CalculationStore/Effects/reactions/loadKpReaction/index.ts new file mode 100644 index 0000000..e6ff163 --- /dev/null +++ b/src/client/stores/CalculationStore/Effects/reactions/loadKpReaction/index.ts @@ -0,0 +1,359 @@ +//@ts-nocheck + +import { gql } from '@apollo/client'; +import { elementsValues } from 'client/Containers/Calculation/lib/elements/values'; +import CrmService from 'client/services/CrmService'; +import initialValues from 'client/stores/CalculationStore/config/initialValues'; +import { IReactionEffect } from 'core/types/Calculation/Store/effect'; +import { ElementStatus } from 'core/types/statuses'; +import mapKPtoValues from './mapKpToValues'; +import { currentDate } from 'client/tools/date'; +import { NIL } from 'uuid'; +import { Process } from 'core/types/Calculation/Store/process'; +import { calculationProcess } from 'client/stores/CalculationStore'; + +const quoteQuery = gql` + query($quoteId: Uuid!) { + quote(quoteId: $quoteId) { + ${Object.values(mapKPtoValues).join(' ')} + evo_addproduct_types { + evo_product_type + evo_addproduct_typeid + } + evo_osago_accountid + evo_kasko_accountid + evo_osago_payer + evo_kasko_payer + evo_osago_price + evo_kasko_price + evo_dgo_price + evo_ns_price + evo_insurance_period + evo_graphs { + createdon + evo_planpayments { + evo_payment_ratio + } + } + } + } +`; + +const optionsQuery = gql` + query( + $statecode: Int + $evo_brandid: Uuid + $evo_modelid: Uuid + $salonaccountid: Uuid! + $currentDate: DateTime + $dealer_person_accountid: Uuid! + $dealer_broker_accountid: Uuid! + $ind_agent_accountid: Uuid! + $double_agent_accountid: Uuid! + $broker_accountid: Uuid! + $findepartment_accountid: Uuid! + $evo_gps_brandid: Uuid! + $evo_regionid: Uuid! + ) { + selectModel: evo_models(statecode: $statecode, evo_brandid: $evo_brandid) { + evo_name + evo_modelid + evo_leasingobject_risk + evo_importer_reward_perc + evo_importer_reward_rub + evo_impairment_groupid + } + selectConfiguration: evo_equipments( + statecode: $statecode + evo_modelid: $evo_modelid + ) { + evo_equipmentid + evo_name + evo_impairment_groupid + evo_leasingobject_risk + evo_start_production_year + } + selectDealerPerson: salon_providers( + statecode: $statecode + salonaccountid: $salonaccountid + ) { + accountid + name + evo_broker_accountid + } + selectDealerRewardCondition: evo_reward_conditions( + evo_agent_accountid: $dealer_person_accountid + evo_datefrom_param: { lte: $currentDate } + evo_dateto_param: { gte: $currentDate } + statecode: $statecode + ) { + evo_reward_conditionid + evo_name + evo_reward_summ + evo_reduce_reward + } + selectDealerBroker: account(accountid: $dealer_broker_accountid) { + accountid + name + } + selectDealerBrokerRewardCondition: evo_reward_conditions( + evo_agent_accountid: $dealer_broker_accountid + evo_datefrom_param: { lte: $currentDate } + evo_dateto_param: { gte: $currentDate } + statecode: $statecode + ) { + evo_reward_conditionid + evo_name + evo_reward_summ + evo_reduce_reward + } + selectIndAgentRewardCondition: evo_reward_conditions( + evo_agent_accountid: $ind_agent_accountid + evo_datefrom_param: { lte: $currentDate } + evo_dateto_param: { gte: $currentDate } + statecode: $statecode + ) { + evo_reward_conditionid + evo_name + evo_double_agent_accountid + evo_reward_summ + evo_reduce_reward + } + calcDoubleAgentRewardCondition: evo_reward_conditions( + evo_agent_accountid: $double_agent_accountid + evo_datefrom_param: { lte: $currentDate } + evo_dateto_param: { gte: $currentDate } + statecode: $statecode + ) { + evo_reward_conditionid + evo_name + evo_reward_summ + evo_reduce_reward + } + calcBrokerRewardCondition: evo_reward_conditions( + evo_agent_accountid: $broker_accountid + evo_datefrom_param: { lte: $currentDate } + evo_dateto_param: { gte: $currentDate } + statecode: $statecode + ) { + evo_reward_conditionid + evo_name + evo_reward_summ + evo_reduce_reward + } + selectFinDepartmentRewardCondtion: evo_reward_conditions( + evo_agent_accountid: $findepartment_accountid + evo_datefrom_param: { lte: $currentDate } + evo_dateto_param: { gte: $currentDate } + statecode: $statecode + ) { + evo_reward_conditionid + evo_name + evo_reward_summ + evo_reduce_reward + } + selectGPSModel: evo_gps_models( + statecode: $statecode + evo_gps_brandid: $evo_gps_brandid + ) { + evo_name + evo_gps_modelid + } + selectTownRegistration: evo_towns( + statecode: $statecode + evo_regionid: $evo_regionid + ) { + evo_name + evo_townid + } + } +`; + +const map_add_product_types_to_values = { + technicalCard: 100000000, + registration: 100000001, + insNSIB: 100000002, + tracker: 100000003, + telematics: 100000004, +}; + +const loadKpReaction: IReactionEffect = calculationStore => ({ + expression: () => { + const { quote } = calculationStore.values; + return quote; + }, + effect: quoteId => { + const { lead, quote, opportunity } = calculationStore.values; + + if (!quoteId) { + calculationStore.setValues({ + ...initialValues, + lead, + opportunity, + quote, + }); + return; + } + + calculationStore.setStatus('selectQuote', ElementStatus.Disabled); + calculationStore.setStatus('btnCalculate', ElementStatus.Disabled); + calculationStore.setStatus('btnCreateKP', ElementStatus.Disabled); + + CrmService.crmgqlquery({ + query: quoteQuery, + variables: { + quoteId, + }, + }) + .then(async ({ entities: { quote } }) => { + if (!quote) { + throw new Error('No quote!'); + } + calculationProcess.setProcess(Process.LoadKp); + if (!Array.isArray(quote)) { + const newValues = Object.assign( + {}, + ...Object.values(elementsValues).map(valueName => ({ + [valueName]: quote[mapKPtoValues[valueName]], + })), + ); + + const { entities: options } = await CrmService.crmgqlquery({ + query: optionsQuery, + variables: { + statecode: 0, + evo_brandid: quote.evo_brandid || NIL, + evo_modelid: quote.evo_modelid || NIL, + salonaccountid: quote.evo_supplier_accountid || NIL, + currentDate, + dealer_person_accountid: quote.evo_dealer_person_accountid || NIL, + dealer_broker_accountid: quote.evo_dealer_broker_accountid || NIL, + ind_agent_accountid: quote.evo_agent_accountid || NIL, + double_agent_accountid: quote.evo_double_agent_accountid || NIL, + broker_accountid: quote.evo_broker_accountid || NIL, + findepartment_accountid: + quote.evo_fin_department_accountid || NIL, + evo_gps_brandid: quote.evo_gps_brandid || NIL, + evo_regionid: quote.evo_regionid || NIL, + }, + toOptions: [ + 'selectModel', + 'selectConfiguration', + 'selectDealerPerson', + 'selectDealerRewardCondition', + 'selectDealerBroker', + 'selectDealerBrokerRewardCondition', + 'selectIndAgentRewardCondition', + 'selectCalcDoubleAgentRewardCondition', + 'selectCalcBrokerRewardCondition', + 'selectFinDepartmentRewardCondtion', + 'selectGPSModel', + 'selectTownRegistration', + ], + }); + + Object.keys(options).forEach(elementName => { + if (Array.isArray(options[elementName])) { + calculationStore.setOptions(elementName, options[elementName]); + } else { + calculationStore.setOptions(elementName, [options[elementName]]); + } + calculationStore.setStatus(elementName, ElementStatus.Default); + }); + + let base_product_evo_id; + const base_product = calculationStore?.options?.selectProduct?.find( + x => x.evo_baseproductid === newValues.product, + ); + if (base_product) { + base_product_evo_id = base_product.evo_id; + } + + let addProducts = Object.assign( + {}, + ...Object.keys(map_add_product_types_to_values).map(elementName => { + const target_add_product_type = quote?.evo_addproduct_types?.find( + x => + x.evo_product_type === + map_add_product_types_to_values[elementName], + ); + + return { + [elementName]: + target_add_product_type && + target_add_product_type.evo_addproduct_typeid, + }; + }), + ); + + const tbxLeaseObjectCount = { + leaseObjectCount: calculationStore.values.recalcWithRevision + ? quote.evo_recalc_limit + : quote.evo_object_count, + }; + + calculationStore.setValues({ + ...initialValues, + ...newValues, + product: base_product_evo_id, + ...addProducts, + ...tbxLeaseObjectCount, + lead, + opportunity, + quote: quoteId, + }); + } + + calculationStore.setTableRows( + 'tableInsurance', + 0, + )([ + { + insuranceCompany: { + value: quote?.evo_osago_accountid, + }, + insured: { value: quote.evo_osago_payer }, + insCost: { value: quote.evo_osago_price }, + }, + { + insuranceCompany: { + value: quote.evo_kasko_accountid, + }, + insured: { value: quote.evo_kasko_payer }, + insCost: { value: quote.evo_kasko_price }, + insTerm: { value: quote.evo_insurance_period }, + }, + { + insured: { value: quote.evo_kasko_payer }, + insCost: { value: quote.evo_dgo_price }, + insTerm: { value: quote.evo_insurance_period }, + }, + { + insured: { value: quote.evo_kasko_payer }, + insCost: { value: quote.evo_ns_price }, + insTerm: { value: quote.evo_insurance_period }, + }, + ]); + + const evo_planpayments = quote.evo_graphs.sort( + (a, b) => b.createdon - a.createdon, + )[0].evo_planpayments; + const payments = evo_planpayments + .slice(1, evo_planpayments.length - 1) + .map(evo_planpayment => ({ + paymentRelation: { + value: evo_planpayment.evo_payment_ratio, + }, + })); + calculationStore.setTableRows('tablePayments', 1)(payments); + }) + .finally(() => { + calculationStore.setStatus('selectQuote', ElementStatus.Default); + calculationStore.setStatus('btnCalculate', ElementStatus.Default); + calculationStore.setStatus('btnCreateKP', ElementStatus.Default); + calculationProcess.setProcess(Process.Default); + }); + }, +}); + +export default loadKpReaction; diff --git a/src/client/constants/Calculation/kpToValues.ts b/src/client/stores/CalculationStore/Effects/reactions/loadKpReaction/mapKpToValues.ts similarity index 85% rename from src/client/constants/Calculation/kpToValues.ts rename to src/client/stores/CalculationStore/Effects/reactions/loadKpReaction/mapKpToValues.ts index 30feaca..c940941 100644 --- a/src/client/constants/Calculation/kpToValues.ts +++ b/src/client/stores/CalculationStore/Effects/reactions/loadKpReaction/mapKpToValues.ts @@ -87,21 +87,6 @@ const mapKPtoValues: TValues = { maxPriceChange: 'evo_max_price_change', importerRewardPerc: 'evo_importer_reward_perc', importerRewardRub: 'evo_importer_reward_rub', - // insuranceCompanyOSAGO: 'evo_osago_accountid', - // insuranceCompanyKASKO: 'evo_kasko_accountid', - // insuranceCompanyDGO: 'evo_kasko_accountid', - // insuranceCompanyNS: 'evo_kasko_accountid', - // insuredOSAGO: 'evo_osago_payer', - // insuredKASKO: 'evo_kasko_payer', - // insuredDGO: 'evo_kasko_payer', - // insuredNS: 'evo_kasko_payer', - // inscostOSAGO: 'evo_osago_price', - // inscostKASKO: 'evo_kasko_price', - // inscostDGO: 'evo_dgo_price', - // inscostNS: 'evo_ns_price', - // insTermKASKO: 'evo_first_insurance_period', - // insTermDGO: 'evo_first_insurance_period', - // insTermNS: 'evo_first_insurance_period', requirementTelematic: 'evo_req_telematic', }; diff --git a/src/client/stores/CalculationStore/Effects/reactions/otherReactions.ts b/src/client/stores/CalculationStore/Effects/reactions/otherReactions.ts new file mode 100644 index 0000000..80a1783 --- /dev/null +++ b/src/client/stores/CalculationStore/Effects/reactions/otherReactions.ts @@ -0,0 +1,1708 @@ +import { openNotification } from 'client/Elements/Notification'; +import { IReactionEffect } from 'core/types/Calculation/Store/effect'; +import { ElementStatus } from 'core/types/statuses'; +import { calcPrice, calculatePerc, calculateRub } from '../lib/tools'; + +const reactionEffects: IReactionEffect[] = [ + // calculationStore => ({ + // expression: () => { + // const { options } = calculationStore; + // return options.selectQuote; + // }, + // effect: quotes => { + // if (quotes.length > 0) { + // calculationStore.setStatus('tbxQuoteName', Status.Disabled); + // } else { + // calculationStore.setStatus('tbxQuoteName', Status.Default); + // } + // }, + // }), + + calculationStore => ({ + expression: () => { + const { opportunity } = calculationStore.values; + return opportunity; + }, + effect: opportunityId => { + const opportunity = calculationStore.options.selectOpportunity?.find( + x => x.opportunityid === opportunityId, + ); + + if (opportunity) { + calculationStore.setValue('lead', opportunity.evo_leadid); + } + }, + }), + + calculationStore => ({ + expression: () => { + const { agent } = calculationStore.values; + return agent; + }, + effect: agentid => { + if (!agentid) { + calculationStore.setStatus('selectDoubleAgent', ElementStatus.Disabled); + } else { + calculationStore.setStatus('selectDoubleAgent', ElementStatus.Default); + } + }, + }), + + calculationStore => ({ + expression: () => { + const { channel } = calculationStore.values; + return channel; + }, + effect: channel => { + switch (channel) { + case 100000000: + calculationStore.setStatus('selectSupplier', ElementStatus.Default); + calculationStore.setStatus('selectAgent', ElementStatus.Default); + + calculationStore.setStatus( + 'selectFinDepartment', + ElementStatus.Disabled, + ); + calculationStore.setValue('finDepartment', null); + + calculationStore.setStatus('selectBroker', ElementStatus.Disabled); + calculationStore.setValue('broker', null); + break; + + case 100000001: + calculationStore.setStatus('selectSupplier', ElementStatus.Default); + calculationStore.setStatus('selectAgent', ElementStatus.Default); + + calculationStore.setStatus( + 'selectFinDepartment', + ElementStatus.Default, + ); + + calculationStore.setStatus('selectBroker', ElementStatus.Disabled); + calculationStore.setValue('broker', null); + break; + case 100000002: + calculationStore.setStatus('selectSupplier', ElementStatus.Disabled); + calculationStore.setValue('supplier', null); + + calculationStore.setStatus('selectAgent', ElementStatus.Default); + + calculationStore.setStatus( + 'selectFinDepartment', + ElementStatus.Disabled, + ); + calculationStore.setValue('finDepartment', null); + + calculationStore.setStatus('selectBroker', ElementStatus.Disabled); + calculationStore.setValue('broker', null); + break; + case 100000003: + calculationStore.setStatus('selectSupplier', ElementStatus.Disabled); + calculationStore.setValue('supplier', null); + + calculationStore.setStatus('selectAgent', ElementStatus.Default); + calculationStore.setValue('agent', null); + + calculationStore.setStatus( + 'selectFinDepartment', + ElementStatus.Disabled, + ); + calculationStore.setValue('finDepartment', null); + + calculationStore.setStatus('selectBroker', ElementStatus.Default); + break; + case 100000004: + default: + calculationStore.setStatus('selectSupplier', ElementStatus.Disabled); + calculationStore.setValue('supplier', null); + + calculationStore.setStatus('selectAgent', ElementStatus.Disabled); + calculationStore.setValue('agent', null); + + calculationStore.setStatus( + 'selectFinDepartment', + ElementStatus.Disabled, + ); + calculationStore.setValue('finDepartment', null); + + calculationStore.setStatus('selectBroker', ElementStatus.Disabled); + calculationStore.setValue('broker', null); + break; + } + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { newClient } = calculationStore.values; + return newClient; + }, + effect: newClient => { + if (newClient && newClient.length > 0) { + calculationStore.setValue('account', null); + calculationStore.setValue('contactClient', null); + calculationStore.setStatus('selectAccount', ElementStatus.Disabled); + calculationStore.setStatus( + 'selectContactClient', + ElementStatus.Disabled, + ); + } else { + calculationStore.setStatus('selectAccount', ElementStatus.Default); + calculationStore.setStatus( + 'selectContactClient', + ElementStatus.Default, + ); + } + }, + }), + + calculationStore => ({ + expression: () => { + const { indAgentRewardCondition } = calculationStore.values; + return indAgentRewardCondition; + }, + effect: indAgentRewardConditionId => { + if (!indAgentRewardConditionId) { + calculationStore.setValue('indAgentRewardSumm', null); + calculationStore.setStatus( + 'tbxIndAgentRewardSumm', + ElementStatus.Disabled, + ); + + const leadId = calculationStore.values.lead; + if (leadId) { + const lead = calculationStore.options.selectLead?.find( + x => x.leadid === leadId, + ); + if (lead && !lead.evo_double_agent_accountid) { + calculationStore.setValue('calcDoubleAgent', null); + calculationStore.setStatus( + 'selectCalcDoubleAgent', + ElementStatus.Disabled, + ); + } + } + } else { + const indAgentRewardCondition = calculationStore.options.selectIndAgentRewardCondition?.find( + x => x.evo_reward_conditionid === indAgentRewardConditionId, + ); + if (indAgentRewardCondition) { + calculationStore.setValue( + 'indAgentRewardSumm', + indAgentRewardCondition.evo_reward_summ || 0, + ); + calculationStore.setStatus( + 'tbxIndAgentRewardSumm', + ElementStatus.Default, + ); + if (indAgentRewardCondition?.evo_double_agent_accountid) { + calculationStore.setStatus( + 'selectCalcDoubleAgent', + ElementStatus.Default, + ); + const doubleAgent = calculationStore.options.selectCalcDoubleAgent?.find( + x => + x.accountid === + indAgentRewardCondition?.evo_double_agent_accountid, + ); + if (doubleAgent) { + calculationStore.setOptions('selectCalcDoubleAgent', [ + doubleAgent, + ]); + calculationStore.setValue( + 'calcDoubleAgent', + doubleAgent.accountid, + ); + } + } else { + calculationStore.setValue('calcDoubleAgent', null); + calculationStore.setStatus( + 'selectCalcDoubleAgent', + ElementStatus.Disabled, + ); + } + } + } + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { calcDoubleAgentRewardCondition } = calculationStore.values; + return calcDoubleAgentRewardCondition; + }, + effect: calcDoubleAgentRewardConditionId => { + if (!calcDoubleAgentRewardConditionId) { + calculationStore.setValue('calcDoubleAgentRewardSumm', 0); + calculationStore.setStatus( + 'tbxCalcDoubleAgentRewardSumm', + ElementStatus.Disabled, + ); + } else { + const calcDoubleAgentRewardCondition = calculationStore.options.selectCalcDoubleAgentRewardCondition?.find( + x => x.evo_reward_conditionid === calcDoubleAgentRewardConditionId, + ); + if (calcDoubleAgentRewardCondition) { + if (calcDoubleAgentRewardCondition.evo_reward_summ) { + calculationStore.setValue( + 'calcDoubleAgentRewardSumm', + calcDoubleAgentRewardCondition.evo_reward_summ, + ); + calculationStore.setStatus( + 'tbxCalcDoubleAgentRewardSumm', + ElementStatus.Default, + ); + } + } + } + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { calcBrokerRewardCondition } = calculationStore.values; + return calcBrokerRewardCondition; + }, + effect: calcBrokerRewardConditionId => { + if (!calcBrokerRewardConditionId) { + calculationStore.setValue('calcBrokerRewardSum', 0); + calculationStore.setStatus( + 'tbxCalcBrokerRewardSum', + ElementStatus.Disabled, + ); + } else { + const calcBrokerRewardCondition = calculationStore.options.selectCalcBrokerRewardCondition?.find( + x => x.evo_reward_conditionid === calcBrokerRewardConditionId, + ); + if (calcBrokerRewardCondition) { + if (calcBrokerRewardCondition.evo_reward_summ) { + calculationStore.setValue( + 'calcBrokerRewardSum', + calcBrokerRewardCondition.evo_reward_summ, + ); + calculationStore.setStatus( + 'tbxCalcBrokerRewardSum', + ElementStatus.Default, + ); + } + } + } + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { finDepartmentRewardCondtion } = calculationStore.values; + return finDepartmentRewardCondtion; + }, + effect: finDepartmentRewardCondtionId => { + if (!finDepartmentRewardCondtionId) { + calculationStore.setValue('finDepartmentRewardSumm', 0); + calculationStore.setStatus( + 'tbxFinDepartmentRewardSumm', + ElementStatus.Disabled, + ); + } else { + const finDepartmentRewardCondtion = calculationStore.options.selectFinDepartmentRewardCondtion?.find( + x => x.evo_reward_conditionid === finDepartmentRewardCondtionId, + ); + if (finDepartmentRewardCondtion) { + if (finDepartmentRewardCondtion.evo_reward_summ) { + calculationStore.setValue( + 'finDepartmentRewardSumm', + finDepartmentRewardCondtion.evo_reward_summ, + ); + calculationStore.setStatus( + 'tbxFinDepartmentRewardSumm', + ElementStatus.Default, + ); + } + } + } + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { indAgentRewardSumm } = calculationStore.values; + return indAgentRewardSumm; + }, + effect: indAgentRewardSumm => { + const indAgentRewardConditionId = + calculationStore.values.indAgentRewardCondition; + if (indAgentRewardConditionId) { + const indAgentRewardCondition = calculationStore.options.selectIndAgentRewardCondition?.find( + x => x.evo_reward_conditionid === indAgentRewardConditionId, + ); + if (indAgentRewardCondition) { + if (indAgentRewardCondition.evo_reward_summ) { + if ( + parseFloat(indAgentRewardSumm) > + indAgentRewardCondition.evo_reward_summ + ) { + calculationStore.setValidation('tbxIndAgentRewardSumm', false); + openNotification({ + type: 'error', + title: 'Ошибка', + description: + 'Вознаграждение агента ФЛ указано больше условия по агентскому договору! \nЗначение установлено по условию договора.', + })(); + return; + } else if ( + !indAgentRewardCondition.evo_reduce_reward && + indAgentRewardCondition.evo_reward_summ + ) { + if ( + parseFloat(indAgentRewardSumm) < + indAgentRewardCondition.evo_reward_summ + ) { + calculationStore.setValidation('tbxIndAgentRewardSumm', false); + openNotification({ + type: 'error', + title: 'Ошибка', + description: + 'Вознаграждение агента ФЛ указано меньше условия по агентскому договору! \nЗначение установлено по условию договора.', + })(); + return; + } + } + calculationStore.setValidation('tbxIndAgentRewardSumm', true); + } + } + } + }, + }), + + calculationStore => ({ + expression: () => { + const { calcDoubleAgentRewardSumm } = calculationStore.values; + return calcDoubleAgentRewardSumm; + }, + effect: calcDoubleAgentRewardSumm => { + const calcDoubleAgentRewardConditionId = + calculationStore.values.calcDoubleAgentRewardCondition; + if (calcDoubleAgentRewardConditionId) { + const selectCalcDoubleAgentRewardCondition = calculationStore.options.selectCalcDoubleAgentRewardCondition?.find( + x => x.evo_reward_conditionid === calcDoubleAgentRewardConditionId, + ); + if (selectCalcDoubleAgentRewardCondition) { + if (selectCalcDoubleAgentRewardCondition.evo_reward_summ) { + if ( + parseFloat(calcDoubleAgentRewardSumm) > + selectCalcDoubleAgentRewardCondition.evo_reward_summ + ) { + calculationStore.setValidation( + 'tbxCalcDoubleAgentRewardSumm', + false, + ); + openNotification({ + type: 'error', + title: 'Ошибка', + description: + 'Вознаграждение двойного агента указано больше условия по агентскому договору! \nЗначение установлено по условию договора.', + })(); + return; + } else if ( + !selectCalcDoubleAgentRewardCondition.evo_reduce_reward && + selectCalcDoubleAgentRewardCondition.evo_reward_summ + ) { + if ( + parseFloat(calcDoubleAgentRewardSumm) < + selectCalcDoubleAgentRewardCondition.evo_reward_summ + ) { + calculationStore.setValidation( + 'tbxCalcDoubleAgentRewardSumm', + false, + ); + openNotification({ + type: 'error', + title: 'Ошибка', + description: + 'Вознаграждение двойного агента указано меньше условия по агентскому договору! \nЗначение установлено по условию договора.', + })(); + return; + } + } + calculationStore.setValidation( + 'tbxCalcDoubleAgentRewardSumm', + true, + ); + } + } + } + }, + }), + + calculationStore => ({ + expression: () => { + const { calcBrokerRewardSum } = calculationStore.values; + return calcBrokerRewardSum; + }, + effect: calcBrokerRewardSum => { + const calcBrokerRewardConditionId = + calculationStore.values.calcBrokerRewardCondition; + if (calcBrokerRewardConditionId) { + const selectCalcBrokerRewardCondition = calculationStore.options.selectCalcBrokerRewardCondition?.find( + x => x.evo_reward_conditionid === calcBrokerRewardConditionId, + ); + if (selectCalcBrokerRewardCondition) { + if (selectCalcBrokerRewardCondition.evo_reward_summ) { + if ( + parseFloat(calcBrokerRewardSum) > + selectCalcBrokerRewardCondition.evo_reward_summ + ) { + calculationStore.setValidation('tbxCalcBrokerRewardSum', false); + openNotification({ + type: 'error', + title: 'Ошибка', + description: + 'Вознаграждение брокера указано больше условия по агентскому договору! \nЗначение установлено по условию договора.', + })(); + return; + } else if ( + !selectCalcBrokerRewardCondition.evo_reduce_reward && + selectCalcBrokerRewardCondition.evo_reward_summ + ) { + if ( + parseFloat(calcBrokerRewardSum) < + selectCalcBrokerRewardCondition.evo_reward_summ + ) { + calculationStore.setValidation('tbxCalcBrokerRewardSum', false); + openNotification({ + type: 'error', + title: 'Ошибка', + description: + 'Вознаграждение брокера указано меньше условия по агентскому договору! \nЗначение установлено по условию договора.', + })(); + return; + } + } + calculationStore.setValidation('tbxCalcBrokerRewardSum', true); + } + } + } + }, + }), + + calculationStore => ({ + expression: () => { + const { finDepartmentRewardSumm } = calculationStore.values; + return finDepartmentRewardSumm; + }, + effect: finDepartmentRewardSumm => { + const finDepartmentRewardCondtionid = + calculationStore.values.finDepartmentRewardCondtion; + if (finDepartmentRewardCondtionid) { + const selectFinDepartmentRewardCondtion = calculationStore.options.selectFinDepartmentRewardCondtion?.find( + x => x.evo_reward_conditionid === finDepartmentRewardCondtionid, + ); + if (selectFinDepartmentRewardCondtion) { + if (selectFinDepartmentRewardCondtion.evo_reward_summ) { + if ( + parseFloat(finDepartmentRewardSumm) > + selectFinDepartmentRewardCondtion.evo_reward_summ + ) { + calculationStore.setValidation( + 'tbxFinDepartmentRewardSumm', + false, + ); + openNotification({ + type: 'error', + title: 'Ошибка', + description: + 'Вознаграждение финотдела указано больше условия по агентскому договору! \nЗначение установлено по условию договора.', + })(); + return; + } else if ( + !selectFinDepartmentRewardCondtion.evo_reduce_reward && + selectFinDepartmentRewardCondtion.evo_reward_summ + ) { + if ( + parseFloat(finDepartmentRewardSumm) < + selectFinDepartmentRewardCondtion.evo_reward_summ + ) { + calculationStore.setValidation( + 'tbxFinDepartmentRewardSumm', + false, + ); + openNotification({ + type: 'error', + title: 'Ошибка', + description: + 'Вознаграждение финотдела указано меньше условия по агентскому договору! \nЗначение установлено по условию договора.', + })(); + return; + } + } + calculationStore.setValidation('tbxFinDepartmentRewardSumm', true); + } + } + } + }, + }), + + calculationStore => ({ + expression: () => { + const { leaseObjectUsed } = calculationStore.values; + return leaseObjectUsed; + }, + effect: leaseObjectUsed => { + if (leaseObjectUsed) { + calculationStore.setValue('leaseObjectCount', 1); + calculationStore.setStatus( + 'tbxLeaseObjectCount', + ElementStatus.Disabled, + ); + } else { + calculationStore.setStatus( + 'tbxLeaseObjectCount', + ElementStatus.Default, + ); + } + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { insUnlimitDrivers } = calculationStore.values; + return insUnlimitDrivers; + }, + effect: insUnlimitDrivers => { + if (insUnlimitDrivers) { + calculationStore.setStatus('tbxInsAgeDrivers', ElementStatus.Disabled); + calculationStore.setStatus('tbxInsExpDrivers', ElementStatus.Disabled); + calculationStore.setStatus( + 'btnDriversApplication', + ElementStatus.Disabled, + ); + } else { + calculationStore.setStatus('tbxInsAgeDrivers', ElementStatus.Default); + calculationStore.setStatus('tbxInsExpDrivers', ElementStatus.Default); + calculationStore.setStatus( + 'btnDriversApplication', + ElementStatus.Default, + ); + } + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { insFranchise } = calculationStore.values; + return insFranchise; + }, + effect: insFranchise => { + if (!insFranchise || parseInt(insFranchise) === 0) { + calculationStore.setStatus('btnFranschise', ElementStatus.Disabled); + } else { + calculationStore.setStatus('btnFranschise', ElementStatus.Default); + } + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { lastPaymentRule } = calculationStore.values; + return lastPaymentRule; + }, + effect: lastPaymentRule => { + if (lastPaymentRule) { + if (lastPaymentRule === 100000000) { + calculationStore.setStatus( + 'tbxLastPaymentPerc', + ElementStatus.Disabled, + ); + calculationStore.setStatus( + 'tbxLastPaymentRub', + ElementStatus.Default, + ); + } else { + calculationStore.setStatus( + 'tbxLastPaymentPerc', + ElementStatus.Default, + ); + calculationStore.setStatus( + 'tbxLastPaymentRub', + ElementStatus.Disabled, + ); + } + } + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { lastPaymentPerc, balanceHolder } = calculationStore.values; + return { + lastPaymentPerc, + balanceHolder, + }; + }, + effect: ({ lastPaymentPerc, balanceHolder }) => { + if (balanceHolder && balanceHolder === 100000001) { + if (!lastPaymentPerc || parseFloat(lastPaymentPerc) < 1) { + calculationStore.setValidation('tbxLastPaymentPerc', false); + openNotification({ + type: 'error', + title: 'Ошибка', + description: + 'При балансе лизингодатель последний платеж не может быть меньше 1%! Увеличьте значение.', + })(); + return; + } + } else { + if (parseFloat(lastPaymentPerc) === 0) { + calculationStore.setValidation('tbxLastPaymentPerc', false); + openNotification({ + type: 'error', + title: 'Ошибка', + description: + 'Последний платеж не может быть равен 0. Увеличьте значение', + })(); + return; + } + } + calculationStore.setValidation('tbxLastPaymentPerc', true); + }, + }), + + calculationStore => ({ + expression: () => { + const { + lastPaymentRub, + lastPaymentPerc, + insNSIB, + } = calculationStore.values; + return { lastPaymentRub, lastPaymentPerc, insNSIB }; + }, + effect: ({ lastPaymentRub, insNSIB }) => { + if (insNSIB && lastPaymentRub && lastPaymentRub < 3500) { + calculationStore.setValidation('tbxLastPaymentRub', false); + openNotification({ + type: 'error', + title: 'Ошибка', + description: + 'Последний платеж меньше 3500 руб. не может быть при наличии НСИБ, укажите большее значение', + })(); + return; + } else if (lastPaymentRub && lastPaymentRub < 1000) { + calculationStore.setValidation('tbxLastPaymentRub', false); + openNotification({ + type: 'error', + title: 'Ошибка', + description: + 'Последний платеж меньше 1000 руб. не может быть, укажите большее значение', + })(); + return; + } + calculationStore.setValidation('tbxLastPaymentRub', true); + }, + }), + + calculationStore => ({ + expression: () => { + const { graphType } = calculationStore.values; + return graphType; + }, + effect: graphType => { + if (graphType) { + switch (graphType) { + case 100000002: { + calculationStore.setStatus( + 'radioSeasonType', + ElementStatus.Disabled, + ); + calculationStore.setStatus( + 'tbxParmentsDecreasePercent', + ElementStatus.Default, + ); + calculationStore.setStatus( + 'selectHighSeasonStart', + ElementStatus.Disabled, + ); + break; + } + + case 100000003: { + calculationStore.setStatus( + 'radioSeasonType', + ElementStatus.Default, + ); + calculationStore.setStatus( + 'tbxParmentsDecreasePercent', + ElementStatus.Disabled, + ); + calculationStore.setStatus( + 'selectHighSeasonStart', + ElementStatus.Default, + ); + break; + } + + default: { + calculationStore.setStatus( + 'radioSeasonType', + ElementStatus.Disabled, + ); + calculationStore.setStatus( + 'tbxParmentsDecreasePercent', + ElementStatus.Disabled, + ); + calculationStore.setStatus( + 'selectHighSeasonStart', + ElementStatus.Disabled, + ); + break; + } + } + } + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { seasonType } = calculationStore.values; + return seasonType; + }, + effect: seasonType => { + if (seasonType) { + switch (seasonType) { + case 100000001: + case 100000002: { + calculationStore.setFilter('selectHighSeasonStart', seasons => { + return seasons.filter( + season => season.value && season.value <= 100000004, + ); + }); + break; + } + default: + calculationStore.setFilter('selectHighSeasonStart', undefined); + break; + } + } + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { leasingPeriod } = calculationStore.values; + return leasingPeriod; + }, + effect: leasingPeriod => { + if (leasingPeriod) { + if (parseInt(leasingPeriod) < 12) { + calculationStore.setStatus( + 'radioBalanceHolder', + ElementStatus.Disabled, + ); + calculationStore.setValue('balanceHolder', 100000000); + calculationStore.setValue('insNSIB', null); + calculationStore.setStatus('selectInsNSIB', ElementStatus.Disabled); + } else { + calculationStore.setStatus( + 'radioBalanceHolder', + ElementStatus.Default, + ); + calculationStore.setStatus('selectInsNSIB', ElementStatus.Default); + } + } + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { balanceHolder } = calculationStore.values; + return balanceHolder; + }, + effect: balanceHolder => { + if (balanceHolder) { + if (balanceHolder === 100000001) { + calculationStore.setStatus( + 'cbxLastPaymentRedemption', + ElementStatus.Disabled, + ); + calculationStore.setValue('lastPaymentRedemption', true); + } else { + calculationStore.setStatus( + 'cbxLastPaymentRedemption', + ElementStatus.Default, + ); + } + } + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { dealerRewardCondition } = calculationStore.values; + return dealerRewardCondition; + }, + effect: dealerRewardConditionId => { + if (!dealerRewardConditionId) { + calculationStore.setValue('dealerRewardSumm', 0); + calculationStore.setStatus( + 'tbxDealerRewardSumm', + ElementStatus.Disabled, + ); + } else { + const dealerRewardContition = calculationStore.options.selectDealerRewardCondition?.find( + x => x.evo_reward_conditionid === dealerRewardConditionId, + ); + if (dealerRewardContition) { + if (dealerRewardContition.evo_reward_summ) { + calculationStore.setValue( + 'dealerRewardSumm', + dealerRewardContition.evo_reward_summ, + ); + calculationStore.setStatus( + 'tbxDealerRewardSumm', + ElementStatus.Default, + ); + } + } + } + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { dealerBrokerRewardCondition } = calculationStore.values; + return dealerBrokerRewardCondition; + }, + effect: dealerBrokerRewardConditionId => { + if (!dealerBrokerRewardConditionId) { + calculationStore.setValue('dealerBrokerRewardSumm', 0); + calculationStore.setStatus( + 'tbxDealerBrokerRewardSumm', + ElementStatus.Disabled, + ); + } else { + const dealerBrokerRewardContition = calculationStore.options.selectDealerBrokerRewardCondition?.find( + x => x.evo_reward_conditionid === dealerBrokerRewardConditionId, + ); + if (dealerBrokerRewardContition) { + if (dealerBrokerRewardContition.evo_reward_summ) { + calculationStore.setValue( + 'dealerBrokerRewardSumm', + dealerBrokerRewardContition.evo_reward_summ, + ); + calculationStore.setStatus( + 'tbxDealerBrokerRewardSumm', + ElementStatus.Default, + ); + } + } + } + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { dealerRewardSumm } = calculationStore.values; + return dealerRewardSumm; + }, + effect: dealerRewardSumm => { + const dealerRewardConditionId = + calculationStore.values.dealerRewardCondition; + if (dealerRewardConditionId) { + const dealerRewardCondition = calculationStore.options.selectDealerRewardCondition?.find( + x => x.evo_reward_conditionid === dealerRewardConditionId, + ); + if (dealerRewardCondition) { + if (dealerRewardCondition.evo_reward_summ) { + if ( + parseFloat(dealerRewardSumm) > + dealerRewardCondition.evo_reward_summ + ) { + calculationStore.setValidation('tbxDealerRewardSumm', false); + openNotification({ + type: 'error', + title: 'Ошибка', + description: + 'Вознаграждение ЮЛ поставщика указано больше условия по агентскому договору! \nЗначение установлено по условию договора.', + })(); + return; + } else if ( + !dealerRewardCondition.evo_reduce_reward && + dealerRewardCondition.evo_reward_summ + ) { + if ( + parseFloat(dealerRewardSumm) < + dealerRewardCondition.evo_reward_summ + ) { + calculationStore.setValidation('tbxDealerRewardSumm', false); + openNotification({ + type: 'error', + title: 'Ошибка', + description: + 'Вознаграждение ЮЛ поставщика указано меньше условия по агентскому договору! \nЗначение установлено по условию договора.', + })(); + return; + } + } + calculationStore.setValidation('tbxDealerRewardSumm', true); + } + } + } + }, + }), + + calculationStore => ({ + expression: () => { + const { dealerBrokerRewardSumm } = calculationStore.values; + return dealerBrokerRewardSumm; + }, + effect: dealerBrokerRewardSumm => { + const dealerBrokerRewardConditionId = + calculationStore.values.dealerBrokerRewardCondition; + if (dealerBrokerRewardConditionId) { + const dealerBrokerRewardCondition = calculationStore.options.selectDealerBrokerRewardCondition?.find( + x => x.evo_reward_conditionid === dealerBrokerRewardConditionId, + ); + if (dealerBrokerRewardCondition) { + if (dealerBrokerRewardCondition.evo_reward_summ) { + if ( + parseFloat(dealerBrokerRewardSumm) > + dealerBrokerRewardCondition.evo_reward_summ + ) { + calculationStore.setValidation( + 'tbxDealerBrokerRewardSumm', + false, + ); + openNotification({ + type: 'error', + title: 'Ошибка', + description: + 'Вознаграждение брокера поставщика указано больше условия по агентскому договору! \nЗначение установлено по условию договора.', + })(); + return; + } + } + if ( + !dealerBrokerRewardCondition.evo_reduce_reward && + dealerBrokerRewardCondition.evo_reward_summ + ) { + if ( + parseFloat(dealerBrokerRewardSumm) < + dealerBrokerRewardCondition.evo_reward_summ + ) { + calculationStore.setValidation( + 'tbxDealerBrokerRewardSumm', + false, + ); + openNotification({ + type: 'error', + title: 'Ошибка', + description: + 'Вознаграждение брокера поставщика указано меньше условия по агентскому договору! \nЗначение установлено по условию договора.', + })(); + return; + } + } + calculationStore.setValidation('tbxDealerBrokerRewardSumm', true); + } + } + }, + }), + + calculationStore => ({ + expression: () => { + const { configuration } = calculationStore.values; + const { selectConfiguration } = calculationStore.options; + return [configuration, selectConfiguration]; + }, + effect: ([configurationId, selectConfigurationOptions]) => { + if (selectConfigurationOptions && selectConfigurationOptions.length) { + if (!configurationId) { + calculationStore.setValidation('selectConfiguration', false); + return; + } + } + calculationStore.setValidation('selectConfiguration', true); + }, + }), + + calculationStore => ({ + expression: () => { + const { model, configuration } = calculationStore.values; + return [model, configuration]; + }, + effect: ([modelId, configurationId]) => { + if (configurationId) { + const configuration = calculationStore.options.selectConfiguration?.find( + x => x.evo_equipmentid === configurationId, + ); + if (configuration) { + if (configuration.evo_impairment_groupid) { + const evo_impairment_groups = calculationStore.getStaticData( + 'evo_impairment_group', + ); + const evo_impairment_group = evo_impairment_groups.find( + x => + x.evo_impairment_groupid === + configuration.evo_impairment_groupid, + ); + calculationStore.setValue( + 'depreciationGroup', + evo_impairment_group ? evo_impairment_group.evo_name : '', + ); + return; + } + } + } + const model = calculationStore.options.selectModel?.find( + x => x.evo_modelid === modelId, + ); + if (model) + if (model.evo_impairment_groupid) { + const evo_impairment_groups = calculationStore.getStaticData( + 'evo_impairment_group', + ); + const evo_impairment_group = evo_impairment_groups.find( + x => x.evo_impairment_groupid === model.evo_impairment_groupid, + ); + calculationStore.setValue( + 'depreciationGroup', + evo_impairment_group ? evo_impairment_group.evo_name : '', + ); + return; + } + calculationStore.setValue('depreciationGroup', null); + }, + }), + + calculationStore => ({ + expression: () => { + const { leaseObjectPrice, supplierDiscountRub } = calculationStore.values; + return [leaseObjectPrice, supplierDiscountRub]; + }, + effect: ([leaseObjectPrice, supplierDiscountRub]) => { + if (supplierDiscountRub === undefined) { + return; + } + + calculationStore.setValue( + 'supplierDiscountPerc', + (supplierDiscountRub / leaseObjectPrice) * 100, + ); + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { + leaseObjectPrice, + supplierDiscountPerc, + } = calculationStore.values; + return [leaseObjectPrice, supplierDiscountPerc]; + }, + effect: ([leaseObjectPrice, supplierDiscountPerc]) => { + if (supplierDiscountPerc === undefined) { + return; + } + + calculationStore.setValue( + 'supplierDiscountRub', + (supplierDiscountPerc * leaseObjectPrice) / 100, + ); + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { firstPaymentRub } = calculationStore.values; + return firstPaymentRub; + }, + effect: firstPaymentRub => { + if (firstPaymentRub === undefined) { + return; + } + const { supplierCurrency, leaseObjectPrice } = calculationStore.values; + calculatePerc(calculationStore)( + supplierCurrency, + leaseObjectPrice, + firstPaymentRub, + 'firstPaymentPerc', + ); + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { + supplierCurrency, + leaseObjectPrice, + firstPaymentPerc, + } = calculationStore.values; + return [supplierCurrency, leaseObjectPrice, firstPaymentPerc]; + }, + effect: ([supplierCurrencyId, leaseObjectPrice, firstPaymentPerc]) => { + if (firstPaymentPerc === undefined) { + return; + } + calculateRub(calculationStore)( + supplierCurrencyId, + leaseObjectPrice, + firstPaymentPerc, + 'firstPaymentRub', + ); + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { + supplierCurrency, + leaseObjectPrice, + comissionPerc, + } = calculationStore.values; + return [supplierCurrency, leaseObjectPrice, comissionPerc]; + }, + effect: ([supplierCurrencyId, leaseObjectPrice, comissionPerc]) => { + if (comissionPerc === undefined) { + return; + } + calculateRub(calculationStore)( + supplierCurrencyId, + leaseObjectPrice, + comissionPerc, + 'comissionRub', + ); + }, + options: { + fireImmediately: true, + }, + }), + calculationStore => ({ + expression: () => { + const { comissionRub } = calculationStore.values; + return comissionRub; + }, + effect: comissionRub => { + if (comissionRub === undefined) { + return; + } + const { supplierCurrency, leaseObjectPrice } = calculationStore.values; + calculatePerc(calculationStore)( + supplierCurrency, + leaseObjectPrice, + comissionRub, + 'comissionPerc', + ); + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { + supplierCurrency, + leaseObjectPrice, + lastPaymentPerc, + lastPaymentRule, + } = calculationStore.values; + return [ + supplierCurrency, + leaseObjectPrice, + lastPaymentPerc, + lastPaymentRule, + ]; + }, + effect: ([ + supplierCurrencyId, + leaseObjectPrice, + lastPaymentPerc, + lastPaymentRule, + ]) => { + if (lastPaymentPerc === undefined || lastPaymentRule !== 100000001) { + return; + } + calculateRub(calculationStore)( + supplierCurrencyId, + leaseObjectPrice, + lastPaymentPerc, + 'lastPaymentRub', + ); + }, + options: { + fireImmediately: true, + }, + }), + calculationStore => ({ + expression: () => { + const { + supplierCurrency, + leaseObjectPrice, + lastPaymentRub, + lastPaymentRule, + } = calculationStore.values; + return [ + supplierCurrency, + leaseObjectPrice, + lastPaymentRub, + lastPaymentRule, + ]; + }, + effect: ([ + supplierCurrencyId, + leaseObjectPrice, + lastPaymentRub, + lastPaymentRule, + ]) => { + if (lastPaymentRub === undefined || lastPaymentRule !== 100000000) { + return; + } + calculatePerc(calculationStore)( + supplierCurrencyId, + leaseObjectPrice, + lastPaymentRub, + 'lastPaymentPerc', + ); + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { + recalcWithRevision, + supplierCurrency, + supplierDiscountRub, + supplierDiscountPerc, + leaseObjectPrice, + } = calculationStore.values; + return [ + recalcWithRevision, + supplierCurrency, + supplierDiscountRub, + supplierDiscountPerc, + leaseObjectPrice, + ]; + }, + effect: ([ + recalcWithRevision, + supplierCurrencyId, + supplierDiscountRub, + supplierDiscountPerc, + leaseObjectPrice, + ]) => { + if (recalcWithRevision === true) { + return; + } + const supplierCurrency = calculationStore + .getOptions('selectSupplierCurrency') + .find(x => x.transactioncurrencyid === supplierCurrencyId); + const evo_currencychange = calculationStore + .getStaticData('evo_currencychange') + ?.find(x => x.evo_ref_transactioncurrency === supplierCurrencyId); + const evo_currencychangeValue = + (evo_currencychange && evo_currencychange.evo_currencychange) || 0; + let price = calcPrice( + supplierCurrency?.isocurrencycode, + leaseObjectPrice, + evo_currencychangeValue, + ); + + let maxPriceChange = + price - supplierDiscountRub < 800000 + ? price - supplierDiscountRub + 50000 + : (price - supplierDiscountRub) * 1.05; + + calculationStore.setValue('maxPriceChange', maxPriceChange); + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { recalcWithRevision } = calculationStore.values; + return recalcWithRevision; + }, + effect: recalcWithRevision => { + if (recalcWithRevision === true) { + calculationStore.setStatus('selectLead', ElementStatus.Disabled); + calculationStore.setStatus('selectOpportunity', ElementStatus.Disabled); + calculationStore.setStatus('selectQuote', ElementStatus.Disabled); + } else { + calculationStore.setStatus('selectLead', ElementStatus.Default); + calculationStore.setStatus('selectOpportunity', ElementStatus.Default); + calculationStore.setStatus('selectQuote', ElementStatus.Default); + } + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { + supplierDiscountRub, + leaseObjectPrice, + supplierCurrency, + } = calculationStore.values; + return [supplierDiscountRub, leaseObjectPrice, supplierCurrency]; + }, + effect: ([supplierDiscountRub, leaseObjectPrice, supplierCurrencyId]) => { + const { recalcWithRevision, maxPriceChange } = calculationStore.values; + if (recalcWithRevision === false) { + return; + } + + const supplierCurrency = calculationStore + .getOptions('selectSupplierCurrency') + .find(x => x.transactioncurrencyid === supplierCurrencyId); + const evo_currencychange = calculationStore + .getStaticData('evo_currencychange') + ?.find(x => x.evo_ref_transactioncurrency === supplierCurrencyId); + const evo_currencychangeValue = + (evo_currencychange && evo_currencychange.evo_currencychange) || 0; + let price = calcPrice( + supplierCurrency?.isocurrencycode, + leaseObjectPrice, + evo_currencychangeValue, + ); + + if (price - supplierDiscountRub > maxPriceChange) { + calculationStore.setValidation('tbxLeaseObjectPrice', false); + openNotification({ + type: 'error', + title: 'Ошибка', + description: + 'Указанная стоимость предмета лизинга больше возмножного изменения стоимости предмета лизинга при пересчете без пересмотра.' + + 'Уменьшите стоимость предмета лизинга', + })(); + } else { + calculationStore.setValidation('tbxLeaseObjectPrice', true); + } + }, + }), + + calculationStore => ({ + expression: () => { + const { leaseObjectUsed } = calculationStore.values; + return leaseObjectUsed; + }, + effect: leaseObjectUsed => { + if (leaseObjectUsed === true) { + calculationStore.setValue('deliveryTime', 100000000); + calculationStore.setStatus('radioDeliveryTime', ElementStatus.Disabled); + } else { + calculationStore.setStatus('radioDeliveryTime', ElementStatus.Default); + } + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { leaseObjectCategory } = calculationStore.values; + return leaseObjectCategory; + }, + effect: leaseObjectCategory => { + if ( + leaseObjectCategory === 100000001 || + leaseObjectCategory === 100000001 + ) { + calculationStore.setValue('withTrailer', null); + calculationStore.setStatus('selectWithTrailer', ElementStatus.Disabled); + } else { + calculationStore.setStatus('selectWithTrailer', ElementStatus.Default); + } + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { quote } = calculationStore.values; + return quote; + }, + effect: quoteId => { + calculationStore.setStatus( + 'cbxRecalcWithRevision', + ElementStatus.Disabled, + ); + if (quoteId) { + const quote = calculationStore.options.selectQuote?.find( + x => x.quoteid === quoteId, + ); + if (quote) { + if (quote.evo_recalc_limit && quote.evo_recalc_limit > 0) { + if (quote.evo_statuscode?.evo_id === '2.3') { + calculationStore.setStatus( + 'cbxRecalcWithRevision', + ElementStatus.Default, + ); + } + } + } + } + }, + }), + + calculationStore => ({ + expression: () => { + const { firstPaymentPerc } = calculationStore.values; + return firstPaymentPerc; + }, + effect: firstPaymentPerc => { + const { recalcWithRevision, quote: quoteId } = calculationStore.values; + if (recalcWithRevision) { + const quote = calculationStore.options.selectQuote?.find( + x => x.quoteid === quoteId, + ); + if (quote && quote.evo_approved_first_payment) { + if (firstPaymentPerc < quote.evo_approved_first_payment) { + calculationStore.setValidation('tbxFirstPaymentPerc', false); + openNotification({ + type: 'error', + title: 'Ошибка', + description: + 'Указанный первый платеж меньше одобренного. При пересчете без пересмотра изменение первого платежа возможно только в большую сторону от одобренного значения', + })(); + return; + } + } + } + calculationStore.setValidation('tbxFirstPaymentPerc', undefined); + }, + }), + + calculationStore => ({ + expression: () => { + const { brand, model, leaseObjectUsed } = calculationStore.values; + return [brand, model, leaseObjectUsed]; + }, + effect: ([brandId, modelId, leaseObjectUsed]) => { + if (leaseObjectUsed === false) { + const model = calculationStore.options.selectModel?.find( + x => x.evo_modelid === modelId, + ); + const brand = calculationStore.options.selectBrand?.find( + x => x.evo_brandid === brandId, + ); + + if ( + model && + ((model.evo_importer_reward_perc && + model.evo_importer_reward_perc > 0) || + (model.evo_importer_reward_rub && + model.evo_importer_reward_rub > 0)) + ) { + calculationStore.setValue( + 'importerRewardPerc', + model.evo_importer_reward_perc || 0, + ); + calculationStore.setValue( + 'importerRewardRub', + model.evo_importer_reward_rub || 0, + ); + return; + } + if ( + brand && + ((brand.evo_importer_reward_perc && + brand.evo_importer_reward_perc > 0) || + (brand.evo_importer_reward_rub && + brand.evo_importer_reward_rub > 0)) + ) { + calculationStore.setValue( + 'importerRewardPerc', + brand.evo_importer_reward_perc || 0, + ); + calculationStore.setValue( + 'importerRewardRub', + brand.evo_importer_reward_rub || 0, + ); + return; + } + } + calculationStore.setValue('importerRewardPerc', 0); + calculationStore.setValue('importerRewardRub', 0); + }, + options: { fireImmediately: true }, + }), + + calculationStore => ({ + expression: () => { + const { graphType, leasingPeriod } = calculationStore.values; + return { graphType, leasingPeriod }; + }, + effect: ({ graphType, leasingPeriod }) => { + if (graphType === 100000003 && leasingPeriod < 14) { + calculationStore.setValue('leasingPeriod', 14); + openNotification({ + type: 'warning', + title: 'Внимание', + description: + 'При сезонном графике срок лизинга должен быть больше 14 месяцев', + })(); + } + }, + }), + + //TODO + // calculationStore => ({ + // expression: () => { + // const { product } = calculationStore.values; + // return product; + // }, + // effect: productId => { + // if (productId) { + // const product = calculationStore.options.selectProduct?.find( + // x => x.evo_id === productId, + // ); + // if ( + // product && + // product.evo_leasingobject_types && + // Array.isArray(product.evo_leasingobject_types) + // ) { + // calculationStore.setOptions( + // 'selectLeaseObjectType', + // product.evo_leasingobject_types, + // ); + // } + // } + // }, + // }), + + calculationStore => ({ + expression: () => { + const { product } = calculationStore.values; + return product; + }, + effect: productId => { + if (productId) { + const product = calculationStore.options.selectProduct?.find( + x => x.evo_id === productId, + ); + if (product) { + const registraction_2020_3 = calculationStore.options.selectRegistration?.find( + x => x.evo_id === '2020_3', + ); + if (registraction_2020_3) { + calculationStore.setValue( + 'registration', + registraction_2020_3.evo_addproduct_typeid, + ); + } + } + } + }, + }), + + calculationStore => ({ + expression: () => { + const { product, leasingPeriod } = calculationStore.values; + return { product_evo_id: product, leasingPeriod }; + }, + effect: ({ product_evo_id, leasingPeriod }) => { + if (product_evo_id && leasingPeriod) { + const product = calculationStore.options.selectProduct?.find( + x => x.evo_id === product_evo_id, + ); + + if (product) { + const tarif = calculationStore.options.selectTarif?.find( + x => + x.evo_baseproductid === product.evo_baseproductid && + x.evo_min_period && + x.evo_min_period <= leasingPeriod && + x.evo_max_period && + x.evo_max_period >= leasingPeriod, + ); + calculationStore.setValue('tarif', tarif?.evo_tarifid); + } + } else { + calculationStore.setValue('tarif', null); + } + }, + }), + + calculationStore => ({ + expression: () => { + const { tarif } = calculationStore.values; + return tarif; + }, + effect: tarif_evo_id => { + const tarif = calculationStore.options.selectTarif?.find( + x => x.evo_tarifid === tarif_evo_id, + ); + + if (tarif) { + calculationStore.setValue('IRR_Perc', tarif.evo_irr); + } + }, + }), +]; + +export default reactionEffects; diff --git a/src/client/stores/CalculationStore/Effects/reactions/requestReactions.ts b/src/client/stores/CalculationStore/Effects/reactions/requestReactions.ts new file mode 100644 index 0000000..ffa0726 --- /dev/null +++ b/src/client/stores/CalculationStore/Effects/reactions/requestReactions.ts @@ -0,0 +1,1162 @@ +import { Process } from './../../../../../core/types/Calculation/Store/process'; +import { currentDate } from 'client/tools/date'; +import { gql } from '@apollo/client'; +import CrmService from 'client/services/CrmService'; +import { IReactionEffect } from 'core/types/Calculation/Store/effect'; +import { ElementStatus } from 'core/types/statuses'; + +export default [ + (calculationStore, calculationProcess) => ({ + expression: () => { + const { values } = calculationStore; + return values.lead; + }, + effect: async leadId => { + if (calculationProcess.process === Process.LoadKp) { + return; + } + if (!leadId) { + calculationStore.setValue('opportunity', null); + calculationStore.setFilter('selectOpportunity', undefined); + calculationStore.setValue('quote', null); + calculationStore.setOptions('selectQuote', []); + calculationStore.setOptions('selectIndAgent', []); + calculationStore.setValue('indAgent', null); + calculationStore.setOptions('selectCalcDoubleAgent', []); + calculationStore.setValue('calcDoubleAgent', null); + calculationStore.setOptions('selectCalcBroker', []); + calculationStore.setValue('calcBroker', null); + calculationStore.setOptions('selectCalcFinDepartment', []); + calculationStore.setValue('calcFinDepartment', null); + return; + } + const lead = calculationStore.options.selectLead?.find( + x => x.leadid === leadId, + ); + + if (lead) { + if (lead.evo_opportunityid) { + calculationStore.setValue('opportunity', lead.evo_opportunityid); + calculationStore.setFilter('selectOpportunity', opportunities => + opportunities.filter( + x => x.opportunityid === lead.evo_opportunityid, + ), + ); + } else { + calculationStore.setFilter('selectOpportunity', () => []); + calculationStore.setValue('opportunity', null); + } + + if (lead.leadid) { + CrmService.crmgqlquery({ + query: gql` + query($leadid: Uuid) { + quote: quotes(evo_leadid: $leadid) { + quoteid + name + evo_quotename + quotenumber + evo_recalc_limit + evo_approved_first_payment + evo_statuscode: evo_statuscodeidData { + evo_id + } + } + } + `, + variables: { + leadid: leadId, + }, + toOptions: ['quote'], + }).then(({ entities }) => { + if ( + entities.quote && + Array.isArray(entities.quote) && + entities.quote.length > 0 + ) { + calculationStore.setOptions('selectQuote', entities.quote); + calculationStore.setValue('quote', null); + } else { + calculationStore.setOptions('selectQuote', []); + calculationStore.setValue('quote', null); + } + }); + } else { + calculationStore.setOptions('selectQuote', []); + calculationStore.setValue('quote', null); + } + + if (lead.evo_agent_accountid) { + CrmService.crmgqlquery({ + query: gql` + query($evo_agent_accountid: Uuid!) { + account(accountid: $evo_agent_accountid) { + accountid + name + } + } + `, + variables: { + evo_agent_accountid: lead.evo_agent_accountid, + }, + toOptions: ['account'], + }).then(({ entities }) => { + if (entities.account) { + if (!Array.isArray(entities.account)) { + calculationStore.setOptions('selectIndAgent', [ + entities.account, + ]); + calculationStore.setValue( + 'indAgent', + entities.account.accountid, + ); + } + } else { + calculationStore.setValue('indAgent', null); + calculationStore.setOptions('selectIndAgent', []); + } + }); + } else { + calculationStore.setValue('indAgent', null); + calculationStore.setOptions('selectIndAgent', []); + } + + if (lead.evo_double_agent_accountid) { + CrmService.crmgqlquery({ + query: gql` + query($evo_double_agent_accountid: Uuid!) { + account(accountid: $evo_double_agent_accountid) { + accountid + name + } + } + `, + variables: { + evo_double_agent_accountid: lead.evo_double_agent_accountid, + }, + toOptions: ['account'], + }).then(({ entities }) => { + if (entities.account) { + if (!Array.isArray(entities.account)) { + calculationStore.setOptions('selectCalcDoubleAgent', [ + entities.account, + ]); + calculationStore.setValue( + 'calcDoubleAgent', + entities.account.accountid, + ); + } + } else { + calculationStore.setValue('calcDoubleAgent', null); + calculationStore.setOptions('selectCalcDoubleAgent', []); + } + }); + } else { + calculationStore.setValue('calcDoubleAgent', null); + calculationStore.setOptions('selectCalcDoubleAgent', []); + } + + if (lead.evo_broker_accountid) { + CrmService.crmgqlquery({ + query: gql` + query($evo_broker_accountid: Uuid!) { + account(accountid: $evo_broker_accountid) { + accountid + name + } + } + `, + variables: { + evo_broker_accountid: lead.evo_broker_accountid, + }, + toOptions: ['account'], + }).then(({ entities }) => { + if (entities.account) { + if (!Array.isArray(entities.account)) { + calculationStore.setOptions('selectCalcBroker', [ + entities.account, + ]); + calculationStore.setValue( + 'calcBroker', + entities.account.accountid, + ); + } + } + }); + } else { + calculationStore.setValue('calcBroker', null); + calculationStore.setOptions('selectCalcBroker', []); + } + + if (lead.evo_fin_department_accountid) { + CrmService.crmgqlquery({ + query: gql` + query($evo_fin_department_accountid: Uuid!) { + account(accountid: $evo_fin_department_accountid) { + accountid + name + } + } + `, + variables: { + evo_fin_department_accountid: lead.evo_fin_department_accountid, + }, + toOptions: ['account'], + }).then(({ entities }) => { + if (entities.account) { + if (!Array.isArray(entities.account)) { + calculationStore.setOptions('selectCalcFinDepartment', [ + entities.account, + ]); + calculationStore.setValue( + 'calcFinDepartment', + entities.account.accountid, + ); + } + } + }); + } else { + calculationStore.setValue('calcFinDepartment', null); + calculationStore.setOptions('selectCalcFinDepartment', []); + } + } + }, + }), + + (calculationStore, calculationProcess) => ({ + expression: () => { + const { lead, opportunity } = calculationStore.values; + return { leadid: lead, opportunityid: opportunity }; + }, + effect: ({ leadid, opportunityid }) => { + if (calculationProcess.process === Process.LoadKp) { + return; + } + if (opportunityid) { + const opportunity = calculationStore.options.selectOpportunity?.find( + x => x.opportunityid === opportunityid, + ); + if (opportunity) { + if (opportunity.evo_client_riskid) { + calculationStore.setValue( + 'clientRisk', + opportunity.evo_client_riskid, + ); + } else { + if (opportunity.parentaccountid) + CrmService.crmgqlquery({ + query: gql` + query($accountid: Uuid!) { + account(accountid: $accountid) { + evo_client_riskid + } + } + `, + variables: { accountid: opportunity.parentaccountid }, + }).then(({ entities }) => { + if ( + entities.account && + !Array.isArray(entities.account) && + entities.account.evo_client_riskid + ) + calculationStore.setValue( + 'clientRisk', + entities.account.evo_client_riskid, + ); + }); + } + } + } else if (leadid && !opportunityid) { + const lead = calculationStore.options.selectLead?.find( + x => x.leadid === leadid, + ); + if (lead) { + if (lead.customerid) { + CrmService.crmgqlquery({ + query: gql` + query($customerid: Uuid!) { + account(accountid: $customerid) { + evo_client_riskid + } + } + `, + variables: { customerid: lead.customerid }, + }).then(({ entities }) => { + if ( + entities.account && + !Array.isArray(entities.account) && + entities.account.evo_client_riskid + ) + calculationStore.setValue( + 'clientRisk', + entities.account.evo_client_riskid, + ); + }); + } + } + } else { + calculationStore.setValue('clientRisk', null); + } + }, + }), + + (calculationStore, calculationProcess) => ({ + expression: () => { + const { account } = calculationStore.values; + return account; + }, + effect: account => { + if (calculationProcess.process === Process.LoadKp) { + return; + } + if (account && account.length > 0) { + calculationStore.setStatus('tbxNewClient', ElementStatus.Disabled); + calculationStore.setValue('newClient', null); + } else { + calculationStore.setStatus('tbxNewClient', ElementStatus.Default); + } + }, + }), + + (calculationStore, calculationProcess) => ({ + expression: () => { + const { indAgent } = calculationStore.values; + return indAgent; + }, + effect: indAgentId => { + if (calculationProcess.process === Process.LoadKp) { + return; + } + if (!indAgentId) { + calculationStore.setValue('indAgentRewardCondition', null); + calculationStore.setStatus( + 'selectIndAgentRewardCondition', + ElementStatus.Disabled, + ); + } else { + calculationStore.setValue('indAgentRewardCondition', null); + calculationStore.setStatus( + 'selectIndAgentRewardCondition', + ElementStatus.Default, + ); + + if (indAgentId) + CrmService.crmgqlquery({ + query: gql` + query( + $statecode: Int + $currentDate: DateTime + $evo_agent_accountid: Uuid + ) { + evo_reward_condition: evo_reward_conditions( + evo_agent_accountid: $evo_agent_accountid + evo_datefrom_param: { lte: $currentDate } + evo_dateto_param: { gte: $currentDate } + statecode: $statecode + ) { + evo_reward_conditionid + evo_name + evo_double_agent_accountid + evo_reward_summ + evo_reduce_reward + } + } + `, + variables: { + evo_agent_accountid: indAgentId, + currentDate, + statecode: 0, + }, + toOptions: ['evo_reward_condition'], + }).then(({ entities: { evo_reward_condition } }) => { + if (evo_reward_condition && Array.isArray(evo_reward_condition)) + calculationStore.setOptions( + 'selectIndAgentRewardCondition', + evo_reward_condition, + ); + }); + } + }, + options: { + fireImmediately: true, + }, + }), + + (calculationStore, calculationProcess) => ({ + expression: () => { + const { calcDoubleAgent } = calculationStore.values; + return calcDoubleAgent; + }, + effect: doubleAgentId => { + if (calculationProcess.process === Process.LoadKp) { + return; + } + if (!doubleAgentId) { + calculationStore.setValue('calcDoubleAgentRewardCondition', null); + calculationStore.setStatus( + 'selectCalcDoubleAgentRewardCondition', + ElementStatus.Disabled, + ); + } else { + calculationStore.setValue('calcDoubleAgentRewardCondition', null); + calculationStore.setStatus( + 'selectCalcDoubleAgentRewardCondition', + ElementStatus.Default, + ); + + if (doubleAgentId) + CrmService.crmgqlquery({ + query: gql` + query( + $statecode: Int + $currentDate: DateTime + $evo_agent_accountid: Uuid + ) { + evo_reward_condition: evo_reward_conditions( + evo_agent_accountid: $evo_agent_accountid + evo_datefrom_param: { lte: $currentDate } + evo_dateto_param: { gte: $currentDate } + statecode: $statecode + ) { + evo_reward_conditionid + evo_name + evo_reward_summ + evo_reduce_reward + } + } + `, + variables: { + evo_agent_accountid: doubleAgentId, + currentDate, + statecode: 0, + }, + toOptions: ['evo_reward_condition'], + }).then(({ entities: { evo_reward_condition } }) => { + if (evo_reward_condition && Array.isArray(evo_reward_condition)) + calculationStore.setOptions( + 'selectCalcDoubleAgentRewardCondition', + evo_reward_condition, + ); + }); + } + }, + options: { + fireImmediately: true, + }, + }), + + (calculationStore, calculationProcess) => ({ + expression: () => { + const { calcFinDepartment } = calculationStore.values; + return calcFinDepartment; + }, + effect: calcFinDepartmentId => { + if (calculationProcess.process === Process.LoadKp) { + return; + } + if (!calcFinDepartmentId) { + calculationStore.setValue('finDepartmentRewardCondtion', null); + calculationStore.setStatus( + 'selectFinDepartmentRewardCondtion', + ElementStatus.Disabled, + ); + } else { + calculationStore.setValue('finDepartmentRewardCondtion', null); + calculationStore.setStatus( + 'selectFinDepartmentRewardCondtion', + ElementStatus.Default, + ); + if (calcFinDepartmentId) + CrmService.crmgqlquery({ + query: gql` + query( + $statecode: Int + $currentDate: DateTime + $evo_agent_accountid: Uuid + ) { + evo_reward_condition: evo_reward_conditions( + evo_agent_accountid: $evo_agent_accountid + evo_datefrom_param: { lte: $currentDate } + evo_dateto_param: { gte: $currentDate } + statecode: $statecode + ) { + evo_reward_conditionid + evo_name + evo_reward_summ + evo_reduce_reward + } + } + `, + variables: { + evo_agent_accountid: calcFinDepartmentId, + currentDate, + statecode: 0, + }, + toOptions: ['evo_reward_condition'], + }).then(({ entities: { evo_reward_condition } }) => { + if (evo_reward_condition && Array.isArray(evo_reward_condition)) + calculationStore.setOptions( + 'selectFinDepartmentRewardCondtion', + evo_reward_condition, + ); + }); + } + }, + options: { + fireImmediately: true, + }, + }), + + (calculationStore, calculationProcess) => ({ + expression: () => { + const { calcBroker } = calculationStore.values; + return calcBroker; + }, + effect: calcBrokerId => { + if (calculationProcess.process === Process.LoadKp) { + return; + } + if (!calcBrokerId) { + calculationStore.setValue('calcBrokerRewardCondition', null); + calculationStore.setStatus( + 'selectCalcBrokerRewardCondition', + ElementStatus.Disabled, + ); + } else { + calculationStore.setValue('calcBrokerRewardCondition', null); + calculationStore.setStatus( + 'selectCalcBrokerRewardCondition', + ElementStatus.Default, + ); + + if (calcBrokerId) + CrmService.crmgqlquery({ + query: gql` + query( + $statecode: Int + $currentDate: DateTime + $evo_agent_accountid: Uuid + ) { + evo_reward_condition: evo_reward_conditions( + evo_agent_accountid: $evo_agent_accountid + evo_datefrom_param: { lte: $currentDate } + evo_dateto_param: { gte: $currentDate } + statecode: $statecode + ) { + evo_reward_conditionid + evo_name + evo_reward_summ + evo_reduce_reward + } + } + `, + variables: { + evo_agent_accountid: calcBrokerId, + currentDate, + statecode: 0, + }, + toOptions: ['evo_reward_condition'], + }).then(({ entities: { evo_reward_condition } }) => { + if (evo_reward_condition && Array.isArray(evo_reward_condition)) + calculationStore.setOptions( + 'selectCalcBrokerRewardCondition', + evo_reward_condition, + ); + }); + } + }, + options: { + fireImmediately: true, + }, + }), + + (calculationStore, calculationProcess) => ({ + expression: () => { + const { dealer } = calculationStore.values; + return dealer; + }, + effect: dealerId => { + if (calculationProcess.process === Process.LoadKp) { + return; + } + if (dealerId) { + const dealer = calculationStore.options.selectDealer?.find( + x => x.accountid === dealerId, + ); + if (dealer) { + CrmService.crmgqlquery({ + query: gql` + query($statecode: Int, $salonaccountid: Uuid!) { + account: salon_providers( + statecode: $statecode + salonaccountid: $salonaccountid + ) { + accountid + name + evo_broker_accountid + } + } + `, + toOptions: ['account'], + variables: { + salonaccountid: dealerId, + statecode: 0, + }, + }).then(({ entities }) => { + if ( + entities.account && + Array.isArray(entities.account) && + entities.account.length > 0 + ) { + calculationStore.setOptions( + 'selectDealerPerson', + entities.account, + ); + calculationStore.setValue( + 'dealerPerson', + entities.account[0].accountid, + ); + calculationStore.setStatus( + 'selectDealerPerson', + ElementStatus.Default, + ); + } + }); + } + } + + calculationStore.setOptions('selectDealerPerson', []); + calculationStore.setValue('dealerPerson', null); + calculationStore.setStatus('selectDealerPerson', ElementStatus.Disabled); + }, + options: { + fireImmediately: true, + }, + }), + + (calculationStore, calculationProcess) => ({ + expression: () => { + const { dealerPerson } = calculationStore.values; + return dealerPerson; + }, + effect: dealerPersonId => { + if (calculationProcess.process === Process.LoadKp) { + return; + } + calculationStore.setOptions('selectDealerBroker', []); + calculationStore.setValue('dealerBroker', null); + calculationStore.setStatus('selectDealerBroker', ElementStatus.Disabled); + if (dealerPersonId) { + const dealerPerson = calculationStore.options.selectDealerPerson?.find( + x => x.accountid === dealerPersonId, + ); + if (dealerPerson && dealerPerson.evo_broker_accountid) { + CrmService.crmgqlquery({ + query: gql` + query($accountid: Uuid!) { + account(accountid: $accountid) { + accountid + name + } + } + `, + toOptions: ['account'], + variables: { + accountid: dealerPerson.evo_broker_accountid, + }, + }).then(({ entities }) => { + if (entities.account && !Array.isArray(entities.account)) { + calculationStore.setOptions('selectDealerBroker', [ + entities.account, + ]); + calculationStore.setValue( + 'dealerBroker', + entities.account.accountid, + ); + calculationStore.setStatus( + 'selectDealerBroker', + ElementStatus.Default, + ); + } + }); + } + } + }, + options: { + fireImmediately: true, + }, + }), + + (calculationStore, calculationProcess) => ({ + expression: () => { + const { dealerBroker } = calculationStore.values; + return dealerBroker; + }, + effect: dealerBrokerId => { + if (calculationProcess.process === Process.LoadKp) { + return; + } + if (dealerBrokerId) { + const dealerBroker = calculationStore.options.selectDealerBroker?.find( + x => x.accountid === dealerBrokerId, + ); + if (dealerBroker) { + calculationStore.setStatus( + 'selectDealerBrokerRewardCondition', + ElementStatus.Default, + ); + + CrmService.crmgqlquery({ + query: gql` + query( + $statecode: Int + $currentDate: DateTime + $evo_agent_accountid: Uuid + ) { + evo_reward_condition: evo_reward_conditions( + evo_agent_accountid: $evo_agent_accountid + evo_datefrom_param: { lte: $currentDate } + evo_dateto_param: { gte: $currentDate } + statecode: $statecode + ) { + evo_reward_conditionid + evo_name + evo_reward_summ + evo_reduce_reward + } + } + `, + variables: { + evo_agent_accountid: dealerBrokerId, + currentDate, + statecode: 0, + }, + toOptions: ['evo_reward_condition'], + }).then(({ entities: { evo_reward_condition } }) => { + if (evo_reward_condition && Array.isArray(evo_reward_condition)) + calculationStore.setOptions( + 'selectDealerBrokerRewardCondition', + evo_reward_condition, + ); + }); + } + } else { + calculationStore.setValue('dealerBrokerRewardCondition', null); + calculationStore.setStatus( + 'selectDealerBrokerRewardCondition', + ElementStatus.Disabled, + ); + } + }, + options: { + fireImmediately: true, + }, + }), + + (calculationStore, calculationProcess) => ({ + expression: () => { + const { dealerBroker, dealerPerson } = calculationStore.values; + return [dealerBroker, dealerPerson]; + }, + effect: ([dealerBrokerId, dealerPersonId]) => { + if (calculationProcess.process === Process.LoadKp) { + return; + } + if (dealerPersonId && !dealerBrokerId) { + calculationStore.setStatus( + 'selectDealerRewardCondition', + ElementStatus.Default, + ); + + CrmService.crmgqlquery({ + query: gql` + query( + $statecode: Int + $currentDate: DateTime + $evo_agent_accountid: Uuid + ) { + evo_reward_condition: evo_reward_conditions( + evo_agent_accountid: $evo_agent_accountid + evo_datefrom_param: { lte: $currentDate } + evo_dateto_param: { gte: $currentDate } + statecode: $statecode + ) { + evo_reward_conditionid + evo_name + evo_reward_summ + evo_reduce_reward + } + } + `, + variables: { + evo_agent_accountid: dealerPersonId, + currentDate, + statecode: 0, + }, + toOptions: ['evo_reward_condition'], + }).then(({ entities: { evo_reward_condition } }) => { + if (evo_reward_condition && Array.isArray(evo_reward_condition)) + calculationStore.setOptions( + 'selectDealerRewardCondition', + evo_reward_condition, + ); + }); + } else { + calculationStore.setValue('dealerRewardCondition', null); + calculationStore.setStatus( + 'selectDealerRewardCondition', + ElementStatus.Disabled, + ); + } + }, + options: { + fireImmediately: true, + }, + }), + + (calculationStore, calculationProcess) => ({ + expression: () => { + const { brand } = calculationStore.values; + return brand; + }, + effect: brandId => { + if (calculationProcess.process === Process.LoadKp) { + return; + } + calculationStore.setStatus('selectModel', ElementStatus.Disabled); + calculationStore.setOptions('selectModel', []); + calculationStore.setValue('model', null); + + if (brandId) { + const brand = calculationStore.options.selectBrand?.find( + x => x.evo_brandid === brandId, + ); + if (brand) { + CrmService.crmgqlquery({ + query: gql` + query($statecode: Int, $evo_brandid: Uuid) { + evo_model: evo_models( + statecode: $statecode + evo_brandid: $evo_brandid + ) { + evo_name + evo_modelid + evo_leasingobject_risk + evo_importer_reward_perc + evo_importer_reward_rub + evo_impairment_groupid + } + } + `, + variables: { + statecode: 0, + evo_brandid: brandId, + }, + toOptions: ['evo_model'], + }).then(({ entities }) => { + if ( + entities.evo_model && + Array.isArray(entities.evo_model) && + entities.evo_model.length > 0 + ) { + calculationStore.setStatus('selectModel', ElementStatus.Default); + calculationStore.setOptions('selectModel', entities.evo_model); + calculationStore.setValue('model', null); + } + }); + } + } + }, + options: { fireImmediately: true }, + }), + + (calculationStore, calculationProcess) => ({ + expression: () => { + const { model } = calculationStore.values; + return model; + }, + effect: modelId => { + if (calculationProcess.process === Process.LoadKp) { + return; + } + calculationStore.setStatus('selectConfiguration', ElementStatus.Disabled); + calculationStore.setOptions('selectConfiguration', []); + calculationStore.setValue('configuration', null); + + if (modelId) { + const model = calculationStore.options.selectModel?.find( + x => x.evo_modelid === modelId, + ); + if (model) { + CrmService.crmgqlquery({ + query: gql` + query($statecode: Int, $evo_modelid: Uuid) { + evo_equipment: evo_equipments( + statecode: $statecode + evo_modelid: $evo_modelid + ) { + evo_equipmentid + evo_name + evo_impairment_groupid + evo_leasingobject_risk + evo_start_production_year + } + } + `, + toOptions: ['evo_equipment'], + variables: { + statecode: 0, + evo_modelid: modelId, + }, + }).then(({ entities }) => { + if ( + entities.evo_equipment && + Array.isArray(entities.evo_equipment) && + entities.evo_equipment.length > 0 + ) { + calculationStore.setStatus( + 'selectConfiguration', + ElementStatus.Default, + ); + calculationStore.setOptions( + 'selectConfiguration', + entities.evo_equipment, + ); + } + }); + } + } + }, + options: { + fireImmediately: true, + }, + }), + + (calculationStore, calculationProcess) => ({ + expression: () => { + const { supplier } = calculationStore.values; + return supplier; + }, + effect: supplierId => { + if (calculationProcess.process === Process.LoadKp) { + return; + } + calculationStore.setOptions('selectFinDepartment', []); + calculationStore.setValue('finDepartment', null); + if (supplierId) { + const supplier = calculationStore.options.selectSupplier?.find( + x => x.accountid === supplierId, + ); + if (supplier && supplier.evo_fin_department_accountid) + CrmService.crmgqlquery({ + query: gql` + query($accountid: Uuid!) { + account(accountid: $accountid) { + accountid + name + } + } + `, + toOptions: ['account'], + variables: { + accountid: supplier.evo_fin_department_accountid, + }, + }).then(({ entities }) => { + if (entities.account && !Array.isArray(entities.account)) { + calculationStore.setOptions('selectFinDepartment', [ + entities.account, + ]); + } + }); + } + }, + }), + (calculationStore, calculationProcess) => ({ + expression: () => { + const { supplier, channel } = calculationStore.values; + return { supplierId: supplier, channelId: channel }; + }, + //TODO: add $ownerid: Uuid + effect: ({ supplierId, channelId }) => { + if (calculationProcess.process === Process.LoadKp) { + return; + } + calculationStore.setOptions('selectAgent', []); + calculationStore.setValue('agent', null); + if (channelId === 100000002) { + CrmService.crmgqlquery({ + query: gql` + query( + $statecode: Int + $evo_account_type: [Int!] + $evo_legal_form: Int + ) { + account: accounts( + statecode: $statecode + evo_account_type: $evo_account_type + evo_legal_form: $evo_legal_form + ) { + accountid + name + } + } + `, + toOptions: ['account'], + variables: { + statecode: 0, + evo_account_type: [100000005], + evo_legal_form: 100000004, + }, + }).then(({ entities }) => { + if (entities.account && Array.isArray(entities.account)) { + calculationStore.setOptions('selectAgent', entities.account); + } + }); + } else { + if (supplierId) { + CrmService.crmgqlquery({ + query: gql` + query($statecode: Int, $salonaccountid: Uuid!) { + account: salon_agents( + statecode: $statecode + salonaccountid: $salonaccountid + ) { + accountid + name + } + } + `, + toOptions: ['account'], + variables: { + statecode: 0, + salonaccountid: supplierId, + }, + }).then(({ entities }) => { + if (entities.account && Array.isArray(entities.account)) { + calculationStore.setOptions('selectAgent', entities.account); + } + }); + } + } + }, + }), + + (calculationStore, calculationProcess) => ({ + expression: () => { + const { GPSBrand } = calculationStore.values; + return GPSBrand; + }, + effect: GPSBrandId => { + if (calculationProcess.process === Process.LoadKp) { + return; + } + calculationStore.setStatus('selectGPSModel', ElementStatus.Disabled); + calculationStore.setOptions('selectGPSModel', []); + calculationStore.setValue('GPSModel', null); + + if (GPSBrandId) { + const gpsBrand = calculationStore.options.selectGPSBrand?.find( + x => x.evo_gps_brandid === GPSBrandId, + ); + if (gpsBrand) { + CrmService.crmgqlquery({ + query: gql` + query($statecode: Int, $evo_gps_brandid: Uuid) { + evo_gps_model: evo_gps_models( + statecode: $statecode + evo_gps_brandid: $evo_gps_brandid + ) { + evo_name + evo_gps_modelid + } + } + `, + variables: { + statecode: 0, + evo_gps_brandid: GPSBrandId, + }, + toOptions: ['evo_gps_model'], + }).then(({ entities }) => { + if ( + entities.evo_gps_model && + Array.isArray(entities.evo_gps_model) && + entities.evo_gps_model.length > 0 + ) { + calculationStore.setStatus( + 'selectGPSModel', + ElementStatus.Default, + ); + calculationStore.setOptions( + 'selectGPSModel', + entities.evo_gps_model, + ); + calculationStore.setValue('GPSModel', null); + } + }); + } + } + }, + options: { fireImmediately: true }, + }), + + (calculationStore, calculationProcess) => ({ + expression: () => { + const { regionRegistration } = calculationStore.values; + return regionRegistration; + }, + effect: regionRegistrationId => { + if (calculationProcess.process === Process.LoadKp) { + return; + } + calculationStore.setStatus( + 'selectTownRegistration', + ElementStatus.Disabled, + ); + calculationStore.setOptions('selectTownRegistration', []); + calculationStore.setValue('townRegistration', null); + + if (regionRegistrationId) { + const regionRegistration = calculationStore.options.selectRegionRegistration?.find( + x => x.evo_regionid === regionRegistrationId, + ); + if (regionRegistration) { + CrmService.crmgqlquery({ + query: gql` + query($statecode: Int, $evo_regionid: Uuid) { + evo_town: evo_towns( + statecode: $statecode + evo_regionid: $evo_regionid + ) { + evo_name + evo_townid + } + } + `, + variables: { + statecode: 0, + evo_regionid: regionRegistrationId, + }, + toOptions: ['evo_town'], + }).then(({ entities }) => { + if ( + entities.evo_town && + Array.isArray(entities.evo_town) && + entities.evo_town.length > 0 + ) { + calculationStore.setStatus( + 'selectTownRegistration', + ElementStatus.Default, + ); + calculationStore.setOptions( + 'selectTownRegistration', + entities.evo_town, + ); + calculationStore.setValue('townRegistration', null); + } + }); + } + } + }, + options: { fireImmediately: true }, + }), +] as IReactionEffect[]; diff --git a/src/client/stores/CalculationStore/Effects/reactions/tablesReactions.ts b/src/client/stores/CalculationStore/Effects/reactions/tablesReactions.ts new file mode 100644 index 0000000..fa4d408 --- /dev/null +++ b/src/client/stores/CalculationStore/Effects/reactions/tablesReactions.ts @@ -0,0 +1,716 @@ +import { openNotification } from 'client/Elements/Notification'; +import { shiftRight, shift } from 'core/tools/array'; +import { IReactionEffect } from 'core/types/Calculation/Store/effect'; +import { Process } from 'core/types/Calculation/Store/process'; +import { ITableCell, TableProps } from 'core/types/Calculation/Store/tables'; +import { ElementStatus } from 'core/types/statuses'; +import { toJS } from 'mobx'; + +export default [ + (calculationStore, calculationProcess) => ({ + expression: () => { + const { rows: tableRows } = calculationStore.tables.tableInsurance; + const kaskoRowIndex = tableRows.findIndex( + x => x.policyType?.value === 'КАСКО', + ); + + const kaskoValues = calculationStore.getTableRowValues( + 'tableInsurance', + kaskoRowIndex, + 'value', + ); + + return { + ...kaskoValues, + }; + }, + effect: ({ insuranceCompany, insTerm, insured }) => { + if (calculationProcess.process === Process.LoadKp) { + return; + } + if (insTerm) { + const { rows: tableRows } = calculationStore.tables.tableInsurance; + const dgoRowIndex = tableRows.findIndex( + x => x.policyType?.value === 'ДГО', + ); + if (dgoRowIndex && dgoRowIndex >= 0) + calculationStore.setTableRows( + 'tableInsurance', + dgoRowIndex, + )([ + { + insuranceCompany: { + value: insuranceCompany, + }, + insTerm: { + value: insTerm, + }, + insured: { + value: insured, + }, + }, + { + insuranceCompany: { + value: insuranceCompany, + }, + insTerm: { + value: insTerm, + }, + insured: { + value: insured, + }, + }, + ]); + } + }, + }), + + (calculationStore, calculationProcess) => ({ + expression: () => { + const { rows: tableRows } = calculationStore.tables.tableInsurance; + const kaskoRowIndex = tableRows.findIndex( + x => x.policyType?.value === 'КАСКО', + ); + const kaskoRow = tableRows[kaskoRowIndex]; + const { leasingPeriod } = calculationStore.values; + + return { + insTerm: kaskoRow.insTerm?.value, + leasingPeriod, + }; + }, + effect: ({ insTerm, leasingPeriod }) => { + if (calculationProcess.process === Process.LoadKp) { + return; + } + const { rows: tableRows } = calculationStore.tables.tableInsurance; + const kaskoRowIndex = tableRows.findIndex( + x => x.policyType?.value === 'КАСКО', + ); + if (insTerm === 100000001 && leasingPeriod > 15 && kaskoRowIndex) { + if (kaskoRowIndex >= 0) + calculationStore.setTableRow( + 'tableInsurance', + kaskoRowIndex, + )({ + insured: { value: 100000001, status: ElementStatus.Disabled }, + }); + } else { + if (kaskoRowIndex >= 0) + calculationStore.setTableRow( + 'tableInsurance', + kaskoRowIndex, + )({ + insured: { status: ElementStatus.Default }, + }); + } + }, + }), + + (calculationStore, calculationProcess) => ({ + expression: () => { + const { rows: tableRows } = calculationStore.tables.tableInsurance; + const kaskoRowIndex = tableRows.findIndex( + x => x.policyType?.value === 'КАСКО', + ); + const kaskoRow = tableRows[kaskoRowIndex]; + const { leasingPeriod } = calculationStore.values; + + return { + insTerm: kaskoRow.insTerm?.value, + leasingPeriod, + }; + }, + effect: ({ insTerm, leasingPeriod }) => { + if (calculationProcess.process === Process.LoadKp) { + return; + } + const { rows: tableRows } = calculationStore.tables.tableInsurance; + const kaskoRowIndex = tableRows.findIndex( + x => x.policyType?.value === 'КАСКО', + ); + if (insTerm === 100000001 && leasingPeriod > 15 && kaskoRowIndex) { + if (kaskoRowIndex >= 0) + calculationStore.setTableRow( + 'tableInsurance', + kaskoRowIndex, + )({ + insured: { value: 100000001, status: ElementStatus.Disabled }, + }); + } else { + if (kaskoRowIndex >= 0) + calculationStore.setTableRow( + 'tableInsurance', + kaskoRowIndex, + )({ + insured: { status: ElementStatus.Default }, + }); + } + }, + }), + + (calculationStore, calculationProcess) => ({ + expression: () => { + const { leasingPeriod } = calculationStore.values; + return leasingPeriod; + }, + effect: leasingPeriod => { + if (calculationProcess.process === Process.LoadKp) { + return; + } + const { rows: tableRows } = calculationStore.tables.tableInsurance; + const kaskoRowIndex = tableRows.findIndex( + x => x.policyType?.value === 'КАСКО', + ); + const osagoRowIndex = tableRows.findIndex( + x => x.policyType?.value === 'ОСАГО', + ); + + if (leasingPeriod) { + if (leasingPeriod < 12) { + calculationStore.setTableRow( + 'tableInsurance', + kaskoRowIndex, + )({ + insured: { value: 100000000, status: ElementStatus.Disabled }, + }); + calculationStore.setTableRow( + 'tableInsurance', + osagoRowIndex, + )({ + insured: { value: 100000000, status: ElementStatus.Disabled }, + }); + } else { + calculationStore.setTableRow( + 'tableInsurance', + kaskoRowIndex, + )({ + insured: { value: 100000000, status: ElementStatus.Default }, + }); + calculationStore.setTableRow( + 'tableInsurance', + osagoRowIndex, + )({ + insured: { status: ElementStatus.Default }, + }); + } + + if (leasingPeriod === 12) { + calculationStore.setTableRow( + 'tableInsurance', + kaskoRowIndex, + )({ + insTerm: { value: 100000000, status: ElementStatus.Disabled }, + }); + return; + } else if (leasingPeriod > 12 && leasingPeriod < 16) { + calculationStore.setTableRow( + 'tableInsurance', + kaskoRowIndex, + )({ + insTerm: { value: 100000001, status: ElementStatus.Disabled }, + }); + return; + } else { + calculationStore.setTableRow( + 'tableInsurance', + kaskoRowIndex, + )({ + insured: { status: ElementStatus.Default }, + insTerm: { status: ElementStatus.Default }, + }); + } + } + }, + options: { + fireImmediately: true, + }, + }), + + (calculationStore, calculationProcess) => ({ + expression: () => { + const { rows: tableRows } = calculationStore.tables.tableInsurance; + const kaskoRowIndex = tableRows.findIndex( + x => x.policyType?.value === 'КАСКО', + ); + const dgoRowIndex = tableRows.findIndex( + x => x.policyType?.value === 'ДГО', + ); + const nsRowIndex = tableRows.findIndex(x => x.policyType?.value === 'НС'); + + const kaskoValues = calculationStore.getTableRowValues( + 'tableInsurance', + kaskoRowIndex, + 'value', + ); + const dgoValues = calculationStore.getTableRowValues( + 'tableInsurance', + dgoRowIndex, + 'value', + ); + const nsValues = calculationStore.getTableRowValues( + 'tableInsurance', + nsRowIndex, + 'value', + ); + + return { + kaskoRow: kaskoValues, + dgoRow: dgoValues, + nsRow: nsValues, + }; + }, + effect: ({ kaskoRow, dgoRow, nsRow }) => { + if (calculationProcess.process === Process.LoadKp) { + return; + } + const { rows: tableRows } = calculationStore.tables.tableInsurance; + const kaskoRowIndex = tableRows.findIndex( + x => x.policyType?.value === 'КАСКО', + ); + + if ( + kaskoRow && + kaskoRow.insCost === 0 && + ((dgoRow && dgoRow.insCost > 0) || (nsRow && nsRow.insCost > 0)) + ) { + calculationStore.setTableRow( + 'tableInsurance', + 1, + )({ + insCost: { + validation: false, + }, + }); + + openNotification({ + type: 'error', + title: 'Ошибка во время расчета графика', + description: 'Укажите стоимость КАСКО', + })(); + } else { + calculationStore.setTableRow( + 'tableInsurance', + kaskoRowIndex, + )({ + insCost: { + validation: true, + }, + }); + } + }, + }), + + (calculationStore, calculationProcess) => ({ + expression: () => { + const { rows: tableRows } = calculationStore.tables.tableInsurance; + const kaskoRowIndex = tableRows.findIndex( + x => x.policyType?.value === 'КАСКО', + ); + const kaskoValues = calculationStore.getTableRowValues( + 'tableInsurance', + kaskoRowIndex, + 'value', + ); + return { + kaskoRow: kaskoValues, + }; + }, + effect: ({ kaskoRow }) => { + if (calculationProcess.process === Process.LoadKp) { + return; + } + const { rows: tableRows } = calculationStore.tables.tableInsurance; + const kaskoRowIndex = tableRows.findIndex( + x => x.policyType?.value === 'КАСКО', + ); + + if ( + kaskoRow && + kaskoRow.insured === 100000001 && + kaskoRow.insCost === 0 + ) { + calculationStore.setTableRow( + 'tableInsurance', + kaskoRowIndex, + )({ + insCost: { + validation: false, + }, + }); + openNotification({ + type: 'error', + title: 'Ошибка во время расчета графика', + description: 'Укажите стоимость КАСКО, включаемую в график', + })(); + } else { + calculationStore.setTableRow( + 'tableInsurance', + kaskoRowIndex, + )({ + insCost: { + validation: true, + }, + }); + } + }, + }), + + (calculationStore, calculationProcess) => ({ + expression: () => { + const { rows: tableRows } = calculationStore.tables.tableInsurance; + const osagoRowIndex = tableRows.findIndex( + x => x.policyType?.value === 'ОСАГО', + ); + const osagoValues = calculationStore.getTableRowValues( + 'tableInsurance', + osagoRowIndex, + 'value', + ); + return { + osagoRow: osagoValues, + }; + }, + effect: ({ osagoRow }) => { + if (calculationProcess.process === Process.LoadKp) { + return; + } + const { rows: tableRows } = calculationStore.tables.tableInsurance; + const osagoRowIndex = tableRows.findIndex( + x => x.policyType?.value === 'ОСАГО', + ); + + if ( + osagoRow && + osagoRow.insured === 100000001 && + osagoRow.insCost === 0 + ) { + calculationStore.setTableRow( + 'tableInsurance', + osagoRowIndex, + )({ + insCost: { + validation: false, + }, + }); + openNotification({ + type: 'error', + title: 'Ошибка во время расчета графика', + description: 'Укажите стоимость ОСАГО, включаемую в график', + })(); + } else { + calculationStore.setTableRow( + 'tableInsurance', + osagoRowIndex, + )({ + insCost: { + validation: true, + }, + }); + } + }, + }), + + (calculationStore, calculationProcess) => ({ + expression: () => { + const { + leasingPeriod, + graphType, + parmentsDecreasePercent, + seasonType, + highSeasonStart: highSeasonStartId, + firstPaymentPerc, + lastPaymentPerc, + } = calculationStore.values; + + const highSeasonStart = calculationStore.options.selectHighSeasonStart?.find( + x => x.value === highSeasonStartId, + ); + + return { + leasingPeriod, + graphType, + parmentsDecreasePercent, + seasonType, + highSeasonStart: parseInt(highSeasonStart?.name || '2'), + firstPaymentPerc, + lastPaymentPerc, + }; + }, + effect: async (nextParams, prevParams) => { + if (calculationProcess.process === Process.LoadKp) { + return; + } + const { + leasingPeriod, + graphType, + parmentsDecreasePercent, + seasonType, + highSeasonStart, + firstPaymentPerc, + lastPaymentPerc, + } = nextParams; + + const prevValues = toJS(calculationStore.tables.tablePayments.rows).map( + x => x.paymentRelation?.value, + ); + let payments: TableProps[] = [ + { + paymentRelation: { + value: firstPaymentPerc, + status: ElementStatus.Disabled, + }, + }, + ]; + + calculationStore.cleanTable('tablePayments'); + + switch (graphType) { + case 100000000: { + const middleRows = Array.from({ length: leasingPeriod - 2 }, () => ({ + paymentRelation: { + value: 100, + status: ElementStatus.Disabled, + }, + })); + payments = [...payments, ...middleRows]; + + break; + } + + case 100000001: { + const middleRows = Array.from({ length: leasingPeriod - 3 }, () => ({ + paymentRelation: { + value: 100, + status: ElementStatus.Default, + }, + })); + payments = [ + ...payments, + { + paymentRelation: { + value: 100, + status: ElementStatus.Disabled, + }, + }, + ...middleRows, + ]; + + break; + } + + case 100000002: { + const middleRows = Array.from( + { length: leasingPeriod - 3 }, + (v, i) => ({ + paymentRelation: { + value: 100, + status: ElementStatus.Disabled, + }, + }), + ); + for (let i in middleRows) { + const currRow = middleRows[parseInt(i)]; + const prevRow = middleRows[parseInt(i) - 1]; + currRow.paymentRelation.value = parseFloat( + ( + ((prevRow ? prevRow.paymentRelation.value : 100) * + parmentsDecreasePercent) / + 100 + ).toFixed(2), + ); + } + payments = [ + ...payments, + { + paymentRelation: { + value: 100, + status: ElementStatus.Disabled, + }, + }, + ...middleRows, + ]; + + break; + } + + case 100000003: { + let HIGH = 100, + MIDDLE = 75, + LOW = 50; + + if ( + prevParams.graphType === nextParams.graphType + // && nextParams.graphType === 100000003 + ) { + /** + * FIND PREV HIGH, MIDDLE, LOW + */ + const { + leasingPeriod: prevLeasingPeriod, + seasonType: prevSeasonType, + highSeasonStart: prevHighSeasonStart, + } = prevParams; + + const prevPeriodsNumber = + prevLeasingPeriod <= 14 ? prevLeasingPeriod - 2 : 12; + const prevShiftNumber = prevHighSeasonStart - 2; + + let middleRows = prevValues.slice(1, prevPeriodsNumber + 1); + if (middleRows.length < 12) { + middleRows = [ + ...middleRows, + ...Array.from({ length: 12 - middleRows.length }, v => 0), + ]; + } + if (prevShiftNumber > 0) + middleRows = shiftRight(middleRows, prevShiftNumber); + + switch (prevSeasonType) { + // 6/6 + case 100000000: { + HIGH = middleRows[0]; + LOW = middleRows[6]; + break; + } + // 8/4 + case 100000001: { + HIGH = middleRows[0]; + LOW = middleRows[8]; + + break; + } + // 4/4/4 + case 100000002: { + HIGH = middleRows[0]; + MIDDLE = middleRows[4]; + LOW = middleRows[8]; + break; + } + } + /** */ + } + + /** + * GENERATE PERIODS + */ + + const { + leasingPeriod: nextLeasingPeriod, + seasonType: nextSeasonType, + highSeasonStart: nextHighSeasonStart, + } = nextParams; + + const nextPeriodsNumber = + nextLeasingPeriod <= 14 ? nextLeasingPeriod - 2 : 12; + const nextShiftNumber = nextHighSeasonStart - 2; + + let nextPeriods: number[] = []; + + switch (nextSeasonType) { + // 6/6 + case 100000000: { + nextPeriods = Array.from({ length: 12 }, (v, i) => + i < 6 ? HIGH : LOW, + ); + break; + } + // 8/4 + case 100000001: { + nextPeriods = Array.from({ length: 12 }, (v, i) => + i < 8 ? HIGH : LOW, + ); + break; + } + // 4/4/4 + case 100000002: { + nextPeriods = Array.from( + { length: 12 }, + (v, i) => (i < 4 && HIGH) || (i < 8 && MIDDLE) || LOW, + ); + break; + } + } + if (nextShiftNumber > 0) { + nextPeriods = shift(nextPeriods, nextShiftNumber); + } + nextPeriods.length = nextPeriodsNumber; + + const middleRows = Array.from( + { length: nextLeasingPeriod - 2 }, + (v, i) => { + return { + paymentRelation: { + value: + nextPeriods[ + i - nextPeriodsNumber * Math.floor(i / nextPeriodsNumber) + ], + status: + i < nextPeriodsNumber + ? ElementStatus.Default + : ElementStatus.Disabled, + }, + }; + }, + ); + + payments = [...payments, ...middleRows]; + + break; + } + + case 100000004: { + const middleRows = Array.from({ length: leasingPeriod - 5 }, () => ({ + paymentRelation: { + value: 100, + status: ElementStatus.Disabled, + }, + })); + + payments = [ + ...payments, + { + paymentRelation: { + value: 25, + status: ElementStatus.Default, + }, + }, + { + paymentRelation: { + value: 50, + status: ElementStatus.Default, + }, + }, + { + paymentRelation: { + value: 75, + status: ElementStatus.Default, + }, + }, + ...middleRows, + ]; + break; + } + + default: { + break; + } + } + + payments = [ + ...payments, + { + paymentRelation: { + value: lastPaymentPerc, + status: ElementStatus.Disabled, + }, + }, + ]; + + calculationStore.setTableRows('tablePayments', 0)(payments); + }, + options: { + fireImmediately: true, + }, + }), +] as IReactionEffect[]; diff --git a/src/client/stores/CalculationStore/config/initialTables/tableInsurance.ts b/src/client/stores/CalculationStore/config/initialTables/tableInsurance.ts index 3cdc565..34ea5c3 100644 --- a/src/client/stores/CalculationStore/config/initialTables/tableInsurance.ts +++ b/src/client/stores/CalculationStore/config/initialTables/tableInsurance.ts @@ -1,4 +1,5 @@ import { openNotification } from 'client/Elements/Notification'; +import { Process } from 'core/types/Calculation/Store/process'; import { ITable } from 'core/types/Calculation/Store/tables'; import { ElementStatus } from 'core/types/statuses'; @@ -128,7 +129,10 @@ const tableInsurance: ITable = { ], }, callbacks: { - insCost: (calculationStore, tableName, rowIndex) => { + insCost: (calculationStore, calculationProcess, tableName, rowIndex) => { + if (calculationProcess.process === Process.LoadKp) { + return; + } if ( calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value && calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value > @@ -221,7 +225,10 @@ const tableInsurance: ITable = { } }, - insured: (calculationStore, tableName, rowIndex) => { + insured: (calculationStore, calculationProcess, tableName, rowIndex) => { + if (calculationProcess.process === Process.LoadKp) { + return; + } if ( calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value && calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value > @@ -253,7 +260,10 @@ const tableInsurance: ITable = { } }, - insTerm: (calculationStore, tableName, rowIndex) => { + insTerm: (calculationStore, calculationProcess, tableName, rowIndex) => { + if (calculationProcess.process === Process.LoadKp) { + return; + } if ( calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value && calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value > @@ -284,7 +294,15 @@ const tableInsurance: ITable = { }); } }, - insuranceCompany: (calculationStore, tableName, rowIndex) => { + insuranceCompany: ( + calculationStore, + calculationProcess, + tableName, + rowIndex, + ) => { + if (calculationProcess.process === Process.LoadKp) { + return; + } if ( calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value && calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value > diff --git a/src/client/stores/CalculationStore/config/initialTables/tablePayments.ts b/src/client/stores/CalculationStore/config/initialTables/tablePayments.ts index 9d09562..31cc0e3 100644 --- a/src/client/stores/CalculationStore/config/initialTables/tablePayments.ts +++ b/src/client/stores/CalculationStore/config/initialTables/tablePayments.ts @@ -1,3 +1,4 @@ +import { Process } from 'core/types/Calculation/Store/process'; import { ITable } from 'core/types/Calculation/Store/tables'; import { toJS } from 'mobx'; @@ -8,7 +9,16 @@ const tablePayments: ITable = { // }, ], callbacks: { - paymentRelation: (calculationStore, tableName, rowIndex, value) => { + paymentRelation: ( + calculationStore, + calculationProcess, + tableName, + rowIndex, + value, + ) => { + if (calculationProcess.process === Process.LoadKp) { + return; + } const { graphType } = calculationStore.values; if (graphType === 100000001) { if ( diff --git a/src/client/stores/CalculationStore/index.ts b/src/client/stores/CalculationStore/index.ts index 7b1b437..a6bb98f 100644 --- a/src/client/stores/CalculationStore/index.ts +++ b/src/client/stores/CalculationStore/index.ts @@ -3,8 +3,8 @@ import { modalData, } from 'client/stores/CalculationStore/Data/modal'; import actionsEffects from 'client/stores/CalculationStore/Effects/action'; -// import assignProperties from 'client/tools/assignProps'; import { ICalculationStore } from 'core/types/Calculation/Store'; +import { Process } from 'core/types/Calculation/Store/process'; import { isEqual } from 'lodash'; import { autorun, makeAutoObservable, reaction, when } from 'mobx'; import { @@ -16,14 +16,19 @@ import { import { staticData, staticDataAction } from './Data/staticEntities'; import autorunEffects from './Effects/autorun'; import computedEffects from './Effects/computed'; -import reactionEffects from './Effects/reaction'; +import reactions from './Effects/reactions'; import whenEffects from './Effects/when'; +export const calculationProcess = makeAutoObservable({ + process: Process.Default, + setProcess(process) { + this.process = process; + }, +}); + const CalculationStore: ICalculationStore = makeAutoObservable( Object.assign( {}, - // assignProperties( - // {}, staticData, staticDataAction, valuesData, @@ -39,8 +44,11 @@ const CalculationStore: ICalculationStore = makeAutoObservable( autorunEffects.map(autorunEffect => autorun(autorunEffect(CalculationStore))); -reactionEffects.map(reactionEffectBuilder => { - const reactionEffect = reactionEffectBuilder(CalculationStore); +reactions.map(reactionEffectBuilder => { + const reactionEffect = reactionEffectBuilder( + CalculationStore, + calculationProcess, + ); return reaction(reactionEffect.expression, reactionEffect.effect, { ...reactionEffect.options, equals: (nextParams, prevParams) => { diff --git a/src/client/stores/index.js b/src/client/stores/index.js index 1667473..3446553 100644 --- a/src/client/stores/index.js +++ b/src/client/stores/index.js @@ -1,8 +1,9 @@ -import CalculationStore from './CalculationStore'; +import CalculationStore, { calculationProcess } from './CalculationStore'; class RootStore { constructor() { this.calculationStore = CalculationStore; + this.calculationProcess = calculationProcess; } } diff --git a/src/core/types/Calculation/Requests.ts b/src/core/types/Calculation/Requests.ts index 2f5e3bd..c55c1df 100644 --- a/src/core/types/Calculation/Requests.ts +++ b/src/core/types/Calculation/Requests.ts @@ -1,16 +1,19 @@ -import { TCRMEntity } from 'core/types/Entities/crmEntities'; import { ColumnsNames, IColumn, PostValues } from 'core/types/Calculation/Core'; import { PreparedPayments, - PreparedValues, + PreparedValues } from 'core/types/Calculation/Prepare'; +import { + ElementsNames +} from 'core/types/Calculation/Store/elements'; import { TableProps } from 'core/types/Calculation/Store/tables'; import { TValues } from 'core/types/Calculation/Store/values'; +import { TCRMEntity } from 'core/types/Entities/crmEntities'; import { CRMEntityNames } from 'core/types/Entities/crmEntityNames'; export interface IQueryToCRMGQL { query: any; - toOptions?: CRMEntityNames[]; + toOptions?: CRMEntityNames[] | ElementsNames[]; variables: { [prop in keyof TCRMEntity]: any; } & { [prop: string]: any }; diff --git a/src/core/types/Calculation/Store/effect.ts b/src/core/types/Calculation/Store/effect.ts index cbc89f7..f7b34cc 100644 --- a/src/core/types/Calculation/Store/effect.ts +++ b/src/core/types/Calculation/Store/effect.ts @@ -1,3 +1,4 @@ +import { calculationProcess } from 'client/stores/CalculationStore'; import { IReactionOptions, IReactionPublic, Lambda } from 'mobx'; import { ICalculationStore } from './'; @@ -7,12 +8,17 @@ export type TAction = { [actionName in ActionsNames]?: () => void; }; +export type TCalculationProcess = typeof calculationProcess; + export interface IAutorunEffect { (CalculationStore: ICalculationStore): () => void; } export interface IReactionEffect { - (CalculationStore: ICalculationStore): { + ( + CalculationStore: ICalculationStore, + calculationProcess: TCalculationProcess, + ): { expression: (r: IReactionPublic) => any; effect: (arg: any, prev: any, r: IReactionPublic) => void; options?: IReactionOptions; diff --git a/src/core/types/Calculation/Store/index.ts b/src/core/types/Calculation/Store/index.ts index 25aab03..18ed682 100644 --- a/src/core/types/Calculation/Store/index.ts +++ b/src/core/types/Calculation/Store/index.ts @@ -42,6 +42,7 @@ interface ICalculationValues { sourceValueName: ValuesNames | ResultValuesNames, newValue: TValue, ) => void; + setValues: (values: TValues) => void; statuses: TElements; getStatus: (elementName: ElementsNames) => ElementStatus; diff --git a/src/core/types/Calculation/Store/process.ts b/src/core/types/Calculation/Store/process.ts new file mode 100644 index 0000000..04b4559 --- /dev/null +++ b/src/core/types/Calculation/Store/process.ts @@ -0,0 +1,4 @@ +export enum Process { + Default, + LoadKp, +} diff --git a/src/core/types/Calculation/Store/tables.ts b/src/core/types/Calculation/Store/tables.ts index c0ec529..93526c4 100644 --- a/src/core/types/Calculation/Store/tables.ts +++ b/src/core/types/Calculation/Store/tables.ts @@ -3,6 +3,7 @@ import { ElementStatus } from '../../statuses'; import { ICalculationStore } from './'; import { TElementFilter } from './filters'; import { IBaseOption } from './options'; +import { TCalculationProcess } from './effect'; export type TableNames = 'tableInsurance' | 'tablePayments' | 'tableResults'; export type TableValuesNames = @@ -19,6 +20,7 @@ export type TableValuesNames = export type TCellCallback = ( calculationStore: ICalculationStore, + calculationProcess: TCalculationProcess, tableName: TableNames, rowIndex: number, value: any, diff --git a/src/core/types/Entities/crmEntities.ts b/src/core/types/Entities/crmEntities.ts index 999a99c..35e228b 100644 --- a/src/core/types/Entities/crmEntities.ts +++ b/src/core/types/Entities/crmEntities.ts @@ -40,6 +40,7 @@ export interface IOpportunity { statecode?: number; accountidData?: IAccount; parentaccountid?: string; + evo_addproduct_types?: IEvoAddproductType[]; } export interface IQuote { @@ -54,6 +55,21 @@ export interface IQuote { evo_statuscode?: IEvoStatusCode; evo_quotename?: string; offerprintform?: string; + evo_object_count?: number; + evo_osago_accountid?: string; + evo_kasko_accountid?: string; + evo_osago_payer?: number; + evo_kasko_payer?: number; + evo_osago_price?: number; + evo_dgo_price?: number; + evo_ns_price?: number; + evo_insurance_period?: number; + evo_graphs?: IEvoGraph[]; +} + +interface IEvoGraph { + createdon?: Date; + evo_planpayment?: IEvoPlanPayment[]; } export interface ITransactionCurrency { @@ -126,6 +142,9 @@ export interface IEvoEquipment { evo_leasingobject_risk?: number; evo_start_production_year?: number; statecode?: number; + evo_supplier_accountid?: string; + evo_dealer_person_accountid?: string; + evo_dealer_broker_accountid?: string; } export interface IEvoRewardCondition { @@ -307,6 +326,7 @@ export interface IEvoPlanPayment { evo_cost_price_telematics_withoutnds?: number; evo_cost_equipment_withoutnds?: number; evo_addproduct_typeid?: string; + evo_payment_ratio?: number; } export interface ISystemUser {