Merge branch 'master' into matias

This commit is contained in:
2021-07-20 17:46:08 -04:00
52 changed files with 1085 additions and 249 deletions

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,22 @@
<?php
namespace App\Exceptions;
use Exception;
class NotAuthorizedException extends Exception {
protected $user;
public function __construct($user) {
$this->user = $user;
}
public function render($request) {
$path = $request->getPathInfo();
return response()->json([
'error' => 'not_authorized',
'message' => 'El usuario ' . $this->user->id . ' no tiene permiso para acceder al endpoint ' . $path
], 401);
}
}

View File

@@ -0,0 +1,233 @@
<?php
namespace App\Http\Controllers;
use App\Models\Restaurante;
use App\Services\UuidService;
use App\Services\PaginatorService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Ramsey\Uuid\Uuid;
class BodegaController extends Controller {
/**
* Obtiene de forma paginada los ingresos de ingredientes
*/
public function ingresos(Request $request, $restaurante_id) {
app(UuidService::class)->validOrFail($restaurante_id);
$restaurante = Restaurante::findOrFail($restaurante_id);
$ingresos = $restaurante->bodegaIngresos()
->orderBy('fecha', 'desc')
->with('ingrediente');
$paginate = app(PaginatorService::class)->paginate(
perPage: $request->input('per_page', 15),
page: $request->input('page', 1),
total: $ingresos->count(),
route: 'bodega.ingresos',
data: ['restaurante_id' => $restaurante_id],
);
$data = $ingresos->get()
->skip($paginate['from'] - 1)
->take($paginate['per_page'])
->all();
return response()->json([
'pagination' => $paginate,
'data' => $data
]);
}
/**
* Obtiene los ingresos de un ingrediente
*/
public function ingresos_ingrediente(Request $request, $restaurante_id, $ingrediente_id) {
app(UuidService::class)->validOrFail($restaurante_id);
$restaurante = Restaurante::findOrFail($restaurante_id);
$ingresos = $restaurante->bodegaIngresos()
->where('ingrediente_id', $ingrediente_id)
->with('ingrediente');
$paginate = app(PaginatorService::class)->paginate(
perPage: $request->input('per_page', 15),
page: $request->input('page', 1),
total: $ingresos->count(),
route: 'bodega.ingresos',
data: ['restaurante_id' => $restaurante_id],
);
$data = $ingresos->get()
->skip($paginate['from'] - 1)
->take($paginate['per_page'])
->all();
return response()->json([
'pagination' => $paginate,
'data' => $data
]);
}
/**
* Obtiene de forma paginada los egresos de ingredientes
*/
public function egresos(Request $request, $restaurante_id) {
app(UuidService::class)->validOrFail($restaurante_id);
$restaurante = Restaurante::findOrFail($restaurante_id);
$egresos = $restaurante->bodegaEgresos()->with('ingrediente');
$paginate = app(PaginatorService::class)->paginate(
perPage: $request->input('per_page', 15),
page: $request->input('page', 1),
total: $egresos->count(),
route: 'bodega.egresos',
data: ['restaurante_id' => $restaurante_id],
);
$data = $egresos->get()
->skip($paginate['from'] - 1)
->take($paginate['per_page'])
->all();
return response()->json([
'pagination' => $paginate,
'data' => $data
]);
}
/**
* Obtiene los egresos de un ingrediente
*/
public function egresos_ingrediente(Request $request, $restaurante_id, $ingrediente_id) {
app(UuidService::class)->validOrFail($restaurante_id);
$restaurante = Restaurante::findOrFail($restaurante_id);
$egresos = $restaurante->bodegaEgresos()
->where('ingrediente_id', $ingrediente_id)
->with('ingrediente');
$paginate = app(PaginatorService::class)->paginate(
perPage: $request->input('per_page', 15),
page: $request->input('page', 1),
total: $egresos->count(),
route: 'bodega.egresos',
data: ['restaurante_id' => $restaurante_id],
);
$data = $egresos->get()
->skip($paginate['from'] - 1)
->take($paginate['per_page'])
->all();
return response()->json([
'pagination' => $paginate,
'data' => $data
]);
}
/**
* Obtiene de forma paginada los movimientos de ingredientes
*/
public function movimientos(Request $request, $restaurante_id) {
app(UuidService::class)->validOrFail($restaurante_id);
$restaurante = Restaurante::findOrFail($restaurante_id);
$movimientos = $restaurante->bodegaMovimientos()->with('ingrediente');
$paginate = app(PaginatorService::class)->paginate(
perPage: $request->input('per_page', 15),
page: $request->input('page', 1),
total: $movimientos->count(),
route: 'bodega.movimientos',
data: ['restaurante_id' => $restaurante_id],
);
$data = $movimientos->get()
->skip($paginate['from'] - 1)
->take($paginate['per_page'])
->all();
return response()->json([
'pagination' => $paginate,
'data' => $data
]);
}
/**
* Obtiene de forma paginada los movimientos de un ingrediente
*/
public function movimientos_ingrediente(Request $request, $restaurante_id, $ingrediente_id) {
app(UuidService::class)->validOrFail($restaurante_id);
$restaurante = Restaurante::findOrFail($restaurante_id);
$movimientos = $restaurante->bodegaMovimientos()
->where('ingrediente_id', $ingrediente_id)
->with('ingrediente');
$paginate = app(PaginatorService::class)->paginate(
perPage: $request->input('per_page', 15),
page: $request->input('page', 1),
total: $movimientos->count(),
route: 'bodega.movimientos',
data: ['restaurante_id' => $restaurante_id],
);
$data = $movimientos->get()
->skip($paginate['from'] - 1)
->take($paginate['per_page'])
->all();
return response()->json([
'pagination' => $paginate,
'data' => $data
]);
}
/**
* Obtiene de forma paginada el estado actual de la bodega
*/
public function actual(Request $request, $restaurante_id) {
app(UuidService::class)->validOrFail($restaurante_id);
$restaurante = Restaurante::findOrFail($restaurante_id);
$actual = $restaurante->bodegaActual()->with('ingrediente');
$paginate = app(PaginatorService::class)->paginate(
perPage: $request->input('per_page', 15),
page: $request->input('page', 1),
total: $actual->count(),
route: 'bodega.actual',
data: ['restaurante_id' => $restaurante_id],
);
$data = $actual->get()
->skip($paginate['from'] - 1)
->take($paginate['per_page'])
->all();
return response()->json([
'pagination' => $paginate,
'data' => $data
]);
}
/**
* Obtiene el estado actual de un ingrediente
*/
public function actual_ingrediente(Request $request, $restaurante_id, $ingrediente_id) {
app(UuidService::class)->validOrFail($restaurante_id);
$restaurante = Restaurante::findOrFail($restaurante_id);
$actual = $restaurante->bodegaActual()
->where('ingrediente_id', $ingrediente_id)
->with('ingrediente')
->first();
return response()->json($actual);
}
}

View File

@@ -0,0 +1,245 @@
<?php
namespace App\Http\Controllers;
use App\Models\Restaurante;
use App\Models\Compra;
use App\Models\Proveedor;
use App\Models\Factura;
use App\Models\Ingrediente;
use App\Models\CompraIngrediente;
use App\Services\PaginatorService;
use App\Services\UuidService;
use App\Exceptions\GenericException;
use App\Exceptions\CantdeletehasChild;
use App\Exceptions\AlreadyExistsException;
use App\Exceptions\ModelNotFoundException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Ramsey\Uuid\Uuid;
class ComprasController extends Controller {
/**
* Obtiene de forma paginada las compras registradas en el backend
*/
public function all(Request $request, $restaurante_id) {
app(UuidService::class)->validOrFail($restaurante_id);
$restaurante = Restaurante::findOrFail($restaurante_id);
$compras = $restaurante->compras();
$paginate = app(PaginatorService::class)->paginate(
perPage: $request->input('per_page', 15),
page: $request->input('page', 1),
total: $compras->count(),
route: 'compras.all',
data: ['restaurante_id' => $restaurante_id]
);
$data = $compras->get()
->skip($paginate['from'] - 1)
->take($paginate['per_page'])
->all();
return response()->json([
'pagination' => $paginate,
'data' => $data
]);
}
/**
* Obtiene una compra por su id
*/
public function get(Request $request, $restaurante_id, $id) {
app(UuidService::class)->validOrFail($restaurante_id);
app(UuidService::class)->validOrFail($id);
$restaurante = Restaurante::findOrFail($restaurante_id);
$compra = Compra::findOrFail($id);
if($compra->restaurante != $restaurante) {
throw new ModelNotFoundException("compra", $id);
}
return response()->json($compra);
}
/**
* Crea una nueva compra
*/
public function create(Request $request, $restaurante_id) {
$this->validate($request, [
'fecha_compra' => 'required|date',
'en_arqueo' => 'required|boolean',
'proveedor_id' => 'required'
]);
app(UuidService::class)->validOrFail($restaurante_id);
app(UuidService::class)->validOrFail($request->input('proveedor_id'));
$restaurante = Restaurante::findOrFail($restaurante_id);
$proveedor = Proveedor::findOrFail($request->input('proveedor_id'));
if($proveedor->restaurante != $restaurante) {
throw new ModelNotFoundException("proveedor", $request->input('proveedor_id'));
}
$compra = Compra::create([
'id' => Uuid::uuid4(),
'fecha_compra' => $request->input('fecha_compra'),
'proveedor_id' => $proveedor->id,
'en_arqueo' => $request->input('en_arqueo'),
'restaurante_id' => $restaurante->id
]);
return response()->json($compra, 201);
}
/**
* Actualiza una compra
*/
public function update(Request $request, $restaurante_id, $id) {
$this->validate($request, [
'fecha_compra' => 'required|date',
'en_arqueo' => 'required|boolean',
'proveedor_id' => 'required'
]);
app(UuidService::class)->validOrFail($restaurante_id);
app(UuidService::class)->validOrFail($id);
app(UuidService::class)->validOrFail($request->input('proveedor_id'));
$restaurante = Restaurante::findOrFail($restaurante_id);
$proveedor = Proveedor::findOrFail($request->input('proveedor_id'));
$compra = Compra::findOrFail($id);
if($proveedor->restaurante != $restaurante) {
throw new ModelNotFoundException("proveedor", $proveedor->id);
}
if($compra->restaurante != $restaurante) {
throw new ModelNotFoundException("compra", $compra->id);
}
$compra->fecha_compra= $request->input('fecha_compra');
$compra->en_arqueo = $request->input('en_arqueo');
$compra->proveedor_id = $proveedor->id;
$compra->save();
return response()->json($compra);
}
/**
* Elimina una compra
*/
public function delete(Request $request, $restaurante_id, $id) {
app(UuidService::class)->validOrFail($restaurante_id);
app(UuidService::class)->validOrFail($id);
$restaurante = Restaurante::findOrFail($restaurante_id);
$compra = Compra::findOrFail($id);
if($compra->restaurante != $restaurante) {
throw new ModelNotFoundException("compra", $id);
}
$compra->ingredientes()->delete();
$compra->facturas()->delete();
$compra->delete();
return response()->json([], 204);
}
/**
* Obtiene los ingredientes de una compra
*/
public function getIngredientes(Request $request, $restaurante_id, $id) {
app(UuidService::class)->validOrFail($restaurante_id);
app(UuidService::class)->validOrFail($id);
$restaurante = Restaurante::findOrFail($restaurante_id);
$compra = Compra::findOrFail($id);
if($compra->restaurante != $restaurante) {
throw new ModelNotFoundException("compra", $id);
}
return response()->json($compra->ingredientes);
}
/**
* Agrega un ingrediente a una compra
*/
public function addIngrediente(Request $request, $restaurante_id, $id, $ingrediente_id) {
$this->validate($request, [
'unidades' => 'required|numeric',
'monto_unitario' => 'required|numeric'
]);
app(UuidService::class)->validOrFail($restaurante_id);
app(UuidService::class)->validOrFail($id);
app(UuidService::class)->validOrFail($ingrediente_id);
$restaurante = Restaurante::findOrFail($restaurante_id);
$compra = Compra::findOrFail($id);
$ingrediente = Ingrediente::findOrFail($ingrediente_id);
if($compra->restaurante != $restaurante) {
throw new ModelNotFoundException("compra", $compra->id);
}
if($ingrediente->restaurante != $restaurante) {
throw new ModelNotFoundException("ingrediente", $ingrediente->id);
}
foreach($compra->ingredientes as $ci){
if($ci->ingrediente->id == $ingrediente->id) {
throw new AlreadyExistsException("ingrediente");
}
}
$compraIngrediente = CompraIngrediente::create([
'id' => Uuid::uuid4(),
'unidades' => $request->input('unidades'),
'monto_unitario' => $request->input('monto_unitario'),
'compra_id' => $compra->id,
'ingrediente_id' => $ingrediente->id
]);
return response()->json($compraIngrediente, 201);
}
/**
* Elimina un ingrediente de una compra
*/
public function deleteIngrediente(Request $request, $restaurante_id, $id, $ingrediente_id) {
app(UuidService::class)->validOrFail($restaurante_id);
app(UuidService::class)->validOrFail($id);
app(UuidService::class)->validOrFail($ingrediente_id);
$restaurante = Restaurante::findOrFail($restaurante_id);
$compra = Compra::findOrFail($id);
$ingrediente = Ingrediente::findOrFail($ingrediente_id);
if($compra->restaurante != $restaurante) {
throw new ModelNotFoundException("compra", $compra->id);
}
if($ingrediente->restaurante != $restaurante) {
throw new ModelNotFoundException("ingrediente", $ingrediente->id);
}
$toDelete = CompraIngrediente::where('compra_id', $compra->id)->where('ingrediente_id', $ingrediente->id);
if($toDelete->count() == 0) {
throw new ModelNotFoundException("compra_ingrediente", null);
}
$toDelete->delete();
return response()->json([], 204);
}
}

View File

@@ -0,0 +1,131 @@
<?php
namespace App\Http\Controllers;
use App\Models\Restaurante;
use App\Models\Compra;
use App\Models\Proveedor;
use App\Models\Factura;
use App\Services\PaginatorService;
use App\Services\UuidService;
use App\Exceptions\GenericException;
use App\Exceptions\CantdeletehasChild;
use App\Exceptions\AlreadyExistsException;
use App\Exceptions\ModelNotFoundException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Ramsey\Uuid\Uuid;
class FacturasController extends Controller {
/**
* Obtiene la factura de una compra
*/
public function get(Request $request, $restaurante_id, $id) {
app(UuidService::class)->validOrFail($restaurante_id);
app(UuidService::class)->validOrFail($id);
$restaurante = Restaurante::findOrFail($restaurante_id);
$compra = Compra::findOrFail($id);
if($compra->restaurante != $restaurante) {
throw new ModelNotFoundException("compra", $id);
}
if($compra->facturas()->count() == 0) {
throw new ModelNotFoundException("factura", null);
}
return response()->json($compra->facturas()->first());
}
/**
* Crea una factura a una compra
*/
public function create(Request $request, $restaurante_id, $id) {
$this->validate($request, [
'numero' => 'required',
'monto_bruto' => 'required|numeric',
]);
app(UuidService::class)->validOrFail($restaurante_id);
app(UuidService::class)->validOrFail($id);
$restaurante = Restaurante::findOrFail($restaurante_id);
$compra = Compra::findOrFail($id);
if($compra->restaurante != $restaurante) {
throw new ModelNotFoundException("compra", $id);
}
if($compra->facturas()->count() > 0) {
throw new AlreadyExistsException("factura");
}
$factura = Factura::create([
'id' => Uuid::uuid4(),
'numero' => $request->input('numero'),
'monto_bruto' => $request->input('monto_bruto'),
'compra_id' => $compra->id
]);
return response()->json($factura, 201);
}
/**
* Modifica la factura a una compra
*/
public function update(Request $request, $restaurante_id, $id) {
$this->validate($request, [
'numero' => 'required',
'monto_bruto' => 'required|numeric',
]);
app(UuidService::class)->validOrFail($restaurante_id);
app(UuidService::class)->validOrFail($id);
$restaurante = Restaurante::findOrFail($restaurante_id);
$compra = Compra::findOrFail($id);
if($compra->restaurante != $restaurante) {
throw new ModelNotFoundException("compra", $id);
}
if($compra->facturas()->count() == 0) {
throw new ModelNotFoundException("factura", null);
}
$factura = $compra->facturas()->first();
$factura->numero = $request->input('numero');
$factura->monto_bruto = $request->input('monto_bruto');
$factura->save();
return response()->json($factura, 201);
}
/**
* Elimina la factura de una compra
*/
public function delete(Request $request, $restaurante_id, $id) {
app(UuidService::class)->validOrFail($restaurante_id);
app(UuidService::class)->validOrFail($id);
$restaurante = Restaurante::findOrFail($restaurante_id);
$compra = Compra::findOrFail($id);
if($compra->restaurante != $restaurante) {
throw new ModelNotFoundException("compra", $id);
}
if($compra->facturas()->count() == 0) {
throw new ModelNotFoundException("factura", null);
}
$factura = $compra->facturas()->first();
$factura->delete();
return response()->json([], 204);
}
}

View File

@@ -19,7 +19,11 @@ class RestaurantesController extends Controller {
* Obtiene de forma paginada los restaurantes registrados en el backend * Obtiene de forma paginada los restaurantes registrados en el backend
*/ */
public function all(Request $request) { public function all(Request $request) {
$restaurantes = Restaurante::all(); if($request->user->isGlobalAdmin()) {
$restaurantes = Restaurante::all();
} else {
$restaurantes = $request->user->restaurantes;
}
$paginate = app(PaginatorService::class)->paginate( $paginate = app(PaginatorService::class)->paginate(
perPage: $request->input('per_page', 15), perPage: $request->input('per_page', 15),
@@ -46,8 +50,8 @@ class RestaurantesController extends Controller {
app(UuidService::class)->validOrFail($id); app(UuidService::class)->validOrFail($id);
$restaurante = Restaurante::findOrFail($id); $restaurante = Restaurante::findOrFail($id);
if(!$request->user->isOnRestaurant($restaurante)){ if(!$request->user->isOnRestaurante($restaurante)){
return ModelNotFoundException('restaurante', $restaurante->id); throw new ModelNotFoundException('restaurante', $restaurante->id);
} }
return response()->json($restaurante); return response()->json($restaurante);
@@ -80,6 +84,11 @@ class RestaurantesController extends Controller {
]); ]);
$restaurant = Restaurante::findOrFail($id); $restaurant = Restaurante::findOrFail($id);
if(!$request->user->isOnRestaurante($restaurant)){
throw new ModelNotFoundException('restaurante', $restaurant->id);
}
$restaurant->nombre = $request->input('nombre'); $restaurant->nombre = $request->input('nombre');
$restaurant->save(); $restaurant->save();
@@ -94,12 +103,19 @@ class RestaurantesController extends Controller {
$restaurant = Restaurante::findOrFail($id); $restaurant = Restaurante::findOrFail($id);
if($restaurant->usuarios()->count() > 0) throw new CantDeleteHasChildException("restaurant", "usuario"); if($restaurant->canalesVenta()->count() > 0) throw new CantDeleteHasChildException("restaurant", "canal_venta");
if($restaurant->canalesVenta()->count() > 0) throw new CantDeleteHasChildException("restaurant", "canal_venta"); if($restaurant->categorias()->count() > 0) throw new CantDeleteHasChildException("restaurant", "categoria");
if($restaurant->sectores()->count() > 0) throw new CantDeleteHasChildException("restaurant", "sector"); if($restaurant->compras()->count() > 0) throw new CantDeleteHasChildException("restaurant", "compra");
if($restaurant->zonasProduccion()->count() > 0) throw new CantDeleteHasChildException("restaurant", "zona_produccion"); if($restaurant->usuarios()->count() > 0) throw new CantDeleteHasChildException("restaurant", "usuario");
if($restaurant->categorias()->count() > 0) throw new CantDeleteHasChildException("restaurant", "categoria"); if($restaurant->sectores()->count() > 0) throw new CantDeleteHasChildException("restaurant", "sector");
if($restaurant->zonasProduccion()->count() > 0) throw new CantDeleteHasChildException("restaurant", "zona_produccion");
if($restaurant->proveedores()->count() > 0) throw new CantDeleteHasChildException("restaurant", "proveedor");
if($restaurant->ingredientes()->count() > 0) throw new CantDeleteHasChildException("restaurant", "ingrediente");
if($restaurant->productos()->count() > 0) throw new CantDeleteHasChildException("restaurant", "producto");
if($restaurant->ventas()->count() > 0) throw new CantDeleteHasChildException("restaurant", "venta");
if($restaurant->boletasElectronicas()->count() > 0) throw new CantDeleteHasChildException("restaurant", "boleta_electronica");
if($restaurant->boletasExentas()->count() > 0) throw new CantDeleteHasChildException("restaurant", "boleta_exenta");
if($restaurant->cajas()->count() > 0) throw new CantDeleteHasChildException("restaurant", "caja");
$restaurant->delete(); $restaurant->delete();
return response()->json([], 204); return response()->json([], 204);

View File

@@ -21,7 +21,11 @@ class UsuariosController extends Controller {
* Obtiene de forma paginada los usuarios registrados en el backend * Obtiene de forma paginada los usuarios registrados en el backend
*/ */
public function all(Request $request) { public function all(Request $request) {
$usuarios = Usuario::all(); if($request->user->isGlobalAdmin()) {
$usuarios = Usuario::all();
} else {
$usuarios = Restaurante::all()->intersect($request->user->restaurantes);
}
$paginate = app(PaginatorService::class)->paginate( $paginate = app(PaginatorService::class)->paginate(
perPage: $request->input('per_page', 15), perPage: $request->input('per_page', 15),
@@ -44,8 +48,11 @@ class UsuariosController extends Controller {
* Obtiene un usuario por su id * Obtiene un usuario por su id
*/ */
public function get(Request $request, $id) { public function get(Request $request, $id) {
if (!str_starts_with($id, 'auth0')) app(UuidService::class)->validOrFail($id); if (!str_starts_with($id, 'auth0') && $id != 'me') app(UuidService::class)->validOrFail($id);
$usuario = Usuario::findOrFail(urldecode($id));
if ($id == 'me') $usuario = $request->user;
else $usuario = Usuario::findOrFail(urldecode($id));
return response()->json($usuario); return response()->json($usuario);
} }
@@ -96,7 +103,7 @@ class UsuariosController extends Controller {
* Actualiza un usuario * Actualiza un usuario
*/ */
public function update(Request $request, $id) { public function update(Request $request, $id) {
if (!str_starts_with($id, 'auth0')) app(UuidService::class)->validOrFail($id); if (!str_starts_with($id, 'auth0') && $id != 'me') app(UuidService::class)->validOrFail($id);
$this->validate($request, [ $this->validate($request, [
'nombre' => 'sometimes', 'nombre' => 'sometimes',
@@ -107,7 +114,8 @@ class UsuariosController extends Controller {
'roles.*' => ['sometimes', Rule::in(['admin', 'mesero', 'recaudador', 'productor'])], 'roles.*' => ['sometimes', Rule::in(['admin', 'mesero', 'recaudador', 'productor'])],
]); ]);
$usuario = Usuario::findOrFail(urldecode($id)); if ($id == 'me') $usuario = $request->user;
else $usuario = Usuario::findOrFail(urldecode($id));
$metadata = []; $metadata = [];
if ($request->input('roles')) $metadata['roles'] = $request->input('roles'); if ($request->input('roles')) $metadata['roles'] = $request->input('roles');
@@ -138,9 +146,10 @@ class UsuariosController extends Controller {
* Elimina un usuario * Elimina un usuario
*/ */
public function delete(Request $request, $id) { public function delete(Request $request, $id) {
if (!str_starts_with($id, 'auth0')) app(UuidService::class)->validOrFail($id); if (!str_starts_with($id, 'auth0') && $id != 'me') app(UuidService::class)->validOrFail($id);
$usuario = Usuario::findOrFail(urldecode($id)); if ($id == 'me') $usuario = $request->user;
else $usuario = Usuario::findOrFail(urldecode($id));
$auth0 = app(Auth0Service::class); $auth0 = app(Auth0Service::class);
$auth0Response = $auth0->deleteUser($usuario->auth0_id); $auth0Response = $auth0->deleteUser($usuario->auth0_id);
@@ -161,8 +170,10 @@ class UsuariosController extends Controller {
* Obtiene los usuarios de un restaurant * Obtiene los usuarios de un restaurant
*/ */
public function getRestaurantes(Request $request, $id) { public function getRestaurantes(Request $request, $id) {
if (!str_starts_with($id, 'auth0')) app(UuidService::class)->validOrFail($id); if (!str_starts_with($id, 'auth0') && $id != 'me') app(UuidService::class)->validOrFail($id);
$usuario = Usuario::findOrFail(urldecode($id));
if ($id == 'me') $usuario = $request->user;
else $usuario = Usuario::findOrFail(urldecode($id));
return response()->json($usuario->restaurantes); return response()->json($usuario->restaurantes);
} }
@@ -171,10 +182,12 @@ class UsuariosController extends Controller {
* Agrega usuario a un restaurant * Agrega usuario a un restaurant
*/ */
public function addToRestaurant(Request $request, $id, $restaurant) { public function addToRestaurant(Request $request, $id, $restaurant) {
if (!str_starts_with($id, 'auth0')) app(UuidService::class)->validOrFail($id); if (!str_starts_with($id, 'auth0') && $id != 'me') app(UuidService::class)->validOrFail($id);
app(UuidService::class)->validOrFail($restaurant); app(UuidService::class)->validOrFail($restaurant);
$usuario = Usuario::findOrFail(urldecode($id)); if ($id == 'me') $usuario = $request->user;
else $usuario = Usuario::findOrFail(urldecode($id));
$restaurant = Restaurante::findOrFail($restaurant); $restaurant = Restaurante::findOrFail($restaurant);
if ($usuario->restaurantes->contains($restaurant)) { if ($usuario->restaurantes->contains($restaurant)) {
@@ -193,10 +206,12 @@ class UsuariosController extends Controller {
* Saca a un usuario de un restaurant * Saca a un usuario de un restaurant
*/ */
public function removeFromRestaurant(Request $request, $id, $restaurant) { public function removeFromRestaurant(Request $request, $id, $restaurant) {
if (!str_starts_with($id, 'auth0')) app(UuidService::class)->validOrFail($id); if (!str_starts_with($id, 'auth0') && $id != 'me') app(UuidService::class)->validOrFail($id);
app(UuidService::class)->validOrFail($restaurant); app(UuidService::class)->validOrFail($restaurant);
if ($id == 'me') $usuario = $request->user;
else $usuario = Usuario::findOrFail(urldecode($id));
$usuario = Usuario::findOrFail(urldecode($id));
$restaurant = Restaurante::findOrFail($restaurant); $restaurant = Restaurante::findOrFail($restaurant);
if (!$usuario->restaurantes->contains($restaurant)) { if (!$usuario->restaurantes->contains($restaurant)) {

View File

@@ -60,6 +60,23 @@ class ZonasProduccionController extends Controller {
return response()->json($zonaProduccion); return response()->json($zonaProduccion);
} }
/**
* Obtiene los usuarios de una zona de produccion
*/
public function users(Request $request, $restaurante_id, $id) {
app(UuidService::class)->validOrFail($restaurante_id);
app(UuidService::class)->validOrFail($id);
$restaurante = Restaurante::findOrFail($restaurante_id);
$zonaProduccion = ZonaProduccion::findOrFail($id);
if($zonaProduccion->restaurante != $restaurante) {
throw new ModelNotFoundException("zona_produccion", $id);
}
return response()->json($zonaProduccion->usuarios);
}
/** /**
* Crea una nueva zona de produccion * Crea una nueva zona de produccion
*/ */

View File

@@ -17,12 +17,20 @@ class Auth0Middleware {
$token = $request->bearerToken(); $token = $request->bearerToken();
if (!$token) { if (!$token) {
Log::warning('Se intento acceder a una ruta protegida sin un token', [
'path' => $request->getPathInfo()
]);
return response()->json(['error' => 'no_token', 'message' => 'No se envío el token'], 401); return response()->json(['error' => 'no_token', 'message' => 'No se envío el token'], 401);
} }
try { try {
$validated = $this->validateToken($token); $validated = $this->validateToken($token);
} catch (InvalidTokenException $e) { } catch (InvalidTokenException $e) {
Log::warning('Se intento acceder a una ruta protegida con un token invalido', [
'path' => $request->getPathInfo(),
'message' => $e->getMessage(),
'token' => $token
]);
return response()->json([ return response()->json([
'error' => 'auth0_invalid_token', 'error' => 'auth0_invalid_token',
'message' => $e->getMessage() 'message' => $e->getMessage()
@@ -30,6 +38,7 @@ class Auth0Middleware {
} }
$user = Usuario::where('auth0_id', $validated['sub'])->first(); $user = Usuario::where('auth0_id', $validated['sub'])->first();
Log::debug('Se identifico al usuario', ['id' => $user->id, 'auth0_id' => $user->auth0_id]);
return $next($request->merge(['user' => $user])); return $next($request->merge(['user' => $user]));
} }

View File

@@ -1,22 +0,0 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\Auth\Factory as Auth;
class Authenticate {
protected $auth;
public function __construct(Auth $auth) {
$this->auth = $auth;
}
public function handle($request, Closure $next, $guard = null) {
if ($this->auth->guard($guard)->guest()) {
return response('Unauthorized.', 401);
}
return $next($request);
}
}

View File

@@ -14,15 +14,13 @@ class CorsMiddleware {
'Access-Control-Allow-Headers' => 'Content-Type, Authorization, X-Requested-With' 'Access-Control-Allow-Headers' => 'Content-Type, Authorization, X-Requested-With'
]; ];
if ($request->isMethod('OPTIONS')) if ($request->isMethod('OPTIONS')) {
{
return response()->json([], 200, $headers); return response()->json([], 200, $headers);
} }
$response = $next($request); $response = $next($request);
foreach($headers as $key => $value) foreach($headers as $key => $value) {
{
$response->header($key, $value); $response->header($key, $value);
} }

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Log;
use App\Exceptions\ModelNotFoundException;
use App\Models\Restaurante;
class InRestauranteMiddleware {
public function handle($request, Closure $next) {
$restaurante = Restaurante::findOrFail($request->route('restaurante_id'));
$user = $request->user;
if(!$user->isOnRestaurante($restaurante)) {
Log::debug('El usuario intento acceder a un restaurante que no le pertenece', [
'user' => $user->id,
'restaurante' => $restaurante->id
]);
throw new ModelNotFoundException('restaurante', $restaurante->id);
} else {
Log::debug('El usuario accedio a un restaurante que si le pertenece', [
'user' => $user->id,
'restaurante' => $restaurante->id
]);
}
return $next($request);
}
}

View File

@@ -7,17 +7,16 @@ use Illuminate\Support\Facades\Log;
class LogEndpointHitMiddleware { class LogEndpointHitMiddleware {
public function handle($request, Closure $next) { public function handle($request, Closure $next) {
$userId = $request->user ? $request->user->id : null; $user = $request->user;
$method = $request->getMethod(); $method = $request->getMethod();
$path = $request->getPathInfo(); $path = $request->getPathInfo();
Log::debug('User ' . $userId . ' hitting ' . $method . ' ' . $path . ' endpoint', [ Log::debug('User ' . $user->id . ' hitting ' . $method . ' ' . $path . ' endpoint', [
'user' => $userId, 'user' => $user->id,
'roles' => implode('|', $user->roles),
'method' => $method, 'method' => $method,
'path' => $path, 'path' => $path,
'input' => array_filter($request->input(), function ($key) { 'input' => array_filter($request->input(), function ($key) { return $key !== 'user'; }, ARRAY_FILTER_USE_KEY)
return $key !== 'user';
}, ARRAY_FILTER_USE_KEY)
]); ]);
return $next($request); return $next($request);

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Log;
use App\Exceptions\NotAuthorizedException;
class RoleMiddleware {
public function handle($request, Closure $next, $raw_roles) {
$user = $request->user;
$roles = explode('|', $raw_roles);
$has_permission = false;
foreach($roles as $role){
$has_permission = $has_permission || $user->hasRole($role);
}
if(!$has_permission) {
Log::warning('El usuario intento acceder a una ruta sin los roles necesarios', [
'user' => $user->id,
'required_roles' => $raw_roles,
'user_roles' => implode('|', $user->roles)
]);
throw new NotAuthorizedException($request->user);
}
return $next($request);
}
}

View File

@@ -1,18 +0,0 @@
<?php
namespace App\Models;
use App\Traits\UuidPrimaryKey;
use App\Exceptions\ModelNotFoundException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Administrador extends Model {
use UuidPrimaryKey, SoftDeletes;
protected $table = 'administradores';
public function usuario() {
return $this->belongsTo(Usuario::class);
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class BodegaActual extends Model {
protected $table = 'bodega_actual';
protected $hidden = ['created_at', 'updated_at', 'deleted_at', 'pivot'];
public function restaurante() {
return $this->belongsTo(Restaurante::class);
}
public function ingrediente() {
return $this->belongsTo(Ingrediente::class);
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class BodegaEgreso extends Model {
protected $table = 'bodega_egresos';
protected $hidden = ['created_at', 'updated_at', 'deleted_at', 'pivot'];
public function restaurante() {
return $this->belongsTo(Restaurante::class);
}
public function ingrediente() {
return $this->belongsTo(Ingrediente::class);
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class BodegaIngreso extends Model {
protected $table = 'bodega_ingresos';
protected $hidden = ['created_at', 'updated_at', 'deleted_at', 'pivot'];
public function restaurante() {
return $this->belongsTo(Restaurante::class);
}
public function ingrediente() {
return $this->belongsTo(Ingrediente::class);
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class BodegaMovimiento extends Model {
protected $table = 'bodega_movimientos';
protected $hidden = ['created_at', 'updated_at', 'deleted_at', 'pivot'];
public function restaurante() {
return $this->belongsTo(Restaurante::class);
}
public function ingrediente() {
return $this->belongsTo(Ingrediente::class);
}
}

View File

@@ -11,6 +11,7 @@ class BoletaElectronica extends Model {
use UuidPrimaryKey, SoftDeletes; use UuidPrimaryKey, SoftDeletes;
protected $table = 'boletas_electronicas'; protected $table = 'boletas_electronicas';
protected $hidden = ['created_at', 'updated_at', 'deleted_at', 'pivot'];
public function venta() { public function venta() {
return $this->belongsTo(Venta::class); return $this->belongsTo(Venta::class);

View File

@@ -11,6 +11,7 @@ class BoletaExenta extends Model {
use UuidPrimaryKey, SoftDeletes; use UuidPrimaryKey, SoftDeletes;
protected $table = 'boletas_exentas'; protected $table = 'boletas_exentas';
protected $hidden = ['created_at', 'updated_at', 'deleted_at', 'pivot'];
public function venta() { public function venta() {
return $this->belongsTo(Venta::class); return $this->belongsTo(Venta::class);

View File

@@ -11,6 +11,7 @@ class Caja extends Model {
use UuidPrimaryKey, SoftDeletes; use UuidPrimaryKey, SoftDeletes;
protected $table = 'cajas'; protected $table = 'cajas';
protected $hidden = ['created_at', 'updated_at', 'deleted_at', 'pivot'];
public function restaurante() { public function restaurante() {
return $this->belongsTo(Restaurante::class); return $this->belongsTo(Restaurante::class);

View File

@@ -11,6 +11,7 @@ class CanalVenta extends Model {
use UuidPrimaryKey, SoftDeletes; use UuidPrimaryKey, SoftDeletes;
protected $table = 'canales_venta'; protected $table = 'canales_venta';
protected $hidden = ['created_at', 'updated_at', 'deleted_at', 'pivot'];
protected $fillable = ['id', 'nombre', 'restaurante_id', 'sector_id', 'tipo_canal_id']; protected $fillable = ['id', 'nombre', 'restaurante_id', 'sector_id', 'tipo_canal_id'];
public static function findOrFail($id) { public static function findOrFail($id) {

View File

@@ -11,6 +11,7 @@ class Categoria extends Model {
use UuidPrimaryKey, SoftDeletes; use UuidPrimaryKey, SoftDeletes;
protected $table = 'categorias'; protected $table = 'categorias';
protected $hidden = ['created_at', 'updated_at', 'deleted_at', 'pivot'];
protected $fillable = ['id', 'nombre', 'restaurante_id']; protected $fillable = ['id', 'nombre', 'restaurante_id'];
public static function findOrFail($id) { public static function findOrFail($id) {

View File

@@ -11,8 +11,18 @@ class Compra extends Model {
use UuidPrimaryKey, SoftDeletes; use UuidPrimaryKey, SoftDeletes;
protected $table = 'compras'; protected $table = 'compras';
protected $hidden = ['created_at', 'updated_at', 'deleted_at', 'pivot'];
protected $fillable = [
'id', 'fecha_compra', 'proveedor_id', 'en_arqueo', 'restaurante_id'
];
public function compraIngredientes() { public static function findOrFail($id) {
$compra = Compra::find($id);
if(!$compra) throw new ModelNotFoundException("compra", $id);
return $compra;
}
public function ingredientes() {
return $this->hasMany(CompraIngrediente::class); return $this->hasMany(CompraIngrediente::class);
} }

View File

@@ -11,6 +11,8 @@ class CompraIngrediente extends Model {
use UuidPrimaryKey, SoftDeletes; use UuidPrimaryKey, SoftDeletes;
protected $table = 'compra_ingredientes'; protected $table = 'compra_ingredientes';
protected $hidden = ['created_at', 'updated_at', 'deleted_at', 'pivot'];
protected $fillable = ['id', 'unidades', 'monto_unitario', 'compra_id', 'ingrediente_id'];
public function ingrediente() { public function ingrediente() {
return $this->belongsTo(Ingrediente::class); return $this->belongsTo(Ingrediente::class);

View File

@@ -11,6 +11,7 @@ class Efectivo extends Model {
use UuidPrimaryKey, SoftDeletes; use UuidPrimaryKey, SoftDeletes;
protected $table = 'efectivos'; protected $table = 'efectivos';
protected $hidden = ['created_at', 'updated_at', 'deleted_at', 'pivot'];
public function caja() { public function caja() {
return $this->belongsTo(Caja::class); return $this->belongsTo(Caja::class);

View File

@@ -11,4 +11,5 @@ class EstadoProduccion extends Model {
use UuidPrimaryKey, SoftDeletes; use UuidPrimaryKey, SoftDeletes;
protected $table = 'estados_produccion'; protected $table = 'estados_produccion';
protected $hidden = ['created_at', 'updated_at', 'deleted_at', 'pivot'];
} }

View File

@@ -11,6 +11,14 @@ class Factura extends Model {
use UuidPrimaryKey, SoftDeletes; use UuidPrimaryKey, SoftDeletes;
protected $table = 'facturas'; protected $table = 'facturas';
protected $hidden = ['created_at', 'updated_at', 'deleted_at', 'pivot'];
protected $fillable = ['id', 'numero', 'monto_bruto', 'compra_id'];
public static function findOrFail($id) {
$factura = Factura::find($id);
if(!$factura) throw new ModelNotFoundException("factura", $id);
return $factura;
}
public function compra() { public function compra() {
return $this->belongsTo(Compra::class); return $this->belongsTo(Compra::class);

View File

@@ -11,6 +11,7 @@ class Ingrediente extends Model {
use UuidPrimaryKey, SoftDeletes; use UuidPrimaryKey, SoftDeletes;
protected $table = 'ingredientes'; protected $table = 'ingredientes';
protected $hidden = ['created_at', 'updated_at', 'deleted_at', 'pivot'];
protected $fillable = ['id', 'nombre', 'medida', 'restaurante_id']; protected $fillable = ['id', 'nombre', 'medida', 'restaurante_id'];
public static function findOrFail($id) { public static function findOrFail($id) {

View File

@@ -11,4 +11,5 @@ class MedioPago extends Model {
use UuidPrimaryKey, SoftDeletes; use UuidPrimaryKey, SoftDeletes;
protected $table = 'medios_pago'; protected $table = 'medios_pago';
protected $hidden = ['created_at', 'updated_at', 'deleted_at', 'pivot'];
} }

View File

@@ -1,18 +0,0 @@
<?php
namespace App\Models;
use App\Traits\UuidPrimaryKey;
use App\Exceptions\ModelNotFoundException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Mesero extends Model {
use UuidPrimaryKey, SoftDeletes;
protected $table = 'meseros';
public function usuario() {
return $this->belongsTo(Usuario::class);
}
}

View File

@@ -11,6 +11,7 @@ class Producto extends Model {
use UuidPrimaryKey, SoftDeletes; use UuidPrimaryKey, SoftDeletes;
protected $table = 'productos'; protected $table = 'productos';
protected $hidden = ['created_at', 'updated_at', 'deleted_at', 'pivot'];
protected $fillable = [ protected $fillable = [
'id', 'nombre', 'precio_venta', 'categoria_id', 'id', 'nombre', 'precio_venta', 'categoria_id',
'zona_produccion_id', 'restaurante_id' 'zona_produccion_id', 'restaurante_id'

View File

@@ -1,22 +0,0 @@
<?php
namespace App\Models;
use App\Traits\UuidPrimaryKey;
use App\Exceptions\ModelNotFoundException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Productor extends Model {
use UuidPrimaryKey, SoftDeletes;
protected $table = 'productores';
public function usuario() {
return $this->belongsTo(Usuario::class);
}
public function zonaProduccion() {
return $this->belongsTo(ZonaProduccion::class);
}
}

View File

@@ -11,6 +11,7 @@ class Proveedor extends Model {
use UuidPrimaryKey, SoftDeletes; use UuidPrimaryKey, SoftDeletes;
protected $table = 'proveedores'; protected $table = 'proveedores';
protected $hidden = ['created_at', 'updated_at', 'deleted_at', 'pivot'];
protected $fillable = [ protected $fillable = [
'id', 'rut', 'nombre', 'descripcion', 'id', 'rut', 'nombre', 'descripcion',
'direccion', 'telefono', 'restaurante_id' 'direccion', 'telefono', 'restaurante_id'

View File

@@ -1,18 +0,0 @@
<?php
namespace App\Models;
use App\Traits\UuidPrimaryKey;
use App\Exceptions\ModelNotFoundException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Recaudador extends Model {
use UuidPrimaryKey, SoftDeletes;
protected $table = 'recaudadores';
public function usuario() {
return $this->belongsTo(Usuario::class);
}
}

View File

@@ -10,6 +10,7 @@ class Receta extends Model {
use UuidPrimaryKey; use UuidPrimaryKey;
protected $table = 'recetas'; protected $table = 'recetas';
protected $hidden = ['created_at', 'updated_at', 'deleted_at', 'pivot'];
protected $fillable = ['unidades', 'producto_id', 'ingrediente_id']; protected $fillable = ['unidades', 'producto_id', 'ingrediente_id'];
public function ingrediente() { public function ingrediente() {

View File

@@ -2,13 +2,6 @@
namespace App\Models; namespace App\Models;
use App\Models\CanalVenta;
use App\Models\Sector;
use App\Models\ZonaProduccion;
use App\Models\Categoria;
use App\Models\Proveedor;
use App\Models\Ingrediente;
use App\Models\Producto;
use App\Traits\UuidPrimaryKey; use App\Traits\UuidPrimaryKey;
use App\Exceptions\ModelNotFoundException; use App\Exceptions\ModelNotFoundException;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
@@ -20,7 +13,7 @@ class Restaurante extends Model {
use UuidPrimaryKey, SoftDeletes; use UuidPrimaryKey, SoftDeletes;
protected $table = 'restaurantes'; protected $table = 'restaurantes';
protected $hidden = ['created_at', 'updated_at', 'deleted_at', 'pivot'];
protected $fillable = ['id', 'nombre']; protected $fillable = ['id', 'nombre'];
public static function findOrFail($id) { public static function findOrFail($id) {
@@ -29,14 +22,22 @@ class Restaurante extends Model {
return $restaurante; return $restaurante;
} }
public function usuarios() {
return $this->belongsToMany(Usuario::class, 'usuarios_restaurantes', 'restaurante_id', 'usuario_id');
}
public function canalesVenta() { public function canalesVenta() {
return $this->hasMany(CanalVenta::class, 'restaurante_id'); return $this->hasMany(CanalVenta::class, 'restaurante_id');
} }
public function categorias() {
return $this->hasMany(Categoria::class, 'restaurante_id');
}
public function compras() {
return $this->hasMany(Compra::class, 'restaurante_id');
}
public function usuarios() {
return $this->belongsToMany(Usuario::class, 'usuarios_restaurantes', 'restaurante_id', 'usuario_id');
}
public function sectores() { public function sectores() {
return $this->hasMany(Sector::class, 'restaurante_id'); return $this->hasMany(Sector::class, 'restaurante_id');
} }
@@ -45,10 +46,6 @@ class Restaurante extends Model {
return $this->hasMany(ZonaProduccion::class, 'restaurante_id'); return $this->hasMany(ZonaProduccion::class, 'restaurante_id');
} }
public function categorias() {
return $this->hasMany(Categoria::class, 'restaurante_id');
}
public function proveedores() { public function proveedores() {
return $this->hasMany(Proveedor::class, 'restaurante_id'); return $this->hasMany(Proveedor::class, 'restaurante_id');
} }
@@ -60,4 +57,36 @@ class Restaurante extends Model {
public function productos() { public function productos() {
return $this->hasMany(Producto::class, 'restaurante_id'); return $this->hasMany(Producto::class, 'restaurante_id');
} }
public function ventas() {
return $this->hasMany(Venta::class, 'restaurante_id');
}
public function boletasElectronicas() {
return $this->hasMany(BoletaElectronica::class, 'restaurante_id');
}
public function boletasExentas() {
return $this->hasMany(BoletaExenta::class, 'restaurante_id');
}
public function cajas() {
return $this->hasMany(Caja::class, 'restaurante_id');
}
public function bodegaIngresos() {
return $this->hasMany(BodegaIngreso::class, 'restaurante_id');
}
public function bodegaEgresos() {
return $this->hasMany(BodegaEgreso::class, 'restaurante_id');
}
public function bodegaMovimientos() {
return $this->hasMany(BodegaMovimiento::class, 'restaurante_id');
}
public function bodegaActual() {
return $this->hasMany(BodegaActual::class, 'restaurante_id');
}
} }

View File

@@ -11,6 +11,7 @@ class Sector extends Model {
use UuidPrimaryKey, SoftDeletes; use UuidPrimaryKey, SoftDeletes;
protected $table = 'sectores'; protected $table = 'sectores';
protected $hidden = ['created_at', 'updated_at', 'deleted_at', 'pivot'];
protected $fillable = ['id', 'nombre', 'restaurante_id']; protected $fillable = ['id', 'nombre', 'restaurante_id'];
public static function findOrFail($id) { public static function findOrFail($id) {

View File

@@ -11,4 +11,5 @@ class TipoCanal extends Model {
use UuidPrimaryKey, SoftDeletes; use UuidPrimaryKey, SoftDeletes;
protected $table = 'tipos_canal'; protected $table = 'tipos_canal';
protected $hidden = ['created_at', 'updated_at', 'deleted_at', 'pivot'];
} }

View File

@@ -16,6 +16,7 @@ class Usuario extends Model {
use UuidPrimaryKey, SoftDeletes; use UuidPrimaryKey, SoftDeletes;
protected $table = 'usuarios'; protected $table = 'usuarios';
protected $hidden = ['created_at', 'updated_at', 'deleted_at', 'pivot'];
protected $fillable = ['id', 'auth0_id', 'nombre']; protected $fillable = ['id', 'auth0_id', 'nombre'];
protected $appends = ['roles']; protected $appends = ['roles'];
@@ -32,32 +33,30 @@ class Usuario extends Model {
} }
public function isGlobalAdmin() { public function isGlobalAdmin() {
return in_array('global_admin', $this->roles); return $this->hasRole('global_admin');
} }
public function isAdmin() { public function isAdmin() {
return in_array('admin', $this->roles); return $this->hasRole('admin');
}
public function hasRole($role) {
return in_array($role, $this->roles);
}
public function isOnRestaurante($restaurante) {
if($this->isGlobalAdmin()) return true;
return $this->restaurantes()->where('id', $restaurante->id)->count() > 0;
} }
public function restaurantes() { public function restaurantes() {
return $this->belongsToMany(Restaurante::class, 'usuarios_restaurantes', 'usuario_id', 'restaurante_id'); return $this->belongsToMany(Restaurante::class, 'usuarios_restaurantes', 'usuario_id', 'restaurante_id');
} }
public function administrador() { public function zonasProduccion() {
return $this->hasOne(Administrador::class); return $this->belongsToMany(ZonaProduccion::class, 'productores', 'usuario_id', 'zona_produccion_id');
} }
public function recaudador() {
return $this->hasOne(Recaudador::class);
}
public function mesero() {
return $this->hasOne(Mesero::class);
}
public function productor() {
return $this->hasOne(Productor::class);
}
public function getRolesAttribute() { public function getRolesAttribute() {
$auth0Service = app(Auth0Service::class); $auth0Service = app(Auth0Service::class);

View File

@@ -11,6 +11,7 @@ class Venta extends Model {
use UuidPrimaryKey, SoftDeletes; use UuidPrimaryKey, SoftDeletes;
protected $table = 'ventas'; protected $table = 'ventas';
protected $hidden = ['created_at', 'updated_at', 'deleted_at', 'pivot'];
public function mesero() { public function mesero() {
return $this->belongsTo(Mesero::class); return $this->belongsTo(Mesero::class);

View File

@@ -11,6 +11,7 @@ class VentaProducto extends Model {
use UuidPrimaryKey, SoftDeletes; use UuidPrimaryKey, SoftDeletes;
protected $table = 'venta_productos'; protected $table = 'venta_productos';
protected $hidden = ['created_at', 'updated_at', 'deleted_at', 'pivot'];
public function venta() { public function venta() {
return $this->belongsTo(Venta::class); return $this->belongsTo(Venta::class);

View File

@@ -11,6 +11,7 @@ class ZonaProduccion extends Model {
use UuidPrimaryKey, SoftDeletes; use UuidPrimaryKey, SoftDeletes;
protected $table = 'zonas_produccion'; protected $table = 'zonas_produccion';
protected $hidden = ['created_at', 'updated_at', 'deleted_at', 'pivot'];
protected $fillable = ['id', 'nombre', 'restaurante_id']; protected $fillable = ['id', 'nombre', 'restaurante_id'];
public static function findOrFail($id) { public static function findOrFail($id) {
@@ -22,4 +23,8 @@ class ZonaProduccion extends Model {
public function restaurante() { public function restaurante() {
return $this->belongsTo(Restaurante::class); return $this->belongsTo(Restaurante::class);
} }
public function usuarios() {
return $this->belongsToMany(Usuario::class, 'productores', 'zona_produccion_id', 'usuario_id');
}
} }

View File

@@ -76,7 +76,9 @@ $app->configure('logging');
$app->routeMiddleware([ $app->routeMiddleware([
'auth' => App\Http\Middleware\Auth0Middleware::class, 'auth' => App\Http\Middleware\Auth0Middleware::class,
'log_endpoint' => App\Http\Middleware\LogEndpointHitMiddleware::class 'log_endpoint' => App\Http\Middleware\LogEndpointHitMiddleware::class,
'role' => App\Http\Middleware\RoleMiddleware::class,
'in_restaurante' => App\Http\Middleware\InRestauranteMiddleware::class
]); ]);
$app->middleware([ $app->middleware([

View File

@@ -8,69 +8,94 @@ $router->get('/', function () use ($router) {
$router->group(['prefix' => 'api/v1', 'middleware' => ['auth', 'log_endpoint']], function () use ($router) { $router->group(['prefix' => 'api/v1', 'middleware' => ['auth', 'log_endpoint']], function () use ($router) {
$router->group(['prefix' => '/users'], function () use ($router) { $router->group(['prefix' => '/users'], function () use ($router) {
$router->get( '/', ['as' => 'users.all', 'uses' => 'UsuariosController@all']); $router->get( '/', ['as' => 'users.all', 'uses' => 'UsuariosController@all', 'middleware' => ['role:admin|global_admin']]);
$router->get( '/{id}', ['as' => 'users.get', 'uses' => 'UsuariosController@get']); $router->get( '/{id}', ['as' => 'users.get', 'uses' => 'UsuariosController@get', 'middleware' => ['role:admin|global_admin']]);
$router->post( '/', ['as' => 'users.create', 'uses' => 'UsuariosController@create']); $router->post( '/', ['as' => 'users.create', 'uses' => 'UsuariosController@create', 'middleware' => ['role:admin|global_admin']]);
$router->put( '/{id}', ['as' => 'users.update', 'uses' => 'UsuariosController@update']); $router->put( '/{id}', ['as' => 'users.update', 'uses' => 'UsuariosController@update', 'middleware' => ['role:admin|global_admin']]);
$router->delete('/{id}', ['as' => 'users.delete', 'uses' => 'UsuariosController@delete']); $router->delete('/{id}', ['as' => 'users.delete', 'uses' => 'UsuariosController@delete', 'middleware' => ['role:admin|global_admin']]);
$router->get( '/{id}/restaurantes/', ['as' => 'users.get_restaurantes', 'uses' => 'UsuariosController@getRestaurantes']); $router->get( '/{id}/restaurantes/', ['as' => 'users.get_restaurantes', 'uses' => 'UsuariosController@getRestaurantes', 'middleware' => ['role:admin|global_admin']]);
$router->put( '/{id}/restaurantes/{restaurant}', ['as' => 'users.add_to_restaurant', 'uses' => 'UsuariosController@addToRestaurant']); $router->put( '/{id}/restaurantes/{restaurant}', ['as' => 'users.add_to_restaurant', 'uses' => 'UsuariosController@addToRestaurant', 'middleware' => ['role:admin|global_admin']]);
$router->delete('/{id}/restaurantes/{restaurant}', ['as' => 'users.remove_from_restaurant', 'uses' => 'UsuariosController@removeFromRestaurant']); $router->delete('/{id}/restaurantes/{restaurant}', ['as' => 'users.remove_from_restaurant', 'uses' => 'UsuariosController@removeFromRestaurant', 'middleware' => ['role:admin|global_admin']]);
}); });
$router->group(['prefix' => '/restaurantes'], function () use ($router) { $router->group(['prefix' => '/restaurantes'], function () use ($router) {
$router->get( '/', ['as' => 'restaurant.all', 'uses' => 'RestaurantesController@all']); $router->get( '/', ['as' => 'restaurant.all', 'uses' => 'RestaurantesController@all']);
$router->get( '/{id}', ['as' => 'restaurant.get', 'uses' => 'RestaurantesController@get']); $router->get( '/{id}', ['as' => 'restaurant.get', 'uses' => 'RestaurantesController@get']);
$router->post( '/', ['as' => 'restaurant.create', 'uses' => 'RestaurantesController@create']); $router->post( '/', ['as' => 'restaurant.create', 'uses' => 'RestaurantesController@create', 'middleware' => ['role:global_admin']]);
$router->put( '/{id}', ['as' => 'restaurant.update', 'uses' => 'RestaurantesController@update']); $router->put( '/{id}', ['as' => 'restaurant.update', 'uses' => 'RestaurantesController@update', 'middleware' => ['role:admin|global_admin']]);
$router->delete('/{id}', ['as' => 'restaurant.delete', 'uses' => 'RestaurantesController@delete']); $router->delete('/{id}', ['as' => 'restaurant.delete', 'uses' => 'RestaurantesController@delete', 'middleware' => ['role:global_admin']]);
$router->get( '/{restaurante_id}/canales-venta', ['as' => 'canales-venta.all', 'uses' => 'CanalesVentaController@all']); $router->get( '/{restaurante_id}/canales-venta', ['as' => 'canales-venta.all', 'uses' => 'CanalesVentaController@all', 'middleware' => ['in_restaurante']]);
$router->get( '/{restaurante_id}/canales-venta/{id}', ['as' => 'canales-venta.get', 'uses' => 'CanalesVentaController@get']); $router->get( '/{restaurante_id}/canales-venta/{id}', ['as' => 'canales-venta.get', 'uses' => 'CanalesVentaController@get', 'middleware' => ['in_restaurante']]);
$router->post( '/{restaurante_id}/canales-venta', ['as' => 'canales-venta.create', 'uses' => 'CanalesVentaController@create']); $router->post( '/{restaurante_id}/canales-venta', ['as' => 'canales-venta.create', 'uses' => 'CanalesVentaController@create', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->put( '/{restaurante_id}/canales-venta/{id}', ['as' => 'canales-venta.update', 'uses' => 'CanalesVentaController@update']); $router->put( '/{restaurante_id}/canales-venta/{id}', ['as' => 'canales-venta.update', 'uses' => 'CanalesVentaController@update', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->delete('/{restaurante_id}/canales-venta/{id}', ['as' => 'canales-venta.delete', 'uses' => 'CanalesVentaController@delete']); $router->delete('/{restaurante_id}/canales-venta/{id}', ['as' => 'canales-venta.delete', 'uses' => 'CanalesVentaController@delete', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->get( '/{restaurante_id}/sectores', ['as' => 'sectores.all', 'uses' => 'SectoresController@all']); $router->get( '/{restaurante_id}/sectores', ['as' => 'sectores.all', 'uses' => 'SectoresController@all', 'middleware' => ['in_restaurante']]);
$router->get( '/{restaurante_id}/sectores/{id}', ['as' => 'sectores.get', 'uses' => 'SectoresController@get']); $router->get( '/{restaurante_id}/sectores/{id}', ['as' => 'sectores.get', 'uses' => 'SectoresController@get', 'middleware' => ['in_restaurante']]);
$router->post( '/{restaurante_id}/sectores', ['as' => 'sectores.create', 'uses' => 'SectoresController@create']); $router->post( '/{restaurante_id}/sectores', ['as' => 'sectores.create', 'uses' => 'SectoresController@create', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->put( '/{restaurante_id}/sectores/{id}', ['as' => 'sectores.update', 'uses' => 'SectoresController@update']); $router->put( '/{restaurante_id}/sectores/{id}', ['as' => 'sectores.update', 'uses' => 'SectoresController@update', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->delete('/{restaurante_id}/sectores/{id}', ['as' => 'sectores.delete', 'uses' => 'SectoresController@delete']); $router->delete('/{restaurante_id}/sectores/{id}', ['as' => 'sectores.delete', 'uses' => 'SectoresController@delete', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->get( '/{restaurante_id}/zonas-produccion', ['as' => 'zonas-produccion.all', 'uses' => 'ZonasProduccionController@all']); $router->get( '/{restaurante_id}/zonas-produccion', ['as' => 'zonas-produccion.all', 'uses' => 'ZonasProduccionController@all', 'middleware' => ['in_restaurante']]);
$router->get( '/{restaurante_id}/zonas-produccion/{id}', ['as' => 'zonas-produccion.get', 'uses' => 'ZonasProduccionController@get']); $router->get( '/{restaurante_id}/zonas-produccion/{id}', ['as' => 'zonas-produccion.get', 'uses' => 'ZonasProduccionController@get', 'middleware' => ['in_restaurante']]);
$router->post( '/{restaurante_id}/zonas-produccion', ['as' => 'zonas-produccion.create', 'uses' => 'ZonasProduccionController@create']); $router->get( '/{restaurante_id}/zonas-produccion/{id}/users', ['as' => 'zonas-produccion.users', 'uses' => 'ZonasProduccionController@users', 'middleware' => ['in_restaurante']]);
$router->put( '/{restaurante_id}/zonas-produccion/{id}', ['as' => 'zonas-produccion.update', 'uses' => 'ZonasProduccionController@update']); $router->post( '/{restaurante_id}/zonas-produccion', ['as' => 'zonas-produccion.create', 'uses' => 'ZonasProduccionController@create', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->delete('/{restaurante_id}/zonas-produccion/{id}', ['as' => 'zonas-produccion.delete', 'uses' => 'ZonasProduccionController@delete']); $router->put( '/{restaurante_id}/zonas-produccion/{id}', ['as' => 'zonas-produccion.update', 'uses' => 'ZonasProduccionController@update', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->delete('/{restaurante_id}/zonas-produccion/{id}', ['as' => 'zonas-produccion.delete', 'uses' => 'ZonasProduccionController@delete', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->get( '/{restaurante_id}/categorias', ['as' => 'categorias.all', 'uses' => 'CategoriasController@all']); $router->get( '/{restaurante_id}/categorias', ['as' => 'categorias.all', 'uses' => 'CategoriasController@all', 'middleware' => ['in_restaurante']]);
$router->get( '/{restaurante_id}/categorias/{id}', ['as' => 'categorias.get', 'uses' => 'CategoriasController@get']); $router->get( '/{restaurante_id}/categorias/{id}', ['as' => 'categorias.get', 'uses' => 'CategoriasController@get', 'middleware' => ['in_restaurante']]);
$router->post( '/{restaurante_id}/categorias', ['as' => 'categorias.create', 'uses' => 'CategoriasController@create']); $router->post( '/{restaurante_id}/categorias', ['as' => 'categorias.create', 'uses' => 'CategoriasController@create', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->put( '/{restaurante_id}/categorias/{id}', ['as' => 'categorias.update', 'uses' => 'CategoriasController@update']); $router->put( '/{restaurante_id}/categorias/{id}', ['as' => 'categorias.update', 'uses' => 'CategoriasController@update', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->delete('/{restaurante_id}/categorias/{id}', ['as' => 'categorias.delete', 'uses' => 'CategoriasController@delete']); $router->delete('/{restaurante_id}/categorias/{id}', ['as' => 'categorias.delete', 'uses' => 'CategoriasController@delete', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->get( '/{restaurante_id}/proveedores', ['as' => 'proveedores.all', 'uses' => 'ProveedoresController@all']); $router->get( '/{restaurante_id}/proveedores', ['as' => 'proveedores.all', 'uses' => 'ProveedoresController@all', 'middleware' => ['in_restaurante']]);
$router->get( '/{restaurante_id}/proveedores/{id}', ['as' => 'proveedores.get', 'uses' => 'ProveedoresController@get']); $router->get( '/{restaurante_id}/proveedores/{id}', ['as' => 'proveedores.get', 'uses' => 'ProveedoresController@get', 'middleware' => ['in_restaurante']]);
$router->post( '/{restaurante_id}/proveedores', ['as' => 'proveedores.create', 'uses' => 'ProveedoresController@create']); $router->post( '/{restaurante_id}/proveedores', ['as' => 'proveedores.create', 'uses' => 'ProveedoresController@create', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->put( '/{restaurante_id}/proveedores/{id}', ['as' => 'proveedores.update', 'uses' => 'ProveedoresController@update']); $router->put( '/{restaurante_id}/proveedores/{id}', ['as' => 'proveedores.update', 'uses' => 'ProveedoresController@update', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->delete('/{restaurante_id}/proveedores/{id}', ['as' => 'proveedores.delete', 'uses' => 'ProveedoresController@delete']); $router->delete('/{restaurante_id}/proveedores/{id}', ['as' => 'proveedores.delete', 'uses' => 'ProveedoresController@delete', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->get( '/{restaurante_id}/ingredientes', ['as' => 'ingredientes.all', 'uses' => 'IngredientesController@all']); $router->get( '/{restaurante_id}/ingredientes', ['as' => 'ingredientes.all', 'uses' => 'IngredientesController@all', 'middleware' => ['in_restaurante']]);
$router->get( '/{restaurante_id}/ingredientes/{id}', ['as' => 'ingredientes.get', 'uses' => 'IngredientesController@get']); $router->get( '/{restaurante_id}/ingredientes/{id}', ['as' => 'ingredientes.get', 'uses' => 'IngredientesController@get', 'middleware' => ['in_restaurante']]);
$router->post( '/{restaurante_id}/ingredientes', ['as' => 'ingredientes.create', 'uses' => 'IngredientesController@create']); $router->post( '/{restaurante_id}/ingredientes', ['as' => 'ingredientes.create', 'uses' => 'IngredientesController@create', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->put( '/{restaurante_id}/ingredientes/{id}', ['as' => 'ingredientes.update', 'uses' => 'IngredientesController@update']); $router->put( '/{restaurante_id}/ingredientes/{id}', ['as' => 'ingredientes.update', 'uses' => 'IngredientesController@update', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->delete('/{restaurante_id}/ingredientes/{id}', ['as' => 'ingredientes.delete', 'uses' => 'IngredientesController@delete']); $router->delete('/{restaurante_id}/ingredientes/{id}', ['as' => 'ingredientes.delete', 'uses' => 'IngredientesController@delete', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->get( '/{restaurante_id}/productos', ['as' => 'productos.all', 'uses' => 'ProductosController@all']); $router->get( '/{restaurante_id}/productos', ['as' => 'productos.all', 'uses' => 'ProductosController@all', 'middleware' => ['in_restaurante']]);
$router->get( '/{restaurante_id}/productos/{id}', ['as' => 'productos.get', 'uses' => 'ProductosController@get']); $router->get( '/{restaurante_id}/productos/{id}', ['as' => 'productos.get', 'uses' => 'ProductosController@get', 'middleware' => ['in_restaurante']]);
$router->post( '/{restaurante_id}/productos', ['as' => 'productos.create', 'uses' => 'ProductosController@create']); $router->post( '/{restaurante_id}/productos', ['as' => 'productos.create', 'uses' => 'ProductosController@create', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->put( '/{restaurante_id}/productos/{id}', ['as' => 'productos.update', 'uses' => 'ProductosController@update']); $router->put( '/{restaurante_id}/productos/{id}', ['as' => 'productos.update', 'uses' => 'ProductosController@update', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->delete('/{restaurante_id}/productos/{id}', ['as' => 'productos.delete', 'uses' => 'ProductosController@delete']); $router->delete('/{restaurante_id}/productos/{id}', ['as' => 'productos.delete', 'uses' => 'ProductosController@delete', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->get( '/{restaurante_id}/productos/{producto_id}/ingredientes/', ['as' => 'productos.receta.all', 'uses' => 'RecetasController@all']); $router->get( '/{restaurante_id}/productos/{producto_id}/ingredientes/', ['as' => 'productos.receta.all', 'uses' => 'RecetasController@all', 'middleware' => ['in_restaurante']]);
$router->get( '/{restaurante_id}/productos/{producto_id}/ingredientes/{ingrediente_id}', ['as' => 'productos.receta.get', 'uses' => 'RecetasController@get']); $router->get( '/{restaurante_id}/productos/{producto_id}/ingredientes/{ingrediente_id}', ['as' => 'productos.receta.get', 'uses' => 'RecetasController@get', 'middleware' => ['in_restaurante']]);
$router->post( '/{restaurante_id}/productos/{producto_id}/ingredientes/{ingrediente_id}', ['as' => 'productos.receta.add_ingrediente', 'uses' => 'RecetasController@create']); $router->post( '/{restaurante_id}/productos/{producto_id}/ingredientes/{ingrediente_id}', ['as' => 'productos.receta.add_ingrediente', 'uses' => 'RecetasController@create', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->put( '/{restaurante_id}/productos/{producto_id}/ingredientes/{ingrediente_id}', ['as' => 'productos.receta.update_ingrediente', 'uses' => 'RecetasController@update']); $router->put( '/{restaurante_id}/productos/{producto_id}/ingredientes/{ingrediente_id}', ['as' => 'productos.receta.update_ingrediente', 'uses' => 'RecetasController@update', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->delete('/{restaurante_id}/productos/{producto_id}/ingredientes/{ingrediente_id}', ['as' => 'productos.receta.remove_ingrediente', 'uses' => 'RecetasController@delete']); $router->delete('/{restaurante_id}/productos/{producto_id}/ingredientes/{ingrediente_id}', ['as' => 'productos.receta.remove_ingrediente', 'uses' => 'RecetasController@delete', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->get( '/{restaurante_id}/compras', ['as' => 'compras.all', 'uses' => 'ComprasController@all', 'middleware' => ['in_restaurante']]);
$router->get( '/{restaurante_id}/compras/{id}', ['as' => 'compras.get', 'uses' => 'ComprasController@get', 'middleware' => ['in_restaurante']]);
$router->post( '/{restaurante_id}/compras', ['as' => 'compras.create', 'uses' => 'ComprasController@create', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->put( '/{restaurante_id}/compras/{id}', ['as' => 'compras.update', 'uses' => 'ComprasController@update', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->delete('/{restaurante_id}/compras/{id}', ['as' => 'compras.delete', 'uses' => 'ComprasController@delete', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->get( '/{restaurante_id}/compras/{id}/ingredientes', ['as' => 'compras.ingredientes.get', 'uses' => 'ComprasController@getIngredientes', 'middleware' => ['in_restaurante']]);
$router->post( '/{restaurante_id}/compras/{id}/ingredientes/{ingrediente_id}', ['as' => 'compras.ingredientes.add', 'uses' => 'ComprasController@addIngrediente', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->delete('/{restaurante_id}/compras/{id}/ingredientes/{ingrediente_id}', ['as' => 'compras.ingredientes.delete', 'uses' => 'ComprasController@deleteIngrediente','middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->get( '/{restaurante_id}/compras/{id}/factura', ['as' => 'factura.get', 'uses' => 'FacturasController@get', 'middleware' => ['in_restaurante']]);
$router->post( '/{restaurante_id}/compras/{id}/factura', ['as' => 'factura.create', 'uses' => 'FacturasController@create', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->put( '/{restaurante_id}/compras/{id}/factura', ['as' => 'factura.update', 'uses' => 'FacturasController@update', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->delete('/{restaurante_id}/compras/{id}/factura', ['as' => 'factura.delete', 'uses' => 'FacturasController@delete', 'middleware' => ['role:admin|global_admin', 'in_restaurante']]);
$router->get( '/{restaurante_id}/bodega/ingresos', ['as' => 'bodega.ingresos', 'uses' => 'BodegaController@ingresos', 'middleware' => ['in_restaurante']]);
$router->get( '/{restaurante_id}/bodega/ingresos/{ingrediente_id}', ['as' => 'bodega.ingresos_ingrediente', 'uses' => 'BodegaController@ingresos_ingrediente', 'middleware' => ['in_restaurante']]);
$router->get( '/{restaurante_id}/bodega/egresos', ['as' => 'bodega.egresos', 'uses' => 'BodegaController@egresos', 'middleware' => ['in_restaurante']]);
$router->get( '/{restaurante_id}/bodega/egresos/{ingrediente_id}', ['as' => 'bodega.egresos_ingrediente', 'uses' => 'BodegaController@egresos_ingrediente', 'middleware' => ['in_restaurante']]);
$router->get( '/{restaurante_id}/bodega/movimientos', ['as' => 'bodega.movimientos', 'uses' => 'BodegaController@movimientos', 'middleware' => ['in_restaurante']]);
$router->get( '/{restaurante_id}/bodega/movimientos/{ingrediente_id}', ['as' => 'bodega.movimientos_ingrediente', 'uses' => 'BodegaController@movimientos_ingrediente', 'middleware' => ['in_restaurante']]);
$router->get( '/{restaurante_id}/bodega/actual', ['as' => 'bodega.actual', 'uses' => 'BodegaController@actual', 'middleware' => ['in_restaurante']]);
$router->get( '/{restaurante_id}/bodega/actual/{ingrediente_id}', ['as' => 'bodega.actual_ingrediente', 'uses' => 'BodegaController@actual_ingrediente', 'middleware' => ['in_restaurante']]);
}); });
}); });

View File

@@ -183,31 +183,25 @@ create table compras
create table facturas create table facturas
( (
id uuid primary key default gen_random_uuid(), id uuid primary key default gen_random_uuid(),
numero text not null, numero text not null,
monto_bruto bigint not null, monto_bruto bigint not null,
iva bigint not null default 0, compra_id uuid references compras,
ila bigint not null default 0, created_at timestamptz not null default current_timestamp,
monto_neto bigint not null, updated_at timestamptz not null default current_timestamp,
fecha_emision date not null, deleted_at timestamptz
fecha_vencimiento date not null,
compra_id uuid references compras,
created_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp,
deleted_at timestamptz
); );
create table compra_ingredientes create table compra_ingredientes
( (
id uuid primary key default gen_random_uuid(), id uuid primary key default gen_random_uuid(),
unidades numeric not null, unidades numeric not null,
monto_unitario_bruto bigint not null, monto_unitario bigint not null,
monto_unitario_neto bigint not null, compra_id uuid references compras,
compra_id uuid references compras, ingrediente_id uuid references ingredientes,
ingrediente_id uuid references ingredientes, created_at timestamptz not null default current_timestamp,
created_at timestamptz not null default current_timestamp, updated_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp, deleted_at timestamptz
deleted_at timestamptz
); );
create table sectores create table sectores
@@ -340,8 +334,8 @@ create table bodega_egresos
( (
unidades numeric not null, unidades numeric not null,
fecha timestamptz not null, fecha timestamptz not null,
ingrediente_id uuid references ingredientes, ingrediente_id uuid not null,
restaurante_id uuid references restaurantes, restaurante_id uuid not null,
created_at timestamptz not null default current_timestamp, created_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp, updated_at timestamptz not null default current_timestamp,
deleted_at timestamptz deleted_at timestamptz
@@ -351,8 +345,8 @@ create table bodega_ingresos
( (
unidades numeric not null, unidades numeric not null,
fecha timestamptz not null, fecha timestamptz not null,
ingrediente_id uuid references ingredientes, ingrediente_id uuid not null,
restaurante_id uuid references restaurantes, restaurante_id uuid not null,
created_at timestamptz not null default current_timestamp, created_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp, updated_at timestamptz not null default current_timestamp,
deleted_at timestamptz deleted_at timestamptz

Binary file not shown.

View File

@@ -0,0 +1,8 @@
alter table facturas drop column iva;
alter table facturas drop column ila;
alter table facturas drop column monto_neto;
alter table facturas drop column fecha_emision;
alter table facturas drop column fecha_vencimiento;
alter table compra_ingredientes rename column monto_unitario_bruto to monto_unitario;
alter table compra_ingredientes drop column monto_unitario_neto;

View File

@@ -0,0 +1,11 @@
alter table bodega_egresos drop constraint bodega_egresos_ingrediente_id_fkey;
alter table bodega_egresos drop constraint bodega_egresos_restaurante_id_fkey;
alter table bodega_ingresos drop constraint bodega_ingresos_ingrediente_id_fkey;
alter table bodega_ingresos drop constraint bodega_ingresos_restaurante_id_fkey;
alter table bodega_ingresos alter column restaurante_id set not null;
alter table bodega_ingresos alter column ingrediente_id set not null;
alter table bodega_egresos alter column restaurante_id set not null;
alter table bodega_egresos alter column ingrediente_id set not null;

View File

@@ -0,0 +1,6 @@
alter table ventas drop column mesero_id;
alter table ventas add column usuario_id uuid references usuarios;
drop table meseros;
drop table administradores;
drop table recaudadores;

View File

@@ -1,8 +0,0 @@
select *
from bodega_ingresos;
select *
from bodega_egresos;
select *
from bodega_movimientos;
select *
from bodega_actual;