276 lines
		
	
	
	
		
			8.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			276 lines
		
	
	
	
		
			8.4 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;
 | |
| 
 | |
| class GrupoDeCompra extends Model
 | |
| {
 | |
|     protected $fillable = ["nombre", "region", "devoluciones_habilitadas"];
 | |
|     protected $table = 'grupos_de_compra';
 | |
| 
 | |
|     public function subpedidos(): HasMany
 | |
|     {
 | |
|         return $this->hasMany(Subpedido::class);
 | |
|     }
 | |
| 
 | |
|     public function toggleDevoluciones(): bool
 | |
|     {
 | |
|         $this->devoluciones_habilitadas = !$this->devoluciones_habilitadas;
 | |
|         $this->save();
 | |
|         return $this->devoluciones_habilitadas;
 | |
|     }
 | |
| 
 | |
|     public function pedidosAprobados()
 | |
|     {
 | |
|         return $this->subpedidos->where('aprobado', 1);
 | |
|     }
 | |
| 
 | |
|     public function totalARecaudar()
 | |
|     {
 | |
|         $total = 0;
 | |
|         foreach ($this->pedidosAprobados() as $subpedido) {
 | |
|             $total = $total + $subpedido->total();
 | |
|         }
 | |
|         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 totalATransferir()
 | |
|     {
 | |
|         return $this->totalCentralesQueNoPaganTransporte()
 | |
|             + $this->totalCentralesQuePaganTransporte()
 | |
|             + $this->totalTransporte();
 | |
|     }
 | |
| 
 | |
|     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());
 | |
|     }
 | |
| 
 | |
|     public function exportarPedidosAPdf()
 | |
|     {
 | |
|         $subpedidos = $this->pedidosAprobados();
 | |
|         PdfHelper::exportarPedidos($this->nombre . '.pdf', $subpedidos);
 | |
|     }
 | |
| 
 | |
|     function pedidoParaPdf(): array
 | |
|     {
 | |
|         $productos = $this->productosPedidos(true, 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();
 | |
|     }
 | |
| 
 | |
|     public static function exportarPedidosBarrialesAPdf()
 | |
|     {
 | |
|         $barrios = GrupoDeCompra::barriosMenosPrueba()->get();
 | |
|         PdfHelper::exportarPedidos('pedidos_por_barrio.pdf', $barrios);
 | |
|     }
 | |
| 
 | |
|     static function filaVacia(string $product, int $columns): array
 | |
|     {
 | |
|         $fila = [$product];
 | |
|         for ($i = 1; $i <= $columns; $i++) {
 | |
|             $fila[$i] = "0";
 | |
|         }
 | |
|         return $fila;
 | |
|     }
 | |
| 
 | |
|     //Asume que los productos están gruadados en orden de fila
 | |
|     public static function obtenerTemplateDeFilasVacias(int $columns): array
 | |
|     {
 | |
|         $productosFilaID = Producto::productosFilaID();
 | |
|         $productosIDNombre = Producto::productosIDNombre();
 | |
|         $num_fila = 1;
 | |
|         $template = [];
 | |
|         foreach ($productosFilaID as $fila => $id) {
 | |
|             for ($i = $num_fila; $i < $fila; $i++) {
 | |
|                 $template[$i] = GrupoDeCompra::filaVacia("", $columns);
 | |
|             }
 | |
|             $template[$fila] = GrupoDeCompra::filaVacia($productosIDNombre[$id], $columns);
 | |
|             $num_fila = $fila + 1;
 | |
|         }
 | |
|         $template[TransporteHelper::filaTransporte()] = GrupoDeCompra::filaVacia("Bonos de transporte", $columns);
 | |
|         return $template;
 | |
|     }
 | |
| 
 | |
|     public function exportarPedidoEnCSV()
 | |
|     {
 | |
|         $records = $this->generarColumnaCantidades();
 | |
| 
 | |
|         CsvHelper::generarCsv('csv/exports/' . $this->nombre . '.csv', $records);
 | |
|     }
 | |
| 
 | |
|     public function generarColumnaCantidades(): array
 | |
|     {
 | |
|         $productos_en_pedido = $this->productosPedidos();
 | |
| 
 | |
|         //si no hay pedidos aprobados, salir
 | |
|         if ($productos_en_pedido->count() == 0) {
 | |
|             Log::debug("El grupo de compra " . $this->nombre . " no tiene pedidos aprobados.");
 | |
|             return [];
 | |
|         }
 | |
| 
 | |
|         $records = $this->obtenerTemplateDeFilasVacias(1);
 | |
|         $productos_id_fila = Producto::productosIDFila();
 | |
|         foreach ($productos_en_pedido as $id => $producto_pedido) {
 | |
|             $fila = $productos_id_fila[$id];
 | |
|             $records[$fila][1] = $producto_pedido->cantidad_pedida;
 | |
|         }
 | |
| 
 | |
|         $records[TransporteHelper::filaTransporte()][1] = $this->cantidadTransporte();
 | |
| 
 | |
|         return $records;
 | |
|     }
 | |
| 
 | |
|     public function exportarPedidoConNucleosEnCSV()
 | |
|     {
 | |
|         $productos_en_pedido = $this->productosPedidos();
 | |
| 
 | |
|         // si no hay pedidos aprobados, salir
 | |
|         if ($productos_en_pedido->count() == 0) {
 | |
|             Log::debug("El grupo de compra " . $this->nombre . " no tiene pedidos aprobados.");
 | |
|             return;
 | |
|         }
 | |
| 
 | |
|         $pedidos = $this->pedidosAprobados();
 | |
|         // Generar tabla vacía con una columna por núcleo
 | |
|         $records = $this->obtenerTemplateDeFilasVacias($pedidos->count());
 | |
|         $productos_id_fila = Producto::productosIDFila();
 | |
| 
 | |
|         foreach ($productos_en_pedido as $id => $producto_pedido) {
 | |
|             $fila = $productos_id_fila[$id];
 | |
|             $i = 1;
 | |
|             // Poner cantidad de cada producto para cada núcleo
 | |
|             foreach ($pedidos as $pedido) {
 | |
|                 list($records, $i, $_) = $this->agregarCantidad($pedido, $id, $records, $fila, $i);
 | |
|             }
 | |
|         }
 | |
|         // Insertar lista de núcleos en la primera fila
 | |
|         $nucleos = [""];
 | |
|         $i = 1;
 | |
|         foreach ($pedidos as $pedido) {
 | |
|             $nucleos[$i] = $pedido->nombre;
 | |
|             $i++;
 | |
|         }
 | |
|         array_splice($records, 0, 0, array($nucleos));
 | |
| 
 | |
|         CsvHelper::generarCsv('csv/exports/' . $this->nombre . '-completo.csv', $records);
 | |
|     }
 | |
| 
 | |
|     public function agregarCantidad($pedido, $id, array $records, $fila, int $i): array
 | |
|     {
 | |
|         $producto = $pedido->productos()->find($id);
 | |
|         $cantidad = $producto == NULL ? 0 : $producto->pivot->cantidad;
 | |
|         $records[$fila][$i] = $cantidad;
 | |
|         $i++;
 | |
|         return array($records, $i, $cantidad);
 | |
|     }
 | |
| 
 | |
|     public static function barriosMenosPrueba(): Builder
 | |
|     {
 | |
|         return self::where('nombre', '<>', 'PRUEBA')
 | |
|             ->orderBy('region')
 | |
|             ->orderBy('nombre');
 | |
|     }
 | |
| 
 | |
|     public static function transportePorBarrio(): array
 | |
|     {
 | |
|         $result = [];
 | |
|         $barrios = GrupoDeCompra::barriosMenosPrueba()->get();
 | |
| 
 | |
|         foreach ($barrios as $barrio) {
 | |
|             $result[] = $barrio->cantidadTransporte();
 | |
|         }
 | |
| 
 | |
|         return $result;
 | |
|     }
 | |
| 
 | |
|     public function productosPedidos($excluirBarriales = false, $excluirBonos = false, $orderBy = 'producto_nombre'): Collection
 | |
|     {
 | |
|         $query = DB::table('pedidos_aprobados')
 | |
|             ->where('grupo_de_compra_id', $this->id);
 | |
| 
 | |
|         if ($excluirBarriales)
 | |
|             $query = $query->where('producto_nombre','NOT LIKE','%barrial%');
 | |
|         if ($excluirBonos)
 | |
|             $query = $query->where('producto_es_bono',false);
 | |
| 
 | |
|         return $query
 | |
|             ->orderBy($orderBy)
 | |
|             ->get()
 | |
|             ->keyBy('producto_id');
 | |
|     }
 | |
| }
 |