process/supplier-agent: add agents validation
This commit is contained in:
parent
1827b9ba54
commit
fef39e4a46
@ -1,12 +1,14 @@
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
import type { ApolloClient } from '@apollo/client';
|
||||
import { gql } from '@apollo/client';
|
||||
import titles from 'Components/Calculation/config/elements-titles';
|
||||
import type * as Values from 'Components/Calculation/config/map/values';
|
||||
import dayjs from 'dayjs';
|
||||
import utc from 'dayjs/plugin/utc';
|
||||
import type * as CRMTypes from 'graphql/crm.types';
|
||||
import { reaction } from 'mobx';
|
||||
import type RootStore from 'stores/root';
|
||||
import type { RemoveError } from 'stores/validation/types';
|
||||
import { normalizeOptions } from 'tools/entity';
|
||||
import { makeDisposable } from 'tools/mobx';
|
||||
|
||||
@ -123,3 +125,91 @@ export function fillAgentRewardSummReaction(
|
||||
() => $process.has('LoadKP')
|
||||
);
|
||||
}
|
||||
|
||||
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 validateAgentRewardSumm(
|
||||
store: RootStore,
|
||||
apolloClient: ApolloClient<object>,
|
||||
agentParams: {
|
||||
rewardConditionField: Values.Elements;
|
||||
rewardSummField: Values.Elements;
|
||||
}
|
||||
) {
|
||||
const { $calculation } = store;
|
||||
const { rewardConditionField, rewardSummField } = agentParams;
|
||||
|
||||
const rewardSummTitle = titles[rewardSummField];
|
||||
|
||||
const errors: Array<RemoveError> = [];
|
||||
|
||||
reaction(
|
||||
() => $calculation.element(rewardSummField).getValue() as number,
|
||||
async (rewardSumm) => {
|
||||
const conditionId = $calculation.element(rewardConditionField).getValue();
|
||||
if (!conditionId) {
|
||||
errors.forEach((removeError) => removeError());
|
||||
errors.length = 0;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const {
|
||||
data: { evo_reward_condition },
|
||||
} = await apolloClient.query<
|
||||
CRMTypes.GetRewardConditionQuery,
|
||||
CRMTypes.GetRewardConditionQueryVariables
|
||||
>({
|
||||
query: QUERY_GET_REWARD_CONDITION,
|
||||
variables: {
|
||||
conditionId,
|
||||
},
|
||||
});
|
||||
|
||||
$calculation
|
||||
.element(rewardSummField)
|
||||
.validate({
|
||||
invalid:
|
||||
evo_reward_condition?.evo_reward_summ &&
|
||||
rewardSumm > evo_reward_condition.evo_reward_summ,
|
||||
message: `${rewardSummTitle} указан больше условия по агентскому договору!`,
|
||||
})
|
||||
.err((removeError) => {
|
||||
errors.push(removeError);
|
||||
});
|
||||
|
||||
$calculation
|
||||
.element(rewardSummField)
|
||||
.validate({
|
||||
invalid:
|
||||
!evo_reward_condition?.evo_reduce_reward &&
|
||||
evo_reward_condition?.evo_reward_summ &&
|
||||
rewardSumm < evo_reward_condition.evo_reward_summ,
|
||||
message: `${rewardSummTitle} указан меньше условия по агентскому договору!`,
|
||||
})
|
||||
.err((removeError) => {
|
||||
errors.push(removeError);
|
||||
});
|
||||
|
||||
$calculation
|
||||
.element(rewardSummField)
|
||||
.validate({
|
||||
invalid:
|
||||
evo_reward_condition?.evo_min_reward_summ &&
|
||||
rewardSumm < evo_reward_condition?.evo_min_reward_summ,
|
||||
message: `${rewardSummTitle} указан меньше условия по агентскому договору!`,
|
||||
})
|
||||
.err((removeError) => {
|
||||
errors.push(removeError);
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
/* eslint-disable implicit-arrow-linebreak */
|
||||
import type { ApolloClient } from '@apollo/client';
|
||||
import { gql } from '@apollo/client';
|
||||
import dayjs from 'dayjs';
|
||||
@ -492,44 +491,33 @@ export const QUERY_GET_REWARD_CONDITION = gql`
|
||||
`;
|
||||
|
||||
export function validationReactions(store: RootStore, apolloClient: ApolloClient<object>) {
|
||||
const { $calculation } = store;
|
||||
createReactions.validateAgentRewardSumm(store, apolloClient, {
|
||||
rewardConditionField: 'selectDealerRewardCondition',
|
||||
rewardSummField: 'tbxDealerRewardSumm',
|
||||
});
|
||||
|
||||
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(),
|
||||
},
|
||||
});
|
||||
createReactions.validateAgentRewardSumm(store, apolloClient, {
|
||||
rewardConditionField: 'selectDealerBrokerRewardCondition',
|
||||
rewardSummField: 'tbxDealerBrokerRewardSumm',
|
||||
});
|
||||
|
||||
$calculation.element('tbxDealerRewardSumm').validate({
|
||||
invalid:
|
||||
evo_reward_condition?.evo_reward_summ &&
|
||||
dealerRewardSumm > evo_reward_condition.evo_reward_summ,
|
||||
message: 'Вознаграждение ЮЛ поставщика указано больше условия по агентскому договору!',
|
||||
});
|
||||
createReactions.validateAgentRewardSumm(store, apolloClient, {
|
||||
rewardConditionField: 'selectIndAgentRewardCondition',
|
||||
rewardSummField: 'tbxIndAgentRewardSumm',
|
||||
});
|
||||
|
||||
$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: 'Вознаграждение ЮЛ поставщика указано меньше условия по агентскому договору!',
|
||||
});
|
||||
createReactions.validateAgentRewardSumm(store, apolloClient, {
|
||||
rewardConditionField: 'selectCalcDoubleAgentRewardCondition',
|
||||
rewardSummField: 'tbxCalcDoubleAgentRewardSumm',
|
||||
});
|
||||
|
||||
$calculation.element('tbxDealerRewardSumm').validate({
|
||||
invalid:
|
||||
evo_reward_condition?.evo_min_reward_summ &&
|
||||
dealerRewardSumm < evo_reward_condition?.evo_min_reward_summ,
|
||||
message: 'Вознаграждение ЮЛ поставщика указано меньше условия по агентскому договору!',
|
||||
});
|
||||
}
|
||||
);
|
||||
createReactions.validateAgentRewardSumm(store, apolloClient, {
|
||||
rewardConditionField: 'selectCalcBrokerRewardCondition',
|
||||
rewardSummField: 'tbxCalcBrokerRewardSum',
|
||||
});
|
||||
|
||||
createReactions.validateAgentRewardSumm(store, apolloClient, {
|
||||
rewardConditionField: 'selectFinDepartmentRewardCondtion',
|
||||
rewardSummField: 'tbxFinDepartmentRewardSumm',
|
||||
});
|
||||
}
|
||||
|
||||
@ -6,7 +6,7 @@ import type { BaseOption } from 'Elements/types';
|
||||
import { observable } from 'mobx';
|
||||
import type RootStore from 'stores/root';
|
||||
import Validation from 'stores/validation';
|
||||
import type { ValidationParams } from '../validation/types';
|
||||
import type { RemoveError, ValidationParams } from '../validation/types';
|
||||
import OptionsStore from './options';
|
||||
import StatusStore from './statuses';
|
||||
import ValuesStore from './values';
|
||||
@ -24,6 +24,13 @@ export default class CalculationStore {
|
||||
this.$validation = observable.object({});
|
||||
}
|
||||
|
||||
private createElementValidation = <E extends Values.Elements>(elementName: E) => {
|
||||
this.$validation[elementName] = new Validation({
|
||||
err_key: elementName,
|
||||
err_title: titles[elementName],
|
||||
});
|
||||
};
|
||||
|
||||
element = <E extends Values.Elements>(elementName: E) => ({
|
||||
reset: () => {
|
||||
const valueName = getValueName(elementName);
|
||||
@ -88,17 +95,20 @@ export default class CalculationStore {
|
||||
},
|
||||
|
||||
validate: ({ invalid, message }: ValidationParams) => {
|
||||
if (!this.$validation[elementName]) this.createElementValidation(elementName);
|
||||
|
||||
let removeError: RemoveError | undefined;
|
||||
if (invalid) {
|
||||
if (!this.$validation[elementName]) {
|
||||
this.$validation[elementName] = new Validation({
|
||||
err_key: elementName,
|
||||
err_title: titles[elementName],
|
||||
});
|
||||
}
|
||||
this.$validation[elementName]?.addError(message);
|
||||
removeError = this.$validation[elementName]?.addError(message);
|
||||
} else {
|
||||
this.$validation[elementName]?.removeError(message);
|
||||
}
|
||||
|
||||
return {
|
||||
err(callback: (removeError: RemoveError) => void) {
|
||||
if (removeError) callback(removeError);
|
||||
},
|
||||
};
|
||||
},
|
||||
|
||||
cleanErrors: () => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user