Compare commits

..

No commits in common. "1abc3f66c41a3d016b29364b287622322952b030" and "d0d323d6f7ac5fca8bff45cca575018123420582" have entirely different histories.

6 changed files with 25 additions and 30 deletions

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,16 +66,19 @@ class Barrio extends Model
} }
public function totalATransferir() : float { public function totalATransferir() : float {
return $this->totalProductosConTransporte() + $this->totalBonosDeTransporte(); return $this->totalNoBarriales() + $this->totalBonosDeTransporte();
} }
public function totalBonosDeTransporte() : int { public function totalBonosDeTransporte() : int {
return TransporteUtils::calcularTotal($this->totalProductosConTransporte()); return TransporteUtils::calcularTotal($this->totalNoBarriales());
} }
public function totalProductosConTransporte(): float public function totalNoBarriales() {
{ 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 {
@ -107,7 +110,6 @@ class Barrio extends Model
return false; return false;
} }
} }
return false;
} }
private function armarColumnaTotales() : array { private function armarColumnaTotales() : array {
@ -115,18 +117,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 $categoria) { foreach (Categoria::orderBy('id')->get() as $keyC => $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' => TransporteUtils::cantidad($this->totalProductosConTransporte())]; $columnaProductos[] = ['nombre' => 'Bono de Transporte', 'cantidad' => $this->totalBonosDeTransporte()];
if ($categoria->nombre == 'PRODUCTOS DE GESTIÓN MENSTRUAL') else 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 $producto) { foreach ($categoria->productos()->orderBy('id')->get() as $keyP => $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;
@ -139,6 +141,6 @@ class Barrio extends Model
} }
private function cantidadPedida($productoId, $productos) { private function cantidadPedida($productoId, $productos) {
return $productos->find($productoId)->cantidad ?? 0; return $productos->first(fn($p) => $p->id == $productoId)->cantidad ?? 0;
} }
} }

View file

@ -4,7 +4,6 @@ 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;
@ -46,23 +45,22 @@ class Pedido extends Model
return $this->belongsToMany(Producto::class)->withPivot(['cantidad']); return $this->belongsToMany(Producto::class)->withPivot(['cantidad']);
} }
public function productosConTransporte() : Collection public function productosConTransporte() {
{ 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()->find($producto->id); $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) 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()->find($producto->id); return $this->productos()->where('id', $producto->id)->first();
} }
} }
@ -82,7 +80,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,9 +48,4 @@ 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;
public static function cantidad(float $total) : int { private 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;

View file

@ -19,7 +19,7 @@ return new class extends Migration
Schema::create('barrios', function (Blueprint $table) { Schema::create('barrios', function (Blueprint $table) {
$table->id(); $table->id();
$table->string('nombre', 100); $table->string('nombre', 100);
$table->unsignedBigInteger('region_id'); $table->unsignedBigInteger('region_id');
$table->timestamps(); $table->timestamps();
$table->foreign('region_id')->references('id')->on('regiones'); $table->foreign('region_id')->references('id')->on('regiones');
@ -31,7 +31,7 @@ return new class extends Migration
*/ */
public function down(): void public function down(): void
{ {
Schema::dropIfExists('barrios');
Schema::dropIfExists('regiones'); Schema::dropIfExists('regiones');
Schema::dropIfExists('barrios');
} }
}; };

View file

@ -38,7 +38,7 @@ return new class extends Migration
*/ */
public function down(): void public function down(): void
{ {
Schema::dropIfExists('pedido_producto');
Schema::dropIfExists('pedidos'); Schema::dropIfExists('pedidos');
Schema::dropIfExists('pedido_producto');
} }
}; };