feature/columna-totales #4

Open
atasistro wants to merge 46 commits from feature/columna-totales into master
4 changed files with 27 additions and 22 deletions
Showing only changes of commit 1abc3f66c4 - Show all commits

View file

@ -53,7 +53,7 @@ class Barrio extends Model
->join('pedido_producto', 'productos.id', '=', 'pedido_producto.producto_id') ->join('pedido_producto', 'productos.id', '=', 'pedido_producto.producto_id')
->join('pedidos', 'pedidos.id', '=', 'pedido_producto.pedido_id') ->join('pedidos', 'pedidos.id', '=', 'pedido_producto.pedido_id')
->where(['pedidos.barrio_id' => $this->id, 'pedidos.pagado' => true]) ->where(['pedidos.barrio_id' => $this->id, 'pedidos.pagado' => true])
->select('productos.*', ->select('productos.*',
DB::raw('SUM(pedido_producto.cantidad) as cantidad'), DB::raw('SUM(pedido_producto.cantidad) as cantidad'),
DB::raw('SUM(pedido_producto.cantidad * productos.precio) as total')) DB::raw('SUM(pedido_producto.cantidad * productos.precio) as total'))
->groupBy('productos.id'); ->groupBy('productos.id');
@ -66,19 +66,16 @@ class Barrio extends Model
} }
public function totalATransferir() : float { public function totalATransferir() : float {
return $this->totalNoBarriales() + $this->totalBonosDeTransporte(); return $this->totalProductosConTransporte() + $this->totalBonosDeTransporte();
} }
public function totalBonosDeTransporte() : int { public function totalBonosDeTransporte() : int {
return TransporteUtils::calcularTotal($this->totalNoBarriales()); return TransporteUtils::calcularTotal($this->totalProductosConTransporte());
} }
public function totalNoBarriales() { public function totalProductosConTransporte(): float
return $this->totalProductosIf(['barrial' => false]); {
} return $this->totalProductosIf(fn($p) => $p->pagaTransporte());
public function totalBarriales() {
return $this->totalProductosIf(['barrial' => true]);
} }
private function totalProductosIf($predicado) : float { private function totalProductosIf($predicado) : float {
@ -110,6 +107,7 @@ class Barrio extends Model
return false; return false;
} }
} }
return false;
} }
private function armarColumnaTotales() : array { private function armarColumnaTotales() : array {
@ -117,18 +115,18 @@ class Barrio extends Model
$filasVaciasAgregadas = false; $filasVaciasAgregadas = false;
$productos = $this->productosPedidos()->where(['barrial' => false])->get(); $productos = $this->productosPedidos()->where(['barrial' => false])->get();
foreach (Categoria::orderBy('id')->get() as $keyC => $categoria) { foreach (Categoria::orderBy('id')->get() as $categoria) {
if ($categoria->productos()->where(['barrial' => false])->count() == 0) if ($categoria->productos()->where(['barrial' => false])->count() == 0)
continue; continue;
$columnaProductos[] = ['nombre' => $categoria->nombre, 'cantidad' => null]; $columnaProductos[] = ['nombre' => $categoria->nombre, 'cantidad' => null];
if ($categoria->nombre == 'TRANSPORTE, BONOS Y FINANCIAMIENTO SORORO') if ($categoria->nombre == 'TRANSPORTE, BONOS Y FINANCIAMIENTO SORORO')
$columnaProductos[] = ['nombre' => 'Bono de Transporte', 'cantidad' => $this->totalBonosDeTransporte()]; $columnaProductos[] = ['nombre' => 'Bono de Transporte', 'cantidad' => TransporteUtils::cantidad($this->totalProductosConTransporte())];
else if ($categoria->nombre == 'PRODUCTOS DE GESTIÓN MENSTRUAL') if ($categoria->nombre == 'PRODUCTOS DE GESTIÓN MENSTRUAL')
$columnaProductos[] = ['nombre' => '¿Cuántas copas quieren y pueden comprar en el grupo?', 'cantidad' => null]; $columnaProductos[] = ['nombre' => '¿Cuántas copas quieren y pueden comprar en el grupo?', 'cantidad' => null];
foreach ($categoria->productos()->orderBy('id')->get() as $keyP => $producto) { foreach ($categoria->productos()->orderBy('id')->get() as $producto) {
if ($producto->precio == 0 && !$filasVaciasAgregadas) { if ($producto->precio == 0 && !$filasVaciasAgregadas) {
$columnaProductos[] = ['nombre' => '¿Cuántas copas quieren adquirir a través del financiamiento sororo?', 'cantidad' => null]; $columnaProductos[] = ['nombre' => '¿Cuántas copas quieren adquirir a través del financiamiento sororo?', 'cantidad' => null];
$filasVaciasAgregadas = true; $filasVaciasAgregadas = true;
@ -141,6 +139,6 @@ class Barrio extends Model
} }
private function cantidadPedida($productoId, $productos) { private function cantidadPedida($productoId, $productos) {
return $productos->first(fn($p) => $p->id == $productoId)->cantidad ?? 0; return $productos->find($productoId)->cantidad ?? 0;
} }
} }

View file

@ -4,6 +4,7 @@ namespace App\Models;
use App\Utils\TransporteUtils; use App\Utils\TransporteUtils;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
@ -45,22 +46,23 @@ class Pedido extends Model
return $this->belongsToMany(Producto::class)->withPivot(['cantidad']); return $this->belongsToMany(Producto::class)->withPivot(['cantidad']);
} }
public function productosConTransporte() { public function productosConTransporte() : Collection
return $this->productos()->where(['bono' => false, 'barrial' => false])->get(); {
return $this->productos()->where(fn($p) => $p->pagaTransporte())->get();
} }
public function agregarProducto(Producto $producto, int $cantidad) : Producto { public function agregarProducto(Producto $producto, int $cantidad) : Producto {
$productoEnChismosa = $this->productos()->where('id', $producto->id)->first(); $productoEnChismosa = $this->productos()->find($producto->id);
if ($productoEnChismosa) { if ($productoEnChismosa) {
$productoEnChismosa->pivot->cantidad += $cantidad; $productoEnChismosa->pivot->cantidad += $cantidad;
if ($productoEnChismosa->pivot->cantidad != 0) if ($productoEnChismosa->pivot->cantidad != 0)
$productoEnChismosa->save(); $productoEnChismosa->save();
else else
$this->quitarProducto($producto); $this->quitarProducto($producto);
return $productoEnChismosa; return $productoEnChismosa;
} else { } else {
$this->productos()->attach($producto, ['cantidad' => $cantidad]); $this->productos()->attach($producto, ['cantidad' => $cantidad]);
return $this->productos()->where('id', $producto->id)->first(); return $this->productos()->find($producto->id);
} }
} }
@ -80,7 +82,7 @@ class Pedido extends Model
* Toma como parámetro una colección de productos * Toma como parámetro una colección de productos
* y devuelve la suma de los totales (precio * cantidad) * y devuelve la suma de los totales (precio * cantidad)
* de cada uno. * de cada uno.
* *
* 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.
*/ */

View file

@ -48,4 +48,9 @@ class Producto extends Model
{ {
return $this->belongsTo(Barrio::class); return $this->belongsTo(Barrio::class);
} }
public function pagaTransporte() : bool
{
return !$this->bono && !$this->barrial;
}
} }

View file

@ -6,8 +6,8 @@ class TransporteUtils
{ {
public const COSTO_TRANSPORTE = 15; public const COSTO_TRANSPORTE = 15;
public const DIVISOR_TRANSPORTE = 500; public const DIVISOR_TRANSPORTE = 500;
private static function cantidad(float $total) : int { public static function cantidad(float $total) : int {
if ($total) if ($total)
return 1 + floor($total / TransporteUtils::DIVISOR_TRANSPORTE); return 1 + floor($total / TransporteUtils::DIVISOR_TRANSPORTE);
return 0; return 0;