Files
InvoiceShelf/app/Http/Controllers/Company/Expense/ExpensesController.php
Darko Gjorgjijoski 6d1816bd1b refactor: reorganize app/Services and app/Support by domain
The app/Services/ directory had grown into 22 flat files at the root plus 7 uneven subdirectories — finding anything required scrolling through an alphabetical mix of small CRUD services, infrastructure drivers, and install-time utilities. This commit groups services by domain, folds Backup into a new Storage namespace, and moves framework-infrastructure and install-time helpers out of Services and into Support where they belong.

New Services layout: Documents/ (Invoice, Estimate, RecurringInvoice, Payment, Expense, Transaction, DocumentItem, SerialNumber, Currency — matches the 'Documents' navigation group); Company/ (Company, Member, Invitation); Mail/ (MailConfiguration, CompanyMailConfig); Storage/ (FileDisk, plus Backup folded in). ExchangeRateProviderService moves next to its drivers in ExchangeRate/; FontService moves into Pdf/ where it belongs. CustomerService, ItemService, CustomFieldService stay at the Services root as standalone single-file domains.

Moves to Support/: Hashids/ (library wrapper — not business logic); Setup/ (one-shot install-time utilities — stateless helpers); Pdf/ (ImageUtils, PdfTemplateUtils, plus the existing PdfHtmlSanitizer consolidated into the same subdir). These are all framework infrastructure and stateless utilities — the 'service' label never really fit them.

Namespace declarations in 29 moved files updated to match new paths. 62 consumer files (controllers, other services, tests, database factories, seeders, routes, bootstrap/providers.php) have their use statements rewritten via a literal-string replacement script — no regex meant no risk of half-matching. Three Documents services needed an explicit 'use App\Services\Mail\CompanyMailConfigService' added because the same-namespace short reference they relied on no longer resolves after the split.

Verified: composer dump-autoload, 350 tests pass (850 assertions), vendor/bin/pint clean, npm run build succeeds.
2026-04-11 10:00:00 +02:00

159 lines
4.2 KiB
PHP

<?php
namespace App\Http\Controllers\Company\Expense;
use App\Http\Controllers\Controller;
use App\Http\Requests\DeleteExpensesRequest;
use App\Http\Requests\ExpenseRequest;
use App\Http\Requests\UploadExpenseReceiptRequest;
use App\Http\Resources\ExpenseResource;
use App\Models\Expense;
use App\Services\Documents\ExpenseService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class ExpensesController extends Controller
{
public function __construct(
private readonly ExpenseService $expenseService,
) {}
/**
* Display a listing of the resource.
*
* @return JsonResponse
*/
public function index(Request $request)
{
$this->authorize('viewAny', Expense::class);
$limit = $request->has('limit') ? $request->limit : 10;
$expenses = Expense::with('category', 'creator', 'fields')
->whereCompany()
->leftJoin('customers', 'customers.id', '=', 'expenses.customer_id')
->join('expense_categories', 'expense_categories.id', '=', 'expenses.expense_category_id')
->applyFilters($request->all())
->select('expenses.*', 'expense_categories.name', 'customers.name as user_name')
->paginateData($limit);
return ExpenseResource::collection($expenses)
->additional(['meta' => [
'expense_total_count' => Expense::whereCompany()->count(),
]]);
}
/**
* Store a newly created resource in storage.
*
* @return JsonResponse
*/
public function store(ExpenseRequest $request)
{
$this->authorize('create', Expense::class);
$expense = $this->expenseService->create($request);
return new ExpenseResource($expense);
}
/**
* Display the specified resource.
*
* @return JsonResponse
*/
public function show(Expense $expense)
{
$this->authorize('view', $expense);
return new ExpenseResource($expense);
}
/**
* Update the specified resource in storage.
*
* @return JsonResponse
*/
public function update(ExpenseRequest $request, Expense $expense)
{
$this->authorize('update', $expense);
$this->expenseService->update($expense, $request);
return new ExpenseResource($expense);
}
public function delete(DeleteExpensesRequest $request)
{
$this->authorize('delete multiple expenses');
$ids = Expense::whereCompany()
->whereIn('id', $request->ids)
->pluck('id');
Expense::destroy($ids);
return response()->json([
'success' => true,
]);
}
public function showReceipt(Expense $expense)
{
$this->authorize('view', $expense);
if ($expense) {
$media = $expense->getFirstMedia('receipts');
if ($media) {
return response()->file($media->getPath());
}
return respondJson('receipt_does_not_exist', 'Receipt does not exist.');
}
}
public function uploadReceipt(UploadExpenseReceiptRequest $request, Expense $expense)
{
$this->authorize('update', $expense);
$data = json_decode($request->attachment_receipt);
if ($data) {
if ($request->type === 'edit') {
$expense->clearMediaCollection('receipts');
}
$expense->addMediaFromBase64($data->data)
->usingFileName($data->name)
->toMediaCollection('receipts');
}
return response()->json([
'success' => 'Expense receipts uploaded successfully',
], 200);
}
public function downloadReceipt(Expense $expense)
{
$this->authorize('view', $expense);
if ($expense) {
$media = $expense->getFirstMedia('receipts');
if ($media) {
$imagePath = $media->getPath();
$response = \Response::download($imagePath, $media->file_name);
if (ob_get_contents()) {
ob_end_clean();
}
return $response;
}
}
return response()->json([
'error' => 'receipt_not_found',
]);
}
}