Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from lucas0headshot/dev
Browse files Browse the repository at this point in the history
Teste técnico finalizado
  • Loading branch information
lucas0headshot authored May 19, 2024
2 parents d8b55ae + 765be1d commit 9a10524
Show file tree
Hide file tree
Showing 31 changed files with 1,490 additions and 200 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ yarn-error.log
/.fleet
/.idea
/.vscode
*.lock
package-lock.json
composer.lock
composer.lock
195 changes: 195 additions & 0 deletions app/Http/Controllers/CompromissoController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
<?php

namespace App\Http\Controllers;

use App\Http\Requests\CompromissoRequest;
use App\Http\Requests\ConsultorRequest;
use App\Models\Compromisso;
use App\Models\Consultor;
use Carbon\Carbon;
use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Yajra\DataTables\DataTables;

class CompromissoController extends Controller
{
/**
* Retorna a view principal.
*
* @return View | JsonResponse
*/
public function index(Request $request): View | JsonResponse
{
if ($request->ajax()) {
//*Ininicar query e adicionar filtros
$query = Compromisso::with('consultor')->latest();

if ($request->filled('data_inicio')) {
$query->whereDate('data', '>=', Carbon::parse($request->data_inicio)->toDateString());
}
if ($request->filled('data_fim')) {
$query->whereDate('data', '<=', Carbon::parse($request->data_fim)->toDateString());
}
if ($request->filled('id_consultor')) {
$query->where('id_consultor', $request->id_consultor);
}

$data = $query->get();

//*Calcular toal horas
$total_horas = $data->reduce(function ($carry, $item) {
return $carry + Compromisso::findOrFail($item->id)->calcularTotalHoras();
}, 0);

$total_horas_completas = floor($total_horas / 60);
$total_minutos_restantes = $total_horas % 60;
$total_horas = sprintf('%02d:%02d', $total_horas_completas, $total_minutos_restantes);

//*Calcular valor total
$total_valor = $data->reduce(function ($carry, $item) {
return $carry + Compromisso::findOrFail($item->id)->calcularValorTotal();
}, 0);

//*Montar e retornar instância do DataTables
return DataTables::of($data)
->addColumn('data', function ($row) {
return Carbon::parse($row->data)->format('d/m/Y');
})
->addColumn('consultor', function ($row) {
return $row->consultor->nome;
})
->addColumn('horarios', function ($row) {
return $row->hora_inicio . ' - ' . $row->hora_fim;
})
->addColumn('intervalo', function ($row) {
return $row->intervalo;
})
->addColumn('total_horas', function ($row) {
return gmdate('H:i', Compromisso::findOrFail($row->id)->calcularTotalHoras() * 60);
})
->addColumn('valor_total', function ($row) {
return 'R$ ' . number_format(Compromisso::findOrFail($row->id)->calcularValorTotal(), 2, ',', '.');
})
->addColumn('acao', function ($row) {
$rota_editar = route('compromissos.edit', $row->id);
$rota_excluir = route('compromissos.destroy', $row->id);
return '<a href="' . $rota_editar . '" class="edit btn btn-warning btn-sm">Editar</a>' .
'<form action="' . $rota_excluir . '" method="POST" style="display: inline;" class="ms-2">' .
csrf_field() .
method_field('DELETE') .
'<button type="submit" onClick="return confirm(\'Deseja realmente remover este compromisso?\')" class="delete btn btn-danger btn-sm">Remover</button>' .
'</form>';
})
->rawColumns(['acao'])
->with([
//*RN01 - Na visualização dos compromissos, o sistema deve possibilitar visualizar um totalizador geral. Sendo este a soma dos totais de horas e soma dos totais de valor, dos filtros informados.
'total_horas' => $total_horas,
'total_valor' => 'R$ ' . number_format($total_valor, 2, ',', '.')
])
->make(true);
}

$consultores = Consultor::allOnlyIDAndName();
return view('compromissos.index', compact('consultores'));
}

/**
* Retorna a view p/ criar um Compromisso.
*
* @return View
*/
public function create(): View
{
$consultores = Consultor::allOnlyIDAndName();

return view('compromissos.create_edit', ['consultores' => $consultores]);
}

/**
* Cria um Compromisso.
*
* @param CompromissoRequest $request
*
* @return RedirectResponse
*/
public function store(CompromissoRequest $request): RedirectResponse
{
try {
Compromisso::create($request->validated());
return redirect()->route('compromissos.index')->with('success', 'Compromisso criado com sucesso');
} catch (Exception $e) {
return redirect()->route('compromissos.create')->withException($e)->withInput();
}
}

/**
* Retorna a view p/ visualizar um Compromisso.
*
* @param int $id_compromisso
*
* @return View
*/
public function show(int $id_compromisso): View
{
$compromisso = Compromisso::findOrFail($id_compromisso);

return view('compromisso.show', ['compromisso' => $compromisso]);
}

/**
* Retorna a view p/ editar um Compromisso.
*
* @param int $id_compromisso
*
* @return View
*/
public function edit(int $id_compromisso): View
{
$compromisso = Compromisso::findOrFail($id_compromisso);
$consultores = Consultor::allOnlyIDAndName();

return view('compromissos.create_edit', ['compromisso' => $compromisso, 'consultores' => $consultores]);
}

/**
* Atualiza um compromisso.
*
* @param CompromissoRequest $request
* @param int $id_compromisso
*
* @return RedirectResponse
*/
public function update(CompromissoRequest $request, int $id_compromisso): RedirectResponse
{
try {
$compromisso = Compromisso::findOrFail($id_compromisso);
$compromisso->updateOrFail($request->validated());

return redirect()->route('compromissos.index')->with('success', 'Compromisso editado com sucesso');
} catch (Exception $e) {
return redirect()->route('compromissos.edit', ['compromisso' => $id_compromisso])->with('erro', $e->getMessage())->withInput();
}
}


/**
* Remove um Compromisso.
*
* @param int $id_compromisso
*
* @return RedirectResponse
*/
public function destroy(int $id_compromisso): RedirectResponse
{
try {
Compromisso::findOrFail($id_compromisso)->deleteOrFail();
return redirect()->route('compromissos.index')->with('success', 'Compromisso removido com sucesso');
} catch (Exception $e) {
return redirect()->route('compromissos.index')->withException($e)->withInput();
}
}
}
157 changes: 157 additions & 0 deletions app/Http/Controllers/ConsultorController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php

namespace App\Http\Controllers;

use App\Http\Requests\ConsultorRequest;
use Illuminate\Http\Request;
use App\Models\Consultor;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
use Yajra\DataTables\DataTables;

class ConsultorController extends Controller
{
/**
* Retorna a view principal.
*
* @param Request $request
*
* @return View | JsonResponse
*/
public function index(Request $request): View | JsonResponse
{
if ($request->ajax()) {
//*Iniciar query e adicionar filtros
$query = Consultor::query();

if ($request->filled('nome')) {
$query->where('nome', 'LIKE', "%{$request->nome}%");
}

if ($request->filled("valor_hora")) {
$query->where('valor_hora', '=', $request->valor_hora);
}

$data = $query->get();

//*Montar e retornar instância do DataTables
return DataTables::of($data)
->addColumn('valor_hora', function ($row) {
return 'R$ ' . number_format($row->valor_hora, 2, ',', '.');
})
->addColumn('acao', function ($row) {
$rota_editar = route('consultores.edit', $row->id);
$rota_excluir = route('consultores.destroy', $row->id);
return '<a href="' . $rota_editar . '" class="edit btn btn-warning btn-sm">Editar</a>' .
'<form action="' . $rota_excluir . '" method="POST" style="display: inline;" class="ms-2">' .
csrf_field() .
method_field('DELETE') .
'<button type="submit" onClick="return confirm(\'Deseja realmente remover este consultor?\')" class="delete btn btn-danger btn-sm">Remover</button>' .
'</form>';
})
->rawColumns(['acao'])
->make(true);
}

return view('consultores.index');
}

/**
* Retorna a view p/ criar um Consultor
*
* @return View
*/
public function create(): View
{
return view('consultores.create_edit');
}

/**
* Cria um Consultor.
*
* @param ConsultorRequest $request
*
* @return RedirectResponse
*/
public function store(ConsultorRequest $request): RedirectResponse
{
try {
Consultor::create($request->validated());
return redirect()->route('consultores.index')->with('success', 'Consultor criado com sucesso');
} catch (Exception $e) {
return redirect()->route('consultores.index')->withException($e)->withInput();
}
}

/**
* Retorna a view p/ visualizar um Consultor.
*
* @param int $id_consultor
*
* @return View
*/
public function show(int $id_consultor): View
{
$consultor = Consultor::findOrFail($id_consultor);
return view('consultores.index', ['consultor' => $consultor]);
}

/**
* Retorna a view p/ editar um Consultor.
*
* @param int $consultor
*
* @return View
*/
public function edit(int $id_consultor): View
{
$consultor = Consultor::findOrFail($id_consultor);
return view('consultores.create_edit', ['consultor' => $consultor]);
}

/**
* Atualiza um Consultor.
*
* @param ConsultorRequest $request
* @param int $id_consultor
*
* @return RedirectResponse
*/
public function update(ConsultorRequest $request, int $id_consultor): RedirectResponse
{
try {
$consultor = Consultor::findOrFail($id_consultor);
$consultor->updateOrFail($request->validated());

return redirect()->route('consultores.index')->with('success', 'Consultor editado com sucesso');
} catch (Exception $e) {
return redirect()->route('consultores.create')->withException($e)->withInput();
}
}

/**
* Remove um Consultor.
*
* @param int $id_consultor
*
* @return RedirectResponse
*/
public function destroy(int $id_consultor): RedirectResponse
{
try {
$consultor = Consultor::findOrFail($id_consultor);

//*RN02 - Sistema deve permitir a exclusão de um consultor apenas se o mesmo não estiver vinculado a um compromisso.
if ($consultor->hasCompromisso()) {
return redirect()->route('consultores.index')->with('error', 'Consultor possui compromisso, não pode ser removido.');
}

$consultor->deleteOrFail();
return redirect()->route('consultores.index')->with('success', 'Consultor removido com sucesso');
} catch (Exception $e) {
return redirect()->route('consultores.index')->withException($e);
}
}
}
Loading

0 comments on commit 9a10524

Please sign in to comment.