From e92c4486aa627b9364506ac2b073e71cac156233 Mon Sep 17 00:00:00 2001 From: "a.bouhuolia" Date: Thu, 25 May 2023 22:04:21 +0200 Subject: [PATCH] fix(webapp): auto-increment estimate transactions --- .../Estimates/EstimateForm/EstimateForm.tsx | 15 +++++++++------ .../EstimateForm/EstimateFormDialogs.tsx | 8 ++++++-- .../EstimateForm/EstimateFormHeaderFields.tsx | 7 ++++++- .../Estimates/EstimateForm/components.tsx | 8 ++++---- .../Sales/Estimates/EstimateForm/utils.tsx | 18 +++++++++++++++++- .../Sales/Invoices/InvoiceForm/InvoiceForm.tsx | 8 +++++--- .../Sales/Invoices/InvoiceForm/utils.tsx | 2 +- 7 files changed, 48 insertions(+), 18 deletions(-) diff --git a/packages/webapp/src/containers/Sales/Estimates/EstimateForm/EstimateForm.tsx b/packages/webapp/src/containers/Sales/Estimates/EstimateForm/EstimateForm.tsx index 9abcd9b80..a4db1cfff 100644 --- a/packages/webapp/src/containers/Sales/Estimates/EstimateForm/EstimateForm.tsx +++ b/packages/webapp/src/containers/Sales/Estimates/EstimateForm/EstimateForm.tsx @@ -19,6 +19,7 @@ import EstimateFloatingActions from './EstimateFloatingActions'; import EstimateFormFooter from './EstimateFormFooter'; import EstimateFormDialogs from './EstimateFormDialogs'; import EstimtaeFormTopBar from './EstimtaeFormTopBar'; +import { EstimateIncrementSyncSettingsToForm } from './components'; import withSettings from '@/containers/Settings/withSettings'; import withCurrentOrganization from '@/containers/Organization/withCurrentOrganization'; @@ -31,8 +32,8 @@ import { defaultEstimate, transfromsFormValuesToRequest, handleErrors, + resetFormState, } from './utils'; -import { EstimateIncrementSyncSettingsToForm } from './components'; /** * Estimate form. @@ -41,7 +42,7 @@ function EstimateForm({ // #withSettings estimateNextNumber, estimateNumberPrefix, - estimateIncrementMode, + estimateAutoIncrementMode, // #withCurrentOrganization organization: { base_currency }, @@ -67,14 +68,16 @@ function EstimateForm({ ? { ...transformToEditForm(estimate) } : { ...defaultEstimate, - ...(estimateIncrementMode && { + // If the auto-increment mode is enabled, take the next estimate + // number from the settings. + ...(estimateAutoIncrementMode && { estimate_number: estimateNumber, }), entries: orderingLinesIndexes(defaultEstimate.entries), currency_code: base_currency, }), }), - [estimate, estimateNumber, estimateIncrementMode, base_currency], + [estimate, estimateNumber, estimateAutoIncrementMode, base_currency], ); // Handles form submit. @@ -119,7 +122,7 @@ function EstimateForm({ history.push('/estimates'); } if (submitPayload.resetForm) { - resetForm(); + resetFormState({ resetForm, initialValues, values }); } }; // Handle the request error. @@ -177,7 +180,7 @@ export default compose( withSettings(({ estimatesSettings }) => ({ estimateNextNumber: estimatesSettings?.nextNumber, estimateNumberPrefix: estimatesSettings?.numberPrefix, - estimateIncrementMode: estimatesSettings?.autoIncrement, + estimateAutoIncrementMode: estimatesSettings?.autoIncrement, })), withCurrentOrganization(), )(EstimateForm); diff --git a/packages/webapp/src/containers/Sales/Estimates/EstimateForm/EstimateFormDialogs.tsx b/packages/webapp/src/containers/Sales/Estimates/EstimateForm/EstimateFormDialogs.tsx index feb18a3c3..aa1c165a1 100644 --- a/packages/webapp/src/containers/Sales/Estimates/EstimateForm/EstimateFormDialogs.tsx +++ b/packages/webapp/src/containers/Sales/Estimates/EstimateForm/EstimateFormDialogs.tsx @@ -9,10 +9,14 @@ import EstimateNumberDialog from '@/containers/Dialogs/EstimateNumberDialog'; export default function EstimateFormDialogs() { const { setFieldValue } = useFormikContext(); - // Update the form once the invoice number form submit confirm. + // Update the form once the estimate number form submit confirm. const handleEstimateNumberFormConfirm = (settings) => { setFieldValue('estimate_number', settings.transactionNumber); - setFieldValue('estimate_number_manually', settings.transactionNumber); + setFieldValue('estimate_number_manually', ''); + + if (settings.incrementMode !== 'auto') { + setFieldValue('estimate_number_manually', settings.transactionNumber); + } }; return ( diff --git a/packages/webapp/src/containers/Sales/Estimates/EstimateForm/EstimateFormHeaderFields.tsx b/packages/webapp/src/containers/Sales/Estimates/EstimateForm/EstimateFormHeaderFields.tsx index 981826851..70444babc 100644 --- a/packages/webapp/src/containers/Sales/Estimates/EstimateForm/EstimateFormHeaderFields.tsx +++ b/packages/webapp/src/containers/Sales/Estimates/EstimateForm/EstimateFormHeaderFields.tsx @@ -67,9 +67,12 @@ const EstimateFormEstimateNumberField = R.compose( const handleEstimateNumberBtnClick = () => { openDialog('estimate-number-form', {}); }; + // Handle estimate no. field blur. const handleEstimateNoBlur = (event) => { const newValue = event.target.value; + // Show the confirmation dialog if the value has changed and auto-increment + // mode is enabled. if (values.estimate_number !== newValue && estimateAutoIncrement) { openDialog('estimate-number-form', { initialFormValues: { @@ -78,6 +81,8 @@ const EstimateFormEstimateNumberField = R.compose( }, }); } + // Setting the estimate number to the form will be manually in case + // auto-increment is disable. if (!estimateAutoIncrement) { setFieldValue('estimate_number', newValue); setFieldValue('estimate_number_manually', newValue); @@ -96,7 +101,7 @@ const EstimateFormEstimateNumberField = R.compose( minimal={true} asyncControl={true} onBlur={handleEstimateNoBlur} - fastField={true} + onChange={() => {}} /> { + // Do not update if the estimate auto-increment mode is disabled. if (!estimateAutoIncrement) return null; - const estimateNo = transactionNumber( - estimateNumberPrefix, - estimateNextNumber, + setFieldValue( + 'estimate_number', + transactionNumber(estimateNumberPrefix, estimateNextNumber), ); - setFieldValue('estimate_number', estimateNo); }, [ setFieldValue, estimateNumberPrefix, diff --git a/packages/webapp/src/containers/Sales/Estimates/EstimateForm/utils.tsx b/packages/webapp/src/containers/Sales/Estimates/EstimateForm/utils.tsx index 08e963e8b..096bd671c 100644 --- a/packages/webapp/src/containers/Sales/Estimates/EstimateForm/utils.tsx +++ b/packages/webapp/src/containers/Sales/Estimates/EstimateForm/utils.tsx @@ -7,7 +7,6 @@ import { useFormikContext } from 'formik'; import { omit, first } from 'lodash'; import { defaultFastFieldShouldUpdate, - transactionNumber, repeatValue, transformToForm, formattedAmount, @@ -37,6 +36,7 @@ export const defaultEstimate = { estimate_date: moment(new Date()).format('YYYY-MM-DD'), expiration_date: moment(new Date()).format('YYYY-MM-DD'), estimate_number: '', + estimate_number_manually: '', delivered: '', reference: '', note: '', @@ -142,6 +142,8 @@ export const transfromsFormValuesToRequest = (values) => { ); return { ...omit(values, ['estimate_number_manually', 'estimate_number']), + // The `estimate_number_manually` will be presented just if the auto-increment + // is disable, always both attributes hold the same value in manual mode. ...(values.estimate_number_manually && { estimate_number: values.estimate_number, }), @@ -223,3 +225,17 @@ export const useEstimateIsForeignCustomer = () => { ); return isForeignCustomer; }; + +/** + * Resets the form values. + */ +export const resetFormState = ({ initialValues, values, resetForm }) => { + resetForm({ + values: { + // Reset the all values except the warehouse and brand id. + ...initialValues, + warehouse_id: values.warehouse_id, + brand_id: values.brand_id, + }, + }); +}; diff --git a/packages/webapp/src/containers/Sales/Invoices/InvoiceForm/InvoiceForm.tsx b/packages/webapp/src/containers/Sales/Invoices/InvoiceForm/InvoiceForm.tsx index 012604d28..983d1d001 100644 --- a/packages/webapp/src/containers/Sales/Invoices/InvoiceForm/InvoiceForm.tsx +++ b/packages/webapp/src/containers/Sales/Invoices/InvoiceForm/InvoiceForm.tsx @@ -42,7 +42,7 @@ function InvoiceForm({ // #withSettings invoiceNextNumber, invoiceNumberPrefix, - invoiceIncrementMode, + invoiceAutoIncrementMode, // #withCurrentOrganization organization: { base_currency }, @@ -71,7 +71,9 @@ function InvoiceForm({ ? { ...transformToEditForm(invoice) } : { ...defaultInvoice, - ...(invoiceIncrementMode && { + // If the auto-increment mode is enabled, take the next invoice + // number from the settings. + ...(invoiceAutoIncrementMode && { invoice_no: invoiceNumber, }), entries: orderingLinesIndexes(defaultInvoice.entries), @@ -184,7 +186,7 @@ export default compose( withSettings(({ invoiceSettings }) => ({ invoiceNextNumber: invoiceSettings?.nextNumber, invoiceNumberPrefix: invoiceSettings?.numberPrefix, - invoiceIncrementMode: invoiceSettings?.autoIncrement, + invoiceAutoIncrementMode: invoiceSettings?.autoIncrement, })), withCurrentOrganization(), )(InvoiceForm); diff --git a/packages/webapp/src/containers/Sales/Invoices/InvoiceForm/utils.tsx b/packages/webapp/src/containers/Sales/Invoices/InvoiceForm/utils.tsx index 9a8ed6f97..78c428ba6 100644 --- a/packages/webapp/src/containers/Sales/Invoices/InvoiceForm/utils.tsx +++ b/packages/webapp/src/containers/Sales/Invoices/InvoiceForm/utils.tsx @@ -154,7 +154,7 @@ export function transformValueToRequest(values) { ); return { ...omit(values, ['invoice_no', 'invoice_no_manually']), - // The `invoice_no_manually` will be presented just in case the auto-increment + // The `invoice_no_manually` will be presented just if the auto-increment // is disable, always both attributes hold the same value in manual mode. ...(values.invoice_no_manually && { invoice_no: values.invoice_no,