mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-07-17 06:15:20 +00:00
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.
114 lines
3.2 KiB
PHP
114 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Facades\Hashids;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\AdminCompanyUpdateRequest;
|
|
use App\Http\Requests\CompaniesRequest;
|
|
use App\Http\Resources\CompanyResource;
|
|
use App\Models\Company;
|
|
use App\Services\Company\CompanyService;
|
|
use Illuminate\Http\Request;
|
|
use Silber\Bouncer\BouncerFacade;
|
|
|
|
class CompaniesController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly CompanyService $companyService,
|
|
) {}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$companies = Company::query()
|
|
->with(['owner', 'address'])
|
|
->when($request->has('search'), function ($query) use ($request) {
|
|
$query->where('name', 'like', '%'.$request->search.'%');
|
|
})
|
|
->when($request->has('orderByField') && $request->has('orderBy'), function ($query) use ($request) {
|
|
$query->orderBy($request->orderByField, $request->orderBy);
|
|
}, function ($query) {
|
|
$query->orderBy('name', 'asc');
|
|
})
|
|
->paginate($request->input('limit', 10));
|
|
|
|
return CompanyResource::collection($companies);
|
|
}
|
|
|
|
public function show(Company $company)
|
|
{
|
|
$company->load(['owner', 'address']);
|
|
|
|
return new CompanyResource($company);
|
|
}
|
|
|
|
public function update(AdminCompanyUpdateRequest $request, Company $company)
|
|
{
|
|
$company->update([
|
|
'name' => $request->name,
|
|
'vat_id' => $request->vat_id,
|
|
'tax_id' => $request->tax_id,
|
|
'owner_id' => $request->owner_id,
|
|
]);
|
|
|
|
if ($request->has('address')) {
|
|
$company->address()->updateOrCreate(
|
|
['company_id' => $company->id],
|
|
$request->address,
|
|
);
|
|
}
|
|
|
|
$company->load(['owner', 'address']);
|
|
|
|
return new CompanyResource($company);
|
|
}
|
|
|
|
public function store(CompaniesRequest $request)
|
|
{
|
|
$this->authorize('create company');
|
|
|
|
$user = $request->user();
|
|
|
|
$company = Company::create($request->getCompanyPayload());
|
|
$company->unique_hash = Hashids::connection(Company::class)->encode($company->id);
|
|
$company->save();
|
|
$this->companyService->setupDefaults($company);
|
|
$user->companies()->attach($company->id);
|
|
|
|
BouncerFacade::scope()->to($company->id);
|
|
$user->assign('owner');
|
|
|
|
if ($request->address) {
|
|
$company->address()->create($request->address);
|
|
}
|
|
|
|
return new CompanyResource($company);
|
|
}
|
|
|
|
public function destroy(Request $request)
|
|
{
|
|
$company = Company::find($request->header('company'));
|
|
|
|
$this->authorize('delete company', $company);
|
|
|
|
$user = $request->user();
|
|
|
|
if ($request->name !== $company->name) {
|
|
return respondJson('company_name_must_match_with_given_name', 'Company name must match with given name');
|
|
}
|
|
|
|
$this->companyService->delete($company, $user);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
]);
|
|
}
|
|
|
|
public function userCompanies(Request $request)
|
|
{
|
|
$companies = $request->user()->companies;
|
|
|
|
return CompanyResource::collection($companies);
|
|
}
|
|
}
|