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;
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Utils\TransporteUtils;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
use League\Csv\CannotInsertRecord;
|
use League\Csv\CannotInsertRecord;
|
||||||
use League\Csv\Writer;
|
use League\Csv\Writer;
|
||||||
|
|
||||||
|
@ -35,22 +38,47 @@ class Barrio extends Model
|
||||||
return $this->hasMany(Pedido::class);
|
return $this->hasMany(Pedido::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
function crearPedido(string $nombre) : Pedido {
|
public function crearPedido(string $nombre) : Pedido {
|
||||||
return $this->pedidos()->create(['nombre' => $name]);
|
return $this->pedidos()->create(['nombre' => $nombre]);
|
||||||
}
|
}
|
||||||
|
|
||||||
function totalARecaudar() : float {
|
public function productosPedidos() {
|
||||||
return $this->calcularTotalPagados();
|
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 {
|
public function totalARecaudar() : float {
|
||||||
if (!$closure)
|
return $this->pedidos()->where(['pagado' => true])->get()->sum(
|
||||||
$closure = fn($p) => $p->totalChismosa();
|
fn($p) => $p->totalATransferir()
|
||||||
return $this->pedidosPagados()->sum($closure);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function totalATransferir() : float {
|
public function totalATransferir() : float {
|
||||||
return $this->calcularTotalPagados($p->totalATransferir());
|
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);
|
return $this->hasMany(Producto::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
function exportarPedidoACsv() {
|
public function exportarPedidoACsv() {
|
||||||
if ($this->pedidosPagados()->exists()) {
|
if ($this->productosPedidos()->get()->isNotEmpty()) {
|
||||||
$columnaProductos = $this->armarColumnasPedido();
|
$columnaProductos = $this->armarColumnaTotales();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$writer = Writer::createFromPath(resource_path('csv/exports/'.$this->nombre.'.csv'), 'w');
|
$writer = Writer::createFromPath(resource_path('csv/exports/'.$this->nombre.'.csv'), 'w');
|
||||||
|
$writer->setDelimiter("|");
|
||||||
|
$writer->setEnclosure("'");
|
||||||
$writer->insertAll($columnaProductos);
|
$writer->insertAll($columnaProductos);
|
||||||
|
return true;
|
||||||
} catch (CannotInsertRecord $e) {
|
} catch (CannotInsertRecord $e) {
|
||||||
var_export($e->getRecords());
|
var_export($e->getRecords());
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function armarColumnasPedido() : array {
|
private function armarColumnaTotales() : array {
|
||||||
$columnaProductos = [];
|
$columnaProductos = [];
|
||||||
$filasVaciasAgregadas = false;
|
$filasVaciasAgregadas = false;
|
||||||
|
$productos = $this->productosPedidos()->where(['barrial' => false])->get();
|
||||||
|
|
||||||
foreach (Categoria::orderBy('id')->get() as $keyC => $categoria) {
|
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[] = ['nombre' => $categoria->nombre, 'cantidad' => null];
|
||||||
$columnaProductos[] = ['name' => 'Bono de Transporte', 'cantidad' => TransporteUtils::cantidad($this->totalParaTransporte)];
|
|
||||||
else if ($categoria->name == 'PRODUCTOS DE GESTIÓN MENSTRUAL')
|
if ($categoria->nombre == 'TRANSPORTE, BONOS Y FINANCIAMIENTO SORORO')
|
||||||
$columnaProductos[] = ['name' => '¿Cuántas copas quieren y pueden comprar en el grupo?', 'cantidad' => null];
|
$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) {
|
foreach ($categoria->productos()->orderBy('id')->get() as $keyP => $producto) {
|
||||||
if ($producto->price == 0 && !$filasVaciasAgregadas) {
|
if ($producto->precio == 0 && !$filasVaciasAgregadas) {
|
||||||
$columnaProductos[] = ['name' => '¿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;
|
||||||
}
|
}
|
||||||
$columnaProductos[] = ['name' => $producto->name, 'cantidad' => $this->cantidadPedida($producto->id)];
|
$columnaProductos[] = ['nombre' => $producto->nombre, 'cantidad' => $this->cantidadPedida($producto->id, $productos)];
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function cantidadPedida($productoId) {
|
return $columnaProductos;
|
||||||
$pedidos = $this->pedidos()
|
}
|
||||||
->whereHas('productos',
|
|
||||||
function ($query) use ($productoId) {
|
|
||||||
$query->where('producto_id', $productoId);})
|
|
||||||
->get();
|
|
||||||
|
|
||||||
return $pedidos->sum(function ($pedido) use ($productoId) {
|
private function cantidadPedida($productoId, $productos) {
|
||||||
return $pedido->productos
|
return $productos->first(fn($p) => $p->id == $productoId)->cantidad ?? 0;
|
||||||
->find($productoId)
|
|
||||||
->pivot->cantidad ?? 0;});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,13 +26,13 @@ class Pedido extends Model
|
||||||
return $this->belongsTo(Barrio::class);
|
return $this->belongsTo(Barrio::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
function togglePagado() : bool {
|
public function togglePagado() : bool {
|
||||||
$this->pagado = !$this->pagado;
|
$this->pagado = !$this->pagado;
|
||||||
$this->save();
|
$this->save();
|
||||||
return $this->pagado;
|
return $this->pagado;
|
||||||
}
|
}
|
||||||
|
|
||||||
function toggleTerminado() : bool {
|
public function toggleTerminado() : bool {
|
||||||
$this->terminado = !$this->terminado;
|
$this->terminado = !$this->terminado;
|
||||||
$this->save();
|
$this->save();
|
||||||
return $this->terminado;
|
return $this->terminado;
|
||||||
|
@ -45,11 +45,18 @@ class Pedido extends Model
|
||||||
return $this->belongsToMany(Producto::class)->withPivot(['cantidad']);
|
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();
|
$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)
|
||||||
$productoEnChismosa->save();
|
$productoEnChismosa->save();
|
||||||
|
else
|
||||||
|
$this->quitarProducto($producto);
|
||||||
return $productoEnChismosa;
|
return $productoEnChismosa;
|
||||||
} else {
|
} else {
|
||||||
$this->productos()->attach($producto, ['cantidad' => $cantidad]);
|
$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);
|
$this->productos()->detach($producto);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,8 +72,8 @@ class Pedido extends Model
|
||||||
* El total de los productos del pedido
|
* El total de los productos del pedido
|
||||||
* sumado al total de los bonos de transporte
|
* sumado al total de los bonos de transporte
|
||||||
*/
|
*/
|
||||||
function totalChismosa() : float {
|
public function totalATransferir() : float {
|
||||||
return $this->totalProductos() + $this->totalTransporte();
|
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
|
* 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 totalProductos($productos = null) : float {
|
private function totalProductos($productos = null) {
|
||||||
if (!$productos)
|
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
|
* El total de bonos de transporte del pedido
|
||||||
*/
|
*/
|
||||||
function totalBonosDeTransporte() : int {
|
public function totalBonosDeTransporte() : int {
|
||||||
return TransporteUtils::calcularTotal($this->productosConTransporte());
|
return TransporteUtils::calcularTotal(
|
||||||
}
|
$this->totalProductos($this->productosConTransporte())
|
||||||
|
);
|
||||||
function totalATransferir() : float {
|
|
||||||
$productos = $this->productos()->where(['bono' => false, 'barrial' => false])->get();
|
|
||||||
return $this->totalProductos($productos);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,19 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App;
|
namespace App\Utils;
|
||||||
|
|
||||||
class TransporteUtils
|
class TransporteUtils
|
||||||
{
|
{
|
||||||
public const COSTO_TRANSPORTE = 15;
|
public const COSTO_TRANSPORTE = 15;
|
||||||
public const DIVISOR_TRANSPORTE = 500;
|
public const DIVISOR_TRANSPORTE = 500;
|
||||||
|
|
||||||
static function cantidad(float $total) : int {
|
private static function cantidad(float $total) : int {
|
||||||
if ($total)
|
if ($total)
|
||||||
return 1 + floor($total / DIVISOR_TRANSPORTE);
|
return 1 + floor($total / TransporteUtils::DIVISOR_TRANSPORTE);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static function calcularTotal(float $total) : int {
|
public static function calcularTotal(float $total) : int {
|
||||||
return cantidad($total) * COSTO_TRANSPORTE;
|
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->foreign('barrio_id')->references('id')->on('barrios');
|
||||||
$table->unique(['barrio_id','nombre']);
|
$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
|
public function down(): void
|
||||||
{
|
{
|
||||||
Schema::dropIfExists('pedidos');
|
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,
|
CaracteristicaSeeder::class,
|
||||||
CanastaSeeder::class,
|
CanastaSeeder::class,
|
||||||
BarrioSeeder::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