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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,11 +1,14 @@
export const decrementCalc = ({ quantity_on_hand, type }, e) => {
if (type == 'decrement') { export const decrementQuantity = (newQuantity, quantityOnHand) => {
return parseInt(quantity_on_hand, 10) - parseInt(e.currentTarget.value, 10); return quantityOnHand - newQuantity;
} else {
return e.currentTarget.value - quantity_on_hand;
}
}; };
export const incrementCalc = ({ quantity_on_hand, quantity }, e) => { export const incrementQuantity = (newQuantity, quantityOnHand) => {
return parseInt(quantity_on_hand, 10) + parseInt(quantity, 10); 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; return result;
}; };
export const toSafeNumber = (number) => {
return _.toNumber(_.defaultTo(number, 0));
}