96 lines
2.9 KiB
PHP
96 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Impressao\Farmacia;
|
|
|
|
use App\Enums\Cadastro\SujeitoAtencao\PreferenciaNome;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Almoxarifado\SaidaItem;
|
|
use App\Services\Farmacia\ReceitaService;
|
|
use App\Utils\Exports\Pdf;
|
|
use App\Utils\Exports\Template;
|
|
use Carbon\Carbon;
|
|
|
|
class ReceitaController extends Controller
|
|
{
|
|
private const TITLE_1 = 'RECIBO DE MEDICAMENTO';
|
|
private const TITLE_2 = 'RECIBO DE ENTREGA REMÉDIO EM CASA';
|
|
private const TEMPLATE_1 = 'farmacia.receita';
|
|
private const TEMPLATE_2 = 'farmacia.receita-remedio-em-casa';
|
|
private static $title = self::TITLE_1;
|
|
private static $template = self::TEMPLATE_1;
|
|
|
|
public static function getTemplate($receitaId)
|
|
{
|
|
$params = request()->all();
|
|
|
|
$items = collect([]);
|
|
|
|
foreach(($params['items'] ?? []) as $item) {
|
|
|
|
if (is_numeric($item['id'])) {
|
|
|
|
$items->push($item);
|
|
}
|
|
}
|
|
|
|
$receita = ReceitaService::getReceitaDispensada($receitaId, $items);
|
|
|
|
$receita->data_nascimento_paciente =
|
|
(new Carbon($receita->data_nascimento_paciente))->format('d/m/Y');
|
|
|
|
$receita->data_reimpressao = Carbon::now()->format('d/m/Y');
|
|
|
|
$receita->itens->map(function ($item) {
|
|
|
|
$item->vencimento_receita = $item->proxima_retirada
|
|
? ReceitaService::getVencimentoReceita($item)->format('d/m/Y')
|
|
: null;
|
|
|
|
$item->proxima_retirada = $item->proxima_retirada
|
|
? (new Carbon($item->proxima_retirada))->format('d/m/Y')
|
|
: null;
|
|
|
|
$lotes = collect();
|
|
|
|
SaidaItem::findOrFail($item->saida_item_id)->entradaItens
|
|
->each(function ($entradaItem) use ($lotes) {
|
|
|
|
if ($entradaItem->lote != null) {
|
|
|
|
$lotes->push("{$entradaItem->lote} ({$entradaItem->validade_lote->format('d/m/Y')})");
|
|
}
|
|
});
|
|
|
|
$item->lotes = $lotes->all();
|
|
});
|
|
|
|
if ($receita->is_remedio_em_casa) {
|
|
|
|
self::$title = self::TITLE_2;
|
|
self::$template = self::TEMPLATE_2;
|
|
|
|
} else {
|
|
|
|
$dispensadoEm = Carbon::createFromFormat('d/m/Y H:i', $receita->dispensado_em);
|
|
$receita->dispensado_em = $dispensadoEm->format('d/m/Y');
|
|
$receita->hora_atendimento = $dispensadoEm->format('H:i');
|
|
}
|
|
|
|
$data = [
|
|
'receita' => $receita,
|
|
'paciente' => [
|
|
'nome' => $receita->nome_paciente,
|
|
'nomeSocial' => $receita->nome_social_paciente,
|
|
'prefereNomeSocial' => $receita->preferencia_nome_id === PreferenciaNome::NOME_SOCIAL
|
|
]
|
|
];
|
|
|
|
return Template::make(self::$title, self::$template)->withData($data)->getHtml();
|
|
}
|
|
|
|
public function __invoke($receitaId)
|
|
{
|
|
return (new Pdf)->stream(self::getTemplate($receitaId), self::$title);
|
|
}
|
|
}
|