117 lines
4.4 KiB
PHP
117 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Farmacia;
|
|
|
|
use App\Models\Farmacia\ReceitaItemEntrega;
|
|
use App\Services\Almoxarifado\AlmoxarifadoService;
|
|
use App\Services\Cadastro\Material\MaterialService;
|
|
use App\Services\Service;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class EstornoDispensacaoService extends Service
|
|
{
|
|
public static function edit($pacienteId)
|
|
{
|
|
return DB::table('farmacia_receita_item_entrega as entrega')
|
|
->join('almoxarifado_saida_item as saida_item', 'saida_item.id', 'entrega.saida_item_id')
|
|
->join('almoxarifado_saida as saida', 'saida.id', 'saida_item.saida_id')
|
|
->join('farmacia_receita_item as item', 'item.id', 'entrega.receita_item_id')
|
|
->join('farmacia_receita as receita', 'receita.id', 'item.receita_id')
|
|
->join('cadastro_material as material', 'material.id', 'item.material_id')
|
|
->join('cadastro_apresentacao as apresentacao', 'apresentacao.id', 'material.apresentacao_id')
|
|
->join('cadastro_usuario as usuario', 'usuario.id', 'receita.usuario_id')
|
|
->join('cadastro_cidadao as cidadao', 'cidadao.id', 'usuario.cidadao_id')
|
|
->join('cadastro_lotacao as lotacao_dispensacao', 'lotacao_dispensacao.id', 'entrega.created_lotacao_id')
|
|
->join('cadastro_usuario as usuario_dispensacao', 'usuario_dispensacao.id', 'lotacao_dispensacao.usuario_id')
|
|
->join('cadastro_cidadao as cidadao_dispensacao', 'cidadao_dispensacao.id', 'usuario_dispensacao.cidadao_id')
|
|
->leftJoin(
|
|
'cadastro_unidade_medida as apresentacao_unidade_medida',
|
|
'apresentacao_unidade_medida.id',
|
|
'material.apresentacao_unidade_medida_id'
|
|
)
|
|
->whereNotNull('entrega.dispensado_em')
|
|
->where('paciente_id', $pacienteId)
|
|
->select(DB::raw("
|
|
entrega.id,
|
|
entrega.dispensado_em,
|
|
CAST(saida.created_at AS DATE) as data_saida,
|
|
" . MaterialService::getDescricaoCompletaRaw(
|
|
'material',
|
|
'apresentacao',
|
|
'apresentacao_unidade_medida'
|
|
) . " as material,
|
|
cidadao.nome as medico,
|
|
cidadao_dispensacao.nome as usuario_dispensacao,
|
|
entrega.quantidade,
|
|
IF(saida_item.estornado_em IS NULL, 0, 1) as estornado
|
|
"))
|
|
->get();
|
|
}
|
|
|
|
public static function update($entregaId)
|
|
{
|
|
$entrega = ReceitaItemEntrega::findOrFail($entregaId);
|
|
|
|
DB::transaction(function () use ($entrega) {
|
|
|
|
self::estornar($entrega);
|
|
});
|
|
|
|
return $entrega->fresh();
|
|
}
|
|
|
|
public static function estornar($entrega)
|
|
{
|
|
$item = $entrega->receitaItem;
|
|
|
|
AlmoxarifadoService::estornarSaidaItens(collect([
|
|
['id' => $entrega->saida_item_id]
|
|
]));
|
|
|
|
$entrega->update(['estornado' => true]);
|
|
|
|
$entregaPendente = ReceitaItemEntrega::where('receita_item_id', $item->id)
|
|
->where('pendente', true)
|
|
->first();
|
|
|
|
if (is_null($entregaPendente)) {
|
|
|
|
ReceitaItemEntrega::create([
|
|
'receita_item_id' => $item->id,
|
|
'quantidade' => $entrega->quantidade,
|
|
'pendente' => true
|
|
]);
|
|
}
|
|
else {
|
|
|
|
$entregaPendente->update([
|
|
'quantidade' => $entregaPendente->quantidade + $entrega->quantidade
|
|
]);
|
|
}
|
|
|
|
$ultimaEntrega = ReceitaItemEntrega::where('receita_item_id', $item->id)
|
|
->where('pendente', false)
|
|
->where('estornado', false)
|
|
->orderByDesc('dispensado_em')
|
|
->first();
|
|
|
|
$proximaRetirada = is_null($ultimaEntrega)
|
|
? null
|
|
: ReceitaService::getDataProximaRetirada(new Carbon($ultimaEntrega->dispensado_em), [
|
|
'material' => [
|
|
'quantidade' => $item->material->quantidade,
|
|
],
|
|
'quantidade' => $item->quantidade,
|
|
'intervalo_tempo' => $item->intervalo_tempo,
|
|
'intervalo_medida_tempo_id' => $item->intervalo_medida_tempo_id,
|
|
'durante_tempo' => $item->durante_tempo,
|
|
'continuo' => false,
|
|
'prescrito' => $item->prescrito,
|
|
'entregue' => $ultimaEntrega->quantidade,
|
|
]);
|
|
|
|
$item->update(['proxima_retirada' => $proximaRetirada]);
|
|
}
|
|
}
|