- 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.
294 lines
11 KiB
PHP
294 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Admin\Finance;
|
|
|
|
use App\Enums\CashTransactionType;
|
|
use App\Models\CashAccount;
|
|
use App\Models\CashTransaction;
|
|
use App\Services\NotificationService;
|
|
use App\Services\S3PresignedService;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Pagination\LengthAwarePaginator as PaginationLengthAwarePaginator;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
|
|
|
class CashAccountService
|
|
{
|
|
public function __construct(
|
|
private S3PresignedService $s3Service = new S3PresignedService,
|
|
) {}
|
|
|
|
public function get(): ?CashAccount
|
|
{
|
|
return CashAccount::select('id', 'name', 'balance')->first();
|
|
}
|
|
|
|
public function getAllTransactions(array $filters = []): Collection
|
|
{
|
|
$cashAccount = $this->get();
|
|
|
|
if (! $cashAccount) {
|
|
return collect();
|
|
}
|
|
|
|
return $cashAccount->cashTransactions()
|
|
->select('id', 'created_by_id', 'reference_type', 'reference_id', 'amount', 'balance_after', 'type', 'description', 'created_at')
|
|
->with('createdBy.userProfile', 'media')
|
|
->when($filters['type'] ?? null, function ($query, $type) {
|
|
$query->where('type', $type);
|
|
})
|
|
->latest()
|
|
->get()
|
|
->map(fn (CashTransaction $transaction) => $this->formatTransaction($transaction));
|
|
}
|
|
|
|
public function paginatedTransactions(int $perPage = 15, string $search = '', string $sort = 'created_at', string $direction = 'desc', array $filters = []): LengthAwarePaginator
|
|
{
|
|
$cashAccount = $this->get();
|
|
|
|
if (! $cashAccount) {
|
|
return new PaginationLengthAwarePaginator(collect(), 0, $perPage);
|
|
}
|
|
|
|
$paginator = $cashAccount->cashTransactions()
|
|
->select('id', 'created_by_id', 'reference_type', 'reference_id', 'amount', 'balance_after', 'type', 'description', 'created_at')
|
|
->with('createdBy.userProfile', 'media')
|
|
->when($search, fn ($q) => $q->where('description', 'like', "%{$search}%"))
|
|
->when($filters['type'] ?? null, function ($query, $type) {
|
|
$query->where('type', $type);
|
|
})
|
|
->orderBy($sort, $direction)
|
|
->paginate($perPage);
|
|
|
|
$paginator->getCollection()->map(fn (CashTransaction $transaction) => $this->formatTransaction($transaction));
|
|
|
|
return $paginator;
|
|
}
|
|
|
|
private function formatTransaction(CashTransaction $transaction): array
|
|
{
|
|
$media = $transaction->getFirstMedia('receipts');
|
|
|
|
if (! $media) {
|
|
return $transaction->toArray() + [
|
|
'receipt_key' => null,
|
|
'receipt_url' => null,
|
|
];
|
|
}
|
|
|
|
$s3Key = $media->file_name;
|
|
|
|
if (str_starts_with($s3Key, '/') || ! str_contains($s3Key, '/')) {
|
|
$s3Key = $media->getPath();
|
|
}
|
|
|
|
return $transaction->toArray() + [
|
|
'receipt_key' => $s3Key,
|
|
'receipt_url' => $this->s3Service->getTemporaryUrl($s3Key),
|
|
];
|
|
}
|
|
|
|
public function deposit(array $data): CashTransaction
|
|
{
|
|
$transaction = DB::transaction(function () use ($data) {
|
|
$cashAccount = CashAccount::firstOrFail();
|
|
$newBalance = $cashAccount->balance + $data['amount'];
|
|
|
|
$cashAccount->update(['balance' => $newBalance]);
|
|
|
|
$transaction = CashTransaction::create([
|
|
'cash_account_id' => $cashAccount->id,
|
|
'created_by_id' => auth()->id(),
|
|
'amount' => $data['amount'],
|
|
'balance_after' => $newBalance,
|
|
'type' => CashTransactionType::DEPOSIT,
|
|
'description' => $data['description'],
|
|
]);
|
|
|
|
if (! empty($data['receipt_key'])) {
|
|
$this->registerMedia($transaction, $data['receipt_key'], $data['file_size'] ?? null, $data['file_mime_type'] ?? null);
|
|
}
|
|
|
|
return $transaction;
|
|
});
|
|
|
|
NotificationService::notify(
|
|
roles: ['Owner', 'Developer', 'Direktur', 'Admin Toko'],
|
|
title: 'Setoran Kas Toko',
|
|
body: 'Setoran sebesar Rp '.number_format($data['amount'], 0, ',', '.').' berhasil dicatat'.' oleh '.auth()->user()->full_name.'.',
|
|
url: route('admin.finance.cash-accounts.index'),
|
|
);
|
|
|
|
return $transaction;
|
|
}
|
|
|
|
public function withdrawal(array $data): CashTransaction
|
|
{
|
|
$transaction = DB::transaction(function () use ($data) {
|
|
$cashAccount = CashAccount::firstOrFail();
|
|
|
|
if ($cashAccount->balance < $data['amount']) {
|
|
throw ValidationException::withMessages([
|
|
'amount' => 'Saldo tidak mencukupi.',
|
|
]);
|
|
}
|
|
|
|
$newBalance = $cashAccount->balance - $data['amount'];
|
|
$cashAccount->update(['balance' => $newBalance]);
|
|
|
|
$transaction = CashTransaction::create([
|
|
'cash_account_id' => $cashAccount->id,
|
|
'created_by_id' => auth()->id(),
|
|
'amount' => $data['amount'],
|
|
'balance_after' => $newBalance,
|
|
'type' => CashTransactionType::WITHDRAWAL,
|
|
'description' => $data['description'],
|
|
]);
|
|
|
|
if (! empty($data['receipt_key'])) {
|
|
$this->registerMedia($transaction, $data['receipt_key'], $data['file_size'] ?? null, $data['file_mime_type'] ?? null);
|
|
}
|
|
|
|
return $transaction;
|
|
});
|
|
|
|
NotificationService::notify(
|
|
roles: ['Owner', 'Developer', 'Direktur', 'Admin Toko'],
|
|
title: 'Penarikan Kas Toko',
|
|
body: 'Penarikan sebesar Rp '.number_format($data['amount'], 0, ',', '.').' berhasil dicatat'.' oleh '.auth()->user()->full_name.'.',
|
|
url: route('admin.finance.cash-accounts.index'),
|
|
);
|
|
|
|
return $transaction;
|
|
}
|
|
|
|
public function updateTransaction(CashTransaction $transaction, array $data): CashTransaction
|
|
{
|
|
$transaction = DB::transaction(function () use ($transaction, $data) {
|
|
if (! in_array($transaction->type, [CashTransactionType::DEPOSIT, CashTransactionType::WITHDRAWAL])) {
|
|
throw ValidationException::withMessages([
|
|
'amount' => 'Transaksi ini tidak dapat diedit.',
|
|
]);
|
|
}
|
|
|
|
$cashAccount = CashAccount::firstOrFail();
|
|
$oldAmount = $transaction->amount;
|
|
$newAmount = $data['amount'];
|
|
$isDeposit = $transaction->type === CashTransactionType::DEPOSIT;
|
|
|
|
$difference = $isDeposit ? $newAmount - $oldAmount : $oldAmount - $newAmount;
|
|
|
|
if ($difference < 0 && $cashAccount->balance < abs($difference)) {
|
|
throw ValidationException::withMessages([
|
|
'amount' => 'Saldo tidak mencukupi.',
|
|
]);
|
|
}
|
|
|
|
$newBalance = $cashAccount->balance + $difference;
|
|
$cashAccount->update(['balance' => $newBalance]);
|
|
|
|
$transaction->update([
|
|
'amount' => $newAmount,
|
|
'balance_after' => $newBalance,
|
|
'description' => $data['description'],
|
|
]);
|
|
|
|
if (array_key_exists('receipt_key', $data)) {
|
|
$currentMedia = $transaction->getFirstMedia('receipts');
|
|
$currentKey = $currentMedia?->file_name;
|
|
|
|
if ($currentMedia && ! str_contains($currentKey, '/')) {
|
|
$currentKey = $currentMedia->getPath();
|
|
}
|
|
|
|
if ($data['receipt_key'] !== $currentKey) {
|
|
$transaction->clearMediaCollection('receipts');
|
|
|
|
if (! empty($data['receipt_key'])) {
|
|
$this->registerMedia($transaction, $data['receipt_key'], $data['file_size'] ?? null, $data['file_mime_type'] ?? null);
|
|
}
|
|
}
|
|
}
|
|
|
|
return $transaction;
|
|
});
|
|
|
|
NotificationService::notify(
|
|
roles: ['Owner', 'Developer', 'Direktur', 'Admin Toko'],
|
|
title: 'Transaksi Kas Diperbarui',
|
|
body: 'Transaksi kas sebesar Rp '.number_format($transaction->amount, 0, ',', '.').' berhasil diperbarui'.' oleh '.auth()->user()->full_name.'.',
|
|
url: route('admin.finance.cash-accounts.index'),
|
|
);
|
|
|
|
return $transaction;
|
|
}
|
|
|
|
public function deleteTransaction(CashTransaction $transaction): bool
|
|
{
|
|
return DB::transaction(function () use ($transaction) {
|
|
if (! in_array($transaction->type, [CashTransactionType::DEPOSIT, CashTransactionType::WITHDRAWAL])) {
|
|
throw ValidationException::withMessages([
|
|
'amount' => 'Transaksi ini tidak dapat dihapus.',
|
|
]);
|
|
}
|
|
|
|
$cashAccount = CashAccount::firstOrFail();
|
|
$isDeposit = $transaction->type === CashTransactionType::DEPOSIT;
|
|
|
|
$newBalance = $isDeposit
|
|
? $cashAccount->balance - $transaction->amount
|
|
: $cashAccount->balance + $transaction->amount;
|
|
|
|
if ($newBalance < 0) {
|
|
throw ValidationException::withMessages([
|
|
'amount' => 'Saldo tidak mencukupi untuk menghapus transaksi ini.',
|
|
]);
|
|
}
|
|
|
|
$cashAccount->update(['balance' => $newBalance]);
|
|
|
|
$transaction->clearMediaCollection('receipts');
|
|
|
|
$deleted = $transaction->delete();
|
|
|
|
if ($deleted) {
|
|
NotificationService::notify(
|
|
roles: ['Owner', 'Developer', 'Direktur', 'Admin Toko'],
|
|
title: 'Transaksi Kas Dihapus',
|
|
body: 'Transaksi kas sebesar Rp '.number_format($transaction->amount, 0, ',', '.').' berhasil dihapus'.' oleh '.auth()->user()->full_name.'.',
|
|
url: route('admin.finance.cash-accounts.index'),
|
|
);
|
|
}
|
|
|
|
return $deleted;
|
|
});
|
|
}
|
|
|
|
private function registerMedia(CashTransaction $transaction, string $s3Key, ?int $fileSize = null, ?string $mimeType = null): void
|
|
{
|
|
$fileName = pathinfo($s3Key, PATHINFO_BASENAME);
|
|
$name = pathinfo($s3Key, PATHINFO_FILENAME);
|
|
|
|
Media::create([
|
|
'model_type' => CashTransaction::class,
|
|
'model_id' => $transaction->id,
|
|
'uuid' => Str::uuid(),
|
|
'collection_name' => 'receipts',
|
|
'name' => $name,
|
|
'file_name' => $s3Key,
|
|
'mime_type' => $mimeType ?? 'image/jpeg',
|
|
'disk' => 's3',
|
|
'conversions_disk' => 's3',
|
|
'size' => $fileSize ?? 0,
|
|
'manipulations' => [],
|
|
'custom_properties' => [],
|
|
'generated_conversions' => ['thumb' => true],
|
|
'responsive_images' => [],
|
|
'order_column' => 1,
|
|
]);
|
|
}
|
|
}
|