updates
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
"use client"
|
||||
|
||||
import { useMemo } from "react"
|
||||
import { AlertTriangle, Plus, Save } from "lucide-react"
|
||||
|
||||
import { Button } from "@/shared/components/ui/button"
|
||||
@@ -11,12 +12,14 @@ import {
|
||||
RhfSelectField,
|
||||
RhfTextareaField,
|
||||
RhfAsyncSelectField,
|
||||
RhfAutoGenerateField,
|
||||
RhfDateField,
|
||||
} from "@/shared/components/form"
|
||||
import { toast } from "sonner"
|
||||
import { useAuthApi } from "@/shared/useApi"
|
||||
import { useResourceForm } from "@/shared/hooks/use-resource-form"
|
||||
import { useFormMutation } from "@/shared/hooks/use-form-mutation"
|
||||
import { toRelation, toId } from "@/shared/lib/utils"
|
||||
import { toRelation, toId, getTodayDate } from "@/shared/lib/utils"
|
||||
|
||||
import {
|
||||
paymentMadeFormSchema,
|
||||
@@ -25,10 +28,10 @@ import {
|
||||
import {
|
||||
PAYMENT_MADE_ROUTES,
|
||||
PAYMENT_MODE_ROUTES,
|
||||
VENDOR_ROUTES,
|
||||
EMPLOYEE_ROUTES,
|
||||
PaymentFor,
|
||||
} from "@garage/api"
|
||||
import { RhfVendorSelectField } from "@/modules/vendors/rhf-vendor-select-field"
|
||||
|
||||
// ── Constants ──
|
||||
|
||||
@@ -43,54 +46,70 @@ export type PaymentMadeFormProps = {
|
||||
resourceId?: string | null
|
||||
initialData?: unknown
|
||||
onSuccess?: () => void
|
||||
billId?: string | number | null
|
||||
expenseId?: string | number | null
|
||||
}
|
||||
|
||||
// ── Default values ──
|
||||
|
||||
const DEFAULT_VALUES: PaymentMadeFormValues = {
|
||||
const DEFAULT_VALUES: PaymentMadeFormValues & { details: Array<{ bill_id?: string | number | null; amount_paid?: number, expense_id?: string | number | null }> } = {
|
||||
vendor: null,
|
||||
employee: null,
|
||||
payment_mode: null,
|
||||
payment_for: "",
|
||||
payment_made: "",
|
||||
payment_for: "bill",
|
||||
amount: "",
|
||||
payment_number: "",
|
||||
payment_reference: "",
|
||||
payment_date: "",
|
||||
payment_date: getTodayDate(),
|
||||
paid_through: "",
|
||||
notes: "",
|
||||
details: []
|
||||
}
|
||||
|
||||
// ── Mapping helpers ──
|
||||
|
||||
function mapToFormValues(data: unknown): PaymentMadeFormValues {
|
||||
function mapToFormValues(data: unknown): typeof DEFAULT_VALUES {
|
||||
const d = (data as any)?.data ?? data ?? {}
|
||||
|
||||
// Resolve payment_mode label from nested object (title) or fallback to id
|
||||
const paymentModeId = d.payment_mode_id ?? d.payment_mode?.id
|
||||
const paymentModeLabel = d.payment_mode?.title ?? d.payment_mode?.name ?? d.payment_mode_name
|
||||
|
||||
return {
|
||||
vendor: toRelation(d.vendor_id, d.vendor_name),
|
||||
employee: toRelation(d.employee_id, d.employee_name),
|
||||
payment_mode: toRelation(d.payment_mode_id, d.payment_mode_name),
|
||||
vendor: toRelation(d.vendor_id, d.vendor?.company_name ?? d.vendor?.name ?? d.vendor_name),
|
||||
employee: toRelation(d.employee_id, d.employee?.first_name ? `${d.employee.first_name} ${d.employee.last_name ?? ""}`.trim() : d.employee_name),
|
||||
payment_mode: toRelation(paymentModeId, paymentModeLabel),
|
||||
payment_for: d.payment_for || "",
|
||||
payment_made: d.payment_made ? String(d.payment_made) : "",
|
||||
amount: d.payment_made ? String(d.payment_made) : "",
|
||||
payment_number: d.payment_number || "",
|
||||
payment_reference: d.payment_reference || "",
|
||||
payment_date: d.payment_date || "",
|
||||
paid_through: d.paid_through || "",
|
||||
notes: d.notes || "",
|
||||
paid_through: d.paid_through || "-",
|
||||
notes: d.notes || "-",
|
||||
details: [{ bill_id: d.bill_id, amount_paid: d.amount_paid }],
|
||||
}
|
||||
}
|
||||
|
||||
function mapFormToPayload(values: PaymentMadeFormValues) {
|
||||
|
||||
function mapFormToPayload(values: PaymentMadeFormValues, billId?: string | number | null, expenseId?: string | number | null) {
|
||||
const paymentDetails =
|
||||
expenseId ? [{ expense_id: expenseId, amount_paid: values.amount ? Number(values.amount) : 0 }]
|
||||
: [{ bill_id: billId, amount_paid: values.amount ? Number(values.amount) : 0 }]
|
||||
return {
|
||||
vendor_id: toId(values.vendor),
|
||||
employee_id: toId(values.employee) || undefined,
|
||||
payment_mode_id: toId(values.payment_mode),
|
||||
payment_for: values.payment_for,
|
||||
payment_made: values.payment_made,
|
||||
amount: values.amount ? Number(values.amount) : 0,
|
||||
payment_number: values.payment_number || undefined,
|
||||
payment_reference: values.payment_reference || undefined,
|
||||
payment_date: values.payment_date,
|
||||
paid_through: values.paid_through || undefined,
|
||||
notes: values.notes || undefined,
|
||||
payment_made: values.amount,
|
||||
details: paymentDetails,
|
||||
...(billId ? { bill_id: Number(billId) } : {}),
|
||||
...(expenseId ? { expense_id: Number(expenseId) } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,20 +136,32 @@ const STORE_OBJECT = { getOptionValue: (o: any) => o, getOptionLabel: (o: any) =
|
||||
|
||||
// ── Component ──
|
||||
|
||||
export function PaymentMadeForm({ resourceId, initialData, onSuccess }: PaymentMadeFormProps) {
|
||||
export function PaymentMadeForm({ resourceId, initialData, onSuccess, billId, expenseId }: PaymentMadeFormProps) {
|
||||
const api = useAuthApi()
|
||||
|
||||
const resolvedInitialData = useMemo(() => {
|
||||
const base: any = { ...(initialData as any) }
|
||||
if (!resourceId) {
|
||||
if (billId) {
|
||||
base.payment_for = "bill"
|
||||
} else if (expenseId) {
|
||||
base.payment_for = "expense"
|
||||
}
|
||||
}
|
||||
return Object.keys(base).length ? base : initialData
|
||||
}, [resourceId, billId, expenseId, initialData])
|
||||
|
||||
const { form, isEditing } = useResourceForm<PaymentMadeFormValues, any>({
|
||||
schema: paymentMadeFormSchema,
|
||||
defaultValues: DEFAULT_VALUES,
|
||||
resourceId,
|
||||
initialData,
|
||||
initialData: resolvedInitialData,
|
||||
mapToFormValues,
|
||||
})
|
||||
|
||||
const { mutate, error, isPending } = useFormMutation(form, {
|
||||
mutationFn: (values: PaymentMadeFormValues) => {
|
||||
const payload = mapFormToPayload(values)
|
||||
const payload = mapFormToPayload(values, billId, expenseId)
|
||||
const promise = (isEditing && resourceId
|
||||
? api.paymentMades.update(resourceId, payload as any)
|
||||
: api.paymentMades.create(payload as any)) as Promise<any>
|
||||
@@ -161,14 +192,9 @@ export function PaymentMadeForm({ resourceId, initialData, onSuccess }: PaymentM
|
||||
|
||||
<FieldGroup>
|
||||
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
|
||||
<RhfAsyncSelectField
|
||||
<RhfVendorSelectField
|
||||
name="vendor"
|
||||
label="Vendor"
|
||||
placeholder="Select vendor"
|
||||
queryKey={[VENDOR_ROUTES.INDEX]}
|
||||
listFn={() => api.vendors.list()}
|
||||
mapOption={mapVendorOption}
|
||||
{...STORE_OBJECT}
|
||||
/>
|
||||
<RhfAsyncSelectField
|
||||
name="employee"
|
||||
@@ -190,7 +216,7 @@ export function PaymentMadeForm({ resourceId, initialData, onSuccess }: PaymentM
|
||||
required
|
||||
/>
|
||||
<RhfTextField
|
||||
name="payment_made"
|
||||
name="amount"
|
||||
label="Amount"
|
||||
placeholder="0.00"
|
||||
type="number"
|
||||
@@ -199,7 +225,9 @@ export function PaymentMadeForm({ resourceId, initialData, onSuccess }: PaymentM
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
|
||||
<RhfTextField
|
||||
<RhfAutoGenerateField
|
||||
table="payments"
|
||||
autoFetch
|
||||
name="payment_number"
|
||||
label="Payment Number"
|
||||
placeholder="PAY-001"
|
||||
@@ -212,10 +240,9 @@ export function PaymentMadeForm({ resourceId, initialData, onSuccess }: PaymentM
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
|
||||
<RhfTextField
|
||||
<RhfDateField
|
||||
name="payment_date"
|
||||
label="Payment Date"
|
||||
type="date"
|
||||
required
|
||||
/>
|
||||
<RhfAsyncSelectField
|
||||
|
||||
@@ -11,8 +11,8 @@ const paymentMadeFormSchema = z.object({
|
||||
payment_mode: relationFieldSchema,
|
||||
|
||||
// ── Payment info ──
|
||||
amount: z.string().min(1, "Amount is required"),
|
||||
payment_for: z.string().min(1, "Payment for is required"),
|
||||
payment_made: z.string().min(1, "Amount is required"),
|
||||
payment_number: z.string().optional(),
|
||||
payment_reference: z.string().optional(),
|
||||
payment_date: z.string().min(1, "Payment date is required"),
|
||||
|
||||
Reference in New Issue
Block a user