144 lines
4.2 KiB
PHP
144 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Finance;
|
|
|
|
use App\Models\Expense;
|
|
use App\Models\User;
|
|
use App\Services\Media\MediaService;
|
|
use App\Support\Media\MediaPresenter;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ExpenseService
|
|
{
|
|
private const MAX_PHOTOS = 1;
|
|
|
|
public function __construct(
|
|
private readonly CashService $cashService,
|
|
private readonly MediaService $mediaService,
|
|
) {}
|
|
|
|
/**
|
|
* @param array{search: string, sort: string, direction: 'asc'|'desc'} $tableQuery
|
|
*/
|
|
public function paginateForIndex(array $tableQuery): LengthAwarePaginator
|
|
{
|
|
$query = Expense::query()
|
|
->with(['createdBy.profile', 'media'])
|
|
->when($tableQuery['search'] !== '', function (Builder $query) use ($tableQuery): void {
|
|
$search = $tableQuery['search'];
|
|
$query->where(function (Builder $query) use ($search): void {
|
|
$query->where('description', 'like', "%{$search}%");
|
|
});
|
|
});
|
|
|
|
$this->applySorting($query, $tableQuery['sort'], $tableQuery['direction']);
|
|
|
|
return $query
|
|
->paginate(10)
|
|
->withQueryString()
|
|
->through(function (Expense $expense) {
|
|
$expense->setAttribute(
|
|
'photos',
|
|
MediaPresenter::collection($expense, 'photos'),
|
|
);
|
|
|
|
return $expense;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @param array{amount: int, description: string} $validated
|
|
*/
|
|
public function create(array $validated, User $user): void
|
|
{
|
|
DB::transaction(function () use ($validated, $user): void {
|
|
$amount = (int) $validated['amount'];
|
|
$description = $validated['description'];
|
|
|
|
$expense = Expense::create([
|
|
'amount' => $amount,
|
|
'description' => $description,
|
|
'created_by_id' => $user->id,
|
|
]);
|
|
|
|
$cashTransaction = $this->cashService->recordOutgoing(
|
|
$expense,
|
|
$amount,
|
|
$description,
|
|
$user,
|
|
);
|
|
|
|
$expense->cash_transaction_id = $cashTransaction->id;
|
|
$expense->save();
|
|
|
|
$this->syncPhotos($expense, $validated);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @param array{amount: int, description: string} $validated
|
|
*/
|
|
public function update(Expense $expense, array $validated): void
|
|
{
|
|
DB::transaction(function () use ($expense, $validated): void {
|
|
$amount = (int) $validated['amount'];
|
|
$description = $validated['description'];
|
|
|
|
$expense->amount = $amount;
|
|
$expense->description = $description;
|
|
$expense->save();
|
|
|
|
if ($expense->cashTransaction) {
|
|
$this->cashService->updateReferencedTransaction(
|
|
$expense->cashTransaction,
|
|
$amount,
|
|
$description,
|
|
);
|
|
}
|
|
|
|
$this->syncPhotos($expense, $validated);
|
|
});
|
|
}
|
|
|
|
public function delete(Expense $expense): void
|
|
{
|
|
DB::transaction(function () use ($expense): void {
|
|
if ($expense->cashTransaction) {
|
|
$this->cashService->deleteReferencedTransaction($expense->cashTransaction);
|
|
}
|
|
|
|
$expense->clearMediaCollection('photos');
|
|
$expense->delete();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $validated
|
|
*/
|
|
private function syncPhotos(Expense $expense, array $validated): void
|
|
{
|
|
$this->mediaService->syncCollection(
|
|
$expense,
|
|
'photos',
|
|
$validated['photos'] ?? null,
|
|
$validated['remove_media_ids'] ?? null,
|
|
self::MAX_PHOTOS,
|
|
required: true,
|
|
errorKey: 'photos',
|
|
);
|
|
}
|
|
|
|
private function applySorting(Builder $query, string $sort, string $direction): void
|
|
{
|
|
if (in_array($sort, ['created_at', 'amount', 'description'], true)) {
|
|
$query->orderBy($sort, $direction);
|
|
|
|
return;
|
|
}
|
|
|
|
$query->latest();
|
|
}
|
|
}
|