Если в 2х и более полях RewardSumm значения больше 0 И в полях RewardCondtion есть хотя бы одна запись,

* у которой evo_reward_condition.evo_agency_agreementid. Выплата без других  агентов
       * (evo_reward_without_other_agent) = True,
       * то выводить ошибку "Согласно условиям АД выплата допустима только этому агенту" на то RewardCondtion,
       * у которого evo_reward_condition.evo_agency_agreementid равно True
       * иначе все ок
This commit is contained in:
vchikalkin 2023-03-29 17:36:51 +03:00
parent 518d766015
commit d3b8e9f099

View File

@ -109,6 +109,36 @@ function helper({ apolloClient, ctx }: ValidationContext & { ctx: RefinementCtx
}
}
},
async validateRewardWithoutOtherAgent({
conditionId,
fieldName,
}: {
conditionId: string | null;
fieldName: string;
}) {
if (conditionId) {
const {
data: { evo_reward_condition },
} = await apolloClient.query<
CRMTypes.GetRewardConditionQuery,
CRMTypes.GetRewardConditionQueryVariables
>({
query: CRMTypes.GetRewardConditionDocument,
variables: {
conditionId,
},
});
if (evo_reward_condition?.evo_agency_agreementidData?.evo_reward_without_other_agent) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Согласно условиям АД выплата допустима только этому агенту',
path: [fieldName],
});
}
}
},
};
}
@ -152,13 +182,15 @@ export function createValidationSchema(context: ValidationContext) {
.merge(SumsSchema)
.superRefine(async (values, ctx) => {
const {
calcBroker,
calcBrokerRewardCondition,
calcBrokerRewardSum,
calcDoubleAgent,
calcDoubleAgentRewardCondition,
calcDoubleAgentRewardSumm,
calcFinDepartment,
dealer: dealerId,
dealerBroker,
calcFinDepartment,
dealerBrokerRewardCondition,
dealerBrokerRewardSumm,
dealerPerson,
@ -169,9 +201,8 @@ export function createValidationSchema(context: ValidationContext) {
indAgent,
indAgentRewardCondition,
indAgentRewardSumm,
calcDoubleAgent,
calcBroker,
} = values;
/**
* Добавить валидацию на кнопку Рассчитать:
* если tbxDealerRewardSumm > 0 и
@ -339,7 +370,7 @@ export function createValidationSchema(context: ValidationContext) {
});
}
const { validateRewardSum } = helper({ ...context, ctx });
const { validateRewardSum, validateRewardWithoutOtherAgent } = helper({ ...context, ctx });
await validateRewardSum({
agentid: dealerPerson,
@ -382,5 +413,54 @@ export function createValidationSchema(context: ValidationContext) {
sum: finDepartmentRewardSumm,
sumFieldName: 'tbxFinDepartmentRewardSumm',
});
/**
* Если в 2х и более полях RewardSumm значения больше 0 И в полях RewardCondtion есть хотя бы одна запись,
* у которой evo_reward_condition.evo_agency_agreementid. Выплата без других агентов
* (evo_reward_without_other_agent) = True,
* то выводить ошибку "Согласно условиям АД выплата допустима только этому агенту" на то RewardCondtion,
* у которого evo_reward_condition.evo_agency_agreementid равно True
* иначе все ок
*/
if (
[
calcBrokerRewardSum,
calcDoubleAgentRewardSumm,
dealerBrokerRewardSumm,
dealerRewardSumm,
finDepartmentRewardSumm,
indAgentRewardSumm,
].filter((x) => x > 0).length > 1
) {
await validateRewardWithoutOtherAgent({
conditionId: calcBrokerRewardCondition,
fieldName: 'selectCalcBrokerRewardCondition',
});
await validateRewardWithoutOtherAgent({
conditionId: calcDoubleAgentRewardCondition,
fieldName: 'selectCalcDoubleAgentRewardCondition',
});
await validateRewardWithoutOtherAgent({
conditionId: dealerBrokerRewardCondition,
fieldName: 'selectDealerBrokerRewardCondition',
});
await validateRewardWithoutOtherAgent({
conditionId: dealerRewardCondition,
fieldName: 'selectDealerRewardCondition',
});
await validateRewardWithoutOtherAgent({
conditionId: finDepartmentRewardCondtion,
fieldName: 'selectFinDepartmentRewardCondtion',
});
await validateRewardWithoutOtherAgent({
conditionId: indAgentRewardCondition,
fieldName: 'selectIndAgentRewardCondition',
});
}
});
}