import type { ColumnDef } from '@tanstack/react-table'; import { Pencil, Trash2 } from 'lucide-react'; import { ImagePreviewButton } from '@/components/dialogs'; import { RowActions } from '@/components/data-display'; export type CashTransaction = { id: number; amount: number; formatted_amount: string; balance_after: number; formatted_balance_after: string; type: 'deposit' | 'withdrawal' | 'expense' | 'transfer'; description: string; receipt_key: string | null; receipt_url: string | null; receipt_conversion_url: string | null; created_at: string; formatted_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', employee_advance: 'Kasbon', }; return labels[type] ?? type; } type CreateColumnsParams = { handleEdit: (transaction: CashTransaction) => void; handleDeleteClick: (transaction: CashTransaction) => void; can: (permission: string) => boolean; }; export function createTransactionColumns( params: CreateColumnsParams, ): ColumnDef[] { const { handleEdit, handleDeleteClick, can } = params; const columns: ColumnDef[] = [ { accessorKey: 'formatted_created_at', header: () => Tanggal, cell: ({ row }) => ( {row.getValue('formatted_created_at') as string} ), }, { id: 'source', header: () => Sumber, cell: ({ row }) => { const transaction = row.original; return (
{getTypeLabel(transaction.type)}
); }, }, { accessorKey: 'formatted_amount', header: () => Jumlah, cell: ({ row }) => { const transaction = row.original; const isDeposit = transaction.type === 'deposit'; return ( {isDeposit ? '+' : '-'}{' '} {row.getValue('formatted_amount') as string} ); }, }, { accessorKey: 'formatted_balance_after', header: () => Saldo Setelah, cell: ({ row }) => ( {row.getValue('formatted_balance_after') as string} ), }, { accessorKey: 'description', header: () => Keterangan, cell: ({ row }) => ( {row.getValue('description') as string} ), }, { id: 'receipt', header: () => Bukti, cell: ({ row }) => { const receiptConversionUrl = row.original.receipt_conversion_url; const receiptUrl = row.original.receipt_url; if (!receiptConversionUrl) { return -; } return ( ); }, }, { id: 'created_by', header: () => Oleh, cell: ({ row }) => { const createdBy = row.original.created_by; return {createdBy?.user_profile?.full_name ?? '-'}; }, }, ]; if (can('cash.update') || can('cash.delete')) { columns.push({ 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 ( , show: can('cash.update'), onClick: () => handleEdit(transaction), }, { label: 'Hapus', icon: ( ), show: can('cash.delete'), onClick: () => handleDeleteClick(transaction), }, ]} /> ); }, }); } return columns; }