This commit is contained in:
elforjani3
2021-09-13 18:37:48 +02:00
17 changed files with 188 additions and 111 deletions

View File

@@ -6,7 +6,7 @@ import {
updateMinEntriesLines,
updateRemoveLineByIndex,
updateAutoAddNewLine,
updateTableRow,
updateTableCell,
} from 'utils';
import { useMakeJournalFormContext } from './MakeJournalProvider';
import { useJournalTableEntriesColumns } from './components';
@@ -38,7 +38,7 @@ export default function MakeJournalEntriesTable({
// Update items entries total.
updateAdjustEntries(rowIndex, columnId, value),
// Update entry of the given row index and column id.
updateTableRow(rowIndex, columnId, value),
updateTableCell(rowIndex, columnId, value),
)(entries);
saveInvoke(onChange, newRows);

View File

@@ -5,7 +5,7 @@ import moment from 'moment';
import * as R from 'ramda';
import {
transactionNumber,
updateTableRow,
updateTableCell,
repeatValue,
transformToForm,
defaultFastFieldShouldUpdate,
@@ -100,10 +100,10 @@ export const updateAdjustEntries = (rowIndex, columnId, value) => (rows) => {
const adjustment = adjustmentEntries(rows);
if (adjustment.credit) {
newRows = updateTableRow(rowIndex, 'credit', adjustment.credit)(newRows);
newRows = updateTableCell(rowIndex, 'credit', adjustment.credit)(newRows);
}
if (adjustment.debit) {
newRows = updateTableRow(rowIndex, 'debit', adjustment.debit)(newRows);
newRows = updateTableCell(rowIndex, 'debit', adjustment.debit)(newRows);
}
}
return newRows;

View File

@@ -1,7 +1,7 @@
import React from 'react';
import intl from 'react-intl-universal';
import { MoneyFieldCell, DataTableEditable } from 'components';
import { compose, updateTableRow } from 'utils';
import { compose, updateTableCell } from 'utils';
/**
* Allocate landed cost entries table.
@@ -51,7 +51,7 @@ export default function AllocateLandedCostEntriesTable({
// Handle update data.
const handleUpdateData = React.useCallback(
(rowIndex, columnId, value) => {
const newRows = compose(updateTableRow(rowIndex, columnId, value))(
const newRows = compose(updateTableCell(rowIndex, columnId, value))(
entries,
);
onUpdateData(newRows);

View File

@@ -1,6 +1,5 @@
import React, { useEffect, useCallback } from 'react';
import classNames from 'classnames';
import { useItem } from 'hooks/query';
import { CLASSES } from 'common/classes';
import { DataTableEditable } from 'components';
@@ -9,12 +8,13 @@ import { useEditableItemsEntriesColumns } from './components';
import {
saveInvoke,
compose,
updateTableRow,
updateTableCell,
updateMinEntriesLines,
updateAutoAddNewLine,
updateRemoveLineByIndex,
} from 'utils';
import { updateItemsEntriesTotal, ITEM_TYPE } from './utils';
import { updateItemsEntriesTotal, useFetchItemRow } from './utils';
import { updateTableRow } from '../../utils';
/**
* Items entries table.
@@ -30,62 +30,9 @@ function ItemsEntriesTable({
linesNumber,
currencyCode,
itemType, // sellable or purchasable
landedCost = false
landedCost = false,
}) {
const [rows, setRows] = React.useState(initialEntries);
const [rowItem, setRowItem] = React.useState(null);
const [cellsLoading, setCellsLoading] = React.useState(null);
// Fetches the item details.
const {
data: item,
isFetching: isItemFetching,
isSuccess: isItemSuccess,
} = useItem(rowItem && rowItem.itemId, {
enabled: !!(rowItem && rowItem.itemId),
});
// Once the item start loading give the table cells loading state.
useEffect(() => {
if (rowItem && isItemFetching) {
setCellsLoading([
[rowItem.rowIndex, 'rate'],
[rowItem.rowIndex, 'description'],
[rowItem.rowIndex, 'quantity'],
[rowItem.rowIndex, 'discount'],
]);
} else {
setCellsLoading(null);
}
}, [isItemFetching, setCellsLoading, rowItem]);
// Once the item selected and fetched set the initial details to the table.
useEffect(() => {
if (isItemSuccess && item && rowItem) {
const { rowIndex } = rowItem;
const price =
itemType === ITEM_TYPE.PURCHASABLE
? item.cost_price
: item.sell_price;
const description =
itemType === ITEM_TYPE.PURCHASABLE
? item.cost_description
: item.sell_description;
// Update the rate, description and quantity data of the row.
const newRows = compose(
updateItemsEntriesTotal,
updateTableRow(rowIndex, 'rate', price),
updateTableRow(rowIndex, 'description', description),
updateTableRow(rowIndex, 'quantity', 1),
)(rows);
setRows(newRows);
setRowItem(null);
saveInvoke(onUpdateData, newRows);
}
}, [item, rowItem, rows, itemType, onUpdateData, isItemSuccess]);
// Allows to observes `entries` to make table rows outside controlled.
useEffect(() => {
@@ -97,22 +44,38 @@ function ItemsEntriesTable({
// Editiable items entries columns.
const columns = useEditableItemsEntriesColumns({ landedCost });
// Handles the editor data update.
const handleUpdateData = useCallback(
(rowIndex, columnId, value) => {
if (columnId === 'item_id') {
setRowItem({ rowIndex, columnId, itemId: value });
}
// Handle the fetch item row details.
const { setItemRow, cellsLoading, isItemFetching } = useFetchItemRow({
landedCost,
itemType,
notifyNewRow: (newRow, rowIndex) => {
// Update the rate, description and quantity data of the row.
const newRows = compose(
updateAutoAddNewLine(defaultEntry, ['item_id']),
updateItemsEntriesTotal,
updateTableRow(rowIndex, columnId, value),
updateTableRow(rowIndex, newRow),
)(rows);
setRows(newRows);
onUpdateData(newRows);
},
[rows, defaultEntry, onUpdateData],
});
// Handles the editor data update.
const handleUpdateData = useCallback(
(rowIndex, columnId, value) => {
if (columnId === 'item_id') {
setItemRow({ rowIndex, columnId, itemId: value });
}
const newRows = compose(
updateAutoAddNewLine(defaultEntry, ['item_id']),
updateItemsEntriesTotal,
updateTableCell(rowIndex, columnId, value),
)(rows);
setRows(newRows);
onUpdateData(newRows);
},
[rows, defaultEntry, onUpdateData, setItemRow],
);
// Handle table rows removing by index.

View File

@@ -179,6 +179,7 @@ export function useEditableItemsEntriesColumns({ landedCost }) {
accessor: 'landed_cost',
Cell: CheckBoxFieldCell,
width: 100,
disabledAccessor: 'landed_cost_disabled',
disableSortBy: true,
disableResizing: true,
className: 'landed-cost',

View File

@@ -1,6 +1,9 @@
import React from 'react';
import * as R from 'ramda';
import { sumBy, isEmpty, last } from 'lodash';
import * as R from 'ramda';
import { toSafeNumber } from 'utils';
import { useItem } from 'hooks/query';
import { toSafeNumber, saveInvoke } from 'utils';
/**
* Retrieve item entry total from the given rate, quantity and discount.
@@ -52,4 +55,79 @@ export const ensureEntriesHaveEmptyLine = R.curry((defaultEntry, entries) => {
return [...entries, defaultEntry];
}
return entries;
});
});
export const isLandedCostDisabled = (item) =>
['service', 'non-inventory'].indexOf(item.type) !== -1;
/**
* Handle fetch item row details and retrieves the new table row.
*/
export function useFetchItemRow({ landedCost, itemType, notifyNewRow }) {
const [itemRow, setItemRow] = React.useState(null);
const [cellsLoading, setCellsLoading] = React.useState(null);
// Fetches the item details.
const {
data: item,
isFetching: isItemFetching,
isSuccess: isItemSuccess,
} = useItem(itemRow && itemRow.itemId, {
enabled: !!(itemRow && itemRow.itemId),
});
// Once the item start loading give the table cells loading state.
React.useEffect(() => {
if (itemRow && isItemFetching) {
setCellsLoading([
[itemRow.rowIndex, 'rate'],
[itemRow.rowIndex, 'description'],
[itemRow.rowIndex, 'quantity'],
[itemRow.rowIndex, 'discount'],
]);
} else {
setCellsLoading(null);
}
}, [isItemFetching, setCellsLoading, itemRow]);
// Once the item selected and fetched set the initial details to the table.
React.useEffect(() => {
if (isItemSuccess && item && itemRow) {
const { rowIndex } = itemRow;
const price =
itemType === ITEM_TYPE.PURCHASABLE ? item.cost_price : item.sell_price;
const description =
itemType === ITEM_TYPE.PURCHASABLE
? item.cost_description
: item.sell_description;
// Detarmines whether the landed cost checkbox should be disabled.
const landedCostDisabled = isLandedCostDisabled(item);
// The new row.
const newRow = {
rate: price,
description,
quantity: 1,
...(landedCost
? {
landed_cost: false,
landed_cost_disabled: landedCostDisabled,
}
: {}),
};
setItemRow(null);
saveInvoke(notifyNewRow, newRow, rowIndex);
}
}, [item, itemRow, itemType, isItemSuccess, landedCost, notifyNewRow]);
return {
isItemFetching,
isItemSuccess,
item,
setItemRow,
itemRow,
cellsLoading,
};
}

View File

@@ -6,7 +6,7 @@ import { useExpenseFormTableColumns } from './components';
import {
saveInvoke,
compose,
updateTableRow,
updateTableCell,
updateMinEntriesLines,
updateAutoAddNewLine,
updateRemoveLineByIndex,
@@ -38,7 +38,7 @@ export default function ExpenseFormEntriesTable({
// Update auto-adding new line.
updateAutoAddNewLine(defaultEntry, ['expense_account_id']),
// Update the row value of the given row index and column id.
updateTableRow(rowIndex, columnId, value),
updateTableCell(rowIndex, columnId, value),
)(entries);
saveInvoke(onChange, newRows);

View File

@@ -12,8 +12,6 @@ import { dateRangeOptions } from 'containers/FinancialStatements/common';
* Financial statement - Date range select.
*/
export default function FinancialStatementDateRange() {
return (
<>
<Row>

View File

@@ -37,10 +37,10 @@ export const useItemFormInitialValues = (item) => {
return useMemo(
() => ({
...defaultInitialValues,
cost_account_id: defaultTo(itemsSettings.preferredCostAccount, ''),
sell_account_id: defaultTo(itemsSettings.preferredSellAccount, ''),
cost_account_id: defaultTo(itemsSettings?.preferredCostAccount, ''),
sell_account_id: defaultTo(itemsSettings?.preferredSellAccount, ''),
inventory_account_id: defaultTo(
itemsSettings.preferredInventoryAccount,
itemsSettings?.preferredInventoryAccount,
'',
),
/**

View File

@@ -5,7 +5,7 @@ import classNames from 'classnames';
import * as R from 'ramda';
import intl from 'react-intl-universal';
import { useHistory } from 'react-router-dom';
import { isEmpty, omit } from 'lodash';
import { isEmpty } from 'lodash';
import { CLASSES } from 'common/classes';
import { EditBillFormSchema, CreateBillFormSchema } from './BillForm.schema';
@@ -18,8 +18,12 @@ import { AppToaster } from 'components';
import { ERROR } from 'common/errors';
import { useBillFormContext } from './BillFormProvider';
import { compose, orderingLinesIndexes, safeSumBy } from 'utils';
import { defaultBill, transformToEditForm } from './utils';
import { compose, safeSumBy } from 'utils';
import {
defaultBill,
transformToEditForm,
transformEntriesToSubmit,
} from './utils';
import withCurrentOrganization from 'containers/Organization/withCurrentOrganization';
/**
@@ -81,7 +85,7 @@ function BillForm({
const form = {
...values,
open: submitPayload.status,
entries: R.compose(orderingLinesIndexes)(entries),
entries: transformEntriesToSubmit(entries),
};
// Handle the request success.
const onSuccess = (response) => {

View File

@@ -12,6 +12,12 @@ import {
const BillFormContext = createContext();
// Filter all purchasable items only.
const stringifiedFilterRoles = JSON.stringify([
{ index: 1, fieldKey: 'purchasable', value: true, condition: '&&', comparator: 'equals' },
{ index: 2, fieldKey: 'active', value: true, condition: '&&', comparator: 'equals' },
]);
/**
* Bill form provider.
*/
@@ -25,16 +31,6 @@ function BillFormProvider({ billId, ...props }) {
isLoading: isVendorsLoading,
} = useVendors({ page_size: 10000 });
// Filter all purchasable items only.
const stringifiedFilterRoles = React.useMemo(
() =>
JSON.stringify([
{ index: 1, fieldKey: 'purchasable', value: true, condition: '&&', comparator: 'equals' },
{ index: 2, fieldKey: 'active', value: true, condition: '&&', comparator: 'equals' },
]),
[],
);
// Handle fetch Items data table or list
const {
data: { items },

View File

@@ -7,14 +7,17 @@ import {
defaultFastFieldShouldUpdate,
transformToForm,
repeatValue,
orderingLinesIndexes,
} from 'utils';
import {
updateItemsEntriesTotal,
ensureEntriesHaveEmptyLine,
} from 'containers/Entries/utils';
import { isLandedCostDisabled } from '../../../Entries/utils';
export const MIN_LINES_NUMBER = 4;
// Default bill entry.
export const defaultBillEntry = {
index: 0,
item_id: '',
@@ -26,6 +29,7 @@ export const defaultBillEntry = {
landed_cost: false,
};
// Default bill.
export const defaultBill = {
vendor_id: '',
bill_number: '',
@@ -37,10 +41,14 @@ export const defaultBill = {
entries: [...repeatValue(defaultBillEntry, MIN_LINES_NUMBER)],
};
/**
* Transformes the bill to initial values of edit form.
*/
export const transformToEditForm = (bill) => {
const initialEntries = [
...bill.entries.map((entry) => ({
...transformToForm(entry, defaultBillEntry),
landed_cost_disabled: isLandedCostDisabled(entry.item),
})),
...repeatValue(
defaultBillEntry,
@@ -58,7 +66,18 @@ export const transformToEditForm = (bill) => {
};
};
// handle delete errors.
/**
* Transformes bill entries to submit request.
*/
export const transformEntriesToSubmit = (entries) => {
const transformBillEntry = R.curry(transformToForm)(R.__, defaultBillEntry);
return R.compose(orderingLinesIndexes, R.map(transformBillEntry))(entries);
};
/**
* Handle delete errors.
*/
export const handleDeleteErrors = (errors) => {
if (
errors.find((error) => error.type === 'BILL_HAS_ASSOCIATED_PAYMENT_ENTRIES')

View File

@@ -8,7 +8,7 @@ import { DataTableEditable } from 'components';
import { usePaymentMadeEntriesTableColumns } from './components';
import { usePaymentMadeInnerContext } from './PaymentMadeInnerProvider';
import { compose, updateTableRow } from 'utils';
import { compose, updateTableCell } from 'utils';
import { useFormikContext } from 'formik';
/**
@@ -33,7 +33,7 @@ export default function PaymentMadeEntriesTable({
// Handle update data.
const handleUpdateData = useCallback(
(rowIndex, columnId, value) => {
const newRows = compose(updateTableRow(rowIndex, columnId, value))(
const newRows = compose(updateTableCell(rowIndex, columnId, value))(
entries,
);
onUpdateData(newRows);

View File

@@ -8,7 +8,7 @@ import { CLASSES } from 'common/classes';
import { usePaymentReceiveInnerContext } from './PaymentReceiveInnerProvider';
import { DataTableEditable } from 'components';
import { usePaymentReceiveEntriesColumns } from './components';
import { compose, updateTableRow } from 'utils';
import { compose, updateTableCell } from 'utils';
/**
* Payment receive items table.
@@ -39,7 +39,7 @@ export default function PaymentReceiveItemsTable({
// Handle update data.
const handleUpdateData = useCallback(
(rowIndex, columnId, value) => {
const newRows = compose(updateTableRow(rowIndex, columnId, value))(
const newRows = compose(updateTableCell(rowIndex, columnId, value))(
entries,
);