4099 lines
210 KiB
PHP
4099 lines
210 KiB
PHP
<?php
|
|
|
|
namespace App\Services\ExameLaboratorial;
|
|
|
|
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
|
use App\Enums\ExameLaboratorial\TipoCota;
|
|
use App\Enums\ExameLaboratorial\TipoImpressaoAgendamento as EnumsTipoImpressaoAgendamento;
|
|
use App\Enums\ExameLaboratorial\TipoUnidade;
|
|
use App\Enums\Parametrizacao\Exame\Status;
|
|
use App\Enums\Parametrizacao\Exame\Tipo;
|
|
use App\Enums\Sigtap\Procedimento as ProcedimentoEnum;
|
|
use App\Exceptions\ExameLaboratorial\DuplicatedCotaExameException;
|
|
use App\Exceptions\ExameLaboratorial\DuplicatedCotaException;
|
|
use App\Exceptions\ExameLaboratorial\ExceededSaldoTransferenciaExameException;
|
|
use App\Exceptions\ExameLaboratorial\ExceededSaldoTransferenciaException;
|
|
use App\Models\Cadastro\Estabelecimento\Infraestrutura;
|
|
use App\Models\Cadastro\Estabelecimento\Unidade;
|
|
use App\Models\Cadastro\SujeitoAtencao\Usuario;
|
|
use App\Models\ExameLaboratorial\AgendamentoExame\Agendamento;
|
|
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoExame;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\Cota;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\CotaExame;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\CotaExamePeriodo;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\CotaExameUnidadeSolicitante;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\CotaTipo;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\CotaTransferencia;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\CotaTransferenciaExames;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\CotaUnidadeSolicitante;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\ExameAmbulatorioAtendimento;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\ImpressaoAgendamento;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\AppCidadaoLaboratorio;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\AppConectasusCidadaoExame;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\AppConectasusCidadaoUnidadeSolicitante;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\IntegracaoAplicativo;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\LaboratorioFatura;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\LaboratorioFaturaUnidade;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\LaboratorioFaturaUsuario;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\MensagemProtocolo;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\MesCota;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\Motivo;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\ProfissionalAssinatura;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\ReagendarExame;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\Supervisor;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\TipoImpressaoAgendamento;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\Unidade as ExameLaboratorial;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\UnidadeFaturamento;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\UnidadeFaturamentoUnidade;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\UnidadeFaturamentoUsuario;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\UnidadeSolicitante;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\UnidadeSolicitanteExame;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\UnidadeSolicitanteExameItem;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\UnidadeTipo;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\WebService;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\WebServiceTipo;
|
|
use App\Models\Parametrizacao\Exame\Exame;
|
|
use App\Services\Service;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ParametrizacaoService extends Service
|
|
{
|
|
public static function create()
|
|
{
|
|
$mensagemProtocolo = MensagemProtocolo::select('mensagem')->first();
|
|
$impressaoAgendamento = ImpressaoAgendamento::select('tipo_id')->first();
|
|
$webservice = WebService::select('tipo_id')->first();
|
|
$reagendarExame = ReagendarExame::select('exibir')->first();
|
|
$exameAmbulatorioAtendimento = ExameAmbulatorioAtendimento::select('exibir')->first();
|
|
$integracaoAplicativo = IntegracaoAplicativo::first();
|
|
|
|
return [
|
|
'form' => [
|
|
'unidades' => Unidade::orderBy('nome_fantasia')
|
|
->get()
|
|
->map(fn($query) => [
|
|
'id' => $query->id,
|
|
'nome_fantasia' => $query->nome_fantasia,
|
|
'infraestruturas' => $query->infraestruturas->sortBy('nome')->map(fn($infraestrutura) => [
|
|
'id' => $infraestrutura->id,
|
|
'nome' => $infraestrutura->nome,
|
|
'principal' => $infraestrutura->principal,
|
|
])->values()
|
|
]),
|
|
'unidades_faturamento' => self::getUnidadesFaturamento(),
|
|
'laboratorios_faturamento' => self::getLaboratoriosFaturamento(),
|
|
'tipo' => CotaTipo::orderBy('descricao')->get()->map(fn($tipo) => [
|
|
'id' => $tipo->id,
|
|
'descricao' => $tipo->descricao
|
|
]),
|
|
'tipo_impressao_agendamento' => TipoImpressaoAgendamento::orderBy('descricao')->get()->map(fn($tipo) => [
|
|
'id' => $tipo->id,
|
|
'descricao' => $tipo->descricao
|
|
]),
|
|
'tipo_webservice' => WebServiceTipo::orderBy('nome')->get()->map(fn($tipo) => [
|
|
'id' => $tipo->id,
|
|
'nome' => $tipo->nome
|
|
]),
|
|
'tipo_unidade' => UnidadeTipo::where('id', '!=', TipoUnidade::UNIDADE_SOLICITANTE)
|
|
->orderBy('descricao')
|
|
->get()
|
|
->map(fn($tipo) => [
|
|
'id' => $tipo->id,
|
|
'descricao' => $tipo->descricao
|
|
]),
|
|
'exames' => Exame::where('tipo_id', Tipo::LABORATORIAL)->orderBy('nome')
|
|
->get()
|
|
->map(fn($query) => [
|
|
'id' => $query->id,
|
|
'nome' => $query->nome,
|
|
'valor' => $query->valor,
|
|
]),
|
|
'anos' => self::getAno(),
|
|
'meses' => MesCota::orderBy('id')->get()->map(fn($mes) => [
|
|
'id' => substr("0" . $mes->id, -2),
|
|
'descricao' => $mes->descricao
|
|
]),
|
|
],
|
|
'model' => [
|
|
'unidades' => Infraestrutura::whereHas('exameLaboratorialUnidade')
|
|
->orderBy('nome')
|
|
->get()
|
|
->map(function ($query) {
|
|
return [
|
|
'id' => $query->id ?? null,
|
|
'unidade_id' => $query->exameLaboratorialUnidade->unidade_id ?? null,
|
|
'infraestrutura_id' => $query->exameLaboratorialUnidade->infraestrutura_id ?? null,
|
|
'infraestrutura_nome' => $query->exameLaboratorialUnidade->infraestrutura->nome,
|
|
'infraestrutura_principal' => $query->exameLaboratorialUnidade->infraestrutura->principal,
|
|
'nome_fantasia' => $query->exameLaboratorialUnidade->unidade->nome_fantasia ?? null,
|
|
'unidade_coleta' => $query->exameLaboratorialUnidade->unidade_coleta ?? null,
|
|
'unidade_coleta_solicitante' => UnidadeSolicitante::where(
|
|
'unidade_tipo_id',
|
|
TipoUnidade::COLETA
|
|
)
|
|
->where('unidade_id', $query->exameLaboratorialUnidade->unidade_id)
|
|
->where('infraestrutura_id', $query->exameLaboratorialUnidade->infraestrutura_id)
|
|
->get()
|
|
->map(function ($laboratorio) {
|
|
return [
|
|
'unidade_id' => $laboratorio->unidade_id ?? null,
|
|
'unidade_solicitante_id' => $laboratorio->unidade_solicitante_id ?? null,
|
|
'unidade_solicitante' => $laboratorio->unidadeSolicitante->nome_fantasia ?? null,
|
|
'tipo' => $laboratorio->tipo ?? null,
|
|
'infraestrutura_id' => $laboratorio->infraestrutura_id ?? null,
|
|
'unidade_solicitante_infraestrutura_id' =>
|
|
$laboratorio->unidade_solicitante_infraestrutura_id ?? null,
|
|
'infraestrutura_nome' => $laboratorio->unidadeSolicitanteInfraestrutura->nome,
|
|
];
|
|
}),
|
|
'laboratorio' => $query->exameLaboratorialUnidade->laboratorio ?? null,
|
|
'laboratorio_padrao_infraestrutura_id' => $query->exameLaboratorialUnidade->laboratorio_padrao_infraestrutura_id ?? null,
|
|
'infraestrutura' => UnidadeSolicitante::where(
|
|
'unidade_tipo_id',
|
|
TipoUnidade::LABORATORIO
|
|
)
|
|
->where('unidade_id', $query->exameLaboratorialUnidade->unidade_id)
|
|
->groupBy('unidade_solicitante_id')
|
|
->get()
|
|
->map(function ($laboratorio) {
|
|
return [
|
|
'unidade_id' => $laboratorio->unidade_id ?? null,
|
|
'unidade_solicitante_id' => $laboratorio->unidade_solicitante_id ?? null,
|
|
'unidade_solicitante' => $laboratorio->unidadeSolicitante->nome_fantasia ?? null,
|
|
'infraestrutura_id' => $laboratorio->infraestrutura_id ?? null,
|
|
'unidade_solicitante_infraestrutura_id' =>
|
|
$laboratorio->unidade_solicitante_infraestrutura_id ?? null,
|
|
'infraestrutura_nome' => $laboratorio->unidadeSolicitanteInfraestrutura->nome,
|
|
'local' => UnidadeSolicitante::where(
|
|
'unidade_tipo_id',
|
|
TipoUnidade::LABORATORIO
|
|
)
|
|
->where('unidade_solicitante_id', $laboratorio->unidade_solicitante_id)
|
|
->where('unidade_id', $laboratorio->unidade_id)
|
|
->where('infraestrutura_id', $laboratorio->infraestrutura_id)
|
|
->get()
|
|
->map(function ($local) {
|
|
return [
|
|
'infraestrutura_id' =>
|
|
$local->unidade_solicitante_infraestrutura_id ?? null,
|
|
'infraestrutura_nome' =>
|
|
$local->unidadeSolicitanteInfraestrutura->nome,
|
|
];
|
|
}),
|
|
];
|
|
}),
|
|
];
|
|
}),
|
|
'unidades_solicitantes' => Infraestrutura::whereHas('exameLaboratorialUnidadeSolicitanteExame')
|
|
->orderBy('nome')
|
|
->get()
|
|
->map(function ($query) {
|
|
return [
|
|
'id' => $query->exameLaboratorialUnidadeSolicitanteExame->id ?? null,
|
|
'alterado' => false,
|
|
'unidade_id' => $query->exameLaboratorialUnidadeSolicitanteExame->unidade_id ?? null,
|
|
'infraestrutura_id' => $query->exameLaboratorialUnidadeSolicitanteExame->infraestrutura_id ?? null,
|
|
'infraestrutura_nome' => $query->exameLaboratorialUnidadeSolicitanteExame->infraestrutura->nome,
|
|
'infraestrutura_principal' => $query->exameLaboratorialUnidadeSolicitanteExame->infraestrutura->principal,
|
|
'nome_fantasia' => $query->exameLaboratorialUnidadeSolicitanteExame->unidade->nome_fantasia ?? null,
|
|
'urgencia' => $query->exameLaboratorialUnidadeSolicitanteExame->urgencia ?? null,
|
|
'exames' => []
|
|
];
|
|
}),
|
|
'unidades_faturamento' => UnidadeFaturamento::get()
|
|
->map(fn($query) => [
|
|
'id' => $query->id,
|
|
'unidade_id' => $query->unidade_id,
|
|
'nomeFantasia' => $query->unidade->nome_fantasia,
|
|
'infraestrutura_id' => $query->infraestrutura_id,
|
|
'infraestrutura_nome' => $query->infraestrutura->nome,
|
|
'profissionais' => [],
|
|
'faturamento_relacionado' => [],
|
|
'alterado' => false,
|
|
]),
|
|
'laboratorios_faturamento' => LaboratorioFatura::get()
|
|
->map(fn($query) => [
|
|
'id' => $query->id,
|
|
'unidade_id' => $query->unidade_id,
|
|
'nomeFantasia' => $query->unidade->nome_fantasia,
|
|
'infraestrutura_id' => $query->infraestrutura_id,
|
|
'infraestrutura_nome' => $query->infraestrutura->nome,
|
|
'profissionais' => [],
|
|
'faturamento_relacionado' => [],
|
|
'alterado' => false,
|
|
]),
|
|
'cotas_unidade_solicitante' => Cota::where('unidade_tipo_id', TipoUnidade::UNIDADE_SOLICITANTE)
|
|
->groupBy('unidade_id', 'infraestrutura_id')
|
|
->get()
|
|
->map(function ($query) {
|
|
return [
|
|
'unidade_id' => $query->unidade->id ?? null,
|
|
'nome_fantasia' => $query->unidade->nome_fantasia ?? null,
|
|
'unidade_tipo_id' => $query->unidade_tipo_id ?? null,
|
|
'infraestrutura_id' => $query->infraestrutura_id ?? null,
|
|
'infraestrutura_nome' => $query->infraestrutura->nome ?? null,
|
|
'cota_unidade_solicitante' => Cota::where('unidade_tipo_id', TipoUnidade::UNIDADE_SOLICITANTE)
|
|
->where('unidade_id', $query->unidade->id)
|
|
->where('infraestrutura_id', $query->infraestrutura_id)
|
|
->orderBy('inicio')
|
|
->get()
|
|
->map(fn($unidade) => [
|
|
'id' => $unidade->id ?? null,
|
|
'unidade_tipo_id' => $unidade->unidade_tipo_id ?? null,
|
|
'cota_tipo_id' => $unidade->cota_tipo_id ?? null,
|
|
'cota_tipo_nome' => $unidade->cota_tipo_id == TipoCota::VALOR
|
|
? 'Valor' : 'Quantidade',
|
|
"periodo" => [
|
|
'start' => (new Carbon($unidade->inicio))->format('d/m/Y') ?? null,
|
|
'end' => (new Carbon($unidade->fim))->format('d/m/Y') ?? null,
|
|
],
|
|
'cota' => $unidade->cota_tipo_id == TipoCota::VALOR
|
|
? number_format($unidade->cota_valor, 2, ',', '.')
|
|
: $unidade->cota_quantidade,
|
|
'saldo' => $unidade->cota_tipo_id == TipoCota::VALOR
|
|
? number_format($unidade->cota_valor, 2, ',', '.')
|
|
: $unidade->cota_quantidade,
|
|
'utilizada' => "0",
|
|
'cota_quantidade' => $unidade->cota_quantidade ?? null,
|
|
'cota_valor' => number_format($unidade->cota_valor, 2, ',', '.') ?? null,
|
|
'observacao' => $unidade->observacao ?? null,
|
|
'quantidade_utilizada' => $unidade->cota_tipo_id == TipoCota::QUANTIDADE
|
|
? self::handleUnidadeQuantidadeUtilizada($unidade, $query) : 0,
|
|
'valor_utilizado' => $unidade->cota_tipo_id == TipoCota::VALOR
|
|
? self::handleUnidadeValorUtilizado($unidade, $query) : 0,
|
|
])
|
|
->values(),
|
|
];
|
|
}),
|
|
'cotas_laboratorio' => DB::table('exame_laboratorial_parametrizacao_cota')
|
|
->join('cadastro_infraestrutura', 'exame_laboratorial_parametrizacao_cota.infraestrutura_id', 'cadastro_infraestrutura.id')
|
|
->join('cadastro_unidade', 'cadastro_infraestrutura.unidade_id', 'cadastro_unidade.id')
|
|
->where('unidade_tipo_id', TipoUnidade::LABORATORIO)
|
|
->whereNull('exame_laboratorial_parametrizacao_cota.deleted_at')
|
|
->whereYear('exame_laboratorial_parametrizacao_cota.inicio', Carbon::now()->year)
|
|
->groupBy('cadastro_infraestrutura.id')
|
|
->select([
|
|
'cadastro_unidade.id as unidade_id',
|
|
'cadastro_infraestrutura.id as infraestrutura_id',
|
|
'cadastro_unidade.nome_fantasia AS nome_fantasia',
|
|
DB::raw('COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia) AS infraestrutura_nome'),
|
|
'exame_laboratorial_parametrizacao_cota.unidade_tipo_id AS unidade_tipo_id',
|
|
'exame_laboratorial_parametrizacao_cota.inicio',
|
|
DB::raw('YEAR(inicio) AS ano'),
|
|
])
|
|
->get()
|
|
->map(function ($query) {
|
|
return [
|
|
'unidade_id' => $query->unidade_id ?? null,
|
|
'nome_fantasia' => $query->nome_fantasia ?? null,
|
|
'unidade_tipo_id' => $query->unidade_tipo_id ?? null,
|
|
'infraestrutura_id' => $query->infraestrutura_id ?? null,
|
|
'infraestrutura_nome' => $query->infraestrutura_nome ?? null,
|
|
'quantidade_utilizada' => self::handleTotalQuantidadeUtilizada($query),
|
|
'valor_utilizado' => self::handleTotalValorUtilizado($query),
|
|
'valor_laboratorio' => self::handleTotalValor($query),
|
|
'quantidade_laboratorio' => self::handleTotalQuantidadeUtilizado($query),
|
|
'ano' => Carbon::now()->year,
|
|
'cota_laboratorio' => [],
|
|
'alterado' => false,
|
|
];
|
|
}),
|
|
'cotas_exame_periodo' => CotaExamePeriodo::groupBy('unidade_id', 'infraestrutura_id', 'cota_tipo_id', 'inicio')
|
|
->get()
|
|
->map(fn($query) => [
|
|
'id' => $query->id ?? null,
|
|
'cota_tipo_id' => $query->cota_tipo_id ?? null,
|
|
'cota_nome' => $query->cota_tipo_id == TipoCota::VALOR ? 'Valor' : 'Quantidade',
|
|
"periodo" => [
|
|
'start' => (new Carbon($query->inicio))->format('d/m/Y') ?? null,
|
|
'end' => (new Carbon($query->fim))->format('d/m/Y') ?? null,
|
|
],
|
|
'unidade_id' => $query->unidade_id ?? null,
|
|
'unidade' => $query->unidade->nome_fantasia ?? null,
|
|
'infraestrutura_id' => $query->infraestrutura_id ?? null,
|
|
'infraestrutura' => $query->infraestrutura->nome ?? null,
|
|
'alterado' => false,
|
|
'exames' => [],
|
|
])
|
|
->sortBy('nome')
|
|
->values(),
|
|
'mensagem_protocolo' => $mensagemProtocolo->mensagem,
|
|
'impressao_agendamento_tipo_id' => $impressaoAgendamento->tipo_id ?? EnumsTipoImpressaoAgendamento::PROTOCOLO_ETIQUETA,
|
|
'webservice_tipo_id' => $webservice->tipo_id ?? null,
|
|
'motivo' => Motivo::get()
|
|
->map(fn($query) => [
|
|
'id' => $query->id ?? null,
|
|
'descricao' => $query->descricao ?? null,
|
|
]),
|
|
'supervisores' => Supervisor::all()
|
|
->map(fn($supervisor) => [
|
|
'id' => $supervisor->id,
|
|
'usuario_id' => $supervisor->usuario_id,
|
|
'nome' => $supervisor->usuario()->withTrashed()->first()->cidadao->nome,
|
|
]),
|
|
'profissionais_assinatura' => ProfissionalAssinatura::all()
|
|
->map(fn($profissional) => [
|
|
'id' => $profissional->id,
|
|
'usuario_id' => $profissional->usuario_id,
|
|
'nome' => $profissional->usuario->cidadao->nome,
|
|
'usuario' => [
|
|
'id' => $profissional->usuario_id,
|
|
'cidadao_nome' => $profissional->usuario->nome,
|
|
],
|
|
'unidade_id' => $profissional->unidade_id,
|
|
'infraestrutura_id' => $profissional->infraestrutura_id,
|
|
'nome_fantasia' => $profissional->unidade->nome_fantasia,
|
|
'infraestrutura_nome' => $profissional->infraestrutura->nome,
|
|
]),
|
|
'cotas_transferencia' => CotaTransferencia::groupBy(
|
|
'laboratorio_unidade_id',
|
|
'laboratorio_infraestrutura_id',
|
|
'origem_unidade_id',
|
|
'origem_infraestrutura_id'
|
|
)
|
|
->get()
|
|
->map(function ($query) {
|
|
return [
|
|
'id' => $query->id ?? null,
|
|
'laboratorio_unidade_id' => $query->laboratorio_unidade_id ?? null,
|
|
'laboratorio_infraestrutura_id' => $query->laboratorio_infraestrutura_id ?? null,
|
|
'laboratorio_nome_fantasia' => $query->laboratorioUnidade->nome_fantasia ?? null,
|
|
'laboratorio_infraestrutura' => $query->laboratorioInfraestrutura->nome ?? null,
|
|
'origem_unidade_id' => $query->origem_unidade_id ?? null,
|
|
'origem_infraestrutura_id' => $query->origem_infraestrutura_id ?? null,
|
|
'origem_nome_fantasia' => $query->origemUnidade->nome_fantasia ?? null,
|
|
'origem_infraestrutura' => $query->origemInfraestrutura->nome ?? null,
|
|
'unidade_tipo_id' => $query->unidade_tipo_id ?? null,
|
|
'destino' => CotaTransferencia::where('laboratorio_unidade_id', $query->laboratorio_unidade_id)
|
|
->where('laboratorio_infraestrutura_id', $query->laboratorio_infraestrutura_id)
|
|
->where('origem_unidade_id', $query->origem_unidade_id)
|
|
->where('origem_infraestrutura_id', $query->origem_infraestrutura_id)
|
|
->get()
|
|
->map(fn($unidade) => [
|
|
'id' => $unidade->id ?? null,
|
|
'cota_id' => $unidade->cota_id ?? null,
|
|
'origem_unidade_id' => $query->origem_unidade_id ?? null,
|
|
'origem_infraestrutura_id' => $query->origem_infraestrutura_id ?? null,
|
|
'origem_nome_fantasia' => $query->origemUnidade->nome_fantasia ?? null,
|
|
'origem_infraestrutura' => $query->origemInfraestrutura->nome ?? null,
|
|
'origem_inicio' => (new Carbon($unidade->origem_inicio))->format('d/m/Y') ?? null,
|
|
'origem_fim' => (new Carbon($unidade->origem_fim))->format('d/m/Y') ?? null,
|
|
'origem_cota_quantidade' => $unidade->origem_cota_quantidade ?? null,
|
|
'origem_cota_valor' => number_format($unidade->origem_cota_valor, 2, ',', '.') ?? null,
|
|
'destino_unidade_id' => $unidade->destino_unidade_id ?? null,
|
|
'destino_infraestrutura_id' => $unidade->destino_infraestrutura_id ?? null,
|
|
'destino_nome_fantasia' => $unidade->destinoUnidade->nome_fantasia ?? null,
|
|
'destino_infraestrutura_nome' => $unidade->destinoInfraestrutura->nome ?? null,
|
|
'destino_inicio' => (new Carbon($unidade->destino_inicio))->format('d/m/Y') ?? null,
|
|
'destino_fim' => (new Carbon($unidade->destino_fim))->format('d/m/Y') ?? null,
|
|
'destino_cota_quantidade' => $unidade->destino_cota_quantidade ?? null,
|
|
'destino_cota_valor' => number_format($unidade->destino_cota_valor, 2, ',', '.') ?? null,
|
|
'origem_cota_tipo_id' => $unidade->cota_tipo_id ?? null,
|
|
'origem_cota_tipo_nome' => $unidade->cota_tipo_id == TipoCota::VALOR
|
|
? 'Valor' : 'Quantidade',
|
|
'exames' => CotaTransferenciaExames::where('cota_transferencia_id', $unidade->id)
|
|
->get()
|
|
->map(fn($exame) => [
|
|
'id' => $exame->id ?? null,
|
|
'exame_id' => $exame->cotaExame->exame_id ?? null,
|
|
'cota_exame_periodo_id' => $exame->cota_exame_id ?? null,
|
|
'nome' => $exame->cotaExame->exame->nome ?? null,
|
|
'cota_quantidade' => $exame->cota_quantidade ?? null,
|
|
'cota_valor' => number_format($exame->cota_valor, 2, ',', '.') ?? null,
|
|
'cota_quantidade_transferido' => $exame->cota_quantidade_transferido ?? null,
|
|
'cota_valor_transferido' => number_format($exame->cota_valor_transferido, 2, ',', '.') ?? null,
|
|
'transferido' => $unidade->cota_tipo_id == TipoCota::VALOR
|
|
? number_format($exame->cota_valor_transferido, 2, ',', '.')
|
|
: $exame->cota_quantidade_transferido,
|
|
])
|
|
])
|
|
->values(),
|
|
];
|
|
}),
|
|
'exibir_reagendar_exame' => $reagendarExame ? $reagendarExame->exibir : false,
|
|
'exibir_exame_ambulatorio_atendimento' => $exameAmbulatorioAtendimento ? $exameAmbulatorioAtendimento->exibir : false,
|
|
'integracao_aplicativo' => [
|
|
'habilita_agendamento_conectasus_cidadao' =>
|
|
$integracaoAplicativo->habilita_agendamento_conectasus_cidadao ?? false,
|
|
'habilita_visualizacao_laudo_conectasus_cidadao' =>
|
|
$integracaoAplicativo->habilita_visualizacao_laudo_conectasus_cidadao ?? false,
|
|
'laboratorios' => self::getLaboratoriosAppCidadao(),
|
|
'exames' => self::getExamesAppCidadao(),
|
|
'unidades_solicitantes' => self::getUnidadesSolicitantesAppCidadao(),
|
|
],
|
|
],
|
|
];
|
|
}
|
|
|
|
public static function getListagemCotaLaboratorio($request)
|
|
{
|
|
|
|
return DB::table('exame_laboratorial_parametrizacao_cota')
|
|
->join('cadastro_infraestrutura', 'exame_laboratorial_parametrizacao_cota.infraestrutura_id', 'cadastro_infraestrutura.id')
|
|
->join('cadastro_unidade', 'cadastro_infraestrutura.unidade_id', 'cadastro_unidade.id')
|
|
->where('unidade_tipo_id', TipoUnidade::LABORATORIO)
|
|
->whereNull('exame_laboratorial_parametrizacao_cota.deleted_at')
|
|
->when($request->mes, function ($agendada) use ($request) {
|
|
$agendada->whereMonth('exame_laboratorial_parametrizacao_cota.inicio', $request->mes);
|
|
})
|
|
->whereYear('exame_laboratorial_parametrizacao_cota.inicio', $request->ano)
|
|
->groupBy('cadastro_infraestrutura.id')
|
|
->select([
|
|
'cadastro_unidade.id as unidade_id',
|
|
'cadastro_infraestrutura.id as infraestrutura_id',
|
|
'cadastro_unidade.nome_fantasia AS nome_fantasia',
|
|
DB::raw('COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia) AS infraestrutura_nome'),
|
|
'exame_laboratorial_parametrizacao_cota.unidade_tipo_id AS unidade_tipo_id',
|
|
'exame_laboratorial_parametrizacao_cota.inicio',
|
|
DB::raw('YEAR(inicio) AS ano'),
|
|
])
|
|
->get()
|
|
->map(function ($query) {
|
|
return [
|
|
'unidade_id' => $query->unidade_id ?? null,
|
|
'nome_fantasia' => $query->nome_fantasia ?? null,
|
|
'unidade_tipo_id' => $query->unidade_tipo_id ?? null,
|
|
'infraestrutura_id' => $query->infraestrutura_id ?? null,
|
|
'infraestrutura_nome' => $query->infraestrutura_nome ?? null,
|
|
'quantidade_utilizada' => self::handleTotalQuantidadeUtilizada($query),
|
|
'valor_utilizado' => self::handleTotalValorUtilizado($query),
|
|
'valor_laboratorio' => self::handleTotalValor($query),
|
|
'quantidade_laboratorio' => self::handleTotalQuantidadeUtilizado($query),
|
|
'ano' => $query->ano,
|
|
'cota_laboratorio' => [],
|
|
'alterado' => false,
|
|
];
|
|
});
|
|
}
|
|
|
|
public static function getCotaExame($request)
|
|
{
|
|
|
|
return CotaExamePeriodo::where('infraestrutura_id', $request->infraestrutura_id)
|
|
->where('cota_tipo_id', $request->cota_tipo_id)
|
|
->where('inicio', $request->inicio)
|
|
->orderBy('inicio')
|
|
->get()
|
|
->map(fn($exame) => [
|
|
'id' => $exame->id ?? null,
|
|
'cota_tipo_id' => $request->cota_tipo_id ?? null,
|
|
'cota_nome' => $request->cota_tipo_id == TipoCota::VALOR ? 'Valor' : 'Quantidade',
|
|
"periodo" => [
|
|
'start' => (new Carbon($request->inicio))->format('Y-m-d') ?? null,
|
|
'end' => (new Carbon($request->fim))->format('Y-m-d') ?? null,
|
|
],
|
|
'unidade_id' => $exame->unidade_id ?? null,
|
|
'infraestrutura_id' => $exame->infraestrutura_id ?? null,
|
|
'exame_id' => $exame->exame_id ?? null,
|
|
'exame_nome' => $exame->exame->nome ?? null,
|
|
'cota' => $exame->cota_tipo_id == TipoCota::VALOR
|
|
? number_format($exame->cota_valor, 2, ',', '.')
|
|
: $exame->cota_quantidade,
|
|
'cota_quantidade' => $exame->cota_quantidade ?? null,
|
|
'cota_valor' => number_format($exame->cota_valor, 2, ',', '.') ?? null,
|
|
'cota_tipo_id' => $exame->cota_tipo_id ?? null,
|
|
'cota_nome' => $exame->cota_tipo_id == TipoCota::VALOR ? 'Valor' : 'Quantidade',
|
|
'observacao' => $exame->observacao ?? null,
|
|
'valor_utilizado' => $exame->cota_tipo_id == TipoCota::VALOR
|
|
? self::handleExameValorUtilizado($request, $exame) : 0,
|
|
'quantidade_utilizada' => $exame->cota_tipo_id == TipoCota::QUANTIDADE ?
|
|
self::handleExameQuantidadeUtilizada($request, $exame) : 0,
|
|
])
|
|
->values();
|
|
}
|
|
|
|
public static function getCotaLaboratorio($request)
|
|
{
|
|
|
|
return
|
|
DB::table('exame_laboratorial_parametrizacao_cota')
|
|
->where('unidade_tipo_id', TipoUnidade::LABORATORIO)
|
|
->where('infraestrutura_id', $request->infraestrutura_id)
|
|
->whereNull('deleted_at')
|
|
->whereYear('inicio', $request->ano)
|
|
->select([
|
|
'id as id',
|
|
'infraestrutura_id',
|
|
'unidade_id',
|
|
'unidade_tipo_id',
|
|
'inicio',
|
|
'fim',
|
|
'cota_tipo_id',
|
|
'cota_quantidade',
|
|
'cota_valor',
|
|
'observacao',
|
|
])
|
|
->get()
|
|
->map(fn($unidade) => [
|
|
'id' => $unidade->id ?? null,
|
|
'infraestrutura_id' => $unidade->infraestrutura_id ?? null,
|
|
'unidade_id' => $unidade->unidade_id ?? null,
|
|
'unidade_tipo_id' => $unidade->unidade_tipo_id ?? null,
|
|
'cota_tipo_id' => $unidade->cota_tipo_id ?? null,
|
|
'cota_tipo_nome' => $unidade->cota_tipo_id == TipoCota::VALOR
|
|
? 'Valor' : 'Quantidade',
|
|
"periodo" => [
|
|
'start' => (new Carbon($unidade->inicio))->format('d/m/Y') ?? null,
|
|
'end' => (new Carbon($unidade->fim))->format('d/m/Y') ?? null,
|
|
],
|
|
'cota' => $unidade->cota_tipo_id == TipoCota::VALOR
|
|
? number_format($unidade->cota_valor, 2, ',', '.')
|
|
: $unidade->cota_quantidade,
|
|
'saldo' => $unidade->cota_tipo_id == TipoCota::VALOR
|
|
? number_format($unidade->cota_valor, 2, ',', '.')
|
|
: $unidade->cota_quantidade,
|
|
'utilizada' => "0",
|
|
'quantidade_utilizada' => $unidade->cota_tipo_id == TipoCota::QUANTIDADE
|
|
? self::handleQuantidadeUtilizada($unidade, $request) : 0,
|
|
'valor_utilizado' => $unidade->cota_tipo_id == TipoCota::VALOR
|
|
? self::handleValorUtilizado($unidade, $request) : 0,
|
|
'cota_quantidade' => $unidade->cota_quantidade ?? null,
|
|
'cota_valor' => number_format($unidade->cota_valor, 2, ',', '.') ?? null,
|
|
'observacao' => $unidade->observacao ?? null,
|
|
'cota_atribuida' => self::getCotaUnidadeAtribuida($unidade) ?? 0,
|
|
'exames' => [],
|
|
'unidade_solicitante' => [],
|
|
'unidade_alterada' => false,
|
|
'exame_alterado' => false,
|
|
])
|
|
->sortBy(fn($cota) => $cota['periodo']['start'])
|
|
->sortBy('unidade_solicitante')
|
|
->values();
|
|
}
|
|
|
|
public static function getCotaLaboratorioTransferencia($request)
|
|
{
|
|
|
|
return
|
|
DB::table('exame_laboratorial_parametrizacao_cota')
|
|
->where('unidade_tipo_id', TipoUnidade::LABORATORIO)
|
|
->where('infraestrutura_id', $request->infraestrutura_id)
|
|
->whereNull('deleted_at')
|
|
->select([
|
|
'id as id',
|
|
'id as cota_id',
|
|
'infraestrutura_id',
|
|
'unidade_id',
|
|
'unidade_tipo_id',
|
|
'inicio',
|
|
'fim',
|
|
'cota_tipo_id',
|
|
'cota_quantidade',
|
|
'cota_valor',
|
|
'observacao',
|
|
])
|
|
->get()
|
|
->map(fn($unidade) => [
|
|
'id' => $unidade->id ?? null,
|
|
'infraestrutura_id' => $unidade->infraestrutura_id ?? null,
|
|
'unidade_id' => $unidade->unidade_id ?? null,
|
|
'unidade_tipo_id' => $unidade->unidade_tipo_id ?? null,
|
|
'cota_tipo_id' => $unidade->cota_tipo_id ?? null,
|
|
'cota_tipo_nome' => $unidade->cota_tipo_id == TipoCota::VALOR
|
|
? 'Valor' : 'Quantidade',
|
|
"periodo" => [
|
|
'start' => (new Carbon($unidade->inicio))->format('d/m/Y') ?? null,
|
|
'end' => (new Carbon($unidade->fim))->format('d/m/Y') ?? null,
|
|
],
|
|
'cota' => $unidade->cota_tipo_id == TipoCota::VALOR
|
|
? number_format($unidade->cota_valor, 2, ',', '.')
|
|
: $unidade->cota_quantidade,
|
|
'saldo' => $unidade->cota_tipo_id == TipoCota::VALOR
|
|
? number_format($unidade->cota_valor, 2, ',', '.')
|
|
: $unidade->cota_quantidade,
|
|
'utilizada' => "0",
|
|
'quantidade_utilizada' => $unidade->cota_tipo_id == TipoCota::QUANTIDADE
|
|
? self::handleQuantidadeUtilizada($unidade, $request) : 0,
|
|
'valor_utilizado' => $unidade->cota_tipo_id == TipoCota::VALOR
|
|
? self::handleValorUtilizado($unidade, $request) : 0,
|
|
'cota_quantidade' => $unidade->cota_quantidade ?? null,
|
|
'cota_valor' => number_format($unidade->cota_valor, 2, ',', '.') ?? null,
|
|
'observacao' => $unidade->observacao ?? null,
|
|
'cota_atribuida' => self::getCotaUnidadeAtribuida($unidade) ?? 0,
|
|
'exames' => self::getCotaLaboratorioExameTransferencia($unidade),
|
|
'unidade_solicitante' => self::getCotaLaboratorioUnidade($unidade),
|
|
'unidade_alterada' => false,
|
|
'exame_alterado' => false,
|
|
])
|
|
->sortBy(fn($cota) => $cota['periodo']['start'])
|
|
->sortBy('unidade_solicitante')
|
|
->values();
|
|
}
|
|
|
|
public static function getCotaLaboratorioUnidade($request)
|
|
{
|
|
|
|
return DB::table('exame_laboratorial_parametrizacao_cota_unidade_solicitante')
|
|
->where('cota_id', $request->cota_id)
|
|
->whereNull('exame_laboratorial_parametrizacao_cota_unidade_solicitante.deleted_at')
|
|
->join('cadastro_infraestrutura', 'exame_laboratorial_parametrizacao_cota_unidade_solicitante.infraestrutura_solicitante_id', 'cadastro_infraestrutura.id')
|
|
->join('cadastro_unidade', 'cadastro_infraestrutura.unidade_id', 'cadastro_unidade.id')
|
|
->select([
|
|
'cadastro_unidade.nome_fantasia AS nome_fantasia',
|
|
DB::raw('COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia) AS infraestrutura_nome'),
|
|
'exame_laboratorial_parametrizacao_cota_unidade_solicitante.id',
|
|
'exame_laboratorial_parametrizacao_cota_unidade_solicitante.cota_id',
|
|
'exame_laboratorial_parametrizacao_cota_unidade_solicitante.cota_id_unidade_solicitante',
|
|
'exame_laboratorial_parametrizacao_cota_unidade_solicitante.unidade_solicitante_id',
|
|
'exame_laboratorial_parametrizacao_cota_unidade_solicitante.infraestrutura_solicitante_id',
|
|
'exame_laboratorial_parametrizacao_cota_unidade_solicitante.cota_quantidade',
|
|
'exame_laboratorial_parametrizacao_cota_unidade_solicitante.cota_valor'
|
|
])
|
|
->get()
|
|
->map(fn($unidadeSolicitante) => [
|
|
'id' => $unidadeSolicitante->id,
|
|
'cota_id' => $unidadeSolicitante->cota_id,
|
|
'cota_id_unidade_solicitante' =>
|
|
$unidadeSolicitante->cota_id_unidade_solicitante,
|
|
'unidade_solicitante_id' => $unidadeSolicitante->unidade_solicitante_id
|
|
?? null,
|
|
'unidade_solicitante' =>
|
|
$unidadeSolicitante->nome_fantasia ?? null,
|
|
'infraestrutura_solicitante_id' =>
|
|
$unidadeSolicitante->infraestrutura_solicitante_id ?? null,
|
|
'infraestrutura_nome' =>
|
|
$unidadeSolicitante->infraestrutura_nome ?? null,
|
|
'cota_quantidade' => $unidadeSolicitante->cota_quantidade ?? null,
|
|
'cota_valor' => number_format($unidadeSolicitante->cota_valor, 2, ',', '.')
|
|
?? null,
|
|
'cota' => $request->cota_tipo_id == TipoCota::VALOR
|
|
? number_format($unidadeSolicitante->cota_valor, 2, ',', '.')
|
|
: $unidadeSolicitante->cota_quantidade,
|
|
'saldo' => $request->cota_tipo_id == TipoCota::VALOR
|
|
? number_format($unidadeSolicitante->cota_valor, 2, ',', '.')
|
|
: $unidadeSolicitante->cota_quantidade,
|
|
'quantidade_utilizada' => $request->cota_tipo_id == TipoCota::QUANTIDADE
|
|
? self::handleQuantidadeSolicitanteUtilizada($request, $unidadeSolicitante)
|
|
: 0,
|
|
'valor_utilizado' => $request->cota_tipo_id == TipoCota::VALOR
|
|
? self::handleValorSolicitanteUtilizada($request, $unidadeSolicitante)
|
|
: 0,
|
|
]);
|
|
}
|
|
|
|
public static function getCotaLaboratorioExameTransferencia($request)
|
|
{
|
|
|
|
return DB::table('exame_laboratorial_parametrizacao_cota_exame')
|
|
->join('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_parametrizacao_cota_exame.exame_id')
|
|
->where('exame_laboratorial_parametrizacao_cota_exame.cota_id', $request->cota_id)
|
|
->whereNull('exame_laboratorial_parametrizacao_cota_exame.deleted_at')
|
|
->select([
|
|
'exame_laboratorial_parametrizacao_cota_exame.id',
|
|
'exame_laboratorial_parametrizacao_cota_exame.cota_id',
|
|
'exame_laboratorial_parametrizacao_cota_exame.cota_quantidade',
|
|
'exame_laboratorial_parametrizacao_cota_exame.cota_valor',
|
|
'exame_laboratorial_parametrizacao_cota_exame.exame_id',
|
|
'exame_laboratorial_parametrizacao_cota_exame.cota_exame_periodo_id',
|
|
'parametrizacao_exame.nome'
|
|
])
|
|
->get()
|
|
->map(fn($exame) => [
|
|
'id' => $exame->id ?? null,
|
|
'exame_id' => $exame->exame_id ?? null,
|
|
'cota_exame_periodo_id' => $exame->cota_exame_periodo_id ?? null,
|
|
'nome' => $exame->nome ?? null,
|
|
'transferido' => null,
|
|
'cota_quantidade_transferido' => null,
|
|
'cota_valor_transferido' => null,
|
|
'cota_quantidade' => $exame->cota_quantidade ?? null,
|
|
'cota_valor' => number_format($exame->cota_valor, 2, ',', '.') ?? null,
|
|
'cota' => $request->cota_tipo_id == TipoCota::VALOR
|
|
? number_format($exame->cota_valor, 2, ',', '.')
|
|
: $exame->cota_quantidade,
|
|
'valor_utilizado' => $request->cota_tipo_id == TipoCota::VALOR
|
|
? self::handleExameValorUtilizado($request, $exame) : 0,
|
|
'quantidade_utilizada' => $request->cota_tipo_id == TipoCota::QUANTIDADE
|
|
? self::handleExameQuantidadeUtilizada($request, $exame) : 0,
|
|
'unidade_solicitante' => DB::table('exame_laboratorial_parametrizacao_cota_exame_unidade')
|
|
->where('cota_exame_id', $exame->id)
|
|
->get()
|
|
->map(fn($unidadeSolicitante) => [
|
|
'id' => $unidadeSolicitante->id,
|
|
'cota_id' => $unidadeSolicitante->cota_id,
|
|
'cota_exame_id' => $unidadeSolicitante->cota_exame_id ?? null,
|
|
'unidade_solicitante_id' =>
|
|
$unidadeSolicitante->unidade_solicitante_id ?? null,
|
|
'unidade_solicitante' =>
|
|
$unidadeSolicitante->unidadeSolicitante->nome_fantasia ?? null,
|
|
'infraestrutura_solicitante_id' =>
|
|
$unidadeSolicitante->infraestrutura_solicitante_id ?? null,
|
|
'infraestrutura_nome' =>
|
|
$unidadeSolicitante->infraestruturaSolicitante->nome ?? null,
|
|
'cota_quantidade' => $unidadeSolicitante->cota_quantidade ?? null,
|
|
'cota_valor' =>
|
|
number_format($unidadeSolicitante->cota_valor, 2, ',', '.')
|
|
?? null,
|
|
'cota' => $request->cota_tipo_id == TipoCota::VALOR
|
|
? number_format($unidadeSolicitante->cota_valor, 2, ',', '.')
|
|
: $unidadeSolicitante->cota_quantidade,
|
|
'valor_utilizado' => $request->cota_tipo_id == TipoCota::VALOR
|
|
? self::handleExameUnidadeSolicitanteValorUtilizado($request, $exame, $unidadeSolicitante)
|
|
: 0,
|
|
'quantidade_utilizada' => $request->cota_tipo_id == TipoCota::QUANTIDADE
|
|
? self::handleExameUnidadeSolicitanteQuantidadeUtilizada($request, $exame, $unidadeSolicitante)
|
|
: 0
|
|
]),
|
|
]);
|
|
}
|
|
|
|
public static function getCotaLaboratorioExame($request)
|
|
{
|
|
|
|
return DB::table('exame_laboratorial_parametrizacao_cota_exame')
|
|
->join('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_parametrizacao_cota_exame.exame_id')
|
|
->where('exame_laboratorial_parametrizacao_cota_exame.cota_id', $request->cota_id)
|
|
->whereNull('exame_laboratorial_parametrizacao_cota_exame.deleted_at')
|
|
->select([
|
|
'exame_laboratorial_parametrizacao_cota_exame.id',
|
|
'exame_laboratorial_parametrizacao_cota_exame.cota_id',
|
|
'exame_laboratorial_parametrizacao_cota_exame.cota_quantidade',
|
|
'exame_laboratorial_parametrizacao_cota_exame.cota_valor',
|
|
'exame_laboratorial_parametrizacao_cota_exame.exame_id',
|
|
'exame_laboratorial_parametrizacao_cota_exame.cota_exame_periodo_id',
|
|
'parametrizacao_exame.nome'
|
|
])
|
|
->get()
|
|
->map(fn($exame) => [
|
|
'id' => $exame->id ?? null,
|
|
'exame_id' => $exame->exame_id ?? null,
|
|
'cota_exame_periodo_id' => $exame->cota_exame_periodo_id ?? null,
|
|
'nome' => $exame->nome ?? null,
|
|
'transferido' => null,
|
|
'cota_quantidade_transferido' => null,
|
|
'cota_valor_transferido' => null,
|
|
'cota_quantidade' => $exame->cota_quantidade ?? null,
|
|
'cota_valor' => number_format($exame->cota_valor, 2, ',', '.') ?? null,
|
|
'cota' => $request->cota_tipo_id == TipoCota::VALOR
|
|
? number_format($exame->cota_valor, 2, ',', '.')
|
|
: $exame->cota_quantidade,
|
|
'valor_utilizado' => $request->cota_tipo_id == TipoCota::VALOR
|
|
? self::handleExameValorUtilizado($request, $exame) : 0,
|
|
'quantidade_utilizada' => $request->cota_tipo_id == TipoCota::QUANTIDADE
|
|
? self::handleExameQuantidadeUtilizada($request, $exame) : 0,
|
|
'unidade_solicitante' => $request->unidades_solicitantes ? self::getCotaLaboratorioExameUnidade($exame->id) : [],
|
|
'unidade_solicitante_alterada' => false,
|
|
]);
|
|
}
|
|
|
|
public static function getCotaLaboratorioExameUnidade($cota_exame_id = null)
|
|
{
|
|
|
|
return DB::table('exame_laboratorial_parametrizacao_cota_exame_unidade')
|
|
->join('cadastro_unidade', 'cadastro_unidade.id', 'exame_laboratorial_parametrizacao_cota_exame_unidade.unidade_solicitante_id')
|
|
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'exame_laboratorial_parametrizacao_cota_exame_unidade.infraestrutura_solicitante_id')
|
|
->where('cota_exame_id', $cota_exame_id ?? request()->cota_exame_id)
|
|
->whereNull('exame_laboratorial_parametrizacao_cota_exame_unidade.deleted_at')
|
|
->get()
|
|
->map(fn($unidadeSolicitante) => [
|
|
'id' => $unidadeSolicitante->id,
|
|
'cota_id' => $unidadeSolicitante->cota_id,
|
|
'cota_exame_id' => $unidadeSolicitante->cota_exame_id ?? null,
|
|
'unidade_solicitante_id' =>
|
|
$unidadeSolicitante->unidade_solicitante_id ?? null,
|
|
'unidade_solicitante' =>
|
|
$unidadeSolicitante->nome_fantasia ?? null,
|
|
'infraestrutura_solicitante_id' =>
|
|
$unidadeSolicitante->infraestrutura_solicitante_id ?? null,
|
|
'infraestrutura_nome' =>
|
|
$unidadeSolicitante->nome ?? $unidadeSolicitante->nome_fantasia,
|
|
'cota_quantidade' => $unidadeSolicitante->cota_quantidade ?? null,
|
|
'cota_valor' =>
|
|
number_format($unidadeSolicitante->cota_valor, 2, ',', '.')
|
|
?? null,
|
|
'cota' => request()->cota_tipo_id == TipoCota::VALOR
|
|
? number_format($unidadeSolicitante->cota_valor, 2, ',', '.')
|
|
: $unidadeSolicitante->cota_quantidade,
|
|
'valor_utilizado' => request()->cota_tipo_id == TipoCota::VALOR
|
|
? self::handleExameUnidadeSolicitanteValorUtilizado(request(), request()->exame_id, $unidadeSolicitante)
|
|
: 0,
|
|
'quantidade_utilizada' => request()->cota_tipo_id == TipoCota::QUANTIDADE
|
|
? self::handleExameUnidadeSolicitanteQuantidadeUtilizada(request(), request()->exame_id, $unidadeSolicitante)
|
|
: 0
|
|
]);
|
|
}
|
|
|
|
private static function getCotaUnidadeAtribuida($unidade)
|
|
{
|
|
|
|
$cota = DB::table('exame_laboratorial_parametrizacao_cota_unidade_solicitante')
|
|
->whereNull('deleted_at')
|
|
->where('cota_id', $unidade->id)
|
|
->selectRaw("
|
|
CASE
|
|
WHEN ? = ? THEN FORMAT(SUM(cota_valor), 2, 'pt_BR')
|
|
ELSE SUM(cota_quantidade)
|
|
END AS total_cota
|
|
", [$unidade->cota_tipo_id, TipoCota::VALOR])
|
|
->value('total_cota');
|
|
|
|
if ($unidade->cota_tipo_id == TipoCota::VALOR) {
|
|
|
|
$cota = str_replace(',', '.', str_replace('.', '', $cota));
|
|
$total = floatval($cota);
|
|
|
|
return number_format($total, 2, ',', '.');
|
|
} else {
|
|
|
|
return $cota;
|
|
}
|
|
|
|
if ($cota == 0) {
|
|
|
|
self::getCotaExameAtribuida($unidade);
|
|
}
|
|
}
|
|
|
|
private static function getCotaExameAtribuida($unidade)
|
|
{
|
|
|
|
$cota = DB::table('exame_laboratorial_parametrizacao_cota_exame')
|
|
->whereNull('deleted_at')
|
|
->where('cota_id', $unidade->id)
|
|
->selectRaw("
|
|
CASE
|
|
WHEN ? = ? THEN FORMAT(SUM(cota_valor), 2, 'pt_BR')
|
|
ELSE SUM(cota_quantidade)
|
|
END AS total_cota
|
|
", [$unidade->cota_tipo_id, TipoCota::VALOR])
|
|
->value('total_cota');
|
|
|
|
if ($unidade->cota_tipo_id == TipoCota::VALOR) {
|
|
|
|
$cota = str_replace(',', '.', str_replace('.', '', $cota));
|
|
$total = floatval($cota);
|
|
|
|
return number_format($total, 2, ',', '.');
|
|
} else {
|
|
|
|
return $cota;
|
|
}
|
|
}
|
|
|
|
private static function handleTotalQuantidadeUtilizada($query)
|
|
{
|
|
|
|
|
|
$exameLaboratorial = DB::table('exame_laboratorial_agendamento_exame')
|
|
->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
|
->whereYear('exame_laboratorial_agendamento.data', $query->ano)
|
|
->when(request()->mes, function ($agendada) {
|
|
$agendada->whereMonth('exame_laboratorial_agendamento.data', request()->mes);
|
|
})
|
|
->whereExists(function ($agendada) use ($query) {
|
|
|
|
if ($query->infraestrutura_id) {
|
|
|
|
$agendada->where(
|
|
'exame_laboratorial_agendamento.laboratorio_infraestrutura_id',
|
|
$query->infraestrutura_id
|
|
)
|
|
->whereExists(function ($q) use ($query) {
|
|
$q->select(DB::raw(1))
|
|
->from('exame_laboratorial_parametrizacao_cota')
|
|
->where('exame_laboratorial_parametrizacao_cota.infraestrutura_id', $query->infraestrutura_id)
|
|
->where('exame_laboratorial_parametrizacao_cota.unidade_tipo_id', TipoUnidade::LABORATORIO)
|
|
->where('exame_laboratorial_parametrizacao_cota.cota_tipo_id', TipoCota::QUANTIDADE)
|
|
->whereColumn('exame_laboratorial_agendamento.data', '>=', 'exame_laboratorial_parametrizacao_cota.inicio')
|
|
->whereColumn('exame_laboratorial_agendamento.data', '<=', 'exame_laboratorial_parametrizacao_cota.fim');
|
|
});
|
|
}
|
|
})
|
|
->where(
|
|
fn($q) =>
|
|
$q->whereIn(
|
|
'status_id',
|
|
[
|
|
StatusAgendamento::AGENDADO,
|
|
StatusAgendamento::REALIZADO,
|
|
StatusAgendamento::PENDENTE_ASSINATURA,
|
|
StatusAgendamento::ASSINADO,
|
|
StatusAgendamento::PENDENTE_ENTREGA,
|
|
StatusAgendamento::ENTREGUE,
|
|
StatusAgendamento::NOVA_COLETA
|
|
]
|
|
)
|
|
->orWhereNull('status_id')
|
|
)
|
|
->count('exame_laboratorial_agendamento_exame.id');
|
|
|
|
|
|
$prontoAtendimento = DB::table('pronto_atendimento_pedido_exame_item')
|
|
->join('pronto_atendimento_pedido_exame', 'pronto_atendimento_pedido_exame.id', '=', 'pronto_atendimento_pedido_exame_item.pedido_exame_id')
|
|
->join('cadastro_lotacao', 'cadastro_lotacao.id', '=', 'pronto_atendimento_pedido_exame.laboratorio_lotacao_id')
|
|
->join('parametrizacao_exame', 'parametrizacao_exame.id', 'pronto_atendimento_pedido_exame_item.exame_id')
|
|
->whereYear('pronto_atendimento_pedido_exame_item.created_at', $query->ano)
|
|
->when(request()->mes, function ($agendada) {
|
|
$agendada->whereMonth('pronto_atendimento_pedido_exame_item.created_at', request()->mes);
|
|
})
|
|
->where('parametrizacao_exame.tipo_id', Tipo::LABORATORIAL)
|
|
->where('pronto_atendimento_pedido_exame_item.status_id', Status::REALIZADO)
|
|
->whereNull('pronto_atendimento_pedido_exame_item.deleted_at')
|
|
->where('cadastro_lotacao.infraestrutura_id', $query->infraestrutura_id)
|
|
->whereExists(function ($q) use ($query) {
|
|
$q->select(DB::raw(1))
|
|
->from('exame_laboratorial_parametrizacao_cota')
|
|
->where('exame_laboratorial_parametrizacao_cota.infraestrutura_id', $query->infraestrutura_id)
|
|
->where('exame_laboratorial_parametrizacao_cota.unidade_tipo_id', TipoUnidade::LABORATORIO)
|
|
->where('exame_laboratorial_parametrizacao_cota.cota_tipo_id', TipoCota::QUANTIDADE)
|
|
->whereColumn('pronto_atendimento_pedido_exame_item.created_at', '>=', 'exame_laboratorial_parametrizacao_cota.inicio')
|
|
->whereColumn('pronto_atendimento_pedido_exame_item.created_at', '<=', 'exame_laboratorial_parametrizacao_cota.fim');
|
|
})
|
|
->count('pronto_atendimento_pedido_exame_item.id');
|
|
|
|
return $exameLaboratorial + $prontoAtendimento;
|
|
}
|
|
|
|
private static function handleQuantidadeUtilizada($unidade, $query)
|
|
{
|
|
|
|
$exameLaboratorial = DB::table('exame_laboratorial_agendamento_exame')
|
|
->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
|
->where(
|
|
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%Y-%m-%d")'),
|
|
'>=',
|
|
Carbon::createFromFormat('Y-m-d', $unidade->inicio)
|
|
->format('Y-m-d')
|
|
)
|
|
->where(
|
|
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%Y-%m-%d")'),
|
|
'<=',
|
|
Carbon::createFromFormat('Y-m-d', $unidade->fim)
|
|
->format('Y-m-d')
|
|
)
|
|
->when($query->infraestrutura_id, function ($agendada) use ($query) {
|
|
$agendada->where(
|
|
'exame_laboratorial_agendamento.laboratorio_infraestrutura_id',
|
|
$query->infraestrutura_id
|
|
);
|
|
})
|
|
->where(
|
|
fn($q) =>
|
|
$q->whereIn(
|
|
'status_id',
|
|
[
|
|
StatusAgendamento::AGENDADO,
|
|
StatusAgendamento::REALIZADO,
|
|
StatusAgendamento::PENDENTE_ASSINATURA,
|
|
StatusAgendamento::ASSINADO,
|
|
StatusAgendamento::PENDENTE_ENTREGA,
|
|
StatusAgendamento::ENTREGUE,
|
|
StatusAgendamento::NOVA_COLETA
|
|
]
|
|
)
|
|
->orWhereNull('status_id')
|
|
)
|
|
->count('exame_laboratorial_agendamento_exame.id');
|
|
|
|
$prontoAtendimento = DB::table('pronto_atendimento_pedido_exame_item')
|
|
->join('pronto_atendimento_pedido_exame', 'pronto_atendimento_pedido_exame.id', '=', 'pronto_atendimento_pedido_exame_item.pedido_exame_id')
|
|
->join('cadastro_lotacao', 'cadastro_lotacao.id', '=', 'pronto_atendimento_pedido_exame.laboratorio_lotacao_id')
|
|
->join('parametrizacao_exame', 'parametrizacao_exame.id', 'pronto_atendimento_pedido_exame_item.exame_id')
|
|
->where('parametrizacao_exame.tipo_id', Tipo::LABORATORIAL)
|
|
->where('pronto_atendimento_pedido_exame_item.status_id', Status::REALIZADO)
|
|
->where('cadastro_lotacao.infraestrutura_id', $query->infraestrutura_id)
|
|
->where(function ($query) use ($unidade) {
|
|
$query
|
|
->where(
|
|
DB::raw('DATE_FORMAT(pronto_atendimento_pedido_exame_item.created_at, "%Y-%m-%d")'),
|
|
'>=',
|
|
Carbon::createFromFormat('Y-m-d', $unidade->inicio)
|
|
->format('Y-m-d')
|
|
)
|
|
->where(
|
|
DB::raw('DATE_FORMAT(pronto_atendimento_pedido_exame_item.created_at, "%Y-%m-%d")'),
|
|
'<=',
|
|
Carbon::createFromFormat('Y-m-d', $unidade->fim)
|
|
->format('Y-m-d')
|
|
);
|
|
})
|
|
->count('pronto_atendimento_pedido_exame_item.id');
|
|
|
|
return $exameLaboratorial + $prontoAtendimento;
|
|
}
|
|
|
|
private static function handleUnidadeQuantidadeUtilizada($unidade, $query)
|
|
{
|
|
|
|
$exameLaboratorial = DB::table('exame_laboratorial_agendamento')
|
|
->where(function ($agendada) use ($unidade, $query) {
|
|
|
|
$agendada
|
|
->where(
|
|
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%Y-%m-%d")'),
|
|
'>=',
|
|
Carbon::createFromFormat('Y-m-d', $unidade->inicio)
|
|
->format('Y-m-d')
|
|
)
|
|
->where(
|
|
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%Y-%m-%d")'),
|
|
'<=',
|
|
Carbon::createFromFormat('Y-m-d', $unidade->fim)
|
|
->format('Y-m-d')
|
|
);
|
|
|
|
|
|
if ($query->infraestrutura_id) {
|
|
|
|
$agendada->where(
|
|
'exame_laboratorial_agendamento.infraestrutura_id',
|
|
$query->infraestrutura_id
|
|
);
|
|
}
|
|
})
|
|
->whereExists(function ($query) {
|
|
|
|
$query
|
|
->select(DB::raw(1))
|
|
->from('exame_laboratorial_agendamento_exame')
|
|
->whereColumn('exame_laboratorial_agendamento_exame.agendamento_id', 'exame_laboratorial_agendamento.id')
|
|
->whereIn(
|
|
'status_id',
|
|
[
|
|
StatusAgendamento::AGENDADO,
|
|
StatusAgendamento::REALIZADO,
|
|
StatusAgendamento::PENDENTE_ASSINATURA,
|
|
StatusAgendamento::ASSINADO,
|
|
StatusAgendamento::PENDENTE_ENTREGA,
|
|
StatusAgendamento::ENTREGUE,
|
|
StatusAgendamento::NOVA_COLETA
|
|
]
|
|
)
|
|
->orWhereNull('status_id');
|
|
})
|
|
->count('id');
|
|
|
|
$prontoAtendimento = DB::table('pronto_atendimento_pedido_exame_item')
|
|
->join('pronto_atendimento_pedido_exame', 'pronto_atendimento_pedido_exame.id', '=', 'pronto_atendimento_pedido_exame_item.pedido_exame_id')
|
|
->join('cadastro_lotacao', 'cadastro_lotacao.id', '=', 'pronto_atendimento_pedido_exame.created_lotacao_id')
|
|
->join('parametrizacao_exame', 'parametrizacao_exame.id', 'pronto_atendimento_pedido_exame_item.exame_id')
|
|
->where('parametrizacao_exame.tipo_id', Tipo::LABORATORIAL)
|
|
->where('pronto_atendimento_pedido_exame_item.status_id', Status::REALIZADO)
|
|
->where('cadastro_lotacao.infraestrutura_id', $query->infraestrutura_id)
|
|
->whereNull('pronto_atendimento_pedido_exame_item.deleted_at')
|
|
->where(function ($query) use ($unidade) {
|
|
$query
|
|
->where(
|
|
DB::raw('DATE_FORMAT(pronto_atendimento_pedido_exame_item.created_at, "%Y-%m-%d")'),
|
|
'>=',
|
|
Carbon::createFromFormat('Y-m-d', $unidade->inicio)
|
|
->format('Y-m-d')
|
|
)
|
|
->where(
|
|
DB::raw('DATE_FORMAT(pronto_atendimento_pedido_exame_item.created_at, "%Y-%m-%d")'),
|
|
'<=',
|
|
Carbon::createFromFormat('Y-m-d', $unidade->fim)
|
|
->format('Y-m-d')
|
|
);
|
|
})
|
|
->count('pronto_atendimento_pedido_exame_item.id');
|
|
|
|
return $exameLaboratorial + $prontoAtendimento;
|
|
}
|
|
|
|
private static function handleQuantidadeSolicitanteUtilizada($unidade, $unidadeSolicitante)
|
|
{
|
|
$exameLaboratorial = DB::table('exame_laboratorial_agendamento_exame')
|
|
->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
|
->where(function ($agendada) use ($unidade, $unidadeSolicitante) {
|
|
|
|
$agendada
|
|
->where(
|
|
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%Y-%m-%d")'),
|
|
'>=',
|
|
Carbon::createFromFormat('Y-m-d', $unidade->inicio)
|
|
->format('Y-m-d')
|
|
)
|
|
->where(
|
|
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%Y-%m-%d")'),
|
|
'<=',
|
|
Carbon::createFromFormat('Y-m-d', $unidade->fim)
|
|
->format('Y-m-d')
|
|
);
|
|
|
|
|
|
if ($unidadeSolicitante->infraestrutura_solicitante_id) {
|
|
|
|
$agendada->where(
|
|
'exame_laboratorial_agendamento.infraestrutura_id',
|
|
$unidadeSolicitante->infraestrutura_solicitante_id
|
|
);
|
|
}
|
|
})
|
|
->where('exame_laboratorial_agendamento.laboratorio_infraestrutura_id', $unidade->infraestrutura_id)
|
|
->where(function ($query) {
|
|
$query
|
|
->whereIn(
|
|
'status_id',
|
|
[
|
|
StatusAgendamento::AGENDADO,
|
|
StatusAgendamento::REALIZADO,
|
|
StatusAgendamento::PENDENTE_ASSINATURA,
|
|
StatusAgendamento::ASSINADO,
|
|
StatusAgendamento::PENDENTE_ENTREGA,
|
|
StatusAgendamento::ENTREGUE,
|
|
StatusAgendamento::NOVA_COLETA
|
|
]
|
|
)
|
|
->orWhereNull('status_id');
|
|
})
|
|
->count('exame_laboratorial_agendamento_exame.id');
|
|
|
|
$prontoAtendimento = DB::table('pronto_atendimento_pedido_exame_item')
|
|
->join('pronto_atendimento_pedido_exame', 'pronto_atendimento_pedido_exame.id', '=', 'pronto_atendimento_pedido_exame_item.pedido_exame_id')
|
|
->join('cadastro_lotacao', 'cadastro_lotacao.id', '=', 'pronto_atendimento_pedido_exame.created_lotacao_id')
|
|
->join('cadastro_lotacao as lotacao_laboratorio', 'lotacao_laboratorio.id', '=', 'pronto_atendimento_pedido_exame.laboratorio_lotacao_id')
|
|
->join('parametrizacao_exame', 'parametrizacao_exame.id', 'pronto_atendimento_pedido_exame_item.exame_id')
|
|
->where('parametrizacao_exame.tipo_id', Tipo::LABORATORIAL)
|
|
->where('pronto_atendimento_pedido_exame_item.status_id', Status::REALIZADO)
|
|
->where('cadastro_lotacao.infraestrutura_id', $unidadeSolicitante->infraestrutura_solicitante_id)
|
|
->where('lotacao_laboratorio.infraestrutura_id', $unidade->infraestrutura_id)
|
|
->where(function ($query) use ($unidade) {
|
|
$query
|
|
->where(
|
|
DB::raw('DATE_FORMAT(pronto_atendimento_pedido_exame_item.created_at, "%Y-%m-%d")'),
|
|
'>=',
|
|
Carbon::createFromFormat('Y-m-d', $unidade->inicio)
|
|
->format('Y-m-d')
|
|
)
|
|
->where(
|
|
DB::raw('DATE_FORMAT(pronto_atendimento_pedido_exame_item.created_at, "%Y-%m-%d")'),
|
|
'<=',
|
|
Carbon::createFromFormat('Y-m-d', $unidade->fim)
|
|
->format('Y-m-d')
|
|
);
|
|
})
|
|
->count('pronto_atendimento_pedido_exame_item.id');
|
|
|
|
return $exameLaboratorial + $prontoAtendimento;
|
|
}
|
|
|
|
private static function handleValorUtilizado($unidade, $query)
|
|
{
|
|
|
|
$inicio = Carbon::createFromFormat('Y-m-d', $unidade->inicio)->startOfDay();
|
|
$fim = Carbon::createFromFormat('Y-m-d', $unidade->fim)->endOfDay();
|
|
|
|
$exameLaboratorial = DB::table('exame_laboratorial_agendamento_exame')
|
|
->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', '=', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
|
->whereBetween('exame_laboratorial_agendamento.data', [$inicio, $fim])
|
|
->when($query->infraestrutura_id, function ($agendada) use ($query) {
|
|
$agendada->where('exame_laboratorial_agendamento.laboratorio_infraestrutura_id', $query->infraestrutura_id);
|
|
})
|
|
->where(function ($jquery) {
|
|
$jquery->where(function ($agendada) {
|
|
$agendada->whereIn('status_id', [
|
|
StatusAgendamento::AGENDADO,
|
|
StatusAgendamento::REALIZADO,
|
|
StatusAgendamento::PENDENTE_ASSINATURA,
|
|
StatusAgendamento::ASSINADO,
|
|
StatusAgendamento::PENDENTE_ENTREGA,
|
|
StatusAgendamento::ENTREGUE,
|
|
StatusAgendamento::NOVA_COLETA,
|
|
])
|
|
->orWhereNull('status_id');
|
|
});
|
|
})
|
|
->selectRaw('FORMAT(SUM(exame_laboratorial_agendamento_exame.valor), 2, "pt_BR") AS valor')
|
|
->value('valor');
|
|
|
|
$prontoAtendimento = DB::table('pronto_atendimento_pedido_exame_item')
|
|
->join('pronto_atendimento_pedido_exame', 'pronto_atendimento_pedido_exame.id', '=', 'pronto_atendimento_pedido_exame_item.pedido_exame_id')
|
|
->join('cadastro_lotacao', 'cadastro_lotacao.id', '=', 'pronto_atendimento_pedido_exame.laboratorio_lotacao_id')
|
|
->join('parametrizacao_exame', 'parametrizacao_exame.id', '=', 'pronto_atendimento_pedido_exame_item.exame_id')
|
|
->where('parametrizacao_exame.tipo_id', Tipo::LABORATORIAL)
|
|
->where('pronto_atendimento_pedido_exame_item.status_id', Status::REALIZADO)
|
|
->where('cadastro_lotacao.infraestrutura_id', $query->infraestrutura_id)
|
|
->whereBetween('pronto_atendimento_pedido_exame_item.created_at', [$inicio, $fim])
|
|
->selectRaw('FORMAT(SUM(pronto_atendimento_pedido_exame_item.valor_exame), 2, "pt_BR") AS valor')
|
|
->value('valor');
|
|
|
|
$exameLaboratorial = str_replace(',', '.', str_replace('.', '', $exameLaboratorial));
|
|
$prontoAtendimento = str_replace(',', '.', str_replace('.', '', $prontoAtendimento));
|
|
|
|
$total = floatval($exameLaboratorial) + floatval($prontoAtendimento);
|
|
|
|
return number_format($total, 2, ',', '.');
|
|
}
|
|
|
|
private static function handleUnidadeValorUtilizado($unidade, $query)
|
|
{
|
|
|
|
$inicio = Carbon::createFromFormat('Y-m-d', $unidade->inicio)->startOfDay();
|
|
$fim = Carbon::createFromFormat('Y-m-d', $unidade->fim)->endOfDay();
|
|
|
|
$exameLaboratorial = DB::table('exame_laboratorial_agendamento_exame')
|
|
->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', '=', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
|
->whereBetween('exame_laboratorial_agendamento.data', [$inicio, $fim])
|
|
->when($query->infraestrutura_id, function ($agendada) use ($query) {
|
|
$agendada->where('exame_laboratorial_agendamento.infraestrutura_id', $query->infraestrutura_id);
|
|
})
|
|
->where(function ($jquery) {
|
|
$jquery->where(function ($agendada) {
|
|
$agendada->whereIn('status_id', [
|
|
StatusAgendamento::AGENDADO,
|
|
StatusAgendamento::REALIZADO,
|
|
StatusAgendamento::PENDENTE_ASSINATURA,
|
|
StatusAgendamento::ASSINADO,
|
|
StatusAgendamento::PENDENTE_ENTREGA,
|
|
StatusAgendamento::ENTREGUE,
|
|
StatusAgendamento::NOVA_COLETA,
|
|
])
|
|
->orWhereNull('status_id');
|
|
});
|
|
})
|
|
->selectRaw('FORMAT(SUM(exame_laboratorial_agendamento_exame.valor), 2, "pt_BR") AS valor')
|
|
->value('valor');
|
|
|
|
$prontoAtendimento = DB::table('pronto_atendimento_pedido_exame_item')
|
|
->join('pronto_atendimento_pedido_exame', 'pronto_atendimento_pedido_exame.id', '=', 'pronto_atendimento_pedido_exame_item.pedido_exame_id')
|
|
->join('cadastro_lotacao', 'cadastro_lotacao.id', '=', 'pronto_atendimento_pedido_exame.created_lotacao_id')
|
|
->join('parametrizacao_exame', 'parametrizacao_exame.id', '=', 'pronto_atendimento_pedido_exame_item.exame_id')
|
|
->where('parametrizacao_exame.tipo_id', Tipo::LABORATORIAL)
|
|
->where('pronto_atendimento_pedido_exame_item.status_id', Status::REALIZADO)
|
|
->where('cadastro_lotacao.infraestrutura_id', $query->infraestrutura_id)
|
|
->whereBetween('pronto_atendimento_pedido_exame_item.created_at', [$inicio, $fim])
|
|
->selectRaw('FORMAT(SUM(pronto_atendimento_pedido_exame_item.valor_exame), 2, "pt_BR") AS valor')
|
|
->value('valor');
|
|
|
|
$exameLaboratorial = str_replace(',', '.', str_replace('.', '', $exameLaboratorial));
|
|
$prontoAtendimento = str_replace(',', '.', str_replace('.', '', $prontoAtendimento));
|
|
|
|
$total = floatval($exameLaboratorial) + floatval($prontoAtendimento);
|
|
|
|
return number_format($total, 2, ',', '.');
|
|
}
|
|
|
|
private static function handleTotalValor($query)
|
|
{
|
|
|
|
$cota = DB::table('exame_laboratorial_parametrizacao_cota')
|
|
->whereYear('exame_laboratorial_parametrizacao_cota.inicio', $query->ano)
|
|
->when(request()->mes, function ($agendada) {
|
|
$agendada->whereMonth('exame_laboratorial_parametrizacao_cota.inicio', request()->mes);
|
|
})
|
|
->where('unidade_tipo_id', TipoUnidade::LABORATORIO)
|
|
->where('cota_tipo_id', TipoCota::VALOR)
|
|
->where('infraestrutura_id', $query->infraestrutura_id)
|
|
->whereNull('deleted_at')
|
|
->selectRaw('FORMAT(SUM(cota_valor), 2, "pt_BR") AS valor')
|
|
->value('valor');
|
|
|
|
$cota = str_replace(',', '.', str_replace('.', '', $cota));
|
|
$total = floatval($cota);
|
|
|
|
return number_format($total, 2, ',', '.');
|
|
}
|
|
|
|
private static function handleTotalQuantidadeUtilizado($query)
|
|
{
|
|
|
|
$cota = DB::table('exame_laboratorial_parametrizacao_cota')
|
|
->whereYear('exame_laboratorial_parametrizacao_cota.inicio', $query->ano)
|
|
->when(request()->mes, function ($agendada) {
|
|
$agendada->whereMonth('exame_laboratorial_parametrizacao_cota.inicio', request()->mes);
|
|
})
|
|
->where('unidade_tipo_id', TipoUnidade::LABORATORIO)
|
|
->where('cota_tipo_id', TipoCota::QUANTIDADE)
|
|
->where('infraestrutura_id', $query->infraestrutura_id)
|
|
->whereNull('deleted_at')
|
|
->sum('cota_quantidade');
|
|
|
|
return $cota;
|
|
}
|
|
|
|
private static function handleTotalValorUtilizado($query)
|
|
{
|
|
|
|
$exameLaboratorial = DB::table('exame_laboratorial_agendamento_exame')
|
|
->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', '=', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
|
->whereYear('exame_laboratorial_agendamento.data', $query->ano)
|
|
->when(request()->mes, function ($agendada) {
|
|
$agendada->whereMonth('exame_laboratorial_agendamento.data', request()->mes);
|
|
})
|
|
->when($query->infraestrutura_id, function ($agendada) use ($query) {
|
|
$agendada->where('exame_laboratorial_agendamento.laboratorio_infraestrutura_id', $query->infraestrutura_id);
|
|
})
|
|
->where(function ($jquery) {
|
|
$jquery->where(function ($agendada) {
|
|
$agendada->whereIn('status_id', [
|
|
StatusAgendamento::AGENDADO,
|
|
StatusAgendamento::REALIZADO,
|
|
StatusAgendamento::PENDENTE_ASSINATURA,
|
|
StatusAgendamento::ASSINADO,
|
|
StatusAgendamento::PENDENTE_ENTREGA,
|
|
StatusAgendamento::ENTREGUE,
|
|
StatusAgendamento::NOVA_COLETA,
|
|
])
|
|
->orWhereNull('status_id');
|
|
});
|
|
})
|
|
->whereExists(function ($q) use ($query) {
|
|
$q->select(DB::raw(1))
|
|
->from('exame_laboratorial_parametrizacao_cota')
|
|
->where('exame_laboratorial_parametrizacao_cota.infraestrutura_id', $query->infraestrutura_id)
|
|
->where('exame_laboratorial_parametrizacao_cota.unidade_tipo_id', TipoUnidade::LABORATORIO)
|
|
->where('exame_laboratorial_parametrizacao_cota.cota_tipo_id', TipoCota::VALOR)
|
|
->whereColumn('exame_laboratorial_agendamento.data', '>=', 'exame_laboratorial_parametrizacao_cota.inicio')
|
|
->whereColumn('exame_laboratorial_agendamento.data', '<=', 'exame_laboratorial_parametrizacao_cota.fim');
|
|
})
|
|
->selectRaw('FORMAT(SUM(exame_laboratorial_agendamento_exame.valor), 2, "pt_BR") AS valor')
|
|
->value('valor');
|
|
|
|
$prontoAtendimento = DB::table('pronto_atendimento_pedido_exame_item')
|
|
->join('pronto_atendimento_pedido_exame', 'pronto_atendimento_pedido_exame.id', '=', 'pronto_atendimento_pedido_exame_item.pedido_exame_id')
|
|
->join('cadastro_lotacao', 'cadastro_lotacao.id', '=', 'pronto_atendimento_pedido_exame.laboratorio_lotacao_id')
|
|
->join('parametrizacao_exame', 'parametrizacao_exame.id', '=', 'pronto_atendimento_pedido_exame_item.exame_id')
|
|
->where('parametrizacao_exame.tipo_id', Tipo::LABORATORIAL)
|
|
->where('pronto_atendimento_pedido_exame_item.status_id', Status::REALIZADO)
|
|
->whereYear('pronto_atendimento_pedido_exame_item.created_at', $query->ano)
|
|
->when(request()->mes, function ($agendada) {
|
|
$agendada->whereMonth('pronto_atendimento_pedido_exame_item.created_at', request()->mes);
|
|
})
|
|
->whereNull('pronto_atendimento_pedido_exame_item.deleted_at')
|
|
->whereExists(function ($q) use ($query) {
|
|
$q->select(DB::raw(1))
|
|
->from('exame_laboratorial_parametrizacao_cota')
|
|
->where('exame_laboratorial_parametrizacao_cota.infraestrutura_id', $query->infraestrutura_id)
|
|
->where('exame_laboratorial_parametrizacao_cota.unidade_tipo_id', TipoUnidade::LABORATORIO)
|
|
->where('exame_laboratorial_parametrizacao_cota.cota_tipo_id', TipoCota::VALOR)
|
|
->whereColumn('pronto_atendimento_pedido_exame_item.created_at', '>=', 'exame_laboratorial_parametrizacao_cota.inicio')
|
|
->whereColumn('pronto_atendimento_pedido_exame_item.created_at', '<=', 'exame_laboratorial_parametrizacao_cota.fim');
|
|
})
|
|
->where('cadastro_lotacao.infraestrutura_id', $query->infraestrutura_id)
|
|
->selectRaw('FORMAT(SUM(pronto_atendimento_pedido_exame_item.valor_exame), 2, "pt_BR") AS valor')
|
|
->value('valor');
|
|
|
|
$exameLaboratorial = str_replace(',', '.', str_replace('.', '', $exameLaboratorial));
|
|
$prontoAtendimento = str_replace(',', '.', str_replace('.', '', $prontoAtendimento));
|
|
|
|
$total = floatval($exameLaboratorial) + floatval($prontoAtendimento);
|
|
|
|
return number_format($total, 2, ',', '.');
|
|
}
|
|
|
|
private static function handleValorSolicitanteUtilizada($unidade, $unidadeSolicitante)
|
|
{
|
|
|
|
$inicio = Carbon::createFromFormat('Y-m-d', $unidade->inicio)->startOfDay();
|
|
$fim = Carbon::createFromFormat('Y-m-d', $unidade->fim)->endOfDay();
|
|
|
|
$exameLaboratorial = DB::table('exame_laboratorial_agendamento_exame')
|
|
->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', '=', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
|
->whereBetween('exame_laboratorial_agendamento.data', [$inicio, $fim])
|
|
->where('exame_laboratorial_agendamento.infraestrutura_id', $unidadeSolicitante->infraestrutura_solicitante_id)
|
|
->where('exame_laboratorial_agendamento.laboratorio_infraestrutura_id', $unidade->infraestrutura_id)
|
|
->where(function ($jquery) {
|
|
$jquery->where(function ($agendada) {
|
|
$agendada->whereIn('status_id', [
|
|
StatusAgendamento::AGENDADO,
|
|
StatusAgendamento::REALIZADO,
|
|
StatusAgendamento::PENDENTE_ASSINATURA,
|
|
StatusAgendamento::ASSINADO,
|
|
StatusAgendamento::PENDENTE_ENTREGA,
|
|
StatusAgendamento::ENTREGUE,
|
|
StatusAgendamento::NOVA_COLETA,
|
|
])
|
|
->orWhereNull('status_id');
|
|
});
|
|
})
|
|
->selectRaw('FORMAT(SUM(exame_laboratorial_agendamento_exame.valor), 2, "pt_BR") AS valor')
|
|
->value('valor');
|
|
|
|
$prontoAtendimento = DB::table('pronto_atendimento_pedido_exame_item')
|
|
->join('pronto_atendimento_pedido_exame', 'pronto_atendimento_pedido_exame.id', '=', 'pronto_atendimento_pedido_exame_item.pedido_exame_id')
|
|
->join('cadastro_lotacao', 'cadastro_lotacao.id', '=', 'pronto_atendimento_pedido_exame.created_lotacao_id')
|
|
->join('cadastro_lotacao as lotacao_laboratorio', 'lotacao_laboratorio.id', '=', 'pronto_atendimento_pedido_exame.laboratorio_lotacao_id')
|
|
->join('parametrizacao_exame', 'parametrizacao_exame.id', '=', 'pronto_atendimento_pedido_exame_item.exame_id')
|
|
->where('parametrizacao_exame.tipo_id', Tipo::LABORATORIAL)
|
|
->where('pronto_atendimento_pedido_exame_item.status_id', Status::REALIZADO)
|
|
->where('cadastro_lotacao.infraestrutura_id', $unidadeSolicitante->infraestrutura_solicitante_id)
|
|
->where('lotacao_laboratorio.infraestrutura_id', $unidade->infraestrutura_id)
|
|
->whereNull('pronto_atendimento_pedido_exame_item.deleted_at')
|
|
->whereBetween('pronto_atendimento_pedido_exame_item.created_at', [$inicio, $fim])
|
|
->selectRaw('FORMAT(SUM(pronto_atendimento_pedido_exame_item.valor_exame), 2, "pt_BR") AS valor')
|
|
->value('valor');
|
|
|
|
$exameLaboratorial = str_replace(',', '.', str_replace('.', '', $exameLaboratorial));
|
|
$prontoAtendimento = str_replace(',', '.', str_replace('.', '', $prontoAtendimento));
|
|
|
|
$total = floatval($exameLaboratorial) + floatval($prontoAtendimento);
|
|
|
|
return number_format($total, 2, ',', '.');
|
|
}
|
|
|
|
private static function handleExameValorUtilizado($unidade, $exame)
|
|
{
|
|
|
|
$inicio = Carbon::createFromFormat('Y-m-d', $unidade->inicio)->startOfDay();
|
|
$fim = Carbon::createFromFormat('Y-m-d', $unidade->fim)->endOfDay();
|
|
|
|
$exameLaboratorial = DB::table('exame_laboratorial_agendamento_exame')
|
|
->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', '=', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
|
->whereBetween('exame_laboratorial_agendamento.data', [$inicio, $fim])
|
|
->when($unidade->infraestrutura_id, function ($agendada) use ($unidade) {
|
|
$agendada->where('exame_laboratorial_agendamento.laboratorio_infraestrutura_id', $unidade->infraestrutura_id);
|
|
})
|
|
->where('exame_laboratorial_agendamento_exame.exame_id', $exame->exame_id)
|
|
->where(function ($jquery) {
|
|
$jquery->where(function ($agendada) {
|
|
$agendada->whereIn('status_id', [
|
|
StatusAgendamento::AGENDADO,
|
|
StatusAgendamento::REALIZADO,
|
|
StatusAgendamento::PENDENTE_ASSINATURA,
|
|
StatusAgendamento::ASSINADO,
|
|
StatusAgendamento::PENDENTE_ENTREGA,
|
|
StatusAgendamento::ENTREGUE,
|
|
StatusAgendamento::NOVA_COLETA,
|
|
])
|
|
->orWhereNull('status_id');
|
|
});
|
|
})
|
|
->selectRaw('FORMAT(SUM(exame_laboratorial_agendamento_exame.valor), 2, "pt_BR") AS valor')
|
|
->value('valor');
|
|
|
|
$prontoAtendimento = DB::table('pronto_atendimento_pedido_exame_item')
|
|
->join('pronto_atendimento_pedido_exame', 'pronto_atendimento_pedido_exame.id', '=', 'pronto_atendimento_pedido_exame_item.pedido_exame_id')
|
|
->join('cadastro_lotacao', 'cadastro_lotacao.id', '=', 'pronto_atendimento_pedido_exame.laboratorio_lotacao_id')
|
|
->join('parametrizacao_exame', 'parametrizacao_exame.id', '=', 'pronto_atendimento_pedido_exame_item.exame_id')
|
|
->where('parametrizacao_exame.tipo_id', Tipo::LABORATORIAL)
|
|
->where('pronto_atendimento_pedido_exame_item.status_id', Status::REALIZADO)
|
|
->whereNull('pronto_atendimento_pedido_exame_item.deleted_at')
|
|
->where('pronto_atendimento_pedido_exame_item.exame_id', $exame->exame_id)
|
|
->when($unidade->infraestrutura_id, function ($agendada) use ($unidade) {
|
|
$agendada->where('cadastro_lotacao.infraestrutura_id', $unidade->infraestrutura_id);
|
|
})
|
|
->whereBetween('pronto_atendimento_pedido_exame_item.created_at', [$inicio, $fim])
|
|
->selectRaw('FORMAT(SUM(pronto_atendimento_pedido_exame_item.valor_exame), 2, "pt_BR") AS valor')
|
|
->value('valor');
|
|
|
|
$exameLaboratorial = str_replace(',', '.', str_replace('.', '', $exameLaboratorial));
|
|
$prontoAtendimento = str_replace(',', '.', str_replace('.', '', $prontoAtendimento));
|
|
|
|
$total = floatval($exameLaboratorial) + floatval($prontoAtendimento);
|
|
|
|
return number_format($total, 2, ',', '.');
|
|
}
|
|
|
|
private static function handleExameQuantidadeUtilizada($unidade, $exame)
|
|
{
|
|
|
|
$inicio = Carbon::createFromFormat('Y-m-d', $unidade->inicio)->startOfDay();
|
|
$fim = Carbon::createFromFormat('Y-m-d', $unidade->fim)->endOfDay();
|
|
|
|
$exameLaboratorial = DB::table('exame_laboratorial_agendamento')
|
|
->where(function ($agendada) use ($unidade, $inicio, $fim) {
|
|
|
|
$agendada->whereBetween('exame_laboratorial_agendamento.data', [$inicio, $fim]);
|
|
|
|
if ($unidade->infraestrutura_id) {
|
|
|
|
$agendada->where(
|
|
'exame_laboratorial_agendamento.laboratorio_infraestrutura_id',
|
|
$unidade->infraestrutura_id
|
|
);
|
|
}
|
|
})
|
|
->whereExists(function ($query) use ($exame) {
|
|
|
|
$query
|
|
->select(DB::raw(1))
|
|
->from('exame_laboratorial_agendamento_exame')
|
|
->where('exame_laboratorial_agendamento_exame.exame_id', $exame->exame_id)
|
|
->whereColumn('exame_laboratorial_agendamento_exame.agendamento_id', 'exame_laboratorial_agendamento.id')
|
|
->where(function ($jquery) {
|
|
$jquery->where(function ($agendada) {
|
|
$agendada->whereIn('status_id', [
|
|
StatusAgendamento::AGENDADO,
|
|
StatusAgendamento::REALIZADO,
|
|
StatusAgendamento::PENDENTE_ASSINATURA,
|
|
StatusAgendamento::ASSINADO,
|
|
StatusAgendamento::PENDENTE_ENTREGA,
|
|
StatusAgendamento::ENTREGUE,
|
|
StatusAgendamento::NOVA_COLETA,
|
|
])
|
|
->orWhereNull('status_id');
|
|
});
|
|
});
|
|
})
|
|
->count('id');
|
|
|
|
$prontoAtendimento = DB::table('pronto_atendimento_pedido_exame_item')
|
|
->join('pronto_atendimento_pedido_exame', 'pronto_atendimento_pedido_exame.id', '=', 'pronto_atendimento_pedido_exame_item.pedido_exame_id')
|
|
->join('cadastro_lotacao', 'cadastro_lotacao.id', '=', 'pronto_atendimento_pedido_exame.laboratorio_lotacao_id')
|
|
->join('parametrizacao_exame', 'parametrizacao_exame.id', 'pronto_atendimento_pedido_exame_item.exame_id')
|
|
->where('parametrizacao_exame.tipo_id', Tipo::LABORATORIAL)
|
|
->where('pronto_atendimento_pedido_exame_item.status_id', Status::REALIZADO)
|
|
->whereNull('pronto_atendimento_pedido_exame_item.deleted_at')
|
|
->where('pronto_atendimento_pedido_exame_item.exame_id', $exame->exame_id)
|
|
->where('cadastro_lotacao.infraestrutura_id', $unidade->infraestrutura_id)
|
|
->whereBetween('pronto_atendimento_pedido_exame_item.created_at', [$inicio, $fim])
|
|
->count('pronto_atendimento_pedido_exame_item.id');
|
|
|
|
return $exameLaboratorial + $prontoAtendimento;
|
|
}
|
|
|
|
private static function handleExameUnidadeSolicitanteValorUtilizado($unidade, $exameId, $unidadeSolicitante)
|
|
{
|
|
|
|
$inicio = Carbon::createFromFormat('Y-m-d', $unidade->inicio)->startOfDay();
|
|
$fim = Carbon::createFromFormat('Y-m-d', $unidade->fim)->endOfDay();
|
|
|
|
$exameLaboratorial = DB::table('exame_laboratorial_agendamento_exame')
|
|
->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', '=', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
|
->whereBetween('exame_laboratorial_agendamento.data', [$inicio, $fim])
|
|
->when($unidade->infraestrutura_id, function ($agendada) use ($unidade) {
|
|
$agendada->where('exame_laboratorial_agendamento.laboratorio_infraestrutura_id', $unidade->infraestrutura_id);
|
|
})
|
|
->where('exame_laboratorial_agendamento.infraestrutura_id', $unidadeSolicitante->infraestrutura_solicitante_id)
|
|
->where('exame_laboratorial_agendamento_exame.exame_id', $exameId)
|
|
->whereIn('status_id', [
|
|
StatusAgendamento::AGENDADO,
|
|
StatusAgendamento::REALIZADO,
|
|
StatusAgendamento::PENDENTE_ASSINATURA,
|
|
StatusAgendamento::ASSINADO,
|
|
StatusAgendamento::PENDENTE_ENTREGA,
|
|
StatusAgendamento::ENTREGUE,
|
|
StatusAgendamento::NOVA_COLETA,
|
|
])
|
|
->orWhereNull('status_id')
|
|
->selectRaw('FORMAT(SUM(exame_laboratorial_agendamento_exame.valor), 2, "pt_BR") AS valor')
|
|
->value('valor');
|
|
|
|
$prontoAtendimento = DB::table('pronto_atendimento_pedido_exame_item')
|
|
->join('pronto_atendimento_pedido_exame', 'pronto_atendimento_pedido_exame.id', '=', 'pronto_atendimento_pedido_exame_item.pedido_exame_id')
|
|
->join('cadastro_lotacao', 'cadastro_lotacao.id', '=', 'pronto_atendimento_pedido_exame.created_lotacao_id')
|
|
->join('cadastro_lotacao as lotacao_laboratorio', 'lotacao_laboratorio.id', '=', 'pronto_atendimento_pedido_exame.laboratorio_lotacao_id')
|
|
->join('parametrizacao_exame', 'parametrizacao_exame.id', '=', 'pronto_atendimento_pedido_exame_item.exame_id')
|
|
->where('parametrizacao_exame.tipo_id', Tipo::LABORATORIAL)
|
|
->where('pronto_atendimento_pedido_exame_item.status_id', Status::REALIZADO)
|
|
->whereNull('pronto_atendimento_pedido_exame_item.deleted_at')
|
|
->where('cadastro_lotacao.infraestrutura_id', $unidadeSolicitante->infraestrutura_solicitante_id)
|
|
->where('pronto_atendimento_pedido_exame_item.exame_id', $exameId)
|
|
->where('lotacao_laboratorio.infraestrutura_id', $unidade->infraestrutura_id)
|
|
->whereBetween('pronto_atendimento_pedido_exame_item.created_at', [$inicio, $fim])
|
|
->selectRaw('FORMAT(SUM(pronto_atendimento_pedido_exame_item.valor_exame), 2, "pt_BR") AS valor')
|
|
->value('valor');
|
|
|
|
$exameLaboratorial = str_replace(',', '.', str_replace('.', '', $exameLaboratorial));
|
|
$prontoAtendimento = str_replace(',', '.', str_replace('.', '', $prontoAtendimento));
|
|
|
|
$total = floatval($exameLaboratorial) + floatval($prontoAtendimento);
|
|
|
|
return number_format($total, 2, ',', '.');
|
|
}
|
|
|
|
private static function handleExameUnidadeSolicitanteQuantidadeUtilizada($unidade, $exameId, $unidadeSolicitante)
|
|
{
|
|
|
|
$exameLaboratorial = DB::table('exame_laboratorial_agendamento')
|
|
->where(function ($agendada) use ($unidade, $unidadeSolicitante) {
|
|
|
|
$agendada
|
|
->where(
|
|
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%Y-%m-%d")'),
|
|
'>=',
|
|
Carbon::createFromFormat('Y-m-d', $unidade->inicio)
|
|
->format('Y-m-d')
|
|
)
|
|
->where(
|
|
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%Y-%m-%d")'),
|
|
'<=',
|
|
Carbon::createFromFormat('Y-m-d', $unidade->fim)
|
|
->format('Y-m-d')
|
|
);
|
|
|
|
|
|
if ($unidade->infraestrutura_id) {
|
|
|
|
$agendada->where(
|
|
'exame_laboratorial_agendamento.laboratorio_infraestrutura_id',
|
|
$unidade->infraestrutura_id
|
|
);
|
|
$agendada->where(
|
|
'exame_laboratorial_agendamento.infraestrutura_id',
|
|
$unidadeSolicitante->infraestrutura_solicitante_id
|
|
);
|
|
}
|
|
})
|
|
->whereExists(function ($query) use ($exameId) {
|
|
|
|
$query
|
|
->select(DB::raw(1))
|
|
->from('exame_laboratorial_agendamento_exame')
|
|
->where('exame_laboratorial_agendamento_exame.exame_id', $exameId)
|
|
->whereColumn('exame_laboratorial_agendamento_exame.agendamento_id', 'exame_laboratorial_agendamento.id')
|
|
->whereIn(
|
|
'status_id',
|
|
[
|
|
StatusAgendamento::AGENDADO,
|
|
StatusAgendamento::REALIZADO,
|
|
StatusAgendamento::PENDENTE_ASSINATURA,
|
|
StatusAgendamento::ASSINADO,
|
|
StatusAgendamento::PENDENTE_ENTREGA,
|
|
StatusAgendamento::ENTREGUE,
|
|
StatusAgendamento::NOVA_COLETA
|
|
]
|
|
)
|
|
->orWhereNull('status_id');
|
|
})
|
|
->count('id');
|
|
|
|
$prontoAtendimento = DB::table('pronto_atendimento_pedido_exame_item')
|
|
->join('pronto_atendimento_pedido_exame', 'pronto_atendimento_pedido_exame.id', '=', 'pronto_atendimento_pedido_exame_item.pedido_exame_id')
|
|
->join('cadastro_lotacao', 'cadastro_lotacao.id', '=', 'pronto_atendimento_pedido_exame.created_lotacao_id')
|
|
->join('cadastro_lotacao as lotacao_laboratorio', 'lotacao_laboratorio.id', '=', 'pronto_atendimento_pedido_exame.laboratorio_lotacao_id')
|
|
->join('parametrizacao_exame', 'parametrizacao_exame.id', 'pronto_atendimento_pedido_exame_item.exame_id')
|
|
->where('parametrizacao_exame.tipo_id', Tipo::LABORATORIAL)
|
|
->where('pronto_atendimento_pedido_exame_item.status_id', Status::REALIZADO)
|
|
->whereNull('pronto_atendimento_pedido_exame_item.deleted_at')
|
|
->where('cadastro_lotacao.infraestrutura_id', $unidadeSolicitante->infraestrutura_solicitante_id)
|
|
->where('pronto_atendimento_pedido_exame_item.exame_id', $exameId)
|
|
->where('lotacao_laboratorio.infraestrutura_id', $unidade->infraestrutura_id)
|
|
->where(function ($query) use ($unidade) {
|
|
$query
|
|
->where(
|
|
DB::raw('DATE_FORMAT(pronto_atendimento_pedido_exame_item.created_at, "%Y-%m-%d")'),
|
|
'>=',
|
|
Carbon::createFromFormat('Y-m-d', $unidade->inicio)
|
|
->format('Y-m-d')
|
|
)
|
|
->where(
|
|
DB::raw('DATE_FORMAT(pronto_atendimento_pedido_exame_item.created_at, "%Y-%m-%d")'),
|
|
'<=',
|
|
Carbon::createFromFormat('Y-m-d', $unidade->fim)
|
|
->format('Y-m-d')
|
|
);
|
|
})
|
|
->count('pronto_atendimento_pedido_exame_item.id');
|
|
|
|
return $exameLaboratorial + $prontoAtendimento;
|
|
}
|
|
|
|
public static function saldo_exame($request)
|
|
{
|
|
return [
|
|
'cotas_laboratorio' => Cota::where('unidade_tipo_id', TipoUnidade::LABORATORIO)
|
|
->where('infraestrutura_id', $request->infraestrutura_id)
|
|
->groupBy('infraestrutura_id')
|
|
->get()
|
|
->map(function ($query) {
|
|
return [
|
|
'unidade_id' => $query->unidade->id ?? null,
|
|
'infraestrutura_id' => $query->infraestrutura_id ?? null,
|
|
'cota_laboratorio' => Cota::where('unidade_tipo_id', TipoUnidade::LABORATORIO)
|
|
->where('unidade_id', $query->unidade->id)
|
|
->where('infraestrutura_id', $query->infraestrutura_id)
|
|
->get()
|
|
->map(fn($unidade) => [
|
|
'id' => $unidade->id ?? null,
|
|
'unidade_id' => $query->unidade->id ?? null,
|
|
'infraestrutura_id' => $query->infraestrutura_id ?? null,
|
|
'unidade_tipo_id' => $unidade->unidade_tipo_id ?? null,
|
|
'cota_tipo_id' => $unidade->cota_tipo_id ?? null,
|
|
'cota_tipo_nome' => $unidade->cota_tipo_id == TipoCota::VALOR ? 'Valor' : 'Quantidade',
|
|
"periodo" => [
|
|
'start' => (new Carbon($unidade->inicio))->format('d/m/Y') ?? null,
|
|
'end' => (new Carbon($unidade->fim))->format('d/m/Y') ?? null,
|
|
],
|
|
'cota' => $unidade->cota_tipo_id == TipoCota::VALOR
|
|
? number_format($unidade->cota_valor, 2, ',', '.')
|
|
: $unidade->cota_quantidade,
|
|
'saldo' => $unidade->cota_tipo_id == TipoCota::VALOR
|
|
? number_format($unidade->cota_valor, 2, ',', '.')
|
|
: $unidade->cota_quantidade,
|
|
'utilizada' => "0",
|
|
'cota_quantidade' => $unidade->cota_quantidade ?? null,
|
|
'cota_valor' => number_format($unidade->cota_valor, 2, ',', '.') ?? null,
|
|
'observacao' => $unidade->observacao ?? null,
|
|
'unidadeSolicitante' => CotaUnidadeSolicitante::where('cota_id', $unidade->id)
|
|
->get()
|
|
->map(fn($unidadeSolicitante) => [
|
|
'cota_id' => $query->id,
|
|
'unidade_solicitante_id' => $unidadeSolicitante->unidade_solicitante_id ?? null,
|
|
'unidade_solicitante' => $unidadeSolicitante->unidadeSolicitante->nome_fantasia
|
|
?? null,
|
|
'infraestrutura_solicitante_id' =>
|
|
$unidadeSolicitante->infraestrutura_solicitante_id ?? null,
|
|
'infraestrutura_nome' =>
|
|
$unidadeSolicitante->infraestruturaSolicitante->nome ?? null,
|
|
'cota_quantidade' => $unidadeSolicitante->cota_quantidade ?? null,
|
|
'cota_valor' => number_format($unidadeSolicitante->cota_valor, 2, ',', '.')
|
|
?? null,
|
|
'cota' => $unidade->cota_tipo_id == TipoCota::VALOR
|
|
? number_format($unidadeSolicitante->cota_valor, 2, ',', '.')
|
|
: $unidadeSolicitante->cota_quantidade,
|
|
]),
|
|
'exames' => CotaExame::where('cota_id', $unidade->id)
|
|
->get()
|
|
->map(fn($exame) => [
|
|
'exame_id' => $exame->exame_id ?? null,
|
|
'cota_exame_periodo_id' => $exame->cota_exame_periodo_id ?? null,
|
|
'nome' => $exame->exame->nome ?? null,
|
|
'cota_quantidade' => $exame->cota_quantidade ?? null,
|
|
'cota_valor' => number_format($exame->cota_valor, 2, ',', '.') ?? null,
|
|
'cota' => $unidade->cota_tipo_id == TipoCota::VALOR
|
|
? number_format($exame->cota_valor, 2, ',', '.')
|
|
: $exame->cota_quantidade,
|
|
'valor_agendado' => AgendamentoExame::where('exame_id', $exame->exame_id)
|
|
->where(function ($query) {
|
|
$query
|
|
->whereIn(
|
|
'status_id',
|
|
[
|
|
StatusAgendamento::AGENDADO,
|
|
StatusAgendamento::REALIZADO,
|
|
StatusAgendamento::PENDENTE_ASSINATURA,
|
|
StatusAgendamento::ASSINADO,
|
|
StatusAgendamento::PENDENTE_ENTREGA,
|
|
StatusAgendamento::ENTREGUE,
|
|
StatusAgendamento::NOVA_COLETA
|
|
]
|
|
)
|
|
->orWhereNull('status_id');
|
|
})
|
|
->whereHas('agendamento', function ($query) use ($unidade) {
|
|
|
|
$query
|
|
->where(
|
|
DB::raw('DATE_FORMAT(data, "%Y-%m-%d")'),
|
|
'>=',
|
|
Carbon::createFromFormat('Y-m-d', $unidade->inicio)
|
|
->format('Y-m-d')
|
|
)
|
|
->where(
|
|
DB::raw('DATE_FORMAT(data, "%Y-%m-%d")'),
|
|
'<=',
|
|
Carbon::createFromFormat('Y-m-d', $unidade->fim)
|
|
->format('Y-m-d')
|
|
);
|
|
})
|
|
->sum('valor'),
|
|
'quantidade_agendada' => AgendamentoExame::where('exame_id', $exame->exame_id)
|
|
->where(function ($query) {
|
|
$query
|
|
->whereIn(
|
|
'status_id',
|
|
[
|
|
StatusAgendamento::AGENDADO,
|
|
StatusAgendamento::REALIZADO,
|
|
StatusAgendamento::PENDENTE_ASSINATURA,
|
|
StatusAgendamento::ASSINADO,
|
|
StatusAgendamento::PENDENTE_ENTREGA,
|
|
StatusAgendamento::ENTREGUE,
|
|
StatusAgendamento::NOVA_COLETA
|
|
]
|
|
)
|
|
->orWhereNull('status_id');
|
|
})
|
|
->whereHas('agendamento', function ($query) use ($unidade) {
|
|
|
|
$query
|
|
->where(
|
|
DB::raw('DATE_FORMAT(data, "%Y-%m-%d")'),
|
|
'>=',
|
|
Carbon::createFromFormat('Y-m-d', $unidade->inicio)
|
|
->format('Y-m-d')
|
|
)
|
|
->where(
|
|
DB::raw('DATE_FORMAT(data, "%Y-%m-%d")'),
|
|
'<=',
|
|
Carbon::createFromFormat('Y-m-d', $unidade->fim)
|
|
->format('Y-m-d')
|
|
);
|
|
})
|
|
->count('id')
|
|
])
|
|
])
|
|
->sortBy(fn($cota) => $cota['periodo']['start'])
|
|
->sortBy('unidade_solicitante')
|
|
->values()
|
|
];
|
|
}),
|
|
'valor_agendado' => AgendamentoExame::where('exame_id', $request->exame_id)
|
|
->where(function ($query) {
|
|
$query
|
|
->whereIn(
|
|
'status_id',
|
|
[
|
|
StatusAgendamento::AGENDADO,
|
|
StatusAgendamento::REALIZADO,
|
|
StatusAgendamento::PENDENTE_ASSINATURA,
|
|
StatusAgendamento::ASSINADO,
|
|
StatusAgendamento::PENDENTE_ENTREGA,
|
|
StatusAgendamento::ENTREGUE,
|
|
StatusAgendamento::NOVA_COLETA
|
|
]
|
|
)
|
|
->orWhereNull('status_id');
|
|
})
|
|
->whereHas('agendamento', function ($query) use ($request) {
|
|
|
|
$query
|
|
->where(
|
|
DB::raw('DATE_FORMAT(data, "%Y-%m-%d")'),
|
|
'>=',
|
|
Carbon::createFromFormat('Y-m-d', $request->inicio)
|
|
->format('Y-m-d')
|
|
)
|
|
->where(
|
|
DB::raw('DATE_FORMAT(data, "%Y-%m-%d")'),
|
|
'<=',
|
|
Carbon::createFromFormat('Y-m-d', $request->fim)
|
|
->format('Y-m-d')
|
|
)
|
|
->where(
|
|
'laboratorio_infraestrutura_id',
|
|
$request->infraestrutura_id
|
|
);
|
|
|
|
if ($request->solicitante_infraestrutura_id) {
|
|
|
|
$query->where(
|
|
'infraestrutura_id',
|
|
$request->solicitante_infraestrutura_id
|
|
);
|
|
}
|
|
})
|
|
->sum('valor'),
|
|
'quantidade_agendada' => AgendamentoExame::where('exame_id', $request->exame_id)
|
|
->where(function ($query) {
|
|
$query
|
|
->whereIn(
|
|
'status_id',
|
|
[
|
|
StatusAgendamento::AGENDADO,
|
|
StatusAgendamento::REALIZADO,
|
|
StatusAgendamento::PENDENTE_ASSINATURA,
|
|
StatusAgendamento::ASSINADO,
|
|
StatusAgendamento::PENDENTE_ENTREGA,
|
|
StatusAgendamento::ENTREGUE,
|
|
StatusAgendamento::NOVA_COLETA
|
|
]
|
|
)
|
|
->orWhereNull('status_id');
|
|
})
|
|
->whereHas('agendamento', function ($query) use ($request) {
|
|
|
|
$query
|
|
->where(
|
|
DB::raw('DATE_FORMAT(data, "%Y-%m-%d")'),
|
|
'>=',
|
|
Carbon::createFromFormat('Y-m-d', $request->inicio)
|
|
->format('Y-m-d')
|
|
)
|
|
->where(
|
|
DB::raw('DATE_FORMAT(data, "%Y-%m-%d")'),
|
|
'<=',
|
|
Carbon::createFromFormat('Y-m-d', $request->fim)
|
|
->format('Y-m-d')
|
|
)
|
|
->where(
|
|
'laboratorio_infraestrutura_id',
|
|
$request->infraestrutura_id
|
|
);
|
|
|
|
if ($request->solicitante_infraestrutura_id) {
|
|
|
|
$query->where(
|
|
'infraestrutura_id',
|
|
$request->solicitante_infraestrutura_id
|
|
);
|
|
}
|
|
})
|
|
->count('id')
|
|
];
|
|
}
|
|
|
|
public static function saldo_unidade_solicitante($request)
|
|
{
|
|
return [
|
|
'cotas_laboratorio' => Cota::where('unidade_tipo_id', TipoUnidade::LABORATORIO)
|
|
->where('cota_tipo_id', $request->cota_tipo_id)
|
|
->where(function ($query) use ($request) {
|
|
|
|
if ($request->inicio) {
|
|
|
|
$query
|
|
->where(
|
|
DB::raw('DATE_FORMAT(inicio, "%Y-%m-%d")'),
|
|
'>=',
|
|
Carbon::createFromFormat('Y-m-d', $request->inicio)
|
|
->format('Y-m-d')
|
|
)
|
|
->where(
|
|
DB::raw('DATE_FORMAT(fim, "%Y-%m-%d")'),
|
|
'<=',
|
|
Carbon::createFromFormat('Y-m-d', $request->fim)
|
|
->format('Y-m-d')
|
|
);
|
|
}
|
|
|
|
if ($request->infraestrutura_id) {
|
|
|
|
$query->where(
|
|
'infraestrutura_id',
|
|
$request->infraestrutura_id
|
|
);
|
|
}
|
|
})
|
|
->groupBy('unidade_id', 'infraestrutura_id')
|
|
->get()
|
|
->map(function ($query) use ($request) {
|
|
return [
|
|
'unidade_id' => $query->unidade->id ?? null,
|
|
'unidade_tipo_id' => $query->unidade_tipo_id ?? null,
|
|
'infraestrutura_id' => $query->infraestrutura_id ?? null,
|
|
'cota_laboratorio' => Cota::where('unidade_tipo_id', TipoUnidade::LABORATORIO)
|
|
->where('infraestrutura_id', $query->infraestrutura_id)
|
|
->where('cota_tipo_id', $request->cota_tipo_id)
|
|
->where(function ($query) use ($request) {
|
|
|
|
if ($request->inicio) {
|
|
|
|
$query
|
|
->where(
|
|
DB::raw('DATE_FORMAT(inicio, "%Y-%m-%d")'),
|
|
'>=',
|
|
Carbon::createFromFormat('Y-m-d', $request->inicio)
|
|
->format('Y-m-d')
|
|
)
|
|
->where(
|
|
DB::raw('DATE_FORMAT(fim, "%Y-%m-%d")'),
|
|
'<=',
|
|
Carbon::createFromFormat('Y-m-d', $request->fim)
|
|
->format('Y-m-d')
|
|
);
|
|
}
|
|
})
|
|
->get()
|
|
->map(fn($unidade) => [
|
|
'id' => $unidade->id ?? null,
|
|
'unidade_id' => $query->unidade->id ?? null,
|
|
'infraestrutura_id' => $query->infraestrutura_id ?? null,
|
|
'unidade_tipo_id' => $unidade->unidade_tipo_id ?? null,
|
|
'cota_tipo_id' => $unidade->cota_tipo_id ?? null,
|
|
"periodo" => [
|
|
'start' => (new Carbon($unidade->inicio))->format('d/m/Y') ?? null,
|
|
'end' => (new Carbon($unidade->fim))->format('d/m/Y') ?? null,
|
|
],
|
|
'cota' => $unidade->cota_tipo_id == TipoCota::VALOR
|
|
? number_format($unidade->cota_valor, 2, ',', '.')
|
|
: $unidade->cota_quantidade,
|
|
'saldo' => $unidade->cota_tipo_id == TipoCota::VALOR
|
|
? number_format($unidade->cota_valor, 2, ',', '.')
|
|
: $unidade->cota_quantidade,
|
|
'utilizada' => "0",
|
|
'cota_quantidade' => $unidade->cota_quantidade ?? null,
|
|
'cota_valor' => number_format($unidade->cota_valor, 2, ',', '.') ?? null,
|
|
'observacao' => $unidade->observacao ?? null,
|
|
'unidadeSolicitante' => CotaUnidadeSolicitante::where('cota_id', $unidade->id)
|
|
->where(function ($query) use ($request) {
|
|
|
|
if ($request->solicitante_infraestrutura_id) {
|
|
|
|
$query->where(
|
|
'infraestrutura_solicitante_id',
|
|
$request->solicitante_infraestrutura_id
|
|
);
|
|
}
|
|
})
|
|
->get()
|
|
->map(fn($unidadeSolicitante) => [
|
|
'cota_id' => $query->id,
|
|
'cota_id_unidade_solicitante' => $unidadeSolicitante->cota_id_unidade_solicitante
|
|
?? null,
|
|
'unidade_solicitante_id' => $unidadeSolicitante->unidade_solicitante_id ?? null,
|
|
'infraestrutura_solicitante_id' =>
|
|
$unidadeSolicitante->infraestrutura_solicitante_id ?? null,
|
|
'cota_quantidade' => $unidadeSolicitante->cota_quantidade ?? null,
|
|
'cota_valor' => number_format($unidadeSolicitante->cota_valor, 2, ',', '.')
|
|
?? null,
|
|
'cota' => $unidade->cota_tipo_id == TipoCota::VALOR
|
|
? number_format($unidadeSolicitante->cota_valor, 2, ',', '.')
|
|
: $unidadeSolicitante->cota_quantidade,
|
|
]),
|
|
'exames' => CotaExame::where('cota_id', $unidade->id)
|
|
->get()
|
|
->map(fn($exame) => [
|
|
'exame_id' => $exame->exame_id ?? null,
|
|
'cota_exame_periodo_id' => $exame->cota_exame_periodo_id ?? null,
|
|
'cota_quantidade' => $exame->cota_quantidade ?? null,
|
|
'cota_valor' => number_format($exame->cota_valor, 2, ',', '.') ?? null,
|
|
'cota' => $unidade->cota_tipo_id == TipoCota::VALOR
|
|
? number_format($exame->cota_valor, 2, ',', '.')
|
|
: $exame->cota_quantidade,
|
|
'unidadeSolicitante' => CotaExameUnidadeSolicitante::where('cota_id', $unidade->id)
|
|
->where(function ($query) use ($request, $exame) {
|
|
|
|
$query->where('cota_exame_id', $exame->id);
|
|
|
|
if ($request->solicitante_infraestrutura_id) {
|
|
|
|
$query->where(
|
|
'infraestrutura_solicitante_id',
|
|
$request->solicitante_infraestrutura_id
|
|
);
|
|
}
|
|
})
|
|
->get()
|
|
->map(fn($unidadeSolicitante) => [
|
|
'cota_id' => $query->id,
|
|
'cota_exame_id' => $unidadeSolicitante->cota_exame_id ?? null,
|
|
'unidade_solicitante_id' => $unidadeSolicitante->unidade_solicitante_id
|
|
?? null,
|
|
'infraestrutura_solicitante_id' =>
|
|
$unidadeSolicitante->infraestrutura_solicitante_id ?? null,
|
|
'cota_quantidade' => $unidadeSolicitante->cota_quantidade ?? null,
|
|
'cota_valor' =>
|
|
number_format($unidadeSolicitante->cota_valor, 2, ',', '.') ?? null,
|
|
'cota' => $unidade->cota_tipo_id == TipoCota::VALOR
|
|
? number_format($unidadeSolicitante->cota_valor, 2, ',', '.')
|
|
: $unidadeSolicitante->cota_quantidade,
|
|
]),
|
|
])
|
|
])
|
|
->sortBy(fn($cota) => $cota['periodo']['start'])
|
|
->sortBy('unidade_solicitante')
|
|
->values()
|
|
];
|
|
}),
|
|
'cotas_transferencia' => CotaTransferencia::groupBy(
|
|
'laboratorio_unidade_id',
|
|
'laboratorio_infraestrutura_id',
|
|
'origem_unidade_id',
|
|
'origem_infraestrutura_id'
|
|
)
|
|
->get()
|
|
->map(function ($query) {
|
|
return [
|
|
'id' => $query->id ?? null,
|
|
'laboratorio_unidade_id' => $query->laboratorio_unidade_id ?? null,
|
|
'laboratorio_infraestrutura_id' => $query->laboratorio_infraestrutura_id ?? null,
|
|
'laboratorio_nome_fantasia' => $query->laboratorioUnidade->nome_fantasia ?? null,
|
|
'laboratorio_infraestrutura' => $query->laboratorioInfraestrutura->nome ?? null,
|
|
'origem_unidade_id' => $query->origem_unidade_id ?? null,
|
|
'origem_infraestrutura_id' => $query->origem_infraestrutura_id ?? null,
|
|
'origem_nome_fantasia' => $query->origemUnidade->nome_fantasia ?? null,
|
|
'origem_infraestrutura' => $query->origemInfraestrutura->nome ?? null,
|
|
'unidade_tipo_id' => $query->unidade_tipo_id ?? null,
|
|
'destino' => CotaTransferencia::where('laboratorio_unidade_id', $query->laboratorio_unidade_id)
|
|
->where('laboratorio_infraestrutura_id', $query->laboratorio_infraestrutura_id)
|
|
->where('origem_unidade_id', $query->origem_unidade_id)
|
|
->where('origem_infraestrutura_id', $query->origem_infraestrutura_id)
|
|
->get()
|
|
->map(fn($unidade) => [
|
|
'id' => $unidade->id ?? null,
|
|
'cota_id' => $unidade->cota_id ?? null,
|
|
'origem_unidade_id' => $query->origem_unidade_id ?? null,
|
|
'origem_infraestrutura_id' => $query->origem_infraestrutura_id ?? null,
|
|
'origem_nome_fantasia' => $query->origemUnidade->nome_fantasia ?? null,
|
|
'origem_infraestrutura' => $query->origemInfraestrutura->nome ?? null,
|
|
'origem_inicio' => (new Carbon($unidade->origem_inicio))->format('d/m/Y') ?? null,
|
|
'origem_fim' => (new Carbon($unidade->origem_fim))->format('d/m/Y') ?? null,
|
|
'origem_cota_quantidade' => $unidade->origem_cota_quantidade ?? null,
|
|
'origem_cota_valor' => $unidade->origem_cota_valor ?? null,
|
|
'destino_unidade_id' => $unidade->destino_unidade_id ?? null,
|
|
'destino_infraestrutura_id' => $unidade->destino_infraestrutura_id ?? null,
|
|
'destino_nome_fantasia' => $unidade->destinoUnidade->nome_fantasia ?? null,
|
|
'destino_infraestrutura_nome' => $unidade->destinoInfraestrutura->nome ?? null,
|
|
'destino_inicio' => (new Carbon($unidade->destino_inicio))->format('d/m/Y') ?? null,
|
|
'destino_fim' => (new Carbon($unidade->destino_fim))->format('d/m/Y') ?? null,
|
|
'destino_cota_quantidade' => $unidade->destino_cota_quantidade ?? null,
|
|
'destino_cota_valor' => number_format($unidade->destino_cota_valor, 2, ',', '.') ?? null ?? null,
|
|
'origem_cota_tipo_id' => $unidade->cota_tipo_id ?? null,
|
|
'origem_cota_tipo_nome' => $unidade->cota_tipo_id == TipoCota::VALOR
|
|
? 'Valor' : 'Quantidade',
|
|
'exames' => CotaTransferenciaExames::where('cota_transferencia_id', $unidade->id)
|
|
->get()
|
|
->map(fn($exame) => [
|
|
'id' => $exame->id ?? null,
|
|
'exame_id' => $exame->cotaExame->exame_id ?? null,
|
|
'cota_exame_periodo_id' => $exame->cota_exame_id ?? null,
|
|
'nome' => $exame->cotaExame->exame->nome ?? null,
|
|
'cota_quantidade' => $exame->cota_quantidade ?? null,
|
|
'cota_valor' => number_format($exame->cota_valor, 2, ',', '.') ?? null,
|
|
'cota_quantidade_transferido' => $exame->cota_quantidade_transferido ?? null,
|
|
'cota_valor_transferido' => number_format($exame->cota_valor_transferido, 2, ',', '.') ?? null,
|
|
'transferido' => $unidade->cota_tipo_id == TipoCota::VALOR
|
|
? number_format($exame->cota_valor_transferido, 2, ',', '.')
|
|
: $exame->cota_quantidade_transferido,
|
|
])
|
|
])
|
|
->values(),
|
|
];
|
|
}),
|
|
'cotas_unidade_solicitante' => Cota::where('unidade_tipo_id', TipoUnidade::UNIDADE_SOLICITANTE)
|
|
->where('cota_tipo_id', $request->cota_tipo_id)
|
|
->where(function ($query) use ($request) {
|
|
|
|
if ($request->inicio) {
|
|
|
|
$query
|
|
->where(
|
|
DB::raw('DATE_FORMAT(inicio, "%Y-%m-%d")'),
|
|
'>=',
|
|
Carbon::createFromFormat('Y-m-d', $request->inicio)
|
|
->format('Y-m-d')
|
|
)
|
|
->where(
|
|
DB::raw('DATE_FORMAT(fim, "%Y-%m-%d")'),
|
|
'<=',
|
|
Carbon::createFromFormat('Y-m-d', $request->fim)
|
|
->format('Y-m-d')
|
|
);
|
|
}
|
|
})
|
|
->groupBy('unidade_id', 'infraestrutura_id')
|
|
->get()
|
|
->map(function ($query) use ($request) {
|
|
return [
|
|
'unidade_id' => $query->unidade->id ?? null,
|
|
'nome_fantasia' => $query->unidade->nome_fantasia ?? null,
|
|
'unidade_tipo_id' => $query->unidade_tipo_id ?? null,
|
|
'infraestrutura_id' => $query->infraestrutura_id ?? null,
|
|
'infraestrutura_nome' => $query->infraestrutura->nome ?? null,
|
|
'cota_unidade_solicitante' => Cota::where('unidade_tipo_id', TipoUnidade::UNIDADE_SOLICITANTE)
|
|
->where('cota_tipo_id', $request->cota_tipo_id)
|
|
->where('unidade_id', $query->unidade->id)
|
|
->where('infraestrutura_id', $query->infraestrutura_id)
|
|
->where(function ($query) use ($request) {
|
|
|
|
if ($request->inicio) {
|
|
|
|
$query
|
|
->where(
|
|
DB::raw('DATE_FORMAT(inicio, "%Y-%m-%d")'),
|
|
'>=',
|
|
Carbon::createFromFormat('Y-m-d', $request->inicio)
|
|
->format('Y-m-d')
|
|
)
|
|
->where(
|
|
DB::raw('DATE_FORMAT(fim, "%Y-%m-%d")'),
|
|
'<=',
|
|
Carbon::createFromFormat('Y-m-d', $request->fim)
|
|
->format('Y-m-d')
|
|
);
|
|
}
|
|
})
|
|
->orderBy('inicio')
|
|
->get()
|
|
->map(fn($unidade) => [
|
|
'id' => $unidade->id ?? null,
|
|
'unidade_tipo_id' => $unidade->unidade_tipo_id ?? null,
|
|
'cota_tipo_id' => $unidade->cota_tipo_id ?? null,
|
|
'cota_tipo_nome' => $unidade->cota_tipo_id == TipoCota::VALOR
|
|
? 'Valor' : 'Quantidade',
|
|
"periodo" => [
|
|
'start' => (new Carbon($unidade->inicio))->format('d/m/Y') ?? null,
|
|
'end' => (new Carbon($unidade->fim))->format('d/m/Y') ?? null,
|
|
],
|
|
'cota' => $unidade->cota_tipo_id == TipoCota::VALOR
|
|
? number_format($unidade->cota_valor, 2, ',', '.')
|
|
: $unidade->cota_quantidade,
|
|
'saldo' => $unidade->cota_tipo_id == TipoCota::VALOR
|
|
? number_format($unidade->cota_valor, 2, ',', '.')
|
|
: $unidade->cota_quantidade,
|
|
'utilizada' => "0",
|
|
'cota_quantidade' => $unidade->cota_quantidade ?? null,
|
|
'cota_valor' => number_format($unidade->cota_valor, 2, ',', '.') ?? null,
|
|
'observacao' => $unidade->observacao ?? null,
|
|
])
|
|
->values(),
|
|
];
|
|
}),
|
|
'valor_agendado' => AgendamentoExame::where(function ($query) {
|
|
|
|
$query
|
|
->whereIn(
|
|
'status_id',
|
|
[
|
|
StatusAgendamento::AGENDADO,
|
|
StatusAgendamento::REALIZADO,
|
|
StatusAgendamento::PENDENTE_ASSINATURA,
|
|
StatusAgendamento::ASSINADO,
|
|
StatusAgendamento::PENDENTE_ENTREGA,
|
|
StatusAgendamento::ENTREGUE,
|
|
StatusAgendamento::NOVA_COLETA
|
|
]
|
|
)
|
|
->orWhereNull('status_id');
|
|
})
|
|
->whereHas('agendamento', function ($query) use ($request) {
|
|
|
|
if ($request->inicio) {
|
|
|
|
$query
|
|
->where(
|
|
DB::raw('DATE_FORMAT(data, "%Y-%m-%d")'),
|
|
'>=',
|
|
Carbon::createFromFormat('Y-m-d', $request->inicio)
|
|
->format('Y-m-d')
|
|
)
|
|
->where(
|
|
DB::raw('DATE_FORMAT(data, "%Y-%m-%d")'),
|
|
'<=',
|
|
Carbon::createFromFormat('Y-m-d', $request->fim)
|
|
->format('Y-m-d')
|
|
);
|
|
}
|
|
|
|
if ($request->infraestrutura_id) {
|
|
|
|
$query->where(
|
|
'laboratorio_infraestrutura_id',
|
|
$request->infraestrutura_id
|
|
);
|
|
}
|
|
|
|
if ($request->solicitante_infraestrutura_id) {
|
|
|
|
$query->where(
|
|
'infraestrutura_id',
|
|
$request->solicitante_infraestrutura_id
|
|
);
|
|
}
|
|
})
|
|
->sum('valor'),
|
|
'quantidade_agendada' => Agendamento::where(function ($query) use ($request) {
|
|
|
|
if ($request->inicio) {
|
|
|
|
$query
|
|
->where(
|
|
DB::raw('DATE_FORMAT(data, "%Y-%m-%d")'),
|
|
'>=',
|
|
Carbon::createFromFormat('Y-m-d', $request->inicio)
|
|
->format('Y-m-d')
|
|
)
|
|
->where(
|
|
DB::raw('DATE_FORMAT(data, "%Y-%m-%d")'),
|
|
'<=',
|
|
Carbon::createFromFormat('Y-m-d', $request->fim)
|
|
->format('Y-m-d')
|
|
);
|
|
}
|
|
|
|
if ($request->infraestrutura_id) {
|
|
|
|
$query->where(
|
|
'laboratorio_infraestrutura_id',
|
|
$request->infraestrutura_id
|
|
);
|
|
}
|
|
|
|
if ($request->solicitante_infraestrutura_id) {
|
|
|
|
$query->where(
|
|
'infraestrutura_id',
|
|
$request->solicitante_infraestrutura_id
|
|
);
|
|
}
|
|
})
|
|
->whereHas('agendamentoExame', function ($query) {
|
|
$query
|
|
->whereIn(
|
|
'status_id',
|
|
[
|
|
StatusAgendamento::AGENDADO,
|
|
StatusAgendamento::REALIZADO,
|
|
StatusAgendamento::PENDENTE_ASSINATURA,
|
|
StatusAgendamento::ASSINADO,
|
|
StatusAgendamento::PENDENTE_ENTREGA,
|
|
StatusAgendamento::ENTREGUE,
|
|
StatusAgendamento::NOVA_COLETA
|
|
]
|
|
)
|
|
->orWhereNull('status_id');
|
|
})
|
|
->count('id')
|
|
];
|
|
}
|
|
|
|
public static function store(Request $request)
|
|
{
|
|
DB::transaction(function () use ($request) {
|
|
|
|
self::validate($request);
|
|
|
|
self::destroyCota($request);
|
|
self::saveUnidadeSolicitante($request);
|
|
self::saveUnidade($request);
|
|
self::saveMotivo($request);
|
|
self::saveCotaUnidadeSolicitante($request);
|
|
self::saveCotaExamePeriodo($request);
|
|
self::saveCotaLaboratorio($request);
|
|
self::saveMensagemProtocolo($request);
|
|
self::saveImpressaoAgendamento($request);
|
|
self::saveWebService($request);
|
|
self::saveReagendar($request);
|
|
self::saveExameAmbulatorioAtendimento($request);
|
|
self::saveIntegracaoAplicativo($request);
|
|
self::saveAppCidadaoLaboratorio($request);
|
|
self::saveAppCidadaoExame($request);
|
|
self::saveAppCidadaoUnidadeSolicitante($request);
|
|
self::saveSupervisores($request);
|
|
self::saveCotaTransferencia($request);
|
|
self::saveUnidadeFaturamento($request);
|
|
self::saveLaboratorioFaturamento($request);
|
|
self::saveProfissionalAssinatura($request);
|
|
});
|
|
|
|
return response()->json(null, Response::HTTP_OK);
|
|
}
|
|
|
|
public static function destroy($id)
|
|
{
|
|
|
|
return CotaExamePeriodo::destroy(CotaExamePeriodo::where('id', $id)->pluck('id'));
|
|
}
|
|
|
|
|
|
private static function saveUnidade($request)
|
|
{
|
|
$ids = [];
|
|
|
|
foreach ($request->unidades as $params) {
|
|
|
|
$tipo = ExameLaboratorial::where('unidade_id', $params['unidade_id'])
|
|
->where('infraestrutura_id', $params['infraestrutura_id'])
|
|
->first() ?? new ExameLaboratorial();
|
|
|
|
$tipo->fill($params);
|
|
$tipo->save();
|
|
|
|
$ids[] = $tipo->id;
|
|
|
|
if ($params['unidade_coleta'] === true) {
|
|
|
|
self::saveUnidadeRelacionada(
|
|
$params['unidade_coleta_solicitante'],
|
|
TipoUnidade::COLETA,
|
|
$params['unidade_id'],
|
|
$params['infraestrutura_id']
|
|
);
|
|
} else {
|
|
|
|
UnidadeSolicitante::destroy(UnidadeSolicitante::where('unidade_id', $params['unidade_id'])
|
|
->where('infraestrutura_id', $params['infraestrutura_id'])
|
|
->where('unidade_tipo_id', TipoUnidade::COLETA)
|
|
->pluck('id'));
|
|
}
|
|
}
|
|
|
|
ExameLaboratorial::destroy(ExameLaboratorial::whereNotIn('id', $ids)->pluck('id'));
|
|
}
|
|
|
|
private static function saveUnidadeSolicitante($request)
|
|
{
|
|
$ids = [];
|
|
|
|
foreach ($request->unidades_solicitantes as $params) {
|
|
|
|
$examesIds = [];
|
|
|
|
$unidade = UnidadeSolicitanteExame::where('unidade_id', $params['unidade_id'])
|
|
->where('infraestrutura_id', $params['infraestrutura_id'])
|
|
->first() ?? new UnidadeSolicitanteExame();
|
|
|
|
$unidade->unidade_id = $params['unidade_id'];
|
|
$unidade->infraestrutura_id = $params['infraestrutura_id'];
|
|
$unidade->urgencia = $params['urgencia'] ?? false;
|
|
$unidade->save();
|
|
|
|
$ids[] = $unidade->id;
|
|
|
|
if ($params['alterado']) {
|
|
|
|
foreach ($params['exames'] as $exames) {
|
|
|
|
$exame = UnidadeSolicitanteExameItem::where('unidade_solicitante_exame_id', $unidade->id)
|
|
->where('exame_id', $exames['exame_id'])
|
|
->first() ?? new UnidadeSolicitanteExameItem();
|
|
|
|
$exame->unidade_solicitante_exame_id = $unidade->id;
|
|
$exame->exame_id = $exames['exame_id'];
|
|
$exame->save();
|
|
|
|
$examesIds[] = $exame->id;
|
|
}
|
|
|
|
UnidadeSolicitanteExameItem::destroy(UnidadeSolicitanteExameItem::whereNotIn('id', $examesIds)
|
|
->where('unidade_solicitante_exame_id', $unidade->id)->pluck('id'));
|
|
}
|
|
}
|
|
|
|
UnidadeSolicitanteExameItem::destroy(UnidadeSolicitanteExameItem::whereNotIn('unidade_solicitante_exame_id', $ids)->pluck('id'));
|
|
UnidadeSolicitanteExame::destroy(UnidadeSolicitanteExame::whereNotIn('id', $ids)->pluck('id'));
|
|
}
|
|
|
|
private static function getUnidadesFaturamento()
|
|
{
|
|
|
|
$relacionadas = UnidadeFaturamentoUnidade::select('infraestrutura_id')->distinct('infraestrutura_id')->get();
|
|
|
|
return $relacionadas;
|
|
}
|
|
|
|
private static function getLaboratoriosFaturamento()
|
|
{
|
|
|
|
$relacionadas = LaboratorioFaturaUnidade::select('infraestrutura_id')->distinct('infraestrutura_id')->get();
|
|
|
|
return $relacionadas;
|
|
}
|
|
|
|
public static function getUnidadeFaturamento()
|
|
{
|
|
|
|
$unidade = unidadeFaturamento::where('infraestrutura_id', request()->infraestrutura_id)->first();
|
|
|
|
return [
|
|
'profissionais' => $unidade->unidadeFaturamentoUsuario
|
|
->map(function ($query) {
|
|
return [
|
|
'usuario_id' => $query->usuario_id,
|
|
'usuario_nome' => $query->usuario->nome,
|
|
'ocupacao_id' => $query->ocupacao_id,
|
|
'ocupacao_codigo_nome' => $query->ocupacao->codigo_nome,
|
|
];
|
|
}),
|
|
'faturamento_relacionado' => $unidade->unidadeFaturamentoUnidade
|
|
->map(function ($relacionada) {
|
|
return [
|
|
'id' => $relacionada->id,
|
|
'relacionada_unidade_id' => $relacionada->unidade_id,
|
|
'relacionada_infraestrutura_id' => $relacionada->infraestrutura_id,
|
|
'unidade_relacionada' => $relacionada->infraestrutura->unidade->nome_fantasia,
|
|
'infraestrutura_nome' => $relacionada->infraestrutura->nome,
|
|
];
|
|
})
|
|
];
|
|
}
|
|
|
|
public static function getLaboratorioFaturamento()
|
|
{
|
|
|
|
$unidade = LaboratorioFatura::where('infraestrutura_id', request()->infraestrutura_id)->first();
|
|
|
|
return [
|
|
'profissionais' => $unidade->laboratorioFaturaUsuario
|
|
->map(function ($query) {
|
|
return [
|
|
'usuario_id' => $query->usuario_id,
|
|
'usuario_nome' => $query->usuario->nome,
|
|
'ocupacao_id' => $query->ocupacao_id,
|
|
'ocupacao_codigo_nome' => $query->ocupacao->codigo_nome,
|
|
];
|
|
}),
|
|
'faturamento_relacionado' => $unidade->laboratorioFaturaUnidade
|
|
->map(function ($relacionada) {
|
|
return [
|
|
'id' => $relacionada->id,
|
|
'relacionada_unidade_id' => $relacionada->unidade_id,
|
|
'relacionada_infraestrutura_id' => $relacionada->infraestrutura_id,
|
|
'unidade_relacionada' => $relacionada->infraestrutura->unidade->nome_fantasia,
|
|
'infraestrutura_nome' => $relacionada->infraestrutura->nome,
|
|
];
|
|
})
|
|
];
|
|
}
|
|
|
|
private static function saveUnidadeFaturamento($request)
|
|
{
|
|
$ids = [];
|
|
|
|
foreach ($request->unidades_faturamento as $params) {
|
|
|
|
$idsRelacionado = [];
|
|
$idsUsuario = [];
|
|
|
|
$unidade = UnidadeFaturamento::where('infraestrutura_id', $params['infraestrutura_id'])
|
|
->first() ?? new UnidadeFaturamento();
|
|
|
|
$unidade->infraestrutura_id = $params['infraestrutura_id'];
|
|
$unidade->unidade_id = $params['unidade_id'];
|
|
$unidade->save();
|
|
|
|
$ids[] = $unidade->id;
|
|
|
|
if ($params['alterado']) {
|
|
|
|
foreach ($params['faturamento_relacionado'] as $params_relacionado) {
|
|
|
|
$relacionado = UnidadeFaturamentoUnidade::where('unidade_faturamento_automatico_id', $unidade->id)
|
|
->where('infraestrutura_id', $params_relacionado['relacionada_infraestrutura_id'])
|
|
->first() ?? new UnidadeFaturamentoUnidade();
|
|
|
|
$relacionado->infraestrutura_id = $params_relacionado['relacionada_infraestrutura_id'];
|
|
$relacionado->unidade_faturamento_automatico_id = $unidade->id;
|
|
$relacionado->save();
|
|
|
|
$idsRelacionado[] = $relacionado->id;
|
|
}
|
|
|
|
foreach ($params['profissionais'] as $params_profissional) {
|
|
|
|
$usuario = UnidadeFaturamentoUsuario::where('unidade_faturamento_automatico_id', $unidade->id)
|
|
->where('usuario_id', $params_profissional['usuario_id'])
|
|
->first() ?? new UnidadeFaturamentoUsuario();
|
|
|
|
$usuario->usuario_id = $params_profissional['usuario_id'];
|
|
$usuario->ocupacao_id = $params_profissional['ocupacao_id'];
|
|
$usuario->unidade_faturamento_automatico_id = $unidade->id;
|
|
$usuario->save();
|
|
|
|
$idsUsuario[] = $usuario->id;
|
|
}
|
|
|
|
UnidadeFaturamentoUnidade::destroy(UnidadeFaturamentoUnidade::where('unidade_faturamento_automatico_id', $unidade->id)
|
|
->whereNotIn('id', $idsRelacionado)->pluck('id'));
|
|
|
|
UnidadeFaturamentoUsuario::destroy(UnidadeFaturamentoUsuario::where('unidade_faturamento_automatico_id', $unidade->id)
|
|
->whereNotIn('id', $idsUsuario)->pluck('id'));
|
|
}
|
|
}
|
|
|
|
UnidadeFaturamentoUnidade::destroy(UnidadeFaturamentoUnidade::whereNotIn('unidade_faturamento_automatico_id', $ids)
|
|
->pluck('id'));
|
|
|
|
UnidadeFaturamentoUsuario::destroy(UnidadeFaturamentoUsuario::whereNotIn('unidade_faturamento_automatico_id', $ids)
|
|
->pluck('id'));
|
|
|
|
UnidadeFaturamento::destroy(UnidadeFaturamento::whereNotIn('id', $ids)->pluck('id'));
|
|
}
|
|
|
|
private static function saveLaboratorioFaturamento($request)
|
|
{
|
|
$ids = [];
|
|
|
|
foreach ($request->laboratorios_faturamento as $params) {
|
|
|
|
$idsRelacionado = [];
|
|
$idsUsuario = [];
|
|
|
|
$unidade = LaboratorioFatura::where('infraestrutura_id', $params['infraestrutura_id'])
|
|
->first() ?? new LaboratorioFatura();
|
|
|
|
$unidade->infraestrutura_id = $params['infraestrutura_id'];
|
|
$unidade->unidade_id = $params['unidade_id'];
|
|
$unidade->save();
|
|
|
|
$ids[] = $unidade->id;
|
|
|
|
if ($params['alterado']) {
|
|
|
|
foreach ($params['faturamento_relacionado'] as $params_relacionado) {
|
|
|
|
$relacionado = LaboratorioFaturaUnidade::where('laboratorio_fatura_automatico_id', $unidade->id)
|
|
->where('infraestrutura_id', $params_relacionado['relacionada_infraestrutura_id'])
|
|
->first() ?? new LaboratorioFaturaUnidade();
|
|
|
|
$relacionado->infraestrutura_id = $params_relacionado['relacionada_infraestrutura_id'];
|
|
$relacionado->laboratorio_fatura_automatico_id = $unidade->id;
|
|
$relacionado->save();
|
|
|
|
$idsRelacionado[] = $relacionado->id;
|
|
}
|
|
|
|
foreach ($params['profissionais'] as $params_profissional) {
|
|
|
|
$usuario = LaboratorioFaturaUsuario::where('laboratorio_fatura_automatico_id', $unidade->id)
|
|
->where('usuario_id', $params_profissional['usuario_id'])
|
|
->first() ?? new LaboratorioFaturaUsuario();
|
|
|
|
$usuario->usuario_id = $params_profissional['usuario_id'];
|
|
$usuario->ocupacao_id = $params_profissional['ocupacao_id'];
|
|
$usuario->laboratorio_fatura_automatico_id = $unidade->id;
|
|
$usuario->save();
|
|
|
|
$idsUsuario[] = $usuario->id;
|
|
}
|
|
|
|
LaboratorioFaturaUnidade::destroy(LaboratorioFaturaUnidade::where('laboratorio_fatura_automatico_id', $unidade->id)
|
|
->whereNotIn('id', $idsRelacionado)->pluck('id'));
|
|
|
|
LaboratorioFaturaUsuario::destroy(LaboratorioFaturaUsuario::where('laboratorio_fatura_automatico_id', $unidade->id)
|
|
->whereNotIn('id', $idsUsuario)->pluck('id'));
|
|
}
|
|
}
|
|
|
|
LaboratorioFaturaUnidade::destroy(LaboratorioFaturaUnidade::whereNotIn('laboratorio_fatura_automatico_id', $ids)
|
|
->pluck('id'));
|
|
|
|
LaboratorioFaturaUsuario::destroy(LaboratorioFaturaUsuario::whereNotIn('laboratorio_fatura_automatico_id', $ids)
|
|
->pluck('id'));
|
|
|
|
LaboratorioFatura::destroy(LaboratorioFatura::whereNotIn('id', $ids)->pluck('id'));
|
|
}
|
|
|
|
private static function saveMotivo($request)
|
|
{
|
|
$ids = [];
|
|
|
|
foreach ($request->motivos as $params) {
|
|
|
|
$motivo = Motivo::where('id', $params['id'])
|
|
->first() ?? new Motivo();
|
|
|
|
$motivo->fill($params);
|
|
$motivo->save();
|
|
|
|
$ids[] = $motivo->id;
|
|
}
|
|
|
|
Motivo::destroy(Motivo::whereNotIn('id', $ids)->pluck('id'));
|
|
}
|
|
|
|
private static function saveCotaExamePeriodo($request)
|
|
{
|
|
|
|
foreach ($request->cotas_exame_periodo as $exames) {
|
|
|
|
$ids = [];
|
|
|
|
if ($exames['alterado']) {
|
|
|
|
foreach ($exames['exames'] as $params) {
|
|
|
|
$id = $params['id'];
|
|
$unidade = $params['unidade_id'];
|
|
$infraestrutura = $params['infraestrutura_id'];
|
|
$exame_id = $params['exame_id'];
|
|
$cota_quantidade = $params['cota_quantidade'];
|
|
$cota_valor = $params['cota_valor'];
|
|
$observacao = $params['observacao'];
|
|
$cota_tipo_id = $params['cota_tipo_id'];
|
|
$inicio = $exames['periodo']['start'];
|
|
$fim = $exames['periodo']['end'];
|
|
|
|
if ($id && is_numeric($id)) {
|
|
|
|
$tipo = CotaExamePeriodo::where('id', $id)->first();
|
|
} else {
|
|
|
|
$tipo = CotaExamePeriodo::where('unidade_id', $unidade)
|
|
->where('infraestrutura_id', $infraestrutura)
|
|
->where('exame_id', $exame_id)
|
|
->where('cota_tipo_id', $cota_tipo_id)
|
|
->where('inicio', Carbon::createFromFormat('d/m/Y', $inicio)->format('Y-m-d'))
|
|
->where('fim', Carbon::createFromFormat('d/m/Y', $fim)->format('Y-m-d'))
|
|
->first() ?? new CotaExamePeriodo();
|
|
|
|
$id = $tipo->id;
|
|
}
|
|
|
|
self::validateDuplicatedExames(
|
|
$id,
|
|
$unidade,
|
|
$infraestrutura,
|
|
$exame_id,
|
|
$cota_tipo_id,
|
|
$inicio,
|
|
$fim
|
|
);
|
|
|
|
$tipo->unidade_id = $unidade;
|
|
$tipo->infraestrutura_id = $infraestrutura;
|
|
$tipo->exame_id = $exame_id;
|
|
$tipo->cota_quantidade = $cota_quantidade;
|
|
$tipo->cota_valor = $cota_valor;
|
|
$tipo->cota_tipo_id = $cota_tipo_id;
|
|
$tipo->observacao = $observacao;
|
|
$tipo->inicio = Carbon::createFromFormat('d/m/Y', $inicio);
|
|
$tipo->fim = Carbon::createFromFormat('d/m/Y', $fim);
|
|
|
|
if (!is_null($params['cota_valor'])) {
|
|
$tipo->cota_valor = str_replace('.', '', $tipo->cota_valor);
|
|
$tipo->cota_valor = str_replace(',', '.', $tipo->cota_valor);
|
|
}
|
|
|
|
$tipo->save();
|
|
$ids[] = $tipo->id;
|
|
}
|
|
|
|
CotaExamePeriodo::destroy(CotaExamePeriodo::where('infraestrutura_id', $infraestrutura)
|
|
->where('cota_tipo_id', $cota_tipo_id)
|
|
->where('inicio', Carbon::createFromFormat('d/m/Y', $inicio)->format('Y-m-d'))
|
|
->whereNotIn('id', $ids)
|
|
->pluck('id'));
|
|
}
|
|
}
|
|
}
|
|
|
|
private static function saveUnidadeRelacionada($request, $categoria, $unidade_id, $infraestrutura_id)
|
|
{
|
|
$ids = [];
|
|
|
|
foreach ($request as $params) {
|
|
|
|
$tipo = UnidadeSolicitante::where('unidade_id', $params['unidade_id'])
|
|
->where('unidade_solicitante_id', $params['unidade_solicitante_id'])
|
|
->where('infraestrutura_id', $params['infraestrutura_id'])
|
|
->where('unidade_solicitante_infraestrutura_id', $params['unidade_solicitante_infraestrutura_id'])
|
|
->where('unidade_tipo_id', $categoria)
|
|
->first() ?? new UnidadeSolicitante();
|
|
|
|
$tipo->fill($params);
|
|
$tipo->save();
|
|
|
|
$ids[] = $tipo->id;
|
|
}
|
|
|
|
UnidadeSolicitante::destroy(UnidadeSolicitante::where('unidade_id', $unidade_id)
|
|
->where('infraestrutura_id', $infraestrutura_id)
|
|
->where('unidade_tipo_id', $categoria)
|
|
->whereNotIn('id', $ids)
|
|
->pluck('id'));
|
|
}
|
|
|
|
private static function saveCotaUnidadeSolicitante($request)
|
|
{
|
|
foreach ($request->cotas_unidade_solicitante as $params) {
|
|
|
|
if (isset($params['cota_unidade_solicitante'])) {
|
|
|
|
foreach ($params['cota_unidade_solicitante'] as $param_unidade) {
|
|
|
|
$id = $param_unidade['id'];
|
|
$unidade = $params['unidade_id'];
|
|
$infraestrutura = $params['infraestrutura_id'];
|
|
$cota_tipo_id = $param_unidade['cota_tipo_id'];
|
|
$unidade_tipo_id = $param_unidade['unidade_tipo_id'];
|
|
$inicio = $param_unidade['periodo']['start'];
|
|
$fim = $param_unidade['periodo']['end'];
|
|
|
|
if ($id && is_numeric($id)) {
|
|
|
|
$tipo = Cota::where('id', $id)->first();
|
|
} else {
|
|
|
|
$tipo = Cota::where('unidade_id', $unidade)
|
|
->where('infraestrutura_id', $infraestrutura)
|
|
->where('unidade_tipo_id', $unidade_tipo_id)
|
|
->where('cota_tipo_id', $cota_tipo_id)
|
|
->where('inicio', Carbon::createFromFormat('d/m/Y', $inicio)->format('Y-m-d'))
|
|
->where('fim', Carbon::createFromFormat('d/m/Y', $fim)->format('Y-m-d'))
|
|
->first() ?? new Cota();
|
|
|
|
$id = $tipo->id;
|
|
}
|
|
|
|
if ($cota_tipo_id == TipoCota::VALOR) {
|
|
|
|
$totalUnidadeSolicitante = Cota::where('unidade_id', $unidade)
|
|
->where('infraestrutura_id', $infraestrutura)
|
|
->where('unidade_tipo_id', $unidade_tipo_id)
|
|
->where('cota_tipo_id', $cota_tipo_id)
|
|
->whereYear('inicio', '=', Carbon::createFromFormat('d/m/Y', $inicio)->format('Y'))
|
|
->whereMonth('inicio', '=', Carbon::createFromFormat('d/m/Y', $inicio)->format('m'))
|
|
->sum('cota_valor') ?? 0;
|
|
|
|
$saldo = CotaExameUnidadeSolicitante::where('unidade_solicitante_id', $unidade)
|
|
->where('infraestrutura_solicitante_id', $infraestrutura)
|
|
->sum('cota_valor');
|
|
|
|
$valor = $param_unidade['cota_valor'];
|
|
$valor = str_replace('.', '', $valor);
|
|
$valor = str_replace(',', '.', $valor);
|
|
$restante = $totalUnidadeSolicitante - $tipo->cota_valor;
|
|
} else {
|
|
|
|
$totalUnidadeSolicitante = Cota::where('unidade_id', $unidade)
|
|
->where('infraestrutura_id', $infraestrutura)
|
|
->where('unidade_tipo_id', $unidade_tipo_id)
|
|
->where('cota_tipo_id', $cota_tipo_id)
|
|
->whereYear('inicio', '=', Carbon::createFromFormat('d/m/Y', $inicio)->format('Y'))
|
|
->whereMonth('inicio', '=', Carbon::createFromFormat('d/m/Y', $inicio)->format('m'))
|
|
->sum('cota_quantidade') ?? 0;
|
|
|
|
$saldo = CotaExameUnidadeSolicitante::where('unidade_solicitante_id', $unidade)
|
|
->where('infraestrutura_solicitante_id', $infraestrutura)
|
|
->sum('cota_quantidade');
|
|
|
|
$valor = $param_unidade['cota_quantidade'];
|
|
$restante = $totalUnidadeSolicitante - $tipo->cota_valor;
|
|
}
|
|
|
|
if ($cota_tipo_id == TipoCota::VALOR) {
|
|
|
|
$totalUnidadeSolicitante = Cota::where('unidade_id', $unidade)
|
|
->where('infraestrutura_id', $infraestrutura)
|
|
->where('unidade_tipo_id', $unidade_tipo_id)
|
|
->where('cota_tipo_id', $cota_tipo_id)
|
|
->whereYear('inicio', '=', Carbon::createFromFormat('d/m/Y', $inicio)->format('Y'))
|
|
->whereMonth('inicio', '=', Carbon::createFromFormat('d/m/Y', $inicio)->format('m'))
|
|
->sum('cota_valor') ?? 0;
|
|
|
|
$saldo = CotaUnidadeSolicitante::where('unidade_solicitante_id', $unidade)
|
|
->where('infraestrutura_solicitante_id', $infraestrutura)
|
|
->where('cota_id_unidade_solicitante', $tipo->id)
|
|
->sum('cota_valor') ?? 0;
|
|
|
|
$valor = $param_unidade['cota_valor'];
|
|
$valor = str_replace('.', '', $valor);
|
|
$valor = str_replace(',', '.', $valor);
|
|
$restante = $totalUnidadeSolicitante - $tipo->cota_valor;
|
|
} else {
|
|
|
|
$totalUnidadeSolicitante = Cota::where('unidade_id', $unidade)
|
|
->where('infraestrutura_id', $infraestrutura)
|
|
->where('unidade_tipo_id', $unidade_tipo_id)
|
|
->where('cota_tipo_id', $cota_tipo_id)
|
|
->whereYear('inicio', '=', Carbon::createFromFormat('d/m/Y', $inicio)->format('Y'))
|
|
->whereMonth('inicio', '=', Carbon::createFromFormat('d/m/Y', $inicio)->format('m'))
|
|
->sum('cota_valor') ?? 0;
|
|
|
|
$saldo = CotaUnidadeSolicitante::where('unidade_solicitante_id', $unidade)
|
|
->where('infraestrutura_solicitante_id', $infraestrutura)
|
|
->where('cota_id_unidade_solicitante', $tipo->id)
|
|
->sum('cota_quantidade') ?? 0;
|
|
|
|
$valor = $param_unidade['cota_quantidade'];
|
|
$restante = $totalUnidadeSolicitante - $tipo->cota_valor;
|
|
}
|
|
|
|
self::validateDuplicatedItems(
|
|
$id,
|
|
$unidade,
|
|
$infraestrutura,
|
|
$unidade_tipo_id,
|
|
$cota_tipo_id,
|
|
$inicio,
|
|
$fim
|
|
);
|
|
|
|
if (($restante + $valor) >= $saldo) {
|
|
|
|
$tipo->fill($param_unidade);
|
|
|
|
$tipo->id = $id;
|
|
$tipo->unidade_id = $unidade;
|
|
$tipo->infraestrutura_id = $infraestrutura;
|
|
$tipo->inicio = Carbon::createFromFormat('d/m/Y', $inicio);
|
|
$tipo->fim = Carbon::createFromFormat('d/m/Y', $fim);
|
|
|
|
$tipo->cota_valor = $param_unidade['cota_valor'];
|
|
|
|
if (!is_null($param_unidade['cota_valor'])) {
|
|
$tipo->cota_valor = str_replace('.', '', $tipo->cota_valor);
|
|
$tipo->cota_valor = str_replace(',', '.', $tipo->cota_valor);
|
|
}
|
|
|
|
$tipo->save();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private static function saveSupervisores($request)
|
|
{
|
|
|
|
$ids = [];
|
|
|
|
foreach ($request->supervisores as $params) {
|
|
|
|
$supervisor = Supervisor::where('usuario_id', $params['usuario_id'])
|
|
->first() ?? new Supervisor();
|
|
|
|
$supervisor->fill($params);
|
|
|
|
$supervisor->save();
|
|
|
|
$ids[] = $supervisor->id;
|
|
}
|
|
|
|
Supervisor::destroy(Supervisor::whereNotIn('id', $ids)->pluck('id'));
|
|
}
|
|
|
|
private static function saveProfissionalAssinatura($request)
|
|
{
|
|
|
|
$ids = [];
|
|
|
|
foreach ($request->profissionais_assinatura as $params) {
|
|
|
|
$profissional = ProfissionalAssinatura::where('usuario_id', $params['usuario_id'])
|
|
->where('unidade_id', $params['unidade_id'])
|
|
->where('infraestrutura_id', $params['infraestrutura_id'])
|
|
->first() ?? new ProfissionalAssinatura();
|
|
|
|
$profissional->fill($params);
|
|
|
|
$profissional->save();
|
|
|
|
$ids[] = $profissional->id;
|
|
}
|
|
|
|
ProfissionalAssinatura::destroy(ProfissionalAssinatura::whereNotIn('id', $ids)->pluck('id'));
|
|
}
|
|
|
|
private static function destroyCota($request)
|
|
{
|
|
$idsExcluirUnidade = [];
|
|
|
|
foreach ($request->cotas_unidade_solicitante as $params) {
|
|
|
|
if (isset($params['cota_unidade_solicitante'])) {
|
|
|
|
foreach ($params['cota_unidade_solicitante'] as $param_unidade) {
|
|
|
|
if ($param_unidade['id']) {
|
|
|
|
$idsExcluirUnidade[] = $param_unidade['id'];
|
|
} else {
|
|
|
|
$cota = Cota::where('unidade_id', $params['unidade_id'])
|
|
->where('infraestrutura_id', $params['infraestrutura_id'])
|
|
->where('unidade_tipo_id', $param_unidade['unidade_tipo_id'])
|
|
->where('cota_tipo_id', $param_unidade['cota_tipo_id'])
|
|
->where('inicio', Carbon::createFromFormat('d/m/Y', $param_unidade['periodo']['start'])
|
|
->format('Y-m-d'))
|
|
->where('fim', Carbon::createFromFormat('d/m/Y', $param_unidade['periodo']['end'])
|
|
->format('Y-m-d'))
|
|
->first();
|
|
|
|
if ($cota) {
|
|
|
|
$idsExcluirUnidade[] = $cota->id;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Cota::destroy(Cota::where('unidade_tipo_id', TipoUnidade::UNIDADE_SOLICITANTE)
|
|
->whereNotIn('id', $idsExcluirUnidade)
|
|
->pluck('id'));
|
|
}
|
|
|
|
public static function getExamesUnidadeSolicitante()
|
|
{
|
|
|
|
return UnidadeSolicitanteExameItem::where('unidade_solicitante_exame_id', request()->id)
|
|
->get()
|
|
->map(fn($exame) => [
|
|
'id' => $exame->id ?? null,
|
|
'exame_id' => $exame->exame_id ?? null,
|
|
'nome' => $exame->exame->nome ?? null,
|
|
]);
|
|
}
|
|
|
|
private static function saveCotaLaboratorio($request)
|
|
{
|
|
$idsCota = [];
|
|
|
|
foreach ($request->cotas_laboratorio as $params) {
|
|
|
|
if ($params['alterado']) {
|
|
|
|
foreach ($params['cota_laboratorio'] as $param_laboratorio) {
|
|
|
|
$idsUnidade = [];
|
|
$idsExame = [];
|
|
|
|
$unidade = $params['unidade_id'];
|
|
$infraestrutura = $params['infraestrutura_id'];
|
|
$cota_tipo_id = $param_laboratorio['cota_tipo_id'];
|
|
$unidade_tipo_id = $param_laboratorio['unidade_tipo_id'];
|
|
$inicio = $param_laboratorio['periodo']['start'];
|
|
$fim = $param_laboratorio['periodo']['end'];
|
|
|
|
$ano = date('Y', strtotime($inicio));
|
|
|
|
if ($param_laboratorio['id'] && is_numeric($param_laboratorio['id'])) {
|
|
|
|
$cota = Cota::where('id', $param_laboratorio['id'])->first();
|
|
} else {
|
|
|
|
$cota = Cota::where('infraestrutura_id', $infraestrutura)
|
|
->where('unidade_tipo_id', $unidade_tipo_id)
|
|
->where('cota_tipo_id', $cota_tipo_id)
|
|
->where('inicio', Carbon::createFromFormat('d/m/Y', $inicio)->format('Y-m-d'))
|
|
->where('fim', Carbon::createFromFormat('d/m/Y', $fim)->format('Y-m-d'))
|
|
->first() ?? new Cota();
|
|
}
|
|
|
|
$id = $cota->id;
|
|
|
|
self::validateDuplicatedItems(
|
|
$id,
|
|
$unidade,
|
|
$infraestrutura,
|
|
$unidade_tipo_id,
|
|
$cota_tipo_id,
|
|
$inicio,
|
|
$fim
|
|
);
|
|
|
|
$cota->fill($param_laboratorio);
|
|
|
|
$cota->id = $id;
|
|
$cota->unidade_id = $params['unidade_id'];
|
|
$cota->infraestrutura_id = $params['infraestrutura_id'];
|
|
$cota->inicio = Carbon::createFromFormat('d/m/Y', $inicio);
|
|
$cota->fim = Carbon::createFromFormat('d/m/Y', $fim);
|
|
|
|
$cota->cota_valor = $param_laboratorio['cota_valor'];
|
|
|
|
if (!is_null($param_laboratorio['cota_valor'])) {
|
|
$cota->cota_valor = str_replace('.', '', $cota->cota_valor);
|
|
$cota->cota_valor = str_replace(',', '.', $cota->cota_valor);
|
|
}
|
|
|
|
$cota->save();
|
|
$idsCota[] = $cota->id;
|
|
$cota_id = $cota->id;
|
|
|
|
if ($param_laboratorio['exame_alterado']) {
|
|
|
|
foreach ($param_laboratorio['exames'] as $param_exame) {
|
|
|
|
$idsExameUnidadeSolicitante = [];
|
|
|
|
$exame = CotaExame::where('cota_id', $cota_id)
|
|
->where('exame_id', $param_exame['exame_id'])
|
|
->first() ?? new CotaExame();
|
|
|
|
$exame->fill($param_exame);
|
|
|
|
$exame->cota_id = $cota_id;
|
|
$exame->cota_exame_periodo_id = $request->cota_exame_periodo_id;
|
|
$exame->cota_valor = $param_exame['cota_valor'];
|
|
|
|
if (!is_null($exame->cota_valor)) {
|
|
$exame->cota_valor = str_replace('.', '', $exame->cota_valor);
|
|
$exame->cota_valor = str_replace(',', '.', $exame->cota_valor);
|
|
}
|
|
|
|
$exame->save();
|
|
|
|
if (isset($param_exame['unidade_solicitante_alterada']) && $param_exame['unidade_solicitante_alterada']) {
|
|
|
|
foreach ($param_exame['unidade_solicitante'] as $param_exame_unidade) {
|
|
|
|
$exame_unidade = CotaExameUnidadeSolicitante::where('cota_exame_id', $exame->id)
|
|
->where('infraestrutura_solicitante_id', $param_exame_unidade['infraestrutura_solicitante_id'])
|
|
->first() ?? new CotaExameUnidadeSolicitante();
|
|
|
|
$exame_unidade->fill($param_exame_unidade);
|
|
|
|
$exame_unidade->cota_id = $cota_id;
|
|
$exame_unidade->cota_exame_id = $exame->id;
|
|
$exame_unidade->cota_valor = $param_exame_unidade['cota_valor'];
|
|
|
|
if (!is_null($exame_unidade->cota_valor)) {
|
|
$exame_unidade->cota_valor = str_replace('.', '', $exame_unidade->cota_valor);
|
|
$exame_unidade->cota_valor = str_replace(',', '.', $exame_unidade->cota_valor);
|
|
}
|
|
|
|
$exame_unidade->save();
|
|
$idsExameUnidadeSolicitante[] = $exame_unidade->id;
|
|
}
|
|
|
|
CotaExameUnidadeSolicitante::destroy(CotaExameUnidadeSolicitante::where('cota_exame_id', $exame->id)
|
|
->whereNotIn('id', $idsExameUnidadeSolicitante)->pluck('id'));
|
|
}
|
|
|
|
$idsExame[] = $exame->id;
|
|
}
|
|
|
|
CotaExame::destroy(CotaExame::where('cota_id', $cota_id)->whereNotIn('id', $idsExame)->pluck('id'));
|
|
}
|
|
|
|
|
|
if ($param_laboratorio['unidade_alterada']) {
|
|
|
|
foreach ($param_laboratorio['unidade_solicitante'] as $param_unidade) {
|
|
|
|
$infraestrutura_solicitante_id = $param_unidade['infraestrutura_solicitante_id'];
|
|
|
|
$unidade = CotaUnidadeSolicitante::where('cota_id', $cota_id)
|
|
->where('infraestrutura_solicitante_id', $infraestrutura_solicitante_id)
|
|
->first() ?? new CotaUnidadeSolicitante();
|
|
|
|
$unidade->fill($param_unidade);
|
|
|
|
$unidade->cota_id = $cota_id;
|
|
$unidade->cota_id_unidade_solicitante = $cota->id;
|
|
$unidade->cota_valor = $param_unidade['cota_valor'];
|
|
|
|
if (!is_null($param_unidade['cota_valor'])) {
|
|
$unidade->cota_valor = str_replace('.', '', $unidade->cota_valor);
|
|
$unidade->cota_valor = str_replace(',', '.', $unidade->cota_valor);
|
|
}
|
|
|
|
$unidade->save();
|
|
|
|
$idsUnidade[] = $unidade->id;
|
|
}
|
|
|
|
CotaUnidadeSolicitante::destroy(CotaUnidadeSolicitante::where('cota_id', $cota_id)->whereNotIn('id', $idsUnidade)->pluck('id'));
|
|
}
|
|
}
|
|
|
|
$idsParaExcluir = Cota::where('infraestrutura_id', $param_laboratorio['infraestrutura_id'])
|
|
->whereNotIn('id', $idsCota)
|
|
->whereYear('inicio', $ano)
|
|
->whereYear('fim', $ano)
|
|
->pluck('id');
|
|
|
|
Cota::destroy($idsParaExcluir);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static function validateDuplicatedItems(
|
|
$id,
|
|
$unidade,
|
|
$infraestrutura,
|
|
$unidade_tipo_id,
|
|
$cota_tipo_id,
|
|
$inicio,
|
|
$fim
|
|
) {
|
|
if (self::isDuplicatedItems($id, $unidade, $infraestrutura, $unidade_tipo_id, $cota_tipo_id, $inicio, $fim)) {
|
|
|
|
throw new DuplicatedCotaException;
|
|
}
|
|
}
|
|
|
|
private static function isDuplicatedItems($id, $unidade, $infraestrutura, $unidade_tipo_id, $cota_tipo_id, $inicio, $fim)
|
|
{
|
|
return Cota::where('unidade_id', $unidade)
|
|
->where('id', '<>', $id)
|
|
->where('infraestrutura_id', $infraestrutura)
|
|
->where('unidade_tipo_id', $unidade_tipo_id)
|
|
->where('cota_tipo_id', $cota_tipo_id)
|
|
->where(function ($plantao) use ($inicio, $fim) {
|
|
|
|
$plantao
|
|
->where(function ($plantao) use ($inicio) {
|
|
|
|
$plantao
|
|
->where('inicio', '<=', Carbon::createFromFormat('d/m/Y', $inicio)->format('Y-m-d'))
|
|
->where('fim', '>=', Carbon::createFromFormat('d/m/Y', $inicio)->format('Y-m-d'));
|
|
})
|
|
->orWhere(function ($plantao) use ($fim) {
|
|
|
|
$plantao
|
|
->where('inicio', '<=', Carbon::createFromFormat('d/m/Y', $fim)->format('Y-m-d'))
|
|
->where('fim', '>=', Carbon::createFromFormat('d/m/Y', $fim)->format('Y-m-d'));
|
|
})
|
|
->orWhere(function ($plantao) use ($inicio, $fim) {
|
|
|
|
$plantao
|
|
->where('inicio', '>=', Carbon::createFromFormat('d/m/Y', $inicio)->format('Y-m-d'))
|
|
->where('inicio', '<=', Carbon::createFromFormat('d/m/Y', $fim)->format('Y-m-d'));
|
|
})
|
|
->orWhere(function ($plantao) use ($inicio, $fim) {
|
|
|
|
$plantao
|
|
->where('fim', '>=', Carbon::createFromFormat('d/m/Y', $inicio)->format('Y-m-d'))
|
|
->where('fim', '<=', Carbon::createFromFormat('d/m/Y', $fim)->format('Y-m-d'));
|
|
});
|
|
})->exists();
|
|
}
|
|
|
|
private static function validateDuplicatedExames($id, $unidade, $infraestrutura, $exame_id, $cota_tipo_id, $inicio, $fim)
|
|
{
|
|
if (self::isDuplicatedExames($id, $unidade, $infraestrutura, $exame_id, $cota_tipo_id, $inicio, $fim)) {
|
|
|
|
throw new DuplicatedCotaExameException;
|
|
}
|
|
}
|
|
|
|
private static function isDuplicatedExames($id, $unidade, $infraestrutura, $exame_id, $cota_tipo_id, $inicio, $fim)
|
|
{
|
|
return CotaExamePeriodo::where('unidade_id', $unidade)
|
|
->where('id', '<>', $id)
|
|
->where('infraestrutura_id', $infraestrutura)
|
|
->where('exame_id', $exame_id)
|
|
->where('cota_tipo_id', $cota_tipo_id)
|
|
->where(function ($plantao) use ($inicio, $fim) {
|
|
|
|
$plantao
|
|
->where(function ($plantao) use ($inicio) {
|
|
|
|
$plantao
|
|
->where('inicio', '<=', Carbon::createFromFormat('d/m/Y', $inicio)->format('Y-m-d'))
|
|
->where('fim', '>=', Carbon::createFromFormat('d/m/Y', $inicio)->format('Y-m-d'));
|
|
})
|
|
->orWhere(function ($plantao) use ($fim) {
|
|
|
|
$plantao
|
|
->where('inicio', '<=', Carbon::createFromFormat('d/m/Y', $fim)->format('Y-m-d'))
|
|
->where('fim', '>=', Carbon::createFromFormat('d/m/Y', $fim)->format('Y-m-d'));
|
|
})
|
|
->orWhere(function ($plantao) use ($inicio, $fim) {
|
|
|
|
$plantao
|
|
->where('inicio', '>=', Carbon::createFromFormat('d/m/Y', $inicio)->format('Y-m-d'))
|
|
->where('inicio', '<=', Carbon::createFromFormat('d/m/Y', $fim)->format('Y-m-d'));
|
|
})
|
|
->orWhere(function ($plantao) use ($inicio, $fim) {
|
|
|
|
$plantao
|
|
->where('fim', '>=', Carbon::createFromFormat('d/m/Y', $inicio)->format('Y-m-d'))
|
|
->where('fim', '<=', Carbon::createFromFormat('d/m/Y', $fim)->format('Y-m-d'));
|
|
});
|
|
})->exists();
|
|
}
|
|
|
|
public static function lotacoes($request)
|
|
{
|
|
$usuario = Usuario::findOrFail($request->usuario_id);
|
|
|
|
return [
|
|
'lotacoes' => $usuario->lotacoes()
|
|
->where('infraestrutura_id', $request->infraestrutura_id)
|
|
->get()
|
|
->filter(function ($lotacao) {
|
|
return $lotacao->ocupacao->procedimentos()
|
|
->where('sigtap.procedimento.codigo', ProcedimentoEnum::COLETA_DE_MATERIAL_PARA_EXAME_LABORATORIAL)
|
|
->where('sigtap.procedimento_ocupacao.competencia_id', function ($query) {
|
|
$query->select('sigtap.competencia.id')
|
|
->from('sigtap.competencia')
|
|
->orderBy('id')
|
|
->limit(1);
|
|
})
|
|
->exists();
|
|
})
|
|
->map(function ($lotacao) {
|
|
return [
|
|
'id' => $lotacao->id,
|
|
'unidade_id' => $lotacao->infraestrutura->unidade_id,
|
|
'unidade_nome' => $lotacao->infraestrutura->unidade->nome_fantasia,
|
|
'infraestrutura_id' => $lotacao->infraestrutura_id,
|
|
'infraestrutura_nome' => $lotacao->infraestrutura->nome,
|
|
'ocupacao' => $lotacao->ocupacao,
|
|
];
|
|
})
|
|
];
|
|
}
|
|
|
|
private static function getAno()
|
|
{
|
|
$data_inicio = Carbon::now()->addYear(2);
|
|
$datas = array($data_inicio->format('Y'));
|
|
|
|
for ($i = 0; $i < 5; $i++) {
|
|
$data_inicio->modify('-1 year');
|
|
$datas[] = $data_inicio->format('Y');
|
|
}
|
|
|
|
return $datas;
|
|
}
|
|
|
|
private static function saveMensagemProtocolo($request)
|
|
{
|
|
$msg = MensagemProtocolo::first() ?? new MensagemProtocolo();
|
|
$msg->mensagem = $request->mensagem_protocolo;
|
|
$msg->save();
|
|
}
|
|
|
|
private static function saveImpressaoAgendamento($request)
|
|
{
|
|
$impressao = ImpressaoAgendamento::first() ?? new ImpressaoAgendamento();
|
|
$impressao->tipo_id = $request->impressao_agendamento_tipo_id;
|
|
$impressao->save();
|
|
}
|
|
|
|
private static function saveWebService($request)
|
|
{
|
|
$webservice = WebService::first() ?? new WebService();
|
|
$webservice->tipo_id = $request->webservice_tipo_id;
|
|
$webservice->save();
|
|
}
|
|
|
|
private static function saveReagendar($request)
|
|
{
|
|
$impressao = ReagendarExame::first() ?? new ReagendarExame();
|
|
$impressao->exibir = $request->exibir_reagendar_exame;
|
|
$impressao->save();
|
|
}
|
|
|
|
private static function saveExameAmbulatorioAtendimento($request)
|
|
{
|
|
$impressao = ExameAmbulatorioAtendimento::first() ?? new ExameAmbulatorioAtendimento();
|
|
$impressao->exibir = $request->exibir_exame_ambulatorio_atendimento ?? false;
|
|
$impressao->save();
|
|
}
|
|
|
|
private static function saveIntegracaoAplicativo($request)
|
|
{
|
|
$integracao = IntegracaoAplicativo::first();
|
|
|
|
if (is_null($integracao)) {
|
|
|
|
IntegracaoAplicativo::create($request->integracao_aplicativo);
|
|
} else {
|
|
|
|
$integracao->update($request->integracao_aplicativo);
|
|
}
|
|
}
|
|
|
|
private static function saveCotaTransferencia($request)
|
|
{
|
|
$ids_transferencia = [];
|
|
$ids_transferencia_exames = [];
|
|
|
|
foreach ($request->cotas_transferencia as $params) {
|
|
|
|
$laboratorio_unidade_id = $params['laboratorio_unidade_id'];
|
|
$laboratorio_infraestrutura_id = $params['laboratorio_infraestrutura_id'];
|
|
$origem_unidade_id = $params['origem_unidade_id'];
|
|
$origem_infraestrutura_id = $params['origem_infraestrutura_id'];
|
|
$unidade_tipo_id = $params['unidade_tipo_id'];
|
|
|
|
foreach ($params['destino'] as $param_destino) {
|
|
|
|
$id = $param_destino['id'];
|
|
$cota_id = $param_destino['cota_id'];
|
|
$origem_inicio = $param_destino['origem_inicio'];
|
|
$origem_fim = $param_destino['origem_fim'];
|
|
$origem_cota_quantidade = $param_destino['origem_cota_quantidade'];
|
|
$origem_cota_valor = $param_destino['origem_cota_valor'];
|
|
$cota_tipo_id = $param_destino['origem_cota_tipo_id'];
|
|
$destino_unidade_id = $param_destino['destino_unidade_id'];
|
|
$destino_infraestrutura_id = $param_destino['destino_infraestrutura_id'];
|
|
$destino_inicio = $param_destino['destino_inicio'];
|
|
$destino_fim = $param_destino['destino_fim'];
|
|
$destino_cota_quantidade = $param_destino['destino_cota_quantidade'];
|
|
$destino_cota_valor = $param_destino['destino_cota_valor'];
|
|
|
|
if (!is_numeric($cota_id)) {
|
|
|
|
$cota = Cota::where('unidade_id', $origem_unidade_id)
|
|
->where('infraestrutura_id', $origem_infraestrutura_id)
|
|
->where('unidade_tipo_id', $unidade_tipo_id)
|
|
->where('cota_tipo_id', $cota_tipo_id)
|
|
->where('inicio', Carbon::createFromFormat('d/m/Y', $origem_inicio)->format('Y-m-d'))
|
|
->where('fim', Carbon::createFromFormat('d/m/Y', $origem_fim)->format('Y-m-d'))
|
|
->first() ?? new Cota();
|
|
|
|
$cota_id = $cota->id;
|
|
}
|
|
|
|
if ($id && is_numeric($id)) {
|
|
|
|
$transferencia = CotaTransferencia::where('id', $id)->first();
|
|
} else {
|
|
|
|
$transferencia = new CotaTransferencia();
|
|
$id = $transferencia->id;
|
|
}
|
|
|
|
$transferencia->id = $id;
|
|
$transferencia->cota_id = $cota_id;
|
|
$transferencia->laboratorio_unidade_id = $laboratorio_unidade_id;
|
|
$transferencia->laboratorio_infraestrutura_id = $laboratorio_infraestrutura_id;
|
|
$transferencia->origem_unidade_id = $origem_unidade_id;
|
|
$transferencia->origem_infraestrutura_id = $origem_infraestrutura_id;
|
|
$transferencia->origem_inicio = Carbon::createFromFormat('d/m/Y', $origem_inicio);
|
|
$transferencia->origem_fim = Carbon::createFromFormat('d/m/Y', $origem_fim);
|
|
$transferencia->origem_cota_quantidade = $origem_cota_quantidade;
|
|
$transferencia->origem_cota_valor = $origem_cota_valor;
|
|
|
|
if (!is_null($origem_cota_valor)) {
|
|
$transferencia->origem_cota_valor = str_replace('.', '', $transferencia->origem_cota_valor);
|
|
$transferencia->origem_cota_valor = str_replace(',', '.', $transferencia->origem_cota_valor);
|
|
}
|
|
|
|
$transferencia->destino_unidade_id = $destino_unidade_id;
|
|
$transferencia->destino_infraestrutura_id = $destino_infraestrutura_id;
|
|
$transferencia->destino_inicio = Carbon::createFromFormat('d/m/Y', $destino_inicio);
|
|
$transferencia->destino_fim = Carbon::createFromFormat('d/m/Y', $destino_fim);
|
|
$transferencia->destino_cota_quantidade = $destino_cota_quantidade;
|
|
$transferencia->destino_cota_valor = $destino_cota_valor;
|
|
|
|
if (!is_null($destino_cota_valor)) {
|
|
$transferencia->destino_cota_valor = str_replace('.', '', $transferencia->destino_cota_valor);
|
|
$transferencia->destino_cota_valor = str_replace(',', '.', $transferencia->destino_cota_valor);
|
|
}
|
|
|
|
$transferencia->unidade_tipo_id = $unidade_tipo_id;
|
|
$transferencia->cota_tipo_id = $cota_tipo_id;
|
|
|
|
if ($cota_tipo_id == TipoCota::VALOR) {
|
|
|
|
$saldoEnviado = CotaTransferencia::where('laboratorio_unidade_id', $laboratorio_unidade_id)
|
|
->where('laboratorio_infraestrutura_id', $laboratorio_infraestrutura_id)
|
|
->where('origem_unidade_id', $origem_unidade_id)
|
|
->where('origem_infraestrutura_id', $origem_infraestrutura_id)
|
|
->where('cota_tipo_id', $cota_tipo_id)
|
|
->where('unidade_tipo_id', $unidade_tipo_id)
|
|
->where('origem_inicio', '>=', Carbon::createFromFormat('d/m/Y', $origem_inicio)->format('Y-m-d'))
|
|
->where('origem_fim', '<=', Carbon::createFromFormat('d/m/Y', $origem_fim)->format('Y-m-d'))
|
|
->sum('destino_cota_valor');
|
|
|
|
$saldoRecebido = CotaTransferencia::where('laboratorio_unidade_id', $laboratorio_unidade_id)
|
|
->where('laboratorio_infraestrutura_id', $laboratorio_infraestrutura_id)
|
|
->where('destino_unidade_id', $origem_unidade_id)
|
|
->where('destino_infraestrutura_id', $origem_infraestrutura_id)
|
|
->where('cota_tipo_id', $cota_tipo_id)
|
|
->where('unidade_tipo_id', $unidade_tipo_id)
|
|
->where('origem_inicio', '>=', Carbon::createFromFormat('d/m/Y', $origem_inicio)->format('Y-m-d'))
|
|
->where('origem_fim', '<=', Carbon::createFromFormat('d/m/Y', $origem_fim)->format('Y-m-d'))
|
|
->sum('destino_cota_valor');
|
|
|
|
if ($unidade_tipo_id == TipoUnidade::LABORATORIO) {
|
|
|
|
$cotaLaboratorio = Cota::where('unidade_id', $laboratorio_unidade_id)
|
|
->where('infraestrutura_id', $laboratorio_infraestrutura_id)
|
|
->where('unidade_tipo_id', $unidade_tipo_id)
|
|
->where('cota_tipo_id', $cota_tipo_id)
|
|
->where('inicio', '<=', Carbon::createFromFormat('d/m/Y', $origem_inicio)->format('Y-m-d'))
|
|
->where('fim', '>=', Carbon::createFromFormat('d/m/Y', $origem_fim)->format('Y-m-d'))
|
|
->first();
|
|
|
|
if (!is_null($cotaLaboratorio)) {
|
|
$totalUnidade = CotaUnidadeSolicitante::where('cota_id', $cotaLaboratorio->id)
|
|
->where('unidade_solicitante_id', $origem_unidade_id)
|
|
->where('infraestrutura_solicitante_id', $origem_infraestrutura_id)
|
|
->sum('cota_valor') ?? 0;
|
|
} else {
|
|
$totalUnidade = 0;
|
|
}
|
|
} else {
|
|
|
|
$totalUnidade = Cota::where('unidade_id', $origem_unidade_id)
|
|
->where('infraestrutura_id', $origem_infraestrutura_id)
|
|
->where('unidade_tipo_id', $unidade_tipo_id)
|
|
->where('cota_tipo_id', $cota_tipo_id)
|
|
->where('inicio', '>=', Carbon::createFromFormat('d/m/Y', $origem_inicio)->format('Y-m-d'))
|
|
->where('fim', '<=', Carbon::createFromFormat('d/m/Y', $origem_fim)->format('Y-m-d'))
|
|
->sum('cota_quantidade') ?? 0;
|
|
}
|
|
} else {
|
|
|
|
$saldoEnviado = CotaTransferencia::where('laboratorio_unidade_id', $laboratorio_unidade_id)
|
|
->where('laboratorio_infraestrutura_id', $laboratorio_infraestrutura_id)
|
|
->where('origem_unidade_id', $origem_unidade_id)
|
|
->where('origem_infraestrutura_id', $origem_infraestrutura_id)
|
|
->where('cota_tipo_id', $cota_tipo_id)
|
|
->where('unidade_tipo_id', $unidade_tipo_id)
|
|
->where('origem_inicio', '>=', Carbon::createFromFormat('d/m/Y', $origem_inicio)->format('Y-m-d'))
|
|
->where('origem_fim', '<=', Carbon::createFromFormat('d/m/Y', $origem_fim)->format('Y-m-d'))
|
|
->sum('destino_cota_quantidade');
|
|
|
|
$saldoRecebido = CotaTransferencia::where('laboratorio_unidade_id', $laboratorio_unidade_id)
|
|
->where('laboratorio_infraestrutura_id', $laboratorio_infraestrutura_id)
|
|
->where('destino_unidade_id', $origem_unidade_id)
|
|
->where('destino_infraestrutura_id', $origem_infraestrutura_id)
|
|
->where('cota_tipo_id', $cota_tipo_id)
|
|
->where('unidade_tipo_id', $unidade_tipo_id)
|
|
->where('origem_inicio', '>=', Carbon::createFromFormat('d/m/Y', $origem_inicio)->format('Y-m-d'))
|
|
->where('origem_fim', '<=', Carbon::createFromFormat('d/m/Y', $origem_fim)->format('Y-m-d'))
|
|
->sum('destino_cota_quantidade');
|
|
|
|
if ($unidade_tipo_id == TipoUnidade::LABORATORIO) {
|
|
|
|
$cotaLaboratorio = Cota::where('unidade_id', $laboratorio_unidade_id)
|
|
->where('infraestrutura_id', $laboratorio_infraestrutura_id)
|
|
->where('unidade_tipo_id', $unidade_tipo_id)
|
|
->where('cota_tipo_id', $cota_tipo_id)
|
|
->where('inicio', '<=', Carbon::createFromFormat('d/m/Y', $origem_inicio)->format('Y-m-d'))
|
|
->where('fim', '>=', Carbon::createFromFormat('d/m/Y', $origem_fim)->format('Y-m-d'))
|
|
->first();
|
|
|
|
$totalUnidade = CotaUnidadeSolicitante::where('cota_id', $cotaLaboratorio->id)
|
|
->where('unidade_solicitante_id', $origem_unidade_id)
|
|
->where('infraestrutura_solicitante_id', $origem_infraestrutura_id)
|
|
->sum('cota_quantidade') ?? 0;
|
|
} else {
|
|
|
|
$totalUnidade = Cota::where('unidade_id', $origem_unidade_id)
|
|
->where('infraestrutura_id', $origem_infraestrutura_id)
|
|
->where('unidade_tipo_id', $unidade_tipo_id)
|
|
->where('cota_tipo_id', $cota_tipo_id)
|
|
->where('inicio', '>=', Carbon::createFromFormat('d/m/Y', $origem_inicio)->format('Y-m-d'))
|
|
->where('fim', '<=', Carbon::createFromFormat('d/m/Y', $origem_fim)->format('Y-m-d'))
|
|
->sum('cota_quantidade') ?? 0;
|
|
}
|
|
}
|
|
|
|
|
|
if ($destino_cota_quantidade > (($totalUnidade - $saldoEnviado) + $saldoRecebido)) {
|
|
|
|
throw new ExceededSaldoTransferenciaException;
|
|
}
|
|
|
|
$transferencia->save();
|
|
|
|
$ids_transferencia[] = $transferencia->id;
|
|
|
|
$transferencia_id = $transferencia->id;
|
|
|
|
foreach ($param_destino['exames'] as $param_exames) {
|
|
|
|
$cota_exame_id = $param_exames['cota_exame_periodo_id'];
|
|
$cota_quantidade = $param_exames['cota_quantidade'];
|
|
$cota_valor = $param_exames['cota_valor'];
|
|
$transferido = $param_exames['transferido'];
|
|
|
|
if (!is_numeric($cota_exame_id)) {
|
|
|
|
$cotasExame = CotaExamePeriodo::where('unidade_id', $laboratorio_unidade_id)
|
|
->where('infraestrutura_id', $laboratorio_infraestrutura_id)
|
|
->where('cota_tipo_id', $cota_tipo_id)
|
|
->where('exame_id', $param_exames['exame_id'])
|
|
->where('inicio', '<=', Carbon::createFromFormat('d/m/Y', $origem_inicio)->format('Y-m-d'))
|
|
->where('fim', '>=', Carbon::createFromFormat('d/m/Y', $origem_fim)->format('Y-m-d'))
|
|
->first();
|
|
|
|
$cota_exame_id = $cotasExame->id;
|
|
}
|
|
|
|
$cota_exame = CotaExame::where('cota_id', $cotaLaboratorio->id)
|
|
->where('exame_id', $param_exames['exame_id'])
|
|
->first();
|
|
|
|
$cotaRecebida = CotaTransferencia::where('laboratorio_unidade_id', $laboratorio_unidade_id)
|
|
->where('laboratorio_infraestrutura_id', $laboratorio_infraestrutura_id)
|
|
->where('destino_unidade_id', $origem_unidade_id)
|
|
->where('destino_infraestrutura_id', $origem_infraestrutura_id)
|
|
->where('cota_tipo_id', $cota_tipo_id)
|
|
->where('unidade_tipo_id', $unidade_tipo_id)
|
|
->where('origem_inicio', '>=', Carbon::createFromFormat('d/m/Y', $origem_inicio)->format('Y-m-d'))
|
|
->where('origem_fim', '<=', Carbon::createFromFormat('d/m/Y', $origem_fim)->format('Y-m-d'))
|
|
->first();
|
|
|
|
if ($cota_tipo_id == TipoCota::VALOR) {
|
|
|
|
$total_exame_unidade = CotaExameUnidadeSolicitante::where('cota_id', $cotaLaboratorio->id)
|
|
->where('cota_exame_id', $cota_exame->cota_exame_periodo_id)
|
|
->where('unidade_solicitante_id', $origem_unidade_id)
|
|
->where('infraestrutura_solicitante_id', $origem_infraestrutura_id)
|
|
->sum('cota_valor');
|
|
|
|
$transferencia_enviada_exame = CotaTransferenciaExames::where('cota_transferencia_id', $transferencia_id)
|
|
->where('cota_exame_id', $cota_exame_id)
|
|
->sum('cota_valor_transferido');
|
|
|
|
if (is_numeric($param_destino['id'])) {
|
|
|
|
$exame_transferido = CotaTransferenciaExames::where('cota_transferencia_id', $transferencia_id)
|
|
->where('cota_exame_id', $cota_exame_id)
|
|
->first();
|
|
|
|
$transferencia_enviada_exame = $transferencia_enviada_exame - $exame_transferido->cota_valor_transferido;
|
|
}
|
|
|
|
if ($cotaRecebida) {
|
|
|
|
$transferencia_recebida_exame = CotaTransferenciaExames::where('cota_transferencia_id', $cotaRecebida->id)
|
|
->where('cota_exame_id', $cota_exame_id)
|
|
->sum('cota_valor_transferido');
|
|
} else {
|
|
|
|
$transferencia_recebida_exame = 0;
|
|
}
|
|
|
|
$replace_transferido = $transferido;
|
|
|
|
$replace_transferido = str_replace('.', '', $replace_transferido);
|
|
$replace_transferido = str_replace(',', '.', $replace_transferido);
|
|
|
|
if ($replace_transferido > (($total_exame_unidade - $transferencia_enviada_exame) + $transferencia_recebida_exame)) {
|
|
|
|
throw new ExceededSaldoTransferenciaExameException;
|
|
}
|
|
} else {
|
|
|
|
$total_exame_unidade = CotaExameUnidadeSolicitante::where('cota_id', $cotaLaboratorio->id)
|
|
->where('cota_exame_id', $cota_exame->cota_exame_periodo_id)
|
|
->where('unidade_solicitante_id', $origem_unidade_id)
|
|
->where('infraestrutura_solicitante_id', $origem_infraestrutura_id)
|
|
->sum('cota_quantidade');
|
|
|
|
$transferencia_enviada_exame = CotaTransferenciaExames::where('cota_transferencia_id', $transferencia_id)
|
|
->where('cota_exame_id', $cota_exame_id)
|
|
->sum('cota_quantidade_transferido');
|
|
|
|
if (is_numeric($param_destino['id'])) {
|
|
|
|
$exame_transferido = CotaTransferenciaExames::where('cota_transferencia_id', $transferencia_id)
|
|
->where('cota_exame_id', $cota_exame_id)
|
|
->first();
|
|
|
|
$transferencia_enviada_exame = $transferencia_enviada_exame - $exame_transferido->cota_quantidade_transferido;
|
|
}
|
|
|
|
if ($cotaRecebida) {
|
|
|
|
$transferencia_recebida_exame = CotaTransferenciaExames::where('cota_transferencia_id', $cotaRecebida->id)
|
|
->where('cota_exame_id', $cota_exame_id)
|
|
->sum('cota_quantidade_transferido');
|
|
} else {
|
|
|
|
$transferencia_recebida_exame = 0;
|
|
}
|
|
|
|
if ($transferido > (($total_exame_unidade - $transferencia_enviada_exame) + $transferencia_recebida_exame)) {
|
|
|
|
throw new ExceededSaldoTransferenciaExameException;
|
|
}
|
|
}
|
|
|
|
$transferencia_exame = CotaTransferenciaExames::where('cota_transferencia_id', $transferencia_id)
|
|
->where('cota_exame_id', $cota_exame_id)
|
|
->first() ?? new CotaTransferenciaExames();
|
|
|
|
$transferencia_exame->cota_transferencia_id = $transferencia_id;
|
|
$transferencia_exame->cota_quantidade = $cota_quantidade;
|
|
$transferencia_exame->cota_valor = $cota_valor;
|
|
|
|
if (!is_null($cota_valor)) {
|
|
|
|
$transferencia_exame->cota_valor = str_replace('.', '', $transferencia_exame->cota_valor);
|
|
$transferencia_exame->cota_valor = str_replace(',', '.', $transferencia_exame->cota_valor);
|
|
}
|
|
|
|
$transferencia_exame->cota_exame_id = $cota_exame_id;
|
|
|
|
if ($cota_tipo_id == TipoCota::VALOR) {
|
|
|
|
$transferencia_exame->cota_valor_transferido = $transferido;
|
|
|
|
if (!is_null($transferido)) {
|
|
|
|
$transferencia_exame->cota_valor_transferido =
|
|
str_replace('.', '', $transferencia_exame->cota_valor_transferido);
|
|
$transferencia_exame->cota_valor_transferido =
|
|
str_replace(',', '.', $transferencia_exame->cota_valor_transferido);
|
|
}
|
|
} else {
|
|
|
|
$transferencia_exame->cota_quantidade_transferido = $transferido;
|
|
}
|
|
|
|
$transferencia_exame->save();
|
|
|
|
$ids_transferencia_exames[] = $transferencia_exame->id;
|
|
}
|
|
}
|
|
}
|
|
|
|
CotaTransferenciaExames::destroy(CotaTransferenciaExames::whereNotIn('id', $ids_transferencia_exames)
|
|
->pluck('id'));
|
|
|
|
CotaTransferencia::destroy(CotaTransferencia::whereNotIn('id', $ids_transferencia)
|
|
->pluck('id'));
|
|
}
|
|
|
|
public static function autocompleteExame()
|
|
{
|
|
return DB::query()
|
|
->fromSub(
|
|
DB::table('cadastro_grupo as grupo')
|
|
->whereNull('grupo.deleted_at')
|
|
->whereExists(
|
|
fn($q) => $q
|
|
->selectRaw(1)
|
|
->from('parametrizacao_exame as exame')
|
|
->whereColumn('exame.grupo_tipo_id', 'grupo.id')
|
|
->whereNull('exame.deleted_at')
|
|
->where('tipo_id', Tipo::LABORATORIAL)
|
|
->where('exame.ativo', true)
|
|
)
|
|
->selectRaw("
|
|
'grupo' as origem,
|
|
grupo.id,
|
|
NULL as codigo,
|
|
grupo.nome,
|
|
NULL as grupo_id,
|
|
NULL as grupo_nome,
|
|
NULL as observacoes
|
|
")
|
|
->unionAll(
|
|
DB::table('parametrizacao_exame as exame')
|
|
->join('cadastro_grupo as grupo', 'grupo.id', 'exame.grupo_tipo_id')
|
|
->whereNull('exame.deleted_at')
|
|
->where('exame.ativo', true)
|
|
->where('tipo_id', Tipo::LABORATORIAL)
|
|
->when(is_array(request()->not_id), fn($q) => $q->whereNotIn('exame.id', request()->not_id))
|
|
->selectRaw("
|
|
'exame' as origem,
|
|
exame.id,
|
|
exame.codigo,
|
|
exame.nome,
|
|
grupo.id as grupo_id,
|
|
grupo.nome as grupo_nome,
|
|
exame.observacao as observacoes
|
|
")
|
|
),
|
|
'item'
|
|
)
|
|
->selectRaw("
|
|
item.origem,
|
|
item.id,
|
|
item.codigo,
|
|
item.nome,
|
|
item.grupo_id,
|
|
item.grupo_nome,
|
|
item.observacoes
|
|
");
|
|
}
|
|
|
|
public static function list()
|
|
{
|
|
$params = request()->all();
|
|
|
|
switch (request()->listar) {
|
|
|
|
case 'exames': {
|
|
|
|
return self::getExames($params);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static function getExames($params)
|
|
{
|
|
return DB::table('parametrizacao_exame as exame')
|
|
->join('cadastro_grupo as grupo', 'grupo.id', 'exame.grupo_tipo_id')
|
|
->where('exame.grupo_tipo_id', $params['grupo_id'])
|
|
->whereNull('exame.deleted_at')
|
|
->where('exame.ativo', true)
|
|
->selectRaw("
|
|
exame.id,
|
|
exame.codigo,
|
|
exame.nome,
|
|
grupo.id as grupo_id,
|
|
grupo.nome as grupo_nome,
|
|
exame.observacao as observacoes
|
|
")
|
|
->get();
|
|
}
|
|
|
|
private static function validate($request)
|
|
{
|
|
$validations = [
|
|
'unidades.unidade_id' => [
|
|
'integer',
|
|
],
|
|
'unidades.unidade_coleta' => [
|
|
'nullable',
|
|
],
|
|
'unidades.laboratorio' => [
|
|
'nullable',
|
|
],
|
|
'cotas.*.unidade_id' => [
|
|
'integer',
|
|
'exists:cadastro_unidade,id',
|
|
],
|
|
'cotas.*.cota_tipo_id' => [
|
|
'integer',
|
|
],
|
|
'cotas.*.cota_valor' => [
|
|
'nullable',
|
|
],
|
|
'cotas.*.cota_quantidade' => [
|
|
'nullable',
|
|
],
|
|
'cotas.*.observacao' => [
|
|
'max:500',
|
|
],
|
|
'mensagem_protocolo' => [
|
|
'required',
|
|
'string',
|
|
'between:3,500',
|
|
],
|
|
'motivos.*.descricao' => [
|
|
'required',
|
|
'string',
|
|
'between:3,100',
|
|
],
|
|
'integracao_aplicativo.habilita_agendamento_conectasus_cidadao' => [
|
|
'required',
|
|
'boolean',
|
|
],
|
|
'integracao_aplicativo.habilita_visualizacao_laudo_conectasus_cidadao' => [
|
|
'required',
|
|
'boolean',
|
|
],
|
|
'integracao_aplicativo.laboratorios.*.infraestrutura_id' => [
|
|
'required',
|
|
'integer',
|
|
'exists:cadastro_infraestrutura,id',
|
|
],
|
|
'integracao_aplicativo.laboratorios.*.percentual_financeiro' => [
|
|
'required',
|
|
'integer',
|
|
'min:0',
|
|
'max:100',
|
|
],
|
|
'integracao_aplicativo.laboratorios.*.percentual_quantitativo' => [
|
|
'required',
|
|
'integer',
|
|
'min:0',
|
|
'max:100',
|
|
],
|
|
'integracao_aplicativo.exames.*.cota_exame_periodo_id' => [
|
|
'required',
|
|
'integer',
|
|
'exists:exame_laboratorial_parametrizacao_cota_exame_periodo,id',
|
|
],
|
|
'integracao_aplicativo.exames.*.percentual_financeiro' => [
|
|
'required',
|
|
'integer',
|
|
'min:0',
|
|
'max:100',
|
|
],
|
|
'integracao_aplicativo.exames.*.percentual_quantitativo' => [
|
|
'required',
|
|
'integer',
|
|
'min:0',
|
|
'max:100',
|
|
],
|
|
'integracao_aplicativo.unidades_solicitantes.*.unidade_id' => [
|
|
'required',
|
|
'integer',
|
|
'exists:cadastro_unidade,id',
|
|
],
|
|
'integracao_aplicativo.unidades_solicitantes.*.infraestrutura_id' => [
|
|
'required',
|
|
'integer',
|
|
'exists:cadastro_infraestrutura,id',
|
|
],
|
|
'integracao_aplicativo.unidades_solicitantes.*.percentual_financeiro' => [
|
|
'required',
|
|
'integer',
|
|
'min:0',
|
|
'max:100',
|
|
],
|
|
'integracao_aplicativo.unidades_solicitantes.*.percentual_quantitativo' => [
|
|
'required',
|
|
'integer',
|
|
'min:0',
|
|
'max:100',
|
|
],
|
|
];
|
|
|
|
$messages = [
|
|
'mensagem_protocolo.required' => 'Mensagem é obrigatória.',
|
|
'motivos.*.descricao.required' => 'Motivo é obrigatório.',
|
|
];
|
|
|
|
return $request->validate($validations, $messages);
|
|
}
|
|
|
|
private static function getLaboratoriosAppCidadao()
|
|
{
|
|
return DB::table('exame_laboratorial_parametrizacao_cota')
|
|
->join('cadastro_infraestrutura', 'exame_laboratorial_parametrizacao_cota.infraestrutura_id', '=', 'cadastro_infraestrutura.id')
|
|
->join('cadastro_unidade', 'cadastro_infraestrutura.unidade_id', '=', 'cadastro_unidade.id')
|
|
->where('exame_laboratorial_parametrizacao_cota.unidade_tipo_id', TipoUnidade::LABORATORIO)
|
|
->whereNull('exame_laboratorial_parametrizacao_cota.deleted_at')
|
|
->whereYear('exame_laboratorial_parametrizacao_cota.inicio', Carbon::now()->year)
|
|
->groupBy('cadastro_infraestrutura.id')
|
|
->select([
|
|
'cadastro_unidade.id as unidade_id',
|
|
'cadastro_infraestrutura.id as infraestrutura_id',
|
|
'cadastro_unidade.nome_fantasia',
|
|
DB::raw('COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia) AS infraestrutura_nome'),
|
|
])
|
|
->get()
|
|
->map(function ($lab) {
|
|
$config = AppCidadaoLaboratorio::where('infraestrutura_id', $lab->infraestrutura_id)->first();
|
|
|
|
$todasCotas = Cota::where('infraestrutura_id', $lab->infraestrutura_id)
|
|
->where('unidade_tipo_id', TipoUnidade::LABORATORIO)
|
|
->whereNull('deleted_at')
|
|
->whereYear('inicio', Carbon::now()->year)
|
|
->orderBy('inicio')
|
|
->get();
|
|
|
|
$cotasMesVigente = $todasCotas->filter(fn($cota) =>
|
|
Carbon::parse($cota->inicio)->month === Carbon::now()->month
|
|
);
|
|
|
|
$percentualFinanceiro = $config->percentual_financeiro ?? 0;
|
|
$percentualQuantitativo = $config->percentual_quantitativo ?? 0;
|
|
|
|
$periodos = $todasCotas->map(fn($cota) => [
|
|
'cota_tipo_id' => $cota->cota_tipo_id,
|
|
'inicio' => (new \Carbon\Carbon($cota->inicio))->format('d/m/Y'),
|
|
'fim' => (new \Carbon\Carbon($cota->fim))->format('d/m/Y'),
|
|
'cota_valor' => $cota->cota_valor ?? 0,
|
|
'cota_quantidade' => $cota->cota_quantidade ?? 0,
|
|
'percentual_financeiro' => $percentualFinanceiro,
|
|
'percentual_quantitativo' => $percentualQuantitativo,
|
|
])->values();
|
|
|
|
return [
|
|
'infraestrutura_id' => $lab->infraestrutura_id,
|
|
'nome_fantasia' => $lab->nome_fantasia,
|
|
'infraestrutura_nome' => $lab->infraestrutura_nome,
|
|
'percentual_financeiro' => $percentualFinanceiro,
|
|
'percentual_quantitativo' => $percentualQuantitativo,
|
|
'cota_valor_total' => $cotasMesVigente->where('cota_tipo_id', TipoCota::VALOR)->sum('cota_valor'),
|
|
'cota_quantidade_total' => $cotasMesVigente->where('cota_tipo_id', TipoCota::QUANTIDADE)->sum('cota_quantidade'),
|
|
'periodos' => $periodos,
|
|
];
|
|
})
|
|
->values();
|
|
}
|
|
|
|
private static function saveAppCidadaoLaboratorio($request)
|
|
{
|
|
$laboratorios = $request->input('integracao_aplicativo.laboratorios', []);
|
|
|
|
$ids = [];
|
|
|
|
foreach ($laboratorios as $params) {
|
|
|
|
$registro = AppCidadaoLaboratorio::withTrashed()
|
|
->where('infraestrutura_id', $params['infraestrutura_id'])
|
|
->first();
|
|
|
|
if ($registro && $registro->trashed()) {
|
|
$registro->restore();
|
|
} elseif (!$registro) {
|
|
$registro = new AppCidadaoLaboratorio();
|
|
$registro->infraestrutura_id = $params['infraestrutura_id'];
|
|
}
|
|
|
|
$registro->percentual_financeiro = $params['percentual_financeiro'] ?? 0;
|
|
$registro->percentual_quantitativo = $params['percentual_quantitativo'] ?? 0;
|
|
$registro->save();
|
|
|
|
$ids[] = $registro->id;
|
|
}
|
|
|
|
AppCidadaoLaboratorio::whereNotIn('id', $ids)->delete();
|
|
}
|
|
|
|
private static function getExamesAppCidadao()
|
|
{
|
|
return CotaExamePeriodo::whereNull('deleted_at')
|
|
->groupBy('unidade_id', 'infraestrutura_id', 'cota_tipo_id', 'inicio')
|
|
->get()
|
|
->map(function ($periodo) {
|
|
|
|
$config = AppConectasusCidadaoExame::where('cota_exame_periodo_id', $periodo->id)->first();
|
|
|
|
$grupo = CotaExamePeriodo::where('unidade_id', $periodo->unidade_id)
|
|
->where('infraestrutura_id', $periodo->infraestrutura_id)
|
|
->where('cota_tipo_id', $periodo->cota_tipo_id)
|
|
->where('inicio', $periodo->inicio)
|
|
->whereNull('deleted_at')
|
|
->get();
|
|
|
|
$cotaValorTotal = $grupo->sum('cota_valor');
|
|
$cotaQuantidadeTotal = $grupo->sum('cota_quantidade');
|
|
|
|
return [
|
|
'cota_exame_periodo_id' => $periodo->id,
|
|
'cota_tipo_id' => $periodo->cota_tipo_id,
|
|
'cota_nome' => $periodo->cota_tipo_id == TipoCota::VALOR ? 'Valor' : 'Quantidade',
|
|
'nome_fantasia' => $periodo->unidade->nome_fantasia ?? null,
|
|
'infraestrutura_nome' => $periodo->infraestrutura->nome ?? null,
|
|
'inicio' => (new \Carbon\Carbon($periodo->inicio))->format('d/m/Y'),
|
|
'fim' => (new \Carbon\Carbon($periodo->fim))->format('d/m/Y'),
|
|
'percentual_financeiro' => $config->percentual_financeiro ?? 0,
|
|
'percentual_quantitativo' => $config->percentual_quantitativo ?? 0,
|
|
'cota_valor_total' => $cotaValorTotal,
|
|
'cota_quantidade_total' => $cotaQuantidadeTotal,
|
|
];
|
|
})
|
|
->values();
|
|
}
|
|
|
|
private static function saveAppCidadaoExame($request)
|
|
{
|
|
$exames = $request->input('integracao_aplicativo.exames', []);
|
|
|
|
$ids = [];
|
|
|
|
foreach ($exames as $params) {
|
|
|
|
$registro = AppConectasusCidadaoExame::withTrashed()
|
|
->where('cota_exame_periodo_id', $params['cota_exame_periodo_id'])
|
|
->first();
|
|
|
|
if ($registro && $registro->trashed()) {
|
|
$registro->restore();
|
|
} elseif (!$registro) {
|
|
$registro = new AppConectasusCidadaoExame();
|
|
$registro->cota_exame_periodo_id = $params['cota_exame_periodo_id'];
|
|
}
|
|
|
|
$registro->percentual_financeiro = $params['percentual_financeiro'] ?? 0;
|
|
$registro->percentual_quantitativo = $params['percentual_quantitativo'] ?? 0;
|
|
$registro->save();
|
|
|
|
$ids[] = $registro->id;
|
|
}
|
|
|
|
AppConectasusCidadaoExame::whereNotIn('id', $ids)->delete();
|
|
}
|
|
|
|
private static function getUnidadesSolicitantesAppCidadao()
|
|
{
|
|
return Cota::where('unidade_tipo_id', TipoUnidade::UNIDADE_SOLICITANTE)
|
|
->whereNull('deleted_at')
|
|
->groupBy('unidade_id', 'infraestrutura_id')
|
|
->get()
|
|
->map(function ($cota) {
|
|
$config = AppConectasusCidadaoUnidadeSolicitante::where('unidade_id', $cota->unidade_id)
|
|
->where('infraestrutura_id', $cota->infraestrutura_id)
|
|
->first();
|
|
|
|
$cotas = Cota::where('unidade_id', $cota->unidade_id)
|
|
->where('infraestrutura_id', $cota->infraestrutura_id)
|
|
->where('unidade_tipo_id', TipoUnidade::UNIDADE_SOLICITANTE)
|
|
->whereNull('deleted_at')
|
|
->get();
|
|
|
|
return [
|
|
'unidade_id' => $cota->unidade_id,
|
|
'infraestrutura_id' => $cota->infraestrutura_id,
|
|
'nome_fantasia' => $cota->unidade->nome_fantasia ?? null,
|
|
'infraestrutura_nome' => $cota->infraestrutura->nome ?? null,
|
|
'percentual_financeiro' => $config->percentual_financeiro ?? 0,
|
|
'percentual_quantitativo' => $config->percentual_quantitativo ?? 0,
|
|
'cota_valor_total' => $cotas->where('cota_tipo_id', TipoCota::VALOR)->sum('cota_valor'),
|
|
'cota_quantidade_total' => $cotas->where('cota_tipo_id', TipoCota::QUANTIDADE)->sum('cota_quantidade'),
|
|
];
|
|
})
|
|
->values();
|
|
}
|
|
|
|
private static function saveAppCidadaoUnidadeSolicitante($request)
|
|
{
|
|
$unidades = $request->input('integracao_aplicativo.unidades_solicitantes', []);
|
|
|
|
$ids = [];
|
|
|
|
foreach ($unidades as $params) {
|
|
|
|
$registro = AppConectasusCidadaoUnidadeSolicitante::withTrashed()
|
|
->where('unidade_id', $params['unidade_id'])
|
|
->where('infraestrutura_id', $params['infraestrutura_id'])
|
|
->first();
|
|
|
|
if ($registro && $registro->trashed()) {
|
|
$registro->restore();
|
|
} elseif (!$registro) {
|
|
$registro = new AppConectasusCidadaoUnidadeSolicitante();
|
|
$registro->unidade_id = $params['unidade_id'];
|
|
$registro->infraestrutura_id = $params['infraestrutura_id'];
|
|
}
|
|
|
|
$registro->percentual_financeiro = $params['percentual_financeiro'] ?? 0;
|
|
$registro->percentual_quantitativo = $params['percentual_quantitativo'] ?? 0;
|
|
$registro->save();
|
|
|
|
$ids[] = $registro->id;
|
|
}
|
|
|
|
AppConectasusCidadaoUnidadeSolicitante::whereNotIn('id', $ids)->delete();
|
|
}
|
|
}
|