140 lines
5.3 KiB
TypeScript
140 lines
5.3 KiB
TypeScript
/* eslint-disable sonarjs/cognitive-complexity */
|
||
import type { ValidationContext } from '../types';
|
||
import elementsProps from '@/Components/Calculation/config/elements-props';
|
||
import { getElementName } from '@/Components/Calculation/config/map/values';
|
||
import ValuesSchema from '@/config/schema/values';
|
||
import * as CRMTypes from '@/graphql/crm.types';
|
||
import type { InputNumberProps } from 'antd/es/input-number';
|
||
import { z } from 'zod';
|
||
|
||
const Schema = ValuesSchema.pick({
|
||
IRR_Perc: true,
|
||
VATInLeaseObjectPrice: true,
|
||
addEquipmentPrice: true,
|
||
bonusCoefficient: true,
|
||
calcBrokerRewardSum: true,
|
||
calcDoubleAgentRewardSumm: true,
|
||
comissionPerc: true,
|
||
comissionRub: true,
|
||
countSeats: true,
|
||
creditRate: true,
|
||
dealerBrokerRewardSumm: true,
|
||
dealerRewardSumm: true,
|
||
engineHours: true,
|
||
engineVolume: true,
|
||
finDepartmentRewardSumm: true,
|
||
firstPaymentPerc: true,
|
||
firstPaymentRub: true,
|
||
floatingRate: true,
|
||
importProgramSum: true,
|
||
importerRewardPerc: true,
|
||
importerRewardRub: true,
|
||
indAgentRewardSumm: true,
|
||
insFranchise: true,
|
||
insKaskoPriceLeasePeriod: true,
|
||
lastPaymentPerc: true,
|
||
lastPaymentRub: true,
|
||
leaseObjectCount: true,
|
||
leaseObjectMotorPower: true,
|
||
leaseObjectPrice: true,
|
||
leaseObjectPriceWthtVAT: true,
|
||
leaseObjectYear: true,
|
||
leasingPeriod: true,
|
||
leasingWithoutKasko: true,
|
||
maxMass: true,
|
||
maxPriceChange: true,
|
||
maxSpeed: true,
|
||
mileage: true,
|
||
minPriceChange: true,
|
||
parmentsDecreasePercent: true,
|
||
pi: true,
|
||
redemptionPaymentSum: true,
|
||
saleBonus: true,
|
||
subsidySum: true,
|
||
supplierDiscountPerc: true,
|
||
supplierDiscountRub: true,
|
||
tarif: true,
|
||
totalPayments: true,
|
||
vehicleTaxInLeasingPeriod: true,
|
||
vehicleTaxInYear: true,
|
||
});
|
||
|
||
type Values = keyof z.infer<typeof Schema>;
|
||
|
||
export function createValidationSchema({ apolloClient }: ValidationContext) {
|
||
return Schema.superRefine(async (values, ctx) => {
|
||
/**
|
||
* На изменение поля Процет убывания платежей tbxParmentsDecreasePercent необходимо заложить проверку:
|
||
* Если значение поля меньше значения в поле "Минимальный % убывания платежей" evo_min_decreasing_perc из записи,
|
||
* указанной в поле ТарифselectTarif , то поле Процет убывания платежей tbxParmentsDecreasePercent должно обводиться красной рамкой
|
||
* и выводиться сообщение "Процент убывания не может быть меньше минимального значения по данному тарифу
|
||
* - <указывается значение из поля "Минимальный % убывания платежей">, иначе красная рамка снимается.
|
||
* При красной рамке в данном поле нельзя осуществить расчет графика.
|
||
*/
|
||
|
||
const { tarif: tarifId, parmentsDecreasePercent } = values;
|
||
|
||
if (tarifId) {
|
||
const {
|
||
data: { evo_tarif },
|
||
} = await apolloClient.query({
|
||
query: CRMTypes.GetTarifDocument,
|
||
variables: {
|
||
tarifId,
|
||
},
|
||
});
|
||
|
||
if (
|
||
evo_tarif?.evo_min_decreasing_perc &&
|
||
parmentsDecreasePercent < evo_tarif?.evo_min_decreasing_perc
|
||
) {
|
||
ctx.addIssue({
|
||
code: z.ZodIssueCode.custom,
|
||
message: `Процент убывания не может быть меньше минимального значения по данному тарифу - ${evo_tarif?.evo_min_decreasing_perc}`,
|
||
path: ['tbxParmentsDecreasePercent'],
|
||
});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Если Плавающая ставка = Да и Лизинг без КАСКО содержит данные,
|
||
* то выводить сообщение "При плавающей ставке нельзя оформлять Лизинг без КАСКО"
|
||
*/
|
||
const { floatingRate, leasingWithoutKasko } = values;
|
||
if (floatingRate && leasingWithoutKasko) {
|
||
ctx.addIssue({
|
||
code: z.ZodIssueCode.custom,
|
||
message: 'При плавающей ставке нельзя оформлять Лизинг без КАСКО',
|
||
path: ['selectLeasingWithoutKasko'],
|
||
});
|
||
}
|
||
|
||
(Object.keys(values) as Values[]).forEach((valueName) => {
|
||
const elementName = getElementName(valueName);
|
||
if (elementName) {
|
||
const props = elementsProps[elementName] as InputNumberProps<number>;
|
||
const value = values[valueName] as number | null | undefined;
|
||
if (props?.min && value !== undefined && value !== null && value < props.min) {
|
||
ctx.addIssue({
|
||
code: z.ZodIssueCode.custom,
|
||
message: `Значение поля меньше минимального: ${Intl.NumberFormat('ru').format(
|
||
props.min
|
||
)}`,
|
||
path: [elementName],
|
||
});
|
||
}
|
||
|
||
if (props?.max && value !== undefined && value !== null && value > props.max) {
|
||
ctx.addIssue({
|
||
code: z.ZodIssueCode.custom,
|
||
message: `Значение поля больше максимального: ${Intl.NumberFormat('ru').format(
|
||
props.max
|
||
)}`,
|
||
path: [elementName],
|
||
});
|
||
}
|
||
}
|
||
});
|
||
});
|
||
}
|