Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(proposal): tax rate in invoice preview #612

Merged
merged 6 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/giant-insects-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@monite/sdk-react': minor
---

bugfix invoice previeww: tax rate showing always as zero, sometimes divided by 100 unnecessarily
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import { useCreateInvoiceProductsTable } from '../../components/useCreateInvoiceProductsTable';
import { CreateReceivablesFormProps } from '../../validation';
// @ts-ignore
// @ts-expect-error

Check failure on line 20 in packages/sdk-react/src/components/receivables/InvoiceDetails/CreateReceivable/sections/components/InvoicePreview.tsx

View workflow job for this annotation

GitHub Actions / Linting

Include a description after the "@ts-expect-error" directive to explain why the @ts-expect-error is necessary. The description must be 3 characters or longer
import invoicePreviewStyles from './InvoicePreview.css';

interface InvoicePreviewProps {
Expand Down Expand Up @@ -98,17 +98,29 @@
const getApplicableTaxRate = (
item: CreateReceivablesFormBeforeValidationLineItemProps
): number => {
if (item.price?.currency === 'EUR') {
return item.vat_rate_value || 0;
if (isNonVatSupported) {
return item.tax_rate_value || 0;
}
return item.tax_rate_value || 0;

return item.vat_rate_value || 0;
};

const formatTaxRate = (
item: CreateReceivablesFormBeforeValidationLineItemProps
): number => {
const taxRate = getApplicableTaxRate(item);
if (taxRate < 100) {
//when the tax value is 19, it should be displayed as 19
return taxRate;
}
return taxRate / 100; //when the tax value is 1900 it should be displayed as 19
};

const groupItemsByTaxRate = (
items: Array<CreateReceivablesFormBeforeValidationLineItemProps>
): Record<number, CreateReceivablesFormBeforeValidationLineItemProps[]> => {
return items.reduce((acc, item) => {
const taxRate = getApplicableTaxRate(item);
const taxRate = formatTaxRate(item);
if (taxRate === 0) {
return acc;
}
Expand All @@ -131,10 +143,10 @@
const items = groupedItems[taxRate];
const totalTax = items.reduce((sum, item) => {
const itemPrice = item.price?.value || 0;
const itemTax = (itemPrice * item.quantity * taxRate) / 10000;
const itemTax = (itemPrice * item.quantity * taxRate) / 100;
return sum + itemTax;
}, 0);
return { taxRate: taxRate / 100, totalTax };
return { taxRate, totalTax };
});
};

Expand Down Expand Up @@ -297,8 +309,8 @@
</tr>
</thead>
<tbody className="products">
{items.length > 0 ? (
items.map((item) => (
{sanitizedItems.length > 0 ? (
sanitizedItems.map((item) => (
<tr>
<td style={{ maxWidth: '120px' }}>{item?.name}</td>
<td>{item?.quantity}</td>
Expand All @@ -314,12 +326,7 @@
false
)}
</td>
<td>
{((item.price.currency === 'EUR'
? item.vat_rate_value
: item.tax_rate_value) || 0) / 100}
%
</td>
<td>{formatTaxRate(item)}%</td>
</tr>
))
) : (
Expand Down
Loading