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){