mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 14:20:31 +00:00
feat(expenses): filter expenses accounts.
This commit is contained in:
@@ -3,7 +3,7 @@ import { MenuItem, Button } from '@blueprintjs/core';
|
|||||||
import { Select } from '@blueprintjs/select';
|
import { Select } from '@blueprintjs/select';
|
||||||
import { FormattedMessage as T } from 'react-intl';
|
import { FormattedMessage as T } from 'react-intl';
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import { isEmpty } from 'lodash';
|
import { filterAccountsByQuery } from './utils';
|
||||||
import { CLASSES } from 'common/classes';
|
import { CLASSES } from 'common/classes';
|
||||||
|
|
||||||
export default function AccountsSelectList({
|
export default function AccountsSelectList({
|
||||||
@@ -14,32 +14,30 @@ export default function AccountsSelectList({
|
|||||||
onAccountSelected,
|
onAccountSelected,
|
||||||
disabled = false,
|
disabled = false,
|
||||||
popoverFill = false,
|
popoverFill = false,
|
||||||
filterByParentTypes = [],
|
|
||||||
filterByTypes = [],
|
filterByParentTypes,
|
||||||
|
filterByTypes,
|
||||||
filterByNormal,
|
filterByNormal,
|
||||||
buttonProps = {}
|
filterByRootTypes,
|
||||||
|
|
||||||
|
buttonProps = {},
|
||||||
}) {
|
}) {
|
||||||
// Filters accounts based on filter props.
|
// Filters accounts based on filter props.
|
||||||
const filteredAccounts = useMemo(() => {
|
const filteredAccounts = useMemo(() => {
|
||||||
let filteredAccounts = [...accounts];
|
let filteredAccounts = filterAccountsByQuery(accounts, {
|
||||||
|
filterByRootTypes,
|
||||||
if (!isEmpty(filterByParentTypes)) {
|
filterByParentTypes,
|
||||||
filteredAccounts = filteredAccounts.filter(
|
filterByTypes,
|
||||||
(account) => filterByParentTypes.indexOf(account.account_parent_type) !== -1,
|
filterByNormal,
|
||||||
);
|
});
|
||||||
}
|
|
||||||
if (!isEmpty(filterByTypes)) {
|
|
||||||
filteredAccounts = filteredAccounts.filter(
|
|
||||||
(account) => filterByTypes.indexOf(account.account_type) !== -1,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (!isEmpty(filterByNormal)) {
|
|
||||||
filteredAccounts = filteredAccounts.filter(
|
|
||||||
(account) => filterByTypes.indexOf(account.account_normal) === filterByNormal,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return filteredAccounts;
|
return filteredAccounts;
|
||||||
}, [accounts, filterByParentTypes, filterByTypes, filterByNormal]);
|
}, [
|
||||||
|
accounts,
|
||||||
|
filterByRootTypes,
|
||||||
|
filterByParentTypes,
|
||||||
|
filterByTypes,
|
||||||
|
filterByNormal,
|
||||||
|
]);
|
||||||
|
|
||||||
// Find initial account object to set it as default account in initial render.
|
// Find initial account object to set it as default account in initial render.
|
||||||
const initialAccount = useMemo(
|
const initialAccount = useMemo(
|
||||||
@@ -103,7 +101,11 @@ export default function AccountsSelectList({
|
|||||||
noResults={<MenuItem disabled={true} text={<T id={'no_accounts'} />} />}
|
noResults={<MenuItem disabled={true} text={<T id={'no_accounts'} />} />}
|
||||||
itemRenderer={accountItem}
|
itemRenderer={accountItem}
|
||||||
itemPredicate={filterAccountsPredicater}
|
itemPredicate={filterAccountsPredicater}
|
||||||
popoverProps={{ minimal: true, usePortal: !popoverFill, inline: popoverFill }}
|
popoverProps={{
|
||||||
|
minimal: true,
|
||||||
|
usePortal: !popoverFill,
|
||||||
|
inline: popoverFill,
|
||||||
|
}}
|
||||||
filterable={true}
|
filterable={true}
|
||||||
onItemSelect={onAccountSelect}
|
onItemSelect={onAccountSelect}
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
|
|||||||
@@ -1,48 +1,47 @@
|
|||||||
import React, { useState, useCallback, useEffect, useMemo } from 'react';
|
import React, { useState, useCallback, useEffect, useMemo } from 'react';
|
||||||
import { MenuItem } from '@blueprintjs/core';
|
import { MenuItem } from '@blueprintjs/core';
|
||||||
import { Suggest } from '@blueprintjs/select';
|
import { Suggest } from '@blueprintjs/select';
|
||||||
import { isEmpty } from 'lodash';
|
|
||||||
|
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import { CLASSES } from 'common/classes';
|
import { CLASSES } from 'common/classes';
|
||||||
|
|
||||||
import { FormattedMessage as T } from 'react-intl';
|
import { FormattedMessage as T } from 'react-intl';
|
||||||
|
import { filterAccountsByQuery } from './utils';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Accounts suggest field.
|
||||||
|
*/
|
||||||
export default function AccountsSuggestField({
|
export default function AccountsSuggestField({
|
||||||
accounts,
|
accounts,
|
||||||
initialAccountId,
|
initialAccountId,
|
||||||
selectedAccountId,
|
selectedAccountId,
|
||||||
defaultSelectText = 'Select account',
|
defaultSelectText = 'Select account',
|
||||||
|
popoverFill = false,
|
||||||
onAccountSelected,
|
onAccountSelected,
|
||||||
|
|
||||||
filterByParentTypes = [],
|
filterByParentTypes = [],
|
||||||
filterByTypes = [],
|
filterByTypes = [],
|
||||||
filterByNormal,
|
filterByNormal,
|
||||||
popoverFill = false,
|
filterByRootTypes = [],
|
||||||
|
|
||||||
...suggestProps
|
...suggestProps
|
||||||
}) {
|
}) {
|
||||||
// Filters accounts based on filter props.
|
// Filters accounts based on filter props.
|
||||||
const filteredAccounts = useMemo(() => {
|
const filteredAccounts = useMemo(() => {
|
||||||
let filteredAccounts = [...accounts];
|
let filteredAccounts = filterAccountsByQuery(accounts, {
|
||||||
|
filterByRootTypes,
|
||||||
if (!isEmpty(filterByParentTypes)) {
|
filterByParentTypes,
|
||||||
filteredAccounts = filteredAccounts.filter(
|
filterByTypes,
|
||||||
(account) => filterByParentTypes.indexOf(account.account_parent_type) !== -1,
|
filterByNormal,
|
||||||
);
|
});
|
||||||
}
|
|
||||||
if (!isEmpty(filterByTypes)) {
|
|
||||||
filteredAccounts = filteredAccounts.filter(
|
|
||||||
(account) => filterByTypes.indexOf(account.account_type) !== -1,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (!isEmpty(filterByNormal)) {
|
|
||||||
filteredAccounts = filteredAccounts.filter(
|
|
||||||
(account) => filterByTypes.indexOf(account.account_normal) === filterByNormal,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return filteredAccounts;
|
return filteredAccounts;
|
||||||
}, [accounts, filterByParentTypes, filterByTypes, filterByNormal]);
|
}, [
|
||||||
|
accounts,
|
||||||
|
filterByRootTypes,
|
||||||
|
filterByParentTypes,
|
||||||
|
filterByTypes,
|
||||||
|
filterByNormal,
|
||||||
|
]);
|
||||||
|
|
||||||
// Find initial account object to set it as default account in initial render.
|
// Find initial account object to set it as default account in initial render.
|
||||||
const initialAccount = useMemo(
|
const initialAccount = useMemo(
|
||||||
|
|||||||
@@ -7,12 +7,14 @@ import AccountsSuggestField from 'components/AccountsSuggestField';
|
|||||||
// import AccountsSelectList from 'components/AccountsSelectList';
|
// import AccountsSelectList from 'components/AccountsSelectList';
|
||||||
import { FormGroup, Classes, Intent } from '@blueprintjs/core';
|
import { FormGroup, Classes, Intent } from '@blueprintjs/core';
|
||||||
|
|
||||||
// Account cell renderer.
|
/**
|
||||||
|
* Account cell renderer.
|
||||||
|
*/
|
||||||
export default function AccountCellRenderer({
|
export default function AccountCellRenderer({
|
||||||
column: {
|
column: {
|
||||||
id,
|
id,
|
||||||
accountsDataProp,
|
accountsDataProp,
|
||||||
filterAccountsByRootType,
|
filterAccountsByRootTypes,
|
||||||
filterAccountsByTypes,
|
filterAccountsByTypes,
|
||||||
},
|
},
|
||||||
row: { index, original },
|
row: { index, original },
|
||||||
@@ -55,7 +57,7 @@ export default function AccountCellRenderer({
|
|||||||
accounts={accounts}
|
accounts={accounts}
|
||||||
onAccountSelected={handleAccountSelected}
|
onAccountSelected={handleAccountSelected}
|
||||||
selectedAccountId={initialValue}
|
selectedAccountId={initialValue}
|
||||||
filterByRootTypes={filterAccountsByRootType}
|
filterByRootTypes={filterAccountsByRootTypes}
|
||||||
filterByTypes={filterAccountsByTypes}
|
filterByTypes={filterAccountsByTypes}
|
||||||
inputProps={{
|
inputProps={{
|
||||||
inputRef: (ref) => (accountRef.current = ref),
|
inputRef: (ref) => (accountRef.current = ref),
|
||||||
|
|||||||
46
client/src/components/utils.js
Normal file
46
client/src/components/utils.js
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
import { castArray, isEmpty, includes, pickBy } from 'lodash';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filters the given accounts of the given query.
|
||||||
|
* @param {*} accounts
|
||||||
|
* @param {*} queryProps
|
||||||
|
* @returns {*}
|
||||||
|
*/
|
||||||
|
export const filterAccountsByQuery = (accounts, queryProps) => {
|
||||||
|
const defaultQuery = {
|
||||||
|
filterByParentTypes: [],
|
||||||
|
filterByTypes: [],
|
||||||
|
filterByNormal: [],
|
||||||
|
filterByRootTypes: [],
|
||||||
|
...pickBy(queryProps, v => v !== undefined),
|
||||||
|
};
|
||||||
|
const query = {
|
||||||
|
filterByParentTypes: castArray(defaultQuery.filterByParentTypes),
|
||||||
|
filterByTypes: castArray(defaultQuery.filterByTypes),
|
||||||
|
filterByNormal: castArray(defaultQuery.filterByNormal),
|
||||||
|
filterByRootTypes: castArray(defaultQuery.filterByRootTypes),
|
||||||
|
};
|
||||||
|
let filteredAccounts = [...accounts];
|
||||||
|
|
||||||
|
if (!isEmpty(query.filterByParentTypes)) {
|
||||||
|
filteredAccounts = filteredAccounts.filter((account) =>
|
||||||
|
includes(query.filterByParentTypes, account.account_parent_type),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (!isEmpty(query.filterByTypes)) {
|
||||||
|
filteredAccounts = filteredAccounts.filter((account) =>
|
||||||
|
includes(query.filterByTypes, account.account_type),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (!isEmpty(query.filterByNormal)) {
|
||||||
|
filteredAccounts = filteredAccounts.filter((account) =>
|
||||||
|
includes(query.filterByTypes, account.account_normal),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (!isEmpty(query.filterByRootTypes)) {
|
||||||
|
filteredAccounts = filteredAccounts.filter((account) =>
|
||||||
|
includes(query.filterByRootTypes, account.account_root_type),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return filteredAccounts;
|
||||||
|
};
|
||||||
@@ -32,7 +32,9 @@ export default function ExpenseFormEntriesTable({
|
|||||||
const handleUpdateData = useCallback(
|
const handleUpdateData = useCallback(
|
||||||
(rowIndex, columnId, value) => {
|
(rowIndex, columnId, value) => {
|
||||||
const newRows = compose(
|
const newRows = compose(
|
||||||
|
// Update auto-adding new line.
|
||||||
updateAutoAddNewLine(defaultEntry, ['expense_account_id']),
|
updateAutoAddNewLine(defaultEntry, ['expense_account_id']),
|
||||||
|
// Update the row value of the given row index and column id.
|
||||||
updateTableRow(rowIndex, columnId, value),
|
updateTableRow(rowIndex, columnId, value),
|
||||||
)(entries);
|
)(entries);
|
||||||
|
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ export function useExpenseFormTableColumns() {
|
|||||||
className: 'expense_account_id',
|
className: 'expense_account_id',
|
||||||
disableSortBy: true,
|
disableSortBy: true,
|
||||||
width: 40,
|
width: 40,
|
||||||
filterAccountsByRootType: ['expense'],
|
filterAccountsByRootTypes: ['expense'],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Header: formatMessage({ id: 'amount_currency' }, { currency: 'USD' }),
|
Header: formatMessage({ id: 'amount_currency' }, { currency: 'USD' }),
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
// Blueprint framework.
|
// Blueprint framework.
|
||||||
// @import '@blueprintjs/core/src/blueprint.scss';
|
// @import '@blueprintjs/core/src/blueprint.scss';
|
||||||
|
|
||||||
@@ -6,15 +5,12 @@
|
|||||||
.bigcapital-datatable {
|
.bigcapital-datatable {
|
||||||
.table {
|
.table {
|
||||||
.tbody {
|
.tbody {
|
||||||
|
.td.amount {
|
||||||
|
|
||||||
.td.total_amount{
|
|
||||||
span {
|
span {
|
||||||
font-weight: 500;
|
font-weight: 600;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
import _ from 'lodash';
|
import _, { castArray } from 'lodash';
|
||||||
import { Intent } from '@blueprintjs/core';
|
import { Intent } from '@blueprintjs/core';
|
||||||
import Currency from 'js-money/lib/currency';
|
import Currency from 'js-money/lib/currency';
|
||||||
import accounting from 'accounting';
|
import accounting from 'accounting';
|
||||||
@@ -92,7 +92,6 @@ export const compose = (...funcs) =>
|
|||||||
(arg) => arg,
|
(arg) => arg,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
export const getObjectDiff = (a, b) => {
|
export const getObjectDiff = (a, b) => {
|
||||||
return _.reduce(
|
return _.reduce(
|
||||||
a,
|
a,
|
||||||
@@ -158,9 +157,9 @@ export function formattedAmount(cents, currency, props) {
|
|||||||
symbol,
|
symbol,
|
||||||
precision: 0,
|
precision: 0,
|
||||||
format: {
|
format: {
|
||||||
pos : "%s%v",
|
pos: '%s%v',
|
||||||
neg : "%s%v",
|
neg: '%s%v',
|
||||||
zero: parsedProps.noZero ? "" : '%s%v'
|
zero: parsedProps.noZero ? '' : '%s%v',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -384,7 +383,7 @@ export function defaultToTransform(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function isBlank(value) {
|
export function isBlank(value) {
|
||||||
return _.isEmpty(value) && !_.isNumber(value) || _.isNaN(value);
|
return (_.isEmpty(value) && !_.isNumber(value)) || _.isNaN(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getColumnWidth = (
|
export const getColumnWidth = (
|
||||||
@@ -403,15 +402,12 @@ export const getColumnWidth = (
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getForceWidth = (
|
export const getForceWidth = (text, magicSpacing = 14) => {
|
||||||
text,
|
|
||||||
magicSpacing = 14,
|
|
||||||
) => {
|
|
||||||
const textLength = text.length;
|
const textLength = text.length;
|
||||||
const result = textLength * magicSpacing
|
const result = textLength * magicSpacing;
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
};
|
||||||
|
|
||||||
export const toSafeNumber = (number) => {
|
export const toSafeNumber = (number) => {
|
||||||
return _.toNumber(_.defaultTo(number, 0));
|
return _.toNumber(_.defaultTo(number, 0));
|
||||||
@@ -436,7 +432,7 @@ export function flatObject(obj) {
|
|||||||
function dig(obj) {
|
function dig(obj) {
|
||||||
if (obj !== Object(obj))
|
if (obj !== Object(obj))
|
||||||
/*is primitive, end of path*/
|
/*is primitive, end of path*/
|
||||||
return flatObject[path.join('.')] = obj; /*<- value*/
|
return (flatObject[path.join('.')] = obj); /*<- value*/
|
||||||
|
|
||||||
//no? so this is an object with keys. go deeper on each key down
|
//no? so this is an object with keys. go deeper on each key down
|
||||||
for (let key in obj) {
|
for (let key in obj) {
|
||||||
@@ -450,7 +446,6 @@ export function flatObject(obj) {
|
|||||||
return flatObject;
|
return flatObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export function randomNumber(min, max) {
|
export function randomNumber(min, max) {
|
||||||
if (min > max) {
|
if (min > max) {
|
||||||
let temp = max;
|
let temp = max;
|
||||||
@@ -464,7 +459,6 @@ export function randomNumber(min, max) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export function transformResponse(response) {
|
export function transformResponse(response) {
|
||||||
return transformToCamelCase(response);
|
return transformToCamelCase(response);
|
||||||
}
|
}
|
||||||
@@ -479,7 +473,6 @@ export function transactionNumber(prefix, number) {
|
|||||||
codes.push(number);
|
codes.push(number);
|
||||||
}
|
}
|
||||||
return codes.join('-');
|
return codes.join('-');
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function safeCallback(callback, ...args) {
|
export function safeCallback(callback, ...args) {
|
||||||
@@ -488,7 +481,7 @@ export function safeCallback(callback, ...args) {
|
|||||||
|
|
||||||
export const createDeepEqualSelector = createSelectorCreator(
|
export const createDeepEqualSelector = createSelectorCreator(
|
||||||
defaultMemoize,
|
defaultMemoize,
|
||||||
isEqual
|
isEqual,
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -499,8 +492,8 @@ export const isTableEmptyStatus = ({ data, pagination, filterMeta }) => {
|
|||||||
_.isEmpty(data),
|
_.isEmpty(data),
|
||||||
_.isEmpty(filterMeta.view),
|
_.isEmpty(filterMeta.view),
|
||||||
pagination.page === 1,
|
pagination.page === 1,
|
||||||
].every(cond => cond === true)
|
].every((cond) => cond === true);
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transformes the pagination meta to table props.
|
* Transformes the pagination meta to table props.
|
||||||
@@ -559,23 +552,19 @@ export function removeRowsByIndex(rows, rowIndex) {
|
|||||||
return newRows;
|
return newRows;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export function safeSumBy(entries, getter) {
|
export function safeSumBy(entries, getter) {
|
||||||
return _.chain(entries)
|
return _.chain(entries)
|
||||||
.map(row => toSafeNumber(_.get(row, getter)))
|
.map((row) => toSafeNumber(_.get(row, getter)))
|
||||||
.sum()
|
.sum()
|
||||||
.value();
|
.value();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export const fullAmountPaymentEntries = (entries) => {
|
export const fullAmountPaymentEntries = (entries) => {
|
||||||
return entries.map((item) => ({
|
return entries.map((item) => ({
|
||||||
...item,
|
...item,
|
||||||
payment_amount: item.due_amount,
|
payment_amount: item.due_amount,
|
||||||
}));
|
}));
|
||||||
}
|
};
|
||||||
|
|
||||||
|
|
||||||
export const amountPaymentEntries = (amount, entries) => {
|
export const amountPaymentEntries = (amount, entries) => {
|
||||||
let total = amount;
|
let total = amount;
|
||||||
@@ -609,6 +598,7 @@ export const updateMinEntriesLines = (min, defaultEntry) => (lines) => {
|
|||||||
const diffLines = Math.max(min - lines.length, 0);
|
const diffLines = Math.max(min - lines.length, 0);
|
||||||
return [...lines, ...repeatValue(defaultEntry, diffLines)];
|
return [...lines, ...repeatValue(defaultEntry, diffLines)];
|
||||||
}
|
}
|
||||||
|
return lines;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const updateRemoveLineByIndex = (rowIndex) => (entries) => {
|
export const updateRemoveLineByIndex = (rowIndex) => (entries) => {
|
||||||
@@ -622,8 +612,8 @@ export const updateTableRow = (rowIndex, columnId, value) => (old) => {
|
|||||||
return {
|
return {
|
||||||
...old[rowIndex],
|
...old[rowIndex],
|
||||||
[columnId]: value,
|
[columnId]: value,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
return row
|
return row;
|
||||||
})
|
});
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user