diff --git a/modules/addons/NFEioServiceInvoices/lib/Admin/Controller.php b/modules/addons/NFEioServiceInvoices/lib/Admin/Controller.php index b42ab4a..e101249 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"); } @@ -408,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 { @@ -427,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 83d8f54..481f4da 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 @@ -45,23 +46,37 @@ public function aliquotsDataTable() ->get(); } - public function save($data) + public function save($codeService, $issHeld) { + $data = [ + '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', '=', $codeService)->exists()) { + $data['created_at'] = Timestamp::currentTimestamp(); + } + try { return Capsule::table($this->tableName)->updateOrInsert( - [ 'code_service' => $data['code_service'] ], - [ - 'iss_held' => $data['iss_held'] - ] + [ 'code_service' => $codeService ], + $data ); } catch (\Exception $exception) { echo $exception->getMessage(); } } - 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(); } /** 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();