import { Head, router, usePage } from '@inertiajs/react'; import { Plus } from 'lucide-react'; import { useEffect, useState } from 'react'; import type { PaginationState } from '@/components/data-display'; import { DataTable } from '@/components/data-display'; import { DatePicker } from '@/components/inputs'; import { DeleteConfirmDialog } from '@/components/dialogs'; import { FilterPopover } from '@/components/data-display'; import { FormDialog } from '@/components/dialogs'; import { InputError } from '@/components/ui'; import { PageHeader } from '@/components/layout'; import { RupiahInput } from '@/components/inputs'; import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { Separator } from '@/components/ui/separator'; import { useCan } from '@/hooks/use-can'; import { useServerTable } from '@/hooks/use-server-table'; import { approve, destroy, index as employeeAdvanceIndex, pay, store, update, } from '@/routes/admin/finance/employee-advances'; import type { EmployeeAdvance } from './columns'; import { createEmployeeAdvanceColumns } from './columns'; type TypeOption = { value: string; label: string; }; type Props = { employeeAdvances: { data: EmployeeAdvance[]; current_page: number; last_page: number; per_page: number; total: number; }; filters: { status?: string; }; filterOptions: { statusOptions: TypeOption[]; }; highlight?: number; }; export default function EmployeeAdvanceIndex({ employeeAdvances, filters, filterOptions, highlight, }: Props) { const { can } = useCan(); const { auth } = usePage().props as { auth: { user: { id: number } } }; const [createOpen, setCreateOpen] = useState(false); const [editing, setEditing] = useState(null); const [deleting, setDeleting] = useState(null); const [approving, setApproving] = useState(null); const [paying, setPaying] = useState(null); const [viewingPayments, setViewingPayments] = useState(null); const [dueDate, setDueDate] = useState(undefined); const [editingDueDate, setEditingDueDate] = useState( undefined, ); const pagination: PaginationState = { current_page: employeeAdvances.current_page, last_page: employeeAdvances.last_page, per_page: employeeAdvances.per_page, total: employeeAdvances.total, }; const { search, filterOpen, setFilterOpen, handlePageChange, handlePerPageChange, handleSearchChange, applyFilter, clearFilters, } = useServerTable({ route: () => employeeAdvanceIndex.url(), pagination, filters, }); useEffect(() => { if (editing) { setEditingDueDate(new Date(editing.due_date)); } else { setEditingDueDate(undefined); } }, [editing]); function handleDelete() { if (!deleting) { return; } router.delete(destroy(deleting.id), { onSuccess: () => setDeleting(null), }); } function handleApprove() { if (!approving) { return; } router.post( approve(approving.id), {}, { onSuccess: () => setApproving(null), }, ); } const columns = createEmployeeAdvanceColumns({ handleEdit: (employeeAdvance) => setEditing(employeeAdvance), handleDeleteClick: (employeeAdvance) => setDeleting(employeeAdvance), handleApprove: (employeeAdvance) => setApproving(employeeAdvance), handlePay: (employeeAdvance) => setPaying(employeeAdvance), handleShowPayments: (employeeAdvance) => setViewingPayments(employeeAdvance), can, authUserId: auth.user.id, }); const filterToolbar = (
); return ( <>
Menampilkan kasbon dari notifikasi.

) } actions={ can('employee_advances.create') ? ( ) : undefined } /> { setCreateOpen(open); if (!open) { setDueDate(undefined); } }} title="Tambah Kasbon" action={store()} resetOnSuccess onSuccess={() => setCreateOpen(false)} > {({ errors }) => ( <>
)}
{ if (!open) { setEditing(null); setEditingDueDate(undefined); } }} title="Edit Kasbon" action={editing ? update(editing.id) : ''} resetOnSuccess onSuccess={() => { setEditing(null); setEditingDueDate(undefined); }} > {({ errors }) => editing && ( <>
) }
{ if (!open) { setDeleting(null); } }} title="Hapus Kasbon" description={(advance) => `Apakah Anda yakin ingin menghapus kasbon "${advance.description}"? Tindakan ini tidak dapat dibatalkan.` } onConfirm={handleDelete} /> { if (!open) { setApproving(null); } }} title="Setujui Kasbon" description={(advance) => `Apakah Anda yakin ingin menyetujui kasbon "${advance.description}"?` } confirmLabel="Setujui" onConfirm={handleApprove} /> { if (!open) { setPaying(null); } }} title="Bayar Kasbon" action={paying ? pay(paying.id) : ''} resetOnSuccess onSuccess={() => setPaying(null)} > {({ errors }) => paying && ( <>

Sisa:{' '} {paying.formatted_remaining_amount}

) }
{ if (!open) { setViewingPayments(null); } }} > Riwayat Pembayaran {viewingPayments?.description} {viewingPayments && (
Total: {viewingPayments.formatted_amount}
Terbayar: {viewingPayments.formatted_paid_amount}
Sisa: {viewingPayments.formatted_remaining_amount}
{viewingPayments.payments.length === 0 ? (

Belum ada pembayaran.

) : (
{viewingPayments.payments.map( (payment) => (
{payment.formatted_amount} {payment.formatted_paid_at}
{payment.paid_by && (

oleh{' '} {payment.paid_by .user_profile .full_name ?? '-'}

)}
), )}
)}
)}
); }