From b5774462d9e6b5c7568d9c22100c6d7f06a9c073 Mon Sep 17 00:00:00 2001 From: Andre Kutianski Date: Tue, 25 Jun 2024 20:34:00 -0300 Subject: [PATCH 1/2] fix(timestamps): corrige timestamps em aliquotas e cod. de produtos Este commit corrige o registro de created_at e updated_at para aliquotas de ISS e codigos de servicos. Em ambos, ao salvar os valores no painel administrativo, o timestamp nao estava sendo registrado. --- .../lib/Admin/Controller.php | 14 +++++++++++--- .../lib/Models/Aliquots/Repository.php | 16 +++++++++++++--- .../lib/Models/ProductCode/Repository.php | 19 ++++++++++++++----- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/modules/addons/NFEioServiceInvoices/lib/Admin/Controller.php b/modules/addons/NFEioServiceInvoices/lib/Admin/Controller.php index b42ab4a..156fcc5 100644 --- a/modules/addons/NFEioServiceInvoices/lib/Admin/Controller.php +++ b/modules/addons/NFEioServiceInvoices/lib/Admin/Controller.php @@ -262,17 +262,25 @@ public function servicesCodeSave($vars) $msg = new FlashMessages(); $post = $_POST; + $product_id = $post['product_id'] ?? null; + $service_code = $post['service_code'] ?? null; + $product_name = $post['product_name'] ?? null; - if (!isset($post) && !is_array($post)) { + if ($_SERVER['REQUEST_METHOD'] != 'POST' || empty($post)) { $msg->error("Erro na submissão: dados inválidos", "{$vars['modulelink']}&action=servicesCode"); } + // caso $product_id, $service_code ou $product_name estejam vazios, retorna erro + if (is_null($product_id) || is_null($service_code) || is_null($product_name)) { + $msg->error("Erro na submissão: campos obrigatórios não preenchidos", "{$vars['modulelink']}&action=servicesCode"); + } + $productCodeRepo = new \NFEioServiceInvoices\Models\ProductCode\Repository(); if ($post['btnSave'] === 'true') { - $response = $productCodeRepo->save($post); + $response = $productCodeRepo->save($product_id, $service_code); if ($response) { - $msg->success("{$post['product_name']} atualizado com sucesso.", "{$vars['modulelink']}&action=servicesCode"); + $msg->success("{$product_name} atualizado com sucesso.", "{$vars['modulelink']}&action=servicesCode"); } else { $msg->info("Nenhuma alteração realizada.", "{$vars['modulelink']}&action=servicesCode"); } diff --git a/modules/addons/NFEioServiceInvoices/lib/Models/Aliquots/Repository.php b/modules/addons/NFEioServiceInvoices/lib/Models/Aliquots/Repository.php index 83d8f54..b2cdfc0 100644 --- a/modules/addons/NFEioServiceInvoices/lib/Models/Aliquots/Repository.php +++ b/modules/addons/NFEioServiceInvoices/lib/Models/Aliquots/Repository.php @@ -2,6 +2,7 @@ namespace NFEioServiceInvoices\Models\Aliquots; +use NFEioServiceInvoices\Helpers\Timestamp; use WHMCS\Database\Capsule; class Repository extends \WHMCSExpert\mtLibs\models\Repository @@ -47,12 +48,21 @@ public function aliquotsDataTable() public function save($data) { + $data = [ + 'code_service' => $data['code_service'], + 'iss_held' => $data['iss_held'], + 'updated_at' => Timestamp::currentTimestamp(), // campo updated_at sempre atualizado + ]; + + // Se o registro não existir, adiciona o campo 'created_at' + if (!Capsule::table($this->tableName)->where('code_service', '=', $data['code_service'])->exists()) { + $data['created_at'] = Timestamp::currentTimestamp(); + } + try { return Capsule::table($this->tableName)->updateOrInsert( [ 'code_service' => $data['code_service'] ], - [ - 'iss_held' => $data['iss_held'] - ] + $data ); } catch (\Exception $exception) { echo $exception->getMessage(); diff --git a/modules/addons/NFEioServiceInvoices/lib/Models/ProductCode/Repository.php b/modules/addons/NFEioServiceInvoices/lib/Models/ProductCode/Repository.php index bd37d8c..e0318de 100644 --- a/modules/addons/NFEioServiceInvoices/lib/Models/ProductCode/Repository.php +++ b/modules/addons/NFEioServiceInvoices/lib/Models/ProductCode/Repository.php @@ -2,6 +2,7 @@ namespace NFEioServiceInvoices\Models\ProductCode; +use NFEioServiceInvoices\Helpers\Timestamp; use WHMCS\Database\Capsule; /** @@ -51,16 +52,24 @@ public function servicesCodeDataTable() ->get(); } - public function save($data) + public function save($productId, $serviceCode) { + $data = [ + 'product_id' => $productId, + 'code_service' => $serviceCode, + 'ID_user' => 1, + 'updated_at' => Timestamp::currentTimestamp(), // campo updated_at sempre atualizado + ]; + + // Se o registro não existir, adiciona o campo 'created_at' + if (!Capsule::table($this->tableName)->where('product_id', $productId)->exists()) { + $data['created_at'] = Timestamp::currentTimestamp(); + } try { return Capsule::table($this->tableName)->updateOrInsert( [ 'product_id' => $data['product_id'] ], - [ - 'code_service' => $data['service_code'], - 'ID_user' => 1, - ] + $data ); } catch (\Exception $exception) { echo $exception->getMessage(); From 6ac39a20b46b87ee68f9d519a4ebba9dc921f40f Mon Sep 17 00:00:00 2001 From: Andre Kutianski Date: Tue, 25 Jun 2024 21:46:18 -0300 Subject: [PATCH 2/2] refactor(aliquotas): melhorias na rotina ao salvar aliquotas ISS Este commit implementa uma refatoracao visando melhorar a manipulacao e validacao ao salvas aliquotas ISS --- .../lib/Admin/Controller.php | 16 +++++++++++----- .../lib/Models/Aliquots/Repository.php | 19 ++++++++++++------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/modules/addons/NFEioServiceInvoices/lib/Admin/Controller.php b/modules/addons/NFEioServiceInvoices/lib/Admin/Controller.php index 156fcc5..e101249 100644 --- a/modules/addons/NFEioServiceInvoices/lib/Admin/Controller.php +++ b/modules/addons/NFEioServiceInvoices/lib/Admin/Controller.php @@ -416,17 +416,23 @@ public function aliquots($vars) public function aliquotsSave($vars) { $msg = new FlashMessages(); - $post = $_POST; - $productCodeRepo = new \NFEioServiceInvoices\Models\ProductCode\Repository(); $aliquotsRepo = new \NFEioServiceInvoices\Models\Aliquots\Repository(); + $post = $_POST; + $iss_held = $post['iss_held'] ?? null; + $code_service = $post['code_service'] ?? null; + $record_id = $post['id'] ?? null; - if (!isset($post) && !is_array($post)) { + if ($_SERVER['REQUEST_METHOD'] != 'POST' || empty($post)) { $msg->error("Erro na submissão: dados inválidos", "{$vars['modulelink']}&action=aliquots"); } + if (is_null($iss_held) || is_null($code_service) || is_null($record_id)) { + $msg->error("Erro na submissão: campos obrigatórios não preenchidos", "{$vars['modulelink']}&action=aliquots"); + } + if ($post['btnSave'] === 'true') { - $response = $aliquotsRepo->save($post); + $response = $aliquotsRepo->save($code_service, $iss_held); if ($response) { $msg->success("Alíquota atualizada com sucesso.", "{$vars['modulelink']}&action=aliquots"); } else { @@ -435,7 +441,7 @@ public function aliquotsSave($vars) } if ($post['btnDelete'] === 'true') { - $aliquotsRepo->delete($post); + $aliquotsRepo->delete($record_id); $msg->warning("Alíquota removida com sucesso.", "{$vars['modulelink']}&action=aliquots"); } } diff --git a/modules/addons/NFEioServiceInvoices/lib/Models/Aliquots/Repository.php b/modules/addons/NFEioServiceInvoices/lib/Models/Aliquots/Repository.php index b2cdfc0..481f4da 100644 --- a/modules/addons/NFEioServiceInvoices/lib/Models/Aliquots/Repository.php +++ b/modules/addons/NFEioServiceInvoices/lib/Models/Aliquots/Repository.php @@ -46,22 +46,22 @@ public function aliquotsDataTable() ->get(); } - public function save($data) + public function save($codeService, $issHeld) { $data = [ - 'code_service' => $data['code_service'], - 'iss_held' => $data['iss_held'], + 'code_service' => $codeService, + 'iss_held' => $issHeld, 'updated_at' => Timestamp::currentTimestamp(), // campo updated_at sempre atualizado ]; // Se o registro não existir, adiciona o campo 'created_at' - if (!Capsule::table($this->tableName)->where('code_service', '=', $data['code_service'])->exists()) { + if (!Capsule::table($this->tableName)->where('code_service', '=', $codeService)->exists()) { $data['created_at'] = Timestamp::currentTimestamp(); } try { return Capsule::table($this->tableName)->updateOrInsert( - [ 'code_service' => $data['code_service'] ], + [ 'code_service' => $codeService ], $data ); } catch (\Exception $exception) { @@ -69,9 +69,14 @@ public function save($data) } } - public function delete($data) + /** + * Remove a aliquota de retenção de ISS + * @param $id + * @return mixed + */ + public function delete($id) { - return Capsule::table($this->tableName())->where('id', '=', $data['id'])->delete(); + return Capsule::table($this->tableName())->where('id', '=', $id)->delete(); } /**