Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correcao de timestamps e melhorias na rotina de aliquotas #161

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 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 Expand Up @@ -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 {
Expand All @@ -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");
}
}
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 @@ -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();
}

/**
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
Loading