Files
rkm-conectasus-ci-sandbox/api/app/Services/Cadastro/Fabricante/FabricanteService.php
Antonio Lopes dos Santos afec302c3e
All checks were successful
build-api-image / build (push) Successful in 51s
init commit
2026-08-04 16:52:02 -03:00

181 lines
5.3 KiB
PHP

<?php
namespace App\Services\Cadastro\Fabricante;
use App\Enums\Cadastro\Fabricante\Tipo as FabricanteTipo;
use App\Models\Cadastro\Fabricante\Fabricante;
use App\Models\Cadastro\Fabricante\Tipo;
use App\Rules\Cnpj;
use App\Services\Service;
use App\Utils\MaskUtil;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
class FabricanteService extends Service
{
public static function autocomplete()
{
$fabricantes = DB::table('cadastro_fabricante')
->select(
'cadastro_fabricante.id',
'cadastro_fabricante.nome',
'cadastro_fabricante.cnpj',
);
return datatables()
->of($fabricantes)
->filterColumn('cadastro_fabricante.cnpj', function ($query, $keyword) {
$query
->whereRaw("cadastro_fabricante.cnpj like ?", ["%{$keyword}%"])
->orWhereRaw(
"CONCAT(
SUBSTR(cadastro_fabricante.cnpj,1,2),
'.',
SUBSTR(cadastro_fabricante.cnpj,3,3),
'.',
SUBSTR(cadastro_fabricante.cnpj,6,3),
'/',
SUBSTR(cadastro_fabricante.cnpj,9,4),
'-',
SUBSTR(cadastro_fabricante.cnpj,13,2)
) like ?",
["%{$keyword}%"]
);
})
->toJson();
}
public static function datatable()
{
$fabricantes = DB::table('cadastro_fabricante')
->join(
'cadastro_fabricante_tipo',
'cadastro_fabricante_tipo.id',
'cadastro_fabricante.tipo_id',
)
->select(
'cadastro_fabricante.id',
'cadastro_fabricante_tipo.nome as tipo_nome',
'cadastro_fabricante.nome',
'cadastro_fabricante.cnpj',
);
return datatables()
->of($fabricantes)
->filterColumn('cadastro_fabricante.cnpj', function ($query, $keyword) {
$query
->whereRaw("cadastro_fabricante.cnpj like ?", ["%{$keyword}%"])
->orWhereRaw(
"CONCAT(
SUBSTR(cadastro_fabricante.cnpj,1,2),
'.',
SUBSTR(cadastro_fabricante.cnpj,3,3),
'.',
SUBSTR(cadastro_fabricante.cnpj,6,3),
'/',
SUBSTR(cadastro_fabricante.cnpj,9,4),
'-',
SUBSTR(cadastro_fabricante.cnpj,13,2)
) like ?",
["%{$keyword}%"]
);
})
->toJson();
}
public static function create()
{
$fabricante = new Fabricante;
$fabricante->fill(array_fill_keys($fabricante->getFillable(), null));
return [
'form' => self::getFormData(),
'model' => $fabricante
];
}
public static function store()
{
self::validate();
return Fabricante::create(self::getParams())->fresh();
}
public static function edit($id)
{
$fabricante = Fabricante::findOrFail($id);
$fabricante->cnpj = MaskUtil::getCnpjMasked($fabricante->cnpj);
return [
'form' => self::getFormData(),
'model' => $fabricante->makeHidden(['id'])
];
}
private static function getFormData()
{
return [
'tipos' => Tipo::orderBy('nome')->get(['id', 'nome']),
];
}
public static function update($id)
{
self::validate();
$fabricante = tap(Fabricante::findOrFail($id))->update(self::getParams());
return $fabricante->fresh();
}
private static function getParams()
{
return [
'tipo_id' => request()->tipo_id,
'nome' => Str::upper(request()->nome),
'cnpj' => request()->cnpj,
];
}
public static function validate()
{
$rules = [
'tipo_id' => [
'required',
'integer',
Rule::exists(Tipo::class, 'id'),
],
'nome' => [
'required',
'string',
'max:180',
Rule::unique(Fabricante::class)->ignore(request()->route('fabricante'))
],
'cnpj' => [
'nullable',
Rule::requiredIf(fn () => request()->tipo_id == FabricanteTipo::NACIONAL),
'digits:14',
new Cnpj,
Rule::unique(Fabricante::class)->ignore(request()->route('fabricante'))
],
];
$messages = [
'nome.unique' => 'O nome do fabricante já foi inserido anteriormente.',
'cnpj.unique' => 'O CNPJ do fabricante já foi inserido anteriormente.',
];
request()->validate($rules, $messages);
}
public static function destroy($id)
{
Fabricante::findOrFail($id)->delete();
return null;
}
}