mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
Implementados botones de imprimir factura y exportar lineas pedido
This commit is contained in:
@ -717,6 +717,14 @@ $routes->group(
|
||||
}
|
||||
);
|
||||
|
||||
$routes->group(
|
||||
'export-lineas',
|
||||
['namespace' => 'App\Controllers\Excel'],
|
||||
function ($routes) {
|
||||
$routes->get('generar/(:num)', 'PrintLineas::generateExcel/$1', ['as' => 'lineasToExcel']);
|
||||
}
|
||||
);
|
||||
|
||||
$routes->group(
|
||||
'buscadorpresupuestos',
|
||||
['namespace' => 'App\Controllers\Presupuestos'],
|
||||
|
||||
70
ci4/app/Controllers/Excel/PrintLineas.php
Normal file
70
ci4/app/Controllers/Excel/PrintLineas.php
Normal file
@ -0,0 +1,70 @@
|
||||
<?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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -97,6 +97,32 @@ class FacturaLineaModel extends \App\Models\BaseModel {
|
||||
return $builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get resource data for creating PDFs.
|
||||
*
|
||||
* @param string $search
|
||||
*
|
||||
* @return \CodeIgniter\Database\BaseBuilder
|
||||
*/
|
||||
public function getResourceForExcel($factura_id = -1)
|
||||
{
|
||||
$builder = $this->db
|
||||
->table($this->table . " t1")
|
||||
->select(
|
||||
"t2.numero AS ref_factura,
|
||||
t3.pedido_id AS pedido_id,
|
||||
t4.referencia_cliente AS referencia_cliente,
|
||||
t1.base AS base"
|
||||
)
|
||||
->join("facturas t2", "t2.id = t1.factura_id", "left")
|
||||
->join("pedidos_linea t3", "t3.id = t1.pedido_linea_impresion_id", "left")
|
||||
->join("presupuestos t4", "t4.id = t3.presupuesto_id", "left")
|
||||
->where("t1.factura_id", $factura_id)
|
||||
->where("t1.deleted_at", null);
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
|
||||
public function addFacturaPedidoLinea($factura_id, $pedido_linea_id, $cantidad)
|
||||
{
|
||||
|
||||
@ -191,15 +191,15 @@
|
||||
<?= lang("Basic.global.edit") ?>
|
||||
</button>
|
||||
<?php endif; ?>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn-label-primary float-start me-sm-3 me-1"
|
||||
name="exportar_lineas"
|
||||
id="exportar_lineas" >
|
||||
<span class="ti-xs ti ti-file-spreadsheet me-1"></span>
|
||||
<?= lang("Facturas.exportarLineas") ?>
|
||||
</button>
|
||||
|
||||
<?= anchor(
|
||||
route_to("lineasToExcel", $facturaEntity->id),
|
||||
'<span class="ti-xs ti ti-file-spreadsheet me-1"></span>' .
|
||||
lang("Facturas.exportarLineas"),
|
||||
[
|
||||
"class" => "btn btn-label-primary float-start me-sm-3 me-1",
|
||||
]
|
||||
) ?>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
|
||||
@ -44,13 +44,21 @@
|
||||
value="<?= lang("Facturas.borrarFactura") ?>"
|
||||
/>
|
||||
<?php endif; ?>
|
||||
<input type="button"
|
||||
class="btn btn-info float-start me-sm-3 me-1"
|
||||
id="imprimirFactura"
|
||||
name="imprimirFactura"
|
||||
value="<?= lang("Facturas.imprimirFactura") ?>"
|
||||
/>
|
||||
<?= anchor(route_to("facturasList"), lang("Basic.global.back"), ["class" => "btn btn-secondary float-start"]) ?>
|
||||
<?= anchor(
|
||||
route_to("facturaToPdf", $facturaEntity->id),
|
||||
lang("Facturas.imprimirFactura"),
|
||||
[
|
||||
"class" => "btn btn-info float-start me-sm-3 me-1",
|
||||
"target" => "_blank"
|
||||
]
|
||||
) ?>
|
||||
<?= anchor(
|
||||
route_to("facturasList"),
|
||||
lang("Basic.global.back"),
|
||||
[
|
||||
"class" => "btn btn-secondary float-start"
|
||||
]
|
||||
) ?>
|
||||
</div><!-- /.card-footer -->
|
||||
</form>
|
||||
</div><!-- //.card -->
|
||||
|
||||
Reference in New Issue
Block a user