Mantenedor de compras!

This commit is contained in:
2021-07-14 03:58:17 -04:00
parent 4b71bc67aa
commit 0816f37611
9 changed files with 436 additions and 29 deletions

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

@@ -11,8 +11,17 @@ class Compra extends Model {
use UuidPrimaryKey, SoftDeletes; use UuidPrimaryKey, SoftDeletes;
protected $table = 'compras'; protected $table = 'compras';
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,7 @@ class CompraIngrediente extends Model {
use UuidPrimaryKey, SoftDeletes; use UuidPrimaryKey, SoftDeletes;
protected $table = 'compra_ingredientes'; protected $table = 'compra_ingredientes';
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,13 @@ class Factura extends Model {
use UuidPrimaryKey, SoftDeletes; use UuidPrimaryKey, SoftDeletes;
protected $table = 'facturas'; protected $table = 'facturas';
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

@@ -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;
@@ -60,4 +53,8 @@ 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 compras() {
return $this->hasMany(Compra::class, 'restaurante_id');
}
} }

View File

@@ -72,5 +72,20 @@ $router->group(['prefix' => 'api/v1', 'middleware' => ['auth', 'log_endpoint']],
$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']);
$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']);
$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']);
$router->get( '/{restaurante_id}/compras', ['as' => 'compras.all', 'uses' => 'ComprasController@all']);
$router->get( '/{restaurante_id}/compras/{id}', ['as' => 'compras.get', 'uses' => 'ComprasController@get']);
$router->post( '/{restaurante_id}/compras', ['as' => 'compras.create', 'uses' => 'ComprasController@create']);
$router->put( '/{restaurante_id}/compras/{id}', ['as' => 'compras.update', 'uses' => 'ComprasController@update']);
$router->delete('/{restaurante_id}/compras/{id}', ['as' => 'compras.delete', 'uses' => 'ComprasController@delete']);
$router->get( '/{restaurante_id}/compras/{id}/ingredientes', ['as' => 'compras.ingredientes.get', 'uses' => 'ComprasController@getIngredientes']);
$router->post( '/{restaurante_id}/compras/{id}/ingredientes/{ingrediente_id}', ['as' => 'compras.ingredientes.add', 'uses' => 'ComprasController@addIngrediente']);
$router->delete('/{restaurante_id}/compras/{id}/ingredientes/{ingrediente_id}', ['as' => 'compras.ingredientes.delete', 'uses' => 'ComprasController@deleteIngrediente']);
$router->get( '/{restaurante_id}/compras/{id}/factura', ['as' => 'factura.get', 'uses' => 'FacturasController@get']);
$router->post( '/{restaurante_id}/compras/{id}/factura', ['as' => 'factura.create', 'uses' => 'FacturasController@create']);
$router->put( '/{restaurante_id}/compras/{id}/factura', ['as' => 'factura.update', 'uses' => 'FacturasController@update']);
$router->delete('/{restaurante_id}/compras/{id}/factura', ['as' => 'factura.delete', 'uses' => 'FacturasController@delete']);
}); });
}); });

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

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;