diff --git a/ci4/app/Config/Routes.php b/ci4/app/Config/Routes.php index 34e261bc..4888631d 100755 --- a/ci4/app/Config/Routes.php +++ b/ci4/app/Config/Routes.php @@ -634,6 +634,7 @@ $routes->resource('pedidos', ['namespace' => 'App\Controllers\Pedidos', 'control $routes->group('albaranes', ['namespace' => 'App\Controllers\Pedidos'], function ($routes) { $routes->post('add', 'Albaran::add', ['as' => 'crearAlbaranesPedido']); + $routes->post('update/(:any)', 'Albaran::update/$1', ['as' => 'actualizarAlbaran']); }); $routes->resource('albaranes', ['namespace' => 'App\Controllers\Pedidos', 'controller' => 'Albaran', 'except' => 'show,new,create,update']); diff --git a/ci4/app/Controllers/Pedidos/Albaran.php b/ci4/app/Controllers/Pedidos/Albaran.php index fdaa85a4..af89d98f 100644 --- a/ci4/app/Controllers/Pedidos/Albaran.php +++ b/ci4/app/Controllers/Pedidos/Albaran.php @@ -55,8 +55,77 @@ class Albaran extends \App\Controllers\BaseResourceController else { return $this->failUnauthorized('Invalid request', 403); } + } + public function update($id = null){ + if ($this->request->isAJAX()) { + $newTokenHash = csrf_hash(); + $csrfTokenName = csrf_token(); + + if ($id == null) : + return $this->redirect2listView(); + endif; + $id = filter_var($id, FILTER_SANITIZE_URL); + $albaranEntity = $this->model->find($id); + + if ($albaranEntity == false) : + $message = lang('Basic.global.notFoundWithIdErr', [mb_strtolower(lang('Pedidos.albaran')), $id]); + return $this->redirect2listView('sweet-error', $message); + endif; + + if ($this->request->getPost()) : + + $nullIfEmpty = true; // !(phpversion() >= '8.1'); + + $postData = $this->request->getPost(); + + $sanitizedData = $this->sanitized($postData, $nullIfEmpty); + + // JJO + $sanitizedData['user_updated_id'] = auth()->user()->id; + + $noException = true; + if ($successfulResult = $this->canValidate()) : // if ($successfulResult = $this->validate($this->formValidationRules) ) : + + if ($this->canValidate()) : + try { + $successfulResult = $this->model->skipValidation(true)->update($id, $sanitizedData); + } catch (\Exception $e) { + $noException = false; + $this->dealWithException($e); + } + else: + $this->viewData['warningMessage'] = lang('Basic.global.formErr1', [mb_strtolower(lang('Pedidos.albaran'))]); + $this->session->setFlashdata('formErrors', $this->model->errors()); + + endif; + + $albaranEntity->fill($sanitizedData); + + endif; + if ($noException && $successfulResult) : + $id = $albaranEntity->id ?? $id; + $message = lang('Basic.global.updateSuccess', [lang('Basic.global.record')]) . '.'; + + $data = [ + 'error' => 0, + $csrfTokenName => $newTokenHash + ]; + return $this->respond($data); + + endif; // $noException && $successfulResult + endif; // ($requestMethod === 'post') + + $data = [ + 'error' => 1, + $csrfTokenName => $newTokenHash + ]; + return $this->respond($data); + } + else { + return $this->failUnauthorized('Invalid request', 403); + } } } diff --git a/ci4/app/Entities/Pedidos/AlbaranEntity.php b/ci4/app/Entities/Pedidos/AlbaranEntity.php index c07449f7..bdff8334 100644 --- a/ci4/app/Entities/Pedidos/AlbaranEntity.php +++ b/ci4/app/Entities/Pedidos/AlbaranEntity.php @@ -13,7 +13,7 @@ class AlbaranEntity extends \CodeIgniter\Entity\Entity 'cliente_id' => null, 'serie_id' => null, 'numero_albaran' => null, - 'mostar_precios' => null, + 'mostrar_precios' => null, 'total' => null, 'direccion_albaran' => null, 'att_albaran' => null, @@ -34,7 +34,7 @@ class AlbaranEntity extends \CodeIgniter\Entity\Entity 'cliente_id' => '?integer', 'serie_id' => '?integer', 'numero_albaran' => '?string', - 'mostar_precios' => '?boolean', + 'mostrar_precios' => '?boolean', 'total' => 'float', 'direccion_albaran' => '?string', 'att_albaran' => '?string', diff --git a/ci4/app/Language/en/Pedidos.php b/ci4/app/Language/en/Pedidos.php index 3a7518f3..4ca46f10 100644 --- a/ci4/app/Language/en/Pedidos.php +++ b/ci4/app/Language/en/Pedidos.php @@ -67,6 +67,7 @@ return [ 'imprimirAlbaran' => 'Print', 'nuevaLinea' => 'New line', 'addIva' => "Add VAT", + 'mostrarPrecios' => 'Show prices', 'facturas' => 'Invoices', diff --git a/ci4/app/Language/es/Pedidos.php b/ci4/app/Language/es/Pedidos.php index c37f9dc4..906321ee 100644 --- a/ci4/app/Language/es/Pedidos.php +++ b/ci4/app/Language/es/Pedidos.php @@ -66,6 +66,7 @@ return [ 'imprimirAlbaran' => 'Imprimir', 'nuevaLinea' => 'Nueva línea', 'addIva' => "Añadir IVA", + 'mostrarPrecios' => 'Mostrar precios', 'facturas' => 'Facturas', diff --git a/ci4/app/Models/Pedidos/AlbaranModel.php b/ci4/app/Models/Pedidos/AlbaranModel.php index 87923a9b..63415da1 100644 --- a/ci4/app/Models/Pedidos/AlbaranModel.php +++ b/ci4/app/Models/Pedidos/AlbaranModel.php @@ -22,7 +22,7 @@ class AlbaranModel extends \App\Models\BaseModel 'cliente_id', 'serie_id', 'numero_albaran', - 'mostar_precios', + 'mostrar_precios', 'total', 'direccion_albaran', 'att_albaran', @@ -82,7 +82,7 @@ class AlbaranModel extends \App\Models\BaseModel 'cliente_id' => $presupuesto->cliente_id, 'serie_id' => 11, // Serie de albaranes 'numero_albaran' => $numero_albaran, - 'mostar_precios' => 0, + 'mostrar_precios' => 0, 'total' => $albaran_linea['total'], 'direccion_albaran' => $envio->direccion, 'att_albaran' => $envio->att, diff --git a/ci4/app/Views/themes/vuexy/form/pedidos/_albaranesItems.php b/ci4/app/Views/themes/vuexy/form/pedidos/_albaranesItems.php index a8fba5ed..caf1a195 100644 --- a/ci4/app/Views/themes/vuexy/form/pedidos/_albaranesItems.php +++ b/ci4/app/Views/themes/vuexy/form/pedidos/_albaranesItems.php @@ -147,7 +147,7 @@ function generarAlbaran(item){
- +
@@ -155,13 +155,13 @@ function generarAlbaran(item){
- +
`; const table = $('', - { id: 'tablaAlbaran' + item.albaran_linea.id, width:'100%', class: 'table table-responsive table-striped table-hover' }) + { id: 'tablaAlbaran' + item.albaran.id, width:'100%', class: 'table table-responsive table-striped table-hover' }) .css({ 'width': '100%', }).append( @@ -187,25 +187,36 @@ function generarAlbaran(item){ ), ); + let isChecked = item.albaran.mostrar_precios == 1 ? 'checked' : ''; const botones_albaran = - ` -
-
- - + ` +
+
+
+ +
+
+ +
+
+
+ + +
-
- - -
-
- - -
-
- - +
+ + +
+
+ + +
+
+ + +
`; @@ -226,7 +237,7 @@ function generarAlbaran(item){ - const datatableAlbaran = new DataTable('#tablaAlbaran' + item.albaran_linea.id,{ + const datatableAlbaran = new DataTable('#tablaAlbaran' + item.albaran.id,{ scrollX: true, searching: false, paging: false, @@ -402,14 +413,18 @@ function generarAlbaran(item){ { targets: [1], orderable: false }, { targets: [2, 3, 4, 5, 6, 7, 8, 9], className: 'dt-center' } ], - }); + initComplete: function(settings){ + var numColumns = this.api().columns().count(); + if(item.albaran.mostrar_precios == 0){ + this.api().column(numColumns - 1).visible(false); + this.api().column(numColumns - 2).visible(false); + } else { + this.api().column(numColumns - 1).visible(true); + this.api().column(numColumns - 2).visible(true); + } - - /*var nuevaFila = [ - item.algunDato1, // Reemplaza estos valores con los datos reales de tu objeto - item.algunDato2, - // Añade tantos elementos como columnas tenga tu tabla - ];*/ + } + }); // Añadir la nueva fila a la tabla datatableAlbaran.row.add(item.albaran_linea).draw(); @@ -417,4 +432,66 @@ function generarAlbaran(item){ } +$(document).on('change', '.cambios-albaran', function(){ + var elementId = $(this).attr('id'); + + data = { + : v, + }; + data[elementId.split('_')[0] + '_albaran'] = $(this).val(); + + var albaran_id = $(this).attr('albaran_id'); + var url = ''; + url = url.replace(':id', albaran_id ); + + $.ajax({ + url: url, + type: 'POST', + data: data, + success: function(response){ + + if('error' in response){ + + } + } + }); + +}); + +$(document).on('change', '.mostrar-precios', function(){ + + var checked = $(this).prop('checked'); + + var albaran_id = $(this).attr('albaran_id'); + var table = $('#tablaAlbaran' + albaran_id).DataTable(); + + var url = ''; + url = url.replace(':id', albaran_id ); + + $.ajax({ + url: url, + type: 'POST', + data: { + mostrar_precios: checked?1:0, + : v, + }, + success: function(response){ + + if('error' in response){ + if(response.error == 0){ + if(checked){ + table.column(9).visible(true); + table.column(8).visible(true); + } else { + table.column(9).visible(false); + table.column(8).visible(false); + } + } + } + } + }); +}); + + + endSection() ?> \ No newline at end of file