fix:(dashboard) pending difference rounding on order details page (#11336)

**Why**
- if pending difference is lass then the rounding threshold for currency the page would show for example "Refund -0.00$"

**What**
- example: hide the refund button if the pending difference in USD is -0.004
- example: show refund button if pending difference on USD is -0.007

---

CLOSES SUP-811
CLOSES https://github.com/medusajs/medusa/issues/11331
This commit is contained in:
Frane Polić authored and GitHub committed 2025-02-07 15:24:43 +00:00
1 parent fcd3e2226e
commit d58c056c53
3 files changed
+34 -4

No files matched your search

@@ -44,10 +44,32 @@ export const getStylizedAmount = (amount: number, currencyCode: string) => {
const symbol = getNativeSymbol(currencyCode) const symbol = getNativeSymbol(currencyCode)
const decimalDigits = getDecimalDigits(currencyCode) const decimalDigits = getDecimalDigits(currencyCode)
const lessThanRoundingPrecission = isAmountLessThenRoundingError(
amount,
currencyCode
)
const total = amount.toLocaleString(undefined, { const total = amount.toLocaleString(undefined, {
minimumFractionDigits: decimalDigits, minimumFractionDigits: decimalDigits,
maximumFractionDigits: decimalDigits, maximumFractionDigits: decimalDigits,
signDisplay: lessThanRoundingPrecission ? "exceptZero" : "auto",
}) })
return `${symbol} ${total} ${currencyCode.toUpperCase()}` return `${symbol} ${total} ${currencyCode.toUpperCase()}`
} }
/**
* Returns true if the amount is less than the rounding error for the currency
* @param amount - The amount to check
* @param currencyCode - The currency code to check the amount in
* @returns - True if the amount is less than the rounding error, false otherwise
*
* For example returns true if amount is < 0.005 for a USD | EUR etc.
*/
export const isAmountLessThenRoundingError = (
amount: number,
currencyCode: string
) => {
const decimalDigits = getDecimalDigits(currencyCode)
return Math.abs(amount) < 1 / 10 ** decimalDigits / 2
}
@@ -44,7 +44,7 @@ export const CreateRefundForm = ({
const paymentId = searchParams.get("paymentId") const paymentId = searchParams.get("paymentId")
const payments = getPaymentsFromOrder(order) const payments = getPaymentsFromOrder(order)
const payment = payments.find((p) => p.id === paymentId)! const payment = payments.find((p) => p.id === paymentId)!
const paymentAmount = payment.amount || 0 const paymentAmount = payment?.amount || 0
const form = useForm<zod.infer<typeof CreateRefundSchema>>({ const form = useForm<zod.infer<typeof CreateRefundSchema>>({
defaultValues: { defaultValues: {
amount: paymentAmount, amount: paymentAmount,
@@ -50,6 +50,7 @@ import { formatCurrency } from "../../../../../lib/format-currency"
import { import {
getLocaleAmount, getLocaleAmount,
getStylizedAmount, getStylizedAmount,
isAmountLessThenRoundingError,
} from "../../../../../lib/money-amount-helpers" } from "../../../../../lib/money-amount-helpers"
import { getTotalCaptured } from "../../../../../lib/payment" import { getTotalCaptured } from "../../../../../lib/payment"
import { getReturnableQuantity } from "../../../../../lib/rma" import { getReturnableQuantity } from "../../../../../lib/rma"
@@ -125,9 +126,16 @@ export const OrderSummarySection = ({ order }: OrderSummarySectionProps) => {
unpaidPaymentCollection?.id! unpaidPaymentCollection?.id!
) )
const pendingDifference = order.summary?.pending_difference || 0
const isAmountSignificant = !isAmountLessThenRoundingError(
pendingDifference,
order.currency_code
)
const showPayment = const showPayment =
unpaidPaymentCollection && (order?.summary?.pending_difference || 0) > 0 unpaidPaymentCollection && pendingDifference > 0 && isAmountSignificant
const showRefund = (order?.summary?.pending_difference || 0) < 0 const showRefund =
unpaidPaymentCollection && pendingDifference < 0 && isAmountSignificant
const handleMarkAsPaid = async ( const handleMarkAsPaid = async (
paymentCollection: AdminPaymentCollection paymentCollection: AdminPaymentCollection
@@ -261,7 +269,7 @@ export const OrderSummarySection = ({ order }: OrderSummarySectionProps) => {
> >
{t("orders.payment.refundAmount", { {t("orders.payment.refundAmount", {
amount: getStylizedAmount( amount: getStylizedAmount(
(order?.summary?.pending_difference || 0) * -1, pendingDifference * -1,
order?.currency_code order?.currency_code
), ),
})} })}