feature/columna-totales #4

Open
atasistro wants to merge 46 commits from feature/columna-totales into master
Showing only changes of commit a8057a2376 - Show all commits

View file

@ -26,13 +26,13 @@ class Pedido extends Model
return $this->belongsTo(Barrio::class); return $this->belongsTo(Barrio::class);
} }
function togglePagado() : bool { public function togglePagado() : bool {
$this->pagado = !$this->pagado; $this->pagado = !$this->pagado;
$this->save(); $this->save();
return $this->pagado; return $this->pagado;
} }
function toggleTerminado() : bool { public function toggleTerminado() : bool {
$this->terminado = !$this->terminado; $this->terminado = !$this->terminado;
$this->save(); $this->save();
return $this->terminado; return $this->terminado;
@ -45,11 +45,18 @@ class Pedido extends Model
return $this->belongsToMany(Producto::class)->withPivot(['cantidad']); return $this->belongsToMany(Producto::class)->withPivot(['cantidad']);
} }
function agregarProducto(Producto $producto, int $cantidad) { public function productosConTransporte() {
return $this->productos()->where(['bono' => false, 'barrial' => false])->get();
}
public function agregarProducto(Producto $producto, int $cantidad) : Producto {
$productoEnChismosa = $this->productos()->where('id', $producto->id)->first(); $productoEnChismosa = $this->productos()->where('id', $producto->id)->first();
if ($productoEnChismosa) { if ($productoEnChismosa) {
$productoEnChismosa->pivot->cantidad += $cantidad; $productoEnChismosa->pivot->cantidad += $cantidad;
if ($productoEnChismosa->pivot->cantidad != 0)
$productoEnChismosa->save(); $productoEnChismosa->save();
else
$this->quitarProducto($producto);
return $productoEnChismosa; return $productoEnChismosa;
} else { } else {
$this->productos()->attach($producto, ['cantidad' => $cantidad]); $this->productos()->attach($producto, ['cantidad' => $cantidad]);
@ -57,7 +64,7 @@ class Pedido extends Model
} }
} }
function quitarProducto(Producto $producto) { public function quitarProducto(Producto $producto) {
$this->productos()->detach($producto); $this->productos()->detach($producto);
} }
@ -65,8 +72,8 @@ class Pedido extends Model
* El total de los productos del pedido * El total de los productos del pedido
* sumado al total de los bonos de transporte * sumado al total de los bonos de transporte
*/ */
function totalChismosa() : float { public function totalATransferir() : float {
return $this->totalProductos() + $this->totalTransporte(); return $this->totalProductos() + $this->totalBonosDeTransporte();
} }
/** /**
@ -77,22 +84,21 @@ class Pedido extends Model
* Si la colección es null o no se pasa ningún parámetro * Si la colección es null o no se pasa ningún parámetro
* se toman todos los productos del pedido. * se toman todos los productos del pedido.
*/ */
function totalProductos($productos = null) : float { private function totalProductos($productos = null) {
if (!$productos) if (!$productos)
$productos = $this->productos(); $productos = $this->productos()->get();
return $productos->sum(fn($p) => $p->price * $p->pivot->cantidad); return $productos->map(
fn($producto) => $producto->precio * $producto->pivot->cantidad
)->sum();
} }
/** /**
* El total de bonos de transporte del pedido * El total de bonos de transporte del pedido
*/ */
function totalBonosDeTransporte() : int { public function totalBonosDeTransporte() : int {
return TransporteUtils::calcularTotal($this->productosConTransporte()); return TransporteUtils::calcularTotal(
} $this->totalProductos($this->productosConTransporte())
);
function totalATransferir() : float {
$productos = $this->productos()->where(['bono' => false, 'barrial' => false])->get();
return $this->totalProductos($productos);
} }
} }