feat: add filtering options for employee advances by status in index view
This commit is contained in:
parent
175decfa22
commit
81b5db77e2
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers\Admin\Finance;
|
namespace App\Http\Controllers\Admin\Finance;
|
||||||
|
|
||||||
|
use App\Enums\EmployeeAdvanceStatus;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Http\Requests\Admin\Finance\EmployeeAdvanceRequest;
|
use App\Http\Requests\Admin\Finance\EmployeeAdvanceRequest;
|
||||||
use App\Http\Requests\PaginatedRequest;
|
use App\Http\Requests\PaginatedRequest;
|
||||||
@ -20,7 +21,14 @@ public function __construct(
|
|||||||
public function index(PaginatedRequest $request): Response
|
public function index(PaginatedRequest $request): Response
|
||||||
{
|
{
|
||||||
return Inertia::render('admin/finance/employee-advance/index', [
|
return Inertia::render('admin/finance/employee-advance/index', [
|
||||||
'employeeAdvances' => $this->service->paginated(...$request->validatedWithDefaults()),
|
'employeeAdvances' => $this->service->paginated(
|
||||||
|
...$request->validatedWithDefaults(),
|
||||||
|
filters: $request->only(['status']),
|
||||||
|
),
|
||||||
|
'filters' => $request->only(['status']),
|
||||||
|
'filterOptions' => [
|
||||||
|
'statusOptions' => EmployeeAdvanceStatus::toSelect(),
|
||||||
|
],
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -3,23 +3,27 @@
|
|||||||
namespace App\Services\Admin\Finance;
|
namespace App\Services\Admin\Finance;
|
||||||
|
|
||||||
use App\Enums\EmployeeAdvanceStatus;
|
use App\Enums\EmployeeAdvanceStatus;
|
||||||
use App\Models\CashAccount;
|
|
||||||
use App\Models\EmployeeAdvance;
|
use App\Models\EmployeeAdvance;
|
||||||
use App\Services\Concerns\HandlesCashTransactions;
|
use App\Services\Concerns\HandlesCashTransactions;
|
||||||
use App\Services\NotificationService;
|
use App\Services\NotificationService;
|
||||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Validation\ValidationException;
|
|
||||||
|
|
||||||
class EmployeeAdvanceService
|
class EmployeeAdvanceService
|
||||||
{
|
{
|
||||||
use HandlesCashTransactions;
|
use HandlesCashTransactions;
|
||||||
|
|
||||||
|
private function canViewAll(): bool
|
||||||
|
{
|
||||||
|
return auth()->user()->hasAnyRole(['developer', 'owner', 'direktur', 'admin-toko']);
|
||||||
|
}
|
||||||
|
|
||||||
public function getAll(array $filters = []): Collection
|
public function getAll(array $filters = []): Collection
|
||||||
{
|
{
|
||||||
return EmployeeAdvance::select(['id', 'employee_id', 'amount', 'paid_amount', 'description', 'due_date', 'status', 'created_at'])
|
return EmployeeAdvance::select(['id', 'employee_id', 'amount', 'paid_amount', 'description', 'due_date', 'status', 'created_at'])
|
||||||
->with(['employee.user.userProfile'])
|
->with(['employee.user.userProfile'])
|
||||||
|
->when(! $this->canViewAll(), fn ($q) => $q->whereHas('employee', fn ($eq) => $eq->where('user_id', auth()->id())))
|
||||||
->latest()
|
->latest()
|
||||||
->get();
|
->get();
|
||||||
}
|
}
|
||||||
@ -29,6 +33,8 @@ public function paginated(int $perPage = 25, string $search = '', string $sort =
|
|||||||
return EmployeeAdvance::query()
|
return EmployeeAdvance::query()
|
||||||
->select(['id', 'employee_id', 'amount', 'paid_amount', 'description', 'due_date', 'status', 'created_at'])
|
->select(['id', 'employee_id', 'amount', 'paid_amount', 'description', 'due_date', 'status', 'created_at'])
|
||||||
->with(['employee.user.userProfile'])
|
->with(['employee.user.userProfile'])
|
||||||
|
->when(! $this->canViewAll(), fn ($q) => $q->whereHas('employee', fn ($eq) => $eq->where('user_id', auth()->id())))
|
||||||
|
->when($filters['status'] ?? null, fn ($q) => $q->where('status', $filters['status']))
|
||||||
->when($search, fn ($q) => $q->where('description', 'like', "%{$search}%"))
|
->when($search, fn ($q) => $q->where('description', 'like', "%{$search}%"))
|
||||||
->orderBy($sort, $direction)
|
->orderBy($sort, $direction)
|
||||||
->paginate($perPage);
|
->paginate($perPage);
|
||||||
@ -36,27 +42,19 @@ public function paginated(int $perPage = 25, string $search = '', string $sort =
|
|||||||
|
|
||||||
public function create(array $data): EmployeeAdvance
|
public function create(array $data): EmployeeAdvance
|
||||||
{
|
{
|
||||||
$employeeAdvance = DB::transaction(function () use ($data) {
|
$employee = auth()->user()->employee;
|
||||||
$employee = auth()->user()->employee;
|
|
||||||
|
|
||||||
if (! $employee) {
|
if (! $employee) {
|
||||||
throw new \Exception('Anda tidak terdaftar sebagai karyawan.');
|
throw new \Exception('Anda tidak terdaftar sebagai karyawan.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$cashTransaction = $this->debitCash(
|
$employeeAdvance = EmployeeAdvance::create([
|
||||||
amount: $data['amount'],
|
'employee_id' => $employee->id,
|
||||||
description: 'Kasbon: '.$data['description'],
|
'amount' => $data['amount'],
|
||||||
);
|
'description' => $data['description'],
|
||||||
|
'due_date' => $data['due_date'],
|
||||||
return EmployeeAdvance::create([
|
'status' => EmployeeAdvanceStatus::PENDING,
|
||||||
'cash_transaction_id' => $cashTransaction->id,
|
]);
|
||||||
'employee_id' => $employee->id,
|
|
||||||
'amount' => $data['amount'],
|
|
||||||
'description' => $data['description'],
|
|
||||||
'due_date' => $data['due_date'],
|
|
||||||
'status' => EmployeeAdvanceStatus::PENDING,
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
|
|
||||||
$employeeAdvance->load('employee.user');
|
$employeeAdvance->load('employee.user');
|
||||||
|
|
||||||
@ -73,56 +71,36 @@ public function create(array $data): EmployeeAdvance
|
|||||||
|
|
||||||
public function update(EmployeeAdvance $employeeAdvance, array $data): EmployeeAdvance
|
public function update(EmployeeAdvance $employeeAdvance, array $data): EmployeeAdvance
|
||||||
{
|
{
|
||||||
return DB::transaction(function () use ($employeeAdvance, $data) {
|
$employeeAdvance->update([
|
||||||
$cashAccount = CashAccount::firstOrFail();
|
'amount' => $data['amount'],
|
||||||
$cashTransaction = $employeeAdvance->cashTransaction;
|
'description' => $data['description'],
|
||||||
|
'due_date' => $data['due_date'],
|
||||||
|
]);
|
||||||
|
|
||||||
$oldAmount = $employeeAdvance->amount;
|
return $employeeAdvance;
|
||||||
$newAmount = $data['amount'];
|
|
||||||
$difference = $newAmount - $oldAmount;
|
|
||||||
|
|
||||||
if ($difference > 0 && $cashAccount->balance < $difference) {
|
|
||||||
throw ValidationException::withMessages([
|
|
||||||
'amount' => 'Saldo tidak mencukupi.',
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
$newBalance = $cashAccount->balance - $difference;
|
|
||||||
$cashAccount->update(['balance' => $newBalance]);
|
|
||||||
|
|
||||||
$cashTransaction->update([
|
|
||||||
'amount' => $newAmount,
|
|
||||||
'balance_after' => $newBalance,
|
|
||||||
'description' => 'Kasbon: '.$data['description'],
|
|
||||||
]);
|
|
||||||
|
|
||||||
$employeeAdvance->update([
|
|
||||||
'amount' => $newAmount,
|
|
||||||
'description' => $data['description'],
|
|
||||||
'due_date' => $data['due_date'],
|
|
||||||
]);
|
|
||||||
|
|
||||||
return $employeeAdvance;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function delete(EmployeeAdvance $employeeAdvance): bool
|
public function delete(EmployeeAdvance $employeeAdvance): bool
|
||||||
{
|
{
|
||||||
return DB::transaction(function () use ($employeeAdvance) {
|
if ($employeeAdvance->status === EmployeeAdvanceStatus::APPROVED && $employeeAdvance->cash_transaction_id) {
|
||||||
$cashAccount = CashAccount::firstOrFail();
|
$cashAccount = $this->getCashAccount();
|
||||||
|
$newBalance = $cashAccount->balance + $employeeAdvance->amount;
|
||||||
|
$cashAccount->update(['balance' => $newBalance]);
|
||||||
|
$employeeAdvance->cashTransaction()->delete();
|
||||||
|
}
|
||||||
|
|
||||||
if ($employeeAdvance->status === EmployeeAdvanceStatus::PAID) {
|
return $employeeAdvance->delete();
|
||||||
$newBalance = $cashAccount->balance + $employeeAdvance->amount;
|
|
||||||
$cashAccount->update(['balance' => $newBalance]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $employeeAdvance->delete();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function approve(EmployeeAdvance $employeeAdvance): EmployeeAdvance
|
public function approve(EmployeeAdvance $employeeAdvance): EmployeeAdvance
|
||||||
{
|
{
|
||||||
|
$cashTransaction = $this->debitCash(
|
||||||
|
amount: $employeeAdvance->amount,
|
||||||
|
description: 'Kasbon: '.$employeeAdvance->description,
|
||||||
|
);
|
||||||
|
|
||||||
$employeeAdvance->update([
|
$employeeAdvance->update([
|
||||||
|
'cash_transaction_id' => $cashTransaction->id,
|
||||||
'status' => EmployeeAdvanceStatus::APPROVED,
|
'status' => EmployeeAdvanceStatus::APPROVED,
|
||||||
'verified_by_id' => auth()->id(),
|
'verified_by_id' => auth()->id(),
|
||||||
'verified_at' => now(),
|
'verified_at' => now(),
|
||||||
|
|||||||
@ -1,10 +1,8 @@
|
|||||||
import { Head, router } from '@inertiajs/react';
|
|
||||||
import { Plus } from 'lucide-react';
|
|
||||||
import { useEffect, useState } from 'react';
|
|
||||||
import type { PaginationState } from '@/components/data-table';
|
import type { PaginationState } from '@/components/data-table';
|
||||||
import { DataTable } from '@/components/data-table';
|
import { DataTable } from '@/components/data-table';
|
||||||
import { DatePicker } from '@/components/date-picker';
|
import { DatePicker } from '@/components/date-picker';
|
||||||
import { DeleteConfirmDialog } from '@/components/delete-confirm-dialog';
|
import { DeleteConfirmDialog } from '@/components/delete-confirm-dialog';
|
||||||
|
import { FilterPopover } from '@/components/filter-popover';
|
||||||
import { FormDialog } from '@/components/form-dialog';
|
import { FormDialog } from '@/components/form-dialog';
|
||||||
import InputError from '@/components/input-error';
|
import InputError from '@/components/input-error';
|
||||||
import { PageHeader } from '@/components/page-header';
|
import { PageHeader } from '@/components/page-header';
|
||||||
@ -12,18 +10,33 @@ import { RupiahInput } from '@/components/rupiah-input';
|
|||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
import { Input } from '@/components/ui/input';
|
import { Input } from '@/components/ui/input';
|
||||||
import { Label } from '@/components/ui/label';
|
import { Label } from '@/components/ui/label';
|
||||||
|
import {
|
||||||
|
Select,
|
||||||
|
SelectContent,
|
||||||
|
SelectItem,
|
||||||
|
SelectTrigger,
|
||||||
|
SelectValue,
|
||||||
|
} from '@/components/ui/select';
|
||||||
import { useCan } from '@/hooks/use-can';
|
import { useCan } from '@/hooks/use-can';
|
||||||
import { useServerTable } from '@/hooks/use-server-table';
|
import { useServerTable } from '@/hooks/use-server-table';
|
||||||
import {
|
import {
|
||||||
|
approve,
|
||||||
destroy,
|
destroy,
|
||||||
index as employeeAdvanceIndex,
|
index as employeeAdvanceIndex,
|
||||||
|
pay,
|
||||||
store,
|
store,
|
||||||
update,
|
update,
|
||||||
approve,
|
|
||||||
pay,
|
|
||||||
} from '@/routes/admin/finance/employee-advances';
|
} from '@/routes/admin/finance/employee-advances';
|
||||||
import { createEmployeeAdvanceColumns } from './columns';
|
import { Head, router, usePage } from '@inertiajs/react';
|
||||||
|
import { Plus } from 'lucide-react';
|
||||||
|
import { useEffect, useState } from 'react';
|
||||||
import type { EmployeeAdvance } from './columns';
|
import type { EmployeeAdvance } from './columns';
|
||||||
|
import { createEmployeeAdvanceColumns } from './columns';
|
||||||
|
|
||||||
|
type TypeOption = {
|
||||||
|
value: string;
|
||||||
|
label: string;
|
||||||
|
};
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
employeeAdvances: {
|
employeeAdvances: {
|
||||||
@ -33,10 +46,21 @@ type Props = {
|
|||||||
per_page: number;
|
per_page: number;
|
||||||
total: number;
|
total: number;
|
||||||
};
|
};
|
||||||
|
filters: {
|
||||||
|
status?: string;
|
||||||
|
};
|
||||||
|
filterOptions: {
|
||||||
|
statusOptions: TypeOption[];
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function EmployeeAdvanceIndex({ employeeAdvances }: Props) {
|
export default function EmployeeAdvanceIndex({
|
||||||
|
employeeAdvances,
|
||||||
|
filters,
|
||||||
|
filterOptions,
|
||||||
|
}: Props) {
|
||||||
const { can } = useCan();
|
const { can } = useCan();
|
||||||
|
const { auth } = usePage().props as { auth: { user: { id: number } } };
|
||||||
const [createOpen, setCreateOpen] = useState(false);
|
const [createOpen, setCreateOpen] = useState(false);
|
||||||
const [editing, setEditing] = useState<EmployeeAdvance | null>(null);
|
const [editing, setEditing] = useState<EmployeeAdvance | null>(null);
|
||||||
const [deleting, setDeleting] = useState<EmployeeAdvance | null>(null);
|
const [deleting, setDeleting] = useState<EmployeeAdvance | null>(null);
|
||||||
@ -56,12 +80,17 @@ export default function EmployeeAdvanceIndex({ employeeAdvances }: Props) {
|
|||||||
|
|
||||||
const {
|
const {
|
||||||
search,
|
search,
|
||||||
|
filterOpen,
|
||||||
|
setFilterOpen,
|
||||||
handlePageChange,
|
handlePageChange,
|
||||||
handlePerPageChange,
|
handlePerPageChange,
|
||||||
handleSearchChange,
|
handleSearchChange,
|
||||||
|
applyFilter,
|
||||||
|
clearFilters,
|
||||||
} = useServerTable({
|
} = useServerTable({
|
||||||
route: () => employeeAdvanceIndex.url(),
|
route: () => employeeAdvanceIndex.url(),
|
||||||
pagination,
|
pagination,
|
||||||
|
filters,
|
||||||
});
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -116,8 +145,39 @@ export default function EmployeeAdvanceIndex({ employeeAdvances }: Props) {
|
|||||||
handleApprove: (employeeAdvance) => setApproving(employeeAdvance),
|
handleApprove: (employeeAdvance) => setApproving(employeeAdvance),
|
||||||
handlePay: (employeeAdvance) => setPaying(employeeAdvance),
|
handlePay: (employeeAdvance) => setPaying(employeeAdvance),
|
||||||
can,
|
can,
|
||||||
|
authUserId: auth.user.id,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const filterToolbar = (
|
||||||
|
<FilterPopover
|
||||||
|
open={filterOpen}
|
||||||
|
onOpenChange={setFilterOpen}
|
||||||
|
filters={filters}
|
||||||
|
hasActiveFilters={Boolean(filters.status)}
|
||||||
|
onClear={clearFilters}
|
||||||
|
>
|
||||||
|
<div className="flex flex-col gap-2">
|
||||||
|
<label className="text-xs text-muted-foreground">Status</label>
|
||||||
|
<Select
|
||||||
|
value={filters.status ?? 'all'}
|
||||||
|
onValueChange={(value) => applyFilter('status', value)}
|
||||||
|
>
|
||||||
|
<SelectTrigger className="w-full">
|
||||||
|
<SelectValue placeholder="Semua Status" />
|
||||||
|
</SelectTrigger>
|
||||||
|
<SelectContent>
|
||||||
|
<SelectItem value="all">Semua Status</SelectItem>
|
||||||
|
{filterOptions.statusOptions.map((opt) => (
|
||||||
|
<SelectItem key={opt.value} value={opt.value}>
|
||||||
|
{opt.label}
|
||||||
|
</SelectItem>
|
||||||
|
))}
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
</div>
|
||||||
|
</FilterPopover>
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Head title="Kasbon" />
|
<Head title="Kasbon" />
|
||||||
@ -187,8 +247,8 @@ export default function EmployeeAdvanceIndex({ employeeAdvances }: Props) {
|
|||||||
value={
|
value={
|
||||||
dueDate
|
dueDate
|
||||||
? dueDate
|
? dueDate
|
||||||
.toISOString()
|
.toISOString()
|
||||||
.split('T')[0]
|
.split('T')[0]
|
||||||
: ''
|
: ''
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
@ -214,6 +274,7 @@ export default function EmployeeAdvanceIndex({ employeeAdvances }: Props) {
|
|||||||
onPerPageChange={handlePerPageChange}
|
onPerPageChange={handlePerPageChange}
|
||||||
onSearchChange={handleSearchChange}
|
onSearchChange={handleSearchChange}
|
||||||
searchValue={search}
|
searchValue={search}
|
||||||
|
toolbar={filterToolbar}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<FormDialog
|
<FormDialog
|
||||||
@ -276,8 +337,8 @@ export default function EmployeeAdvanceIndex({ employeeAdvances }: Props) {
|
|||||||
value={
|
value={
|
||||||
editingDueDate
|
editingDueDate
|
||||||
? editingDueDate
|
? editingDueDate
|
||||||
.toISOString()
|
.toISOString()
|
||||||
.split('T')[0]
|
.split('T')[0]
|
||||||
: ''
|
: ''
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
@ -331,7 +392,7 @@ export default function EmployeeAdvanceIndex({ employeeAdvances }: Props) {
|
|||||||
}}
|
}}
|
||||||
title="Bayar Kasbon"
|
title="Bayar Kasbon"
|
||||||
description={(advance) =>
|
description={(advance) =>
|
||||||
`Apakah Anda yakin ingin membayar kasbon "${advance.description}" sebesar ${advance.amount}? Saldo kas akan dikembalikan.`
|
`Apakah Anda yakin ingin membayar kasbon "${advance.description}" sebesar ${advance.formatted_amount}? Saldo kas akan dikembalikan.`
|
||||||
}
|
}
|
||||||
confirmLabel="Bayar"
|
confirmLabel="Bayar"
|
||||||
onConfirm={handlePay}
|
onConfirm={handlePay}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user