Compare commits

...

4 commits

Author SHA1 Message Date
ale
4c9ef3f782 minimo cambio 2024-03-19 22:37:00 -03:00
ale
554015a616 combinacion nombre-barrio unica 2024-03-19 22:34:54 -03:00
ale
830002b4cf confirmacion, agregar y quitar productos 2024-03-19 22:34:40 -03:00
ale
784b4d97f8 Tipo + crear pedido 2024-03-19 22:34:13 -03:00
3 changed files with 29 additions and 3 deletions

View file

@ -33,14 +33,17 @@ class Barrio extends Model
return $this->hasMany(Pedido::class);
}
function crearPedido(string $nombre) : Pedido {
return $this->pedidos()->create(['name' => $name]);
}
function pedidosConfirmados() {
return $this->pedidos()->where('confirmed',true);
}
private function calcularTotalConfirmados($closure = null) : float {
private function calcularTotalConfirmados(Closure $closure = null) : float {
if (!$closure)
$closure = fn($p) => $p->totalChismosa();
return $this->pedidosConfirmados()->sum($closure);
}
@ -49,7 +52,9 @@ class Barrio extends Model
}
function totalNoBarriales() : float {
return $this->calcularTotalConfirmados(fn($p) => $p->total($p->productosNoBarriales()));
return $this->calcularTotalConfirmados(
fn($p) => $p->total($p->productosNoBarriales())
);
}
function totalTransporte() : float {

View file

@ -96,4 +96,24 @@ class Pedido extends Model
function totalChismosa() : float {
return $this->total() + $this->totalTransporte();
}
function toggleConfirm() : bool {
$this->confirmed = !$this->confirmed;
$this->save();
return $this->confirmed;
}
function agregarProducto(Producto $producto, int $cantidad) {
$productoEnChismosa = $this->productos()->where('id', $producto->id)->first();
if ($productoEnChismosa) {
$productoEnChismosa->pivot->cantidad += $cantidad;
$productoEnChismosa->save();
} else {
$this->productos()->attach($producto, ['cantidad' => $cantidad]);
}
}
function quitarProducto(Producto $producto) {
$this->productos()->detach($producto);
}
}

View file

@ -18,6 +18,7 @@ return new class extends Migration
$table->unsignedBigInteger('barrio_id');
$table->timestamps();
$table->foreign('barrio_id')->references('id')->on('barrios');
$table->unique(['barrio_id','name']);
});
}