diff --git a/src/components/dashboard/Officer_EventRequestForm/EventRequestFormPreview.tsx b/src/components/dashboard/Officer_EventRequestForm/EventRequestFormPreview.tsx index 70a5861..07f1eb4 100644 --- a/src/components/dashboard/Officer_EventRequestForm/EventRequestFormPreview.tsx +++ b/src/components/dashboard/Officer_EventRequestForm/EventRequestFormPreview.tsx @@ -123,8 +123,8 @@ const normalizeFormData = (data: EventRequestFormData | (EventRequest & { ...(parsed as any), items: Array.isArray((parsed as any).items) ? (parsed as any).items : [], // Normalize tax/tip fields - taxAmount: parseFloat(parsed.taxAmount || parsed.tax || 0), - tipAmount: parseFloat(parsed.tipAmount || parsed.tip || 0) + taxAmount: Number(parsed.taxAmount ?? parsed.tax ?? 0), + tipAmount: Number(parsed.tipAmount ?? parsed.tip ?? 0) }; // Normalize item property names @@ -153,8 +153,8 @@ const normalizeFormData = (data: EventRequestFormData | (EventRequest & { ...parsed, items: Array.isArray(parsed.items) ? parsed.items : [], // Normalize tax/tip fields - taxAmount: parseFloat(parsed.taxAmount || parsed.tax || 0), - tipAmount: parseFloat(parsed.tipAmount || parsed.tip || 0) + taxAmount: Number(parsed.taxAmount ?? parsed.tax ?? 0), + tipAmount: Number(parsed.tipAmount ?? parsed.tip ?? 0) }; // Normalize item property names @@ -180,8 +180,8 @@ const normalizeFormData = (data: EventRequestFormData | (EventRequest & { ...parsed, items: Array.isArray(parsed.items) ? parsed.items : [], // Normalize tax/tip fields - taxAmount: parseFloat(parsed.taxAmount || parsed.tax || 0), - tipAmount: parseFloat(parsed.tipAmount || parsed.tip || 0) + taxAmount: Number(parsed.taxAmount ?? parsed.tax ?? 0), + tipAmount: Number(parsed.tipAmount ?? parsed.tip ?? 0) }; // Normalize item property names @@ -775,11 +775,11 @@ const EventRequestFormPreview: React.FC = ({ Tax: - ${(typeof formData.invoiceData.taxAmount === 'number' ? formData.invoiceData.taxAmount : 0).toFixed(2)} + ${(formData.invoiceData.taxAmount || 0).toFixed(2)} Tip: - ${(typeof formData.invoiceData.tipAmount === 'number' ? formData.invoiceData.tipAmount : 0).toFixed(2)} + ${(formData.invoiceData.tipAmount || 0).toFixed(2)} Total: diff --git a/src/components/dashboard/Officer_EventRequestForm/UserEventRequests.tsx b/src/components/dashboard/Officer_EventRequestForm/UserEventRequests.tsx index 821b2db..f29d0d8 100644 --- a/src/components/dashboard/Officer_EventRequestForm/UserEventRequests.tsx +++ b/src/components/dashboard/Officer_EventRequestForm/UserEventRequests.tsx @@ -27,12 +27,81 @@ const convertToFormData = (request: EventRequest): EventRequestFormData => { try { if (request.itemized_invoice) { if (typeof request.itemized_invoice === 'string') { - invoiceData = JSON.parse(request.itemized_invoice); + const parsedInvoice = JSON.parse(request.itemized_invoice) as Record; + + // Get or calculate subtotal from items + const subtotal = parsedInvoice.subtotal ?? + (Array.isArray(parsedInvoice.items) ? + parsedInvoice.items.reduce((sum: number, item: any) => { + const amount = typeof item.amount === 'number' ? item.amount : 0; + return sum + amount; + }, 0) : 0); + + // Normalize tax and tip amounts + const taxAmount = Number(parsedInvoice.taxAmount ?? parsedInvoice.tax ?? 0); + const tipAmount = Number(parsedInvoice.tipAmount ?? parsedInvoice.tip ?? 0); + + // Calculate or get total + const total = parsedInvoice.total ?? (subtotal + taxAmount + tipAmount); + + invoiceData = { + ...parsedInvoice, + subtotal, + taxAmount, + tipAmount, + total + }; } else { - invoiceData = request.itemized_invoice; + const parsedInvoice = request.itemized_invoice as Record; + + // Get or calculate subtotal from items + const subtotal = parsedInvoice.subtotal ?? + (Array.isArray(parsedInvoice.items) ? + parsedInvoice.items.reduce((sum: number, item: any) => { + const amount = typeof item.amount === 'number' ? item.amount : 0; + return sum + amount; + }, 0) : 0); + + // Normalize tax and tip amounts + const taxAmount = Number(parsedInvoice.taxAmount ?? parsedInvoice.tax ?? 0); + const tipAmount = Number(parsedInvoice.tipAmount ?? parsedInvoice.tip ?? 0); + + // Calculate or get total + const total = parsedInvoice.total ?? (subtotal + taxAmount + tipAmount); + + invoiceData = { + ...parsedInvoice, + subtotal, + taxAmount, + tipAmount, + total + }; } } else if (request.invoice_data) { - invoiceData = request.invoice_data; + const parsedInvoice = request.invoice_data as Record; + + // Get or calculate subtotal from items + const subtotal = parsedInvoice.subtotal ?? + (Array.isArray(parsedInvoice.items) ? + parsedInvoice.items.reduce((sum: number, item: any) => { + const amount = typeof item.amount === 'number' ? item.amount : 0; + return sum + amount; + }, 0) : 0); + + // Normalize tax and tip amounts + const taxAmount = Number(parsedInvoice.taxAmount ?? parsedInvoice.tax ?? 0); + const tipAmount = Number(parsedInvoice.tipAmount ?? parsedInvoice.tip ?? 0); + + // Calculate or get total + const total = parsedInvoice.total ?? (subtotal + taxAmount + tipAmount); + + invoiceData = { + ...parsedInvoice, + subtotal, + taxAmount, + tipAmount, + total + }; } } catch (e) { console.error('Error parsing invoice data:', e);