From d8eefcf1b75ff44f6b6f6d8dce8f57847d81c4f5 Mon Sep 17 00:00:00 2001 From: imnavajas Date: Sat, 17 Aug 2024 18:37:13 +0200 Subject: [PATCH] Implementados botones de imprimir factura y exportar lineas pedido --- ci4/app/Config/Routes.php | 8 +++ ci4/app/Controllers/Excel/PrintLineas.php | 70 +++++++++++++++++++ ci4/app/Models/Facturas/FacturaLineaModel.php | 26 +++++++ .../form/facturas/_facturaCabeceraItems.php | 18 ++--- .../vuexy/form/facturas/viewFacturaForm.php | 22 ++++-- 5 files changed, 128 insertions(+), 16 deletions(-) create mode 100644 ci4/app/Controllers/Excel/PrintLineas.php diff --git a/ci4/app/Config/Routes.php b/ci4/app/Config/Routes.php index 41530954..1425d588 100644 --- a/ci4/app/Config/Routes.php +++ b/ci4/app/Config/Routes.php @@ -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'], diff --git a/ci4/app/Controllers/Excel/PrintLineas.php b/ci4/app/Controllers/Excel/PrintLineas.php new file mode 100644 index 00000000..729c79ff --- /dev/null +++ b/ci4/app/Controllers/Excel/PrintLineas.php @@ -0,0 +1,70 @@ +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; + + } + +} \ No newline at end of file diff --git a/ci4/app/Models/Facturas/FacturaLineaModel.php b/ci4/app/Models/Facturas/FacturaLineaModel.php index 539ee3fd..2abb52e2 100644 --- a/ci4/app/Models/Facturas/FacturaLineaModel.php +++ b/ci4/app/Models/Facturas/FacturaLineaModel.php @@ -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) { diff --git a/ci4/app/Views/themes/vuexy/form/facturas/_facturaCabeceraItems.php b/ci4/app/Views/themes/vuexy/form/facturas/_facturaCabeceraItems.php index 4bba5e7f..06227a6f 100644 --- a/ci4/app/Views/themes/vuexy/form/facturas/_facturaCabeceraItems.php +++ b/ci4/app/Views/themes/vuexy/form/facturas/_facturaCabeceraItems.php @@ -191,15 +191,15 @@ - - + + id), + '' . + lang("Facturas.exportarLineas"), + [ + "class" => "btn btn-label-primary float-start me-sm-3 me-1", + ] + ) ?>