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

@ -66,19 +66,16 @@ class Barrio extends Model
}
public function totalATransferir() : float {
return $this->totalNoBarriales() + $this->totalBonosDeTransporte();
return $this->totalProductosConTransporte() + $this->totalBonosDeTransporte();
}
public function totalBonosDeTransporte() : int {
return TransporteUtils::calcularTotal($this->totalNoBarriales());
return TransporteUtils::calcularTotal($this->totalProductosConTransporte());
}
public function totalNoBarriales() {
return $this->totalProductosIf(['barrial' => false]);
}
public function totalBarriales() {
return $this->totalProductosIf(['barrial' => true]);
public function totalProductosConTransporte(): float
{
return $this->totalProductosIf(fn($p) => $p->pagaTransporte());
}
private function totalProductosIf($predicado) : float {
@ -110,6 +107,7 @@ class Barrio extends Model
return false;
}
}
return false;
}
private function armarColumnaTotales() : array {
@ -117,18 +115,18 @@ class Barrio extends Model
$filasVaciasAgregadas = false;
$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)
continue;
$columnaProductos[] = ['nombre' => $categoria->nombre, 'cantidad' => null];
if ($categoria->nombre == 'TRANSPORTE, BONOS Y FINANCIAMIENTO SORORO')
$columnaProductos[] = ['nombre' => 'Bono de Transporte', 'cantidad' => $this->totalBonosDeTransporte()];
else if ($categoria->nombre == 'PRODUCTOS DE GESTIÓN MENSTRUAL')
$columnaProductos[] = ['nombre' => 'Bono de Transporte', 'cantidad' => TransporteUtils::cantidad($this->totalProductosConTransporte())];
if ($categoria->nombre == 'PRODUCTOS DE GESTIÓN MENSTRUAL')
$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) {
$columnaProductos[] = ['nombre' => '¿Cuántas copas quieren adquirir a través del financiamiento sororo?', 'cantidad' => null];
$filasVaciasAgregadas = true;
@ -141,6 +139,6 @@ class Barrio extends Model
}
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 Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Model;
@ -45,12 +46,13 @@ class Pedido extends Model
return $this->belongsToMany(Producto::class)->withPivot(['cantidad']);
}
public function productosConTransporte() {
return $this->productos()->where(['bono' => false, 'barrial' => false])->get();
public function productosConTransporte() : Collection
{
return $this->productos()->where(fn($p) => $p->pagaTransporte())->get();
}
public function agregarProducto(Producto $producto, int $cantidad) : Producto {
$productoEnChismosa = $this->productos()->where('id', $producto->id)->first();
$productoEnChismosa = $this->productos()->find($producto->id);
if ($productoEnChismosa) {
$productoEnChismosa->pivot->cantidad += $cantidad;
if ($productoEnChismosa->pivot->cantidad != 0)
@ -60,7 +62,7 @@ class Pedido extends Model
return $productoEnChismosa;
} else {
$this->productos()->attach($producto, ['cantidad' => $cantidad]);
return $this->productos()->where('id', $producto->id)->first();
return $this->productos()->find($producto->id);
}
}

View file

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

View file

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