80 lines
2.5 KiB
TypeScript
80 lines
2.5 KiB
TypeScript
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'],
|
||
});
|
||
}
|
||
}
|
||
);
|
||
}
|