From f042e23e6c7a504fcd4ab0b92c9a786cb23c3ee2 Mon Sep 17 00:00:00 2001 From: elforjani3 Date: Tue, 12 Jan 2021 15:37:25 +0200 Subject: [PATCH 1/6] fix: remove opening quantity & cost & date from item. --- client/src/containers/Items/ItemForm.js | 3 - .../src/containers/Items/ItemForm.schema.js | 20 ------ .../Items/ItemFormInventorySection.js | 71 ------------------- 3 files changed, 94 deletions(-) diff --git a/client/src/containers/Items/ItemForm.js b/client/src/containers/Items/ItemForm.js index b30b6c3ee..89af435d6 100644 --- a/client/src/containers/Items/ItemForm.js +++ b/client/src/containers/Items/ItemForm.js @@ -43,9 +43,6 @@ const defaultInitialValues = { category_id: '', sellable: 1, purchasable: true, - opening_quantity: '', - opening_cost: '', - opening_date: moment(new Date()).format('YYYY-MM-DD'), }; /** diff --git a/client/src/containers/Items/ItemForm.schema.js b/client/src/containers/Items/ItemForm.schema.js index 963b12f12..8eade519d 100644 --- a/client/src/containers/Items/ItemForm.schema.js +++ b/client/src/containers/Items/ItemForm.schema.js @@ -61,26 +61,6 @@ 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() - .integer() - .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) => { diff --git a/client/src/containers/Items/ItemFormInventorySection.js b/client/src/containers/Items/ItemFormInventorySection.js index f595635cc..efb2762fe 100644 --- a/client/src/containers/Items/ItemFormInventorySection.js +++ b/client/src/containers/Items/ItemFormInventorySection.js @@ -65,77 +65,6 @@ function ItemFormInventorySection({ accountsList, baseCurrency }) { )} - - {/*------------- Opening quantity ------------- */} - - {({ field, meta: { touched, error } }) => ( - } - labelInfo={} - className={'form-group--opening_quantity'} - intent={inputIntent({ error, touched })} - helperText={} - inline={true} - > - - - )} - - - {/*------------- Opening date ------------- */} - - {({ form, field: { value }, meta: { touched, error } }) => ( - } - labelInfo={} - className={classNames( - 'form-group--select-list', - 'form-group--opening_date', - CLASSES.FILL, - )} - intent={inputIntent({ error, touched })} - helperText={} - inline={true} - > - { - form.setFieldValue('opening_date', value); - })} - helperText={} - popoverProps={{ position: Position.BOTTOM, minimal: true }} - /> - - )} - - - - {/*------------- Opening cost ------------- */} - - - {({ form, field: { value }, meta: { touched, error } }) => ( - } - labelInfo={} - className={'form-group--opening_cost'} - intent={inputIntent({ error, touched })} - helperText={} - inline={true} - > - - - { - form.setFieldValue('opening_cost', unformattedValue); - }} - /> - - - )} - From 244a32e5fd8eb633572fd84d0a033226ec87f7fd Mon Sep 17 00:00:00 2001 From: elforjani3 Date: Wed, 13 Jan 2021 07:14:38 +0200 Subject: [PATCH 2/6] feat: adjustments. --- .../DecrementAdjustmentFields.js | 46 ++- .../IncrementAdjustmentFields.js | 46 ++- .../InventoryAdjustmentFloatingActions.js | 51 ++++ .../InventoryAdjustmentForm.schema.js | 19 +- .../InventoryAdjustmentFormDialogContent.js | 66 ++-- .../InventoryAdjustmentFormDialogFields.js | 284 ++++++++---------- .../InventoryAdjustmentFormDialog/index.js | 2 +- .../InventoryAdjustmentFormDialog/utils.js | 20 ++ .../Items/InventoryAdjustmentDataTable.js | 125 ++++++-- .../Items/InventoryAdjustmentList.js | 63 +++- client/src/containers/Items/ItemsDataTable.js | 19 +- .../Items/withInventoryAdjustmentActions.js | 2 +- .../Items/withInventoryAdjustments.js | 30 ++ client/src/lang/en/index.js | 9 +- .../inventoryAdjustment.actions.js | 13 +- .../inventoryAdjustment.reducer.js | 5 - .../inventoryAdjustment.selector.js | 52 ++++ .../inventoryAdjustment.type.js | 2 +- 18 files changed, 582 insertions(+), 272 deletions(-) create mode 100644 client/src/containers/Dialogs/InventoryAdjustmentFormDialog/InventoryAdjustmentFloatingActions.js create mode 100644 client/src/containers/Dialogs/InventoryAdjustmentFormDialog/utils.js diff --git a/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/DecrementAdjustmentFields.js b/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/DecrementAdjustmentFields.js index 85c3c0f2c..56564d374 100644 --- a/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/DecrementAdjustmentFields.js +++ b/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/DecrementAdjustmentFields.js @@ -1,21 +1,23 @@ import React from 'react'; -import { FastField, ErrorMessage } from 'formik'; -import { FormGroup, InputGroup } from '@blueprintjs/core'; -import { Row, Col, FieldRequiredHint } from 'components'; +import { sumBy, subtract, add } from 'lodash'; +import { FastField, ErrorMessage, useFormikContext } from 'formik'; +import { FormGroup, InputGroup, Intent } from '@blueprintjs/core'; import { inputIntent } from 'utils'; +import { Row, Col, If, FieldRequiredHint } from 'components'; import { FormattedMessage as T } from 'react-intl'; +import { calculate } from './utils'; function DecrementAdjustmentFields() { return ( {/*------------ Quantity on hand -----------*/} - + {({ field, meta: { error, touched } }) => ( } intent={inputIntent({ error, touched })} - helperText={} + helperText={} > @@ -24,15 +26,24 @@ function DecrementAdjustmentFields() { {/*------------ Decrement -----------*/} - - {({ field, meta: { error, touched } }) => ( + + {({ + form: { values, setFieldValue }, + field, + meta: { error, touched }, + }) => ( } intent={inputIntent({ error, touched })} - helperText={} + helperText={} fill={true} > - + { + setFieldValue('new_quantity', calculate(values, field.value)); + }} + /> )} @@ -40,13 +51,26 @@ function DecrementAdjustmentFields() { {/*------------ New quantity -----------*/} - {({ field, meta: { error, touched } }) => ( + {({ + form: { values, setFieldValue }, + field, + meta: { error, touched }, + }) => ( } intent={inputIntent({ error, touched })} helperText={} > - + { + setFieldValue( + 'quantity', + subtract(field.value, values.quantity_on_hand), + ); + }} + /> )} diff --git a/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/IncrementAdjustmentFields.js b/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/IncrementAdjustmentFields.js index 4a667e94d..558a58a9f 100644 --- a/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/IncrementAdjustmentFields.js +++ b/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/IncrementAdjustmentFields.js @@ -1,21 +1,23 @@ import React from 'react'; -import { FastField, ErrorMessage } from 'formik'; +import { FastField, ErrorMessage, useFormikContext } from 'formik'; +import { add, sumBy, subtract } from 'lodash'; import { FormGroup, InputGroup, Intent } from '@blueprintjs/core'; import { Row, Col, FieldRequiredHint } from 'components'; import { inputIntent } from 'utils'; import { FormattedMessage as T } from 'react-intl'; +import { calculate } from './utils'; function IncrementAdjustmentFields() { return ( {/*------------ Quantity on hand -----------*/} - + {({ field, meta: { error, touched } }) => ( } intent={inputIntent({ error, touched })} - helperText={} + helperText={} > @@ -24,15 +26,28 @@ function IncrementAdjustmentFields() { {/*------------ Increment -----------*/} - - {({ field, meta: { error, touched } }) => ( + + {({ + form: { values, setFieldValue }, + field, + meta: { error, touched }, + }) => ( } intent={inputIntent({ error, touched })} - helperText={} + helperText={} fill={true} > - + { + setFieldValue( + 'new_quantity', + calculate(values, event.currentTarget.value), + ); + }} + /> )} @@ -54,13 +69,26 @@ function IncrementAdjustmentFields() { {/*------------ New quantity -----------*/} - {({ field, meta: { error, touched } }) => ( + {({ + form: { values, setFieldValue }, + field, + meta: { error, touched }, + }) => ( } intent={inputIntent({ error, touched })} helperText={} > - + { + setFieldValue( + 'quantity', + subtract(field.value, values.quantity_on_hand), + ); + }} + /> )} diff --git a/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/InventoryAdjustmentFloatingActions.js b/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/InventoryAdjustmentFloatingActions.js new file mode 100644 index 000000000..1665e8adf --- /dev/null +++ b/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/InventoryAdjustmentFloatingActions.js @@ -0,0 +1,51 @@ +import React from 'react'; +import { Intent, Button, Classes } from '@blueprintjs/core'; +import { useFormikContext } from 'formik'; +import { FormattedMessage as T } from 'react-intl'; +import { saveInvoke } from 'utils'; + +export default function InventoryAdjustmentFloatingActions({ + onCloseClick, + onSubmitClick, +}) { + const { isSubmitting } = useFormikContext(); + + const handleSubmitDraftBtnClick = (event) => { + saveInvoke(onSubmitClick, event, { + publish: false, + }); + }; + const handleSubmitMakeAdjustmentBtnClick = (event) => { + saveInvoke(onSubmitClick, event, { + publish: true, + }); + }; + + return ( +
+
+ + + + +
+
+ ); +} diff --git a/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/InventoryAdjustmentForm.schema.js b/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/InventoryAdjustmentForm.schema.js index 7b0048f2d..25fe6c687 100644 --- a/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/InventoryAdjustmentForm.schema.js +++ b/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/InventoryAdjustmentForm.schema.js @@ -6,22 +6,25 @@ const Schema = Yup.object().shape({ date: Yup.date() .required() .label(formatMessage({ id: 'date' })), - type: Yup.number().required(), - adjustment_account_id: Yup.number().required(), + type: Yup.string().required(), + adjustment_account_id: Yup.string().required(), + item_id: Yup.number().required(), reason: Yup.string() .required() .label(formatMessage({ id: 'reason' })), - quantity: Yup.number().when(['type'], { - is: (type) => type, - then: Yup.number().required(), - }), + quantity_on_hand: Yup.number() + .min(0) + .required() + .label(formatMessage({ id: 'qty' })), + quantity: Yup.number().integer().max(Yup.ref('quantity_on_hand')).required(), cost: Yup.number().when(['type'], { is: (type) => type, - then: Yup.number().required(), + then: Yup.number(), }), reference_no: Yup.string(), - new_quantity: Yup.number(), + new_quantity: Yup.number().min(Yup.ref('quantity')).required(), description: Yup.string().min(3).max(DATATYPES_LENGTH.TEXT).nullable().trim(), + publish: Yup.boolean(), }); export const CreateInventoryAdjustmentFormSchema = Schema; diff --git a/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/InventoryAdjustmentFormDialogContent.js b/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/InventoryAdjustmentFormDialogContent.js index 4887fbc00..c66401cfb 100644 --- a/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/InventoryAdjustmentFormDialogContent.js +++ b/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/InventoryAdjustmentFormDialogContent.js @@ -1,33 +1,31 @@ -import React, { useCallback, useMemo } from 'react'; +import React, { useState, useCallback, useMemo } from 'react'; import { Intent } from '@blueprintjs/core'; -import { Formik } from 'formik'; +import { Formik, Form } from 'formik'; import { FormattedMessage as T, useIntl } from 'react-intl'; import { useQuery, queryCache } from 'react-query'; import moment from 'moment'; import { omit } from 'lodash'; -import { - AppToaster, - DialogContent, - Row, - Col, - ListSelect, - IF, -} from 'components'; +import { AppToaster, DialogContent } from 'components'; import { CreateInventoryAdjustmentFormSchema } from './InventoryAdjustmentForm.schema'; import InventoryAdjustmentFormDialogFields from './InventoryAdjustmentFormDialogFields'; - +import InventoryAdjustmentFloatingActions from './InventoryAdjustmentFloatingActions'; import withDialogActions from 'containers/Dialog/withDialogActions'; - +import withInventoryAdjustmentActions from 'containers/Items/withInventoryAdjustmentActions'; import { compose } from 'utils'; const defaultInitialValues = { date: moment(new Date()).format('YYYY-MM-DD'), type: 'decrement', adjustment_account_id: '', + item_id: '', reason: '', + cost: '', + quantity: '', reference_no: '', + quantity_on_hand: '', description: '', + publish: '', }; /** @@ -40,31 +38,38 @@ function InventoryAdjustmentFormDialogContent({ // #withAccountsActions requestFetchAccounts, + // #withInventoryAdjustmentActions + requestSubmitInventoryAdjustment, + // #ownProp + itemDetail, dialogName, - action, }) { const { formatMessage } = useIntl(); + const [submitPayload, setSubmitPayload] = useState({}); // Fetches accounts list. - const fetchAccountsList = useQuery('accounts-list', () => - requestFetchAccounts(), - ); + const fetchAccount = useQuery('accounts-list', () => requestFetchAccounts()); const initialValues = useMemo( () => ({ ...defaultInitialValues, + ...itemDetail, }), [], ); // Handles the form submit. const handleFormSubmit = (values, { setSubmitting, setErrors }) => { - const form = { ...values }; + const form = { + ...omit(values, ['quantity_on_hand', 'new_quantity', 'action']), + publish: submitPayload.publish, + }; const onSuccess = ({ response }) => { closeDialog(dialogName); queryCache.invalidateQueries('accounts-list'); + queryCache.invalidateQueries('items-table'); AppToaster.show({ message: formatMessage({ @@ -76,14 +81,21 @@ function InventoryAdjustmentFormDialogContent({ const onError = (error) => { setSubmitting(false); }; - //requestInventoryAdjustment + requestSubmitInventoryAdjustment({ form }).then(onSuccess).catch(onError); }; // Handles dialog close. - const handleClose = useCallback(() => { + const handleCloseClick = useCallback(() => { closeDialog(dialogName); }, [closeDialog, dialogName]); + const handleSubmitClick = useCallback( + (event, payload) => { + setSubmitPayload({ ...payload }); + }, + [setSubmitPayload], + ); + return ( - +
+ + +
); } -export default compose(withDialogActions)(InventoryAdjustmentFormDialogContent); +export default compose( + withInventoryAdjustmentActions, + withDialogActions, +)(InventoryAdjustmentFormDialogContent); diff --git a/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/InventoryAdjustmentFormDialogFields.js b/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/InventoryAdjustmentFormDialogFields.js index fca2d3b27..af6982c01 100644 --- a/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/InventoryAdjustmentFormDialogFields.js +++ b/client/src/containers/Dialogs/InventoryAdjustmentFormDialog/InventoryAdjustmentFormDialogFields.js @@ -1,24 +1,16 @@ import React from 'react'; +import { FastField, ErrorMessage, useFormikContext } from 'formik'; import { - Form, - FastField, - ErrorMessage, - useFormikContext, - useField, -} from 'formik'; -import { - Button, Classes, FormGroup, InputGroup, - Intent, TextArea, Position, } from '@blueprintjs/core'; import classNames from 'classnames'; -import { FormattedMessage as T } from 'react-intl'; +import { FormattedMessage as T, useIntl } from 'react-intl'; import { DateInput } from '@blueprintjs/datetime'; -import { ListSelect, Choose, If, FieldRequiredHint } from 'components'; +import { ListSelect, Choose, FieldRequiredHint } from 'components'; import { inputIntent, momentFormatter, @@ -30,7 +22,6 @@ import adjustmentType from 'common/adjustmentType'; import IncrementAdjustmentFields from './IncrementAdjustmentFields'; import DecrementAdjustmentFields from './DecrementAdjustmentFields'; import AccountsSuggestField from 'components/AccountsSuggestField'; - import withAccounts from 'containers/Accounts/withAccounts'; import { compose } from 'redux'; @@ -38,158 +29,135 @@ import { compose } from 'redux'; * Inventory adjustment form dialogs fields. */ function InventoryAdjustmentFormDialogFields({ - // #ownProps - onClose, - //# withAccount accountsList, }) { - const { values, isSubmitting } = useFormikContext(); - + const { values } = useFormikContext(); + const { formatMessage } = useIntl(); + // console.log(values, 'EE'); return ( -
-
- {/*------------ Date -----------*/} - - {({ form, field: { value }, meta: { error, touched } }) => ( - } - labelInfo={} - intent={inputIntent({ error, touched })} - helperText={} - minimal={true} - className={classNames(CLASSES.FILL)} - > - { - form.setFieldValue('date', formattedDate); - })} - value={tansformDateValue(value)} - popoverProps={{ - position: Position.BOTTOM, - minimal: true, - }} - /> - - )} - - - {/*------------ Adjustment type -----------*/} - - {({ form, field: { value }, meta: { error, touched } }) => ( - } - labelInfo={} - helperText={} - intent={inputIntent({ error, touched })} - className={classNames(CLASSES.FILL)} - > - { - console.log(type.value, 'EE'); - form.setFieldValue('type', type.value); - }} - selectedItem={value} - selectedItemProp={'value'} - labelProp={'name'} - popoverProps={{ minimal: true }} - /> - - )} - - - - - - - - - - - {/*------------ Reason -----------*/} - - {({ form, field, meta: { error, touched } }) => ( - } - labelInfo={} - intent={inputIntent({ error, touched })} - helperText={} - > - - - )} - - {/*------------ Adjustment account -----------*/} - - {({ form, field, meta: { error, touched } }) => ( - } - labelInfo={} - intent={inputIntent({ error, touched })} - helperText={} - > - - form.setFieldValue('adjustment_account_id', item) - } - /> - - )} - - {/*------------ Reference -----------*/} - - {({ form, field, meta: { error, touched } }) => ( - } - className={classNames(CLASSES.FILL)} - intent={inputIntent({ error, touched })} - helperText={} - > - - - )} - - {/*------------ description -----------*/} - - {({ field, meta: { error, touched } }) => ( - } - className={'form-group--description'} - intent={inputIntent({ error, touched })} - helperText={} - > -