Skip to content
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
2 changes: 1 addition & 1 deletion packages/admin/dashboard/src/lib/format-currency.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const formatCurrency = (amount: number, currency: string) => {
return new Intl.NumberFormat("en-US", {
return new Intl.NumberFormat(undefined, {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Why was this change required?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this change, the default runtime locale is used for formatting instead of forcing en-US. This way currency formatting should be the same for the currency input and the places we use this helper (previously for example with DE locale formatting would be 1.234,5 but the helper would return 1,234.5.

style: "currency",
currency,
signDisplay: "auto",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
clx,
CurrencyInput,
Divider,
Input,
Label,
RadioGroup,
Select,
Expand All @@ -15,8 +14,10 @@ import {
import { useEffect, useMemo, useState } from "react"
import { formatValue } from "react-currency-input-field"
import { useForm } from "react-hook-form"
import { useSearchParams } from "react-router-dom"
import { useTranslation } from "react-i18next"
import * as zod from "zod"

import { Form } from "../../../../../components/common/form"
import { RouteDrawer, useRouteModal } from "../../../../../components/modals"
import { KeyboundForm } from "../../../../../components/utilities/keybound-form"
Expand All @@ -33,13 +34,19 @@ const OrderBalanceSettlementSchema = zod.object({
settlement_type: zod.enum(["credit_line", "refund"]),
refund: zod
.object({
amount: zod.string().or(zod.number()).optional(),
amount: zod.object({
value: zod.string().or(zod.number()).optional(),
float: zod.number().or(zod.null()),
}),
note: zod.string().optional(),
})
.optional(),
credit_line: zod
.object({
amount: zod.string().or(zod.number()).optional(),
amount: zod.object({
value: zod.string().or(zod.number()).optional(),
float: zod.number().or(zod.null()),
}),
note: zod.string().optional(),
})
.optional(),
Expand All @@ -51,19 +58,30 @@ export const OrderBalanceSettlementForm = ({
order: AdminOrder
}) => {
const { t } = useTranslation()
const [searchParams] = useSearchParams()
const { handleSuccess } = useRouteModal()
const [activePayment, setActivePayment] = useState<AdminPayment | null>(null)
const paymentId = searchParams.get("paymentId")
const payments = getPaymentsFromOrder(order)
const pendingDifference = order.summary.pending_difference * -1

const [activePayment, setActivePayment] = useState<AdminPayment | null>(
paymentId ? payments.find((p) => p.id === paymentId) || null : null
)

const form = useForm<zod.infer<typeof OrderBalanceSettlementSchema>>({
defaultValues: {
settlement_type: "refund",
refund: {
amount: 0,
amount: {
value: "",
float: null,
},
},
credit_line: {
amount: 0,
amount: {
value: "",
float: null,
},
},
},
resolver: zodResolver(OrderBalanceSettlementSchema),
Expand All @@ -79,9 +97,14 @@ export const OrderBalanceSettlementForm = ({

const handleSubmit = form.handleSubmit(async (data) => {
if (data.settlement_type === "credit_line") {
if (data.credit_line?.amount.float === null) {
return
}
await createCreditLine(
{
amount: parseFloat(data.credit_line!.amount! as string) * -1,
amount: data.credit_line!.amount.float! * -1,
reference: "refund",
reference_id: order.id,
},
{
onSuccess: () => {
Expand All @@ -97,17 +120,20 @@ export const OrderBalanceSettlementForm = ({
}

if (data.settlement_type === "refund") {
if (data.refund?.amount.float === null) {
return
}
await createRefund(
{
amount: parseFloat(data.refund!.amount! as string),
amount: data.refund!.amount!.float!,
note: data.refund!.note,
},
{
onSuccess: () => {
toast.success(
t("orders.payment.refundPaymentSuccess", {
amount: formatCurrency(
parseFloat(data.refund!.amount! as string),
data.refund!.amount!.float!,
order.currency_code!
),
})
Expand All @@ -131,18 +157,23 @@ export const OrderBalanceSettlementForm = ({
useEffect(() => {
form.clearErrors()

const minimum = activePayment?.amount
const _minimum = activePayment?.amount
? Math.min(pendingDifference, activePayment.amount)
: pendingDifference

const minimum = {
value: _minimum.toFixed(currency.decimal_digits),
float: _minimum,
}

if (settlementType === "refund") {
form.setValue("refund.amount", activePayment ? minimum : 0)
form.setValue("refund.amount", minimum)
}

if (settlementType === "credit_line") {
form.setValue("credit_line.amount", minimum)
}
}, [settlementType, activePayment, pendingDifference, form])
}, [settlementType, activePayment, pendingDifference, form, currency])

return (
<RouteDrawer.Form form={form}>
Expand Down Expand Up @@ -194,6 +225,7 @@ export const OrderBalanceSettlementForm = ({
<>
<div className="flex flex-col gap-y-4">
<Select
defaultValue={activePayment?.id}
onValueChange={(value) => {
setActivePayment(payments.find((p) => p.id === value)!)
}}
Expand Down Expand Up @@ -260,9 +292,12 @@ export const OrderBalanceSettlementForm = ({
decimalScale={currency.decimal_digits}
symbol={currency.symbol_native}
code={currency.code}
value={field.value}
value={field.value.value}
onValueChange={(_value, _name, values) =>
onChange(values?.value ? values?.value : "")
onChange({
value: values?.value,
float: values?.float || null,
})
}
autoFocus
/>
Expand Down Expand Up @@ -315,10 +350,13 @@ export const OrderBalanceSettlementForm = ({
decimalScale={currency.decimal_digits}
symbol={currency.symbol_native}
code={currency.code}
value={field.value}
onValueChange={(_value, _name, values) =>
onChange(values?.value ? values?.value : "")
}
value={field.value.value}
onValueChange={(_value, _name, values) => {
onChange({
value: values?.value,
float: values?.float || null,
})
}}
autoFocus
/>
</Form.Control>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,16 @@ export const ClaimCreateForm = ({
useState(false)

const [customInboundShippingAmount, setCustomInboundShippingAmount] =
useState<number | string>(0)
useState<{ value: string; float: number | null }>({
value: "0",
float: 0,
})

const [customOutboundShippingAmount, setCustomOutboundShippingAmount] =
useState<number | string>(0)
useState<{ value: string; float: number | null }>({
value: "0",
float: 0,
})

const [inventoryMap, setInventoryMap] = useState<
Record<string, InventoryLevelDTO[]>
Expand Down Expand Up @@ -263,13 +269,23 @@ export const ClaimCreateForm = ({

useEffect(() => {
if (inboundShipping) {
setCustomInboundShippingAmount(inboundShipping.total)
setCustomInboundShippingAmount({
value: inboundShipping.total.toFixed(
currencies[order.currency_code.toUpperCase()].decimal_digits
),
float: inboundShipping.total,
})
}
}, [inboundShipping])

useEffect(() => {
if (outboundShipping) {
setCustomOutboundShippingAmount(outboundShipping.total)
setCustomOutboundShippingAmount({
value: outboundShipping.total.toFixed(
currencies[order.currency_code.toUpperCase()].decimal_digits
),
float: outboundShipping.total,
})
}
}, [outboundShipping])

Expand Down Expand Up @@ -519,6 +535,7 @@ export const ClaimCreateForm = ({
).variants

variants.forEach((variant) => {
// TODO: fix this for inventory kits
ret[variant.id] = variant.inventory?.[0]?.location_levels || []
})

Expand Down Expand Up @@ -560,6 +577,15 @@ export const ClaimCreateForm = ({
return (method?.total as number) || 0
}, [preview.shipping_methods])

const outboundShippingTotal = useMemo(() => {
const method = preview.shipping_methods.find(
(sm) =>
!!sm.actions?.find((a) => a.action === "SHIPPING_ADD" && !a.return_id)
)

return (method?.total as number) || 0
}, [preview.shipping_methods])

return (
<RouteFocusModal.Form form={form}>
<KeyboundForm onSubmit={handleSubmit} className="flex h-full flex-col">
Expand Down Expand Up @@ -866,10 +892,7 @@ export const ClaimCreateForm = ({
}
})

const customPrice =
customInboundShippingAmount === ""
? null
: parseFloat(customInboundShippingAmount)
const customPrice = customInboundShippingAmount.float

if (actionId) {
updateInboundShipping(
Expand All @@ -891,8 +914,13 @@ export const ClaimCreateForm = ({
.symbol_native
}
code={order.currency_code}
onValueChange={setCustomInboundShippingAmount}
value={customInboundShippingAmount}
onValueChange={(value, _name, values) => {
setCustomInboundShippingAmount({
value: values?.value || "",
float: values?.float || null,
})
}}
value={customInboundShippingAmount.value}
disabled={showInboundItemsPlaceholder}
/>
) : (
Expand Down Expand Up @@ -937,10 +965,7 @@ export const ClaimCreateForm = ({
}
})

const customPrice =
customOutboundShippingAmount === ""
? null
: parseFloat(customOutboundShippingAmount)
const customPrice = customOutboundShippingAmount.float

if (actionId) {
updateOutboundShipping(
Expand All @@ -962,13 +987,18 @@ export const ClaimCreateForm = ({
.symbol_native
}
code={order.currency_code}
onValueChange={setCustomOutboundShippingAmount}
value={customOutboundShippingAmount}
onValueChange={(value, _name, values) => {
setCustomOutboundShippingAmount({
value: values?.value || "",
float: values?.float || null,
})
}}
value={customOutboundShippingAmount.value}
disabled={showOutboundItemsPlaceholder}
/>
) : (
getStylizedAmount(
outboundShipping?.amount ?? 0,
outboundShippingTotal,
order.currency_code
)
)}
Expand Down
Loading