2023-04-17 15:05:26 +03:00

101 lines
3.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import type { ValidationContext } from '../types';
import ValuesSchema from '@/config/schema/values';
import { VAT } from '@/constants/values';
import * as CRMTypes from '@/graphql/crm.types';
import { round } from 'tools';
import { z } from 'zod';
export function createValidationSchema({ apolloClient }: ValidationContext) {
return ValuesSchema.pick({
VATInLeaseObjectPrice: true,
balanceHolder: true,
firstPaymentRub: true,
lastPaymentPerc: true,
leaseObjectPriceWthtVAT: true,
plPriceRub: true,
product: true,
subsidySum: true,
supplierDiscountRub: true,
}).superRefine(
async (
{
VATInLeaseObjectPrice,
leaseObjectPriceWthtVAT,
product: productId,
supplierDiscountRub,
plPriceRub,
firstPaymentRub,
subsidySum,
balanceHolder,
lastPaymentPerc,
},
ctx
) => {
if (productId) {
const {
data: { evo_baseproduct },
} = await apolloClient.query({
query: CRMTypes.GetProductDocument,
variables: {
productId,
},
});
if (
evo_baseproduct?.evo_sale_without_nds &&
round(VATInLeaseObjectPrice / leaseObjectPriceWthtVAT, 2) >= VAT
) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message:
'При продаже ПЛ после ФЛ размер НДС в стоимости ПЛ не может составлять 20% и более от стоимости с НДС. Проверьте корректность НДС, либо измените Продукт',
path: ['tbxVATInLeaseObjectPrice'],
});
}
}
if (supplierDiscountRub >= plPriceRub) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Скидка не может быть больше или равна стоимости ПЛ',
path: ['tbxSupplierDiscountRub'],
});
}
if (firstPaymentRub >= plPriceRub) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Первый платеж не может быть больше или равен стоимости ПЛ',
path: ['tbxFirstPaymentRub'],
});
}
if (firstPaymentRub - subsidySum < 0) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message:
'Первый платеж с учетом субсидии получается отрицательный, увеличьте первый платеж',
path: ['tbxFirstPaymentRub'],
});
}
if (balanceHolder === 100_000_001) {
if (lastPaymentPerc < 1) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message:
'При балансе лизингодатель последний платеж не может быть меньше 1%! Увеличьте значение.',
path: ['tbxLastPaymentPerc'],
});
}
} else if (lastPaymentPerc === 0) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Последний платеж не может быть равен 0. Увеличьте значение',
path: ['tbxLastPaymentPerc'],
});
}
}
);
}