feat: make journal auto-adjustment for entries.

feat: auto-focus cells inside the table.
This commit is contained in:
a.bouhuolia
2021-01-27 15:02:12 +02:00
parent 81c59f8a1f
commit f8ee455985
9 changed files with 121 additions and 27 deletions

View File

@@ -2,11 +2,7 @@ import React, { useState, useMemo, useEffect, useCallback } from 'react';
import { Button } from '@blueprintjs/core';
import { FormattedMessage as T, useIntl } from 'react-intl';
import { omit } from 'lodash';
import classNames from 'classnames';
import { CLASSES } from 'common/classes';
import DataTable from 'components/DataTable';
import { compose, transformUpdatedRows, saveInvoke } from 'utils';
import { compose, saveInvoke } from 'utils';
import {
AccountsListFieldCell,
MoneyFieldCell,
@@ -21,9 +17,12 @@ import {
NoteCellRenderer,
} from './components';
import { DataTableEditable } from 'components';
import withAccounts from 'containers/Accounts/withAccounts';
import withCustomers from 'containers/Customers/withCustomers';
import { updateDataReducer } from './utils';
/**
* Make journal entries table component.
*/
@@ -126,8 +125,9 @@ function MakeJournalEntriesTable({
};
// Handles update datatable data.
const handleUpdateData = (rowIndex, columnIdOrObj, value) => {
const newRows = transformUpdatedRows(rows, rowIndex, columnIdOrObj, value);
const handleUpdateData = (rowIndex, columnId, value) => {
const newRows = updateDataReducer(rows, rowIndex, columnId, value);
saveInvoke(
onChange,
newRows
@@ -185,6 +185,7 @@ function MakeJournalEntriesTable({
contact_type: 'customer',
})),
],
autoFocus: ['account_id', 0],
}}
actions={
<>

View File

@@ -91,7 +91,6 @@ export default function MakeJournalFloatingAction({
<Button
disabled={isSubmitting}
intent={Intent.PRIMARY}
type="submit"
onClick={handleSubmitPublishBtnClick}
text={<T id={'save_publish'} />}
/>
@@ -123,7 +122,6 @@ export default function MakeJournalFloatingAction({
<Button
disabled={isSubmitting}
className={'ml1'}
type="submit"
onClick={handleSubmitDraftBtnClick}
text={<T id={'save_as_draft'} />}
/>
@@ -156,7 +154,6 @@ export default function MakeJournalFloatingAction({
<Button
disabled={isSubmitting}
intent={Intent.PRIMARY}
type="submit"
onClick={handleSubmitPublishBtnClick}
text={<T id={'save'} />}
/>

View File

@@ -1,8 +1,9 @@
import React from 'react';
import { Intent } from '@blueprintjs/core';
import { get, sumBy, setWith, toSafeInteger } from 'lodash';
import { AppToaster } from 'components';
import { formatMessage } from 'services/intl';
import { setWith } from 'lodash';
import { transformUpdatedRows } from 'utils';
const ERROR = {
JOURNAL_NUMBER_ALREADY_EXISTS: 'JOURNAL.NUMBER.ALREADY.EXISTS',
@@ -15,6 +16,46 @@ const ERROR = {
ENTRIES_SHOULD_ASSIGN_WITH_CONTACT: 'ENTRIES_SHOULD_ASSIGN_WITH_CONTACT',
};
function adjustmentEntries(entries) {
const credit = sumBy(entries, e => toSafeInteger(e.credit));
const debit = sumBy(entries, e => toSafeInteger(e.debit));
return {
debit: Math.max(credit - debit, 0),
credit: Math.max(debit - credit, 0),
}
}
export const updateDataReducer = (rows, rowIndex, columnId, value) => {
let newRows = transformUpdatedRows(rows, rowIndex, columnId, value);
const oldCredit = get(rows, `[${rowIndex}].credit`);
const oldDebit = get(rows, `[${rowIndex}].debit`);
if (columnId === 'account_id' && !oldCredit && !oldDebit) {
const adjustment = adjustmentEntries(rows);
if (adjustment.credit) {
newRows = transformUpdatedRows(
newRows,
rowIndex,
'credit',
adjustment.credit,
);
}
if (adjustment.debit) {
newRows = transformUpdatedRows(
newRows,
rowIndex,
'debit',
adjustment.debit,
);
}
}
return newRows;
};
// Transform API errors in toasts messages.
export const transformErrors = (resErrors, { setErrors, errors }) => {
const getError = (errorType) => resErrors.find((e) => e.type === errorType);