import { Banknote, Receipt } from "lucide-react"; import { Divider, Group, Paper, Stack, Text } from "@mantine/core"; import { useQuery } from "@tanstack/react-query"; import { api } from "@/services/api"; import type { BookingDetail } from "@/types/booking"; import { SectionCard } from "./detail/SectionCard"; import { detailStyles } from "./detail/booking-detail.styles"; export function BookingPricingSummary({ booking }: { booking: BookingDetail }) { // The booking's own freight invoice: source `booking`, sourceId = booking id // (which `search` matches). Newest first — a re-issue supersedes the old one. const invoiceQuery = useQuery( api.invoices.list.queryOptions({ input: { filter: { page: 1, pageSize: 1, sources: "booking", search: booking.id, sortBy: "createdAt", sortOrder: "DESC", }, }, }), ); const invoiceNumber = invoiceQuery.data?.items[0]?.invoiceNumber ?? null; const computed = Number(booking.totalAmount); // The booking price is computed from the contract and is NOT staff-editable. // A historical `adjustedTotalAmount` (from before adjustments were removed) // is still shown read-only so old records render correctly. const isAdjusted = booking.adjustedTotalAmount !== null && booking.adjustedTotalAmount !== undefined; const effective = isAdjusted ? Number(booking.adjustedTotalAmount) : computed; const lineItems = booking.pricingBreakdown?.lineItems ?? []; const fmt = (n: number) => `${booking.paymentCurrency} ${n.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2, })}`; return ( {isAdjusted ? "Adjusted total" : "Total amount"} {fmt(effective)} {isAdjusted && ( Computed: {fmt(computed)} {booking.adjustmentReason ? ` · ${booking.adjustmentReason}` : ""} )} {invoiceNumber && } {booking.pnrCode && } {lineItems.length > 0 && ( <> Price breakdown {lineItems.map((li, i) => ( {li.description} {Number(li.amount).toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2, })}{" "} {li.currency} ))} )} ); } function Row({ label, value, mono }: { label: string; value: string; mono?: boolean }) { return ( {label} {value} ); }