process/price: use new validation

This commit is contained in:
vchikalkin 2023-03-07 13:47:21 +03:00
parent a242fe26de
commit bb9bded60f
3 changed files with 105 additions and 57 deletions

View File

@ -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);

View File

@ -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:
'Первый платеж с учетом субсидии получается отрицательный, увеличьте первый платеж',
});
}
);
}

View File

@ -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'],
});
}
}
);
}