From d2a524e5dfda5538d7e0961761e97bd499c99a50 Mon Sep 17 00:00:00 2001 From: Jaime Jimenez Date: Tue, 5 Sep 2023 21:26:32 +0200 Subject: [PATCH] =?UTF-8?q?a=C3=B1adida=20la=20funcionalidad=20de=20borrar?= =?UTF-8?q?=20en=20cascada=20las=20tarifas=20envio.=20Corregido=20error=20?= =?UTF-8?q?en=20editor=20(campo=20nombre)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ci4/app/Controllers/Tarifas/Tarifasenvios.php | 44 +++++++++++++++++++ .../Tarifas/Tarifasenvioszonas.php | 44 +++---------------- .../Models/Tarifas/TarifaEnvioPrecioModel.php | 4 +- .../Models/Tarifas/TarifaEnvioZonaModel.php | 12 +++++ 4 files changed, 65 insertions(+), 39 deletions(-) diff --git a/ci4/app/Controllers/Tarifas/Tarifasenvios.php b/ci4/app/Controllers/Tarifas/Tarifasenvios.php index 0eb098b9..f5ce1e26 100644 --- a/ci4/app/Controllers/Tarifas/Tarifasenvios.php +++ b/ci4/app/Controllers/Tarifas/Tarifasenvios.php @@ -12,6 +12,8 @@ use App\Models\Compras\ProveedorModel; use App\Models\Compras\ProveedorTipoModel; use App\Models\Tarifas\TarifaEnvioModel; +use App\Models\Tarifas\TarifaEnvioPrecioModel; +use App\Models\Tarifas\TarifaEnvioZonaModel; class Tarifasenvios extends \App\Controllers\GoBaseResourceController { @@ -226,6 +228,48 @@ class Tarifasenvios extends \App\Controllers\GoBaseResourceController { } // end function edit(...) + // JJO + public function delete($id = null) + { + if (!empty(static::$pluralObjectNameCc) && !empty(static::$singularObjectNameCc)) { + $objName = mb_strtolower(lang(ucfirst(static::$pluralObjectNameCc).'.'.static::$singularObjectNameCc)); + } else { + $objName = lang('Basic.global.record'); + } + + if (!$this->soft_delete){ + + if (!$this->model->delete($id)) { + return $this->failNotFound(lang('Basic.global.deleteError', [$objName])); + } + } + else{ + $datetime = (new \CodeIgniter\I18n\Time("now")); + + + $zonaModel = new TarifaEnvioZonaModel(); + $zonaResult = $zonaModel->removeAllZonasLineas($id, $datetime, $this->delete_flag); + + $precioModel = new TarifaEnvioPrecioModel(); + $precioResult = $precioModel->removeAllPrecioLineas($id, $datetime, $this->delete_flag); + + $rawResult = $this->model->where('id',$id) + ->set(['deleted_at' => $datetime->format('Y-m-d H:i:s'), + 'is_deleted' => $this->delete_flag]) + ->update(); + + if (!$rawResult && !$precioResult && !$zonaResult) { + return $this->failNotFound(lang('Basic.global.deleteError', [$objName])); + } + + } + + // $message = lang('Basic.global.deleteSuccess', [$objName]); IMN commented + $message = lang('Basic.global.deleteSuccess', [lang('Basic.global.record')]); + $response = $this->respondDeleted(['id' => $id, 'msg' => $message]); + return $response; + } + public function datatable() { if ($this->request->isAJAX()) { diff --git a/ci4/app/Controllers/Tarifas/Tarifasenvioszonas.php b/ci4/app/Controllers/Tarifas/Tarifasenvioszonas.php index 1c3271c2..39ebd1cb 100644 --- a/ci4/app/Controllers/Tarifas/Tarifasenvioszonas.php +++ b/ci4/app/Controllers/Tarifas/Tarifasenvioszonas.php @@ -209,43 +209,6 @@ class Tarifasenvioszonas extends \App\Controllers\GoBaseResourceController { } // end function edit(...) - public function delete($id = null) - { - if (!empty(static::$pluralObjectNameCc) && !empty(static::$singularObjectNameCc)) { - $objName = mb_strtolower(lang(ucfirst(static::$pluralObjectNameCc).'.'.static::$singularObjectNameCc)); - } else { - $objName = lang('Basic.global.record'); - } - - if (!$this->soft_delete){ - - if (!$this->model->delete($id)) { - return $this->failNotFound(lang('Basic.global.deleteError', [$objName])); - } - } - else{ - $datetime = (new \CodeIgniter\I18n\Time("now")); - - $precioModel = new TarifaEnvioPrecioModel(); - $precioResult = $precioModel->removeAllPrecioLineas($id, $datetime, $this->delete_flag); - - $rawResult = $this->model->where('id',$id) - ->set(['deleted_at' => $datetime->format('Y-m-d H:i:s'), - 'is_deleted' => $this->delete_flag]) - ->update(); - - if (!$rawResult && !$precioResult) { - return $this->failNotFound(lang('Basic.global.deleteError', [$objName])); - } - - } - - // $message = lang('Basic.global.deleteSuccess', [$objName]); IMN commented - $message = lang('Basic.global.deleteSuccess', [lang('Basic.global.record')]); - $response = $this->respondDeleted(['id' => $id, 'msg' => $message]); - return $response; - } - public function datatable_editor() { if ($this->request->isAJAX()) { @@ -255,6 +218,13 @@ class Tarifasenvioszonas extends \App\Controllers\GoBaseResourceController { // Build our Editor instance and process the data coming from _POST $response = Editor::inst($db, 'tarifas_envios_zonas') ->fields( + Field::inst('nombre') + ->validator('Validate::required', array( + 'message' => lang('TarifasEnviosZonas.validation.nombre.required')) + ) + ->validator(Validate::maxLen(50), array( + 'message' => lang('TarifasEnviosZonas.validation.nombre.max_length')) + ), Field::inst('cp_inicial') ->validator('Validate::required', array( 'message' => lang('TarifasEnviosZonas.validation.cp_inicial.required')) diff --git a/ci4/app/Models/Tarifas/TarifaEnvioPrecioModel.php b/ci4/app/Models/Tarifas/TarifaEnvioPrecioModel.php index 8d76bbd4..5f296493 100644 --- a/ci4/app/Models/Tarifas/TarifaEnvioPrecioModel.php +++ b/ci4/app/Models/Tarifas/TarifaEnvioPrecioModel.php @@ -196,13 +196,13 @@ class TarifaEnvioPrecioModel extends \App\Models\GoBaseModel return ""; } - public function removeAllPrecioLineas($zona_id = -1, $datetime = null, $delete_flag=1){ + public function removeAllPrecioLineas($tarifa_envio_id = -1, $datetime = null, $delete_flag=1){ $builder = $this->db ->table($this->table) ->set(['deleted_at' => $datetime->format('Y-m-d H:i:s'), 'is_deleted' => $delete_flag]) - ->where('tarifa_envio_id',$zona_id) + ->where('tarifa_envio_id',$tarifa_envio_id) ->update(); return $builder; diff --git a/ci4/app/Models/Tarifas/TarifaEnvioZonaModel.php b/ci4/app/Models/Tarifas/TarifaEnvioZonaModel.php index e2da9b9b..ba960240 100644 --- a/ci4/app/Models/Tarifas/TarifaEnvioZonaModel.php +++ b/ci4/app/Models/Tarifas/TarifaEnvioZonaModel.php @@ -125,4 +125,16 @@ class TarifaEnvioZonaModel extends \App\Models\GoBaseModel ->orLike("t1.importe_fijo", $search) ->groupEnd(); } + + public function removeAllZonasLineas($tarifa_envio_id = -1, $datetime = null, $delete_flag=1){ + + $builder = $this->db + ->table($this->table) + ->set(['deleted_at' => $datetime->format('Y-m-d H:i:s'), + 'is_deleted' => $delete_flag]) + ->where('tarifa_envio_id',$tarifa_envio_id) + ->update(); + + return $builder; + } }