import type { ColumnDef } from '@tanstack/react-table'; import { Pencil, Printer, Trash2 } from 'lucide-react'; import { RowActions } from '@/components/data-display'; import { Badge } from '@/components/ui/badge'; import { edit, print } from '@/routes/admin/manage/invoices'; import { InvoiceShareButton } from './invoice-share-button'; export type Invoice = { id: number; invoice_number: string; customer_name: string; amount: number; formatted_amount: string; date: string; formatted_date: string; due_date: string | null; formatted_due_date: string | null; status: 'unpaid' | 'paid'; status_label: string; description: string | null; items_count: number; }; export type InvoiceItem = { id?: number; name: string; quantity: number; price: number; }; export type InvoiceForEdit = { id: number; invoice_number: string; customer_name: string; customer_phone: string | null; customer_address: string | null; date: string; due_date: string | null; status: 'unpaid' | 'paid'; description: string | null; items: InvoiceItem[]; }; function getStatusBadge(status: string) { const statusConfig: Record = { unpaid: { label: 'Belum Lunas', className: 'bg-yellow-100 text-yellow-800 hover:bg-yellow-100', }, paid: { label: 'Lunas', className: 'bg-green-100 text-green-800 hover:bg-green-100', }, }; const config = statusConfig[status] ?? statusConfig.unpaid; return ( {config.label} ); } type CreateColumnsParams = { handleDeleteClick: (invoice: Invoice) => void; can: (permission: string) => boolean; }; export function createInvoiceColumns( params: CreateColumnsParams, ): ColumnDef[] { const { handleDeleteClick, can } = params; const columns: ColumnDef[] = [ { accessorKey: 'invoice_number', header: () => Nomor Invoice, cell: ({ row }) => ( {row.getValue('invoice_number') as string} ), }, { accessorKey: 'customer_name', header: () => Pelanggan, cell: ({ row }) => ( {row.getValue('customer_name') as string} ), }, { accessorKey: 'items_count', header: () => Item, cell: ({ row }) => ( {row.getValue('items_count') as number} ), }, { accessorKey: 'formatted_amount', header: () => Jumlah, cell: ({ row }) => ( {row.getValue('formatted_amount') as string} ), }, { accessorKey: 'formatted_date', header: () => Tanggal, cell: ({ row }) => ( {row.getValue('formatted_date') as string} ), }, { accessorKey: 'formatted_due_date', header: () => Jatuh Tempo, cell: ({ row }) => ( {(row.getValue('formatted_due_date') as string) ?? '-'} ), }, { accessorKey: 'status', header: () => Status, cell: ({ row }) => ( {getStatusBadge(row.getValue('status') as string)} ), }, ]; columns.push({ id: 'actions', header: () => Aksi, meta: { className: 'w-[170px] text-center', headerClassName: 'w-[170px] text-center', }, cell: ({ row }) => { const invoice = row.original; return (
, href: print.url(invoice.id), }, { label: 'Edit', icon: , show: can('invoices.update'), href: edit.url(invoice.id), }, { label: 'Hapus', icon: ( ), show: can('invoices.delete'), onClick: () => handleDeleteClick(invoice), }, ]} />
); }, }); return columns; }