Eliminados metodos no usados + nombres mas descriptivos

This commit is contained in:
Alejandro Tasistro 2024-07-05 19:10:13 -03:00
parent 3b0ad4b4f0
commit 197230a4ba
4 changed files with 44 additions and 94 deletions

View file

@ -39,36 +39,20 @@ class Barrio extends Model
return $this->pedidos()->create(['nombre' => $name]); return $this->pedidos()->create(['nombre' => $name]);
} }
function pedidosPagados() {
return $this->pedidos()->where('pagado',true);
}
function totalARecaudar() : float { function totalARecaudar() : float {
return $this->calcularTotalPagados(); return $this->calcularTotalPagados();
} }
function totalNoBarriales() : float {
return $this->calcularTotalPagados(
fn($p) => $p->total($p->productosNoBarriales())
);
}
function totalParaTransporte() : float {
return $this->calcularTotalPagados(
fn($p) => $p->total($p->productosConTransporte())
);
}
function totalATransferir() : float {
return $this->totalNoBarriales() + TransporteUtils::total($this->totalParaTransporte());
}
private function calcularTotalPagados(Closure $closure = null) : float { private function calcularTotalPagados(Closure $closure = null) : float {
if (!$closure) if (!$closure)
$closure = fn($p) => $p->totalChismosa(); $closure = fn($p) => $p->totalChismosa();
return $this->pedidosPagados()->sum($closure); return $this->pedidosPagados()->sum($closure);
} }
function totalATransferir() : float {
return $this->calcularTotalPagados($p->totalATransferir());
}
/** /**
* Los productos que pertenecen al barrio. * Los productos que pertenecen al barrio.
*/ */
@ -112,8 +96,7 @@ class Barrio extends Model
} }
} }
private function cantidadPedida($productoId) private function cantidadPedida($productoId) {
{
$pedidos = $this->pedidos() $pedidos = $this->pedidos()
->whereHas('productos', ->whereHas('productos',
function ($query) use ($productoId) { function ($query) use ($productoId) {
@ -122,7 +105,7 @@ class Barrio extends Model
return $pedidos->sum(function ($pedido) use ($productoId) { return $pedidos->sum(function ($pedido) use ($productoId) {
return $pedido->productos return $pedido->productos
->where('id', $productoId) ->find($productoId)
->first()->pivot->cantidad ?? 0;}); ->pivot->cantidad ?? 0;});
} }
} }

View file

@ -26,6 +26,18 @@ class Pedido extends Model
return $this->belongsTo(Barrio::class); return $this->belongsTo(Barrio::class);
} }
function togglePagado() : bool {
$this->pagado = !$this->pagado;
$this->save();
return $this->pagado;
}
function toggleTerminado() : bool {
$this->terminado = !$this->terminado;
$this->save();
return $this->terminado;
}
/** /**
* Los productos que pertenecen al pedido. * Los productos que pertenecen al pedido.
*/ */
@ -33,34 +45,28 @@ class Pedido extends Model
return $this->belongsToMany(Producto::class)->withPivot(['cantidad']); return $this->belongsToMany(Producto::class)->withPivot(['cantidad']);
} }
/** function agregarProducto(Producto $producto, int $cantidad) {
* Los productos del pedido que no aportan para el $productoEnChismosa = $this->productos()->where('id', $producto->id)->first();
* bono de transporte if ($productoEnChismosa) {
*/ $productoEnChismosa->pivot->cantidad += $cantidad;
function productosSinTransporte() { $productoEnChismosa->save();
return $this->productos()->orWhere(['bono' => true, 'barrial' => true]); return $productoEnChismosa;
} else {
$this->productos()->attach($producto, ['cantidad' => $cantidad]);
return $this->productos()->where('id', $producto->id)->first();
}
}
function quitarProducto(Producto $producto) {
$this->productos()->detach($producto);
} }
/** /**
* Los productos del pedido que aportan para el * El total de los productos del pedido
* bono de transporte. * sumado al total de los bonos de transporte
*/ */
function productosConTransporte() { function totalChismosa() : float {
return $this->productos()->where(['bono' => false, 'barrial' => false]); return $this->totalProductos() + $this->totalTransporte();
}
/**
* Los productos no barriales del pedido.
*/
function productosNoBarriales() {
return $this->productos()->where('barrial',false);
}
/**
* Los bonos del pedido.
*/
function bonos() {
return $this->productos()->where('bono',true);
} }
/** /**
@ -71,7 +77,7 @@ 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 total($productos = null) : float { function totalProductos($productos = null) : float {
if (!$productos) if (!$productos)
$productos = $this->productos(); $productos = $this->productos();
@ -81,38 +87,12 @@ class Pedido extends Model
/** /**
* El total de bonos de transporte del pedido * El total de bonos de transporte del pedido
*/ */
function totalTransporte() : int { function totalBonosDeTransporte() : int {
if ($this->productos()->every(fn($prod) => !$prod->pagaTransporte())) return TransporteUtils::calcularTotal($this->productosConTransporte());
return 0;
return TransporteUtils::total($this->productosConTransporte());
} }
/** function totalATransferir() : float {
* El total de los productos del pedido $productos = $this->productos()->where(['bono' => false, 'barrial' => false])->get();
* sumado al total de los bonos de transporte return $this->totalProductos($productos);
*/
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

@ -48,17 +48,4 @@ class Producto extends Model
{ {
return $this->belongsTo(Barrio::class); return $this->belongsTo(Barrio::class);
} }
/**
* Los productos centrales.
*/
public static function centrales() {
return Producto::where([
'barrial' => false,
]);
}
function pagaTransporte() : bool {
return !$this->bono && !$this->barrial;
}
} }

View file

@ -13,7 +13,7 @@ class TransporteUtils
return 0; return 0;
} }
static function total(float $total) : int { static function calcularTotal(float $total) : int {
return cantidad($total) * COSTO_TRANSPORTE; return cantidad($total) * COSTO_TRANSPORTE;
} }
} }