206 lines
5.5 KiB
PHP
206 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App;
|
|
|
|
use App\Helpers\CsvHelper;
|
|
use App\Helpers\PdfHelper;
|
|
use App\Helpers\TransporteHelper;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use League\Csv\Exception;
|
|
use Mpdf\MpdfException;
|
|
|
|
class GrupoDeCompra extends Model
|
|
{
|
|
protected $fillable = ["nombre", "region", "devoluciones_habilitadas", "saldo"];
|
|
protected $table = 'grupos_de_compra';
|
|
|
|
public function subpedidos(): HasMany
|
|
{
|
|
return $this->hasMany(Subpedido::class);
|
|
}
|
|
|
|
public function users(): HasMany
|
|
{
|
|
return $this->hasMany(User::class);
|
|
}
|
|
|
|
public function toggleDevoluciones(): bool
|
|
{
|
|
$this->devoluciones_habilitadas = !$this->devoluciones_habilitadas;
|
|
$this->save();
|
|
return $this->devoluciones_habilitadas;
|
|
}
|
|
|
|
public function pedidosAprobados()
|
|
{
|
|
return $this->pedidosHogares()
|
|
->where('aprobado', 1);
|
|
}
|
|
|
|
public function pedidosHogares()
|
|
{
|
|
return $this->subpedidos
|
|
->where('tipo_pedido_id', '=', 1);
|
|
}
|
|
|
|
public function totalARecaudar()
|
|
{
|
|
$total = 0;
|
|
foreach ($this->pedidosAprobados() as $subpedido) {
|
|
$total = $total + $subpedido->total();
|
|
}
|
|
return $total;
|
|
}
|
|
|
|
public function totalSinDevoluciones() {
|
|
$total = 0;
|
|
foreach ($this->pedidosAprobados() as $subpedido) {
|
|
$total = $total + $subpedido->totalSinDevoluciones();
|
|
}
|
|
return $total;
|
|
}
|
|
|
|
public function totalBarrial()
|
|
{
|
|
$total = 0;
|
|
foreach ($this->pedidosAprobados() as $subpedido) {
|
|
$total = $total + $subpedido->totalBarrial();
|
|
}
|
|
return $total;
|
|
}
|
|
|
|
public function totalDevoluciones()
|
|
{
|
|
$total = 0;
|
|
foreach ($this->pedidosAprobados() as $subpedido) {
|
|
$total = $total + $subpedido->devoluciones_total;
|
|
}
|
|
return $total;
|
|
}
|
|
|
|
public function totalDePedido()
|
|
{
|
|
return $this->totalCentralesQueNoPaganTransporte()
|
|
+ $this->totalCentralesQuePaganTransporte()
|
|
+ $this->totalTransporte()
|
|
;
|
|
}
|
|
|
|
public function totalATransferir()
|
|
{
|
|
return $this->totalDePedido() - $this->saldo;
|
|
}
|
|
|
|
public function totalCentralesQueNoPaganTransporte()
|
|
{
|
|
$total = 0;
|
|
foreach ($this->pedidosAprobados() as $subpedido) {
|
|
$total = $total + $subpedido->totalCentralesQueNoPaganTransporte();
|
|
}
|
|
return $total;
|
|
}
|
|
|
|
public function totalCentralesQuePaganTransporte()
|
|
{
|
|
$total = 0;
|
|
foreach ($this->pedidosAprobados() as $subpedido) {
|
|
$total = $total + $subpedido->totalCentralesQuePaganTransporte();
|
|
}
|
|
return $total;
|
|
}
|
|
|
|
public function totalTransporte()
|
|
{
|
|
return TransporteHelper::totalTransporte($this->totalCentralesQuePaganTransporte());
|
|
}
|
|
|
|
/**
|
|
* @return int
|
|
* Calcula la cantidad de bonos de transporte del barrio
|
|
*/
|
|
public function cantidadTransporte(): int
|
|
{
|
|
return TransporteHelper::cantidadTransporte($this->totalCentralesQuePaganTransporte());
|
|
}
|
|
|
|
/**
|
|
* @throws MpdfException
|
|
*/
|
|
public function exportarPedidosAPdf()
|
|
{
|
|
$subpedidos = $this->pedidosAprobados();
|
|
$fecha = now()->format('Y-m-d');
|
|
$filepath = $this->nombre . '-' . $fecha . '.pdf';
|
|
PdfHelper::exportarPedidos($filepath, $subpedidos);
|
|
}
|
|
|
|
function pedidoParaPdf(): array
|
|
{
|
|
$productos = $this->productosPedidos(true, 'producto_id');
|
|
$pedido = [];
|
|
$pedido['productos'] = [];
|
|
|
|
$pedido['nombre'] = $this->nombre;
|
|
foreach ($productos as $producto) {
|
|
$productoParaPdf = [];
|
|
$productoParaPdf['pivot'] = [];
|
|
$productoParaPdf['nombre'] = $producto->producto_nombre;
|
|
$productoParaPdf['pivot']['cantidad'] = $producto->cantidad_pedida;
|
|
$productoParaPdf['pivot']['notas'] = false;
|
|
$productoParaPdf['bono'] = $producto->producto_es_bono;
|
|
|
|
$pedido['productos'][] = $productoParaPdf;
|
|
}
|
|
|
|
return $pedido;
|
|
}
|
|
|
|
public function generarHTML()
|
|
{
|
|
$view = view("pdfgen.pedido_tabla", ["pedido" => $this->pedidoParaPdf()]);
|
|
return $view->render();
|
|
}
|
|
|
|
/**
|
|
* @throws MpdfException
|
|
*/
|
|
public static function exportarPedidosBarrialesAPdf()
|
|
{
|
|
$barrios = GrupoDeCompra::barriosMenosPrueba()->get();
|
|
$fecha = now()->format('Y-m-d');
|
|
$filepath = 'pedidos_por_barrio-' . $fecha . '.pdf';
|
|
PdfHelper::exportarPedidos($filepath, $barrios);
|
|
}
|
|
|
|
public static function barriosMenosPrueba(): Builder
|
|
{
|
|
return self::where('nombre', '<>', 'PRUEBA')
|
|
->orderBy('region')
|
|
->orderBy('nombre');
|
|
}
|
|
|
|
public function productosPedidos($excluirBonos = false, $orderBy = 'producto_nombre'): Collection
|
|
{
|
|
$query = DB::table('pedidos_aprobados')
|
|
->where('grupo_de_compra_id', $this->id)
|
|
->where('producto_nombre','NOT LIKE','%barrial%');
|
|
|
|
if ($excluirBonos)
|
|
$query = $query->where('producto_es_bono',false);
|
|
|
|
return $query
|
|
->orderBy($orderBy)
|
|
->get()
|
|
->keyBy('producto_id');
|
|
}
|
|
|
|
public function setSaldo(float $saldo) {
|
|
$this->saldo = $saldo;
|
|
$this->save();
|
|
}
|
|
}
|