diff --git a/src/pages/client/MyInvoices.jsx b/src/pages/client/MyInvoices.jsx index 52dd15d..46fa43e 100644 --- a/src/pages/client/MyInvoices.jsx +++ b/src/pages/client/MyInvoices.jsx @@ -3,10 +3,15 @@ import Layout from '../../components/Layout'; import LoadingButton from '../../components/LoadingButton'; import { supabase } from '../../lib/supabase'; import { generateInvoicePDF } from '../../lib/invoice'; +import { useAuth } from '../../context/AuthContext'; const statusColor = { draft: 'not_started', sent: 'in_progress', paid: 'client_approved' }; export default function MyInvoices() { + const { currentUser } = useAuth(); + const companies = (currentUser?.companies?.length ? currentUser.companies : (currentUser?.company ? [currentUser.company] : [])).slice().sort((a, b) => a.name.localeCompare(b.name)); + const [activeCompanyId, setActiveCompanyId] = useState(companies[0]?.id || null); + const [invoices, setInvoices] = useState([]); const [loading, setLoading] = useState(true); const [generatingInvoiceId, setGeneratingInvoiceId] = useState(''); @@ -33,19 +38,44 @@ export default function MyInvoices() { } }; - const outstanding = invoices.filter(i => i.status === 'sent').reduce((s, i) => s + Number(i.total), 0); - const paid = invoices.filter(i => i.status === 'paid').reduce((s, i) => s + Number(i.total), 0); - const overdueCount = invoices.filter(inv => inv.status !== 'paid' && new Date(inv.due_date) < new Date()).length; + const visible = companies.length > 1 && activeCompanyId + ? invoices.filter(inv => inv.company_id === activeCompanyId) + : invoices; + + const outstanding = visible.filter(i => i.status === 'sent').reduce((s, i) => s + Number(i.total), 0); + const paid = visible.filter(i => i.status === 'paid').reduce((s, i) => s + Number(i.total), 0); + const overdueCount = visible.filter(inv => inv.status !== 'paid' && new Date(inv.due_date) < new Date()).length; return (
Invoices
-
{invoices.length} invoice{invoices.length !== 1 ? 's' : ''}
+
{visible.length} invoice{visible.length !== 1 ? 's' : ''}
+ {companies.length > 1 && ( +
+ {companies.map((company, index) => ( + + {index > 0 && |} + + + ))} +
+ )} +
${outstanding.toFixed(2)}
@@ -63,14 +93,14 @@ export default function MyInvoices() { {loading ? (

Loading...

- ) : invoices.length === 0 ? ( + ) : visible.length === 0 ? (

No invoices yet

Your invoices will appear here once they are sent.

) : (
- {invoices.map(inv => { + {visible.map(inv => { const isOverdue = inv.status !== 'paid' && new Date(inv.due_date) < new Date(); return (