This commit is contained in:
@@ -0,0 +1,219 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\WebService\Almoxarifado\Cliente;
|
||||
|
||||
use App\Enums\Almoxarifado\DocumentoTipo as AlmoxarifadoDocumentoTipo;
|
||||
use App\Enums\WebService\Almoxarifado\Cliente\EntradaDocumentoTipo;
|
||||
use App\Enums\WebService\Almoxarifado\Cliente\EntradaStatus as EntradaStatusEnum;
|
||||
use App\Exceptions\WebService\Almoxarifado\Cliente\Entrada\BalanceteConectaSusClosedException;
|
||||
use App\Exceptions\WebService\Almoxarifado\Cliente\Entrada\EntradaNotFoundException;
|
||||
use App\Models\Almoxarifado\ParametrizacaoControleEstoqueGrupo;
|
||||
use App\Models\WebService\Almoxarifado\Cliente\Entrada;
|
||||
use App\Services\Almoxarifado\AlmoxarifadoService;
|
||||
use App\Services\Almoxarifado\BalanceteService;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class AprovacaoEntradaService extends Service
|
||||
{
|
||||
public static function list()
|
||||
{
|
||||
return [
|
||||
'additionals' => [
|
||||
'data_atual' => Carbon::today()->format('d/m/Y'),
|
||||
],
|
||||
'model' => [
|
||||
'periodo' => ['start' => null, 'end' => null],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function edit($id)
|
||||
{
|
||||
$entrada = self::getEntradaModel($id);
|
||||
|
||||
$centroCustoGrupos = collect();
|
||||
$centroCustoSubgrupos = collect();
|
||||
|
||||
if (!is_null($entrada->unidade->conectasus_centro_custo_id)) {
|
||||
|
||||
ParametrizacaoControleEstoqueGrupo::where('centro_custo_id', $entrada->unidade->conectasus_centro_custo_id)
|
||||
->get()
|
||||
->each(function ($parametrizacaoGrupo) use ($centroCustoGrupos, $centroCustoSubgrupos) {
|
||||
|
||||
$centroCustoGrupos->push($parametrizacaoGrupo->grupo_id);
|
||||
|
||||
$parametrizacaoGrupo->subgrupos->each(function ($parametrizacaoSubgrupo) use ($centroCustoSubgrupos) {
|
||||
|
||||
$centroCustoSubgrupos->push($parametrizacaoSubgrupo->subgrupo_id);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
$model = [
|
||||
'unidade_id' => $entrada->unidade->id,
|
||||
'unidade_nome' => $entrada->unidade->nome,
|
||||
'is_centro_custo_conectasus_pendente' => is_null($entrada->unidade->conectasus_centro_custo_id),
|
||||
'fornecedor_id' => $entrada->fornecedor->id,
|
||||
'fornecedor_nome' => $entrada->fornecedor->nome,
|
||||
'is_fornecedor_conectasus_pendente' => is_null($entrada->fornecedor->conectasus_fornecedor_id),
|
||||
'codigo_nota' => $entrada->codigo_nota,
|
||||
'documento_tipo_nome' => $entrada->documentoTipo->nome,
|
||||
'data_documento' => $entrada->data_documento->format('d/m/Y'),
|
||||
'numero_documento' => $entrada->numero_documento,
|
||||
'numero_pedido' => $entrada->numero_pedido,
|
||||
'status_nome' => $entrada->status->nome,
|
||||
'itens' => $entrada->itens->map(fn ($item) => [
|
||||
'material_id' => $item->material->id,
|
||||
'material_nome' => $item->material->nome,
|
||||
'is_material_conectasus_pendente' => is_null($item->material->conectasus_material_id),
|
||||
'is_material_conectasus_parametrizacao_controle_estoque_pendente' => is_null($item->material->conectasus_material_id)
|
||||
? false
|
||||
: (is_null($item->material->conectasusMaterial->subgrupo_id)
|
||||
? !$centroCustoGrupos->contains($item->material->conectasusMaterial->grupo_id)
|
||||
: !$centroCustoSubgrupos->contains($item->material->conectasusMaterial->subgrupo_id)
|
||||
),
|
||||
'lote' => $item->lote,
|
||||
'validade_lote' => $item->validade_lote,
|
||||
'valor_unitario' => $item->valor_unitario_formatted,
|
||||
'quantidade' => $item->quantidade,
|
||||
'fabricante_id' => $item->fabricante->id,
|
||||
'fabricante_nome' => $item->fabricante->nome,
|
||||
'is_fabricante_conectasus_pendente' => is_null($item->fabricante->conectasus_fabricante_id),
|
||||
]),
|
||||
];
|
||||
|
||||
$hasPendenciasMateriais = $model['itens']
|
||||
->filter(fn ($i) => $i['is_material_conectasus_pendente']
|
||||
|| $i['is_material_conectasus_parametrizacao_controle_estoque_pendente']
|
||||
|| $i['is_fabricante_conectasus_pendente']
|
||||
)
|
||||
->isNotEmpty();
|
||||
|
||||
return [
|
||||
'additionals' => [
|
||||
'allow_update' => $entrada->status_id === EntradaStatusEnum::PENDENTE,
|
||||
'has_pendencias' => $model['is_centro_custo_conectasus_pendente']
|
||||
|| $model['is_fornecedor_conectasus_pendente']
|
||||
|| $hasPendenciasMateriais,
|
||||
'has_pendencias_materiais' => $hasPendenciasMateriais
|
||||
],
|
||||
'model' => $model,
|
||||
];
|
||||
}
|
||||
|
||||
public static function approve($id)
|
||||
{
|
||||
$entrada = self::getEntradaModel($id);
|
||||
|
||||
if (self::isDataDocumentoEmBalanceteDoAlmoxarifadoFechado($entrada->data_documento)) {
|
||||
|
||||
throw new BalanceteConectaSusClosedException();
|
||||
}
|
||||
|
||||
DB::transaction(function () use ($entrada) {
|
||||
|
||||
$almoxarifadoEntrada = AlmoxarifadoService::saveEntradaManualWebService(
|
||||
$entrada->unidade->conectasus_centro_custo_id,
|
||||
[
|
||||
'documento' => [
|
||||
'fornecedor_id' => $entrada->fornecedor->conectasus_fornecedor_id,
|
||||
'tipo_id' => self::getAlmoxarifadoDocumentoTipoId($entrada->documento_tipo_id),
|
||||
'numero_pedido' => $entrada->numero_pedido,
|
||||
'numero' => $entrada->numero_documento,
|
||||
'data' => $entrada->data_documento,
|
||||
],
|
||||
'itens' => $entrada->itens->map(fn ($item) => [
|
||||
'id' => null,
|
||||
'material_id' => $item->material->conectasus_material_id,
|
||||
'quantidade' => $item->quantidade,
|
||||
'lote' => Str::upper($item->lote),
|
||||
'validade_lote' => $item->validade_lote,
|
||||
'valor_unitario' => $item->valor_unitario,
|
||||
'valor_total' => $item->valor_unitario * $item->quantidade,
|
||||
'numero_empenho' => null,
|
||||
'despesas' => null,
|
||||
'numero_autorizacao_fornecimento' => null,
|
||||
'fabricante_id' => $item->fabricante->conectasus_fabricante_id,
|
||||
'codigo_barras' => null,
|
||||
])
|
||||
]
|
||||
);
|
||||
|
||||
$entrada->conectasus_entrada_id = $almoxarifadoEntrada->id;
|
||||
$entrada->status_id = EntradaStatusEnum::APROVADA;
|
||||
|
||||
$entrada->save();
|
||||
});
|
||||
}
|
||||
|
||||
private static function getAlmoxarifadoDocumentoTipoId($documentoTipoId)
|
||||
{
|
||||
switch ($documentoTipoId) {
|
||||
|
||||
case EntradaDocumentoTipo::NOTA_FISCAL: {
|
||||
|
||||
return AlmoxarifadoDocumentoTipo::NOTA_FISCAL;
|
||||
}
|
||||
case EntradaDocumentoTipo::DOACAO: {
|
||||
|
||||
return AlmoxarifadoDocumentoTipo::DOACAO;
|
||||
}
|
||||
case EntradaDocumentoTipo::OUTROS: {
|
||||
|
||||
return AlmoxarifadoDocumentoTipo::OUTRAS;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static function isDataDocumentoEmBalanceteDoAlmoxarifadoFechado($dataDocumento)
|
||||
{
|
||||
return BalanceteService::checkDate($dataDocumento->format('Y-m-d'))['mes_fechado'];
|
||||
}
|
||||
|
||||
public static function reject($id)
|
||||
{
|
||||
$entrada = self::getEntradaModel($id);
|
||||
|
||||
$entrada->update(['status_id' => EntradaStatusEnum::RECUSADA]);
|
||||
}
|
||||
|
||||
public static function datatable($request)
|
||||
{
|
||||
return DB::table('web_service_almoxarifado_cliente_entrada AS entrada')
|
||||
->join('web_service_almoxarifado_cliente_unidade AS unidade', 'unidade.id', 'entrada.unidade_id')
|
||||
->join('web_service_almoxarifado_cliente_fornecedor AS fornecedor', 'fornecedor.id', 'entrada.fornecedor_id')
|
||||
->join(
|
||||
'web_service_almoxarifado_cliente_entrada_documento_tipo AS documento_tipo',
|
||||
'documento_tipo.id',
|
||||
'entrada.documento_tipo_id'
|
||||
)
|
||||
->join('web_service_almoxarifado_cliente_entrada_status AS status', 'status.id', 'entrada.status_id')
|
||||
->whereBetween('entrada.data_documento', [$request->periodo['start'], $request->periodo['end']])
|
||||
->select([
|
||||
'entrada.id',
|
||||
'unidade.nome AS unidade_nome',
|
||||
'fornecedor.nome AS fornecedor_nome',
|
||||
'entrada.codigo_nota',
|
||||
'documento_tipo.nome AS documento_tipo_nome',
|
||||
DB::raw("DATE_FORMAT(entrada.data_documento, '%d/%m/%Y') AS data_documento"),
|
||||
'entrada.numero_documento',
|
||||
'entrada.numero_pedido',
|
||||
'status.nome as status_nome',
|
||||
]);
|
||||
}
|
||||
|
||||
private static function getEntradaModel($id)
|
||||
{
|
||||
$entrada = Entrada::find($id);
|
||||
|
||||
if (is_null($entrada)) {
|
||||
|
||||
throw new EntradaNotFoundException();
|
||||
}
|
||||
|
||||
return $entrada;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,341 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\WebService\Almoxarifado\Cliente;
|
||||
|
||||
use App\Enums\WebService\Almoxarifado\Cliente\EntradaDocumentoTipo as ClienteEntradaDocumentoTipo;
|
||||
use App\Enums\WebService\Almoxarifado\Cliente\EntradaStatus as EntradaStatusEnum;
|
||||
use App\Exceptions\WebService\Almoxarifado\Cliente\Entrada\BalanceteConectaSusClosedException;
|
||||
use App\Exceptions\WebService\Almoxarifado\Cliente\Entrada\CannotDeleteEntradaAprovadaException;
|
||||
use App\Exceptions\WebService\Almoxarifado\Cliente\Entrada\ConflictEntradaException;
|
||||
use App\Exceptions\WebService\Almoxarifado\Cliente\Entrada\DuplicatedMaterialLoteException;
|
||||
use App\Exceptions\WebService\Almoxarifado\Cliente\Entrada\EntradaNotFoundException;
|
||||
use App\Exceptions\WebService\Almoxarifado\Cliente\Entrada\InvalidSearchPeriodException;
|
||||
use App\Exceptions\WebService\Almoxarifado\Cliente\Entrada\PeriodoContabilClosedException;
|
||||
use App\Models\WebService\Almoxarifado\Cliente\Entrada;
|
||||
use App\Models\WebService\Almoxarifado\Cliente\EntradaDocumentoTipo;
|
||||
use App\Models\WebService\Almoxarifado\Cliente\EntradaStatus;
|
||||
use App\Models\WebService\Almoxarifado\Cliente\Fabricante;
|
||||
use App\Models\WebService\Almoxarifado\Cliente\Fornecedor;
|
||||
use App\Models\WebService\Almoxarifado\Cliente\Material;
|
||||
use App\Models\WebService\Almoxarifado\Cliente\PeriodoContabil;
|
||||
use App\Models\WebService\Almoxarifado\Cliente\Unidade;
|
||||
use App\Services\Almoxarifado\BalanceteService;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class EntradaService extends Service
|
||||
{
|
||||
public const LIMITE_DIAS_PERIODO_BUSCA = 31;
|
||||
|
||||
public static function index($request)
|
||||
{
|
||||
self::validateIndex($request);
|
||||
|
||||
$entradas = collect();
|
||||
|
||||
DB::table('web_service_almoxarifado_cliente_entrada_item AS entrada_item')
|
||||
->join('web_service_almoxarifado_cliente_entrada AS entrada', 'entrada.id', 'entrada_item.entrada_id')
|
||||
->whereBetween('entrada.data_documento', [
|
||||
$request->query('data_documento_inicial'),
|
||||
$request->query('data_documento_final')
|
||||
])
|
||||
->when($request->query('status_id'), fn ($q, $id) => $q->where('entrada.status_id', $id))
|
||||
->select([
|
||||
'entrada.id AS entrada_id',
|
||||
'entrada.unidade_id',
|
||||
'entrada.fornecedor_id',
|
||||
'entrada.codigo_nota',
|
||||
'entrada.documento_tipo_id',
|
||||
'entrada.data_documento',
|
||||
'entrada.numero_documento',
|
||||
'entrada.numero_pedido',
|
||||
'entrada.status_id',
|
||||
'entrada_item.material_id',
|
||||
'entrada_item.fabricante_id',
|
||||
'entrada_item.lote',
|
||||
'entrada_item.validade_lote',
|
||||
'entrada_item.quantidade',
|
||||
'entrada_item.valor_unitario',
|
||||
])
|
||||
->orderBy('entrada.data_documento')
|
||||
->each(function ($entradaItem) use ($entradas) {
|
||||
|
||||
$entrada = $entradas->where('id', $entradaItem->entrada_id)->first();
|
||||
|
||||
if (is_null($entrada)) {
|
||||
|
||||
$entrada = (object) [
|
||||
'id' => $entradaItem->entrada_id,
|
||||
'unidade_id' => $entradaItem->unidade_id,
|
||||
'fornecedor_id' => $entradaItem->fornecedor_id,
|
||||
'codigo_nota' => $entradaItem->codigo_nota,
|
||||
'documento_tipo_id' => $entradaItem->documento_tipo_id,
|
||||
'data_documento' => $entradaItem->data_documento,
|
||||
'numero_documento' => $entradaItem->numero_documento,
|
||||
'numero_pedido' => $entradaItem->numero_pedido,
|
||||
'status_id' => $entradaItem->status_id,
|
||||
'itens' => collect(),
|
||||
];
|
||||
|
||||
$entradas->push($entrada);
|
||||
}
|
||||
|
||||
$entrada->itens->push((object) [
|
||||
'material_id' => $entradaItem->material_id,
|
||||
'fabricante_id' => $entradaItem->fabricante_id,
|
||||
'lote' => $entradaItem->lote,
|
||||
'validade_lote' => $entradaItem->validade_lote,
|
||||
'quantidade' => $entradaItem->quantidade,
|
||||
'valor_unitario' => (float) $entradaItem->valor_unitario,
|
||||
]);
|
||||
});
|
||||
|
||||
return $entradas;
|
||||
}
|
||||
|
||||
private static function validateIndex($request)
|
||||
{
|
||||
$request->validate([
|
||||
'data_documento_inicial' => [
|
||||
'required',
|
||||
'date_format:Y-m-d',
|
||||
'before_or_equal:data_documento_final',
|
||||
],
|
||||
'data_documento_final' => [
|
||||
'required',
|
||||
'date_format:Y-m-d',
|
||||
'after_or_equal:data_documento_inicial',
|
||||
],
|
||||
'status_id' => [
|
||||
'integer',
|
||||
Rule::exists(EntradaStatus::class, 'id'),
|
||||
]
|
||||
]);
|
||||
|
||||
$dataDocumentoInicial = Carbon::create($request->data_documento_inicial);
|
||||
$dataDocumentoFinal = Carbon::create($request->data_documento_final);
|
||||
|
||||
if ($dataDocumentoInicial->diff($dataDocumentoFinal)->days > self::LIMITE_DIAS_PERIODO_BUSCA) {
|
||||
|
||||
throw new InvalidSearchPeriodException();
|
||||
}
|
||||
}
|
||||
|
||||
public static function show($id)
|
||||
{
|
||||
$entrada = self::getEntradaModel($id)->load('itens');
|
||||
|
||||
$entrada->itens->each(function ($item) {
|
||||
|
||||
$item->valor_unitario = (float) $item->valor_unitario;
|
||||
});
|
||||
|
||||
return $entrada;
|
||||
}
|
||||
|
||||
public static function store($request)
|
||||
{
|
||||
self::validate($request);
|
||||
|
||||
$data = self::getEntradaData($request);
|
||||
$entrada = new Entrada();
|
||||
|
||||
DB::transaction(function () use ($entrada, $data) {
|
||||
|
||||
$entrada->fill($data);
|
||||
$entrada->save();
|
||||
|
||||
$entrada->itens()->createMany($data['itens']);
|
||||
});
|
||||
|
||||
return self::show($entrada->id);
|
||||
}
|
||||
|
||||
private static function getEntradaData($request)
|
||||
{
|
||||
$data = $request->only([
|
||||
'unidade_id',
|
||||
'fornecedor_id',
|
||||
'codigo_nota',
|
||||
'documento_tipo_id',
|
||||
'data_documento',
|
||||
'numero_documento',
|
||||
'numero_pedido',
|
||||
]);
|
||||
|
||||
$data['status_id'] = EntradaStatusEnum::PENDENTE;
|
||||
|
||||
$data['itens'] = collect($request->itens)->map(fn ($item) => [
|
||||
'material_id' => $item['material_id'],
|
||||
'fabricante_id' => $item['fabricante_id'],
|
||||
'lote' => $item['lote'],
|
||||
'validade_lote' => $item['validade_lote'],
|
||||
'quantidade' => $item['quantidade'],
|
||||
'valor_unitario' => $item['valor_unitario'],
|
||||
]);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public static function destroy($id)
|
||||
{
|
||||
$entrada = self::getEntradaModel($id);
|
||||
|
||||
if ($entrada->status_id === EntradaStatusEnum::APROVADA) {
|
||||
|
||||
throw new CannotDeleteEntradaAprovadaException();
|
||||
}
|
||||
|
||||
$entrada->delete();
|
||||
}
|
||||
|
||||
private static function validate($request)
|
||||
{
|
||||
$request->validate([
|
||||
'unidade_id' => [
|
||||
'bail',
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists(Unidade::class, 'id'),
|
||||
function ($attribute, $value, $fail) {
|
||||
|
||||
if (!Unidade::find($value)->is_almoxarifado) {
|
||||
|
||||
$fail("O campo {$attribute} não é um almoxarifado.");
|
||||
}
|
||||
},
|
||||
],
|
||||
'fornecedor_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists(Fornecedor::class, 'id'),
|
||||
],
|
||||
'codigo_nota' => [
|
||||
'required',
|
||||
'integer',
|
||||
],
|
||||
'documento_tipo_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists(EntradaDocumentoTipo::class, 'id'),
|
||||
],
|
||||
'data_documento' => [
|
||||
'bail',
|
||||
'required',
|
||||
'date_format:Y-m-d',
|
||||
'before_or_equal:' . Carbon::today()->format('Y-m-d'),
|
||||
],
|
||||
'numero_documento' => [
|
||||
'nullable',
|
||||
'present',
|
||||
'digits_between:1,10',
|
||||
],
|
||||
'numero_pedido' => [
|
||||
'nullable',
|
||||
'present',
|
||||
'digits_between:1,20',
|
||||
],
|
||||
'itens' => [
|
||||
'required',
|
||||
'array',
|
||||
],
|
||||
'itens.*.material_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists(Material::class, 'id'),
|
||||
],
|
||||
'itens.*.fabricante_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists(Fabricante::class, 'id'),
|
||||
],
|
||||
'itens.*.lote' => [
|
||||
'required',
|
||||
'string',
|
||||
'max:15',
|
||||
],
|
||||
'itens.*.validade_lote' => [
|
||||
'bail',
|
||||
'required',
|
||||
'date_format:Y-m-d',
|
||||
'after_or_equal:data_documento',
|
||||
],
|
||||
'itens.*.quantidade' => [
|
||||
'required',
|
||||
'integer',
|
||||
'between:1,99999999'
|
||||
],
|
||||
'itens.*.valor_unitario' => [
|
||||
'required',
|
||||
'numeric',
|
||||
'min:' . (
|
||||
(EntradaDocumentoTipo::find($request->documento_tipo_id)->id ?? null) === ClienteEntradaDocumentoTipo::DOACAO
|
||||
? '0.000000'
|
||||
: '0.000001'
|
||||
),
|
||||
'max:99999999.999999'
|
||||
],
|
||||
]);
|
||||
|
||||
$hasDuplicatedMaterialLote = collect($request->itens)
|
||||
->groupBy(['material_id', 'lote'])
|
||||
->filter(fn ($m) => $m->filter(fn ($l) => $l->count() > 1)->isNotEmpty())
|
||||
->isNotEmpty();
|
||||
|
||||
if ($hasDuplicatedMaterialLote) {
|
||||
|
||||
throw new DuplicatedMaterialLoteException();
|
||||
}
|
||||
|
||||
$entradaConflitante = Entrada::where('unidade_id', $request->unidade_id)
|
||||
->where('fornecedor_id', $request->fornecedor_id)
|
||||
->where('codigo_nota', $request->codigo_nota)
|
||||
->where('documento_tipo_id', $request->documento_tipo_id)
|
||||
->where('data_documento', $request->data_documento)
|
||||
->where('numero_documento', $request->numero_documento)
|
||||
->where('numero_pedido', $request->numero_pedido)
|
||||
->whereIn('status_id', [EntradaStatusEnum::PENDENTE, EntradaStatusEnum::APROVADA])
|
||||
->exists();
|
||||
|
||||
if ($entradaConflitante) {
|
||||
|
||||
throw new ConflictEntradaException();
|
||||
}
|
||||
|
||||
if (self::isDataDocumentoEmPeriodoContabilFechado($request->data_documento)) {
|
||||
|
||||
throw new PeriodoContabilClosedException();
|
||||
}
|
||||
|
||||
if (self::isDataDocumentoEmBalanceteDoAlmoxarifadoFechado($request->data_documento)) {
|
||||
|
||||
throw new BalanceteConectaSusClosedException();
|
||||
}
|
||||
}
|
||||
|
||||
private static function isDataDocumentoEmPeriodoContabilFechado($dataDocumento)
|
||||
{
|
||||
return PeriodoContabil::where('inicio', '<=', $dataDocumento)
|
||||
->where('fim', '>=', $dataDocumento)
|
||||
->exists();
|
||||
}
|
||||
|
||||
private static function isDataDocumentoEmBalanceteDoAlmoxarifadoFechado($dataDocumento)
|
||||
{
|
||||
return BalanceteService::checkDate($dataDocumento)['mes_fechado'];
|
||||
}
|
||||
|
||||
private static function getEntradaModel($id)
|
||||
{
|
||||
$entrada = Entrada::find($id);
|
||||
|
||||
if (is_null($entrada)) {
|
||||
|
||||
throw new EntradaNotFoundException();
|
||||
}
|
||||
|
||||
return $entrada;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\WebService\Almoxarifado\Cliente;
|
||||
|
||||
use App\Exceptions\WebService\Almoxarifado\Cliente\Fabricante\CannotDeleteFabricanteException;
|
||||
use App\Exceptions\WebService\Almoxarifado\Cliente\Fabricante\FabricanteNotFoundException;
|
||||
use App\Models\Cadastro\Fabricante\Fabricante as CadastroFabricante;
|
||||
use App\Models\WebService\Almoxarifado\Cliente\Fabricante;
|
||||
use App\Rules\Cnpj;
|
||||
use App\Services\Service;
|
||||
use App\Utils\MaskUtil;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class FabricanteService extends Service
|
||||
{
|
||||
public static function index()
|
||||
{
|
||||
return DB::table('web_service_almoxarifado_cliente_fabricante AS fabricante')
|
||||
->select([
|
||||
'fabricante.id',
|
||||
'fabricante.conectasus_fabricante_id',
|
||||
'fabricante.nome',
|
||||
'fabricante.cnpj',
|
||||
])
|
||||
->orderBy('fabricante.nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function create()
|
||||
{
|
||||
return [
|
||||
'form' => self::getFormData(),
|
||||
'model' => [
|
||||
'conectasus_fabricante_id' => null,
|
||||
'nome' => null,
|
||||
'cnpj' => null,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function store($request)
|
||||
{
|
||||
self::validate($request);
|
||||
|
||||
return Fabricante::create(self::getFabricanteData($request))->fresh();
|
||||
}
|
||||
|
||||
public static function show($id)
|
||||
{
|
||||
return self::getFabricanteModel($id);
|
||||
}
|
||||
|
||||
public static function edit($id)
|
||||
{
|
||||
$fabricante = self::getFabricanteModel($id);
|
||||
|
||||
return [
|
||||
'form' => self::getFormData(),
|
||||
'model' => [
|
||||
'conectasus_fabricante_id' => $fabricante->conectasus_fabricante_id,
|
||||
'nome' => $fabricante->nome,
|
||||
'cnpj' => MaskUtil::getCnpjMasked($fabricante->cnpj),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private static function getFormData()
|
||||
{
|
||||
return [
|
||||
'conectasus_fabricantes' => CadastroFabricante::orderBy('nome')
|
||||
->get(['id', 'nome', 'cnpj'])
|
||||
->map(fn ($fabricante) => (object) [
|
||||
'id' => $fabricante->id,
|
||||
'descricao' => $fabricante->nome
|
||||
. ($fabricante->cnpj ? (' - ' . MaskUtil::getCnpjMasked($fabricante->cnpj)) : '')
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
public static function update($request, $id)
|
||||
{
|
||||
$fabricante = self::getFabricanteModel($id);
|
||||
|
||||
self::validate($request);
|
||||
|
||||
return tap($fabricante)->update(self::getFabricanteData($request))->fresh();
|
||||
}
|
||||
|
||||
private static function getFabricanteData($request)
|
||||
{
|
||||
return $request->only(['conectasus_fabricante_id', 'nome', 'cnpj']);
|
||||
}
|
||||
|
||||
public static function destroy($id)
|
||||
{
|
||||
$fabricante = self::getFabricanteModel($id);
|
||||
|
||||
if ($fabricante->entradaItens()->exists()) {
|
||||
|
||||
throw new CannotDeleteFabricanteException();
|
||||
}
|
||||
|
||||
$fabricante->delete();
|
||||
}
|
||||
|
||||
public static function datatable()
|
||||
{
|
||||
return DB::table('web_service_almoxarifado_cliente_fabricante AS fabricante')
|
||||
->leftJoin(
|
||||
'cadastro_fabricante as conectasus_fabricante',
|
||||
'conectasus_fabricante.id',
|
||||
'fabricante.conectasus_fabricante_id'
|
||||
)
|
||||
->select([
|
||||
'fabricante.id',
|
||||
'fabricante.nome',
|
||||
'fabricante.cnpj',
|
||||
'conectasus_fabricante.nome AS conectasus_fabricante_nome',
|
||||
]);
|
||||
}
|
||||
|
||||
private static function validate($request)
|
||||
{
|
||||
$request->validate([
|
||||
'conectasus_fabricante_id' => [
|
||||
'nullable',
|
||||
'present',
|
||||
'integer',
|
||||
Rule::exists(CadastroFabricante::class, 'id'),
|
||||
],
|
||||
'nome' => [
|
||||
'required',
|
||||
'string',
|
||||
'between:3,100',
|
||||
Rule::unique(Fabricante::class, 'nome')->ignore($request->route('id')),
|
||||
],
|
||||
'cnpj' => [
|
||||
'nullable',
|
||||
'present',
|
||||
'digits:14',
|
||||
new Cnpj,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
private static function getFabricanteModel($id)
|
||||
{
|
||||
$fabricante = Fabricante::find($id);
|
||||
|
||||
if (is_null($fabricante)) {
|
||||
|
||||
throw new FabricanteNotFoundException();
|
||||
}
|
||||
|
||||
return $fabricante;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\WebService\Almoxarifado\Cliente;
|
||||
|
||||
use App\Exceptions\WebService\Almoxarifado\Cliente\Fornecedor\CannotDeleteFornecedorException;
|
||||
use App\Exceptions\WebService\Almoxarifado\Cliente\Fornecedor\FornecedorNotFoundException;
|
||||
use App\Models\Cadastro\Fornecedor\Fornecedor as CadastroFornecedor;
|
||||
use App\Models\WebService\Almoxarifado\Cliente\Fornecedor;
|
||||
use App\Rules\Cnpj;
|
||||
use App\Services\Service;
|
||||
use App\Utils\MaskUtil;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class FornecedorService extends Service
|
||||
{
|
||||
public static function index()
|
||||
{
|
||||
return DB::table('web_service_almoxarifado_cliente_fornecedor AS fornecedor')
|
||||
->select([
|
||||
'fornecedor.id',
|
||||
'fornecedor.conectasus_fornecedor_id',
|
||||
'fornecedor.nome',
|
||||
'fornecedor.cnpj',
|
||||
])
|
||||
->orderBy('fornecedor.nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function create()
|
||||
{
|
||||
return [
|
||||
'form' => self::getFormData(),
|
||||
'model' => [
|
||||
'conectasus_fornecedor_id' => null,
|
||||
'nome' => null,
|
||||
'cnpj' => null,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function store($request)
|
||||
{
|
||||
self::validate($request);
|
||||
|
||||
return Fornecedor::create(self::getFornecedorData($request))->fresh();
|
||||
}
|
||||
|
||||
public static function show($id)
|
||||
{
|
||||
return self::getFornecedorModel($id);
|
||||
}
|
||||
|
||||
public static function edit($id)
|
||||
{
|
||||
$fornecedor = self::getFornecedorModel($id);
|
||||
|
||||
return [
|
||||
'form' => self::getFormData(),
|
||||
'model' => [
|
||||
'conectasus_fornecedor_id' => $fornecedor->conectasus_fornecedor_id,
|
||||
'nome' => $fornecedor->nome,
|
||||
'cnpj' => MaskUtil::getCnpjMasked($fornecedor->cnpj),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private static function getFormData()
|
||||
{
|
||||
return [
|
||||
'conectasus_fornecedores' => CadastroFornecedor::orderBy('nome_fantasia')
|
||||
->get(['id', 'nome_fantasia', 'cnpj'])
|
||||
->map(fn ($fornecedor) => (object) [
|
||||
'id' => $fornecedor->id,
|
||||
'descricao' => $fornecedor->nome_fantasia
|
||||
. ($fornecedor->cnpj ? (' - ' . MaskUtil::getCnpjMasked($fornecedor->cnpj)) : '')
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
public static function update($request, $id)
|
||||
{
|
||||
$fornecedor = self::getFornecedorModel($id);
|
||||
|
||||
self::validate($request);
|
||||
|
||||
return tap($fornecedor)->update(self::getFornecedorData($request))->fresh();
|
||||
}
|
||||
|
||||
private static function getFornecedorData($request)
|
||||
{
|
||||
return $request->only(['conectasus_fornecedor_id', 'nome', 'cnpj']);
|
||||
}
|
||||
|
||||
public static function destroy($id)
|
||||
{
|
||||
$fornecedor = self::getFornecedorModel($id);
|
||||
|
||||
if ($fornecedor->entradas()->exists()) {
|
||||
|
||||
throw new CannotDeleteFornecedorException();
|
||||
}
|
||||
|
||||
$fornecedor->delete();
|
||||
}
|
||||
|
||||
public static function datatable()
|
||||
{
|
||||
return DB::table('web_service_almoxarifado_cliente_fornecedor AS fornecedor')
|
||||
->leftJoin(
|
||||
'cadastro_fornecedor as conectasus_fornecedor',
|
||||
'conectasus_fornecedor.id',
|
||||
'fornecedor.conectasus_fornecedor_id'
|
||||
)
|
||||
->select([
|
||||
'fornecedor.id',
|
||||
'fornecedor.nome',
|
||||
'fornecedor.cnpj',
|
||||
'conectasus_fornecedor.nome_fantasia AS conectasus_fornecedor_nome_fantasia',
|
||||
]);
|
||||
}
|
||||
|
||||
private static function validate($request)
|
||||
{
|
||||
$request->validate([
|
||||
'conectasus_fornecedor_id' => [
|
||||
'nullable',
|
||||
'present',
|
||||
'integer',
|
||||
Rule::exists(CadastroFornecedor::class, 'id'),
|
||||
],
|
||||
'nome' => [
|
||||
'required',
|
||||
'string',
|
||||
'between:3,100',
|
||||
Rule::unique(Fornecedor::class, 'nome')->ignore($request->route('id')),
|
||||
],
|
||||
'cnpj' => [
|
||||
'nullable',
|
||||
'present',
|
||||
'digits:14',
|
||||
new Cnpj,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
private static function getFornecedorModel($id)
|
||||
{
|
||||
$fornecedor = Fornecedor::find($id);
|
||||
|
||||
if (is_null($fornecedor)) {
|
||||
|
||||
throw new FornecedorNotFoundException();
|
||||
}
|
||||
|
||||
return $fornecedor;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\WebService\Almoxarifado\Cliente;
|
||||
|
||||
use App\Exceptions\WebService\Almoxarifado\Cliente\Material\CannotDeleteMaterialException;
|
||||
use App\Exceptions\WebService\Almoxarifado\Cliente\Material\MaterialNotFoundException;
|
||||
use App\Models\Cadastro\Grupo\Grupo as CadastroGrupo;
|
||||
use App\Models\Cadastro\Material\Material as CadastroMaterial;
|
||||
use App\Models\WebService\Almoxarifado\Cliente\Material;
|
||||
use App\Services\Cadastro\Material\MaterialService as CadastroMaterialService;
|
||||
use App\Services\Service;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class MaterialService extends Service
|
||||
{
|
||||
public static function index()
|
||||
{
|
||||
return DB::table('web_service_almoxarifado_cliente_material AS material')
|
||||
->select([
|
||||
'material.id',
|
||||
'material.conectasus_material_id',
|
||||
'material.nome',
|
||||
])
|
||||
->orderBy('material.nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function create()
|
||||
{
|
||||
return [
|
||||
'form' => self::getFormData(),
|
||||
'model' => [
|
||||
'conectasus_material_grupo_id' => null,
|
||||
'conectasus_material' => null,
|
||||
'nome' => null,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function store($request)
|
||||
{
|
||||
self::validate($request);
|
||||
|
||||
return Material::create(self::getMaterialData($request))->fresh();
|
||||
}
|
||||
|
||||
public static function show($id)
|
||||
{
|
||||
return self::getMaterialModel($id);
|
||||
}
|
||||
|
||||
public static function edit($id)
|
||||
{
|
||||
$material = self::getMaterialModel($id);
|
||||
|
||||
return [
|
||||
'form' => self::getFormData(),
|
||||
'model' => [
|
||||
'conectasus_material_grupo_id' => $material->conectasusMaterial->grupo_id ?? null,
|
||||
'conectasus_material' => $material->conectasusMaterial ? [
|
||||
'id' => $material->conectasusMaterial->id,
|
||||
'descricao_completa' => $material->conectasusMaterial->descricao_completa,
|
||||
] : null,
|
||||
'nome' => $material->nome,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private static function getFormData()
|
||||
{
|
||||
return [
|
||||
'conectasus_grupos' => CadastroGrupo::orderBy('nome')->get(['id', 'nome']),
|
||||
];
|
||||
}
|
||||
|
||||
public static function update($request, $id)
|
||||
{
|
||||
$material = self::getMaterialModel($id);
|
||||
|
||||
self::validate($request);
|
||||
|
||||
return tap($material)->update(self::getMaterialData($request))->fresh();
|
||||
}
|
||||
|
||||
private static function getMaterialData($request)
|
||||
{
|
||||
return $request->only(['conectasus_material_id', 'nome']);
|
||||
}
|
||||
|
||||
public static function destroy($id)
|
||||
{
|
||||
$material = self::getMaterialModel($id);
|
||||
|
||||
if ($material->entradaItens()->exists()) {
|
||||
|
||||
throw new CannotDeleteMaterialException();
|
||||
}
|
||||
|
||||
$material->delete();
|
||||
}
|
||||
|
||||
public static function datatable()
|
||||
{
|
||||
return DB::table('web_service_almoxarifado_cliente_material AS material')
|
||||
->leftJoin(
|
||||
'cadastro_material AS conectasus_material',
|
||||
'conectasus_material.id',
|
||||
'material.conectasus_material_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_apresentacao AS conectasus_material_apresentacao',
|
||||
'conectasus_material_apresentacao.id',
|
||||
'conectasus_material.apresentacao_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_unidade_medida as conectasus_material_apresentacao_unidade_medida',
|
||||
'conectasus_material_apresentacao_unidade_medida.id',
|
||||
'conectasus_material.apresentacao_unidade_medida_id'
|
||||
)
|
||||
->select([
|
||||
'material.id',
|
||||
'material.nome',
|
||||
DB::raw(CadastroMaterialService::getDescricaoCompletaRaw(
|
||||
'conectasus_material',
|
||||
'conectasus_material_apresentacao',
|
||||
'conectasus_material_apresentacao_unidade_medida'
|
||||
) . " AS conectasus_material_descricao_completa"),
|
||||
]);
|
||||
}
|
||||
|
||||
private static function validate($request)
|
||||
{
|
||||
$request->validate([
|
||||
'conectasus_material_id' => [
|
||||
'nullable',
|
||||
'present',
|
||||
'integer',
|
||||
Rule::exists(CadastroMaterial::class, 'id'),
|
||||
],
|
||||
'nome' => [
|
||||
'required',
|
||||
'string',
|
||||
'between:3,100',
|
||||
Rule::unique(Material::class, 'nome')->ignore($request->route('id')),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
private static function getMaterialModel($id)
|
||||
{
|
||||
$material = Material::find($id);
|
||||
|
||||
if (is_null($material)) {
|
||||
|
||||
throw new MaterialNotFoundException();
|
||||
}
|
||||
|
||||
return $material;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\WebService\Almoxarifado\Cliente;
|
||||
|
||||
use App\Exceptions\WebService\Almoxarifado\Cliente\PeriodoContabil\ConflictException;
|
||||
use App\Exceptions\WebService\Almoxarifado\Cliente\PeriodoContabil\InvalidDaysLimitException;
|
||||
use App\Exceptions\WebService\Almoxarifado\Cliente\PeriodoContabil\PeriodoContabilNotFoundException;
|
||||
use App\Models\WebService\Almoxarifado\Cliente\PeriodoContabil;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class PeriodoContabilService extends Service
|
||||
{
|
||||
public const LIMITE_DIAS_PERIODO = 31;
|
||||
|
||||
public static function index()
|
||||
{
|
||||
return DB::table('web_service_almoxarifado_cliente_periodo_contabil AS periodo_contabil')
|
||||
->select([
|
||||
'periodo_contabil.id',
|
||||
'periodo_contabil.inicio',
|
||||
'periodo_contabil.fim',
|
||||
])
|
||||
->orderBy('periodo_contabil.inicio')
|
||||
->orderBy('periodo_contabil.fim')
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function create()
|
||||
{
|
||||
return [
|
||||
'model' => [
|
||||
'periodo_contabil' => ['start' => null, 'end' => null],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function store($request)
|
||||
{
|
||||
self::validate($request);
|
||||
|
||||
return PeriodoContabil::create(self::getPeriodoContabilData($request))->fresh();
|
||||
}
|
||||
|
||||
public static function show($id)
|
||||
{
|
||||
return self::getPeriodoContabilModel($id);
|
||||
}
|
||||
|
||||
private static function getPeriodoContabilData($request)
|
||||
{
|
||||
return [
|
||||
'inicio' => $request->inicio,
|
||||
'fim' => $request->fim,
|
||||
];
|
||||
}
|
||||
|
||||
public static function destroy($id)
|
||||
{
|
||||
self::getPeriodoContabilModel($id)->delete();
|
||||
}
|
||||
|
||||
public static function datatable()
|
||||
{
|
||||
return DB::table('web_service_almoxarifado_cliente_periodo_contabil AS periodo_contabil')
|
||||
->select([
|
||||
'periodo_contabil.id',
|
||||
DB::raw("DATE_FORMAT(periodo_contabil.inicio, '%d/%m/%Y') AS inicio"),
|
||||
DB::raw("DATE_FORMAT(periodo_contabil.fim, '%d/%m/%Y') AS fim"),
|
||||
]);
|
||||
}
|
||||
|
||||
private static function validate($request)
|
||||
{
|
||||
$request->validate([
|
||||
'inicio' => [
|
||||
'required',
|
||||
'date_format:Y-m-d',
|
||||
'before_or_equal:fim',
|
||||
],
|
||||
'fim' => [
|
||||
'required',
|
||||
'date_format:Y-m-d',
|
||||
'after_or_equal:inicio',
|
||||
],
|
||||
]);
|
||||
|
||||
$inicio = Carbon::create($request->inicio);
|
||||
$fim = Carbon::create($request->fim);
|
||||
|
||||
if ($inicio->diff($fim)->days > self::LIMITE_DIAS_PERIODO) {
|
||||
|
||||
throw new InvalidDaysLimitException();
|
||||
}
|
||||
|
||||
$periodoContabilConflitante = PeriodoContabil::whereBetween('inicio', [$request->inicio, $request->fim])
|
||||
->orWhereBetween('fim', [$request->inicio, $request->fim])
|
||||
->exists();
|
||||
|
||||
if ($periodoContabilConflitante) {
|
||||
|
||||
throw new ConflictException();
|
||||
}
|
||||
}
|
||||
|
||||
private static function getPeriodoContabilModel($id)
|
||||
{
|
||||
$periodoContabil = PeriodoContabil::find($id);
|
||||
|
||||
if (is_null($periodoContabil)) {
|
||||
|
||||
throw new PeriodoContabilNotFoundException();
|
||||
}
|
||||
|
||||
return $periodoContabil;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\WebService\Almoxarifado\Cliente;
|
||||
|
||||
use App\Models\WebService\Almoxarifado\Cliente\Material;
|
||||
use App\Models\WebService\Almoxarifado\Cliente\Unidade;
|
||||
use App\Services\Service;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class PosicaoEstoqueService extends Service
|
||||
{
|
||||
public static function index($request)
|
||||
{
|
||||
self::validateIndex($request);
|
||||
|
||||
$conectaSusCentroCustoId = Unidade::find($request->query('unidade_id'))->conectasus_centro_custo_id;
|
||||
|
||||
$posicoesEstoques = DB::table('web_service_almoxarifado_cliente_material AS cliente_material')
|
||||
->join(
|
||||
'cadastro_material AS conectasus_material',
|
||||
'conectasus_material.id',
|
||||
'cliente_material.conectasus_material_id'
|
||||
)
|
||||
->leftJoin('almoxarifado_entrada_item AS entrada_item', fn ($j) => $j
|
||||
->on('entrada_item.material_id', 'conectasus_material.id')
|
||||
->whereExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('almoxarifado_entrada AS entrada')
|
||||
->whereColumn('entrada.id', 'entrada_item.entrada_id')
|
||||
->where('entrada.finalizada', true)
|
||||
->where('entrada.centro_custo_id', $conectaSusCentroCustoId)
|
||||
)
|
||||
)
|
||||
->where(fn ($q) => $q
|
||||
->where(fn ($q) => $q
|
||||
->whereNull('conectasus_material.subgrupo_id')
|
||||
->whereExists(fn ($q) => $q
|
||||
->select(DB::raw(1))
|
||||
->from('almoxarifado_parametrizacao_controle_estoque_grupo AS controle_estoque_grupo')
|
||||
->whereColumn('controle_estoque_grupo.grupo_id', 'conectasus_material.grupo_id')
|
||||
->where('controle_estoque_grupo.centro_custo_id', $conectaSusCentroCustoId)
|
||||
)
|
||||
)
|
||||
->orWhere(fn ($q) => $q
|
||||
->whereNotNull('conectasus_material.subgrupo_id')
|
||||
->whereExists(fn ($q) => $q
|
||||
->select(DB::raw(1))
|
||||
->from('almoxarifado_parametrizacao_controle_estoque_subgrupo AS controle_estoque_subgrupo')
|
||||
->join(
|
||||
'almoxarifado_parametrizacao_controle_estoque_grupo AS controle_estoque_grupo',
|
||||
'controle_estoque_grupo.id',
|
||||
'controle_estoque_subgrupo.parametrizacao_controle_estoque_grupo_id'
|
||||
)
|
||||
->whereColumn('controle_estoque_subgrupo.subgrupo_id', 'conectasus_material.subgrupo_id')
|
||||
->where('controle_estoque_grupo.centro_custo_id', $conectaSusCentroCustoId)
|
||||
)
|
||||
)
|
||||
)
|
||||
->when($request->query('material_id'), fn ($q, $id) => $q->where('cliente_material.id', $id))
|
||||
->select([
|
||||
'cliente_material.id',
|
||||
'cliente_material.conectasus_material_id',
|
||||
'cliente_material.nome',
|
||||
DB::raw(
|
||||
"COALESCE(SUM(
|
||||
COALESCE(entrada_item.quantidade, 0)
|
||||
-
|
||||
COALESCE((
|
||||
SELECT SUM(saida_item_entrada_item.quantidade_saida)
|
||||
FROM almoxarifado_saida_item_entrada_item AS saida_item_entrada_item
|
||||
JOIN almoxarifado_saida_item AS saida_item
|
||||
ON saida_item.id = saida_item_entrada_item.saida_item_id
|
||||
JOIN almoxarifado_saida AS saida
|
||||
ON saida.id = saida_item.saida_id
|
||||
WHERE saida_item_entrada_item.entrada_item_id = entrada_item.id
|
||||
AND saida.finalizada = 1
|
||||
), 0)
|
||||
), 0) AS quantidade"
|
||||
)
|
||||
])
|
||||
->groupBy([
|
||||
'cliente_material.id',
|
||||
'cliente_material.conectasus_material_id',
|
||||
'cliente_material.nome',
|
||||
])
|
||||
->get();
|
||||
|
||||
return tap($posicoesEstoques)->each(function ($posicaoEstoque) {
|
||||
|
||||
$posicaoEstoque->quantidade = (int) $posicaoEstoque->quantidade;
|
||||
});
|
||||
}
|
||||
|
||||
private static function validateIndex($request)
|
||||
{
|
||||
$request->validate([
|
||||
'unidade_id' => [
|
||||
'bail',
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists(Unidade::class, 'id'),
|
||||
function ($attribute, $value, $fail) {
|
||||
|
||||
if (is_null(Unidade::find($value)->conectasus_centro_custo_id)) {
|
||||
|
||||
$fail("O campo {$attribute} selecionado não possui vínculo com um centro de custo do ConectaSUS.");
|
||||
}
|
||||
},
|
||||
],
|
||||
'material_id' => [
|
||||
'bail',
|
||||
'integer',
|
||||
Rule::exists(Material::class, 'id'),
|
||||
function ($attribute, $value, $fail) {
|
||||
|
||||
if (is_null(Material::find($value)->conectasus_material_id)) {
|
||||
|
||||
$fail("O campo {$attribute} selecionado não possui vínculo com um material do ConectaSUS.");
|
||||
}
|
||||
},
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\WebService\Almoxarifado\Cliente;
|
||||
|
||||
use App\Enums\Almoxarifado\SaidaTipo;
|
||||
use App\Exceptions\WebService\Almoxarifado\Cliente\Saida\InvalidSearchPeriodException;
|
||||
use App\Models\WebService\Almoxarifado\Cliente\Unidade;
|
||||
use App\Services\Service;
|
||||
use App\Utils\DataUtil;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class SaidaService extends Service
|
||||
{
|
||||
public const LIMITE_DIAS_PERIODO_BUSCA = 31;
|
||||
|
||||
public static function index($request)
|
||||
{
|
||||
self::validateIndex($request);
|
||||
|
||||
$unidadeAlmoxarifado = Unidade::find($request->unidade_almoxarifado_id);
|
||||
$unidadeRequisitante = is_null($request->unidade_id) ? null : Unidade::find($request->unidade_id);
|
||||
|
||||
$dataInicial = Carbon::create($request->data_inicial)->startOfDay();
|
||||
$dataFinal = Carbon::create($request->data_final)->endOfDay();
|
||||
|
||||
$saidas = DB::table('almoxarifado_saida_item_entrada_item AS saida_item_entrada_item')
|
||||
->join(
|
||||
'almoxarifado_entrada_item AS entrada_item',
|
||||
'entrada_item.id',
|
||||
'saida_item_entrada_item.entrada_item_id'
|
||||
)
|
||||
->join(
|
||||
'almoxarifado_saida_item AS saida_item',
|
||||
'saida_item.id',
|
||||
'saida_item_entrada_item.saida_item_id'
|
||||
)
|
||||
->join(
|
||||
'almoxarifado_saida AS saida',
|
||||
'saida.id',
|
||||
'saida_item.saida_id'
|
||||
)
|
||||
->join(
|
||||
'web_service_almoxarifado_cliente_material AS cliente_material',
|
||||
'cliente_material.conectasus_material_id',
|
||||
'saida_item.material_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'almoxarifado_saida_requisicao AS saida_requisicao',
|
||||
'saida_requisicao.saida_id',
|
||||
'saida.id'
|
||||
)
|
||||
->leftJoin(
|
||||
'almoxarifado_saida_transferencia AS saida_transferencia',
|
||||
'saida_transferencia.saida_id',
|
||||
'saida.id'
|
||||
)
|
||||
->leftJoin(
|
||||
'almoxarifado_requisicao_resposta AS requisicao_resposta',
|
||||
'requisicao_resposta.saida_id',
|
||||
'saida.id'
|
||||
)
|
||||
->leftJoin(
|
||||
'almoxarifado_requisicao AS requisicao',
|
||||
'requisicao.id',
|
||||
'requisicao_resposta.requisicao_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'web_service_almoxarifado_cliente_unidade AS cliente_unidade_requisitante',
|
||||
'cliente_unidade_requisitante.conectasus_centro_custo_id',
|
||||
DB::raw("COALESCE(
|
||||
saida_requisicao.centro_custo_id,
|
||||
saida_transferencia.centro_custo_id,
|
||||
requisicao.created_centro_custo_id
|
||||
)")
|
||||
)
|
||||
->where('saida.finalizada', true)
|
||||
->where('saida.centro_custo_id', $unidadeAlmoxarifado->conectasus_centro_custo_id)
|
||||
->whereBetween('saida.created_at', [$dataInicial, $dataFinal])
|
||||
->where('saida.tipo_id', '<>', SaidaTipo::ESTORNO)
|
||||
->when($unidadeRequisitante->conectasus_centro_custo_id ?? null, fn ($q, $id) => $q->where(fn ($q) => $q
|
||||
->where('saida_requisicao.centro_custo_id', $id)
|
||||
->orWhere('saida_transferencia.centro_custo_id', $id)
|
||||
->orWhere('requisicao.created_centro_custo_id', $id)
|
||||
))
|
||||
->when(DataUtil::isStringBoolean($request->query('is_estornada')), fn ($q) => $q
|
||||
->when(DataUtil::isStringTrue($request->query('is_estornada')),
|
||||
fn ($q) => $q->whereNotNull('saida_item.estornado_em'),
|
||||
fn ($q) => $q->whereNull('saida_item.estornado_em')
|
||||
)
|
||||
)
|
||||
->select([
|
||||
DB::raw("CAST(saida.created_at AS DATE) AS data"),
|
||||
DB::raw("CAST(saida_item.estornado_em AS DATE) AS data_estorno"),
|
||||
'cliente_unidade_requisitante.id AS unidade_id',
|
||||
'cliente_material.id AS material_id',
|
||||
'entrada_item.lote',
|
||||
'entrada_item.validade_lote',
|
||||
DB::raw('IF(saida_item.estornado_em IS NULL, 0, 1) AS is_estornada'),
|
||||
DB::raw("SUM(saida_item_entrada_item.quantidade_saida) AS quantidade"),
|
||||
])
|
||||
->orderBy('saida.created_at')
|
||||
->orderBy('saida_item.estornado_em')
|
||||
->groupBy([
|
||||
DB::raw("CAST(saida.created_at AS DATE)"),
|
||||
DB::raw("CAST(saida_item.estornado_em AS DATE)"),
|
||||
'cliente_unidade_requisitante.id',
|
||||
'cliente_material.id',
|
||||
'entrada_item.lote',
|
||||
'entrada_item.validade_lote',
|
||||
DB::raw('IF(saida_item.estornado_em IS NULL, 0, 1)'),
|
||||
])
|
||||
->get();
|
||||
|
||||
return tap($saidas)->each(function ($saida) {
|
||||
|
||||
$saida->is_estornada = (bool) $saida->is_estornada;
|
||||
$saida->quantidade = (int) $saida->quantidade;
|
||||
});
|
||||
}
|
||||
|
||||
private static function validateIndex($request)
|
||||
{
|
||||
$request->validate([
|
||||
'data_inicial' => [
|
||||
'required',
|
||||
'date_format:Y-m-d',
|
||||
'before_or_equal:data_final',
|
||||
],
|
||||
'data_final' => [
|
||||
'required',
|
||||
'date_format:Y-m-d',
|
||||
'after_or_equal:data_inicial',
|
||||
],
|
||||
'unidade_almoxarifado_id' => [
|
||||
'bail',
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists(Unidade::class, 'id'),
|
||||
function ($attribute, $value, $fail) {
|
||||
|
||||
if (!Unidade::find($value)->is_almoxarifado) {
|
||||
|
||||
$fail("O campo {$attribute} não é um almoxarifado.");
|
||||
}
|
||||
},
|
||||
function ($attribute, $value, $fail) {
|
||||
|
||||
if (is_null(Unidade::find($value)->conectasus_centro_custo_id)) {
|
||||
|
||||
$fail("O campo {$attribute} não está vinculado com um centro de custo do ConectaSUS.");
|
||||
}
|
||||
},
|
||||
],
|
||||
'unidade_id' => [
|
||||
'bail',
|
||||
'integer',
|
||||
Rule::exists(Unidade::class, 'id'),
|
||||
function ($attribute, $value, $fail) {
|
||||
|
||||
if (is_null(Unidade::find($value)->conectasus_centro_custo_id)) {
|
||||
|
||||
$fail("O campo {$attribute} não está vinculado com um centro de custo do ConectaSUS.");
|
||||
}
|
||||
},
|
||||
],
|
||||
'is_estornada' => [
|
||||
'string',
|
||||
Rule::in('true', 'false'),
|
||||
]
|
||||
]);
|
||||
|
||||
$dataInicial = Carbon::create($request->data_inicial);
|
||||
$dataFinal = Carbon::create($request->data_final);
|
||||
|
||||
if ($dataInicial->diff($dataFinal)->days > self::LIMITE_DIAS_PERIODO_BUSCA) {
|
||||
|
||||
throw new InvalidSearchPeriodException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,268 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\WebService\Almoxarifado\Cliente;
|
||||
|
||||
use App\Exceptions\WebService\Almoxarifado\Cliente\Unidade\CannotDeleteUnidadeException;
|
||||
use App\Exceptions\WebService\Almoxarifado\Cliente\Unidade\UnidadeNotFoundException;
|
||||
use App\Models\Cadastro\Estabelecimento\CentroCusto as CadastroCentroCusto;
|
||||
use App\Models\Cadastro\Estabelecimento\Unidade as CadastroUnidade;
|
||||
use App\Models\WebService\Almoxarifado\Cliente\Unidade;
|
||||
use App\Services\Service;
|
||||
use App\Utils\DataUtil;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class UnidadeService extends Service
|
||||
{
|
||||
public static function list($request)
|
||||
{
|
||||
switch ($request->listar) {
|
||||
|
||||
case 'infraestruturas': {
|
||||
|
||||
return self::getInfraestruturas($request->unidade_id);
|
||||
}
|
||||
case 'centros-custo': {
|
||||
|
||||
return self::getCentrosCusto($request->infraestrutura_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function index($request)
|
||||
{
|
||||
self::validateIndex($request);
|
||||
|
||||
$unidades = DB::table('web_service_almoxarifado_cliente_unidade AS unidade')
|
||||
->when(DataUtil::isStringBoolean($request->query('is_almoxarifado')), fn ($q) => $q
|
||||
->where(
|
||||
'unidade.is_almoxarifado',
|
||||
DataUtil::getBooleanValueFromStringBoolean($request->query('is_almoxarifado'))
|
||||
)
|
||||
)
|
||||
->select([
|
||||
'unidade.id',
|
||||
'unidade.conectasus_centro_custo_id',
|
||||
'unidade.nome',
|
||||
'unidade.cnes',
|
||||
'unidade.is_almoxarifado',
|
||||
])
|
||||
->orderBy('unidade.nome')
|
||||
->get();
|
||||
|
||||
$unidades->each(function ($unidade) {
|
||||
|
||||
$unidade->is_almoxarifado = (bool) $unidade->is_almoxarifado;
|
||||
});
|
||||
|
||||
return $unidades;
|
||||
}
|
||||
|
||||
private static function validateIndex($request)
|
||||
{
|
||||
$request->validate([
|
||||
'is_almoxarifado' => [
|
||||
'string',
|
||||
Rule::in('true', 'false'),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public static function create()
|
||||
{
|
||||
return [
|
||||
'form' => self::getFormData(),
|
||||
'model' => [
|
||||
'conectasus_unidade_id' => null,
|
||||
'conectasus_infraestrutura_id' => null,
|
||||
'conectasus_centro_custo_id' => null,
|
||||
'nome' => null,
|
||||
'cnes' => null,
|
||||
'is_almoxarifado' => false,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function store($request)
|
||||
{
|
||||
self::validate($request);
|
||||
|
||||
return Unidade::create(self::getUnidadeData($request))->fresh();
|
||||
}
|
||||
|
||||
public static function show($id)
|
||||
{
|
||||
return self::getUnidadeModel($id);
|
||||
}
|
||||
|
||||
public static function edit($id)
|
||||
{
|
||||
$unidade = self::getUnidadeModel($id);
|
||||
|
||||
return [
|
||||
'form' => self::getFormData($unidade),
|
||||
'model' => [
|
||||
'conectasus_unidade_id' => $unidade->conectasusCentroCusto->infraestrutura->unidade_id ?? null,
|
||||
'conectasus_infraestrutura_id' => $unidade->conectasusCentroCusto->infraestrutura_id ?? null,
|
||||
'conectasus_centro_custo_id' => $unidade->conectasus_centro_custo_id,
|
||||
'nome' => $unidade->nome,
|
||||
'cnes' => $unidade->cnes,
|
||||
'is_almoxarifado' => $unidade->is_almoxarifado,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private static function getFormData($unidade = null)
|
||||
{
|
||||
$conectaSusInfraestruturas = [];
|
||||
$conectaSusCentrosCusto = [];
|
||||
|
||||
if (isset($unidade->conectasus_centro_custo_id)) {
|
||||
|
||||
$conectaSusInfraestruturas = self::getInfraestruturas(
|
||||
$unidade->conectasusCentroCusto->infraestrutura->unidade_id
|
||||
);
|
||||
|
||||
$conectaSusCentrosCusto = self::getCentrosCusto($unidade->conectasusCentroCusto->infraestrutura_id);
|
||||
}
|
||||
|
||||
return [
|
||||
'conectasus_unidades' => CadastroUnidade::orderBy('nome_fantasia')->get(['id', 'nome_fantasia']),
|
||||
'conectasus_infraestruturas' => $conectaSusInfraestruturas,
|
||||
'conectasus_centros_custo' => $conectaSusCentrosCusto,
|
||||
];
|
||||
}
|
||||
|
||||
private static function getInfraestruturas($unidadeId)
|
||||
{
|
||||
return DB::table('cadastro_infraestrutura AS infraestrutura')
|
||||
->join('cadastro_unidade AS unidade', 'unidade.id', 'infraestrutura.unidade_id')
|
||||
->whereNull('infraestrutura.deleted_at')
|
||||
->where('infraestrutura.unidade_id', $unidadeId)
|
||||
->orderBy('infraestrutura.nome')
|
||||
->select([
|
||||
'infraestrutura.id',
|
||||
DB::raw("COALESCE(infraestrutura.nome, unidade.nome_fantasia) AS nome"),
|
||||
])
|
||||
->get();
|
||||
}
|
||||
|
||||
private static function getCentrosCusto($infraestruturaId)
|
||||
{
|
||||
return DB::table('cadastro_centro_custo AS centro_custo')
|
||||
->join('cadastro_setor AS setor', 'setor.id', 'centro_custo.setor_id')
|
||||
->whereNull('centro_custo.deleted_at')
|
||||
->where('centro_custo.infraestrutura_id', $infraestruturaId)
|
||||
->orderBy('setor.nome')
|
||||
->select([
|
||||
'centro_custo.id',
|
||||
'setor.nome AS setor_nome',
|
||||
])
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function update($request, $id)
|
||||
{
|
||||
$unidade = self::getUnidadeModel($id);
|
||||
|
||||
self::validate($request);
|
||||
|
||||
return tap($unidade)->update(self::getUnidadeData($request))->fresh();
|
||||
}
|
||||
|
||||
private static function getUnidadeData($request)
|
||||
{
|
||||
return $request->only(['conectasus_centro_custo_id', 'nome', 'cnes', 'is_almoxarifado']);
|
||||
}
|
||||
|
||||
public static function destroy($id)
|
||||
{
|
||||
$unidade = self::getUnidadeModel($id);
|
||||
|
||||
if ($unidade->entradas()->exists()) {
|
||||
|
||||
throw new CannotDeleteUnidadeException();
|
||||
}
|
||||
|
||||
$unidade->delete();
|
||||
}
|
||||
|
||||
public static function datatable()
|
||||
{
|
||||
return DB::table('web_service_almoxarifado_cliente_unidade AS unidade')
|
||||
->leftJoin(
|
||||
'cadastro_centro_custo AS conectasus_centro_custo',
|
||||
'conectasus_centro_custo.id',
|
||||
'unidade.conectasus_centro_custo_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_setor AS conectasus_setor',
|
||||
'conectasus_setor.id',
|
||||
'conectasus_centro_custo.setor_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_infraestrutura AS conectasus_infraestrutura',
|
||||
'conectasus_infraestrutura.id',
|
||||
'conectasus_centro_custo.infraestrutura_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_unidade AS conectasus_unidade',
|
||||
'conectasus_unidade.id',
|
||||
'conectasus_infraestrutura.unidade_id'
|
||||
)
|
||||
->select([
|
||||
'unidade.id',
|
||||
'unidade.nome',
|
||||
'unidade.cnes',
|
||||
DB::raw("CONCAT(
|
||||
conectasus_unidade.nome_fantasia,
|
||||
IF(conectasus_infraestrutura.nome IS NOT NULL,
|
||||
CONCAT(' - ', conectasus_infraestrutura.nome),
|
||||
''
|
||||
),
|
||||
' - ',
|
||||
conectasus_setor.nome
|
||||
) AS conectasus_centro_custo_descricao"),
|
||||
DB::raw("IF(unidade.is_almoxarifado, 'SIM', 'NÃO') AS is_almoxarifado"),
|
||||
]);
|
||||
}
|
||||
|
||||
private static function validate($request)
|
||||
{
|
||||
$request->validate([
|
||||
'conectasus_centro_custo_id' => [
|
||||
'nullable',
|
||||
'present',
|
||||
'integer',
|
||||
Rule::exists(CadastroCentroCusto::class, 'id'),
|
||||
],
|
||||
'nome' => [
|
||||
'required',
|
||||
'string',
|
||||
'between:3,100',
|
||||
Rule::unique(Unidade::class, 'nome')->ignore($request->route('id')),
|
||||
],
|
||||
'cnes' => [
|
||||
'nullable',
|
||||
'present',
|
||||
'digits:7',
|
||||
],
|
||||
'is_almoxarifado' => [
|
||||
'required',
|
||||
'boolean',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
private static function getUnidadeModel($id)
|
||||
{
|
||||
$unidade = Unidade::find($id);
|
||||
|
||||
if (is_null($unidade)) {
|
||||
|
||||
throw new UnidadeNotFoundException();
|
||||
}
|
||||
|
||||
return $unidade;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\WebService\Almoxarifado\ConectaSus;
|
||||
|
||||
use App\Exceptions\WebService\Almoxarifado\ConectaSus\CentroCustoNotFoundException;
|
||||
use App\Models\Cadastro\Estabelecimento\CentroCusto;
|
||||
use App\Services\Service;
|
||||
use App\Utils\DataUtil;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class CentroCustoService extends Service
|
||||
{
|
||||
public static function index($request)
|
||||
{
|
||||
self::validateIndex($request);
|
||||
|
||||
$centrosCusto = collect();
|
||||
|
||||
DB::table('cadastro_centro_custo AS centro_custo')
|
||||
->join('cadastro_setor AS setor', 'setor.id', 'centro_custo.setor_id')
|
||||
->join('cadastro_infraestrutura AS infraestrutura', 'infraestrutura.id', 'centro_custo.infraestrutura_id')
|
||||
->join('cadastro_unidade AS unidade', 'unidade.id', 'infraestrutura.unidade_id')
|
||||
->whereNull('centro_custo.deleted_at')
|
||||
->when(DataUtil::isStringBoolean($request->query('is_almoxarifado')), fn ($q) => $q
|
||||
->when(DataUtil::isStringTrue($request->query('is_almoxarifado')),
|
||||
fn ($q) => $q->whereExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('almoxarifado_parametrizacao_controle_estoque_grupo AS controle_estoque_grupo')
|
||||
->whereColumn('controle_estoque_grupo.centro_custo_id', 'centro_custo.id')
|
||||
),
|
||||
fn ($q) => $q->whereNotExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('almoxarifado_parametrizacao_controle_estoque_grupo AS controle_estoque_grupo')
|
||||
->whereColumn('controle_estoque_grupo.centro_custo_id', 'centro_custo.id')
|
||||
)
|
||||
)
|
||||
)
|
||||
->select([
|
||||
'centro_custo.id',
|
||||
'unidade.id as unidade_id',
|
||||
'unidade.nome_fantasia AS unidade_nome',
|
||||
'unidade.cnes AS unidade_cnes',
|
||||
'infraestrutura.id as infraestrutura_id',
|
||||
DB::raw("COALESCE(infraestrutura.nome, unidade.nome_fantasia) AS infraestrutura_nome"),
|
||||
'setor.nome AS setor_nome',
|
||||
])
|
||||
->orderBy('unidade.nome_fantasia')
|
||||
->orderByRaw("COALESCE(infraestrutura.nome, unidade.nome_fantasia)")
|
||||
->orderBy('setor.nome')
|
||||
->get()
|
||||
->each(function ($centroCusto) use ($centrosCusto) {
|
||||
|
||||
$unidade = $centrosCusto->where('unidade_id', $centroCusto->unidade_id)->first();
|
||||
|
||||
if (is_null($unidade)) {
|
||||
|
||||
$unidade = (object) [
|
||||
'unidade_id' => $centroCusto->unidade_id,
|
||||
'unidade_nome' => $centroCusto->unidade_nome,
|
||||
'unidade_cnes' => $centroCusto->unidade_cnes,
|
||||
'locais_atendimento' => collect(),
|
||||
];
|
||||
|
||||
$centrosCusto->push($unidade);
|
||||
}
|
||||
|
||||
$infraestrutura = $unidade->locais_atendimento
|
||||
->where('infraestrutura_id', $centroCusto->infraestrutura_id)
|
||||
->first();
|
||||
|
||||
if (is_null($infraestrutura)) {
|
||||
|
||||
$infraestrutura = (object) [
|
||||
'infraestrutura_id' => $centroCusto->infraestrutura_id,
|
||||
'nome' => $centroCusto->infraestrutura_nome,
|
||||
'setores' => collect(),
|
||||
];
|
||||
|
||||
$unidade->locais_atendimento->push($infraestrutura);
|
||||
}
|
||||
|
||||
$infraestrutura->setores->push((object) [
|
||||
'centro_custo_id' => $centroCusto->id,
|
||||
'nome' => $centroCusto->setor_nome,
|
||||
]);
|
||||
});
|
||||
|
||||
return tap($centrosCusto)->each(function ($centroCusto) {
|
||||
|
||||
unset($centroCusto->unidade_id);
|
||||
|
||||
$centroCusto->locais_atendimento->each(function ($localAtendimento) {
|
||||
|
||||
unset($localAtendimento->infraestrutura_id);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private static function validateIndex($request)
|
||||
{
|
||||
$request->validate([
|
||||
'is_almoxarifado' => [
|
||||
'string',
|
||||
Rule::in('true', 'false'),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public static function show($id)
|
||||
{
|
||||
$centroCusto = self::getCentroCustoModel($id);
|
||||
|
||||
return (object) [
|
||||
'id' => $centroCusto->id,
|
||||
'unidade_nome' => $centroCusto->infraestrutura->unidade->nome_fantasia,
|
||||
'unidade_cnes' => $centroCusto->infraestrutura->unidade->cnes,
|
||||
'local_atendimento_nome' => $centroCusto->infraestrutura->nome,
|
||||
'setor_nome' => $centroCusto->setor->nome,
|
||||
];
|
||||
}
|
||||
|
||||
private static function getCentroCustoModel($id)
|
||||
{
|
||||
$centroCusto = CentroCusto::find($id);
|
||||
|
||||
if (is_null($centroCusto)) {
|
||||
|
||||
throw new CentroCustoNotFoundException();
|
||||
}
|
||||
|
||||
return $centroCusto;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\WebService\Almoxarifado\ConectaSus;
|
||||
|
||||
use App\Exceptions\WebService\Almoxarifado\ConectaSus\FabricanteNotFoundException;
|
||||
use App\Models\Cadastro\Fabricante\Fabricante;
|
||||
use App\Services\Service;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class FabricanteService extends Service
|
||||
{
|
||||
public static function index()
|
||||
{
|
||||
return DB::table('cadastro_fabricante AS fabricante')
|
||||
->select([
|
||||
'fabricante.id',
|
||||
'fabricante.nome',
|
||||
'fabricante.cnpj',
|
||||
])
|
||||
->orderBy('fabricante.nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function show($id)
|
||||
{
|
||||
$fabricante = self::getFabricanteModel($id);
|
||||
|
||||
return (object) [
|
||||
'id' => $fabricante->id,
|
||||
'nome' => $fabricante->nome,
|
||||
'cnpj' => $fabricante->cnpj,
|
||||
];
|
||||
}
|
||||
|
||||
private static function getFabricanteModel($id)
|
||||
{
|
||||
$fabricante = Fabricante::find($id);
|
||||
|
||||
if (is_null($fabricante)) {
|
||||
|
||||
throw new FabricanteNotFoundException();
|
||||
}
|
||||
|
||||
return $fabricante;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\WebService\Almoxarifado\ConectaSus;
|
||||
|
||||
use App\Exceptions\WebService\Almoxarifado\ConectaSus\FornecedorNotFoundException;
|
||||
use App\Models\Cadastro\Fornecedor\Fornecedor;
|
||||
use App\Services\Service;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class FornecedorService extends Service
|
||||
{
|
||||
public static function index()
|
||||
{
|
||||
return DB::table('cadastro_fornecedor AS fornecedor')
|
||||
->select([
|
||||
'fornecedor.id',
|
||||
'fornecedor.nome_fantasia AS nome',
|
||||
'fornecedor.cnpj',
|
||||
])
|
||||
->orderBy('fornecedor.nome_fantasia')
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function show($id)
|
||||
{
|
||||
$fornecedor = self::getFornecedorModel($id);
|
||||
|
||||
return (object) [
|
||||
'id' => $fornecedor->id,
|
||||
'nome' => $fornecedor->nome_fantasia,
|
||||
'cnpj' => $fornecedor->cnpj,
|
||||
];
|
||||
}
|
||||
|
||||
private static function getFornecedorModel($id)
|
||||
{
|
||||
$fornecedor = Fornecedor::find($id);
|
||||
|
||||
if (is_null($fornecedor)) {
|
||||
|
||||
throw new FornecedorNotFoundException();
|
||||
}
|
||||
|
||||
return $fornecedor;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\WebService\Almoxarifado\ConectaSus;
|
||||
|
||||
use App\Exceptions\WebService\Almoxarifado\ConectaSus\MaterialNotFoundException;
|
||||
use App\Models\Cadastro\Material\Material;
|
||||
use App\Services\Cadastro\Material\MaterialService as CadastroMaterialService;
|
||||
use App\Services\Service;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class MaterialService extends Service
|
||||
{
|
||||
public static function index()
|
||||
{
|
||||
return DB::table('cadastro_material AS material')
|
||||
->join(
|
||||
'cadastro_apresentacao AS apresentacao',
|
||||
'apresentacao.id',
|
||||
'material.apresentacao_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade_medida as apresentacao_unidade_medida',
|
||||
'apresentacao_unidade_medida.id',
|
||||
'material.apresentacao_unidade_medida_id'
|
||||
)
|
||||
->whereNull('material.deleted_at')
|
||||
->where('material.is_ativo', true)
|
||||
->select([
|
||||
'material.id',
|
||||
'material.codigo_material AS codigo',
|
||||
DB::raw(CadastroMaterialService::getDescricaoCompletaRaw(
|
||||
'material',
|
||||
'apresentacao',
|
||||
'apresentacao_unidade_medida'
|
||||
) . " AS descricao"),
|
||||
])
|
||||
->orderByRaw(CadastroMaterialService::getDescricaoCompletaRaw(
|
||||
'material',
|
||||
'apresentacao',
|
||||
'apresentacao_unidade_medida'
|
||||
))
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function show($id)
|
||||
{
|
||||
$material = self::getMaterialModel($id);
|
||||
|
||||
return (object) [
|
||||
'id' => $material->id,
|
||||
'codigo' => $material->codigo_material,
|
||||
'descricao' => $material->descricao_completa,
|
||||
];
|
||||
}
|
||||
|
||||
private static function getMaterialModel($id)
|
||||
{
|
||||
$material = Material::find($id);
|
||||
|
||||
if (is_null($material)) {
|
||||
|
||||
throw new MaterialNotFoundException();
|
||||
}
|
||||
|
||||
return $material;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user