388 lines
14 KiB
PHP
388 lines
14 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Farmacia;
|
|
|
|
use App\Enums\Farmacia\AltoCusto\ProcessoItemMesStatus;
|
|
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 Carbon\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class RelatorioAltoCustoRetiradaPendenteService extends Service
|
|
{
|
|
public static function index()
|
|
{
|
|
$periodo = [
|
|
'start' => Carbon::createFromFormat('d/m/Y', request()->periodo['start'])->setTime(0, 0, 0),
|
|
'end' => Carbon::createFromFormat('d/m/Y', request()->periodo['end'])->setTime(23, 59, 59)
|
|
];
|
|
|
|
$processos = 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_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'
|
|
)
|
|
->where('mes.status_id', ProcessoItemMesStatus::PENDENTE_RETIRADA_PACIENTE)
|
|
->whereBetween(request()->periodo_tipo === 'solicitacao'
|
|
? 'processo.solicitado_em' : 'mes.retirado_drs_em', $periodo)
|
|
->when(request()->centro_custo_id, fn ($q, $id) => $q->where('processo.created_centro_custo_id', $id))
|
|
->when(request()->paciente_id, fn ($q, $id) => $q->where('paciente.id', $id))
|
|
->when(request()->material_id, fn ($q, $id) => $q->where('material.id', $id))
|
|
->select(DB::raw("
|
|
cidadao.nome as paciente,
|
|
LPAD(paciente.prontuario, 6, '0') as prontuario,
|
|
mes.retirado_drs_em,
|
|
" . MaterialService::getDescricaoCompletaRaw(
|
|
'material',
|
|
'apresentacao',
|
|
'apresentacao_unidade_medida'
|
|
) . " as material
|
|
"))
|
|
->addSelect([
|
|
'prontuario_unidade' => function ($query) {
|
|
|
|
$query->select('prontuario_unidade.prontuario')
|
|
->from('cadastro_prontuario_unidade as prontuario_unidade')
|
|
->whereColumn('prontuario_unidade.paciente_id', 'paciente.id')
|
|
->where('prontuario_unidade.unidade_id', request()->unidade_id)
|
|
->limit(1);
|
|
}
|
|
])
|
|
->when(request()->order_by, fn ($q, $orderBy) => $q->orderBy($orderBy))
|
|
->get();
|
|
|
|
return compact('processos');
|
|
}
|
|
|
|
public static function list()
|
|
{
|
|
$data = (object) [
|
|
'additionals' => [],
|
|
'form' => [],
|
|
'model' => [],
|
|
];
|
|
|
|
$params = self::getParams();
|
|
|
|
$sets = [
|
|
'periodo' => function () use ($data) {
|
|
|
|
$data->model['periodo'] = ['start' => null, 'end' => null];
|
|
},
|
|
'unidade' => function () use ($data, $params) {
|
|
|
|
$unidadesData = self::getUnidadesData($params);
|
|
|
|
$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) {
|
|
|
|
$centrosCustoData = self::getCentrosDeCustoData($params);
|
|
|
|
$data->form['centros_custo'] = $centrosCustoData['form'];
|
|
$data->model['centro_custo_id'] = $params->centro_custo_id = $centrosCustoData['model'];
|
|
},
|
|
'paciente' => function () use ($data, $params) {
|
|
|
|
$pacientesData = self::getPacientesData($params);
|
|
|
|
$data->form['pacientes'] = $pacientesData['form'];
|
|
$data->model['paciente_id'] = $params->paciente_id = $pacientesData['model'];
|
|
},
|
|
'material' => function () use ($data) {
|
|
|
|
$data->model['material'] = null;
|
|
}
|
|
];
|
|
|
|
$load = is_null(request()->load_from);
|
|
|
|
if ($load) {
|
|
|
|
$data->form['periodo_tipos'] = self::getTiposPeriodo();
|
|
$data->model['periodo_tipo'] = 'solicitacao';
|
|
|
|
$data->additionals = [
|
|
'data_atual' => Carbon::now()->format('d/m/Y'),
|
|
];
|
|
}
|
|
|
|
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 getTiposPeriodo()
|
|
{
|
|
return collect([
|
|
(object) ['value' => 'retirada_drs', 'text' => 'Retirada DRS'],
|
|
(object) ['value' => 'solicitacao', 'text' => 'Solicitação'],
|
|
]);
|
|
}
|
|
|
|
private static function getParams()
|
|
{
|
|
$params = (object) request()->only([
|
|
'periodo_tipo',
|
|
'periodo',
|
|
'unidade_id',
|
|
'infraestrutura_id',
|
|
'centro_custo_id',
|
|
'paciente_id',
|
|
]);
|
|
|
|
if (isset($params->periodo)
|
|
&& !is_null($params->periodo['start'])
|
|
&& !is_null($params->periodo['end'])
|
|
) {
|
|
|
|
$start = Carbon::createFromFormat('d/m/Y', $params->periodo['start'])->setTime(0, 0, 0);
|
|
$end = Carbon::createFromFormat('d/m/Y', $params->periodo['end'])->setTime(23, 59, 59);
|
|
|
|
$params->periodo['start'] = $start;
|
|
$params->periodo['end'] = $end;
|
|
}
|
|
else {
|
|
|
|
$params->periodo = null;
|
|
}
|
|
|
|
return $params;
|
|
}
|
|
|
|
private static function getUnidadesData()
|
|
{
|
|
$unidades = collect();
|
|
$id = self::getCurrentCentroCusto()->infraestrutura->unidade_id;
|
|
|
|
$unidades = Unidade::where('id', $id)
|
|
->orderBy('nome_fantasia')
|
|
->get(['id', 'nome_fantasia']);
|
|
|
|
return ['form' => $unidades, 'model' => $id];
|
|
}
|
|
|
|
private static function getInfraestruturasData($params)
|
|
{
|
|
$infraestruturas = collect();
|
|
$id = self::getCurrentCentroCusto()->infraestrutura_id;
|
|
|
|
if (!is_null($params->unidade_id)) {
|
|
|
|
$infraestruturas = Infraestrutura::where('id', $id)
|
|
->where('unidade_id', $params->unidade_id)
|
|
->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 = self::getCurrentCentroCusto()->id;
|
|
|
|
if (!is_null($params->infraestrutura_id)) {
|
|
|
|
$centrosCusto = CentroCusto::where('id', $id)
|
|
->where('infraestrutura_id', $params->infraestrutura_id)
|
|
->get(['id', 'setor_id'])
|
|
->map(fn ($cc) => ['id' => $cc->id, 'setor_nome' => $cc->setor->nome])
|
|
->sortBy('setor_nome')
|
|
->values();
|
|
}
|
|
|
|
return ['form' => $centrosCusto, 'model' => $id];
|
|
}
|
|
|
|
private static function getPacientesData($params)
|
|
{
|
|
$pacientes = collect();
|
|
$id = null;
|
|
|
|
if (!is_null($params->periodo)) {
|
|
|
|
$pacientes = Paciente::whereHas('farmaciaAltoCustoProcessos', fn ($q) =>
|
|
self::setWhereForSelectedCentroCusto($q, $params)
|
|
->when($params->periodo_tipo === 'solicitacao', fn ($q) => $q
|
|
->whereBetween('solicitado_em', $params->periodo)
|
|
)
|
|
->whereHas('itens', fn ($q) => $q
|
|
->whereHas('meses', fn ($q) => $q
|
|
->when($params->periodo_tipo === 'retirada_drs', fn ($q) => $q
|
|
->whereBetween('retirado_drs_em', $params->periodo)
|
|
)
|
|
->where('status_id', ProcessoItemMesStatus::PENDENTE_RETIRADA_PACIENTE)
|
|
)
|
|
)
|
|
)
|
|
->get(['id', 'cidadao_id'])
|
|
->map(fn ($p) => ['id' => $p->id, 'nome' => $p->cidadao->nome])
|
|
->sortBy('nome')
|
|
->values();
|
|
}
|
|
|
|
return ['form' => $pacientes, 'model' => $id];
|
|
}
|
|
|
|
public static function setWhereForFilterMaterial($query, $params)
|
|
{
|
|
$query->whereHas('farmaciaProcessoItens', fn ($q) => $q
|
|
->whereHas('processo', fn ($q) =>
|
|
self::setWhereForSelectedCentroCusto($q, $params)
|
|
->when($params->periodo_tipo === 'solicitacao', fn ($q) => $q
|
|
->whereBetween('solicitado_em', $params->periodo)
|
|
)
|
|
->when(request()->paciente_id, fn ($q, $id) => $q->where('paciente_id', $id))
|
|
)
|
|
->whereHas('meses', fn ($q) => $q
|
|
->when($params->periodo_tipo === 'retirada_drs', fn ($q) => $q
|
|
->whereBetween('retirado_drs_em', $params->periodo)
|
|
)
|
|
->where('status_id', ProcessoItemMesStatus::PENDENTE_RETIRADA_PACIENTE)
|
|
)
|
|
);
|
|
}
|
|
|
|
private static function setWhereForSelectedCentroCusto($query, $params)
|
|
{
|
|
return $query->when($params->centro_custo_id,
|
|
fn ($q, $centroCustoId) => $q->where('created_centro_custo_id', $centroCustoId),
|
|
fn ($q) => $q->when(!is_null($params->infraestrutura_id) || !is_null($params->unidade_id),
|
|
fn ($q) => $q->whereHas('createdCentroCusto', fn ($q) => $q
|
|
->when($params->infraestrutura_id,
|
|
fn ($q, $infraestruturaId) => $q->where('infraestrutura_id', $infraestruturaId),
|
|
fn ($q) => $q->whereHas('infraestrutura', fn ($q) => $q
|
|
->where('unidade_id', $params->unidade_id))
|
|
)
|
|
)
|
|
)
|
|
);
|
|
}
|
|
|
|
public static function autocompleteMaterial()
|
|
{
|
|
$params = self::getParams();
|
|
|
|
$materiais = Material::where(fn ($q) => self::setWhereForFilterMaterial($q, $params))
|
|
->getQuery()
|
|
->join(
|
|
'cadastro_apresentacao',
|
|
'cadastro_apresentacao.id',
|
|
'cadastro_material.apresentacao_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_unidade_medida as apresentacao_unidade_medida',
|
|
'apresentacao_unidade_medida.id',
|
|
'cadastro_material.apresentacao_unidade_medida_id'
|
|
)
|
|
->select(
|
|
'cadastro_material.id',
|
|
DB::raw(MaterialService::getDescricaoCompletaRaw(
|
|
'cadastro_material',
|
|
'cadastro_apresentacao',
|
|
'apresentacao_unidade_medida'
|
|
) . " as descricao"),
|
|
);
|
|
|
|
return $materiais;
|
|
}
|
|
|
|
public static function getPrintExportData()
|
|
{
|
|
$resultado = self::index();
|
|
$itens = $resultado['processos'];
|
|
|
|
$itens->each(function ($item) {
|
|
|
|
$item->retirado_drs_em = (new Carbon($item->retirado_drs_em))->format('d/m/Y');
|
|
});
|
|
|
|
$periodoTipo = self::getTiposPeriodo()->where('value', request()->periodo_tipo)->first();
|
|
$unidade = Unidade::find(request()->unidade_id);
|
|
$infraestrutura = Infraestrutura::find(request()->infraestrutura_id);
|
|
$centroCusto = CentroCusto::find(request()->centro_custo_id);
|
|
$paciente = Paciente::find(request()->paciente_id);
|
|
$material = Material::find(request()->material_id);
|
|
|
|
return [
|
|
'periodoTipo' => $periodoTipo->text,
|
|
'inicio' => request()->periodo['start'],
|
|
'fim' => request()->periodo['end'],
|
|
'unidade' => $unidade->nome_fantasia ?? 'Todas',
|
|
'infraestrutura' => $infraestrutura->nome ?? 'Todos',
|
|
'setor' => $centroCusto->setor->nome ?? 'Todos',
|
|
'paciente' => $paciente->cidadao->nome ?? 'Todos',
|
|
'material' => $material->descricao_completa ?? 'Todos',
|
|
'colunas' => collect(request()->selectedColumns),
|
|
'itens' => $itens,
|
|
];
|
|
}
|
|
}
|