fix: adjustment item calculate quantity.

This commit is contained in:
a.bouhuolia
2021-01-17 20:29:34 +02:00
parent 79f9190de3
commit 10ab8f4711
7 changed files with 129 additions and 72 deletions

View File

@@ -1,10 +1,11 @@
import React from 'react';
import { FastField, ErrorMessage } from 'formik';
import { Field, ErrorMessage, FastField } from 'formik';
import { FormGroup, InputGroup } from '@blueprintjs/core';
import { inputIntent } from 'utils';
import { Row, Col } from 'components';
import { Row, Col, MoneyInputGroup } from 'components';
import { FormattedMessage as T } from 'react-intl';
import { decrementCalc, dec } from './utils';
import { decrementQuantity } from './utils';
import { toSafeNumber } from 'utils';
function DecrementAdjustmentFields() {
return (
@@ -27,10 +28,10 @@ function DecrementAdjustmentFields() {
<Col className={'col--sign'}>
<span></span>
</Col>
{/*------------ Decrement -----------*/}
<Col className={'col--decrement'}>
<FastField name={'quantity'}>
<Field name={'quantity'}>
{({
form: { values, setFieldValue },
field,
@@ -42,15 +43,26 @@ function DecrementAdjustmentFields() {
helperText={<ErrorMessage name="quantity" />}
fill={true}
>
<InputGroup
{...field}
onBlur={(event) => {
setFieldValue('new_quantity', decrementCalc(values, event));
<MoneyInputGroup
value={field.value}
allowDecimals={false}
allowNegativeValue={true}
onChange={(value) => {
setFieldValue('quantity', value);
}}
onBlurValue={(value) => {
setFieldValue(
'new_quantity',
decrementQuantity(
toSafeNumber(value),
toSafeNumber(values.quantity_on_hand),
),
);
}}
/>
</FormGroup>
)}
</FastField>
</Field>
</Col>
<Col className={'col--sign'}>
@@ -58,7 +70,7 @@ function DecrementAdjustmentFields() {
</Col>
{/*------------ New quantity -----------*/}
<Col className={'col--quantity'}>
<FastField name={'new_quantity'}>
<Field name={'new_quantity'}>
{({
form: { values, setFieldValue },
field,
@@ -69,15 +81,26 @@ function DecrementAdjustmentFields() {
intent={inputIntent({ error, touched })}
helperText={<ErrorMessage name="new_quantity" />}
>
<InputGroup
{...field}
onBlur={(event) => {
setFieldValue('quantity', decrementCalc(values, event));
<MoneyInputGroup
value={field.value}
allowDecimals={false}
allowNegativeValue={true}
onChange={(value) => {
setFieldValue('new_quantity', value);
}}
onBlurValue={(value) => {
setFieldValue(
'quantity',
decrementQuantity(
toSafeNumber(value),
toSafeNumber(values.quantity_on_hand),
),
);
}}
/>
</FormGroup>
)}
</FastField>
</Field>
</Col>
</Row>
);

View File

@@ -1,10 +1,10 @@
import React from 'react';
import { FastField, ErrorMessage } from 'formik';
import { Field, FastField, ErrorMessage } from 'formik';
import { FormGroup, InputGroup } from '@blueprintjs/core';
import { Row, Col } from 'components';
import { inputIntent } from 'utils';
import { Row, Col, MoneyInputGroup } from 'components';
import { inputIntent, toSafeNumber } from 'utils';
import { FormattedMessage as T } from 'react-intl';
import { decrementCalc, incrementCalc } from './utils';
import { decrementQuantity, incrementQuantity } from './utils';
function IncrementAdjustmentFields() {
return (
@@ -31,7 +31,7 @@ function IncrementAdjustmentFields() {
{/*------------ Increment -----------*/}
<Col className={'col--quantity'}>
<FastField name={'quantity'}>
<Field name={'quantity'}>
{({
form: { values, setFieldValue },
field,
@@ -43,27 +43,47 @@ function IncrementAdjustmentFields() {
helperText={<ErrorMessage name="quantity" />}
fill={true}
>
<InputGroup
{...field}
onBlur={(event) => {
setFieldValue('new_quantity', incrementCalc(values, event));
<MoneyInputGroup
value={field.value}
allowDecimals={false}
allowNegativeValue={true}
onChange={(value) => {
setFieldValue('quantity', value);
}}
onBlurValue={(value) => {
setFieldValue(
'new_quantity',
incrementQuantity(
toSafeNumber(value),
toSafeNumber(values.quantity_on_hand),
),
);
}}
/>
</FormGroup>
)}
</FastField>
</Field>
</Col>
{/*------------ Cost -----------*/}
<Col className={'col--cost'}>
<FastField name={'cost'}>
{({ field, meta: { error, touched } }) => (
{({
form: { setFieldValue },
field: { value },
meta: { error, touched },
}) => (
<FormGroup
label={<T id={'cost'} />}
intent={inputIntent({ error, touched })}
helperText={<ErrorMessage name="cost" />}
>
<InputGroup medium={'true'} {...field} />
<MoneyInputGroup
value={value}
onChange={(value) => {
setFieldValue('cost', value);
}}
/>
</FormGroup>
)}
</FastField>
@@ -76,7 +96,7 @@ function IncrementAdjustmentFields() {
{/*------------ New quantity -----------*/}
<Col className={'col--quantity-on-hand'}>
<FastField name={'new_quantity'}>
<Field name={'new_quantity'}>
{({
form: { values, setFieldValue },
field,
@@ -87,15 +107,26 @@ function IncrementAdjustmentFields() {
intent={inputIntent({ error, touched })}
helperText={<ErrorMessage name="new_quantity" />}
>
<InputGroup
{...field}
onBlur={(event) => {
setFieldValue('quantity', decrementCalc(values, event));
<MoneyInputGroup
value={field.value}
allowDecimals={false}
allowNegativeValue={true}
onChange={(value) => {
setFieldValue('new_quantity', value);
}}
onBlurValue={(value) => {
setFieldValue(
'quantity',
decrementQuantity(
toSafeNumber(value),
toSafeNumber(values.quantity_on_hand),
),
);
}}
/>
</FormGroup>
)}
</FastField>
</Field>
</Col>
</Row>
);

View File

@@ -11,19 +11,19 @@ const Schema = Yup.object().shape({
item_id: Yup.number().required(),
reason: Yup.string()
.required()
.min(3)
.max(DATATYPES_LENGTH.TEXT)
.label(formatMessage({ id: 'reason' })),
quantity_on_hand: Yup.number()
.min(0)
.required()
.label(formatMessage({ id: 'qty' })),
quantity: Yup.number().integer().max(Yup.ref('quantity_on_hand')).required(),
quantity: Yup.number().integer().min(1).required(),
cost: Yup.number().when(['type'], {
is: (type) => type,
then: Yup.number(),
is: (type) => type === 'increment',
then: Yup.number().required(),
}),
reference_no: Yup.string(),
new_quantity: Yup.number().min(Yup.ref('quantity')).required(),
description: Yup.string().min(3).max(DATATYPES_LENGTH.TEXT).nullable().trim(),
new_quantity: Yup.number().required(),
publish: Yup.boolean(),
});

View File

@@ -33,7 +33,6 @@ const defaultInitialValues = {
quantity: '',
reference_no: '',
quantity_on_hand: '',
description: '',
publish: '',
};
@@ -73,6 +72,7 @@ function InventoryAdjustmentFormDialogContent({
// Initial form values.
const initialValues = {
...defaultInitialValues,
item_id: itemId,
quantity_on_hand: get(item, 'quantity_on_hand', 0),
};
@@ -104,7 +104,7 @@ function InventoryAdjustmentFormDialogContent({
const handleCloseClick = useCallback(() => {
closeDialog(dialogName);
}, [closeDialog, dialogName]);
const handleSubmitClick = useCallback(
(event, payload) => {
setSubmitPayload({ ...payload });

View File

@@ -1,5 +1,5 @@
import React from 'react';
import { FastField, ErrorMessage, useFormikContext } from 'formik';
import { FastField, ErrorMessage, Field, useFormikContext } from 'formik';
import {
Classes,
FormGroup,
@@ -10,20 +10,21 @@ import {
import classNames from 'classnames';
import { FormattedMessage as T, useIntl } from 'react-intl';
import { DateInput } from '@blueprintjs/datetime';
import { compose } from 'redux';
import { ListSelect, FieldRequiredHint, Col, Row } from 'components';
import {
inputIntent,
momentFormatter,
tansformDateValue,
handleDateChange,
toSafeNumber
} from 'utils';
import { CLASSES } from 'common/classes';
import adjustmentType from 'common/adjustmentType';
import AccountsSuggestField from 'components/AccountsSuggestField';
import withAccounts from 'containers/Accounts/withAccounts';
import { compose } from 'redux';
import { decrementCalc, incrementCalc, dec } from './utils';
import { diffQuantity } from './utils';
import InventoryAdjustmentQuantityFields from './InventoryAdjustmentQuantityFields';
/**
@@ -66,10 +67,11 @@ function InventoryAdjustmentFormDialogFields({
)}
</FastField>
</Col>
<Col xs={5}>
{/*------------ Adjustment type -----------*/}
<FastField name={'type'}>
{({ form, field: { value }, meta: { error, touched } }) => (
<Field name={'type'}>
{({ form: { values, setFieldValue }, field: { value }, meta: { error, touched } }) => (
<FormGroup
label={<T id={'adjustment_type'} />}
labelInfo={<FieldRequiredHint />}
@@ -80,16 +82,13 @@ function InventoryAdjustmentFormDialogFields({
<ListSelect
items={adjustmentType}
onItemSelect={(type) => {
form.setFieldValue('type', type.value);
type?.value == 'increment'
? form.setFieldValue(
'new_quantity',
incrementCalc(values),
)
: form.setFieldValue(
'new_quantity',
values.quantity_on_hand - values.quantity,
);
const result = diffQuantity(
toSafeNumber(values.quantity),
toSafeNumber(values.quantity_on_hand),
type.value
);
setFieldValue('type', type.value);
setFieldValue('new_quantity', result);
}}
filterable={false}
selectedItem={value}
@@ -99,7 +98,7 @@ function InventoryAdjustmentFormDialogFields({
/>
</FormGroup>
)}
</FastField>
</Field>
</Col>
</Row>
@@ -145,7 +144,7 @@ function InventoryAdjustmentFormDialogFields({
</FastField>
{/*------------ description -----------*/}
<FastField name={'adjustment_reasons'}>
<FastField name={'reason'}>
{({ field, meta: { error, touched } }) => (
<FormGroup
label={<T id={'adjustment_reasons'} />}
@@ -153,11 +152,7 @@ function InventoryAdjustmentFormDialogFields({
intent={inputIntent({ error, touched })}
helperText={<ErrorMessage name={'adjustment_reasons'} />}
>
<TextArea
growVertically={true}
large={true}
{...field}
/>
<TextArea growVertically={true} large={true} {...field} />
</FormGroup>
)}
</FastField>

View File

@@ -1,11 +1,14 @@
export const decrementCalc = ({ quantity_on_hand, type }, e) => {
if (type == 'decrement') {
return parseInt(quantity_on_hand, 10) - parseInt(e.currentTarget.value, 10);
} else {
return e.currentTarget.value - quantity_on_hand;
}
export const decrementQuantity = (newQuantity, quantityOnHand) => {
return quantityOnHand - newQuantity;
};
export const incrementCalc = ({ quantity_on_hand, quantity }, e) => {
return parseInt(quantity_on_hand, 10) + parseInt(quantity, 10);
export const incrementQuantity = (newQuantity, quantityOnHand) => {
return quantityOnHand + newQuantity;
};
export const diffQuantity = (newQuantity, quantityOnHand, type) => {
return type === 'decrement'
? decrementQuantity(newQuantity, quantityOnHand)
: incrementQuantity(newQuantity, quantityOnHand);
};

View File

@@ -388,3 +388,8 @@ export const getColumnWidth = (
return result;
};
export const toSafeNumber = (number) => {
return _.toNumber(_.defaultTo(number, 0));
}