106 lines
3.3 KiB
PHP
106 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App;
|
|
|
|
use App\Filtros\FiltroDeProducto;
|
|
use App\Helpers\CsvHelper;
|
|
use App\Helpers\TransporteHelper;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
use League\Csv\CannotInsertRecord;
|
|
use League\Csv\Exception;
|
|
use League\Csv\InvalidArgument;
|
|
|
|
class Producto extends Model
|
|
{
|
|
protected $fillable = ["nombre", "precio", "categoria", "bono", "es_solidario", "requiere_notas"];
|
|
|
|
public function subpedidos(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Subpedido::class, 'productos_subpedidos')
|
|
->withPivot(["cantidad", "notas"]);
|
|
}
|
|
|
|
public function scopeFiltrar($query, FiltroDeProducto $filtros): Builder
|
|
{
|
|
return $filtros->aplicar($query);
|
|
}
|
|
|
|
public static function getPaginar(Request $request): int
|
|
{
|
|
return $request->has('paginar') && intval($request->input('paginar')) ?
|
|
intval($request->input('paginar')) :
|
|
self::all()->count();
|
|
}
|
|
|
|
public static function productosFilaID()
|
|
{
|
|
return self::noBarriales()->pluck('id', 'fila')->all();
|
|
}
|
|
|
|
public static function productosIDFila()
|
|
{
|
|
return self::noBarriales()->pluck('fila', 'id')->all();
|
|
}
|
|
|
|
public static function productosIDNombre()
|
|
{
|
|
return self::noBarriales()->pluck('nombre', 'id')->all();
|
|
}
|
|
|
|
public static function noBarriales()
|
|
{
|
|
return self::where('nombre', 'not like', '%barrial%');
|
|
}
|
|
|
|
public static function notasPorBarrio(): Collection
|
|
{
|
|
return DB::table('productos')
|
|
->where('productos.nombre', 'not like', '%barrial%')
|
|
->join('producto_subpedido', 'productos.id', '=', 'producto_subpedido.producto_id')
|
|
->join('subpedidos', 'producto_subpedido.subpedido_id', '=', 'subpedidos.id')
|
|
->join('grupos_de_compra', 'subpedidos.grupo_de_compra_id', '=', 'grupos_de_compra.id')
|
|
->where('productos.requiere_notas', 1)
|
|
->where('subpedidos.tipo_pedido_id', '=', 1)
|
|
->select(
|
|
'productos.nombre as producto',
|
|
'grupos_de_compra.nombre as barrio',
|
|
'producto_subpedido.notas'
|
|
)
|
|
->get()
|
|
->groupBy('producto');
|
|
}
|
|
|
|
/**
|
|
* @throws InvalidArgument
|
|
* @throws CannotInsertRecord
|
|
*/
|
|
static public function planillaNotas() {
|
|
$headers = ['Producto'];
|
|
$barrios = GrupoDeCompra::barriosMenosPrueba()
|
|
->pluck('nombre')->toArray();
|
|
$headers = array_merge($headers, $barrios);
|
|
|
|
$notasPorBarrio = self::notasPorBarrio();
|
|
$planilla = [];
|
|
|
|
foreach ($notasPorBarrio as $producto => $notasGrupo) {
|
|
$fila = [$producto];
|
|
foreach ($barrios as $barrio) {
|
|
$notas = $notasGrupo->where('barrio', $barrio)
|
|
->pluck('notas')
|
|
->implode('; ');
|
|
$fila[] = $notas ?: '';
|
|
}
|
|
$planilla[] = $fila;
|
|
}
|
|
|
|
$fecha = now()->format('Y-m-d');
|
|
$filePath = 'csv/exports/notas-por-barrio-' . $fecha . '.csv';
|
|
CsvHelper::generarCsv($filePath, $planilla, $headers);
|
|
}
|
|
}
|