From 5f8407ae4c60922057e2f88f65178773786bddb4 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sun, 16 Oct 2022 12:46:41 +0530 Subject: calculating currency/other floating point values with currency.js --- src/components/tables/invoice-item-table.js | 30 +++++++++++---------- src/components/tables/invoice-summary.js | 42 ++++++++++++++--------------- 2 files changed, 37 insertions(+), 35 deletions(-) (limited to 'src/components') diff --git a/src/components/tables/invoice-item-table.js b/src/components/tables/invoice-item-table.js index fb91af3..d008ebc 100644 --- a/src/components/tables/invoice-item-table.js +++ b/src/components/tables/invoice-item-table.js @@ -16,7 +16,7 @@ */ import './scss/table.scss'; -import { getDiscountValue, getGSTValue, getAmount } from './../../classes/item'; +import { getDiscountValue, getGSTValue, getAmount, currency } from './../../classes/item'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' import { faPencil, faTrashCan } from '@fortawesome/free-solid-svg-icons' @@ -70,17 +70,18 @@ const ItemTable = ({items, setItems, isInterstate, sum}) => { {i.Brand.Name} {i.Quantity} {i.UnitOfMeasure} - 0 ? "" : "empty"}>{i.UnitPrice} - 0 ? "" : "empty"}>{getDiscountValue(i)} ({i.DiscountPercentage}%) + 0 ? "" : "empty"}>{currency(i.UnitPrice).format()} + 0 ? "" : "empty"}>{getDiscountValue(i).format()} ({i.DiscountPercentage}%) {isInterstate - ? 0 ? "" : "empty"}>{getGSTValue(i)} ({i.GSTPercentage}%) + ? 0 ? "" : "empty"}>{getGSTValue(i).format()} : <> - 0 ? "" : "empty"}>{getGSTValue(i) / 2} ({i.GSTPercentage / 2}%) - 0 ? "" : "empty"}>{getGSTValue(i) / 2} ({i.GSTPercentage / 2}%) + {getGSTValue(i).distribute(2).map((j, id) => + 0 ? "" : "empty"}>{j.format()} ({currency(i.GSTPercentage).divide(2).value}%) + )} } {i.HSN} - {getAmount(i)} + {getAmount(i).format()} handleEdit(i)}/> handleDelete(i)}/> @@ -92,19 +93,20 @@ const ItemTable = ({items, setItems, isInterstate, sum}) => { - 0 ? "" : "empty"}>{sum.Quantity} + 0 ? "" : "empty"}>{sum.Quantity.value} - 0 ? "" : "empty"}>{sum.UnitPrice} - 0 ? "" : "empty"}>{sum.Discount} + 0 ? "" : "empty"}>{sum.UnitPrice.format()} + 0 ? "" : "empty"}>{sum.Discount.format()} {isInterstate - ? 0 ? "" : "empty"}>{sum.GST || 0} + ? 0 ? "" : "empty"}>{sum.GST.format()} : <> - 0 ? "" : "empty"}>{sum.GST / 2 || 0} - 0 ? "" : "empty"}>{sum.GST / 2 || 0} + {sum.GST.distribute(2).map((i, id) => + 0 ? "" : "empty"}>{i.format()} + )} } - 0 ? "" : "empty"}>{sum.Amount} + 0 ? "" : "empty"}>{sum.Amount.format()} diff --git a/src/components/tables/invoice-summary.js b/src/components/tables/invoice-summary.js index 5899105..7018d50 100644 --- a/src/components/tables/invoice-summary.js +++ b/src/components/tables/invoice-summary.js @@ -15,47 +15,47 @@ * along with this program. If not, see . */ -import './scss/table.scss'; +import { currency } from '../../classes/item'; const InvoiceSummary = ({sum}) => { - const totalRoundedOff = Math.round(sum.Amount); - const roundedOffDiff = sum.Amount - totalRoundedOff; - - const formatter = new Intl.NumberFormat("en-US", { - maximumSignificantDigits: 2, - }) + const totalRoundedOff = currency(sum.Amount !== undefined ? Math.round(sum.Amount.value) : 0.00); + const roundedOffDiff = sum.Amount !== undefined ? sum.Amount.subtract(totalRoundedOff) : currency(0.00); return ( <>

Summary:

- - - - - {sum.Discount > 0 && + {sum.UnitPrice !== undefined && + + + + + } + {sum.Discount !== undefined && sum.Discount.value > 0 && - + } - {sum.GST > 0 && + {sum.GST !== undefined && sum.GST.value > 0 && - + } - {(isNaN(roundedOffDiff) || roundedOffDiff !== 0) && + {roundedOffDiff.intValue !== 0 && - + + + } + {sum.Amount !== undefined && + + + } - - - -
Base Total{formatter.format(sum.UnitPrice)}
Base Total{sum.UnitPrice.format()}
Total After Discount{formatter.format(sum.UnitPrice - sum.Discount)} (-{formatter.format(sum.Discount)}){sum.UnitPrice.subtract(sum.Discount).format()} (-{sum.Discount.format()})
Total After Tax{formatter.format(sum.UnitPrice - (sum.Discount > 0 ? sum.Discount : 0) + sum.GST)} (+{formatter.format(sum.GST)}){sum.UnitPrice.subtract(sum.Discount.value > 0 ? sum.Discount : currency(0)).add(sum.GST).format()} (+{sum.GST.format()})
Rounded Off{`${roundedOffDiff > 0 ? `(-) ${formatter.format(roundedOffDiff)}` : `(+) ${formatter.format(roundedOffDiff * -1)}`}`}{`${roundedOffDiff.value > 0 ? `(-) ${roundedOffDiff.format()}` : `(+) ${roundedOffDiff.multiply(-1).format()}`}`}
Grand Total{sum.Amount.subtract(roundedOffDiff).format()}
Grand Total{formatter.format(sum.Amount - (isNaN(roundedOffDiff) ? 0 : roundedOffDiff))}
-- cgit v1.2.3