407 lines
15 KiB
PHP
407 lines
15 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Farmacia;
|
|
|
|
use App\Models\Cadastro\Estabelecimento\CentroCusto;
|
|
use App\Models\Cadastro\Estabelecimento\Infraestrutura;
|
|
use App\Models\Cadastro\Estabelecimento\Unidade;
|
|
use App\Models\Cadastro\Material\Material;
|
|
use App\Models\Cadastro\SujeitoAtencao\Paciente;
|
|
use App\Services\Cadastro\Material\MaterialService;
|
|
use App\Services\Service;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class RelatorioAltoCustoProcessoPacienteService extends Service
|
|
{
|
|
public static function index()
|
|
{
|
|
$pacientes = collect();
|
|
|
|
$retiradas = DB::table('farmacia_alto_custo_processo_item_mes as mes')
|
|
->join(
|
|
'farmacia_alto_custo_processo_item as item',
|
|
'item.id',
|
|
'mes.item_id'
|
|
)
|
|
->join(
|
|
'cadastro_material as material',
|
|
'material.id',
|
|
'item.material_id'
|
|
)
|
|
->join(
|
|
'cadastro_apresentacao as apresentacao',
|
|
'apresentacao.id',
|
|
'material.apresentacao_id'
|
|
)
|
|
->join(
|
|
'farmacia_alto_custo_processo as processo',
|
|
'processo.id',
|
|
'item.processo_id'
|
|
)
|
|
->join(
|
|
'cadastro_centro_custo as centro_custo',
|
|
'centro_custo.id',
|
|
'processo.created_centro_custo_id'
|
|
)
|
|
->join(
|
|
'cadastro_infraestrutura as infraestrutura',
|
|
'infraestrutura.id',
|
|
'centro_custo.infraestrutura_id'
|
|
)
|
|
->join(
|
|
'cadastro_paciente as paciente',
|
|
'paciente.id',
|
|
'processo.paciente_id'
|
|
)
|
|
->join(
|
|
'cadastro_cidadao as cidadao',
|
|
'cidadao.id',
|
|
'paciente.cidadao_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_unidade_medida as apresentacao_unidade_medida',
|
|
'apresentacao_unidade_medida.id',
|
|
'material.apresentacao_unidade_medida_id'
|
|
)
|
|
->leftJoin('cadastro_prontuario_unidade as prontuario_unidade', function ($join) {
|
|
|
|
$join->on('prontuario_unidade.paciente_id', 'paciente.id')
|
|
->where('prontuario_unidade.unidade_id', self::getCurrentCentroCusto()->infraestrutura->unidade_id);
|
|
})
|
|
->whereNotNull('mes.retirado_paciente_em')
|
|
->when(request()->unidade_id, fn ($q, $id) => $q->where('infraestrutura.unidade_id', $id))
|
|
->when(request()->infraestrutura_id, fn ($q, $id) => $q->where('infraestrutura.id', $id))
|
|
->when(request()->centro_custo_id, fn ($q, $id) => $q->where('centro_custo.id', $id))
|
|
->when(request()->material_id, fn ($q, $id) => $q->where('item.material_id', $id))
|
|
->when(request()->paciente_id, fn ($q, $id) => $q->where('paciente.id', $id))
|
|
->select(DB::raw("
|
|
paciente.id as paciente_id,
|
|
cidadao.nome as paciente_nome,
|
|
LPAD(paciente.prontuario, 6, '0') as paciente_prontuario,
|
|
prontuario_unidade.prontuario as paciente_prontuario_unidade,
|
|
material.id as material_id,
|
|
" . MaterialService::getDescricaoCompletaRaw(
|
|
'material',
|
|
'apresentacao',
|
|
'apresentacao_unidade_medida'
|
|
) . " as material_descricao,
|
|
DATE_FORMAT(mes.retirado_paciente_em, '%d/%m/%Y') as retirado_paciente_em
|
|
"))
|
|
->orderBy('mes.retirado_paciente_em')
|
|
->get()
|
|
->each(function ($data) use ($pacientes) {
|
|
|
|
$paciente = $pacientes->where('id', $data->paciente_id)->first();
|
|
|
|
if (is_null($paciente)) {
|
|
|
|
$paciente = (object) [
|
|
'id' => $data->paciente_id,
|
|
'paciente_nome' => $data->paciente_nome,
|
|
'paciente_prontuario' => $data->paciente_prontuario,
|
|
'paciente_prontuario_unidade' => $data->paciente_prontuario_unidade,
|
|
'materiais' => collect(),
|
|
];
|
|
|
|
$pacientes->push($paciente);
|
|
}
|
|
|
|
$material = $paciente->materiais->where('id', $data->material_id)->first();
|
|
|
|
if (is_null($material)) {
|
|
|
|
$material = (object) [
|
|
'id' => $data->material_id,
|
|
'material_descricao' => $data->material_descricao,
|
|
'data_primeira_retirada' => $data->retirado_paciente_em,
|
|
'retiradas' => collect(),
|
|
];
|
|
|
|
$paciente->materiais->push($material);
|
|
}
|
|
|
|
$material->retiradas->push($data->retirado_paciente_em);
|
|
});
|
|
|
|
$pacientes->each(fn ($paciente) => $paciente->materiais = $paciente->materiais
|
|
->sortBy('material_descricao')->values());
|
|
|
|
return compact('pacientes');
|
|
}
|
|
|
|
public static function list()
|
|
{
|
|
$data = (object) [
|
|
'form' => [],
|
|
'model' => [],
|
|
];
|
|
|
|
$params = self::getParams();
|
|
|
|
$sets = [
|
|
'unidade' => function () use ($data, $params) {
|
|
|
|
$unidadesData = self::getUnidadesData();
|
|
|
|
$data->form['unidades'] = $unidadesData['form'];
|
|
$data->model['unidade_id'] = $params->unidade_id = $unidadesData['model'];
|
|
},
|
|
'infraestrutura' => function () use ($data, $params) {
|
|
|
|
$infraestruturasData = self::getInfraestruturasData($params);
|
|
|
|
$data->form['infraestruturas'] = $infraestruturasData['form'];
|
|
$data->model['infraestrutura_id'] = $params->infraestrutura_id = $infraestruturasData['model'];
|
|
},
|
|
'centro_custo' => function () use ($data, $params) {
|
|
|
|
$centrosDeCustoData = self::getCentrosDeCustoData($params);
|
|
|
|
$data->form['centros_custo'] = $centrosDeCustoData['form'];
|
|
$data->model['centro_custo_id'] = $params->centro_custo_id = $centrosDeCustoData['model'];
|
|
},
|
|
'material' => function () use ($data) {
|
|
|
|
$data->model['material'] = null;
|
|
},
|
|
'paciente' => function () use ($data) {
|
|
|
|
$data->model['paciente'] = null;
|
|
},
|
|
];
|
|
|
|
$load = is_null(request()->load_from);
|
|
|
|
foreach ($sets as $field => $set) {
|
|
|
|
if ($load) {
|
|
|
|
$set();
|
|
}
|
|
else if ($field === request()->load_from) {
|
|
|
|
$load = true;
|
|
}
|
|
}
|
|
|
|
foreach ($data as $property => $itens) {
|
|
|
|
if (empty($itens)) {
|
|
|
|
unset($data->{$property});
|
|
}
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
private static function getParams()
|
|
{
|
|
return (object) request()->only([
|
|
'unidade_id',
|
|
'infraestrutura_id',
|
|
'centro_custo_id',
|
|
'material_id',
|
|
]);
|
|
}
|
|
|
|
private static function getUnidadesData()
|
|
{
|
|
$unidades = Unidade::setEagerLoads([])
|
|
->whereHas('infraestruturas', fn ($q) => $q
|
|
->whereHas('centrosCusto', fn ($q) => self::setWhereForFilterCentroCusto($q))
|
|
)
|
|
->orderBy('nome_fantasia')
|
|
->get(['id', 'nome_fantasia']);
|
|
|
|
return ['form' => $unidades, 'model' => null];
|
|
}
|
|
|
|
private static function getInfraestruturasData($params)
|
|
{
|
|
$infraestruturas = collect();
|
|
$id = null;
|
|
|
|
if (!is_null($params->unidade_id)) {
|
|
|
|
$infraestruturas = Infraestrutura::setEagerLoads([])
|
|
->where('unidade_id', $params->unidade_id)
|
|
->whereHas('centrosCusto', fn ($q) => self::setWhereForFilterCentroCusto($q))
|
|
->get(['id', 'nome', 'principal', 'unidade_id'])
|
|
->map(fn ($i) => [
|
|
'id' => $i->id,
|
|
'nome' => $i->nome
|
|
])
|
|
->sortBy('nome')
|
|
->values();
|
|
}
|
|
|
|
return ['form' => $infraestruturas, 'model' => $id];
|
|
}
|
|
|
|
private static function getCentrosDeCustoData($params)
|
|
{
|
|
$centrosCusto = collect();
|
|
$id = null;
|
|
|
|
if (!is_null($params->infraestrutura_id)) {
|
|
|
|
$centrosCusto = CentroCusto::setEagerLoads([])
|
|
->where('infraestrutura_id', $params->infraestrutura_id)
|
|
->where(fn ($q) => self::setWhereForFilterCentroCusto($q))
|
|
->get(['id', 'setor_id'])
|
|
->map(fn ($cc) => [
|
|
'id' => $cc->id,
|
|
'setor_nome' => $cc->setor()->setEagerLoads([])->first(['nome'])->nome
|
|
])
|
|
->sortBy('setor_nome')
|
|
->values();
|
|
}
|
|
|
|
return ['form' => $centrosCusto, 'model' => $id];
|
|
}
|
|
|
|
private static function setWhereForFilterCentroCusto($query)
|
|
{
|
|
return $query->whereHas('farmaciaAltoCustoProcessos', fn ($q) => $q
|
|
->whereHas('itens', fn ($q) => $q
|
|
->whereHas('meses', fn ($q) => $q
|
|
->whereNotNull('retirado_paciente_em')
|
|
)
|
|
)
|
|
);
|
|
}
|
|
|
|
public static function autocompleteMaterial()
|
|
{
|
|
$materiais = DB::table('cadastro_material as material')
|
|
->join(
|
|
'cadastro_apresentacao as apresentacao',
|
|
'apresentacao.id',
|
|
'material.apresentacao_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_unidade_medida as apresentacao_unidade_medida',
|
|
'apresentacao_unidade_medida.id',
|
|
'material.apresentacao_unidade_medida_id'
|
|
)
|
|
->whereExists(function ($query) {
|
|
|
|
$params = self::getParams();
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('farmacia_alto_custo_processo_item as item')
|
|
->join(
|
|
'farmacia_alto_custo_processo_item_mes as mes',
|
|
'mes.item_id',
|
|
'item.id'
|
|
)
|
|
->join(
|
|
'farmacia_alto_custo_processo as processo',
|
|
'processo.id',
|
|
'item.processo_id'
|
|
)
|
|
->join(
|
|
'cadastro_centro_custo as centro_custo',
|
|
'centro_custo.id',
|
|
'processo.created_centro_custo_id'
|
|
)
|
|
->join(
|
|
'cadastro_infraestrutura as infraestrutura',
|
|
'infraestrutura.id',
|
|
'centro_custo.infraestrutura_id'
|
|
)
|
|
->whereColumn('item.material_id', 'material.id')
|
|
->whereNotNull('mes.retirado_paciente_em')
|
|
->when($params->unidade_id, fn ($q, $id) => $q->where('infraestrutura.unidade_id', $id))
|
|
->when($params->infraestrutura_id, fn ($q, $id) => $q->where('infraestrutura.id', $id))
|
|
->when($params->centro_custo_id, fn ($q, $id) => $q->where('centro_custo.id', $id));
|
|
})
|
|
->select(
|
|
'material.id',
|
|
DB::raw(MaterialService::getDescricaoCompletaRaw(
|
|
'material',
|
|
'apresentacao',
|
|
'apresentacao_unidade_medida'
|
|
) . " AS descricao")
|
|
);
|
|
|
|
return $materiais;
|
|
}
|
|
|
|
public static function autocompletePaciente()
|
|
{
|
|
return DB::table('cadastro_paciente as paciente')
|
|
->join(
|
|
'cadastro_cidadao as cidadao',
|
|
'cidadao.id',
|
|
'paciente.cidadao_id'
|
|
)
|
|
->leftJoin('cadastro_prontuario_unidade as prontuario_unidade', function ($join) {
|
|
|
|
$join->on('prontuario_unidade.paciente_id', 'paciente.id')
|
|
->where('prontuario_unidade.unidade_id', self::getCurrentCentroCusto()->infraestrutura->unidade_id);
|
|
})
|
|
->whereExists(function ($query) {
|
|
|
|
$params = self::getParams();
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('farmacia_alto_custo_processo as processo')
|
|
->join(
|
|
'farmacia_alto_custo_processo_item as item',
|
|
'item.processo_id',
|
|
'processo.id'
|
|
)
|
|
->join(
|
|
'farmacia_alto_custo_processo_item_mes as mes',
|
|
'mes.item_id',
|
|
'item.id'
|
|
)
|
|
->join(
|
|
'cadastro_centro_custo as centro_custo',
|
|
'centro_custo.id',
|
|
'processo.created_centro_custo_id'
|
|
)
|
|
->join(
|
|
'cadastro_infraestrutura as infraestrutura',
|
|
'infraestrutura.id',
|
|
'centro_custo.infraestrutura_id'
|
|
)
|
|
->whereColumn('processo.paciente_id', 'paciente.id')
|
|
->whereNotNull('mes.retirado_paciente_em')
|
|
->when($params->unidade_id, fn ($q, $id) => $q->where('infraestrutura.unidade_id', $id))
|
|
->when($params->infraestrutura_id, fn ($q, $id) => $q->where('infraestrutura.id', $id))
|
|
->when($params->centro_custo_id, fn ($q, $id) => $q->where('centro_custo.id', $id))
|
|
->when($params->material_id, fn ($q, $id) => $q->where('item.material_id', $id));
|
|
})
|
|
->select(DB::raw("
|
|
paciente.id,
|
|
cidadao.nome
|
|
"));
|
|
}
|
|
|
|
public static function getPrintExportData()
|
|
{
|
|
$resultado = self::index();
|
|
$itens = $resultado['pacientes'];
|
|
|
|
$unidade = Unidade::find(request()->unidade_id);
|
|
$infraestrutura = Infraestrutura::find(request()->infraestrutura_id);
|
|
$centroCusto = CentroCusto::find(request()->centro_custo_id);
|
|
$material = Material::find(request()->material_id);
|
|
$paciente = Paciente::find(request()->paciente_id);
|
|
|
|
return [
|
|
'unidade' => $unidade->nome_fantasia ?? 'Todas',
|
|
'infraestrutura' => $infraestrutura->nome ?? 'Todos',
|
|
'setor' => $centroCusto->setor->nome ?? 'Todos',
|
|
'material' => $material->descricao_completa ?? 'Todos',
|
|
'paciente' => $paciente->cidadao->nome ?? 'Todos',
|
|
'colunas' => collect(request()->selectedColumns),
|
|
'itens' => $itens,
|
|
];
|
|
}
|
|
}
|