This commit is contained in:
@@ -0,0 +1,462 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Farmacia;
|
||||
|
||||
use App\Models\Cadastro\Estabelecimento\CentroCusto;
|
||||
use App\Models\Cadastro\Estabelecimento\Infraestrutura;
|
||||
use App\Models\Cadastro\Estabelecimento\Unidade;
|
||||
use App\Models\Cadastro\Grupo\Grupo;
|
||||
use App\Models\Cadastro\Material\Material;
|
||||
use App\Models\Cadastro\Subgrupo\Subgrupo;
|
||||
use App\Services\Cadastro\Material\MaterialService;
|
||||
use App\Services\Service;
|
||||
use App\Utils\DataUtil;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class RelatorioQuantitativoDispensacaoService extends Service
|
||||
{
|
||||
public static function index()
|
||||
{
|
||||
$totalizadores = (object) [
|
||||
'quantidade' => 0,
|
||||
];
|
||||
|
||||
$periodo = [
|
||||
'start' => Carbon::createFromFormat('d/m/Y', request()->periodo['start'])->setTime(0, 0, 0),
|
||||
'end' => Carbon::createFromFormat('d/m/Y', request()->periodo['end'])->setTime(23, 59, 59)
|
||||
];
|
||||
|
||||
$materiais = DB::table('cadastro_material as material')
|
||||
->join(
|
||||
'cadastro_apresentacao as apresentacao',
|
||||
'apresentacao.id',
|
||||
'material.apresentacao_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_grupo as grupo',
|
||||
'grupo.id',
|
||||
'material.grupo_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_subgrupo as subgrupo',
|
||||
'subgrupo.id',
|
||||
'material.subgrupo_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_unidade_medida as apresentacao_unidade_medida',
|
||||
'apresentacao_unidade_medida.id',
|
||||
'material.apresentacao_unidade_medida_id'
|
||||
)
|
||||
->when(request()->material_id, fn ($q, $id) => $q->where('material.id', $id))
|
||||
->when(request()->grupo_id, fn ($q, $id) => $q->where('grupo.id', $id))
|
||||
->when(request()->subgrupo_id, fn ($q, $id) => $q->where('subgrupo.id', $id))
|
||||
->whereExists(function ($query) use ($periodo) {
|
||||
|
||||
$query->select(DB::raw(1))
|
||||
->from('farmacia_receita_item_entrega as entrega')
|
||||
->join(
|
||||
'farmacia_receita_item as item',
|
||||
'item.id',
|
||||
'entrega.receita_item_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_centro_custo',
|
||||
'cadastro_centro_custo.id',
|
||||
'entrega.created_centro_custo_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_infraestrutura',
|
||||
'cadastro_infraestrutura.id',
|
||||
'cadastro_centro_custo.infraestrutura_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade',
|
||||
'cadastro_unidade.id',
|
||||
'cadastro_infraestrutura.unidade_id'
|
||||
)
|
||||
->whereColumn('item.material_id', 'material.id')
|
||||
->where('entrega.pendente', false)
|
||||
->where('entrega.estornado', false)
|
||||
->when(request()->unidade_id, fn ($q, $unidade_id) => $q->where('cadastro_unidade.id', $unidade_id))
|
||||
->whereBetween('entrega.dispensado_em', $periodo);
|
||||
})
|
||||
->select(DB::raw("
|
||||
" . MaterialService::getDescricaoCompletaRaw(
|
||||
'material',
|
||||
'apresentacao',
|
||||
'apresentacao_unidade_medida'
|
||||
) . " as material,
|
||||
subgrupo.nome as subgrupo,
|
||||
grupo.nome as grupo
|
||||
"))
|
||||
->addSelect([
|
||||
'quantidade' => function ($query) use ($periodo) {
|
||||
|
||||
$query->select(DB::raw("SUM(entrega.quantidade)"))
|
||||
->from('farmacia_receita_item_entrega as entrega')
|
||||
->join(
|
||||
'farmacia_receita_item as item',
|
||||
'item.id',
|
||||
'entrega.receita_item_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_centro_custo',
|
||||
'cadastro_centro_custo.id',
|
||||
'entrega.created_centro_custo_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_infraestrutura',
|
||||
'cadastro_infraestrutura.id',
|
||||
'cadastro_centro_custo.infraestrutura_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade',
|
||||
'cadastro_unidade.id',
|
||||
'cadastro_infraestrutura.unidade_id'
|
||||
)
|
||||
->whereColumn('item.material_id', 'material.id')
|
||||
->where('entrega.pendente', false)
|
||||
->where('entrega.estornado', false)
|
||||
->when(request()->unidade_id, fn ($q, $unidade_id) => $q->where('cadastro_unidade.id', $unidade_id))
|
||||
->whereBetween('entrega.created_at', $periodo);
|
||||
}
|
||||
]);
|
||||
|
||||
self::setOrderBy($materiais);
|
||||
|
||||
$materiais = $materiais->get();
|
||||
|
||||
$materiais->each(function ($material) use ($totalizadores) {
|
||||
|
||||
$totalizadores->quantidade += $material->quantidade;
|
||||
});
|
||||
|
||||
return compact('materiais', 'totalizadores');
|
||||
}
|
||||
|
||||
private static function setOrderBy($builder)
|
||||
{
|
||||
$columns = [
|
||||
'material' => 'material',
|
||||
'grupo' => 'grupo.nome',
|
||||
'subgrupo' => 'subgrupo.nome',
|
||||
'quantidade' => 'quantidade',
|
||||
];
|
||||
|
||||
if (is_null(request()->sort_by) || !isset($columns[request()->sort_by])) {
|
||||
$builder->orderBy('grupo.nome')
|
||||
->orderBy('subgrupo.nome')
|
||||
->orderBy('material');
|
||||
return;
|
||||
}
|
||||
|
||||
$column = $columns[request()->sort_by];
|
||||
$direction = DataUtil::getBooleanValueFromStringBoolean(request()->sort_desc) ? 'DESC' : 'ASC';
|
||||
|
||||
$builder->orderBy($column, $direction);
|
||||
|
||||
foreach ($columns as $key => $col) {
|
||||
if ($key !== request()->sort_by) {
|
||||
$builder->orderBy($col);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function list()
|
||||
{
|
||||
$data = (object) [
|
||||
'additionals' => [],
|
||||
'form' => [],
|
||||
'model' => [],
|
||||
];
|
||||
|
||||
$params = self::getParams();
|
||||
|
||||
$sets = [
|
||||
'periodo' => function () use ($data) {
|
||||
|
||||
$data->model['periodo'] = ['start' => null, 'end' => null];
|
||||
},
|
||||
'unidade' => function () use ($data, $params) {
|
||||
|
||||
$unidadesData = self::getUnidadesData($params);
|
||||
|
||||
$data->form['unidades'] = $unidadesData['form'];
|
||||
$data->model['unidade_id'] = $params->unidade_id = $unidadesData['model'];
|
||||
},
|
||||
'infraestrutura' => function () use ($data, $params) {
|
||||
|
||||
$infraestruturasData = self::getInfraestruturasData($params);
|
||||
|
||||
$data->form['infraestruturas'] = $infraestruturasData['form'];
|
||||
$data->model['infraestrutura_id'] = $params->infraestrutura_id = $infraestruturasData['model'];
|
||||
},
|
||||
'centro_custo' => function () use ($data, $params) {
|
||||
|
||||
$centrosDeCustoData = self::getCentrosDeCustoData($params);
|
||||
|
||||
$data->form['centros_custo'] = $centrosDeCustoData['form'];
|
||||
$data->model['centro_custo_id'] = $params->centro_custo_id = $centrosDeCustoData['model'];
|
||||
},
|
||||
'grupo' => function () use ($data, $params) {
|
||||
|
||||
$gruposData = self::getGruposData($params);
|
||||
|
||||
$data->form['grupos'] = $gruposData['form'];
|
||||
$data->model['grupo_id'] = $params->grupo_id = $gruposData['model'];
|
||||
},
|
||||
'subgrupo' => function () use ($data, $params) {
|
||||
|
||||
$subgruposData = self::getSubgruposData($params);
|
||||
|
||||
$data->form['subgrupos'] = $subgruposData['form'];
|
||||
$data->model['subgrupo_id'] = $params->subgrupo_id = $subgruposData['model'];
|
||||
},
|
||||
'material' => function () use ($data) {
|
||||
|
||||
$data->model['material'] = null;
|
||||
}
|
||||
];
|
||||
|
||||
$load = is_null(request()->load_from);
|
||||
|
||||
if ($load) {
|
||||
|
||||
$data->additionals = [
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
];
|
||||
}
|
||||
|
||||
foreach ($sets as $field => $set) {
|
||||
|
||||
if ($load) {
|
||||
|
||||
$set();
|
||||
}
|
||||
else if ($field === request()->load_from) {
|
||||
|
||||
$load = true;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($data as $property => $itens) {
|
||||
|
||||
if (empty($itens)) {
|
||||
|
||||
unset($data->{$property});
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
private static function getParams()
|
||||
{
|
||||
$params = (object) request()->only([
|
||||
'periodo',
|
||||
'unidade_id',
|
||||
'infraestrutura_id',
|
||||
'centro_custo_id',
|
||||
'grupo_id',
|
||||
'subgrupo_id',
|
||||
]);
|
||||
|
||||
if (isset($params->periodo)
|
||||
&& !is_null($params->periodo['start'])
|
||||
&& !is_null($params->periodo['end'])
|
||||
) {
|
||||
|
||||
$start = Carbon::createFromFormat('d/m/Y', $params->periodo['start'])->setTime(0, 0, 0);
|
||||
$end = Carbon::createFromFormat('d/m/Y', $params->periodo['end'])->setTime(23, 59, 59);
|
||||
|
||||
$params->periodo['start'] = $start;
|
||||
$params->periodo['end'] = $end;
|
||||
}
|
||||
else {
|
||||
|
||||
$params->periodo = null;
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
private static function getUnidadesData($params)
|
||||
{
|
||||
$unidades = collect();
|
||||
$id = null;
|
||||
|
||||
if (!is_null($params->periodo)) {
|
||||
|
||||
$unidades = Unidade::whereHas('infraestruturas', fn ($q) => $q
|
||||
->whereHas('centrosCusto', fn ($q) => self::setWhereForFilterCentroCusto($q, $params))
|
||||
)
|
||||
->orderBy('nome_fantasia')
|
||||
->get(['id', 'nome_fantasia']);
|
||||
}
|
||||
|
||||
return ['form' => $unidades, 'model' => $id];
|
||||
}
|
||||
|
||||
private static function getInfraestruturasData($params)
|
||||
{
|
||||
$infraestruturas = collect();
|
||||
$id = null;
|
||||
|
||||
if (!is_null($params->periodo) && !is_null($params->unidade_id)) {
|
||||
|
||||
$infraestruturas = Infraestrutura::where('unidade_id', $params->unidade_id)
|
||||
->whereHas('centrosCusto', fn ($q) => self::setWhereForFilterCentroCusto($q, $params))
|
||||
->get(['id', 'nome', 'principal', 'unidade_id'])
|
||||
->map(fn ($i) => ['id' => $i->id, 'nome' => $i->nome])
|
||||
->sortBy('nome')
|
||||
->values();
|
||||
}
|
||||
|
||||
return ['form' => $infraestruturas, 'model' => $id];
|
||||
}
|
||||
|
||||
private static function getCentrosDeCustoData($params)
|
||||
{
|
||||
$centrosCusto = collect();
|
||||
$id = null;
|
||||
|
||||
if (!is_null($params->periodo) && !is_null($params->infraestrutura_id)) {
|
||||
|
||||
$centrosCusto = CentroCusto::where('infraestrutura_id', $params->infraestrutura_id)
|
||||
->where(fn ($q) => self::setWhereForFilterCentroCusto($q, $params))
|
||||
->get(['id', 'setor_id'])
|
||||
->map(fn ($cc) => ['id' => $cc->id, 'setor_nome' => $cc->setor->nome])
|
||||
->sortBy('setor_nome')
|
||||
->values();
|
||||
}
|
||||
|
||||
return ['form' => $centrosCusto, 'model' => $id];
|
||||
}
|
||||
|
||||
private static function setWhereForFilterCentroCusto($query, $params)
|
||||
{
|
||||
return $query->whereHas('farmaciaReceitaEntregas', fn ($q) => $q
|
||||
->where('pendente', false)
|
||||
->where('estornado', false)
|
||||
->whereBetween('dispensado_em', $params->periodo)
|
||||
);
|
||||
}
|
||||
|
||||
private static function getGruposData($params)
|
||||
{
|
||||
$grupos = collect();
|
||||
$id = null;
|
||||
|
||||
if (!is_null($params->periodo)) {
|
||||
|
||||
$grupos = Grupo::whereHas('materiais', fn ($q) => self::setWhereForFilterMaterial($q, $params))
|
||||
->orderBy('nome')
|
||||
->get(['id', 'nome']);
|
||||
}
|
||||
|
||||
return ['form' => $grupos, 'model' => $id];
|
||||
}
|
||||
|
||||
private static function getSubgruposData($params)
|
||||
{
|
||||
$subgrupos = collect();
|
||||
$id = null;
|
||||
|
||||
if (!is_null($params->periodo) && !is_null($params->grupo_id)) {
|
||||
|
||||
$subgrupos = Subgrupo::where('grupo_id', $params->grupo_id)
|
||||
->whereHas('materiais', fn ($q) => self::setWhereForFilterMaterial($q, $params))
|
||||
->orderBy('nome')
|
||||
->get(['id', 'nome']);
|
||||
}
|
||||
|
||||
return ['form' => $subgrupos, 'model' => $id];
|
||||
}
|
||||
|
||||
public static function setWhereForFilterMaterial($query, $params)
|
||||
{
|
||||
$query->whereHas('farmaciaReceitaItens', fn ($q) => $q
|
||||
->whereHas('entregas', fn ($q) =>
|
||||
self::setWhereForSelectedCentroCusto($q, $params)
|
||||
->where('pendente', false)
|
||||
->where('estornado', false)
|
||||
->whereBetween('dispensado_em', $params->periodo)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private static function setWhereForSelectedCentroCusto($query, $params)
|
||||
{
|
||||
return $query->when($params->centro_custo_id,
|
||||
fn ($q, $centroCustoId) => $q->where('created_centro_custo_id', $centroCustoId),
|
||||
fn ($q) => $q->when(!is_null($params->infraestrutura_id) || !is_null($params->unidade_id),
|
||||
fn ($q) => $q->whereHas('createdCentroCusto', fn ($q) => $q
|
||||
->when($params->infraestrutura_id,
|
||||
fn ($q, $infraestruturaId) => $q->where('infraestrutura_id', $infraestruturaId),
|
||||
fn ($q) => $q->whereHas('infraestrutura', fn ($q) => $q
|
||||
->where('unidade_id', $params->unidade_id))
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public static function autocompleteMaterial()
|
||||
{
|
||||
$params = self::getParams();
|
||||
|
||||
$materiais = Material::when($params->grupo_id, fn ($q, $grupoId) => $q->where('grupo_id', $grupoId))
|
||||
->when($params->subgrupo_id, fn ($q, $subgrupoId) => $q->where('subgrupo_id', $subgrupoId))
|
||||
->where(fn ($q) => self::setWhereForFilterMaterial($q, $params))
|
||||
->getQuery()
|
||||
->join(
|
||||
'cadastro_apresentacao',
|
||||
'cadastro_apresentacao.id',
|
||||
'cadastro_material.apresentacao_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_unidade_medida as apresentacao_unidade_medida',
|
||||
'apresentacao_unidade_medida.id',
|
||||
'cadastro_material.apresentacao_unidade_medida_id'
|
||||
)
|
||||
->select(
|
||||
'cadastro_material.id',
|
||||
DB::raw(MaterialService::getDescricaoCompletaRaw(
|
||||
'cadastro_material',
|
||||
'cadastro_apresentacao',
|
||||
'apresentacao_unidade_medida'
|
||||
) . " as descricao"),
|
||||
);
|
||||
|
||||
return $materiais;
|
||||
}
|
||||
|
||||
public static function getPrintExportData()
|
||||
{
|
||||
$resultado = self::index();
|
||||
$itens = $resultado['materiais'];
|
||||
|
||||
$unidade = Unidade::find(request()->unidade_id);
|
||||
$infraestrutura = Infraestrutura::find(request()->infraestrutura_id);
|
||||
$centroCusto = CentroCusto::find(request()->centro_custo_id);
|
||||
$grupo = Grupo::find(request()->grupo_id);
|
||||
$subgrupo = Subgrupo::find(request()->subgrupo_id);
|
||||
$material = Material::find(request()->material_id);
|
||||
|
||||
return [
|
||||
'inicio' => request()->periodo['start'],
|
||||
'fim' => request()->periodo['end'],
|
||||
'unidade' => $unidade->nome_fantasia ?? 'Todas',
|
||||
'infraestrutura' => $infraestrutura->nome ?? 'Todos',
|
||||
'setor' => $centroCusto->setor->nome ?? 'Todos',
|
||||
'grupo' => $grupo->nome ?? 'Todos',
|
||||
'subgrupo' => $subgrupo->nome ?? 'Todos',
|
||||
'material' => $material->descricao_completa ?? 'Todos',
|
||||
'colunas' => collect(request()->selectedColumns),
|
||||
'itens' => $itens,
|
||||
'totalizadores' => $resultado['totalizadores'],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user