Spaces:
Sleeping
Sleeping
| import { taClaims, TAClaim, User } from "@/data/dummyData"; | |
| const LOCAL_KEY = "auditor_local_ta_claims"; | |
| function readLocal(): TAClaim[] { | |
| if (typeof window === "undefined") return []; | |
| try { | |
| const raw = localStorage.getItem(LOCAL_KEY); | |
| if (!raw) return []; | |
| const parsed = JSON.parse(raw); | |
| if (!Array.isArray(parsed)) return []; | |
| return parsed as TAClaim[]; | |
| } catch { | |
| return []; | |
| } | |
| } | |
| function writeLocal(claims: TAClaim[]) { | |
| if (typeof window === "undefined") return; | |
| try { | |
| localStorage.setItem(LOCAL_KEY, JSON.stringify(claims)); | |
| } catch { | |
| // ignore | |
| } | |
| } | |
| export function getAuditorTaClaims(user?: User | null): TAClaim[] { | |
| if (!user) return []; | |
| const base = taClaims.filter((c) => c.auditorId === user.id); | |
| const local = readLocal().filter((c) => c.auditorId === user.id); | |
| return [...base, ...local].sort( | |
| (a, b) => | |
| new Date(b.submittedDate).getTime() - new Date(a.submittedDate).getTime(), | |
| ); | |
| } | |
| export interface NewTaClaimInput { | |
| auditId: string; | |
| travelDate: string; | |
| fromLocation: string; | |
| toLocation: string; | |
| travelMode: string; | |
| distance: string; | |
| fareAmount: string; | |
| daAmount: string; | |
| } | |
| export function createTaClaimForAuditor( | |
| user: User, | |
| input: NewTaClaimInput, | |
| ): TAClaim { | |
| const now = new Date(); | |
| const id = `TC${now.getTime()}`; | |
| const totalAmount = | |
| Number(input.fareAmount || 0) + Number(input.daAmount || 0); | |
| const claim: TAClaim = { | |
| id, | |
| auditorId: user.id, | |
| auditorName: user.name, | |
| auditId: input.auditId, | |
| applicationNo: "", | |
| travelDate: input.travelDate, | |
| fromLocation: input.fromLocation, | |
| toLocation: input.toLocation, | |
| travelMode: input.travelMode, | |
| distance: Number(input.distance || 0), | |
| fareAmount: Number(input.fareAmount || 0), | |
| daAmount: Number(input.daAmount || 0), | |
| totalAmount, | |
| status: "submitted", | |
| submittedDate: now.toISOString().slice(0, 10), | |
| processedDate: undefined, | |
| }; | |
| const all = readLocal(); | |
| writeLocal([...all, claim]); | |
| return claim; | |
| } | |