mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 20:30:33 +00:00
feat: add auto increment in vendor credit number.
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
import React from 'react';
|
||||
import intl from 'react-intl-universal';
|
||||
import { useSaveSettings } from 'hooks/query';
|
||||
|
||||
import { VendorCreditNumberDilaogProvider } from './VendorCreditNumberDilaogProvider';
|
||||
import ReferenceNumberForm from 'containers/JournalNumber/ReferenceNumberForm';
|
||||
|
||||
import withDialogActions from 'containers/Dialog/withDialogActions';
|
||||
import withSettings from 'containers/Settings/withSettings';
|
||||
import withSettingsActions from 'containers/Settings/withSettingsActions';
|
||||
import { compose } from 'utils';
|
||||
import {
|
||||
transformFormToSettings,
|
||||
transformSettingsToForm,
|
||||
} from 'containers/JournalNumber/utils';
|
||||
|
||||
/**
|
||||
* Vendor credit number dialog
|
||||
*/
|
||||
function VendorCreditNumberDialogContent({
|
||||
// #ownProps
|
||||
initialValues,
|
||||
onConfirm,
|
||||
|
||||
// #withSettings
|
||||
nextNumber,
|
||||
numberPrefix,
|
||||
autoIncrement,
|
||||
|
||||
// #withDialogActions
|
||||
closeDialog,
|
||||
}) {
|
||||
const { mutateAsync: saveSettings } = useSaveSettings();
|
||||
const [referenceFormValues, setReferenceFormValues] = React.useState(null);
|
||||
|
||||
// Handle the submit form.
|
||||
const handleSubmitForm = (values, { setSubmitting }) => {
|
||||
// Handle the form success.
|
||||
const handleSuccess = () => {
|
||||
setSubmitting(false);
|
||||
closeDialog('vendor-credit-form');
|
||||
onConfirm(values);
|
||||
};
|
||||
// Handle the form errors.
|
||||
const handleErrors = () => {
|
||||
setSubmitting(false);
|
||||
};
|
||||
if (values.incrementMode === 'manual-transaction') {
|
||||
handleSuccess();
|
||||
return;
|
||||
}
|
||||
// Transformes the form values to settings to save it.
|
||||
const options = transformFormToSettings(values, 'vendor_credit');
|
||||
|
||||
// Save the settings.
|
||||
saveSettings({ options }).then(handleSuccess).catch(handleErrors);
|
||||
};
|
||||
|
||||
// Handle the dialog close.
|
||||
const handleClose = () => {
|
||||
closeDialog('vendor-credit-form');
|
||||
};
|
||||
// Handle form change.
|
||||
const handleChange = (values) => {
|
||||
setReferenceFormValues(values);
|
||||
};
|
||||
|
||||
// Description.
|
||||
const description =
|
||||
referenceFormValues?.incrementMode === 'auto'
|
||||
? intl.get('vendor_credit.auto_increment.auto')
|
||||
: intl.get('vendor_credit.auto_increment.manually');
|
||||
|
||||
return (
|
||||
<VendorCreditNumberDilaogProvider>
|
||||
<ReferenceNumberForm
|
||||
initialValues={{
|
||||
...transformSettingsToForm({
|
||||
nextNumber,
|
||||
numberPrefix,
|
||||
autoIncrement,
|
||||
}),
|
||||
...initialValues,
|
||||
}}
|
||||
description={description}
|
||||
onSubmit={handleSubmitForm}
|
||||
onClose={handleClose}
|
||||
onChange={handleChange}
|
||||
/>
|
||||
</VendorCreditNumberDilaogProvider>
|
||||
);
|
||||
}
|
||||
|
||||
export default compose(
|
||||
withDialogActions,
|
||||
withSettingsActions,
|
||||
withSettings(({ vendorsCreditNoteSetting }) => ({
|
||||
autoIncrement: vendorsCreditNoteSetting?.autoIncrement,
|
||||
nextNumber: vendorsCreditNoteSetting?.nextNumber,
|
||||
numberPrefix: vendorsCreditNoteSetting?.numberPrefix,
|
||||
})),
|
||||
)(VendorCreditNumberDialogContent);
|
||||
@@ -0,0 +1,28 @@
|
||||
import React from 'react';
|
||||
import { DialogContent } from 'components';
|
||||
import { useSettingsVendorCredits } from 'hooks/query';
|
||||
|
||||
const VendorCreditNumberDialogContext = React.createContext();
|
||||
|
||||
/**
|
||||
* Vendor credit number dialog provider
|
||||
*/
|
||||
function VendorCreditNumberDilaogProvider({ query, ...props }) {
|
||||
const { isLoading: isSettingsLoading } = useSettingsVendorCredits();
|
||||
|
||||
// Provider payload.
|
||||
const provider = {
|
||||
isSettingsLoading,
|
||||
};
|
||||
|
||||
return (
|
||||
<DialogContent isLoading={isSettingsLoading}>
|
||||
<VendorCreditNumberDialogContext.Provider value={provider} {...props} />
|
||||
</DialogContent>
|
||||
);
|
||||
}
|
||||
|
||||
const useVendorCreditNumberDialogContext = () =>
|
||||
React.useContext(VendorCreditNumberDialogContext);
|
||||
|
||||
export { VendorCreditNumberDilaogProvider, useVendorCreditNumberDialogContext };
|
||||
40
src/containers/Dialogs/VendorCreditNumberDialog/index.js
Normal file
40
src/containers/Dialogs/VendorCreditNumberDialog/index.js
Normal file
@@ -0,0 +1,40 @@
|
||||
import React from 'react';
|
||||
import { Dialog, DialogSuspense, FormattedMessage as T } from 'components';
|
||||
import withDialogRedux from 'components/DialogReduxConnect';
|
||||
import { compose, saveInvoke } from 'utils';
|
||||
|
||||
const VendorCreditNumberDialogContent = React.lazy(() =>
|
||||
import('./VendorCreditNumberDialogContent'),
|
||||
);
|
||||
|
||||
/**
|
||||
* Vendor Credit number dialog.
|
||||
*/
|
||||
function VendorCreditNumberDialog({
|
||||
dialogName,
|
||||
payload: { initialFormValues },
|
||||
isOpen,
|
||||
onConfirm,
|
||||
}) {
|
||||
const handleConfirm = (values) => {
|
||||
saveInvoke(onConfirm, values);
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog
|
||||
title={<T id={'vendor_credit_number_settings'} />}
|
||||
name={dialogName}
|
||||
autoFocus={true}
|
||||
canEscapeKeyClose={true}
|
||||
isOpen={isOpen}
|
||||
>
|
||||
<DialogSuspense>
|
||||
<VendorCreditNumberDialogContent
|
||||
initialValues={{ ...initialFormValues }}
|
||||
onConfirm={handleConfirm}
|
||||
/>
|
||||
</DialogSuspense>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
export default compose(withDialogRedux())(VendorCreditNumberDialog);
|
||||
Reference in New Issue
Block a user