This commit is contained in:
@@ -0,0 +1,319 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Impressao\Ambulatorio;
|
||||
|
||||
use App\Enums\Ambulatorio\AtendimentoImpressaoTipo;
|
||||
use App\Enums\Cadastro\Medicamento\TipoReceita;
|
||||
use App\Enums\Cadastro\SujeitoAtencao\PreferenciaNome;
|
||||
use App\Enums\Cadastro\SujeitoAtencao\TipoRegistroUsuario;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Ambulatorio\Atendimento;
|
||||
use App\Models\Cadastro\SujeitoAtencao\Paciente;
|
||||
use App\Services\Ambulatorio\AtendimentoService;
|
||||
use App\Utils\DataUtil;
|
||||
use App\Utils\Exports\Pdf;
|
||||
use App\Utils\Exports\Template;
|
||||
use App\Utils\MaskUtil;
|
||||
use Carbon\Carbon;
|
||||
use DOMDocument;
|
||||
|
||||
class ReceitaMedicaNaoPadronizadaComumController extends Controller
|
||||
{
|
||||
private const TITLE = 'RECEITUÁRIO';
|
||||
private const TEMPLATE = 'ambulatorio.receita-medica-nao-padronizada-comum';
|
||||
|
||||
public static function getTemplate($atendimentoId, $dados = [])
|
||||
{
|
||||
$receitas = collect();
|
||||
|
||||
if (request()->has('em_atendimento') && request()->em_atendimento == true) {
|
||||
self::setReceitasNaoSalvas($atendimentoId, $receitas);
|
||||
|
||||
$data = [
|
||||
'exibirAssinatura' => true,
|
||||
'receitas' => $receitas
|
||||
];
|
||||
|
||||
AtendimentoService::saveLogImpressao(request()->atendimento_id, AtendimentoImpressaoTipo::RECEITA_NAO_PADRONIZADA_COMUM);
|
||||
} else {
|
||||
self::getAtendimento($atendimentoId, $receitas, $dados);
|
||||
|
||||
$data = [
|
||||
'exibirAssinatura' => true,
|
||||
'receitas' => $receitas->filter(fn ($r) => count($r['medicamentos']) > 0)->values()->all()
|
||||
];
|
||||
}
|
||||
|
||||
$paraAssinaturaEletronica = request()->para_assinatura_eletronica === true
|
||||
|| (DataUtil::isStringBoolean(request()->para_assinatura_eletronica)
|
||||
&& DataUtil::isStringTrue(request()->para_assinatura_eletronica));
|
||||
|
||||
$data = [
|
||||
'exibirAssinatura' => !$paraAssinaturaEletronica,
|
||||
'paraAssinar' => $paraAssinaturaEletronica,
|
||||
'receitas' => $receitas
|
||||
];
|
||||
|
||||
return Template::make(self::TITLE, self::TEMPLATE, true)->withData($data)->getHtml();
|
||||
}
|
||||
|
||||
public static function setReceitasNaoSalvas($pacienteId, $receitas)
|
||||
{
|
||||
$currentLotacao = self::getCurrentLotacao();
|
||||
$paciente = Paciente::where('id', $pacienteId)->first();
|
||||
|
||||
$medicamentos = [];
|
||||
|
||||
if (request()->has('multiple') && request()->multiple == true) {
|
||||
foreach (request()->medicamentos as $solicitacao) {
|
||||
$medicamentos[] = [
|
||||
'is_imprimir_separado' => isset($solicitacao['is_imprimir_separado']) && $solicitacao['is_imprimir_separado'] === 'true' ? true : false,
|
||||
'recomendacoes' => $solicitacao['recomendacoes'] ?? '',
|
||||
'descricao' => $solicitacao['descricao'] ?? '',
|
||||
];
|
||||
}
|
||||
} else {
|
||||
$medicamentos[] = [
|
||||
'is_imprimir_separado' => isset(request()->is_imprimir_separado) && request()->is_imprimir_separado === 'true' ? true : false,
|
||||
'recomendacoes' => request()->recomendacoes ?? '',
|
||||
'descricao' => request()->descricao ?? '',
|
||||
];
|
||||
}
|
||||
|
||||
foreach ($medicamentos as &$medicamento) {
|
||||
$htmlOriginal = $medicamento['descricao'] ?? '';
|
||||
|
||||
$htmlOriginal = preg_replace('/<p>\s*<br\s*\/?>\s*<\/p>/i', '', $htmlOriginal);
|
||||
|
||||
$descricaoPaginas = [];
|
||||
|
||||
$dom = new DOMDocument();
|
||||
libxml_use_internal_errors(true);
|
||||
$dom->loadHTML('<?xml encoding="UTF-8"><div>' . $htmlOriginal . '</div>', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
|
||||
libxml_clear_errors();
|
||||
|
||||
$wrapper = $dom->getElementsByTagName('div')->item(0);
|
||||
|
||||
$currentPage = '';
|
||||
$currentLineCount = 0;
|
||||
$maxLineLength = 100;
|
||||
$maxLinesPerPage = 15;
|
||||
|
||||
foreach (iterator_to_array($wrapper->childNodes) as $node) {
|
||||
$tempDom = new DOMDocument();
|
||||
$imported = $tempDom->importNode($node, true);
|
||||
$tempDom->appendChild($imported);
|
||||
$htmlNode = $tempDom->saveHTML();
|
||||
|
||||
$textoVisivel = trim(preg_replace('/\s+/', ' ', strip_tags($htmlNode)));
|
||||
$linhasEstimadas = (int) ceil(mb_strlen($textoVisivel) / $maxLineLength);
|
||||
$linhasEstimadas += substr_count(strtolower($htmlNode), '<br');
|
||||
|
||||
if (stripos($htmlNode, '<p') !== false && mb_strlen($textoVisivel) === 0) {
|
||||
$linhasEstimadas += 1;
|
||||
}
|
||||
|
||||
if ($currentLineCount + $linhasEstimadas > $maxLinesPerPage && $currentLineCount > 0) {
|
||||
$descricaoPaginas[] = $currentPage;
|
||||
$currentPage = '';
|
||||
$currentLineCount = 0;
|
||||
}
|
||||
|
||||
$currentPage .= $htmlNode;
|
||||
$currentLineCount += $linhasEstimadas;
|
||||
}
|
||||
|
||||
if (!empty(trim($currentPage))) {
|
||||
$descricaoPaginas[] = $currentPage;
|
||||
}
|
||||
|
||||
$medicamento['descricao_paginas'] = $descricaoPaginas;
|
||||
}
|
||||
|
||||
$dadosPrincipais = [
|
||||
'isEspecial' => true,
|
||||
'profissional' => [
|
||||
'nome' => $currentLotacao->usuario->cidadao->nome,
|
||||
'cbo' => $currentLotacao->ocupacao->nome,
|
||||
'documento' => $currentLotacao->usuario->registro_tipo_id
|
||||
? ($currentLotacao->usuario->registro_tipo_id === TipoRegistroUsuario::OUTRO
|
||||
? $currentLotacao->usuario->registro_tipo_outro
|
||||
: $currentLotacao->usuario->registroTipo->sigla
|
||||
) . ": {$currentLotacao->usuario->registro_numero}"
|
||||
: 'Conselho profissional: NÃO INFORMADO',
|
||||
],
|
||||
'unidade' => [
|
||||
'nome_fantasia' => $currentLotacao->infraestrutura->unidade->nome_fantasia,
|
||||
'cnpj' => $currentLotacao->infraestrutura->unidade->cnpj
|
||||
? MaskUtil::getCnpjMasked($currentLotacao->infraestrutura->unidade->cnpj)
|
||||
: null,
|
||||
'endereco' => $currentLotacao->infraestrutura->endereco->completo,
|
||||
'telefone' => MaskUtil::getPhoneMasked($currentLotacao->infraestrutura->telefone),
|
||||
],
|
||||
'paciente' => [
|
||||
'nome' => $paciente->cidadao->nome,
|
||||
'nomeSocial' => $paciente->cidadao->nome_social,
|
||||
'prefereNomeSocial' => $paciente->cidadao->preferencia_nome_id === PreferenciaNome::NOME_SOCIAL,
|
||||
'endereco' => $paciente->cidadao->domicilio->endereco->completo ?? '-',
|
||||
'telefone' => MaskUtil::getPhoneMasked($paciente->cidadao->telefone_principal->numero ?? null),
|
||||
'cns' => $paciente->cidadao->cns,
|
||||
'cpf' => $paciente->cidadao->cpf ? MaskUtil::getCpfMasked($paciente->cidadao->cpf) : null,
|
||||
'prontuario' => $paciente->prontuario,
|
||||
],
|
||||
'dataCadastro' => [
|
||||
'porExtenso' => Carbon::now()->translatedFormat('d \de F \de Y'),
|
||||
],
|
||||
];
|
||||
|
||||
$medicamentosAgrupados = collect($medicamentos)->where('is_imprimir_separado', false);
|
||||
|
||||
if ($medicamentosAgrupados->isNotEmpty()) {
|
||||
$receitas->push(array_merge($dadosPrincipais, [
|
||||
'medicamentos' => $medicamentosAgrupados->all()
|
||||
]));
|
||||
}
|
||||
|
||||
$medicamentosSeparados = collect($medicamentos)->where('is_imprimir_separado', true);
|
||||
|
||||
if ($medicamentosSeparados->isNotEmpty()) {
|
||||
$medicamentosSeparados->each(function ($medicamento) use ($receitas, $dadosPrincipais) {
|
||||
$receitas->push(array_merge($dadosPrincipais, [
|
||||
'medicamentos' => [$medicamento]
|
||||
]));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public static function getAtendimento($atendimentoId, $receitas, $dados = [])
|
||||
{
|
||||
$atendimento = Atendimento::findOrFail($atendimentoId);
|
||||
$paciente = $atendimento->paciente;
|
||||
|
||||
$atendimento->soaps()->where('is_rascunho', false)->get()->each(fn ($soap) => $soap->prescricoesMedicamentos
|
||||
->each(function ($prescricaoMedicamento) use ($receitas, $paciente, $dados) {
|
||||
|
||||
$dadosPrincipais = [
|
||||
'isEspecial' => true,
|
||||
'profissional' => [
|
||||
'nome' => $prescricaoMedicamento->createdLotacao->usuario->cidadao->nome,
|
||||
'cbo' => $prescricaoMedicamento->createdLotacao->ocupacao->nome,
|
||||
'documento' => $prescricaoMedicamento->createdLotacao->usuario->registro_tipo_id
|
||||
? ($prescricaoMedicamento->createdLotacao->usuario->registro_tipo_id === TipoRegistroUsuario::OUTRO
|
||||
? $prescricaoMedicamento->createdLotacao->usuario->registro_tipo_outro
|
||||
: $prescricaoMedicamento->createdLotacao->usuario->registroTipo->sigla
|
||||
) . ": {$prescricaoMedicamento->createdLotacao->usuario->registro_numero}"
|
||||
: 'Conselho profissional: NÃO INFORMADO',
|
||||
],
|
||||
'unidade' => [
|
||||
'nome_fantasia' => $prescricaoMedicamento->createdLotacao->infraestrutura->unidade->nome_fantasia,
|
||||
'cnpj' => $prescricaoMedicamento->createdLotacao->infraestrutura->unidade->cnpj
|
||||
? MaskUtil::getCnpjMasked($prescricaoMedicamento->createdLotacao->infraestrutura->unidade->cnpj)
|
||||
: null,
|
||||
'endereco' => $prescricaoMedicamento->createdLotacao->infraestrutura->endereco->completo,
|
||||
'telefone' => MaskUtil::getPhoneMasked($prescricaoMedicamento->createdLotacao->infraestrutura->telefone),
|
||||
],
|
||||
'paciente' => [
|
||||
'nome' => $paciente->cidadao->nome,
|
||||
'nomeSocial' => $paciente->cidadao->nome_social,
|
||||
'prefereNomeSocial' => $paciente->cidadao->preferencia_nome_id === PreferenciaNome::NOME_SOCIAL,
|
||||
'endereco' => $paciente->cidadao->domicilio->endereco->completo ?? '-',
|
||||
'telefone' => MaskUtil::getPhoneMasked($paciente->cidadao->telefone_principal->numero ?? null),
|
||||
'cpf' => $paciente->cidadao->cpf ? MaskUtil::getCpfMasked($paciente->cidadao->cpf) : null,
|
||||
'prontuario' => $paciente->prontuario,
|
||||
],
|
||||
'dataCadastro' => [
|
||||
'porExtenso' => $prescricaoMedicamento->created_at->translatedFormat('d \de F \de Y'),
|
||||
],
|
||||
];
|
||||
|
||||
$itens = $prescricaoMedicamento->itens
|
||||
->whereNull('material_id')
|
||||
->where('receita_tipo_id', TipoReceita::COMUM)
|
||||
->when(isset($dados['medicamentos']), fn ($q) => $q->filter(fn ($m) => collect($dados['medicamentos'])->contains($m->id)));
|
||||
|
||||
$processarItens = function ($itens) {
|
||||
return $itens->map(function ($item) {
|
||||
$htmlOriginal = $item->descricao ?? '';
|
||||
|
||||
$htmlOriginal = preg_replace('/<p>\s*<br\s*\/?>\s*<\/p>/i', '', $htmlOriginal);
|
||||
|
||||
$descricaoPaginas = [];
|
||||
|
||||
$dom = new \DOMDocument();
|
||||
libxml_use_internal_errors(true);
|
||||
$dom->loadHTML('<?xml encoding="UTF-8"><div>' . $htmlOriginal . '</div>', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
|
||||
libxml_clear_errors();
|
||||
|
||||
$wrapper = $dom->getElementsByTagName('div')->item(0);
|
||||
|
||||
$currentPage = '';
|
||||
$currentLineCount = 0;
|
||||
$maxLineLength = 100;
|
||||
$maxLinesPerPage = 15;
|
||||
|
||||
foreach (iterator_to_array($wrapper->childNodes) as $node) {
|
||||
$tempDom = new \DOMDocument();
|
||||
$imported = $tempDom->importNode($node, true);
|
||||
$tempDom->appendChild($imported);
|
||||
$htmlNode = $tempDom->saveHTML();
|
||||
|
||||
$textoVisivel = trim(preg_replace('/\s+/', ' ', strip_tags($htmlNode)));
|
||||
$linhasEstimadas = (int) ceil(mb_strlen($textoVisivel) / $maxLineLength);
|
||||
$linhasEstimadas += substr_count(strtolower($htmlNode), '<br');
|
||||
|
||||
if (stripos($htmlNode, '<p') !== false && mb_strlen($textoVisivel) === 0) {
|
||||
$linhasEstimadas += 1;
|
||||
}
|
||||
|
||||
if ($currentLineCount + $linhasEstimadas > $maxLinesPerPage && $currentLineCount > 0) {
|
||||
$descricaoPaginas[] = $currentPage;
|
||||
$currentPage = '';
|
||||
$currentLineCount = 0;
|
||||
}
|
||||
|
||||
$currentPage .= $htmlNode;
|
||||
$currentLineCount += $linhasEstimadas;
|
||||
}
|
||||
|
||||
if (!empty(trim($currentPage))) {
|
||||
$descricaoPaginas[] = $currentPage;
|
||||
}
|
||||
|
||||
return [
|
||||
'recomendacoes' => $item->recomendacoes ?? '',
|
||||
'descricao' => $item->descricao ?? '',
|
||||
'descricao_paginas' => $descricaoPaginas,
|
||||
];
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
$itensAgrupados = $itens->where('is_imprimir_separado', false);
|
||||
if ($itensAgrupados->isNotEmpty()) {
|
||||
$receitas->push(array_merge($dadosPrincipais, [
|
||||
'medicamentos' => $processarItens($itensAgrupados)->all()
|
||||
]));
|
||||
}
|
||||
|
||||
$itensSeparados = $itens->where('is_imprimir_separado', true);
|
||||
if ($itensSeparados->isNotEmpty()) {
|
||||
$processarItens($itensSeparados)->each(function ($medicamentoSeparado) use ($receitas, $dadosPrincipais) {
|
||||
$receitas->push(array_merge($dadosPrincipais, [
|
||||
'medicamentos' => [$medicamentoSeparado]
|
||||
]));
|
||||
});
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
public function __invoke($atendimentoId, $output = null, $dados = [])
|
||||
{
|
||||
$pdf = new Pdf();
|
||||
$pdf->setPaper('A4', 'landscape');
|
||||
|
||||
return $output
|
||||
? $pdf->output(self::getTemplate($atendimentoId, $dados))
|
||||
: $pdf->stream(self::getTemplate($atendimentoId, $dados));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user