121 lines
5.3 KiB
PHP
121 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Impressao\Farmacia;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Farmacia\Receita;
|
|
use App\Services\Farmacia\ReceitaService;
|
|
use App\Utils\Exports\Pdf1;
|
|
use App\Utils\MaskUtil;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Str;
|
|
|
|
class ReceitaRemedioEmCasaController extends Controller
|
|
{
|
|
private const TITLE = 'RECIBO DE ENTREGA REMÉDIO EM CASA';
|
|
private const TEMPLATE = 'farmacia.receita-remedio-em-casa';
|
|
|
|
public function __invoke($receitaId)
|
|
{
|
|
$receita = Receita::findOrFail($receitaId);
|
|
|
|
$entregas = collect();
|
|
|
|
$receita->itens->each(function ($item) use ($entregas) {
|
|
|
|
$saldo = $item->entregas()
|
|
->where('pendente', true)
|
|
->first(['quantidade'])
|
|
->quantidade ?? 0;
|
|
|
|
$item->entregas()
|
|
->where('pendente', false)
|
|
->where('estornado', false)
|
|
->get()
|
|
->map(function ($entrega) use ($item, $saldo, $entregas) {
|
|
|
|
$entregas->push((object) [
|
|
'data_hora_dispensacao' => new Carbon($entrega->dispensado_em),
|
|
'material' => $item->material->descricao_completa,
|
|
'lotes' => $entrega->saidaItem->entradaItens->map(fn ($entradaItem) => (object) [
|
|
'lote' => $entradaItem->lote,
|
|
'validade' => is_null($entradaItem->validade_lote)
|
|
? null
|
|
: $entradaItem->validade_lote->format('d/m/Y'),
|
|
]),
|
|
'posologia' => $item->posologia,
|
|
'quantidade_entregue' => $entrega->saidaItem->quantidade,
|
|
'validade_receita' => is_null($item->proxima_retirada)
|
|
? null
|
|
: ReceitaService::getVencimentoReceita((object) [
|
|
'proxima_retirada' => $item->proxima_retirada->format('Y-m-d'),
|
|
'saldo' => $saldo,
|
|
'prescrito' => $item->prescrito,
|
|
'intervalo_medida_tempo_id' => $item->intervalo_medida_tempo_id,
|
|
'durante_tempo' => $item->durante_tempo,
|
|
])->format('d/m/Y'),
|
|
'data_proxima_entrega' => is_null($item->proxima_retirada)
|
|
? null
|
|
: $item->proxima_retirada->format('d/m/Y'),
|
|
'observacao' => $item->observacao,
|
|
'operador' => $item->createdLotacao->usuario->cidadao->nome,
|
|
]);
|
|
});
|
|
});
|
|
|
|
$entregas = $entregas->sortBy([
|
|
['data_hora_dispensacao', 'desc'],
|
|
['descricao', 'asc'],
|
|
])->values();
|
|
|
|
$entregas->each(function ($entrega) {
|
|
|
|
$entrega->data_hora_dispensacao = $entrega->data_hora_dispensacao->format('d/m/Y H:i');
|
|
});
|
|
|
|
$telefonesPaciente = collect();
|
|
|
|
if (!is_null($receita->paciente->cidadao->telefone_principal)) {
|
|
|
|
$telefonesPaciente->push(MaskUtil::getPhoneMasked($receita->paciente->cidadao->telefone_principal->numero));
|
|
}
|
|
|
|
$receita->paciente->cidadao->telefones()
|
|
->where('principal', false)
|
|
->limit($telefonesPaciente->isEmpty() ? 3 : 2)
|
|
->get()
|
|
->each(fn ($t) => $telefonesPaciente->push(MaskUtil::getPhoneMasked($t->numero)));
|
|
|
|
$data = [
|
|
'estabelecimento' => [
|
|
'unidade' => $receita->createdCentroCusto->infraestrutura->unidade->nome_fantasia,
|
|
'infraestrutura' => $receita->createdCentroCusto->infraestrutura->nome,
|
|
'endereco' => "{$receita->createdCentroCusto->infraestrutura->endereco->completo} - "
|
|
. MaskUtil::getCepMasked($receita->createdCentroCusto->infraestrutura->endereco->logradouro->cep),
|
|
'telefone' => MaskUtil::getPhoneMasked($receita->createdCentroCusto->infraestrutura->telefone),
|
|
],
|
|
'paciente' => [
|
|
'nome' => $receita->paciente->cidadao->nome,
|
|
'nome_social' => $receita->paciente->cidadao->nome_social,
|
|
'data_nascimento' => is_null($receita->paciente->cidadao->data_nascimento)
|
|
? null
|
|
: (new Carbon($receita->paciente->cidadao->data_nascimento))->format('d/m/Y'),
|
|
'prontuario' => $receita->paciente->prontuario,
|
|
'cns' => MaskUtil::getCnsMasked($receita->paciente->cidadao->cns),
|
|
'endereco' => is_null($receita->paciente->cidadao->domicilio)
|
|
? null
|
|
: Str::upper("{$receita->paciente->cidadao->domicilio->endereco->completo} - "
|
|
. MaskUtil::getCepMasked($receita->paciente->cidadao->domicilio->endereco->logradouro->cep)),
|
|
'unidade_referencia' => $receita->paciente->unidade->nome_fantasia ?? null,
|
|
'telefones' => $telefonesPaciente,
|
|
],
|
|
'receita' => [
|
|
'entregas' => $entregas,
|
|
'observacao' => $receita->observacao,
|
|
],
|
|
];
|
|
|
|
return Pdf1::generate(self::TITLE, self::TEMPLATE, $data);
|
|
}
|
|
}
|