diff --git a/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/DecrementAdjustmentFields.js b/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/DecrementAdjustmentFields.js index 5c185e112..ecf61e359 100644 --- a/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/DecrementAdjustmentFields.js +++ b/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/DecrementAdjustmentFields.js @@ -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() { - + {/*------------ Decrement -----------*/} - + {({ form: { values, setFieldValue }, field, @@ -42,15 +43,26 @@ function DecrementAdjustmentFields() { helperText={} fill={true} > - { - setFieldValue('new_quantity', decrementCalc(values, event)); + { + setFieldValue('quantity', value); + }} + onBlurValue={(value) => { + setFieldValue( + 'new_quantity', + decrementQuantity( + toSafeNumber(value), + toSafeNumber(values.quantity_on_hand), + ), + ); }} /> )} - + @@ -58,7 +70,7 @@ function DecrementAdjustmentFields() { {/*------------ New quantity -----------*/} - + {({ form: { values, setFieldValue }, field, @@ -69,15 +81,26 @@ function DecrementAdjustmentFields() { intent={inputIntent({ error, touched })} helperText={} > - { - setFieldValue('quantity', decrementCalc(values, event)); + { + setFieldValue('new_quantity', value); + }} + onBlurValue={(value) => { + setFieldValue( + 'quantity', + decrementQuantity( + toSafeNumber(value), + toSafeNumber(values.quantity_on_hand), + ), + ); }} /> )} - + ); diff --git a/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/IncrementAdjustmentFields.js b/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/IncrementAdjustmentFields.js index 4d7a726ca..b352c9155 100644 --- a/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/IncrementAdjustmentFields.js +++ b/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/IncrementAdjustmentFields.js @@ -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 -----------*/} - + {({ form: { values, setFieldValue }, field, @@ -43,27 +43,47 @@ function IncrementAdjustmentFields() { helperText={} fill={true} > - { - setFieldValue('new_quantity', incrementCalc(values, event)); + { + setFieldValue('quantity', value); + }} + onBlurValue={(value) => { + setFieldValue( + 'new_quantity', + incrementQuantity( + toSafeNumber(value), + toSafeNumber(values.quantity_on_hand), + ), + ); }} /> )} - + {/*------------ Cost -----------*/} - {({ field, meta: { error, touched } }) => ( + {({ + form: { setFieldValue }, + field: { value }, + meta: { error, touched }, + }) => ( } intent={inputIntent({ error, touched })} helperText={} > - + { + setFieldValue('cost', value); + }} + /> )} @@ -76,7 +96,7 @@ function IncrementAdjustmentFields() { {/*------------ New quantity -----------*/} - + {({ form: { values, setFieldValue }, field, @@ -87,15 +107,26 @@ function IncrementAdjustmentFields() { intent={inputIntent({ error, touched })} helperText={} > - { - setFieldValue('quantity', decrementCalc(values, event)); + { + setFieldValue('new_quantity', value); + }} + onBlurValue={(value) => { + setFieldValue( + 'quantity', + decrementQuantity( + toSafeNumber(value), + toSafeNumber(values.quantity_on_hand), + ), + ); }} /> )} - + ); diff --git a/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/InventoryAdjustmentForm.schema.js b/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/InventoryAdjustmentForm.schema.js index 25fe6c687..41a1af3a1 100644 --- a/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/InventoryAdjustmentForm.schema.js +++ b/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/InventoryAdjustmentForm.schema.js @@ -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(), }); diff --git a/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/InventoryAdjustmentFormDialogContent.js b/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/InventoryAdjustmentFormDialogContent.js index 64d58455d..c2017709d 100644 --- a/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/InventoryAdjustmentFormDialogContent.js +++ b/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/InventoryAdjustmentFormDialogContent.js @@ -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 }); diff --git a/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/InventoryAdjustmentFormDialogFields.js b/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/InventoryAdjustmentFormDialogFields.js index 858f08b1b..9623384bb 100644 --- a/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/InventoryAdjustmentFormDialogFields.js +++ b/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/InventoryAdjustmentFormDialogFields.js @@ -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({ )} + {/*------------ Adjustment type -----------*/} - - {({ form, field: { value }, meta: { error, touched } }) => ( + + {({ form: { values, setFieldValue }, field: { value }, meta: { error, touched } }) => ( } labelInfo={} @@ -80,16 +82,13 @@ function InventoryAdjustmentFormDialogFields({ { - 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({ /> )} - + @@ -145,7 +144,7 @@ function InventoryAdjustmentFormDialogFields({ {/*------------ description -----------*/} - + {({ field, meta: { error, touched } }) => ( } @@ -153,11 +152,7 @@ function InventoryAdjustmentFormDialogFields({ intent={inputIntent({ error, touched })} helperText={} > -