diff --git a/ci4/app/Config/Routes.php b/ci4/app/Config/Routes.php index 8a92468c..713e1915 100644 --- a/ci4/app/Config/Routes.php +++ b/ci4/app/Config/Routes.php @@ -670,7 +670,7 @@ $routes->group('facturas', ['namespace' => 'App\Controllers\Facturacion'], funct $routes->post('updateCabecera/(:any)', 'Facturas::updateCabecera/$1', ['as' => 'updateCabecera']); $routes->post('datatablePagos/(:any)', 'FacturasPagos::datatable/$1', ['as' => 'dataTableOfPagosFacturas']); $routes->post('editorPagos', 'FacturasPagos::datatable_editor', ['as' => 'editorOfPagosFacturas']); - + $routes->post('datatablePedidos', 'Facturas::datatablePedidos', ['as' => 'dataTableOfFacturasPedido']); }); diff --git a/ci4/app/Controllers/Facturacion/Facturas.php b/ci4/app/Controllers/Facturacion/Facturas.php index e7fd5709..abf1db51 100755 --- a/ci4/app/Controllers/Facturacion/Facturas.php +++ b/ci4/app/Controllers/Facturacion/Facturas.php @@ -218,6 +218,38 @@ class Facturas extends \App\Controllers\BaseResourceController } } + + public function datatablePedidos(){ + + if ($this->request->isAJAX()) { + + $reqData = $this->request->getPost(); + if (!isset($reqData['draw']) || !isset($reqData['columns']) ) { + $errstr = 'No data available in response to this specific request.'; + $response = $this->respond(Collection::datatable( [], 0, 0, $errstr ), 400, $errstr); + return $response; + } + $start = $reqData['start'] ?? 0; + $length = $reqData['length'] ?? 5; + $search = $reqData['search']['value']; + $requestedOrder = $reqData['order']['0']['column'] ?? 0; + $order = FacturaModel::SORTABLE_PEDIDOS[$requestedOrder >= 0 ? $requestedOrder : 0]; + $dir = $reqData['order']['0']['dir'] ?? 'asc'; + + $pedido_id = $reqData['pedido_id'] ?? 0; + + $resourceData = $this->model->getResourcePedidos($pedido_id)->orderBy($order, $dir)->limit($length, $start)->get()->getResultObject(); + + return $this->respond(Collection::datatable( + $resourceData, + $this->model->getResourcePedidos($pedido_id)->countAllResults(), + $this->model->getResourcePedidos($pedido_id)->countAllResults() + )); + } else { + return $this->failUnauthorized('Invalid request', 403); + } + } + public function update($id = null){ if ($this->request->isAJAX()) { diff --git a/ci4/app/Models/Facturas/FacturaModel.php b/ci4/app/Models/Facturas/FacturaModel.php index e492a8a4..ccdf7f43 100644 --- a/ci4/app/Models/Facturas/FacturaModel.php +++ b/ci4/app/Models/Facturas/FacturaModel.php @@ -23,6 +23,14 @@ class FacturaModel extends \App\Models\BaseModel { 11 => "DAFEDIFF(days, NOW(), t3.fecha_vencimiento_at)", ]; + const SORTABLE_PEDIDOS = [ + 1 => "t1.numero", + 2 => "t2.nombre", + 3 => "t1.estado", + 4 => "t1.fecha_factura_at", + 5 => "t1.total", + ]; + // Lista de columnas basada en los campos de la tabla, para asignación masiva protected $allowedFields = [ 'pedido_id', @@ -93,6 +101,30 @@ class FacturaModel extends \App\Models\BaseModel { ->groupEnd(); } + + public function getResourcePedidos($pedido_id) + { + $builder = $this->db + ->table($this->table . " t1") + ->select( + "t1.id AS id, t1.numero AS numero, t2.nombre AS serie, t1.estado AS estado, + DATE_FORMAT(t1.fecha_factura_at, '%d/%m/%Y') AS fecha_factura_at, t1.total AS total" + ); + + $builder->join("series t2", "t2.id = t1.serie_id", "left"); + $builder->join("facturas_lineas t3", "t1.id = t3.factura_id", "left"); + $builder->join("facturas_pedidos_lineas t4", "t1.id = t4.factura_id", "left"); + $builder->join("pedidos_linea t5", "t4.pedido_linea_id = t5.id", "left"); + $builder->join("pedidos t6", "t5.pedido_id = t6.id", "left"); + + $builder->where("t1.deleted_at IS NULL"); + $builder->where("t6.id", $pedido_id); + + $builder->groupBy("t1.id"); // Agrupa por id de la factura + + return $builder; + } + public function getCantidadLineaPedidoFacturada($linea_pedido_id) { $builder = $this->db diff --git a/ci4/app/Views/themes/vuexy/form/pedidos/_facturasItems.php b/ci4/app/Views/themes/vuexy/form/pedidos/_facturasItems.php index c3871138..03c61bc5 100644 --- a/ci4/app/Views/themes/vuexy/form/pedidos/_facturasItems.php +++ b/ci4/app/Views/themes/vuexy/form/pedidos/_facturasItems.php @@ -10,6 +10,21 @@
+ + + + + + + + + + + + + + +
ID
@@ -19,6 +34,77 @@ section('additionalInlineJs') ?> +const actionBtns_facturas = function(data) { + return ` + +
+ +
+ `; +}; +tablaFacturasPedido = $('#tableOfFacturas').DataTable({ + processing: true, + serverSide: true, + autoWidth: true, + responsive: true, + scrollX: true, + "dom": 't', + stateSave: true, + order: [[1, 'asc']], + language: { + url: "/themes/vuexy/vendor/libs/datatables-sk/plugins/i18n/es-ES.json" + }, + ajax : $.fn.dataTable.pipeline( { + url: '', + method: 'POST', + data: function ( d ) { + d.pedido_id = id ?>; + }, + headers: {'X-Requested-With': 'XMLHttpRequest'}, + async: true, + }), + columnDefs: [ + { + orderable: false, + searchable: false, + targets: [0] + } + ], + columns : [ + { 'data': actionBtns_facturas }, + { 'data': 'id' }, + { 'data': 'numero' }, + { 'data': 'serie' }, + { 'data': 'estado', + render: function(data, type, row, meta) { + switch(data){ + case "borrador": + return ''; + break; + + case "validada": + return ''; + break; + + default: + return '--'; // Debug + break; + + } + } + }, + { 'data': 'fecha_factura_at' }, + { 'data': 'total' }, + ] +}); + +$(document).on('click', '.btn-view-factura', function(e) { + var pedido_id = $(this).data('id'); + var url = ''; + url = url.replace(':id', pedido_id ); + + window.open(url, '_blank'); +}); endSection() ?> \ No newline at end of file