* feat(invoices): concept scene 15 for the customer invoice list Kundfakturor becomes the concept dry-table: Nr, Kund, Forfaller, Belopp and ONE derivable status chip per row (Utkast/Ej skickad/Skickad/Forfallen N dgr/Betald datum/Delbetald/Kredit/Makulerad). The eight tabs collapse to the seg [Alla/Obetalda/Forfallna (n)/Utkast] with the low-frequency views (Proforma, Foljesedlar, Kreditfakturor, Makulerade) behind a quiet 'Fler ...' menu, plus search and the FyPicker far right. Create paths collapse into the Ny faktura split button (Ny/Aterkommande/Sjalvfaktura, last-used mode persisted); sjalvfaktura preselects the editor's tab via ?new=1&self=1. The summary line under the header is dropped per the plan. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(invoices): status views behind one chip-picker Founder direction: the Alla/Obetalda/Forfallna/Utkast seg and the 'Fler ...' menu collapse into a single status ContextPicker chip with the overdue count on the trigger and as a row annotation. All eight views, same filtering semantics. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(invoices): audit round: Betalda view, viewer states, deep links Regression-audit fixes: the Betalda view returns as a status-picker row (it existed on main and was dropped in the seg redesign); the create options render disabled with the viewer tooltip for read-only members instead of silently no-opping; and the page answers ?status=/?tab= deep links again (the Att gora worklist links /invoices?status=unpaid). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
157 lines
5.5 KiB
TypeScript
157 lines
5.5 KiB
TypeScript
'use client'
|
|
|
|
import dynamic from 'next/dynamic'
|
|
import { useEffect, useMemo, useState } from 'react'
|
|
import { useTranslations } from 'next-intl'
|
|
import { Dialog, DialogContent, DialogTitle } from '@/components/ui/dialog'
|
|
import { Skeleton } from '@/components/ui/skeleton'
|
|
import { Button } from '@/components/ui/button'
|
|
import { createClient } from '@/lib/supabase/client'
|
|
import { useCompany } from '@/contexts/CompanyContext'
|
|
import { fetchAllRows } from '@/lib/supabase/fetch-all'
|
|
import {
|
|
buildInvoiceCopyInitial,
|
|
canCopyInvoice,
|
|
type InvoiceCopyInitial,
|
|
type InvoiceCopySource,
|
|
} from '@/lib/invoices/copy-invoice'
|
|
|
|
// Deferred: the editor (and its framer-motion dependency) is a large chunk
|
|
// that would otherwise ship with the invoice LIST bundle: it is only needed
|
|
// once this dialog actually opens.
|
|
const InvoiceEditor = dynamic(() => import('@/components/invoices/InvoiceEditor'), {
|
|
ssr: false,
|
|
loading: () => (
|
|
<div className="space-y-4 p-6">
|
|
<Skeleton className="h-8 w-1/3" />
|
|
<Skeleton className="h-32 w-full" />
|
|
<Skeleton className="h-32 w-full" />
|
|
</div>
|
|
),
|
|
})
|
|
|
|
interface Props {
|
|
open: boolean
|
|
onOpenChange: (open: boolean) => void
|
|
copyFromId?: string | null
|
|
/** Preselect the självfaktura tab (split-button entry on /invoices). */
|
|
selfBilled?: boolean
|
|
}
|
|
|
|
/**
|
|
* "Ny faktura" as a modal: mirrors NewJournalEntryDialog. Wraps the bare
|
|
* InvoiceEditor; the editor's own review/confirm/send dialogs stack on top of
|
|
* this one, and every successful create navigates to the invoice detail page
|
|
* (unmounting the host list page and this dialog with it).
|
|
*
|
|
* The accessible title is visually hidden: the bare editor renders its own
|
|
* live heading, which tracks document type (faktura/proforma/följesedel) and
|
|
* shows the invoice-number preview: a static DialogTitle would duplicate or
|
|
* contradict it.
|
|
*/
|
|
export default function NewInvoiceDialog({ open, onOpenChange, copyFromId = null, selfBilled = false }: Props) {
|
|
const t = useTranslations('invoice_editor')
|
|
const { company } = useCompany()
|
|
const supabase = useMemo(() => createClient(), [])
|
|
const [copyLoad, setCopyLoad] = useState<{
|
|
sourceId: string | null
|
|
initial: InvoiceCopyInitial | null
|
|
failed: boolean
|
|
}>({ sourceId: null, initial: null, failed: false })
|
|
|
|
useEffect(() => {
|
|
if (!open || !copyFromId) return
|
|
if (!company?.id) return
|
|
|
|
let cancelled = false
|
|
|
|
const loadCopySource = async () => {
|
|
const { data, error } = await supabase
|
|
.from('invoices')
|
|
.select('*')
|
|
.eq('id', copyFromId)
|
|
.eq('company_id', company.id)
|
|
.single()
|
|
|
|
let items: Record<string, unknown>[] = []
|
|
if (!error && data) {
|
|
try {
|
|
items = await fetchAllRows<Record<string, unknown>>(({ from, to }) =>
|
|
supabase
|
|
.from('invoice_items')
|
|
.select('*')
|
|
.eq('invoice_id', copyFromId)
|
|
.eq('company_id', company.id)
|
|
.order('id', { ascending: true })
|
|
.range(from, to),
|
|
)
|
|
} catch {
|
|
if (!cancelled) setCopyLoad({ sourceId: copyFromId, initial: null, failed: true })
|
|
return
|
|
}
|
|
}
|
|
|
|
if (cancelled) return
|
|
const source = data ? { ...data, items } : null
|
|
if (error || !source || !canCopyInvoice(source)) {
|
|
setCopyLoad({ sourceId: copyFromId, initial: null, failed: true })
|
|
return
|
|
}
|
|
setCopyLoad({
|
|
sourceId: copyFromId,
|
|
initial: buildInvoiceCopyInitial(source as InvoiceCopySource),
|
|
failed: false,
|
|
})
|
|
}
|
|
|
|
void loadCopySource()
|
|
|
|
return () => {
|
|
cancelled = true
|
|
}
|
|
}, [company?.id, copyFromId, open, supabase])
|
|
|
|
const copyInitial = copyLoad.sourceId === copyFromId ? copyLoad.initial : null
|
|
const copyLoadFailed = copyLoad.sourceId === copyFromId && copyLoad.failed
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
<DialogContent
|
|
className="sm:max-w-5xl max-h-[95dvh] sm:max-h-[90vh] overflow-y-auto"
|
|
// A half-typed invoice must survive an accidental backdrop click or a
|
|
// stray Escape (nested comboboxes and date pickers portal outside the
|
|
// dialog). Closing is explicit: the header X. Same convention as
|
|
// NewJournalEntryDialog.
|
|
onEscapeKeyDown={(e) => e.preventDefault()}
|
|
onPointerDownOutside={(e) => e.preventDefault()}
|
|
onInteractOutside={(e) => e.preventDefault()}
|
|
>
|
|
<DialogTitle className="sr-only">
|
|
{copyFromId ? t('title_copy') : t('title_invoice')}
|
|
</DialogTitle>
|
|
{copyFromId ? (
|
|
copyLoadFailed ? (
|
|
<div className="space-y-4 p-6 text-center">
|
|
<p className="font-medium">{t('copy_load_failed_title')}</p>
|
|
<p className="text-sm text-muted-foreground">{t('copy_load_failed_description')}</p>
|
|
<Button variant="outline" onClick={() => onOpenChange(false)}>
|
|
{t('close')}
|
|
</Button>
|
|
</div>
|
|
) : copyInitial ? (
|
|
<InvoiceEditor key={copyFromId} mode="copy" initial={copyInitial} bare />
|
|
) : (
|
|
<div className="space-y-4 p-6">
|
|
<Skeleton className="h-8 w-1/3" />
|
|
<Skeleton className="h-32 w-full" />
|
|
<Skeleton className="h-32 w-full" />
|
|
</div>
|
|
)
|
|
) : (
|
|
<InvoiceEditor mode="create" bare initialSelfBilled={selfBilled} />
|
|
)}
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
}
|