Compare commits
6 commits
197230a4ba
...
10b1dd738e
Author | SHA1 | Date | |
---|---|---|---|
10b1dd738e | |||
32a3015e89 | |||
a8057a2376 | |||
5f13f59e0e | |||
a52dce2b37 | |||
f9a5b5bca7 |
7 changed files with 142 additions and 90 deletions
|
@ -2,9 +2,12 @@
|
|||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Utils\TransporteUtils;
|
||||
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use League\Csv\CannotInsertRecord;
|
||||
use League\Csv\Writer;
|
||||
|
||||
|
@ -35,22 +38,47 @@ class Barrio extends Model
|
|||
return $this->hasMany(Pedido::class);
|
||||
}
|
||||
|
||||
function crearPedido(string $nombre) : Pedido {
|
||||
return $this->pedidos()->create(['nombre' => $name]);
|
||||
public function crearPedido(string $nombre) : Pedido {
|
||||
return $this->pedidos()->create(['nombre' => $nombre]);
|
||||
}
|
||||
|
||||
function totalARecaudar() : float {
|
||||
return $this->calcularTotalPagados();
|
||||
public function productosPedidos() {
|
||||
return DB::table('productos')
|
||||
->join('pedido_producto', 'productos.id', '=', 'pedido_producto.producto_id')
|
||||
->join('pedidos', 'pedidos.id', '=', 'pedido_producto.pedido_id')
|
||||
->where(['pedidos.barrio_id' => $this->id, 'pedidos.pagado' => true])
|
||||
->select('productos.*',
|
||||
DB::raw('SUM(pedido_producto.cantidad) as cantidad'),
|
||||
DB::raw('SUM(pedido_producto.cantidad * productos.precio) as total'))
|
||||
->groupBy('productos.id');
|
||||
}
|
||||
|
||||
private function calcularTotalPagados(Closure $closure = null) : float {
|
||||
if (!$closure)
|
||||
$closure = fn($p) => $p->totalChismosa();
|
||||
return $this->pedidosPagados()->sum($closure);
|
||||
public function totalARecaudar() : float {
|
||||
return $this->pedidos()->where(['pagado' => true])->get()->sum(
|
||||
fn($p) => $p->totalATransferir()
|
||||
);
|
||||
}
|
||||
|
||||
function totalATransferir() : float {
|
||||
return $this->calcularTotalPagados($p->totalATransferir());
|
||||
public function totalATransferir() : float {
|
||||
return $this->totalNoBarriales() + $this->totalBonosDeTransporte();
|
||||
}
|
||||
|
||||
public function totalNoBarriales() {
|
||||
return $this->totalProductosIf(['barrial' => false]);
|
||||
}
|
||||
|
||||
public function totalBarriales() {
|
||||
return $this->totalProductosIf(['barrial' => true]);
|
||||
}
|
||||
|
||||
private function totalProductosIf($predicado) : float {
|
||||
return $this->productosPedidos()->where($predicado)->get()->sum(
|
||||
fn($producto) => $producto->total
|
||||
);
|
||||
}
|
||||
|
||||
public function totalBonosDeTransporte() : int {
|
||||
return TransporteUtils::calcularTotal($this->totalNoBarriales());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -61,51 +89,52 @@ class Barrio extends Model
|
|||
return $this->hasMany(Producto::class);
|
||||
}
|
||||
|
||||
function exportarPedidoACsv() {
|
||||
if ($this->pedidosPagados()->exists()) {
|
||||
$columnaProductos = $this->armarColumnasPedido();
|
||||
public function exportarPedidoACsv() {
|
||||
if ($this->productosPedidos()->get()->isNotEmpty()) {
|
||||
$columnaProductos = $this->armarColumnaTotales();
|
||||
|
||||
try {
|
||||
$writer = Writer::createFromPath(resource_path('csv/exports/'.$this->nombre.'.csv'), 'w');
|
||||
$writer->setDelimiter("|");
|
||||
$writer->setEnclosure("'");
|
||||
$writer->insertAll($columnaProductos);
|
||||
return true;
|
||||
} catch (CannotInsertRecord $e) {
|
||||
var_export($e->getRecords());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function armarColumnasPedido() : array {
|
||||
private function armarColumnaTotales() : array {
|
||||
$columnaProductos = [];
|
||||
$filasVaciasAgregadas = false;
|
||||
$productos = $this->productosPedidos()->where(['barrial' => false])->get();
|
||||
|
||||
foreach (Categoria::orderBy('id')->get() as $keyC => $categoria) {
|
||||
$columnaProductos[] = ['name' => $categoria->name, 'cantidad' => null];
|
||||
if ($categoria->productos()->where(['barrial' => false])->count() == 0)
|
||||
continue;
|
||||
|
||||
if ($categoria->name == 'TRANSPORTE, BONOS Y FINANCIAMIENTO SORORO')
|
||||
$columnaProductos[] = ['name' => 'Bono de Transporte', 'cantidad' => TransporteUtils::cantidad($this->totalParaTransporte)];
|
||||
else if ($categoria->name == 'PRODUCTOS DE GESTIÓN MENSTRUAL')
|
||||
$columnaProductos[] = ['name' => '¿Cuántas copas quieren y pueden comprar en el grupo?', 'cantidad' => null];
|
||||
$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' => '¿Cuántas copas quieren y pueden comprar en el grupo?', 'cantidad' => null];
|
||||
|
||||
foreach ($categoria->productos()->orderBy('id')->get() as $keyP => $producto) {
|
||||
if ($producto->price == 0 && !$filasVaciasAgregadas) {
|
||||
$columnaProductos[] = ['name' => '¿Cuántas copas quieren adquirir a través del financiamiento sororo?', 'cantidad' => null];
|
||||
if ($producto->precio == 0 && !$filasVaciasAgregadas) {
|
||||
$columnaProductos[] = ['nombre' => '¿Cuántas copas quieren adquirir a través del financiamiento sororo?', 'cantidad' => null];
|
||||
$filasVaciasAgregadas = true;
|
||||
}
|
||||
$columnaProductos[] = ['name' => $producto->name, 'cantidad' => $this->cantidadPedida($producto->id)];
|
||||
$columnaProductos[] = ['nombre' => $producto->nombre, 'cantidad' => $this->cantidadPedida($producto->id, $productos)];
|
||||
}
|
||||
}
|
||||
|
||||
return $columnaProductos;
|
||||
}
|
||||
|
||||
private function cantidadPedida($productoId) {
|
||||
$pedidos = $this->pedidos()
|
||||
->whereHas('productos',
|
||||
function ($query) use ($productoId) {
|
||||
$query->where('producto_id', $productoId);})
|
||||
->get();
|
||||
|
||||
return $pedidos->sum(function ($pedido) use ($productoId) {
|
||||
return $pedido->productos
|
||||
->find($productoId)
|
||||
->pivot->cantidad ?? 0;});
|
||||
private function cantidadPedida($productoId, $productos) {
|
||||
return $productos->first(fn($p) => $p->id == $productoId)->cantidad ?? 0;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,13 +26,13 @@ class Pedido extends Model
|
|||
return $this->belongsTo(Barrio::class);
|
||||
}
|
||||
|
||||
function togglePagado() : bool {
|
||||
public function togglePagado() : bool {
|
||||
$this->pagado = !$this->pagado;
|
||||
$this->save();
|
||||
return $this->pagado;
|
||||
}
|
||||
|
||||
function toggleTerminado() : bool {
|
||||
public function toggleTerminado() : bool {
|
||||
$this->terminado = !$this->terminado;
|
||||
$this->save();
|
||||
return $this->terminado;
|
||||
|
@ -45,11 +45,18 @@ class Pedido extends Model
|
|||
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();
|
||||
if ($productoEnChismosa) {
|
||||
$productoEnChismosa->pivot->cantidad += $cantidad;
|
||||
$productoEnChismosa->save();
|
||||
if ($productoEnChismosa->pivot->cantidad != 0)
|
||||
$productoEnChismosa->save();
|
||||
else
|
||||
$this->quitarProducto($producto);
|
||||
return $productoEnChismosa;
|
||||
} else {
|
||||
$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);
|
||||
}
|
||||
|
||||
|
@ -65,8 +72,8 @@ class Pedido extends Model
|
|||
* El total de los productos del pedido
|
||||
* sumado al total de los bonos de transporte
|
||||
*/
|
||||
function totalChismosa() : float {
|
||||
return $this->totalProductos() + $this->totalTransporte();
|
||||
public function totalATransferir() : float {
|
||||
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
|
||||
* se toman todos los productos del pedido.
|
||||
*/
|
||||
function totalProductos($productos = null) : float {
|
||||
private function totalProductos($productos = null) {
|
||||
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
|
||||
*/
|
||||
function totalBonosDeTransporte() : int {
|
||||
return TransporteUtils::calcularTotal($this->productosConTransporte());
|
||||
}
|
||||
|
||||
function totalATransferir() : float {
|
||||
$productos = $this->productos()->where(['bono' => false, 'barrial' => false])->get();
|
||||
return $this->totalProductos($productos);
|
||||
public function totalBonosDeTransporte() : int {
|
||||
return TransporteUtils::calcularTotal(
|
||||
$this->totalProductos($this->productosConTransporte())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,19 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace App;
|
||||
namespace App\Utils;
|
||||
|
||||
class TransporteUtils
|
||||
{
|
||||
public const COSTO_TRANSPORTE = 15;
|
||||
public const DIVISOR_TRANSPORTE = 500;
|
||||
|
||||
static function cantidad(float $total) : int {
|
||||
private static function cantidad(float $total) : int {
|
||||
if ($total)
|
||||
return 1 + floor($total / DIVISOR_TRANSPORTE);
|
||||
return 1 + floor($total / TransporteUtils::DIVISOR_TRANSPORTE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static function calcularTotal(float $total) : int {
|
||||
return cantidad($total) * COSTO_TRANSPORTE;
|
||||
public static function calcularTotal(float $total) : int {
|
||||
return TransporteUtils::cantidad($total) * TransporteUtils::COSTO_TRANSPORTE;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,16 @@ return new class extends Migration
|
|||
$table->foreign('barrio_id')->references('id')->on('barrios');
|
||||
$table->unique(['barrio_id','nombre']);
|
||||
});
|
||||
|
||||
Schema::create('pedido_producto', function (Blueprint $table) {
|
||||
$table->unsignedBigInteger('pedido_id');
|
||||
$table->unsignedBigInteger('producto_id');
|
||||
$table->unsignedInteger('cantidad');
|
||||
$table->timestamps();
|
||||
$table->primary(['pedido_id','producto_id']);
|
||||
$table->foreign('pedido_id')->references('id')->on('pedidos');
|
||||
$table->foreign('producto_id')->references('id')->on('productos');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -29,5 +39,6 @@ return new class extends Migration
|
|||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('pedidos');
|
||||
Schema::dropIfExists('pedido_producto');
|
||||
}
|
||||
};
|
||||
|
|
|
@ -1,34 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use Illuminate\Support\Facades\Date;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||
use Illuminate\Database\Seeder;
|
||||
use App\Models\Region;
|
||||
use App\Models\Barrio;
|
||||
use App\Models\Producto;
|
||||
use App\Models\Categoria;
|
||||
|
||||
class BonoBarrialSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
Producto::create([
|
||||
'nombre' => 'Bono barrial',
|
||||
'precio' => 20,
|
||||
'solidario' => false,
|
||||
'bono' => true,
|
||||
'categoria_id' => Categoria::firstOrCreate([
|
||||
'nombre' => 'TRANSPORTE, BONOS Y FINANCIAMIENTO SORORO'
|
||||
])->id,
|
||||
'barrio_id' => Barrio::firstOrCreate([
|
||||
'nombre'=>'PRUEBA'
|
||||
])->id,
|
||||
]);
|
||||
}
|
||||
}
|
|
@ -18,7 +18,7 @@ class DatabaseSeeder extends Seeder
|
|||
CaracteristicaSeeder::class,
|
||||
CanastaSeeder::class,
|
||||
BarrioSeeder::class,
|
||||
BonoBarrialSeeder::class,
|
||||
TestDataSeeder::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
40
database/seeders/TestDataSeeder.php
Normal file
40
database/seeders/TestDataSeeder.php
Normal file
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use Illuminate\Support\Facades\Date;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||
use Illuminate\Database\Seeder;
|
||||
use App\Models\Region;
|
||||
use App\Models\Barrio;
|
||||
use App\Models\Producto;
|
||||
use App\Models\Categoria;
|
||||
|
||||
class TestDataSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
$barrioPrueba = Barrio::find(1);
|
||||
$productoBarrial = $barrioPrueba->productos()->create([
|
||||
'nombre' => 'Producto Barrial',
|
||||
'precio' => 100,
|
||||
'solidario' => true,
|
||||
'bono' => true,
|
||||
'barrial' => true,
|
||||
'categoria_id' => Categoria::firstOrCreate([
|
||||
'nombre' => 'PRODUCTOS BARRIALES'
|
||||
])->id
|
||||
]);
|
||||
$pedido = $barrioPrueba->crearPedido("Pedido de prueba");
|
||||
$pedido->agregarProducto($productoBarrial, 2);
|
||||
$pedido->agregarProducto(Producto::find(1), 1);
|
||||
$segundoPedido = $barrioPrueba->crearPedido("Segunda prueba");
|
||||
$segundoPedido->agregarProducto($productoBarrial, 5);
|
||||
$tercerPedido = $barrioPrueba->crearPedido("Tercera prueba");
|
||||
$tercerPedido->agregarProducto($productoBarrial, 3);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue