Formato + omitiendo pedidos de ollas de metodos que trabajan con pedidos
This commit is contained in:
parent
db87c38e3b
commit
cc6b0f5a6e
1 changed files with 31 additions and 20 deletions
|
@ -19,15 +19,10 @@ class Producto extends Model
|
||||||
|
|
||||||
public function subpedidos(): BelongsToMany
|
public function subpedidos(): BelongsToMany
|
||||||
{
|
{
|
||||||
return $this->belongsToMany(Subpedido::class, 'productos_subpedidos')->withPivot(["cantidad", "notas"]);
|
return $this->belongsToMany(Subpedido::class, 'productos_subpedidos')
|
||||||
|
->withPivot(["cantidad", "notas"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function noBarriales()
|
|
||||||
{
|
|
||||||
return self::where('nombre', 'not like', '%barrial%');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Este método permite que se apliquen los filtros al hacer una request (por ejemplo, de búsqueda)
|
|
||||||
public function scopeFiltrar($query, FiltroDeProducto $filtros): Builder
|
public function scopeFiltrar($query, FiltroDeProducto $filtros): Builder
|
||||||
{
|
{
|
||||||
return $filtros->aplicar($query);
|
return $filtros->aplicar($query);
|
||||||
|
@ -35,7 +30,9 @@ class Producto extends Model
|
||||||
|
|
||||||
public static function getPaginar(Request $request): int
|
public static function getPaginar(Request $request): int
|
||||||
{
|
{
|
||||||
return $request->has('paginar') && intval($request->input('paginar')) ? intval($request->input('paginar')) : self::all()->count();
|
return $request->has('paginar') && intval($request->input('paginar')) ?
|
||||||
|
intval($request->input('paginar')) :
|
||||||
|
self::all()->count();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function productosFilaID()
|
public static function productosFilaID()
|
||||||
|
@ -53,19 +50,29 @@ class Producto extends Model
|
||||||
return self::noBarriales()->pluck('nombre', 'id')->all();
|
return self::noBarriales()->pluck('nombre', 'id')->all();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function noBarriales()
|
||||||
|
{
|
||||||
|
return self::where('nombre', 'not like', '%barrial%');
|
||||||
|
}
|
||||||
|
|
||||||
static public function cantidadesPorBarrio(): Collection
|
static public function cantidadesPorBarrio(): Collection
|
||||||
{
|
{
|
||||||
$barrios = GrupoDeCompra::barriosMenosPrueba()
|
$barrios = GrupoDeCompra::barriosMenosPrueba()->pluck('id', 'nombre');
|
||||||
->pluck('id', 'nombre');
|
|
||||||
|
|
||||||
$columnasBarrios = $barrios->map(function ($id, $nombre) {
|
$columnasBarrios = $barrios->map(function ($id, $nombre) {
|
||||||
return DB::raw("SUM(CASE WHEN subpedidos.grupo_de_compra_id = $id AND subpedidos.aprobado = 1 THEN producto_subpedido.cantidad ELSE 0 END) as `$nombre`");
|
return DB::raw("
|
||||||
|
SUM(CASE WHEN subpedidos.grupo_de_compra_id = $id
|
||||||
|
AND subpedidos.aprobado = 1
|
||||||
|
AND subpedidos.tipo_pedido_id = 1
|
||||||
|
THEN producto_subpedido.cantidad
|
||||||
|
ELSE 0 END)
|
||||||
|
as `$nombre`");
|
||||||
})->toArray();
|
})->toArray();
|
||||||
|
|
||||||
return DB::table('productos')
|
return self::noBarriales()
|
||||||
->where('productos.nombre', 'not like', '%barrial%')
|
|
||||||
->leftJoin('producto_subpedido', 'productos.id', '=', 'producto_subpedido.producto_id')
|
->leftJoin('producto_subpedido', 'productos.id', '=', 'producto_subpedido.producto_id')
|
||||||
->leftJoin('subpedidos', 'subpedidos.id', '=', 'producto_subpedido.subpedido_id')
|
->leftJoin('subpedidos', 'subpedidos.id', '=', 'producto_subpedido.subpedido_id')
|
||||||
|
->where('subpedidos.tipo_pedido_id', '=', 1)
|
||||||
->select(array_merge(
|
->select(array_merge(
|
||||||
['productos.fila as fila'],
|
['productos.fila as fila'],
|
||||||
['productos.nombre as producto'],
|
['productos.nombre as producto'],
|
||||||
|
@ -82,8 +89,7 @@ class Producto extends Model
|
||||||
static public function planillaTotales()
|
static public function planillaTotales()
|
||||||
{
|
{
|
||||||
$headers = ['Producto'];
|
$headers = ['Producto'];
|
||||||
$barrios = GrupoDeCompra::barriosMenosPrueba()
|
$barrios = GrupoDeCompra::barriosMenosPrueba()->pluck('nombre')->toArray();
|
||||||
->pluck('nombre')->toArray();
|
|
||||||
$headers = array_merge($headers, $barrios);
|
$headers = array_merge($headers, $barrios);
|
||||||
|
|
||||||
$cantidadesPorBarrio = self::cantidadesPorBarrio();
|
$cantidadesPorBarrio = self::cantidadesPorBarrio();
|
||||||
|
@ -109,21 +115,23 @@ class Producto extends Model
|
||||||
$ultimaFila = $fila;
|
$ultimaFila = $fila;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($transportePorBarrio as $key => $cantidad) {
|
foreach ($transportePorBarrio as $cantidad) {
|
||||||
$planilla[$filaTransporte][] = $cantidad;
|
$planilla[$filaTransporte][] = $cantidad;
|
||||||
}
|
}
|
||||||
|
|
||||||
$fecha = now()->format('Y-m-d');
|
$fecha = now()->format('Y-m-d');
|
||||||
CsvHelper::generarCsv('csv/exports/pedidos-por-barrio- ' . $fecha . '.csv', $planilla, $headers);
|
$filePath = 'csv/exports/pedidos-por-barrio- ' . $fecha . '.csv';
|
||||||
|
CsvHelper::generarCsv($filePath, $planilla, $headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function notasPorBarrio(): Collection
|
public static function notasPorBarrio(): Collection
|
||||||
{
|
{
|
||||||
return DB::table('productos')
|
return self::noBarriales()
|
||||||
->join('producto_subpedido', 'productos.id', '=', 'producto_subpedido.producto_id')
|
->join('producto_subpedido', 'productos.id', '=', 'producto_subpedido.producto_id')
|
||||||
->join('subpedidos', 'producto_subpedido.subpedido_id', '=', 'subpedidos.id')
|
->join('subpedidos', 'producto_subpedido.subpedido_id', '=', 'subpedidos.id')
|
||||||
->join('grupos_de_compra', 'subpedidos.grupo_de_compra_id', '=', 'grupos_de_compra.id')
|
->join('grupos_de_compra', 'subpedidos.grupo_de_compra_id', '=', 'grupos_de_compra.id')
|
||||||
->where('productos.requiere_notas', 1)
|
->where('productos.requiere_notas', 1)
|
||||||
|
->where('subpedidos.tipo_pedido_id', '=', 1)
|
||||||
->select(
|
->select(
|
||||||
'productos.nombre as producto',
|
'productos.nombre as producto',
|
||||||
'grupos_de_compra.nombre as barrio',
|
'grupos_de_compra.nombre as barrio',
|
||||||
|
@ -145,13 +153,16 @@ class Producto extends Model
|
||||||
foreach ($notasPorBarrio as $producto => $notasGrupo) {
|
foreach ($notasPorBarrio as $producto => $notasGrupo) {
|
||||||
$fila = [$producto];
|
$fila = [$producto];
|
||||||
foreach ($barrios as $barrio) {
|
foreach ($barrios as $barrio) {
|
||||||
$notas = $notasGrupo->where('barrio', $barrio)->pluck('notas')->implode('; ');
|
$notas = $notasGrupo->where('barrio', $barrio)
|
||||||
|
->pluck('notas')
|
||||||
|
->implode('; ');
|
||||||
$fila[] = $notas ?: '';
|
$fila[] = $notas ?: '';
|
||||||
}
|
}
|
||||||
$planilla[] = $fila;
|
$planilla[] = $fila;
|
||||||
}
|
}
|
||||||
|
|
||||||
$fecha = now()->format('Y-m-d');
|
$fecha = now()->format('Y-m-d');
|
||||||
CsvHelper::generarCsv('csv/exports/notas-por-barrio-' . $fecha . '.csv', $planilla, $headers);
|
$filePath = 'csv/exports/notas-por-barrio-' . $fecha . '.csv';
|
||||||
|
CsvHelper::generarCsv($filePath, $planilla, $headers);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue