filters , order inspections , inventory items , credit notes

This commit is contained in:
2026-04-08 14:38:16 +03:00
parent 90c84a0bda
commit bb02b77be2
33 changed files with 2552 additions and 145 deletions
@@ -8,7 +8,11 @@ import { FieldGroup } from "@/shared/components/ui/field"
import {
Rhform,
RhfTextField,
RhfTextareaField,
RhfSelectField,
RhfAsyncSelectField,
RhfDateField,
RhfTimeField,
} from "@/shared/components/form"
import { toast } from "sonner"
import { useAuthApi } from "@/shared/useApi"
@@ -25,6 +29,9 @@ import {
import {
INSPECTION_ROUTES,
DEPARTMENT_ROUTES,
JOB_CARD_ROUTES,
InspectionStatus,
RateType,
} from "@garage/api"
import { RhfVehicleSelectField } from "@/modules/vehicles/rhf-vehicle-select-field"
import { RhfCustomerSelectField } from "@/modules/customers/rhf-customer-select-field"
@@ -46,10 +53,22 @@ const DEFAULT_VALUES: InspectionFormValues = {
department: null,
inspection_category: null,
employee: null,
job_card: null,
labor_rate: null,
title: "",
order_number: "",
date: "",
time: "",
status: "in_progress",
note: "",
description: "",
rate_type: "flat_rate",
quantity: 1,
rate: 0,
working_hours: 0,
labor_hours: 0,
tax: "",
chart_of_account: "",
}
// ── Mapping helpers ──
@@ -63,10 +82,26 @@ function mapToFormValues(data: unknown): InspectionFormValues {
department: toRelation(d.department_id, d.department?.name),
inspection_category: toRelation(d.inspection_category_id, d.inspection_category?.name),
employee: toRelation(d.employee_id, d.employee?.first_name ? `${d.employee.first_name} ${d.employee.last_name ?? ""}`.trim() : undefined),
job_card: d.job_card_id
? { value: String(d.job_card_id), label: d.job_card?.title ?? d.job_card?.order_number ?? String(d.job_card_id) }
: null,
labor_rate: d.labor_rate_id
? { value: String(d.labor_rate_id), label: d.labor_rate?.title ?? String(d.labor_rate_id) }
: null,
title: d.title ?? "",
order_number: d.order_number ?? "",
date: d.date ?? "",
date: d.date ? d.date.split("T")[0] : "",
time: d.time ?? "",
status: d.status ?? "in_progress",
note: d.note ?? "",
description: d.description ?? "",
rate_type: d.rate_type ?? "flat_rate",
quantity: d.quantity != null ? Number(d.quantity) : 1,
rate: d.rate != null ? Number(d.rate) : 0,
working_hours: d.working_hours != null ? Number(d.working_hours) : 0,
labor_hours: d.labor_hours != null ? Number(d.labor_hours) : 0,
tax: d.tax ?? "",
chart_of_account: d.chart_of_account ?? "",
}
}
@@ -77,14 +112,36 @@ function mapFormToPayload(values: InspectionFormValues) {
department_id: toId(values.department),
inspection_category_id: toId(values.inspection_category),
employee_id: toId(values.employee),
job_card_id: toId(values.job_card) ?? null,
labor_rate_id: values.labor_rate ? Number(values.labor_rate.value) : undefined,
title: values.title,
order_number: values.order_number || undefined,
date: values.date || undefined,
time: values.time || undefined,
status: values.status || undefined,
note: values.note || undefined,
description: values.description || undefined,
rate_type: values.rate_type || undefined,
quantity: values.quantity ?? undefined,
rate: values.rate ?? undefined,
working_hours: values.working_hours ?? undefined,
labor_hours: values.labor_hours ?? undefined,
tax: values.tax || undefined,
chart_of_account: values.chart_of_account || undefined,
}
}
// ── Shared mapOption for async selects ──
// ── Select options ──
const STATUS_OPTIONS = InspectionStatus.map((v) => ({
value: v,
label: v.replace(/_/g, " ").replace(/\b\w/g, (c) => c.toUpperCase()),
}))
const RATE_TYPE_OPTIONS = RateType.map((v) => ({
value: v,
label: v === "flat_rate" ? "Flat Rate" : "Hourly",
}))
const mapLookupOption = (item: any) => ({
value: String(item.id),
@@ -171,15 +228,74 @@ export function InspectionForm({ resourceId, initialData, onSuccess }: Inspectio
/>
</div>
<RhfEmployeeSelectField name="employee" />
<RhfTextField name="order_number" label="Order Number" placeholder="e.g. ORD-001" />
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
<RhfEmployeeSelectField name="employee" />
<RhfAsyncSelectField
name="job_card"
label="Job Card"
placeholder="Select job card"
queryKey={[JOB_CARD_ROUTES.INDEX]}
listFn={() => api.jobCards.list()}
mapOption={(item: any) => ({
value: String(item.id),
label: item.title ?? item.order_number ?? String(item.id),
})}
{...STORE_OBJECT}
/>
</div>
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
<RhfTextField name="date" label="Date" placeholder="YYYY-MM-DD" type="date" />
<RhfTextField name="time" label="Time" placeholder="HH:MM:SS" type="time" step={1} />
<RhfSelectField
name="status"
label="Status"
placeholder="Select status"
options={STATUS_OPTIONS}
/>
<RhfTextField name="order_number" label="Order Number" placeholder="e.g. ORD-001" />
</div>
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
<RhfDateField name="date" label="Date" />
<RhfTimeField name="time" label="Time" withSeconds />
</div>
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
<RhfSelectField
name="rate_type"
label="Rate Type"
placeholder="Select rate type"
options={RATE_TYPE_OPTIONS}
/>
<RhfAsyncSelectField
name="labor_rate"
label="Labor Rate"
placeholder="Select labor rate"
queryKey={["labor-rates"]}
listFn={() => api.inventory.listLaborRates()}
mapOption={(item: any) => ({
value: String(item.id),
label: item.title ?? String(item.id),
})}
{...STORE_OBJECT}
/>
</div>
<div className="grid grid-cols-1 gap-4 sm:grid-cols-3">
<RhfTextField name="quantity" label="Quantity" type="number" placeholder="1" />
<RhfTextField name="rate" label="Rate" type="number" placeholder="0.00" />
<RhfTextField name="tax" label="Tax" placeholder="e.g. 5%" />
</div>
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
<RhfTextField name="working_hours" label="Working Hours" type="number" placeholder="0" />
<RhfTextField name="labor_hours" label="Labor Hours" type="number" placeholder="0" />
</div>
<RhfTextField name="chart_of_account" label="Chart of Account" placeholder="e.g. COA-123" />
<RhfTextareaField name="note" label="Note" placeholder="Internal notes..." />
<RhfTextareaField name="description" label="Description" placeholder="Full description..." />
<Button type="submit" variant="default" disabled={isPending}>
{isEditing ? <Save /> : <Plus />}
{isPending
@@ -10,10 +10,22 @@ const inspectionFormSchema = z.object({
department: relationFieldSchema,
inspection_category: relationFieldSchema,
employee: relationFieldSchema,
job_card: relationFieldSchema.optional(),
labor_rate: relationFieldSchema.optional(),
title: z.string().min(1, "Title is required"),
order_number: z.string().optional(),
date: z.string().optional(),
time: z.string().optional(),
status: z.string().optional(),
note: z.string().optional(),
description: z.string().optional(),
rate_type: z.string().optional(),
quantity: z.coerce.number().min(0).optional(),
rate: z.coerce.number().min(0).optional(),
working_hours: z.coerce.number().min(0).optional(),
labor_hours: z.coerce.number().min(0).optional(),
tax: z.string().optional(),
chart_of_account: z.string().optional(),
})
type InspectionFormValues = z.infer<typeof inspectionFormSchema>