2023-03-10 09:37:37 +03:00

81 lines
2.6 KiB
TypeScript
Raw Permalink 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.

/* eslint-disable @typescript-eslint/naming-convention */
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,
firstPaymentRub: true,
leaseObjectPriceWthtVAT: true,
plPriceRub: true,
product: true,
subsidySum: true,
supplierDiscountRub: true,
}).superRefine(
async (
{
VATInLeaseObjectPrice,
leaseObjectPriceWthtVAT,
product: productId,
supplierDiscountRub,
plPriceRub,
firstPaymentRub,
subsidySum,
},
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'],
});
}
}
);
}