import type { ColumnDef } from '@tanstack/react-table'; import { Pencil, Trash2 } from 'lucide-react'; import { ImagePreviewButton } from '@/components/image-preview-button'; import { RowActions } from '@/components/row-actions'; import { formatDate } from '@/lib/format'; import { formatCurrency } from '@/lib/utils'; export type CashTransaction = { id: number; amount: number; balance_after: number; type: 'deposit' | 'withdrawal' | 'expense' | 'transfer'; description: string; receipt_key: string | null; receipt_url: string | null; created_at: string; created_by: { user_profile: { full_name: string; }; }; reference: { type: string; } | null; }; function getTypeLabel(type: string): string { const labels: Record = { deposit: 'Deposit', withdrawal: 'Withdrawal', expense: 'Pengeluaran', transfer: 'Transfer', }; return labels[type] ?? type; } function getReferenceLabel(type: string): string { const labels: Record = { 'App\\Models\\Expense': 'Pengeluaran', 'App\\Models\\Order': 'Penjualan Tunai', 'App\\Models\\Purchase': 'Belanja', 'App\\Models\\CashAccount': 'Transfer Kas', }; return labels[type] ?? '-'; } type CreateColumnsParams = { handleEdit: (transaction: CashTransaction) => void; handleDeleteClick: (transaction: CashTransaction) => void; }; export function createTransactionColumns( params: CreateColumnsParams, ): ColumnDef[] { const { handleEdit, handleDeleteClick } = params; return [ { accessorKey: 'created_at', header: () => Tanggal, cell: ({ row }) => ( {formatDate(row.getValue('created_at') as string)} ), }, { id: 'source', header: () => Sumber, cell: ({ row }) => { const transaction = row.original; return (
{getTypeLabel(transaction.type)} {getReferenceLabel( transaction.reference?.type ?? '', )}
); }, }, { accessorKey: 'amount', header: () => Jumlah, cell: ({ row }) => { const transaction = row.original; const isDeposit = transaction.type === 'deposit'; return ( {isDeposit ? '+' : '-'}{' '} {formatCurrency(row.getValue('amount') as number)} ); }, }, { accessorKey: 'balance_after', header: () => Saldo Setelah, cell: ({ row }) => ( {formatCurrency(row.getValue('balance_after') as number)} ), }, { accessorKey: 'description', header: () => Keterangan, cell: ({ row }) => ( {row.getValue('description') as string} ), }, { id: 'receipt', header: () => Bukti, cell: ({ row }) => { const receiptUrl = row.original.receipt_url; if (!receiptUrl) { return -; } return ( ); }, }, { id: 'created_by', header: () => Oleh, cell: ({ row }) => { const createdBy = row.original.created_by; return {createdBy?.user_profile?.full_name ?? '-'}; }, }, { id: 'actions', header: () => Aksi, meta: { className: 'w-[100px] text-center', headerClassName: 'w-[100px] text-center', }, cell: ({ row }) => { const transaction = row.original; const canEdit = transaction.type === 'deposit' || transaction.type === 'withdrawal'; if (!canEdit) { return -; } return ( , onClick: () => handleEdit(transaction), }, { label: 'Hapus', icon: ( ), onClick: () => handleDeleteClick(transaction), }, ]} /> ); }, }, ]; }