diff --git a/graphql/crm.types.ts b/graphql/crm.types.ts index 1bcf2bd..c3cf351 100644 --- a/graphql/crm.types.ts +++ b/graphql/crm.types.ts @@ -295,6 +295,13 @@ export type GetRewardWithoutOtherAgentQueryVariables = Exact<{ export type GetRewardWithoutOtherAgentQuery = { __typename?: 'Query', evo_reward_condition?: { __typename?: 'evo_reward_condition', evo_agency_agreementidData?: { __typename?: 'evo_agency_agreement', evo_reward_without_other_agent?: boolean | null } | null } | null }; +export type GetRewardConditionQueryVariables = Exact<{ + conditionId: Scalars['Uuid']; +}>; + + +export type GetRewardConditionQuery = { __typename?: 'Query', evo_reward_condition?: { __typename?: 'evo_reward_condition', evo_reward_summ?: any | null, evo_reduce_reward?: boolean | null, evo_min_reward_summ?: any | null } | null }; + export type GetDealerPersonQueryVariables = Exact<{ dealerId: Scalars['Uuid']; }>; diff --git a/process/fingap/reactions/validation.ts b/process/fingap/reactions/validation.ts index 1a2778d..90966da 100644 --- a/process/fingap/reactions/validation.ts +++ b/process/fingap/reactions/validation.ts @@ -2,6 +2,7 @@ import type { ApolloClient } from '@apollo/client'; import type { QueryClient } from '@tanstack/react-query'; import { reaction } from 'mobx'; import type RootStore from 'stores/root'; +import type { RemoveError } from 'stores/validation'; export default function validationReactions( store: RootStore, @@ -11,7 +12,7 @@ export default function validationReactions( const { $tables } = store; const errorText = 'Неверно заполнены платежи'; - let removeError: () => void; + let removeError: RemoveError | undefined; reaction( () => { diff --git a/process/payments/reactions.ts b/process/payments/reactions.ts index c48f993..ccdb043 100644 --- a/process/payments/reactions.ts +++ b/process/payments/reactions.ts @@ -8,6 +8,7 @@ import { shift } from 'radash'; import type { CalculationOptions } from 'stores/calculation/options/types'; import type RootStore from 'stores/root'; import type { Row } from 'stores/tables/payments/types'; +import type { RemoveError } from 'stores/validation'; import { difference } from 'tools/array'; import { makeDisposable } from 'tools/mobx'; import * as seasonsConstants from './lib/seasons-constants'; @@ -557,7 +558,7 @@ export default function paymentsReactions( /** * Валидация */ - let removeError: () => void; + let removeError: RemoveError | undefined; reaction( () => { diff --git a/process/supplier-agent/reactions/agents.ts b/process/supplier-agent/reactions/agents.ts index 1dbcddd..0ca74bc 100644 --- a/process/supplier-agent/reactions/agents.ts +++ b/process/supplier-agent/reactions/agents.ts @@ -481,4 +481,55 @@ export function commonReactions(store: RootStore, apolloClient: ApolloClient) {} +export const QUERY_GET_REWARD_CONDITION = gql` + query GetRewardCondition($conditionId: Uuid!) { + evo_reward_condition(evo_reward_conditionid: $conditionId) { + evo_reward_summ + evo_reduce_reward + evo_min_reward_summ + } + } +`; + +export function validationReactions(store: RootStore, apolloClient: ApolloClient) { + const { $calculation } = store; + + reaction( + () => $calculation.element('tbxDealerRewardSumm').getValue(), + async (dealerRewardSumm) => { + const { + data: { evo_reward_condition }, + } = await apolloClient.query< + CRMTypes.GetRewardConditionQuery, + CRMTypes.GetRewardConditionQueryVariables + >({ + query: QUERY_GET_REWARD_CONDITION, + variables: { + conditionId: $calculation.element('selectDealerRewardCondition').getValue(), + }, + }); + + $calculation.element('tbxDealerRewardSumm').validate({ + invalid: () => + evo_reward_condition?.evo_reward_summ && + dealerRewardSumm > evo_reward_condition.evo_reward_summ, + message: 'Вознаграждение ЮЛ поставщика указано больше условия по агентскому договору!', + }); + + $calculation.element('tbxDealerRewardSumm').validate({ + invalid: () => + !evo_reward_condition?.evo_reduce_reward && + evo_reward_condition?.evo_reward_summ && + dealerRewardSumm < evo_reward_condition.evo_reward_summ, + message: 'Вознаграждение ЮЛ поставщика указано меньше условия по агентскому договору!', + }); + + $calculation.element('tbxDealerRewardSumm').validate({ + invalid: () => + evo_reward_condition?.evo_min_reward_summ && + dealerRewardSumm < evo_reward_condition?.evo_min_reward_summ, + message: 'Вознаграждение ЮЛ поставщика указано меньше условия по агентскому договору!', + }); + } + ); +} diff --git a/stores/calculation/index.ts b/stores/calculation/index.ts index 12eea84..01c8360 100644 --- a/stores/calculation/index.ts +++ b/stores/calculation/index.ts @@ -10,6 +10,11 @@ import OptionsStore from './options'; import StatusStore from './statuses'; import ValuesStore from './values'; +type ValidateParams = { + invalid: () => boolean; + message: string; +}; + export default class CalculationStore { $values: ValuesStore; $status: StatusStore; @@ -86,15 +91,18 @@ export default class CalculationStore { return this.element(elementName); }, - addError: (message: string) => { - if (!this.$validation[elementName]) { - this.$validation[elementName] = new Validation({ - err_key: elementName, - err_title: titles[elementName], - }); + validate: ({ invalid, message }: ValidateParams) => { + if (invalid()) { + if (!this.$validation[elementName]) { + this.$validation[elementName] = new Validation({ + err_key: elementName, + err_title: titles[elementName], + }); + } + this.$validation[elementName]?.addError(message); + } else { + this.$validation[elementName]?.removeError(message); } - - return this.$validation[elementName]?.addError(message); }, cleanErrors: () => { diff --git a/stores/validation.ts b/stores/validation.ts index 3b504da..07ee4b3 100644 --- a/stores/validation.ts +++ b/stores/validation.ts @@ -6,6 +6,8 @@ type Params = { err_title: string; }; +export type RemoveError = () => void; + export default class Validation { params: Params; messages: Set; @@ -24,24 +26,25 @@ export default class Validation { return [...this.messages]; } + removeError = (message: string) => { + this.messages.delete(message); + if (this.messages.size === 0) notification.close(this.params.err_key); + }; + addError = (message: string) => { this.messages.add(message); - const removeError = () => { - this.messages.delete(message); - notification.close(this.params.err_key); - }; - notification.error({ key: this.params.err_key, message: this.params.err_title, description: message, }); - return removeError; + return () => this.removeError(message); }; clearErrors = () => { + notification.close(this.params.err_key); this.messages.clear(); }; }