Compare commits
3 commits
ba22988026
...
1c5b8ecb29
Author | SHA1 | Date | |
---|---|---|---|
1c5b8ecb29 | |||
00b41afa5a | |||
1324898483 |
4 changed files with 341 additions and 351 deletions
|
@ -3,6 +3,7 @@
|
|||
namespace App\Http\Controllers;
|
||||
|
||||
use App\GrupoDeCompra;
|
||||
use App\Producto;
|
||||
|
||||
class ComprasController
|
||||
{
|
||||
|
@ -11,11 +12,11 @@ class ComprasController
|
|||
}
|
||||
|
||||
public function descargarPedidos() {
|
||||
GrupoDeCompra::exportarTodosLosPedidosEnCSV();
|
||||
Producto::planillaTotales();
|
||||
$file = resource_path('csv/exports/total-pedidos.csv');
|
||||
return response()->download($file);
|
||||
}
|
||||
|
||||
|
||||
public function descargarNotas() {
|
||||
GrupoDeCompra::exportarProductosConNotasEnCSV();
|
||||
$file = resource_path('csv/exports/pedidos-notas.csv');
|
||||
|
|
129
app/Producto.php
129
app/Producto.php
|
@ -2,52 +2,115 @@
|
|||
|
||||
namespace App;
|
||||
|
||||
use App\Filtros\FiltroDeProducto;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Filtros\FiltroDeProducto;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
use League\Csv\CannotInsertRecord;
|
||||
use League\Csv\Writer;
|
||||
|
||||
class Producto extends Model
|
||||
{
|
||||
public $timestamps = false;
|
||||
protected $fillable = [ "nombre", "precio", "presentacion", "stock", "categoria" ];
|
||||
static $paginarPorDefecto = 10;
|
||||
public $timestamps = false;
|
||||
protected $fillable = ["nombre", "precio", "presentacion", "stock", "categoria"];
|
||||
static $paginarPorDefecto = 10;
|
||||
|
||||
public function subpedidos()
|
||||
{
|
||||
return $this->belongsToMany('App\Subpedido','productos_subpedidos')->withPivot(["cantidad", "notas"]);
|
||||
}
|
||||
public function subpedidos()
|
||||
{
|
||||
return $this->belongsToMany('App\Subpedido', 'productos_subpedidos')->withPivot(["cantidad", "notas"]);
|
||||
}
|
||||
|
||||
public function proveedor()
|
||||
{
|
||||
return $this->belongsTo('App\Proveedor');
|
||||
}
|
||||
public function proveedor()
|
||||
{
|
||||
return $this->belongsTo('App\Proveedor');
|
||||
}
|
||||
|
||||
public function pagaTransporte() {
|
||||
return !($this->bono || Str::contains($this->categoria, 'SUBSIDIADO'));
|
||||
}
|
||||
public function pagaTransporte()
|
||||
{
|
||||
return !($this->bono || Str::contains($this->categoria, 'SUBSIDIADO'));
|
||||
}
|
||||
|
||||
//Este método permite que se apliquen los filtros al hacer una request (por ejemplo, de búsqueda)
|
||||
public function scopeFiltrar($query, FiltroDeProducto $filtros)
|
||||
{
|
||||
return $filtros->aplicar($query);
|
||||
}
|
||||
//Este método permite que se apliquen los filtros al hacer una request (por ejemplo, de búsqueda)
|
||||
public function scopeFiltrar($query, FiltroDeProducto $filtros)
|
||||
{
|
||||
return $filtros->aplicar($query);
|
||||
}
|
||||
|
||||
public static function getPaginar(Request $request)
|
||||
{
|
||||
return $request->has('paginar') && intval($request->input('paginar')) ? intval($request->input('paginar')) : self::$paginarPorDefecto;
|
||||
}
|
||||
public static function getPaginar(Request $request)
|
||||
{
|
||||
return $request->has('paginar') && intval($request->input('paginar')) ? intval($request->input('paginar')) : self::$paginarPorDefecto;
|
||||
}
|
||||
|
||||
public static function productosFilaID() {
|
||||
return Producto::pluck('id', 'fila',)->all();
|
||||
}
|
||||
public static function productosFilaID()
|
||||
{
|
||||
return Producto::pluck('id', 'fila',)->all();
|
||||
}
|
||||
|
||||
public static function productosIDFila() {
|
||||
return Producto::pluck('fila', 'id',)->all();
|
||||
}
|
||||
public static function productosIDFila()
|
||||
{
|
||||
return Producto::pluck('fila', 'id',)->all();
|
||||
}
|
||||
|
||||
public static function productosIDNombre() {
|
||||
return Producto::pluck('nombre', 'id',)->all();
|
||||
}
|
||||
public static function productosIDNombre()
|
||||
{
|
||||
return Producto::pluck('nombre', 'id',)->all();
|
||||
}
|
||||
|
||||
static public function cantidadesPorBarrio()
|
||||
{
|
||||
$barrios = DB::table('grupos_de_compra')
|
||||
->where('nombre', '<>', 'PRUEBA')
|
||||
->pluck('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`");
|
||||
})->toArray();
|
||||
|
||||
return DB::table('productos')
|
||||
->where('productos.nombre','not like','%barrial%')
|
||||
->join('producto_subpedido', 'productos.id', '=', 'producto_subpedido.producto_id')
|
||||
->join('subpedidos', 'subpedidos.id', '=', 'producto_subpedido.subpedido_id')
|
||||
->select(array_merge(
|
||||
['productos.fila as fila'],
|
||||
['productos.nombre as producto'],
|
||||
$columnasBarrios
|
||||
))
|
||||
->groupBy('productos.fila','productos.id','productos.nombre')
|
||||
->orderBy('productos.fila')
|
||||
->get();
|
||||
}
|
||||
|
||||
static public function planillaTotales() {
|
||||
$headers = ['Producto'];
|
||||
$barrios = DB::table('grupos_de_compra')->pluck('nombre')->toArray();
|
||||
$headers = array_merge($headers, $barrios);
|
||||
|
||||
$cantidadesPorBarrio = Producto::cantidadesPorBarrio();
|
||||
$planilla = [];
|
||||
$ultimaFila = 1;
|
||||
|
||||
foreach ($cantidadesPorBarrio as $productoCantidades) {
|
||||
$fila = $productoCantidades->fila;
|
||||
while ($fila - $ultimaFila > 1) {
|
||||
$producto = Producto::where('fila', $ultimaFila)->first();
|
||||
$planilla[$ultimaFila] = [$producto ? $producto->nombre : ''];
|
||||
$ultimaFila++;
|
||||
}
|
||||
$planilla[$fila] = [$productoCantidades->producto];
|
||||
foreach ($barrios as $barrio) {
|
||||
$planilla[$fila][] = $productoCantidades->$barrio ?? 0;
|
||||
}
|
||||
$ultimaFila = $fila;
|
||||
}
|
||||
|
||||
// Guardar en un archivo .csv
|
||||
try {
|
||||
$writer = Writer::createFromPath(resource_path('csv/exports/total-pedidos.csv'), 'w');
|
||||
$writer->insertOne($headers);
|
||||
$writer->insertAll($planilla);
|
||||
} catch (CannotInsertRecord $e) {
|
||||
var_export($e->getRecords());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
],
|
||||
"license": "MIT",
|
||||
"require": {
|
||||
"php": "^7.3",
|
||||
"php": "^7.4",
|
||||
"fideloper/proxy": "^4.4",
|
||||
"fruitcake/laravel-cors": "^2.0",
|
||||
"guzzlehttp/guzzle": "^6.3.1|^7.0.1",
|
||||
|
|
556
composer.lock
generated
556
composer.lock
generated
File diff suppressed because it is too large
Load diff
Loading…
Add table
Reference in a new issue