mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
70 lines
2.0 KiB
PHP
Executable File
70 lines
2.0 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Controllers\Excel;
|
|
|
|
use App\Controllers\BaseController;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
|
|
|
|
class PrintLineas extends BaseController
|
|
{
|
|
public function generateExcel($factura_id = null)
|
|
{
|
|
|
|
// Modelos
|
|
$lineasFacturaModel = model('App\Models\Facturas\FacturaLineaModel');
|
|
$infoLineasFactura = $lineasFacturaModel->getResourceForExcel($factura_id)->get()->getResultObject();
|
|
|
|
|
|
// Crear un nuevo Spreadsheet
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
|
|
// Especificar encabezados
|
|
$headers = [
|
|
'Factura',
|
|
'ID Pedido',
|
|
'Ref. Cliente',
|
|
'Base'
|
|
];
|
|
|
|
// Establecer los encabezados en la primera fila
|
|
$column = 'A';
|
|
foreach ($headers as $header) {
|
|
$sheet->setCellValue($column . '1', $header);
|
|
$column++;
|
|
}
|
|
|
|
// Rellenar las filas con datos
|
|
$rowNumber = 2; // Empezar en la segunda fila
|
|
foreach ($infoLineasFactura as $infoLineaFactura) {
|
|
$column = 'A';
|
|
foreach ($infoLineaFactura as $cell) {
|
|
$sheet->setCellValue($column . $rowNumber, $cell);
|
|
$column++;
|
|
}
|
|
$rowNumber++;
|
|
}
|
|
|
|
// Ajustar automáticamente el tamaño de las columnas
|
|
foreach (range('A', $column) as $col) {
|
|
$sheet->getColumnDimension($col)->setAutoSize(true);
|
|
}
|
|
|
|
// Crear un escritor para guardar el archivo
|
|
$writer = new Xlsx($spreadsheet);
|
|
|
|
// Configurar la respuesta para descarga
|
|
$fileName = 'lineas-pedido.xlsx';
|
|
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
header('Content-Disposition: attachment;filename="' . $fileName . '"');
|
|
header('Cache-Control: max-age=0');
|
|
|
|
// Escribir el archivo a la salida
|
|
$writer->save('php://output');
|
|
exit;
|
|
|
|
}
|
|
|
|
} |