- Created ExampleTest and RoutesTest for basic application response and route testing. - Implemented IsolatedDatabaseTestCase for isolated database testing. - Added unit tests for various models and services including Contract, CPE, and LeadDetail. - Developed request validation tests for CreateClientRequest, UpdateClientRequest, and ContractRequest. - Introduced Vite configuration for asset management in the Laravel application.
96 lines
3.0 KiB
PHP
96 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Clients;
|
|
use App\Models\ClientType;
|
|
use App\Models\Contract;
|
|
use App\Models\User;
|
|
use App\Traits\ResourcesCache;
|
|
use Illuminate\Http\Request;
|
|
|
|
class ClientService
|
|
{
|
|
use ResourcesCache;
|
|
|
|
public function listing(Request $request, bool $paginated = false)
|
|
{
|
|
$query = Clients::query()->where('status', 'A');
|
|
if ($paginated) {
|
|
$query->where('type', 'client');
|
|
}
|
|
foreach (['name', 'tax_number', 'email'] as $field) {
|
|
if ($request->filled($field)) {
|
|
$query->where($field, 'like', '%' . $request->input($field) . '%');
|
|
}
|
|
}
|
|
if ($request->filled('client_type_id')) {
|
|
$query->where('client_type_id', $request->input('client_type_id'));
|
|
}
|
|
if ($request->filled('contract_code')) {
|
|
$query->whereHas('contracts', fn ($contracts) => $contracts->where('code', 'like', '%' . $request->input('contract_code') . '%'));
|
|
}
|
|
return $paginated
|
|
? $query->orderByDesc('id')->paginate(max(1, min(100, (int) $request->input('per_page', 10))))->appends($request->query())
|
|
: $query->orderBy('id')->get();
|
|
}
|
|
|
|
public function types()
|
|
{
|
|
return ClientType::all();
|
|
}
|
|
|
|
public function form($type, User $user): array
|
|
{
|
|
return ['type' => ClientType::findOrFail($type), 'sellers' => $user->id === 1 ? User::all() : $this->_getSellers()];
|
|
}
|
|
|
|
public function find($id)
|
|
{
|
|
return Clients::with('contracts')->findOrFail($id);
|
|
}
|
|
|
|
public function detail($id): array
|
|
{
|
|
$contracts = Contract::with(['contractType', 'status'])->where('client_id', $id)->orderByDesc('activation_date')->get();
|
|
return [
|
|
'client' => $this->find($id),
|
|
'contractsByStatus' => $contracts->groupBy(fn ($contract) => $contract->status->description ?? 'Sem status'),
|
|
'clientTypes' => $this->types(),
|
|
'sellers' => $this->_getSellers(),
|
|
];
|
|
}
|
|
|
|
public function create(array $data)
|
|
{
|
|
return Clients::create($data);
|
|
}
|
|
|
|
public function update($id, array $data)
|
|
{
|
|
$client = $this->find($id);
|
|
$client->update($data);
|
|
return $client;
|
|
}
|
|
|
|
public function delete($id): void
|
|
{
|
|
$client = $this->find($id);
|
|
$client->status = 'I';
|
|
$client->save();
|
|
}
|
|
|
|
public function searchName(string $term)
|
|
{
|
|
return Clients::with('clientType')->where('type', 'client')->where('status', 'A')
|
|
->where('name', 'like', '%' . $term . '%')->orderBy('name')->limit(10)->get()
|
|
->map(fn ($client) => ['id' => $client->id, 'label' => $client->name . ' (' . optional($client->clientType)->description . ')']);
|
|
}
|
|
|
|
public function searchID($id)
|
|
{
|
|
abort_unless(ctype_digit((string) $id), 400, 'ID invalido');
|
|
return Clients::where('type', 'client')->where('status', 'A')->findOrFail((int) $id);
|
|
}
|
|
}
|