This commit is contained in:
2026-04-23 14:38:41 +03:00
parent f17dd1486c
commit c0f78c6e18
90 changed files with 10819 additions and 1583 deletions
@@ -11,6 +11,8 @@ import {
RhfTextField,
RhfTextareaField,
RhfAsyncSelectField,
RhfDateField,
RhfAutoGenerateField,
} from "@/shared/components/form"
import { toast } from "sonner"
import { useAuthApi } from "@/shared/useApi"
@@ -31,6 +33,9 @@ export type PaymentReceivedFormProps = {
initialData?: unknown
onSuccess?: () => void
defaultJobCard?: { id?: number | null; title?: string | null } | null
invoiceId?: string | null
invoiceCustomer?: { id?: number | null; first_name?: string | null; last_name?: string | null } | null
invoiceAmount?: number | string | null
}
// ── Default values ──
@@ -41,7 +46,7 @@ const DEFAULT_VALUES: PaymentReceivedFormValues = {
customer: null,
amount_received: "",
payment_number: "",
payment_date: "",
payment_date: new Date().toISOString().split("T")[0],
note: "",
}
@@ -61,7 +66,7 @@ function mapToFormValues(data: unknown): PaymentReceivedFormValues {
}
}
function mapFormToPayload(values: PaymentReceivedFormValues) {
function mapFormToPayload(values: PaymentReceivedFormValues, invoiceId?: string | null) {
return {
job_card_id: toId(values.job_card),
payment_mode_id: toId(values.payment_mode),
@@ -70,6 +75,7 @@ function mapFormToPayload(values: PaymentReceivedFormValues) {
payment_number: values.payment_number || undefined,
payment_date: values.payment_date,
note: values.note || undefined,
...(invoiceId ? { invoice_id: Number(invoiceId) } : {}),
}
}
@@ -84,18 +90,29 @@ const STORE_OBJECT = { getOptionValue: (o: any) => o, getOptionLabel: (o: any) =
// ── Component ──
export function PaymentReceivedForm({ resourceId, initialData, onSuccess, defaultJobCard }: PaymentReceivedFormProps) {
export function PaymentReceivedForm({ resourceId, initialData, onSuccess, defaultJobCard, invoiceId, invoiceCustomer, invoiceAmount }: PaymentReceivedFormProps) {
const api = useAuthApi()
const resolvedInitialData = useMemo(() => {
if (!resourceId && defaultJobCard?.id != null) {
return {
...(initialData as any),
job_card: toRelation(defaultJobCard.id, defaultJobCard.title ?? undefined),
const base: any = { ...(initialData as any) }
if (!resourceId) {
if (defaultJobCard?.id != null) {
base.job_card = toRelation(defaultJobCard.id, defaultJobCard.title ?? undefined)
}
if (invoiceCustomer?.id != null) {
base.customer = toRelation(
invoiceCustomer.id,
invoiceCustomer.first_name
? `${invoiceCustomer.first_name} ${invoiceCustomer.last_name || ""}`.trim()
: undefined
)
}
if (invoiceAmount != null && invoiceAmount !== "") {
base.amount_received = String(invoiceAmount)
}
}
return initialData
}, [resourceId, defaultJobCard, initialData])
return Object.keys(base).length ? base : initialData
}, [resourceId, defaultJobCard, initialData, invoiceCustomer, invoiceAmount])
const { form, isEditing } = useResourceForm<PaymentReceivedFormValues, any>({
schema: paymentReceivedFormSchema,
@@ -107,7 +124,7 @@ export function PaymentReceivedForm({ resourceId, initialData, onSuccess, defaul
const { mutate, error, isPending } = useFormMutation(form, {
mutationFn: (values: PaymentReceivedFormValues) => {
const payload = mapFormToPayload(values)
const payload = mapFormToPayload(values, invoiceId)
const promise = isEditing && resourceId
? api.paymentReceived.update(resourceId, payload as any)
: api.paymentReceived.create(payload as any)
@@ -186,16 +203,16 @@ export function PaymentReceivedForm({ resourceId, initialData, onSuccess, defaul
</div>
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
<RhfTextField
<RhfAutoGenerateField
autoFetch
table="payments"
name="payment_number"
label="Payment Number"
placeholder="PAY-001"
/>
<RhfTextField
<RhfDateField
name="payment_date"
label="Payment Date"
type="date"
required
/>
</div>