diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index ceea408..30941ea 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -15,7 +15,7 @@ protected function handleAction(callable $action, string $successMessage, string $action(); Inertia::flash('toast', ['type' => 'success', 'message' => $successMessage]); - return to_route($redirectRoute); + return to_route($redirectRoute, $parameters); } catch (ValidationException $e) { $firstError = collect($e->errors())->flatten()->first(); Inertia::flash('toast', ['type' => 'error', 'message' => $firstError ?? 'Terjadi kesalahan.']); diff --git a/app/Services/Admin/Finance/PayrollPeriodService.php b/app/Services/Admin/Finance/PayrollPeriodService.php index a1eab3d..d2eee81 100644 --- a/app/Services/Admin/Finance/PayrollPeriodService.php +++ b/app/Services/Admin/Finance/PayrollPeriodService.php @@ -17,19 +17,30 @@ class PayrollPeriodService { use HandlesCashTransactions; + private function canViewAll(): bool + { + return auth()->user()->hasAnyRole(['developer', 'owner', 'direktur', 'admin-toko']); + } + public function getAll(array $filters = []): Collection { return PayrollPeriod::select(['id', 'year', 'month', 'status', 'closed_at', 'created_at']) - ->withCount('payrolls') - ->withSum('payrolls', 'total_amount') - ->withSum('payrolls', 'bonus_amount') - ->withSum('payrolls', 'deduction_amount') - ->withCount(['payrolls as paid_count' => function ($q) { - $q->paid(); - }]) - ->withCount(['payrolls as cancelled_count' => function ($q) { - $q->cancelled(); - }]) + ->when(! $this->canViewAll(), function ($query) { + $query->withCount(['payrolls as payrolls_count' => fn ($q) => $q->whereHas('employee', fn ($eq) => $eq->where('user_id', auth()->id()))]) + ->withSum(['payrolls as payrolls_sum_total_amount' => fn ($q) => $q->whereHas('employee', fn ($eq) => $eq->where('user_id', auth()->id()))], 'total_amount') + ->withSum(['payrolls as payrolls_sum_bonus_amount' => fn ($q) => $q->whereHas('employee', fn ($eq) => $eq->where('user_id', auth()->id()))], 'bonus_amount') + ->withSum(['payrolls as payrolls_sum_deduction_amount' => fn ($q) => $q->whereHas('employee', fn ($eq) => $eq->where('user_id', auth()->id()))], 'deduction_amount') + ->withCount(['payrolls as paid_count' => fn ($q) => $q->paid()->whereHas('employee', fn ($eq) => $eq->where('user_id', auth()->id()))]) + ->withCount(['payrolls as cancelled_count' => fn ($q) => $q->cancelled()->whereHas('employee', fn ($eq) => $eq->where('user_id', auth()->id()))]); + }) + ->when($this->canViewAll(), function ($query) { + $query->withCount('payrolls') + ->withSum('payrolls', 'total_amount') + ->withSum('payrolls', 'bonus_amount') + ->withSum('payrolls', 'deduction_amount') + ->withCount(['payrolls as paid_count' => fn ($q) => $q->paid()]) + ->withCount(['payrolls as cancelled_count' => fn ($q) => $q->cancelled()]); + }) ->latest('year') ->latest('month') ->get(); @@ -39,16 +50,22 @@ public function paginated(int $perPage = 25, string $search = '', string $sort = { return PayrollPeriod::query() ->select(['id', 'year', 'month', 'status', 'closed_at', 'created_at']) - ->withCount('payrolls') - ->withSum('payrolls', 'total_amount') - ->withSum('payrolls', 'bonus_amount') - ->withSum('payrolls', 'deduction_amount') - ->withCount(['payrolls as paid_count' => function ($q) { - $q->paid(); - }]) - ->withCount(['payrolls as cancelled_count' => function ($q) { - $q->cancelled(); - }]) + ->when(! $this->canViewAll(), function ($query) { + $query->withCount(['payrolls as payrolls_count' => fn ($q) => $q->whereHas('employee', fn ($eq) => $eq->where('user_id', auth()->id()))]) + ->withSum(['payrolls as payrolls_sum_total_amount' => fn ($q) => $q->whereHas('employee', fn ($eq) => $eq->where('user_id', auth()->id()))], 'total_amount') + ->withSum(['payrolls as payrolls_sum_bonus_amount' => fn ($q) => $q->whereHas('employee', fn ($eq) => $eq->where('user_id', auth()->id()))], 'bonus_amount') + ->withSum(['payrolls as payrolls_sum_deduction_amount' => fn ($q) => $q->whereHas('employee', fn ($eq) => $eq->where('user_id', auth()->id()))], 'deduction_amount') + ->withCount(['payrolls as paid_count' => fn ($q) => $q->paid()->whereHas('employee', fn ($eq) => $eq->where('user_id', auth()->id()))]) + ->withCount(['payrolls as cancelled_count' => fn ($q) => $q->cancelled()->whereHas('employee', fn ($eq) => $eq->where('user_id', auth()->id()))]); + }) + ->when($this->canViewAll(), function ($query) { + $query->withCount('payrolls') + ->withSum('payrolls', 'total_amount') + ->withSum('payrolls', 'bonus_amount') + ->withSum('payrolls', 'deduction_amount') + ->withCount(['payrolls as paid_count' => fn ($q) => $q->paid()]) + ->withCount(['payrolls as cancelled_count' => fn ($q) => $q->cancelled()]); + }) ->when($search, fn ($q) => $q->where('year', 'like', "%{$search}%")) ->orderBy($sort, $direction) ->paginate($perPage); @@ -59,6 +76,7 @@ public function getDetail(PayrollPeriod $period): PayrollPeriod return $period->load([ 'payrolls' => function ($query) { $query->with(['employee.user.userProfile', 'payrollAdjustments']) + ->when(! $this->canViewAll(), fn ($q) => $q->whereHas('employee', fn ($eq) => $eq->where('user_id', auth()->id()))) ->orderBy('id'); }, ]); diff --git a/resources/js/pages/admin/finance/payroll-period/columns.tsx b/resources/js/pages/admin/finance/payroll-period/columns.tsx index 541782b..329a599 100644 --- a/resources/js/pages/admin/finance/payroll-period/columns.tsx +++ b/resources/js/pages/admin/finance/payroll-period/columns.tsx @@ -50,14 +50,15 @@ type CreateColumnsParams = { handleClose: (period: PayrollPeriod) => void; handleReopen: (period: PayrollPeriod) => void; can: (permission: string) => boolean; + canViewAll: boolean; }; export function createPayrollPeriodColumns( params: CreateColumnsParams, ): ColumnDef[] { - const { showUrl, handleClose, handleReopen, can } = params; + const { showUrl, handleClose, handleReopen, can, canViewAll } = params; - return [ + const columns: ColumnDef[] = [ { accessorKey: 'year', header: () => Periode, @@ -67,7 +68,10 @@ export function createPayrollPeriodColumns( ), }, - { + ]; + + if (canViewAll) { + columns.push({ accessorKey: 'payrolls_count', header: () => Jumlah Karyawan, cell: ({ row }) => ( @@ -75,7 +79,10 @@ export function createPayrollPeriodColumns( {row.getValue('payrolls_count') as number} ), - }, + }); + } + + columns.push( { accessorKey: 'payrolls_sum_total_amount', header: () => Total Gaji, @@ -131,7 +138,10 @@ export function createPayrollPeriodColumns( ); }, }, - { + ); + + if (canViewAll) { + columns.push({ id: 'payment_status', header: () => Status Bayar, cell: ({ row }) => { @@ -161,7 +171,10 @@ export function createPayrollPeriodColumns( ); }, - }, + }); + } + + columns.push( { accessorKey: 'status', header: () => Status, @@ -212,5 +225,7 @@ export function createPayrollPeriodColumns( ); }, }, - ]; + ); + + return columns; } diff --git a/resources/js/pages/admin/finance/payroll-period/index.tsx b/resources/js/pages/admin/finance/payroll-period/index.tsx index afc94b3..7a90eae 100644 --- a/resources/js/pages/admin/finance/payroll-period/index.tsx +++ b/resources/js/pages/admin/finance/payroll-period/index.tsx @@ -27,7 +27,8 @@ type Props = { }; export default function PayrollPeriodIndex({ payrollPeriods }: Props) { - const { can } = useCan(); + const { can, hasAnyRole } = useCan(); + const canViewAll = hasAnyRole(['developer', 'owner', 'direktur', 'admin-toko']); const [closing, setClosing] = useState(null); const [reopening, setReopening] = useState(null); @@ -81,6 +82,7 @@ export default function PayrollPeriodIndex({ payrollPeriods }: Props) { handleClose: (period) => setClosing(period), handleReopen: (period) => setReopening(period), can, + canViewAll, }); return ( diff --git a/resources/js/pages/admin/finance/payroll-period/show-columns.tsx b/resources/js/pages/admin/finance/payroll-period/show-columns.tsx index 99be5df..dca3f49 100644 --- a/resources/js/pages/admin/finance/payroll-period/show-columns.tsx +++ b/resources/js/pages/admin/finance/payroll-period/show-columns.tsx @@ -81,7 +81,9 @@ export function createPayrollColumns( can, } = params; - return [ + const hasAnyPayrollAction = can('payroll.adjust') || can('payroll.pay') || can('payroll.cancel'); + + const columns: ColumnDef[] = [ { id: 'employee_name', header: () => Nama Karyawan, @@ -185,7 +187,7 @@ export function createPayrollColumns( {adj.description} - {payroll.status === 'unpaid' && ( + {can('payroll.adjust') && payroll.status === 'unpaid' && (