2024-03-11 19:08:31 -03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
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-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 = [
|
|
|
|
'name',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-03-19 22:34:13 -03:00
|
|
|
function crearPedido(string $nombre) : Pedido {
|
|
|
|
return $this->pedidos()->create(['name' => $name]);
|
|
|
|
}
|
|
|
|
|
2024-03-19 22:13:49 -03:00
|
|
|
function pedidosConfirmados() {
|
|
|
|
return $this->pedidos()->where('confirmed',true);
|
|
|
|
}
|
|
|
|
|
2024-03-19 22:34:13 -03:00
|
|
|
private function calcularTotalConfirmados(Closure $closure = null) : float {
|
2024-03-19 22:13:49 -03:00
|
|
|
if (!$closure)
|
|
|
|
$closure = fn($p) => $p->totalChismosa();
|
|
|
|
return $this->pedidosConfirmados()->sum($closure);
|
|
|
|
}
|
|
|
|
|
|
|
|
function totalARecaudar() : float {
|
|
|
|
return $this->calcularTotalConfirmados();
|
|
|
|
}
|
|
|
|
|
|
|
|
function totalNoBarriales() : float {
|
2024-03-19 22:37:00 -03:00
|
|
|
return $this->calcularTotalConfirmados(
|
|
|
|
fn($p) => $p->total($p->productosNoBarriales())
|
|
|
|
);
|
2024-03-19 22:13:49 -03:00
|
|
|
}
|
|
|
|
|
2024-03-20 00:13:53 -03:00
|
|
|
function totalParaTransporte() : float {
|
|
|
|
return $this->calcularTotalConfirmados(
|
|
|
|
fn($p) => $p->total($p->productosConTransporte())
|
2024-03-19 22:13:49 -03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function totalATransferir() : float {
|
2024-03-20 00:13:53 -03:00
|
|
|
return $this->totalNoBarriales() + TransporteUtils::total($this->totalParaTransporte());
|
2024-03-19 22:13:49 -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
|
|
|
|
|
|
|
|
|
|
|
private function cantidadPedida(Producto $producto) : int {
|
|
|
|
return DB::table('pedido_producto')
|
|
|
|
->join('pedidos', 'pedido_producto.order_id', '=', 'pedidos.id')
|
|
|
|
->where('pedidos.barrio_id', $this->id)
|
|
|
|
->where('pedido_producto.producto_id', $producto->id)
|
|
|
|
->where('pedidos.confirmed', true)
|
|
|
|
->sum('pedido_producto.cantidad');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function exportarPedidoACsv() {
|
|
|
|
$columnaProductos = [];
|
|
|
|
$filasVaciasAgregadas = false;
|
|
|
|
|
|
|
|
foreach (Categoria::orderBy('id')->get() as $key => $categoria) {
|
|
|
|
$columnaProductos[] = ['name' => $categoria->name, 'cantidad' => null];
|
|
|
|
|
|
|
|
if ($categoria->name == 'TRANSPORTE, BONOS Y FINANCIAMIENTO SORORO')
|
|
|
|
$columnaProductos[] = ['name' => 'Bono de Transporte', 'cantidad' => TransporteUtils::cantidad($this->totalParaTransporte)];
|
|
|
|
|
|
|
|
if ($categoria->name == 'PRODUCTOS DE GESTIÓN MENSTRUAL')
|
|
|
|
$columnaProductos[] = ['name' => '¿Cuántas copas quieren y pueden comprar en el grupo?', 'cantidad' => null];
|
|
|
|
|
|
|
|
foreach ($categoria->productos()->orderBy('id')->get() as $key => $producto) {
|
|
|
|
if ($producto->price == 0 && !$filasVaciasAgregadas) {
|
|
|
|
$columnaProductos[] = ['name' => '¿Cuántas copas quieren adquirir a través del financiamiento sororo?', 'cantidad' => null];
|
|
|
|
$filasVaciasAgregadas = true;
|
|
|
|
}
|
|
|
|
$columnaProductos[] = ['name' => $producto->name, 'cantidad' => $this->cantidadPedida($producto)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$writer = Writer::createFromPath(resource_path('csv/exports/'.$this->nombre.'.csv'), 'w');
|
|
|
|
$writer->insertAll($columnaProductos);
|
|
|
|
} catch (CannotInsertRecord $e) {
|
|
|
|
var_export($e->getRecords());
|
|
|
|
}
|
|
|
|
}
|
2024-03-11 19:08:31 -03:00
|
|
|
}
|