fix: opening quantity & cost & date

This commit is contained in:
elforjani3
2020-12-26 23:09:48 +02:00
parent af96d4bde4
commit 085545c039
7 changed files with 112 additions and 45 deletions

View File

@@ -2,6 +2,7 @@ import * as Yup from 'yup';
import { defaultTo } from 'lodash';
import { formatMessage } from 'services/intl';
import { DATATYPES_LENGTH } from 'common/dataTypes';
import { isBlank } from 'utils';
const Schema = Yup.object().shape({
active: Yup.boolean(),
@@ -17,20 +18,24 @@ const Schema = Yup.object().shape({
.max(DATATYPES_LENGTH.STRING)
.label(formatMessage({ id: 'item_type_' })),
code: Yup.string().trim().min(0).max(DATATYPES_LENGTH.STRING),
cost_price: Yup.number().min(0).when(['purchasable'], {
is: true,
then: Yup.number()
.required()
.label(formatMessage({ id: 'cost_price_' })),
otherwise: Yup.number().nullable(true),
}),
sell_price: Yup.number().min(0).when(['sellable'], {
is: true,
then: Yup.number()
.required()
.label(formatMessage({ id: 'sell_price_' })),
otherwise: Yup.number().nullable(true),
}),
cost_price: Yup.number()
.min(0)
.when(['purchasable'], {
is: true,
then: Yup.number()
.required()
.label(formatMessage({ id: 'cost_price_' })),
otherwise: Yup.number().nullable(true),
}),
sell_price: Yup.number()
.min(0)
.when(['sellable'], {
is: true,
then: Yup.number()
.required()
.label(formatMessage({ id: 'sell_price_' })),
otherwise: Yup.number().nullable(true),
}),
cost_account_id: Yup.number()
.when(['purchasable'], {
is: true,
@@ -56,9 +61,27 @@ const Schema = Yup.object().shape({
stock: Yup.string() || Yup.boolean(),
sellable: Yup.boolean().required(),
purchasable: Yup.boolean().required(),
opening_cost: Yup.number().when(['opening_quantity'], {
is: (value) => value,
then: Yup.number()
.min(0)
.required()
.label(formatMessage({ id: 'opening_cost_' })),
otherwise: Yup.number().nullable(),
}),
opening_quantity: Yup.number()
.min(1)
.nullable()
.label(formatMessage({ id: 'opening_quantity_' })),
opening_date: Yup.date().when(['opening_quantity', 'opening_cost'], {
is: (quantity, cost) => !isBlank(quantity) && !isBlank(cost),
then: Yup.date()
.required()
.label(formatMessage({ id: 'opening_date_' })),
otherwise: Yup.date().nullable(),
}),
});
export const transformItemFormData = (item, defaultValue) => {
return {
...item,
@@ -66,7 +89,7 @@ export const transformItemFormData = (item, defaultValue) => {
purchasable: !!defaultTo(item?.purchasable, defaultValue.purchasable),
active: !!defaultTo(item?.active, defaultValue.active),
};
}
};
export const CreateItemFormSchema = Schema;
export const EditItemFormSchema = Schema;