From bb9bded60fd03f22694236ea7597c5761a5c876a Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Tue, 7 Mar 2023 13:47:21 +0300 Subject: [PATCH] process/price: use new validation --- apps/web/config/process/default.ts | 4 +- .../web/process/price/reactions/validation.ts | 78 ++++++------------ apps/web/process/price/validation.ts | 80 +++++++++++++++++++ 3 files changed, 105 insertions(+), 57 deletions(-) create mode 100644 apps/web/process/price/validation.ts diff --git a/apps/web/config/process/default.ts b/apps/web/config/process/default.ts index 0eb5236..360def9 100644 --- a/apps/web/config/process/default.ts +++ b/apps/web/config/process/default.ts @@ -12,7 +12,7 @@ import * as leadOpportunity from '@/process/lead-opportunity'; import * as leasingWithoutKasko from '@/process/leasing-without-kasko'; import * as loadKP from '@/process/load-kp'; import * as payments from '@/process/payments'; -// import * as price from '@/process/price'; +import * as price from '@/process/price'; import * as subsidy from '@/process/subsidy'; import * as subsidyImportProgram from '@/process/subsidy-import-program'; // import * as supplierAgent from '@/process/supplier-agent'; @@ -23,7 +23,7 @@ export default function useReactions() { useProcess(loadKP); useProcess(calculate); // useProcess(supplierAgent); - // useProcess(price); + useProcess(price); useProcess(fingap); useProcess(leasingWithoutKasko); useProcess(subsidy); diff --git a/apps/web/process/price/reactions/validation.ts b/apps/web/process/price/reactions/validation.ts index 8860c17..9068822 100644 --- a/apps/web/process/price/reactions/validation.ts +++ b/apps/web/process/price/reactions/validation.ts @@ -1,74 +1,42 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { VAT } from '@/constants/values'; -import * as CRMTypes from '@/graphql/crm.types'; +import { createValidationSchema } from '../validation'; +import type { Elements } from '@/Components/Calculation/config/map/values'; import type { ProcessContext } from '@/process/types'; +import ValidationHelper from '@/stores/validation/helper'; import { reaction } from 'mobx'; -import { round } from 'tools'; +import { uid } from 'radash'; -export default function reactions({ store, apolloClient }: ProcessContext) { +const key = uid(7); + +export default function reactions(context: ProcessContext) { + const { store } = context; const { $calculation } = store; + const validationSchema = createValidationSchema(context); + const helper = new ValidationHelper(); reaction( () => $calculation.$values.getValues([ 'VATInLeaseObjectPrice', 'leaseObjectPriceWthtVAT', 'product', + 'supplierDiscountRub', + 'plPriceRub', + 'firstPaymentRub', + 'subsidySum', ]), - async ({ VATInLeaseObjectPrice, leaseObjectPriceWthtVAT, product: productId }) => { - let evo_sale_without_nds = false; + async (values) => { + helper.removeErrors(); + const validationResult = await validationSchema.safeParseAsync(values); - if (productId) { - const { - data: { evo_baseproduct }, - } = await apolloClient.query({ - query: CRMTypes.GetProductDocument, - variables: { - productId, - }, + if (!validationResult.success) { + validationResult.error.errors.forEach(({ path, message }) => { + (path as Elements[]).forEach((elementName) => { + const removeError = $calculation.element(elementName).setError({ key, message }); + if (removeError) helper.add(removeError); + }); }); - if (evo_baseproduct?.evo_sale_without_nds) { - evo_sale_without_nds = evo_baseproduct.evo_sale_without_nds; - } } - - $calculation.element('tbxVATInLeaseObjectPrice').validate({ - invalid: - evo_sale_without_nds && round(VATInLeaseObjectPrice / leaseObjectPriceWthtVAT, 2) >= VAT, - message: - 'При продаже ПЛ после ФЛ размер НДС в стоимости ПЛ не может составлять 20% и более от стоимости с НДС. Проверьте корректность НДС, либо измените Продукт', - }); - } - ); - - reaction( - () => $calculation.$values.getValues(['supplierDiscountRub', 'plPriceRub']), - ({ supplierDiscountRub, plPriceRub }) => { - $calculation.element('tbxSupplierDiscountRub').validate({ - invalid: supplierDiscountRub >= plPriceRub, - message: 'Скидка от поставщика не может быть больше или равна стоимости ПЛ', - }); - } - ); - - reaction( - () => $calculation.$values.getValues(['firstPaymentRub', 'plPriceRub']), - ({ firstPaymentRub, plPriceRub }) => { - $calculation.element('tbxFirstPaymentRub').validate({ - invalid: firstPaymentRub >= plPriceRub, - message: 'Первый платеж не может быть больше или равен стоимости ПЛ', - }); - } - ); - - reaction( - () => $calculation.$values.getValues(['firstPaymentRub', 'subsidySum']), - ({ firstPaymentRub, subsidySum }) => { - $calculation.element('tbxFirstPaymentRub').validate({ - invalid: firstPaymentRub - subsidySum < 0, - message: - 'Первый платеж с учетом субсидии получается отрицательный, увеличьте первый платеж', - }); } ); } diff --git a/apps/web/process/price/validation.ts b/apps/web/process/price/validation.ts new file mode 100644 index 0000000..427a09e --- /dev/null +++ b/apps/web/process/price/validation.ts @@ -0,0 +1,80 @@ +/* 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'], + }); + } + } + ); +}