This commit is contained in:
287
api/app/Services/TransportePaciente/QuantitativoService.php
Normal file
287
api/app/Services/TransportePaciente/QuantitativoService.php
Normal file
@@ -0,0 +1,287 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\TransportePaciente;
|
||||
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class QuantitativoService extends Service
|
||||
{
|
||||
public static function index()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('d/m/Y');
|
||||
|
||||
return [
|
||||
'model' => [
|
||||
'periodo' => [
|
||||
'start' => null,
|
||||
'end' => null,
|
||||
],
|
||||
'veiculo' => null,
|
||||
'municipio' => null,
|
||||
'destino' => null,
|
||||
'motivo' => null,
|
||||
'protocolo' => null
|
||||
],
|
||||
'form' => [
|
||||
'periodo' => [
|
||||
'start' => null,
|
||||
'end' => null,
|
||||
],
|
||||
'veiculos' => null,
|
||||
'municipios' => null,
|
||||
'destinos' => null,
|
||||
'motivos' => null,
|
||||
],
|
||||
'additionals' => [
|
||||
'periodo_min_date' => $now->copy()->subYears(130)->format('d/m/Y'),
|
||||
'current_date' => $now->format('d/m/Y')
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public static function getLoadFields(Request $request)
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$veiculos = DB::table('transporte_paciente_agendar_transporte as agendar')
|
||||
->select(
|
||||
'veiculo.id',
|
||||
DB::raw("CONCAT(veiculo.numero_placa, ' - ', veiculo.descricao_modelo) AS descricao_veiculo")
|
||||
)
|
||||
->join('transporte_paciente_veiculo as veiculo', 'veiculo.id', 'agendar.veiculo_id')
|
||||
->leftJoin('transporte_paciente_veiculo_marca as marca', 'marca.id', 'veiculo.marca_id')
|
||||
->where('veiculo.bloqueado', false)
|
||||
->orderBy('veiculo.numero_placa')
|
||||
->groupBy(['veiculo.id',]);
|
||||
|
||||
if ($request->periodo) {
|
||||
|
||||
$veiculos->where('data', '>=', $request->periodo['start']);
|
||||
$veiculos->where('data', '<=', $request->periodo['end']);
|
||||
} else {
|
||||
|
||||
$veiculos->where('data', $currentDate);
|
||||
}
|
||||
|
||||
$veiculos = $veiculos->get();
|
||||
|
||||
$municipios = DB::table('intranet.cadastro_municipio as municipio')
|
||||
->select([
|
||||
'municipio.id',
|
||||
'municipio.nome',
|
||||
'agendar.veiculo_id'
|
||||
])
|
||||
->join('transporte_paciente_agendar_transporte as agendar', 'agendar.municipio_id', 'municipio.id')
|
||||
->join('transporte_paciente_veiculo as veiculo', 'veiculo.id', 'agendar.veiculo_id')
|
||||
->orderBy('municipio.nome')
|
||||
->groupBy([
|
||||
'municipio.id',
|
||||
]);
|
||||
|
||||
if ($request->periodo) {
|
||||
|
||||
$municipios->where('data', '>=', $request->periodo['start']);
|
||||
$municipios->where('data', '<=', $request->periodo['end']);
|
||||
} else {
|
||||
|
||||
$municipios->where('data', $currentDate);
|
||||
}
|
||||
|
||||
if ($request->veiculo) {
|
||||
|
||||
$municipios->where('agendar.veiculo_id', $request->veiculo);
|
||||
}
|
||||
|
||||
$municipios = $municipios->get();
|
||||
|
||||
$destinos = DB::table('transporte_paciente_destino as destino')
|
||||
->select([
|
||||
'destino.id',
|
||||
'destino.descricao_destino as descricao',
|
||||
'agendar.veiculo_id',
|
||||
'agendar.municipio_id',
|
||||
])
|
||||
->join('transporte_paciente_agendar_transporte as agendar', 'agendar.destino_id', 'destino.id')
|
||||
->join('transporte_paciente_viagem as viagem', 'viagem.id', 'agendar.viagem_id')
|
||||
->orderBy('destino.descricao_destino')
|
||||
->groupBy(['destino.id']);
|
||||
|
||||
if ($request->periodo) {
|
||||
|
||||
$destinos->where('agendar.data', '>=', $request->periodo['start']);
|
||||
$destinos->where('agendar.data', '<=', $request->periodo['end']);
|
||||
} else {
|
||||
|
||||
$destinos->where('agendar.data', $currentDate);
|
||||
}
|
||||
|
||||
if ($request->veiculo) {
|
||||
|
||||
$destinos->where('agendar.veiculo_id', $request->veiculo);
|
||||
}
|
||||
|
||||
if ($request->municipio) {
|
||||
|
||||
$destinos->where('agendar.municipio_id', $request->municipio);
|
||||
}
|
||||
|
||||
$destinos = $destinos->get();
|
||||
|
||||
$motivos = DB::table('transporte_paciente_motivo_viagem_procedimento as motivo')
|
||||
->select([
|
||||
'motivo.id',
|
||||
'motivo.descricao',
|
||||
'agendar.motivo_id',
|
||||
'agendar.veiculo_id',
|
||||
'agendar.municipio_id',
|
||||
'agendar.destino_id',
|
||||
])
|
||||
->join('transporte_paciente_agendar_transporte as agendar', 'agendar.motivo_id', 'motivo.id')
|
||||
->join('transporte_paciente_viagem as viagem', 'viagem.id', 'agendar.viagem_id')
|
||||
->orderBy('motivo.descricao')
|
||||
->groupBy(['motivo.id']);
|
||||
|
||||
if ($request->periodo) {
|
||||
|
||||
$motivos->where('agendar.data', '>=', $request->periodo['start']);
|
||||
$motivos->where('agendar.data', '<=', $request->periodo['end']);
|
||||
} else {
|
||||
|
||||
$motivos->where('agendar.data', $currentDate);
|
||||
}
|
||||
|
||||
if ($request->veiculo) {
|
||||
|
||||
$motivos->where('agendar.veiculo_id', $request->veiculo);
|
||||
}
|
||||
|
||||
if ($request->municipio) {
|
||||
|
||||
$motivos->where('agendar.municipio_id', $request->municipio);
|
||||
}
|
||||
|
||||
if ($request->destino) {
|
||||
|
||||
$motivos->where('agendar.destino_id', $request->destino);
|
||||
}
|
||||
|
||||
$motivos = $motivos->get();
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'periodo' => [
|
||||
'start' => $currentDate,
|
||||
'end' => $currentDate,
|
||||
],
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
],
|
||||
'model' => [
|
||||
'veiculos' => $veiculos->map(fn ($veiculo) => [
|
||||
'id' => $veiculo->id,
|
||||
'descricao_veiculo' => $veiculo->descricao_veiculo
|
||||
]),
|
||||
'municipios' => $municipios->map(fn ($municipio) => [
|
||||
'id' => $municipio->id,
|
||||
'nome' => $municipio->nome,
|
||||
'veiculo_id' => $municipio->veiculo_id,
|
||||
]),
|
||||
'destinos' => $destinos->map(fn ($destino) => [
|
||||
'id' => $destino->id,
|
||||
'descricao' => $destino->descricao,
|
||||
'veiculo_id' => $destino->veiculo_id,
|
||||
'municipio_id' => $destino->municipio_id,
|
||||
]),
|
||||
'motivos' => $motivos->map(fn ($motivo) => [
|
||||
'id' => $motivo->id,
|
||||
'descricao' => $motivo->descricao,
|
||||
'motivo_id' => $motivo->motivo_id,
|
||||
'veiculo_id' => $motivo->veiculo_id,
|
||||
'municipio_id' => $motivo->municipio_id,
|
||||
]),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function list(Request $request)
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$consulta = DB::table('transporte_paciente_agendar_transporte as agendar')
|
||||
->select(
|
||||
'agendar.acompanhante',
|
||||
'agendar.veiculo_id',
|
||||
'municipio.nome as municipio',
|
||||
'destino.descricao_destino as destino',
|
||||
'motivo.descricao as motivo_descricao',
|
||||
DB::raw("CONCAT(DATE_FORMAT(agendar.data, '%d/%m/%Y'), ' - ', DATE_FORMAT(agendar.horario, '%H:%i')) as hora_agendamento"),
|
||||
DB::raw("CONCAT(veiculo.numero_placa, ' - ', veiculo.descricao_modelo) AS descricao_veiculo"),
|
||||
DB::raw('COUNT(*) as total_paciente'),
|
||||
DB::raw('SUM(IFNULL(agendar.acompanhante, 0)) AS quantidade_acompanhantes'),
|
||||
DB::raw('SUM(IFNULL(agendar.acompanhante, 0) + 1) AS quantidade_paciente_acompanhante'),
|
||||
DB::raw('IF(viagem.protocolo IS NOT NULL, viagem.protocolo, "-") AS protocolo')
|
||||
)
|
||||
->leftJoin('transporte_paciente_veiculo as veiculo', 'agendar.veiculo_id', 'veiculo.id')
|
||||
->leftJoin('transporte_paciente_veiculo_marca as marca', 'marca.id', 'veiculo.marca_id')
|
||||
->leftJoin('transporte_paciente_viagem as viagem', 'viagem.id', 'agendar.viagem_id')
|
||||
->join('intranet.cadastro_municipio as municipio', 'agendar.municipio_id', 'municipio.id')
|
||||
->join('transporte_paciente_destino as destino', 'agendar.destino_id', 'destino.id')
|
||||
->join('transporte_paciente_motivo_viagem_procedimento as motivo', 'motivo.id', 'agendar.motivo_id')
|
||||
->groupBy(
|
||||
'agendar.veiculo_id',
|
||||
'municipio.nome',
|
||||
'destino.descricao_destino',
|
||||
'motivo.descricao',
|
||||
DB::raw("CONCAT(DATE_FORMAT(agendar.data, '%d/%m/%Y'), ' - ', DATE_FORMAT(agendar.horario, '%H:%i'))")
|
||||
)
|
||||
->orderBy('agendar.data');
|
||||
|
||||
if ($request->periodo) {
|
||||
|
||||
$consulta->where('agendar.data', '>=', $request->periodo['start']);
|
||||
$consulta->where('agendar.data', '<=', $request->periodo['end']);
|
||||
} else {
|
||||
|
||||
$consulta->where('agendar.data', $currentDate);
|
||||
}
|
||||
|
||||
if ($request->veiculo) {
|
||||
|
||||
$consulta->where('agendar.veiculo_id', $request->veiculo);
|
||||
}
|
||||
|
||||
if ($request->municipio) {
|
||||
|
||||
$consulta->where('agendar.municipio_id', $request->municipio);
|
||||
}
|
||||
|
||||
if ($request->destino) {
|
||||
|
||||
$consulta->where('agendar.destino_id', $request->destino);
|
||||
}
|
||||
|
||||
if ($request->motivo) {
|
||||
|
||||
$consulta->where('agendar.motivo_id', $request->motivo);
|
||||
}
|
||||
|
||||
if(request()->protocolo) {
|
||||
$consulta->where('viagem.protocolo', request()->protocolo);
|
||||
}
|
||||
|
||||
$consulta = $consulta->get();
|
||||
|
||||
$totalizadores = [
|
||||
'total_paciente' => $consulta->sum('total_paciente'),
|
||||
'total_acompanhantes' => $consulta->sum('quantidade_acompanhantes'),
|
||||
'total_paciente_acompanhante' => $consulta->sum('quantidade_paciente_acompanhante'),
|
||||
];
|
||||
|
||||
return ['consulta' => $consulta, 'totalizadores' => $totalizadores];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user