diff --git a/src/client/common/constants.js b/src/client/common/constants.js index 71b4737..8f99dc4 100644 --- a/src/client/common/constants.js +++ b/src/client/common/constants.js @@ -1 +1,4 @@ +import { DateTime } from 'luxon'; + export const API_HOSTNAME = window.location.hostname; +export const currentDate = DateTime.local().toUTC().toJSDate().toDateString(); diff --git a/src/client/stores/CalculationStore/Effects/reaction.ts b/src/client/stores/CalculationStore/Effects/reaction.ts index de4238c..96292c1 100644 --- a/src/client/stores/CalculationStore/Effects/reaction.ts +++ b/src/client/stores/CalculationStore/Effects/reaction.ts @@ -8,6 +8,7 @@ import { toJS } from 'mobx'; import { calcPrice, calculatePerc, calculateRub } from './lib/tools'; import { query } from 'express'; import { gql } from '@apollo/client'; +import { currentDate } from 'client/common/constants'; const reactionEffects: IReactionEffect[] = [ calculationStore => ({ @@ -21,6 +22,14 @@ const reactionEffects: IReactionEffect[] = [ 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( @@ -230,7 +239,6 @@ const reactionEffects: IReactionEffect[] = [ const opportunity = calculationStore.options.selectOpportunity?.find( x => x.opportunityid === opportunityId, ); - console.log('opportunity', opportunity); if (opportunity) { calculationStore.setValue('lead', opportunity.evo_leadid); @@ -238,268 +246,306 @@ const reactionEffects: IReactionEffect[] = [ }, }), - // 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 { - // CalculationService.getEntityOptions({ - // query: { - // entityName: 'account', - // where: { accountid: opportunity.evo_accountid }, - // }, - // }).then(({ entityOptions: accounts }) => { - // if ( - // accounts && - // accounts.length > 0 && - // accounts[0].evo_client_riskid - // ) - // calculationStore.setValue( - // 'clientRisk', - // accounts[0].evo_client_riskid || null, - // ); - // }); - // } - // } - // } else if (leadid && !opportunityid) { - // const lead = calculationStore.options.selectLead?.find( - // x => x.leadid === leadid, - // ); - // if (lead) { - // if (lead.account) { - // CalculationService.getEntityOptions({ - // query: { - // entityName: 'account', - // where: { accountid: lead.account }, - // }, - // }).then(({ entityOptions: accounts }) => { - // if (accounts.length > 0) - // calculationStore.setValue( - // 'clientRisk', - // accounts[0].evo_client_riskid || null, - // ); - // }); - // } - // } - // } else { - // calculationStore.setValue('clientRisk', null); - // } - // }, - // }), + 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 { + //TODO: проверить + if (opportunity.parentaccountid) + CalculationService.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) { + CalculationService.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', Status.Disabled); - // } else { - // calculationStore.setStatus('selectDoubleAgent', Status.Default); - // } - // }, - // }), + calculationStore => ({ + expression: () => { + const { agent } = calculationStore.values; + return agent; + }, + effect: agentid => { + if (!agentid) { + calculationStore.setStatus('selectDoubleAgent', Status.Disabled); + } else { + calculationStore.setStatus('selectDoubleAgent', Status.Default); + } + }, + }), - // calculationStore => ({ - // expression: () => { - // const { channel } = calculationStore.values; - // return channel; - // }, - // effect: channel => { - // switch (channel) { - // case 100000000: - // calculationStore.setStatus('selectSupplier', Status.Default); - // calculationStore.setStatus('selectAgent', Status.Default); + calculationStore => ({ + expression: () => { + const { channel } = calculationStore.values; + return channel; + }, + effect: channel => { + switch (channel) { + case 100000000: + calculationStore.setStatus('selectSupplier', Status.Default); + calculationStore.setStatus('selectAgent', Status.Default); - // calculationStore.setStatus('selectFinDepartment', Status.Disabled); - // calculationStore.setValue('finDepartment', undefined); + calculationStore.setStatus('selectFinDepartment', Status.Disabled); + calculationStore.setValue('finDepartment', undefined); - // calculationStore.setStatus('selectBroker', Status.Disabled); - // calculationStore.setValue('broker', undefined); - // break; + calculationStore.setStatus('selectBroker', Status.Disabled); + calculationStore.setValue('broker', undefined); + break; - // case 100000001: - // calculationStore.setStatus('selectSupplier', Status.Default); - // calculationStore.setStatus('selectAgent', Status.Default); + case 100000001: + calculationStore.setStatus('selectSupplier', Status.Default); + calculationStore.setStatus('selectAgent', Status.Default); - // calculationStore.setStatus('selectFinDepartment', Status.Default); + calculationStore.setStatus('selectFinDepartment', Status.Default); - // calculationStore.setStatus('selectBroker', Status.Disabled); - // calculationStore.setValue('broker', undefined); - // break; - // case 100000002: - // calculationStore.setStatus('selectSupplier', Status.Disabled); - // calculationStore.setValue('supplier', undefined); + calculationStore.setStatus('selectBroker', Status.Disabled); + calculationStore.setValue('broker', undefined); + break; + case 100000002: + calculationStore.setStatus('selectSupplier', Status.Disabled); + calculationStore.setValue('supplier', undefined); - // calculationStore.setStatus('selectAgent', Status.Default); + calculationStore.setStatus('selectAgent', Status.Default); - // calculationStore.setStatus('selectFinDepartment', Status.Disabled); - // calculationStore.setValue('finDepartment', undefined); + calculationStore.setStatus('selectFinDepartment', Status.Disabled); + calculationStore.setValue('finDepartment', undefined); - // calculationStore.setStatus('selectBroker', Status.Disabled); - // calculationStore.setValue('broker', undefined); - // break; - // case 100000003: - // calculationStore.setStatus('selectSupplier', Status.Disabled); - // calculationStore.setValue('supplier', undefined); + calculationStore.setStatus('selectBroker', Status.Disabled); + calculationStore.setValue('broker', undefined); + break; + case 100000003: + calculationStore.setStatus('selectSupplier', Status.Disabled); + calculationStore.setValue('supplier', undefined); - // calculationStore.setStatus('selectAgent', Status.Default); - // calculationStore.setValue('agent', undefined); + calculationStore.setStatus('selectAgent', Status.Default); + calculationStore.setValue('agent', undefined); - // calculationStore.setStatus('selectFinDepartment', Status.Disabled); - // calculationStore.setValue('finDepartment', undefined); + calculationStore.setStatus('selectFinDepartment', Status.Disabled); + calculationStore.setValue('finDepartment', undefined); - // calculationStore.setStatus('selectBroker', Status.Default); - // break; - // case 100000004: - // default: - // calculationStore.setStatus('selectSupplier', Status.Disabled); - // calculationStore.setValue('supplier', undefined); + calculationStore.setStatus('selectBroker', Status.Default); + break; + case 100000004: + default: + calculationStore.setStatus('selectSupplier', Status.Disabled); + calculationStore.setValue('supplier', undefined); - // calculationStore.setStatus('selectAgent', Status.Disabled); - // calculationStore.setValue('agent', undefined); + calculationStore.setStatus('selectAgent', Status.Disabled); + calculationStore.setValue('agent', undefined); - // calculationStore.setStatus('selectFinDepartment', Status.Disabled); - // calculationStore.setValue('finDepartment', undefined); + calculationStore.setStatus('selectFinDepartment', Status.Disabled); + calculationStore.setValue('finDepartment', undefined); - // calculationStore.setStatus('selectBroker', Status.Disabled); - // calculationStore.setValue('broker', undefined); - // break; - // } - // }, - // }), + calculationStore.setStatus('selectBroker', Status.Disabled); + calculationStore.setValue('broker', undefined); + break; + } + }, + }), - // 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', Status.Disabled); - // calculationStore.setStatus('selectContactClient', Status.Disabled); - // } else { - // calculationStore.setStatus('selectAccount', Status.Default); - // calculationStore.setStatus('selectContactClient', Status.Default); - // } - // }, - // }), + 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', Status.Disabled); + calculationStore.setStatus('selectContactClient', Status.Disabled); + } else { + calculationStore.setStatus('selectAccount', Status.Default); + calculationStore.setStatus('selectContactClient', Status.Default); + } + }, + }), - // calculationStore => ({ - // expression: () => { - // const { account } = calculationStore.values; - // return account; - // }, - // effect: account => { - // if (account && account.length > 0) { - // calculationStore.setStatus('tbxNewClient', Status.Disabled); - // calculationStore.setValue('newClient', null); - // } else { - // calculationStore.setStatus('tbxNewClient', Status.Default); - // } - // }, - // }), + calculationStore => ({ + expression: () => { + const { account } = calculationStore.values; + return account; + }, + effect: account => { + if (account && account.length > 0) { + calculationStore.setStatus('tbxNewClient', Status.Disabled); + calculationStore.setValue('newClient', null); + } else { + calculationStore.setStatus('tbxNewClient', Status.Default); + } + }, + }), - // calculationStore => ({ - // expression: () => { - // const { indAgent } = calculationStore.values; - // return indAgent; - // }, - // effect: indAgentId => { - // if (!indAgentId) { - // calculationStore.setValue('indAgentRewardCondition', null); - // calculationStore.setStatus( - // 'selectIndAgentRewardCondition', - // Status.Disabled, - // ); - // } else { - // calculationStore.setValue('indAgentRewardCondition', null); - // calculationStore.setStatus( - // 'selectIndAgentRewardCondition', - // Status.Default, - // ); - // CalculationService.getEntityOptions({ - // query: { - // entityName: 'evo_reward_condition', - // where: { - // statecode: 0, - // // TODO < > текущей даты - // // evo_datefrom: new Date(), - // // evo_dateto: new Date(), - // evo_agent_accountid: indAgentId, - // }, - // }, - // }) - // .then(({ entityOptions: reward_conditions }) => { - // calculationStore.setOptions( - // 'selectIndAgentRewardCondition', - // reward_conditions, - // ); - // }) - // .catch(err => { - // throw err; - // }); - // } - // }, - // }), + calculationStore => ({ + expression: () => { + const { indAgent } = calculationStore.values; + return indAgent; + }, + effect: indAgentId => { + if (!indAgentId) { + calculationStore.setValue('indAgentRewardCondition', null); + calculationStore.setStatus( + 'selectIndAgentRewardCondition', + Status.Disabled, + ); + } else { + calculationStore.setValue('indAgentRewardCondition', null); + calculationStore.setStatus( + 'selectIndAgentRewardCondition', + Status.Default, + ); - // calculationStore => ({ - // expression: () => { - // const { calcDoubleAgent } = calculationStore.values; - // return calcDoubleAgent; - // }, - // effect: doubleAgentId => { - // if (!doubleAgentId) { - // calculationStore.setValue('calcDoubleAgentRewardCondition', null); - // calculationStore.setStatus( - // 'selectCalcDoubleAgentRewardCondition', - // Status.Disabled, - // ); - // } else { - // calculationStore.setValue('calcDoubleAgentRewardCondition', null); - // calculationStore.setStatus( - // 'selectCalcDoubleAgentRewardCondition', - // Status.Default, - // ); - // CalculationService.getEntityOptions({ - // query: { - // entityName: 'evo_reward_condition', - // where: { - // statecode: 0, - // // TODO < > текущей даты - // // evo_datefrom: new Date(), - // // evo_dateto: new Date(), - // evo_agent_accountid: doubleAgentId, - // }, - // }, - // }) - // .then(({ entityOptions: reward_conditions }) => { - // calculationStore.setOptions( - // 'selectCalcDoubleAgentRewardCondition', - // reward_conditions, - // ); - // }) - // .catch(err => { - // throw err; - // }); - // } - // }, - // }), + if (indAgentId) + CalculationService.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 + } + } + `, + 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, + ); + }); + } + }, + }), + + calculationStore => ({ + expression: () => { + const { calcDoubleAgent } = calculationStore.values; + return calcDoubleAgent; + }, + effect: doubleAgentId => { + if (!doubleAgentId) { + calculationStore.setValue('calcDoubleAgentRewardCondition', null); + calculationStore.setStatus( + 'selectCalcDoubleAgentRewardCondition', + Status.Disabled, + ); + } else { + calculationStore.setValue('calcDoubleAgentRewardCondition', null); + calculationStore.setStatus( + 'selectCalcDoubleAgentRewardCondition', + Status.Default, + ); + + if (doubleAgentId) + CalculationService.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 + } + } + `, + 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, + ); + }); + } + }, + }), // calculationStore => ({ // expression: () => { diff --git a/src/core/Data/initialOptions.ts b/src/core/Data/initialOptions.ts index cc33a39..9bc9091 100644 --- a/src/core/Data/initialOptions.ts +++ b/src/core/Data/initialOptions.ts @@ -1,13 +1,12 @@ import { TEntityQuery } from 'core/types/Entities/query'; -import { DateTime } from 'luxon'; - -const currentDate = DateTime.local().toUTC().toJSDate().toDateString(); +import { currentDate } from 'client/common/constants'; const initialOptions: TEntityQuery[] = [ { alias: 'selectLead', entityName: 'lead', fields: [ + 'customerid', 'leadid', 'fullname', 'evo_opportunityid', @@ -29,6 +28,7 @@ const initialOptions: TEntityQuery[] = [ 'accountid', 'evo_leadid', 'evo_client_riskid', + // 'parentaccountid', ], where: { statecode: 0 }, many: true, diff --git a/src/core/types/Entities/crmEntities.ts b/src/core/types/Entities/crmEntities.ts index b9a8f62..7e7e746 100644 --- a/src/core/types/Entities/crmEntities.ts +++ b/src/core/types/Entities/crmEntities.ts @@ -13,6 +13,7 @@ export interface IAccount { evo_broker_accountid?: string; evo_client_riskid?: string; evo_type_ins_policy?: number[]; + customerid?: string; } export interface ILead { @@ -37,6 +38,7 @@ export interface IOpportunity { ownerid?: string; statecode?: number; accountidData?: IAccount; + parentaccountid?: string; } export interface IQuote {