Skip to content

Commit

Permalink
refactor(aliquotas): melhorias na rotina ao salvar aliquotas ISS
Browse files Browse the repository at this point in the history
Este commit implementa uma refatoracao visando melhorar a manipulacao e validacao ao salvas aliquotas ISS
  • Loading branch information
andrekutianski committed Jun 26, 2024
1 parent b577446 commit 6ac39a2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
16 changes: 11 additions & 5 deletions modules/addons/NFEioServiceInvoices/lib/Admin/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,32 +46,37 @@ 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) {
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();
}

/**
Expand Down

0 comments on commit 6ac39a2

Please sign in to comment.