This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\WebService\Almoxarifado\Cliente;
|
||||
|
||||
use App\Exceptions\WebService\Almoxarifado\Cliente\Fabricante\CannotDeleteFabricanteException;
|
||||
use App\Exceptions\WebService\Almoxarifado\Cliente\Fabricante\FabricanteNotFoundException;
|
||||
use App\Models\Cadastro\Fabricante\Fabricante as CadastroFabricante;
|
||||
use App\Models\WebService\Almoxarifado\Cliente\Fabricante;
|
||||
use App\Rules\Cnpj;
|
||||
use App\Services\Service;
|
||||
use App\Utils\MaskUtil;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class FabricanteService extends Service
|
||||
{
|
||||
public static function index()
|
||||
{
|
||||
return DB::table('web_service_almoxarifado_cliente_fabricante AS fabricante')
|
||||
->select([
|
||||
'fabricante.id',
|
||||
'fabricante.conectasus_fabricante_id',
|
||||
'fabricante.nome',
|
||||
'fabricante.cnpj',
|
||||
])
|
||||
->orderBy('fabricante.nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function create()
|
||||
{
|
||||
return [
|
||||
'form' => self::getFormData(),
|
||||
'model' => [
|
||||
'conectasus_fabricante_id' => null,
|
||||
'nome' => null,
|
||||
'cnpj' => null,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function store($request)
|
||||
{
|
||||
self::validate($request);
|
||||
|
||||
return Fabricante::create(self::getFabricanteData($request))->fresh();
|
||||
}
|
||||
|
||||
public static function show($id)
|
||||
{
|
||||
return self::getFabricanteModel($id);
|
||||
}
|
||||
|
||||
public static function edit($id)
|
||||
{
|
||||
$fabricante = self::getFabricanteModel($id);
|
||||
|
||||
return [
|
||||
'form' => self::getFormData(),
|
||||
'model' => [
|
||||
'conectasus_fabricante_id' => $fabricante->conectasus_fabricante_id,
|
||||
'nome' => $fabricante->nome,
|
||||
'cnpj' => MaskUtil::getCnpjMasked($fabricante->cnpj),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private static function getFormData()
|
||||
{
|
||||
return [
|
||||
'conectasus_fabricantes' => CadastroFabricante::orderBy('nome')
|
||||
->get(['id', 'nome', 'cnpj'])
|
||||
->map(fn ($fabricante) => (object) [
|
||||
'id' => $fabricante->id,
|
||||
'descricao' => $fabricante->nome
|
||||
. ($fabricante->cnpj ? (' - ' . MaskUtil::getCnpjMasked($fabricante->cnpj)) : '')
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
public static function update($request, $id)
|
||||
{
|
||||
$fabricante = self::getFabricanteModel($id);
|
||||
|
||||
self::validate($request);
|
||||
|
||||
return tap($fabricante)->update(self::getFabricanteData($request))->fresh();
|
||||
}
|
||||
|
||||
private static function getFabricanteData($request)
|
||||
{
|
||||
return $request->only(['conectasus_fabricante_id', 'nome', 'cnpj']);
|
||||
}
|
||||
|
||||
public static function destroy($id)
|
||||
{
|
||||
$fabricante = self::getFabricanteModel($id);
|
||||
|
||||
if ($fabricante->entradaItens()->exists()) {
|
||||
|
||||
throw new CannotDeleteFabricanteException();
|
||||
}
|
||||
|
||||
$fabricante->delete();
|
||||
}
|
||||
|
||||
public static function datatable()
|
||||
{
|
||||
return DB::table('web_service_almoxarifado_cliente_fabricante AS fabricante')
|
||||
->leftJoin(
|
||||
'cadastro_fabricante as conectasus_fabricante',
|
||||
'conectasus_fabricante.id',
|
||||
'fabricante.conectasus_fabricante_id'
|
||||
)
|
||||
->select([
|
||||
'fabricante.id',
|
||||
'fabricante.nome',
|
||||
'fabricante.cnpj',
|
||||
'conectasus_fabricante.nome AS conectasus_fabricante_nome',
|
||||
]);
|
||||
}
|
||||
|
||||
private static function validate($request)
|
||||
{
|
||||
$request->validate([
|
||||
'conectasus_fabricante_id' => [
|
||||
'nullable',
|
||||
'present',
|
||||
'integer',
|
||||
Rule::exists(CadastroFabricante::class, 'id'),
|
||||
],
|
||||
'nome' => [
|
||||
'required',
|
||||
'string',
|
||||
'between:3,100',
|
||||
Rule::unique(Fabricante::class, 'nome')->ignore($request->route('id')),
|
||||
],
|
||||
'cnpj' => [
|
||||
'nullable',
|
||||
'present',
|
||||
'digits:14',
|
||||
new Cnpj,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
private static function getFabricanteModel($id)
|
||||
{
|
||||
$fabricante = Fabricante::find($id);
|
||||
|
||||
if (is_null($fabricante)) {
|
||||
|
||||
throw new FabricanteNotFoundException();
|
||||
}
|
||||
|
||||
return $fabricante;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user