This commit is contained in:
101
api/app/Services/Almoxarifado/MotivoBaixaService.php
Normal file
101
api/app/Services/Almoxarifado/MotivoBaixaService.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Almoxarifado;
|
||||
|
||||
use App\Models\Almoxarifado\MotivoBaixa;
|
||||
use App\Models\Almoxarifado\MotivoBaixaTipo;
|
||||
use App\Services\Service;
|
||||
use DB;
|
||||
|
||||
class MotivoBaixaService extends Service
|
||||
{
|
||||
public static function datatable()
|
||||
{
|
||||
$motivosBaixa = DB::table('almoxarifado_motivo_baixa')
|
||||
->leftJoin(
|
||||
'almoxarifado_motivo_baixa_tipo',
|
||||
'almoxarifado_motivo_baixa_tipo.id',
|
||||
'almoxarifado_motivo_baixa.tipo_id'
|
||||
)
|
||||
->select(
|
||||
'almoxarifado_motivo_baixa.id',
|
||||
DB::raw('almoxarifado_motivo_baixa_tipo.nome as tipo'),
|
||||
'almoxarifado_motivo_baixa.descricao',
|
||||
DB::raw('DECODE_ORACLE(almoxarifado_motivo_baixa.justificar, 1, "Sim", "Não") AS justificar')
|
||||
)
|
||||
->whereNull('almoxarifado_motivo_baixa.deleted_at');
|
||||
|
||||
return $motivosBaixa;
|
||||
}
|
||||
|
||||
public static function create()
|
||||
{
|
||||
$motivoBaixa = new MotivoBaixa;
|
||||
$motivoBaixa->fill(array_fill_keys($motivoBaixa->getFillable(), null));
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'tipos' => MotivoBaixaTipo::orderBy('nome')->get(['id', 'nome'])
|
||||
],
|
||||
'model' => $motivoBaixa->makeHidden(['id'])
|
||||
];
|
||||
}
|
||||
|
||||
public static function store()
|
||||
{
|
||||
self::validate();
|
||||
|
||||
return MotivoBaixa::create(request()->all())->fresh();
|
||||
}
|
||||
|
||||
public static function edit($id)
|
||||
{
|
||||
$motivoBaixa = MotivoBaixa::findOrFail($id);
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'tipos' => MotivoBaixaTipo::orderBy('nome')->get(['id', 'nome'])
|
||||
],
|
||||
'model' => $motivoBaixa->makeHidden(['id'])
|
||||
];
|
||||
}
|
||||
|
||||
public static function update($id)
|
||||
{
|
||||
self::validate();
|
||||
|
||||
$motivoBaixa = tap(MotivoBaixa::findOrFail($id))->update(request()->all());
|
||||
|
||||
return $motivoBaixa->fresh();
|
||||
}
|
||||
|
||||
public static function validate()
|
||||
{
|
||||
$rule = [
|
||||
'tipo_id' => [
|
||||
'nullable',
|
||||
'integer',
|
||||
],
|
||||
'descricao' => [
|
||||
'string',
|
||||
'required',
|
||||
'between:3,100',
|
||||
'unique:\App\Models\Almoxarifado\MotivoBaixa,descricao,'
|
||||
. (request()->route('motivo_baixa') ?? 'NULL') . ',id,deleted_at,NULL',
|
||||
]
|
||||
];
|
||||
|
||||
$messages = [
|
||||
'descricao.unique' => 'A descrição do Motivo de baixa já foi inserido anteriormente.',
|
||||
];
|
||||
|
||||
request()->validate($rule, $messages);
|
||||
}
|
||||
|
||||
public static function destroy($id)
|
||||
{
|
||||
MotivoBaixa::findOrFail($id)->delete();
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user