55 clx ,
66 CurrencyInput ,
77 Divider ,
8- Input ,
98 Label ,
109 RadioGroup ,
1110 Select ,
@@ -15,8 +14,10 @@ import {
1514import { useEffect , useMemo , useState } from "react"
1615import { formatValue } from "react-currency-input-field"
1716import { useForm } from "react-hook-form"
17+ import { useSearchParams } from "react-router-dom"
1818import { useTranslation } from "react-i18next"
1919import * as zod from "zod"
20+
2021import { Form } from "../../../../../components/common/form"
2122import { RouteDrawer , useRouteModal } from "../../../../../components/modals"
2223import { KeyboundForm } from "../../../../../components/utilities/keybound-form"
@@ -33,13 +34,19 @@ const OrderBalanceSettlementSchema = zod.object({
3334 settlement_type : zod . enum ( [ "credit_line" , "refund" ] ) ,
3435 refund : zod
3536 . object ( {
36- amount : zod . string ( ) . or ( zod . number ( ) ) . optional ( ) ,
37+ amount : zod . object ( {
38+ value : zod . string ( ) . or ( zod . number ( ) ) . optional ( ) ,
39+ float : zod . number ( ) . or ( zod . null ( ) ) ,
40+ } ) ,
3741 note : zod . string ( ) . optional ( ) ,
3842 } )
3943 . optional ( ) ,
4044 credit_line : zod
4145 . object ( {
42- amount : zod . string ( ) . or ( zod . number ( ) ) . optional ( ) ,
46+ amount : zod . object ( {
47+ value : zod . string ( ) . or ( zod . number ( ) ) . optional ( ) ,
48+ float : zod . number ( ) . or ( zod . null ( ) ) ,
49+ } ) ,
4350 note : zod . string ( ) . optional ( ) ,
4451 } )
4552 . optional ( ) ,
@@ -51,19 +58,30 @@ export const OrderBalanceSettlementForm = ({
5158 order : AdminOrder
5259} ) => {
5360 const { t } = useTranslation ( )
61+ const [ searchParams ] = useSearchParams ( )
5462 const { handleSuccess } = useRouteModal ( )
55- const [ activePayment , setActivePayment ] = useState < AdminPayment | null > ( null )
63+ const paymentId = searchParams . get ( "paymentId" )
5664 const payments = getPaymentsFromOrder ( order )
5765 const pendingDifference = order . summary . pending_difference * - 1
5866
67+ const [ activePayment , setActivePayment ] = useState < AdminPayment | null > (
68+ paymentId ? payments . find ( ( p ) => p . id === paymentId ) || null : null
69+ )
70+
5971 const form = useForm < zod . infer < typeof OrderBalanceSettlementSchema > > ( {
6072 defaultValues : {
6173 settlement_type : "refund" ,
6274 refund : {
63- amount : 0 ,
75+ amount : {
76+ value : "" ,
77+ float : null ,
78+ } ,
6479 } ,
6580 credit_line : {
66- amount : 0 ,
81+ amount : {
82+ value : "" ,
83+ float : null ,
84+ } ,
6785 } ,
6886 } ,
6987 resolver : zodResolver ( OrderBalanceSettlementSchema ) ,
@@ -79,9 +97,14 @@ export const OrderBalanceSettlementForm = ({
7997
8098 const handleSubmit = form . handleSubmit ( async ( data ) => {
8199 if ( data . settlement_type === "credit_line" ) {
100+ if ( data . credit_line ?. amount . float === null ) {
101+ return
102+ }
82103 await createCreditLine (
83104 {
84- amount : parseFloat ( data . credit_line ! . amount ! as string ) * - 1 ,
105+ amount : data . credit_line ! . amount . float ! * - 1 ,
106+ reference : "refund" ,
107+ reference_id : order . id ,
85108 } ,
86109 {
87110 onSuccess : ( ) => {
@@ -97,17 +120,20 @@ export const OrderBalanceSettlementForm = ({
97120 }
98121
99122 if ( data . settlement_type === "refund" ) {
123+ if ( data . refund ?. amount . float === null ) {
124+ return
125+ }
100126 await createRefund (
101127 {
102- amount : parseFloat ( data . refund ! . amount ! as string ) ,
128+ amount : data . refund ! . amount ! . float ! ,
103129 note : data . refund ! . note ,
104130 } ,
105131 {
106132 onSuccess : ( ) => {
107133 toast . success (
108134 t ( "orders.payment.refundPaymentSuccess" , {
109135 amount : formatCurrency (
110- parseFloat ( data . refund ! . amount ! as string ) ,
136+ data . refund ! . amount ! . float ! ,
111137 order . currency_code !
112138 ) ,
113139 } )
@@ -131,18 +157,23 @@ export const OrderBalanceSettlementForm = ({
131157 useEffect ( ( ) => {
132158 form . clearErrors ( )
133159
134- const minimum = activePayment ?. amount
160+ const _minimum = activePayment ?. amount
135161 ? Math . min ( pendingDifference , activePayment . amount )
136162 : pendingDifference
137163
164+ const minimum = {
165+ value : _minimum . toFixed ( currency . decimal_digits ) ,
166+ float : _minimum ,
167+ }
168+
138169 if ( settlementType === "refund" ) {
139- form . setValue ( "refund.amount" , activePayment ? minimum : 0 )
170+ form . setValue ( "refund.amount" , minimum )
140171 }
141172
142173 if ( settlementType === "credit_line" ) {
143174 form . setValue ( "credit_line.amount" , minimum )
144175 }
145- } , [ settlementType , activePayment , pendingDifference , form ] )
176+ } , [ settlementType , activePayment , pendingDifference , form , currency ] )
146177
147178 return (
148179 < RouteDrawer . Form form = { form } >
@@ -194,6 +225,7 @@ export const OrderBalanceSettlementForm = ({
194225 < >
195226 < div className = "flex flex-col gap-y-4" >
196227 < Select
228+ defaultValue = { activePayment ?. id }
197229 onValueChange = { ( value ) => {
198230 setActivePayment ( payments . find ( ( p ) => p . id === value ) ! )
199231 } }
@@ -260,9 +292,12 @@ export const OrderBalanceSettlementForm = ({
260292 decimalScale = { currency . decimal_digits }
261293 symbol = { currency . symbol_native }
262294 code = { currency . code }
263- value = { field . value }
295+ value = { field . value . value }
264296 onValueChange = { ( _value , _name , values ) =>
265- onChange ( values ?. value ? values ?. value : "" )
297+ onChange ( {
298+ value : values ?. value ,
299+ float : values ?. float || null ,
300+ } )
266301 }
267302 autoFocus
268303 />
@@ -315,10 +350,13 @@ export const OrderBalanceSettlementForm = ({
315350 decimalScale = { currency . decimal_digits }
316351 symbol = { currency . symbol_native }
317352 code = { currency . code }
318- value = { field . value }
319- onValueChange = { ( _value , _name , values ) =>
320- onChange ( values ?. value ? values ?. value : "" )
321- }
353+ value = { field . value . value }
354+ onValueChange = { ( _value , _name , values ) => {
355+ onChange ( {
356+ value : values ?. value ,
357+ float : values ?. float || null ,
358+ } )
359+ } }
322360 autoFocus
323361 />
324362 </ Form . Control >
0 commit comments