2024-03-11 19:08:31 -03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2024-07-09 22:15:09 -03:00
|
|
|
use App\Utils\TransporteUtils;
|
|
|
|
|
2024-03-19 15:40:27 -03:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2024-03-11 19:08:31 -03:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2024-07-09 22:15:09 -03:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2024-03-20 00:13:53 -03:00
|
|
|
use League\Csv\CannotInsertRecord;
|
|
|
|
use League\Csv\Writer;
|
2024-03-11 19:08:31 -03:00
|
|
|
|
|
|
|
class Barrio extends Model
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
*
|
|
|
|
* @var array<int, string>
|
|
|
|
*/
|
|
|
|
protected $fillable = [
|
2024-07-03 19:22:42 -03:00
|
|
|
'nombre',
|
2024-03-11 19:08:31 -03:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* La región a la que pertenece el barrio.
|
|
|
|
*/
|
|
|
|
public function region(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Region::class);
|
|
|
|
}
|
2024-03-11 19:41:52 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Los pedidos que pertenecen al barrio.
|
|
|
|
*/
|
|
|
|
public function pedidos(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany(Pedido::class);
|
|
|
|
}
|
2024-03-14 23:04:54 -03:00
|
|
|
|
2024-07-09 22:15:09 -03:00
|
|
|
public function crearPedido(string $nombre) : Pedido {
|
|
|
|
return $this->pedidos()->create(['nombre' => $nombre]);
|
|
|
|
}
|
|
|
|
|
|
|
|
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')
|
|
|
|
->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function totalARecaudar() : float {
|
|
|
|
return $this->pedidos()->where(['pagado' => true])->get()->sum(
|
|
|
|
fn($p) => $p->totalATransferir()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function totalATransferir() : float {
|
|
|
|
return $this->totalNoBarriales() + $this->totalBonosDeTransporte();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function totalNoBarriales() {
|
|
|
|
return $this->totalProductosIf(fn($p) => !$p->barrial);
|
2024-03-19 22:34:13 -03:00
|
|
|
}
|
|
|
|
|
2024-07-09 22:15:09 -03:00
|
|
|
public function totalBarriales() {
|
|
|
|
return $this->totalProductosIf(fn($p) => $p->barrial);
|
2024-03-19 22:13:49 -03:00
|
|
|
}
|
|
|
|
|
2024-07-09 22:15:09 -03:00
|
|
|
private function totalProductosIf(callable $predicado) : float {
|
|
|
|
return $this->productosPedidos()->sum(
|
|
|
|
function ($producto) use ($predicado) {
|
|
|
|
return $predicado($producto) ? $producto->total : 0;
|
|
|
|
}
|
|
|
|
);
|
2024-03-20 00:26:05 -03:00
|
|
|
}
|
|
|
|
|
2024-07-09 22:15:09 -03:00
|
|
|
public function totalBonosDeTransporte() : int {
|
|
|
|
return TransporteUtils::calcularTotal($this->totalNoBarriales());
|
2024-07-05 19:10:13 -03:00
|
|
|
}
|
|
|
|
|
2024-03-14 23:04:54 -03:00
|
|
|
/**
|
|
|
|
* Los productos que pertenecen al barrio.
|
|
|
|
*/
|
|
|
|
public function productos(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany(Producto::class);
|
|
|
|
}
|
2024-03-20 00:13:53 -03:00
|
|
|
|
2024-03-20 00:26:05 -03:00
|
|
|
function exportarPedidoACsv() {
|
2024-07-03 19:35:27 -03:00
|
|
|
if ($this->pedidosPagados()->exists()) {
|
2024-03-20 00:26:05 -03:00
|
|
|
$columnaProductos = $this->armarColumnasPedido();
|
2024-03-20 00:13:53 -03:00
|
|
|
|
2024-03-20 00:26:05 -03:00
|
|
|
try {
|
|
|
|
$writer = Writer::createFromPath(resource_path('csv/exports/'.$this->nombre.'.csv'), 'w');
|
|
|
|
$writer->insertAll($columnaProductos);
|
|
|
|
} catch (CannotInsertRecord $e) {
|
|
|
|
var_export($e->getRecords());
|
|
|
|
}
|
|
|
|
}
|
2024-03-20 00:13:53 -03:00
|
|
|
}
|
|
|
|
|
2024-03-20 00:26:05 -03:00
|
|
|
private function armarColumnasPedido() : array {
|
2024-03-20 00:13:53 -03:00
|
|
|
$columnaProductos = [];
|
|
|
|
$filasVaciasAgregadas = false;
|
|
|
|
|
2024-03-20 00:46:08 -03:00
|
|
|
foreach (Categoria::orderBy('id')->get() as $keyC => $categoria) {
|
2024-03-20 00:13:53 -03:00
|
|
|
$columnaProductos[] = ['name' => $categoria->name, 'cantidad' => null];
|
|
|
|
|
|
|
|
if ($categoria->name == 'TRANSPORTE, BONOS Y FINANCIAMIENTO SORORO')
|
|
|
|
$columnaProductos[] = ['name' => 'Bono de Transporte', 'cantidad' => TransporteUtils::cantidad($this->totalParaTransporte)];
|
2024-03-31 18:22:03 -03:00
|
|
|
else if ($categoria->name == 'PRODUCTOS DE GESTIÓN MENSTRUAL')
|
2024-03-20 00:13:53 -03:00
|
|
|
$columnaProductos[] = ['name' => '¿Cuántas copas quieren y pueden comprar en el grupo?', 'cantidad' => null];
|
|
|
|
|
2024-03-20 00:46:08 -03:00
|
|
|
foreach ($categoria->productos()->orderBy('id')->get() as $keyP => $producto) {
|
2024-03-20 00:13:53 -03:00
|
|
|
if ($producto->price == 0 && !$filasVaciasAgregadas) {
|
|
|
|
$columnaProductos[] = ['name' => '¿Cuántas copas quieren adquirir a través del financiamiento sororo?', 'cantidad' => null];
|
|
|
|
$filasVaciasAgregadas = true;
|
|
|
|
}
|
2024-03-31 18:22:03 -03:00
|
|
|
$columnaProductos[] = ['name' => $producto->name, 'cantidad' => $this->cantidadPedida($producto->id)];
|
2024-03-20 00:13:53 -03:00
|
|
|
}
|
|
|
|
}
|
2024-03-20 00:26:05 -03:00
|
|
|
}
|
2024-03-20 00:13:53 -03:00
|
|
|
|
2024-07-05 19:10:13 -03:00
|
|
|
private function cantidadPedida($productoId) {
|
2024-03-31 18:22:03 -03:00
|
|
|
$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
|
2024-07-05 19:10:13 -03:00
|
|
|
->find($productoId)
|
|
|
|
->pivot->cantidad ?? 0;});
|
2024-03-20 00:13:53 -03:00
|
|
|
}
|
2024-03-11 19:08:31 -03:00
|
|
|
}
|