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ć
2025-02-07 16:24:43 +01:00
committed by GitHub
parent fcd3e2226e
commit d58c056c53
3 changed files with 34 additions and 4 deletions

View File

@@ -44,10 +44,32 @@ export const getStylizedAmount = (amount: number, currencyCode: string) => {
const symbol = getNativeSymbol(currencyCode)
const decimalDigits = getDecimalDigits(currencyCode)
const lessThanRoundingPrecission = isAmountLessThenRoundingError(
amount,
currencyCode
)
const total = amount.toLocaleString(undefined, {
minimumFractionDigits: decimalDigits,
maximumFractionDigits: decimalDigits,
signDisplay: lessThanRoundingPrecission ? "exceptZero" : "auto",
})
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
}

View File

@@ -44,7 +44,7 @@ export const CreateRefundForm = ({
const paymentId = searchParams.get("paymentId")
const payments = getPaymentsFromOrder(order)
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>>({
defaultValues: {
amount: paymentAmount,

View File

@@ -50,6 +50,7 @@ import { formatCurrency } from "../../../../../lib/format-currency"
import {
getLocaleAmount,
getStylizedAmount,
isAmountLessThenRoundingError,
} from "../../../../../lib/money-amount-helpers"
import { getTotalCaptured } from "../../../../../lib/payment"
import { getReturnableQuantity } from "../../../../../lib/rma"
@@ -125,9 +126,16 @@ export const OrderSummarySection = ({ order }: OrderSummarySectionProps) => {
unpaidPaymentCollection?.id!
)
const pendingDifference = order.summary?.pending_difference || 0
const isAmountSignificant = !isAmountLessThenRoundingError(
pendingDifference,
order.currency_code
)
const showPayment =
unpaidPaymentCollection && (order?.summary?.pending_difference || 0) > 0
const showRefund = (order?.summary?.pending_difference || 0) < 0
unpaidPaymentCollection && pendingDifference > 0 && isAmountSignificant
const showRefund =
unpaidPaymentCollection && pendingDifference < 0 && isAmountSignificant
const handleMarkAsPaid = async (
paymentCollection: AdminPaymentCollection
@@ -261,7 +269,7 @@ export const OrderSummarySection = ({ order }: OrderSummarySectionProps) => {
>
{t("orders.payment.refundAmount", {
amount: getStylizedAmount(
(order?.summary?.pending_difference || 0) * -1,
pendingDifference * -1,
order?.currency_code
),
})}