Skip to content

Commit

Permalink
fix(timestamps): corrige timestamps em aliquotas e cod. de produtos
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
andrekutianski committed Jun 25, 2024
1 parent 0b67fd2 commit b577446
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
14 changes: 11 additions & 3 deletions modules/addons/NFEioServiceInvoices/lib/Admin/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace NFEioServiceInvoices\Models\Aliquots;

use NFEioServiceInvoices\Helpers\Timestamp;
use WHMCS\Database\Capsule;

class Repository extends \WHMCSExpert\mtLibs\models\Repository
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace NFEioServiceInvoices\Models\ProductCode;

use NFEioServiceInvoices\Helpers\Timestamp;
use WHMCS\Database\Capsule;

/**
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit b577446

Please sign in to comment.