- Add EmployeeAdvanceService for handling employee advance operations including create, update, delete, approve, and pay. - Create new components for date picking and calendar UI. - Develop Employee Advance columns for data table representation. - Implement Employee Advance index page with CRUD operations and dialogs for creating, editing, approving, and paying advances. - Update routes to include employee advances resource and specific actions for approval and payment. - Add necessary dependencies for date handling and UI components.
288 lines
10 KiB
TypeScript
288 lines
10 KiB
TypeScript
import type { ColumnDef } from '@tanstack/react-table';
|
|
import { ArrowUpDown, CheckCircle, CircleDollarSign, Pencil, Trash2 } from 'lucide-react';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Badge } from '@/components/ui/badge';
|
|
import {
|
|
Tooltip,
|
|
TooltipContent,
|
|
TooltipProvider,
|
|
TooltipTrigger,
|
|
} from '@/components/ui/tooltip';
|
|
import { formatCurrency } from '@/lib/utils';
|
|
|
|
export type EmployeeAdvance = {
|
|
id: number;
|
|
amount: number;
|
|
paid_amount: number;
|
|
description: string;
|
|
due_date: string;
|
|
status: 'pending' | 'approved' | 'paid' | 'rejected' | 'cancelled';
|
|
created_at: string;
|
|
employee: {
|
|
user: {
|
|
user_profile: {
|
|
full_name: string;
|
|
};
|
|
};
|
|
};
|
|
};
|
|
|
|
function formatDate(dateString: string): string {
|
|
const date = new Date(dateString);
|
|
|
|
return date.toLocaleDateString('id-ID', {
|
|
day: '2-digit',
|
|
month: 'short',
|
|
year: 'numeric',
|
|
}) + ' ' + date.toLocaleTimeString('id-ID', {
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
});
|
|
}
|
|
|
|
function formatShortDate(dateString: string): string {
|
|
const date = new Date(dateString);
|
|
|
|
return date.toLocaleDateString('id-ID', {
|
|
day: '2-digit',
|
|
month: 'short',
|
|
year: 'numeric',
|
|
});
|
|
}
|
|
|
|
function getStatusBadge(status: string) {
|
|
const statusConfig: Record<string, { label: string; className: string }> = {
|
|
pending: {
|
|
label: 'Menunggu',
|
|
className: 'bg-yellow-100 text-yellow-800 hover:bg-yellow-100',
|
|
},
|
|
approved: {
|
|
label: 'Disetujui',
|
|
className: 'bg-green-100 text-green-800 hover:bg-green-100',
|
|
},
|
|
paid: {
|
|
label: 'Dibayar',
|
|
className: 'bg-blue-100 text-blue-800 hover:bg-blue-100',
|
|
},
|
|
rejected: {
|
|
label: 'Ditolak',
|
|
className: 'bg-red-100 text-red-800 hover:bg-red-100',
|
|
},
|
|
cancelled: {
|
|
label: 'Dibatalkan',
|
|
className: 'bg-gray-100 text-gray-800 hover:bg-gray-100',
|
|
},
|
|
};
|
|
|
|
const config = statusConfig[status] ?? statusConfig.pending;
|
|
|
|
return (
|
|
<Badge variant="secondary" className={config.className}>
|
|
{config.label}
|
|
</Badge>
|
|
);
|
|
}
|
|
|
|
type CreateColumnsParams = {
|
|
handleEdit: (employeeAdvance: EmployeeAdvance) => void;
|
|
handleDeleteClick: (employeeAdvance: EmployeeAdvance) => void;
|
|
handleApprove: (employeeAdvance: EmployeeAdvance) => void;
|
|
handlePay: (employeeAdvance: EmployeeAdvance) => void;
|
|
};
|
|
|
|
export function createEmployeeAdvanceColumns(
|
|
params: CreateColumnsParams,
|
|
): ColumnDef<EmployeeAdvance>[] {
|
|
const { handleEdit, handleDeleteClick, handleApprove, handlePay } = params;
|
|
|
|
return [
|
|
{
|
|
id: 'no',
|
|
header: () => <span className="block text-center">No</span>,
|
|
cell: ({ row }) => (
|
|
<span className="block text-center">
|
|
{row.index + 1}
|
|
</span>
|
|
),
|
|
meta: {
|
|
className: 'w-[50px] text-center',
|
|
headerClassName: 'w-[50px] text-center',
|
|
},
|
|
},
|
|
{
|
|
accessorKey: 'created_at',
|
|
header: ({ column }) => (
|
|
<Button
|
|
variant="ghost"
|
|
className="-ml-3 h-8"
|
|
onClick={() =>
|
|
column.toggleSorting(
|
|
column.getIsSorted() === 'asc',
|
|
)
|
|
}
|
|
>
|
|
<span>Tanggal</span>
|
|
<ArrowUpDown className="ml-2 h-4 w-4" />
|
|
</Button>
|
|
),
|
|
cell: ({ row }) => (
|
|
<span>{formatDate(row.getValue('created_at') as string)}</span>
|
|
),
|
|
},
|
|
{
|
|
id: 'employee_name',
|
|
header: () => <span>Oleh</span>,
|
|
cell: ({ row }) => {
|
|
const employee = row.original.employee;
|
|
|
|
return <span>{employee?.user?.user_profile?.full_name ?? '-'}</span>;
|
|
},
|
|
},
|
|
{
|
|
accessorKey: 'amount',
|
|
header: ({ column }) => (
|
|
<Button
|
|
variant="ghost"
|
|
className="-ml-3 h-8"
|
|
onClick={() =>
|
|
column.toggleSorting(
|
|
column.getIsSorted() === 'asc',
|
|
)
|
|
}
|
|
>
|
|
<span>Jumlah</span>
|
|
<ArrowUpDown className="ml-2 h-4 w-4" />
|
|
</Button>
|
|
),
|
|
cell: ({ row }) => (
|
|
<span className="text-red-600 font-medium">
|
|
- {formatCurrency(row.getValue('amount') as number)}
|
|
</span>
|
|
),
|
|
},
|
|
{
|
|
accessorKey: 'description',
|
|
header: () => <span>Keterangan</span>,
|
|
cell: ({ row }) => (
|
|
<span className="max-w-[200px] truncate block">{row.getValue('description') as string}</span>
|
|
),
|
|
},
|
|
{
|
|
accessorKey: 'due_date',
|
|
header: ({ column }) => (
|
|
<Button
|
|
variant="ghost"
|
|
className="-ml-3 h-8"
|
|
onClick={() =>
|
|
column.toggleSorting(
|
|
column.getIsSorted() === 'asc',
|
|
)
|
|
}
|
|
>
|
|
<span>Jatuh Tempo</span>
|
|
<ArrowUpDown className="ml-2 h-4 w-4" />
|
|
</Button>
|
|
),
|
|
cell: ({ row }) => (
|
|
<span>{formatShortDate(row.getValue('due_date') as string)}</span>
|
|
),
|
|
},
|
|
{
|
|
accessorKey: 'status',
|
|
header: () => <span>Status</span>,
|
|
cell: ({ row }) => (
|
|
<span>{getStatusBadge(row.getValue('status') as string)}</span>
|
|
),
|
|
},
|
|
{
|
|
id: 'actions',
|
|
header: () => <span className="block text-center">Aksi</span>,
|
|
meta: {
|
|
className: 'w-[150px] text-center',
|
|
headerClassName: 'w-[150px] text-center',
|
|
},
|
|
cell: ({ row }) => {
|
|
const employeeAdvance = row.original;
|
|
|
|
return (
|
|
<TooltipProvider>
|
|
<div className="flex items-center justify-center gap-1">
|
|
{employeeAdvance.status === 'pending' && (
|
|
<Tooltip>
|
|
<TooltipTrigger asChild>
|
|
<Button
|
|
variant="ghost"
|
|
size="icon"
|
|
onClick={() =>
|
|
handleApprove(employeeAdvance)
|
|
}
|
|
>
|
|
<CheckCircle className="h-4 w-4 text-green-600" />
|
|
</Button>
|
|
</TooltipTrigger>
|
|
<TooltipContent side="top">
|
|
Setujui
|
|
</TooltipContent>
|
|
</Tooltip>
|
|
)}
|
|
|
|
{employeeAdvance.status === 'approved' && (
|
|
<Tooltip>
|
|
<TooltipTrigger asChild>
|
|
<Button
|
|
variant="ghost"
|
|
size="icon"
|
|
onClick={() =>
|
|
handlePay(employeeAdvance)
|
|
}
|
|
>
|
|
<CircleDollarSign className="h-4 w-4 text-blue-600" />
|
|
</Button>
|
|
</TooltipTrigger>
|
|
<TooltipContent side="top">
|
|
Bayar
|
|
</TooltipContent>
|
|
</Tooltip>
|
|
)}
|
|
|
|
<Tooltip>
|
|
<TooltipTrigger asChild>
|
|
<Button
|
|
variant="ghost"
|
|
size="icon"
|
|
onClick={() =>
|
|
handleEdit(employeeAdvance)
|
|
}
|
|
>
|
|
<Pencil className="h-4 w-4" />
|
|
</Button>
|
|
</TooltipTrigger>
|
|
<TooltipContent side="top">
|
|
Edit
|
|
</TooltipContent>
|
|
</Tooltip>
|
|
|
|
<Tooltip>
|
|
<TooltipTrigger asChild>
|
|
<Button
|
|
variant="ghost"
|
|
size="icon"
|
|
onClick={() =>
|
|
handleDeleteClick(employeeAdvance)
|
|
}
|
|
>
|
|
<Trash2 className="h-4 w-4 text-destructive" />
|
|
</Button>
|
|
</TooltipTrigger>
|
|
<TooltipContent side="top">
|
|
Hapus
|
|
</TooltipContent>
|
|
</Tooltip>
|
|
</div>
|
|
</TooltipProvider>
|
|
);
|
|
},
|
|
},
|
|
];
|
|
}
|