- Implemented push notifications with a service worker (sw.ts) to handle caching and notifications. - Added API routes for push subscription and notification management in routes/api.php. - Created NotificationTest to validate notification service functionality and ensure correct notifications are sent based on user roles and actions. - Updated Permissions component to manage notification permissions and subscriptions. - Enhanced CategoryIndex component to highlight categories based on notifications. - Excluded service worker from TypeScript compilation in tsconfig.json. - Updated Vite configuration to include service worker in the build process.
194 lines
6.3 KiB
PHP
194 lines
6.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Admin\Finance;
|
|
|
|
use App\Enums\CashTransactionType;
|
|
use App\Enums\PayrollPeriodStatus;
|
|
use App\Enums\PayrollStatus;
|
|
use App\Models\CashAccount;
|
|
use App\Models\CashTransaction;
|
|
use App\Models\Payroll;
|
|
use App\Models\PayrollPeriod;
|
|
use App\Services\NotificationService;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class PayrollPeriodService
|
|
{
|
|
public function getAll(): 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();
|
|
}])
|
|
->latest('year')
|
|
->latest('month')
|
|
->get();
|
|
}
|
|
|
|
public function paginated(int $perPage = 15, string $search = '', string $sort = 'created_at', string $direction = 'desc'): LengthAwarePaginator
|
|
{
|
|
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($search, fn ($q) => $q->where('year', 'like', "%{$search}%"))
|
|
->orderBy($sort, $direction)
|
|
->paginate($perPage);
|
|
}
|
|
|
|
public function getDetail(PayrollPeriod $period): PayrollPeriod
|
|
{
|
|
return $period->load([
|
|
'payrolls' => function ($query) {
|
|
$query->with(['employee.user.userProfile', 'payrollAdjustments'])
|
|
->orderBy('id');
|
|
},
|
|
]);
|
|
}
|
|
|
|
public function close(PayrollPeriod $period): PayrollPeriod
|
|
{
|
|
if ($period->status === PayrollPeriodStatus::CLOSED) {
|
|
throw ValidationException::withMessages([
|
|
'period' => 'Periode sudah ditutup.',
|
|
]);
|
|
}
|
|
|
|
$hasUnpaid = $period->payrolls()
|
|
->where('status', PayrollStatus::UNPAID)
|
|
->exists();
|
|
|
|
if ($hasUnpaid) {
|
|
throw ValidationException::withMessages([
|
|
'period' => 'Masih ada gaji yang belum dibayar.',
|
|
]);
|
|
}
|
|
|
|
$period->update([
|
|
'status' => PayrollPeriodStatus::CLOSED,
|
|
'closed_by_id' => auth()->id(),
|
|
'closed_at' => now(),
|
|
]);
|
|
|
|
return $period;
|
|
}
|
|
|
|
public function reopen(PayrollPeriod $period): PayrollPeriod
|
|
{
|
|
if ($period->status === PayrollPeriodStatus::OPEN) {
|
|
throw ValidationException::withMessages([
|
|
'period' => 'Periode sudah terbuka.',
|
|
]);
|
|
}
|
|
|
|
$period->update([
|
|
'status' => PayrollPeriodStatus::OPEN,
|
|
'closed_by_id' => null,
|
|
'closed_at' => null,
|
|
]);
|
|
|
|
return $period;
|
|
}
|
|
|
|
public function pay(Payroll $payroll): Payroll
|
|
{
|
|
if ($payroll->status === PayrollStatus::PAID) {
|
|
throw ValidationException::withMessages([
|
|
'payroll' => 'Gaji sudah dibayar.',
|
|
]);
|
|
}
|
|
|
|
if ($payroll->status === PayrollStatus::CANCELLED) {
|
|
throw ValidationException::withMessages([
|
|
'payroll' => 'Gaji sudah dibatalkan.',
|
|
]);
|
|
}
|
|
|
|
$payroll = DB::transaction(function () use ($payroll) {
|
|
$cashAccount = CashAccount::firstOrFail();
|
|
|
|
$newBalance = $cashAccount->balance + $payroll->total_amount;
|
|
$cashAccount->update(['balance' => $newBalance]);
|
|
|
|
$cashTransaction = CashTransaction::create([
|
|
'cash_account_id' => $cashAccount->id,
|
|
'created_by_id' => auth()->id(),
|
|
'amount' => $payroll->total_amount,
|
|
'balance_after' => $newBalance,
|
|
'type' => CashTransactionType::DEPOSIT,
|
|
'description' => 'Pembayaran gaji karyawan',
|
|
]);
|
|
|
|
$payroll->update([
|
|
'status' => PayrollStatus::PAID,
|
|
'cash_transaction_id' => $cashTransaction->id,
|
|
'paid_by_id' => auth()->id(),
|
|
'paid_at' => now(),
|
|
]);
|
|
|
|
return $payroll;
|
|
});
|
|
|
|
$employeeUser = $payroll->employee->user ?? null;
|
|
|
|
NotificationService::notify(
|
|
roles: ['Owner', 'Developer', 'Direktur', 'Admin Toko'],
|
|
title: 'Gaji Dibayar',
|
|
body: "Gaji karyawan {$payroll->employee->name} sebesar {$payroll->formatted_amount} telah dibayar".' oleh '.auth()->user()->full_name.'.',
|
|
url: route('admin.finance.payroll-periods.index'),
|
|
additionalUser: $employeeUser,
|
|
);
|
|
|
|
return $payroll;
|
|
}
|
|
|
|
public function cancel(Payroll $payroll): Payroll
|
|
{
|
|
if ($payroll->status === PayrollStatus::PAID) {
|
|
throw ValidationException::withMessages([
|
|
'payroll' => 'Gaji yang sudah dibayar tidak dapat dibatalkan.',
|
|
]);
|
|
}
|
|
|
|
if ($payroll->status === PayrollStatus::CANCELLED) {
|
|
throw ValidationException::withMessages([
|
|
'payroll' => 'Gaji sudah dibatalkan.',
|
|
]);
|
|
}
|
|
|
|
$payroll->update([
|
|
'status' => PayrollStatus::CANCELLED,
|
|
]);
|
|
|
|
$employeeUser = $payroll->employee->user ?? null;
|
|
|
|
NotificationService::notify(
|
|
roles: ['Owner', 'Developer', 'Direktur', 'Admin Toko'],
|
|
title: 'Gaji Dibatalkan',
|
|
body: "Gaji karyawan {$payroll->employee->name} sebesar {$payroll->formatted_amount} telah dibatalkan".' oleh '.auth()->user()->full_name.'.',
|
|
url: route('admin.finance.payroll-periods.index'),
|
|
additionalUser: $employeeUser,
|
|
);
|
|
|
|
return $payroll;
|
|
}
|
|
}
|