Skip to content

Commit

Permalink
Merge pull request #293 from bigcapitalhq/big-84-fix-the-transaction-…
Browse files Browse the repository at this point in the history
…tax-rate-currency

fix: the currency code of transaction tax rate entry
  • Loading branch information
abouolia authored Dec 17, 2023
2 parents fe043eb + d0126ff commit cdff641
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
43 changes: 24 additions & 19 deletions packages/webapp/src/containers/Entries/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -284,26 +284,31 @@ export const useComposeRowsOnRemoveTableRow = () => {

/**
* Retrieves the aggregate tax rates from the given item entries.
* @param {string} currencyCode -
* @param {any} taxRates -
* @param {any} entries -
*/
export const aggregateItemEntriesTaxRates = R.curry((taxRates, entries) => {
const taxRatesById = keyBy(taxRates, 'id');
export const aggregateItemEntriesTaxRates = R.curry(
(currencyCode, taxRates, entries) => {
const taxRatesById = keyBy(taxRates, 'id');

// Calculate the total tax amount of invoice entries.
const filteredEntries = entries.filter((e) => e.tax_rate_id);
const groupedTaxRates = groupBy(filteredEntries, 'tax_rate_id');
// Calculate the total tax amount of invoice entries.
const filteredEntries = entries.filter((e) => e.tax_rate_id);
const groupedTaxRates = groupBy(filteredEntries, 'tax_rate_id');

return Object.keys(groupedTaxRates).map((taxRateId) => {
const taxRate = taxRatesById[taxRateId];
const taxRates = groupedTaxRates[taxRateId];
const totalTaxAmount = sumBy(taxRates, 'tax_amount');
const taxAmountFormatted = formattedAmount(totalTaxAmount, 'USD');
return Object.keys(groupedTaxRates).map((taxRateId) => {
const taxRate = taxRatesById[taxRateId];
const taxRates = groupedTaxRates[taxRateId];
const totalTaxAmount = sumBy(taxRates, 'tax_amount');
const taxAmountFormatted = formattedAmount(totalTaxAmount, currencyCode);

return {
taxRateId,
taxRate: taxRate.rate,
label: `${taxRate.name} [${taxRate.rate}%]`,
taxAmount: totalTaxAmount,
taxAmountFormatted,
};
});
});
return {
taxRateId,
taxRate: taxRate.rate,
label: `${taxRate.name} [${taxRate.rate}%]`,
taxAmount: totalTaxAmount,
taxAmountFormatted,
};
});
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,8 @@ export const useBillAggregatedTaxRates = () => {
const { taxRates } = useBillFormContext();

const aggregateTaxRates = React.useMemo(
() => aggregateItemEntriesTaxRates(taxRates),
[taxRates],
() => aggregateItemEntriesTaxRates(values.currency_code, taxRates),
[values.currency_code, taxRates],
);
// Calculate the total tax amount of bill entries.
return React.useMemo(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,8 @@ export const useInvoiceAggregatedTaxRates = () => {
const { taxRates } = useInvoiceFormContext();

const aggregateTaxRates = React.useMemo(
() => aggregateItemEntriesTaxRates(taxRates),
[taxRates],
() => aggregateItemEntriesTaxRates(values.currency_code, taxRates),
[values.currency_code, taxRates],
);
// Calculate the total tax amount of invoice entries.
return React.useMemo(() => {
Expand Down

0 comments on commit cdff641

Please sign in to comment.