forked from nathalie/pedi2
Usando CsvHelper en vez de League\Csv\Writer
This commit is contained in:
parent
d02505a70b
commit
9c3b328de0
3 changed files with 23 additions and 33 deletions
|
@ -2,12 +2,11 @@
|
||||||
|
|
||||||
namespace App;
|
namespace App;
|
||||||
|
|
||||||
|
use App\Helpers\CsvHelper;
|
||||||
use App\Helpers\TransporteHelper;
|
use App\Helpers\TransporteHelper;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use League\Csv\CannotInsertRecord;
|
|
||||||
use League\Csv\Writer;
|
|
||||||
use Mpdf\Mpdf;
|
use Mpdf\Mpdf;
|
||||||
|
|
||||||
class GrupoDeCompra extends Model
|
class GrupoDeCompra extends Model
|
||||||
|
@ -146,12 +145,8 @@ class GrupoDeCompra extends Model
|
||||||
public function exportarPedidoEnCSV()
|
public function exportarPedidoEnCSV()
|
||||||
{
|
{
|
||||||
$records = $this->generarColumnaCantidades();
|
$records = $this->generarColumnaCantidades();
|
||||||
try {
|
|
||||||
$writer = Writer::createFromPath(resource_path('csv/exports/' . $this->nombre . '.csv'), 'w');
|
CsvHelper::generarCsv('csv/exports/' . $this->nombre . '.csv', $records);
|
||||||
$writer->insertAll($records);
|
|
||||||
} catch (CannotInsertRecord $e) {
|
|
||||||
var_export($e->getRecords());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function generarColumnaCantidades()
|
public function generarColumnaCantidades()
|
||||||
|
@ -211,13 +206,7 @@ class GrupoDeCompra extends Model
|
||||||
}
|
}
|
||||||
array_splice($records, 0, 0, array($nucleos));
|
array_splice($records, 0, 0, array($nucleos));
|
||||||
|
|
||||||
// Guardar en un archivo .csv
|
CsvHelper::generarCsv('csv/exports/' . $this->nombre . '-completo.csv', $records);
|
||||||
try {
|
|
||||||
$writer = Writer::createFromPath(resource_path('csv/exports/' . $this->nombre . '-completo.csv'), 'w');
|
|
||||||
$writer->insertAll($records);
|
|
||||||
} catch (CannotInsertRecord $e) {
|
|
||||||
var_export($e->getRecords());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function agregarCantidad($pedido, $id, array $records, $fila, int $i): array
|
public function agregarCantidad($pedido, $id, array $records, $fila, int $i): array
|
||||||
|
|
|
@ -4,9 +4,11 @@ namespace App\Helpers;
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
use Iterator;
|
use Iterator;
|
||||||
|
use League\Csv\CannotInsertRecord;
|
||||||
use League\Csv\Exception;
|
use League\Csv\Exception;
|
||||||
use League\Csv\InvalidArgument;
|
use League\Csv\InvalidArgument;
|
||||||
use League\Csv\Reader;
|
use League\Csv\Reader;
|
||||||
|
use League\Csv\Writer;
|
||||||
|
|
||||||
class CsvHelper
|
class CsvHelper
|
||||||
{
|
{
|
||||||
|
@ -23,4 +25,16 @@ class CsvHelper
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function generarCsv($filePath, $contenido, $headers = null): void
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$writer = Writer::createFromPath(resource_path($filePath), 'w');
|
||||||
|
if ($headers) {
|
||||||
|
$writer->insertOne($headers);
|
||||||
|
}
|
||||||
|
$writer->insertAll($contenido);
|
||||||
|
} catch (CannotInsertRecord $e) {
|
||||||
|
var_export($e->getRecords());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,14 +3,13 @@
|
||||||
namespace App;
|
namespace App;
|
||||||
|
|
||||||
use App\Filtros\FiltroDeProducto;
|
use App\Filtros\FiltroDeProducto;
|
||||||
|
use App\Helpers\CsvHelper;
|
||||||
use App\Helpers\TransporteHelper;
|
use App\Helpers\TransporteHelper;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Log;
|
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use League\Csv\CannotInsertRecord;
|
|
||||||
use League\Csv\Writer;
|
|
||||||
|
|
||||||
class Producto extends Model
|
class Producto extends Model
|
||||||
{
|
{
|
||||||
|
@ -116,16 +115,10 @@ class Producto extends Model
|
||||||
$planilla[$filaTransporte][] = $cantidad;
|
$planilla[$filaTransporte][] = $cantidad;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
CsvHelper::generarCsv('csv/exports/pedidos-por-barrio.csv', $planilla, $headers);
|
||||||
$writer = Writer::createFromPath(resource_path('csv/exports/pedidos-por-barrio.csv'), 'w');
|
|
||||||
$writer->insertOne($headers);
|
|
||||||
$writer->insertAll($planilla);
|
|
||||||
} catch (CannotInsertRecord $e) {
|
|
||||||
var_export($e->getRecords());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function notasPorBarrio(): \Illuminate\Support\Collection
|
public static function notasPorBarrio(): Collection
|
||||||
{
|
{
|
||||||
return DB::table('productos')
|
return DB::table('productos')
|
||||||
->join('producto_subpedido', 'productos.id', '=', 'producto_subpedido.producto_id')
|
->join('producto_subpedido', 'productos.id', '=', 'producto_subpedido.producto_id')
|
||||||
|
@ -159,12 +152,6 @@ class Producto extends Model
|
||||||
$planilla[] = $fila;
|
$planilla[] = $fila;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
CsvHelper::generarCsv('csv/exports/notas-por-barrio.csv', $planilla, $headers);
|
||||||
$writer = Writer::createFromPath(resource_path('csv/exports/notas-por-barrio.csv'), 'w');
|
|
||||||
$writer->insertOne($headers);
|
|
||||||
$writer->insertAll($planilla);
|
|
||||||
} catch (CannotInsertRecord $e) {
|
|
||||||
var_export($e->getRecords());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue