feat: assign default sell/purchase tax rates to items (#261)

This commit is contained in:
Ahmed Bouhuolia
2023-10-08 23:55:59 +02:00
committed by GitHub
parent d40de4d22b
commit 1ed1c9ea1d
25 changed files with 400 additions and 18 deletions

View File

@@ -8,9 +8,9 @@ import { DataTableEditable } from '@/components';
import { useEditableItemsEntriesColumns } from './components';
import {
useFetchItemRow,
composeRowsOnNewRow,
useComposeRowsOnEditTableCell,
useComposeRowsOnRemoveTableRow,
useComposeRowsOnNewRow,
} from './utils';
import {
ItemEntriesTableProvider,
@@ -61,6 +61,7 @@ function ItemEntriesTableRoot() {
currencyCode,
landedCost,
taxRates,
itemType,
} = useItemEntriesTableContext();
// Editiable items entries columns.
@@ -68,11 +69,12 @@ function ItemEntriesTableRoot() {
const composeRowsOnEditCell = useComposeRowsOnEditTableCell();
const composeRowsOnDeleteRow = useComposeRowsOnRemoveTableRow();
const composeRowsOnNewRow = useComposeRowsOnNewRow();
// Handle the fetch item row details.
const { setItemRow, cellsLoading, isItemFetching } = useFetchItemRow({
landedCost,
itemType: null,
itemType,
notifyNewRow: (newRow, rowIndex) => {
// Update the rate, description and quantity data of the row.
const newRows = composeRowsOnNewRow(rowIndex, newRow, localValue);

View File

@@ -1,5 +1,5 @@
// @ts-nocheck
import React, { useCallback } from 'react';
import React, { useCallback, useMemo } from 'react';
import * as R from 'ramda';
import { sumBy, isEmpty, last, keyBy, groupBy } from 'lodash';
import { useItem } from '@/hooks/query';
@@ -116,19 +116,20 @@ export function useFetchItemRow({ landedCost, itemType, notifyNewRow }) {
? item.purchase_description
: item.sell_description;
// Detarmines whether the landed cost checkbox should be disabled.
const landedCostDisabled = isLandedCostDisabled(item);
const taxRateId =
itemType === ITEM_TYPE.PURCHASABLE
? item.purchase_tax_rate_id
: item.sell_tax_rate_id;
// Detarmines whether the landed cost checkbox should be disabled.
const landedCostDisabled = isLandedCostDisabled(item);
// The new row.
const newRow = {
rate: price,
description,
quantity: 1,
tax_rate_id: taxRateId,
...(landedCost
? {
landed_cost: false,
@@ -164,13 +165,21 @@ export const composeRowsOnEditCell = R.curry(
/**
* Compose table rows when insert a new row to table rows.
*/
export const composeRowsOnNewRow = R.curry((rowIndex, newRow, rows) => {
return compose(
orderingLinesIndexes,
updateItemsEntriesTotal,
updateTableRow(rowIndex, newRow),
)(rows);
});
export const useComposeRowsOnNewRow = () => {
const { taxRates, isInclusiveTax } = useItemEntriesTableContext();
return React.useMemo(() => {
return R.curry((rowIndex, newRow, rows) => {
return compose(
assignEntriesTaxAmount(isInclusiveTax),
assignEntriesTaxRate(taxRates),
orderingLinesIndexes,
updateItemsEntriesTotal,
updateTableRow(rowIndex, newRow),
)(rows);
});
}, [isInclusiveTax, taxRates]);
};
/**
* Associate tax rate to entries.