Skip to content

Commit

Permalink
fix: corrige propriedades de colunas de timestamp
Browse files Browse the repository at this point in the history
Esta atualização modifica como as colunas de registro de data e hora de criação e atualização são manipuladas no módulo NFEioServiceInvoices. Agora, as colunas 'created_at' e 'updated_at' são gerenciadas automaticamente pelo banco de dados, evitando a necessidade de atualizá-las manualmente no código. Além disso, foi implementada uma migração para atualizar as colunas em questão nas tabelas existentes.

ref: #156
  • Loading branch information
andrekutianski committed Apr 26, 2024
1 parent 60687a5 commit 916f390
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 16 deletions.
2 changes: 0 additions & 2 deletions modules/addons/NFEioServiceInvoices/callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,6 @@
'environment' => $nfe['environment'],
'flow_status' => $nf_flow_status,
'pdf' => $nfe['pdf'],
'created_at' => $nfe['created_at'],
'updated_at' => date('Y-m-d H:i:s'),
];

try {
Expand Down
18 changes: 18 additions & 0 deletions modules/addons/NFEioServiceInvoices/lib/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,5 +283,23 @@ public function upgrade($vars)
* @see https://github.com/nfe/whmcs-addon/issues/134
*/
}

/**
* Atualiza as colunas de timestamp para a versão inferior a 2.1.8
* nas tabelas informadas.
*
* @see https://github.com/nfe/whmcs-addon/issues/156
*/
if(version_compare($currentlyInstalledVersion, '2.1.8', 'le')) {

// atualiza o nome da coluna de timestamp para a tabela productcode
\NFEioServiceInvoices\Migrations\Migrations::changeProductCodeTimestampColumnsName();

// altera as colunas de timestamp para as tabelas
\NFEioServiceInvoices\Migrations\Migrations::migrateTimestampColumns('mod_nfeio_si_productcode');
\NFEioServiceInvoices\Migrations\Migrations::migrateTimestampColumns('mod_nfeio_si_serviceinvoices');
\NFEioServiceInvoices\Migrations\Migrations::migrateTimestampColumns('mod_nfeio_si_aliquots');

}
}
}
10 changes: 3 additions & 7 deletions modules/addons/NFEioServiceInvoices/lib/Legacy/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,6 @@ function gnfe_queue_nfe($invoice_id, $create_all = false)
'environment' => 'waiting',
'flow_status' => 'waiting',
'pdf' => 'waiting',
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => 'waiting',
'rpsSerialNumber' => 'waiting',
'service_code' => $item['code_service'],
];
Expand All @@ -225,9 +223,9 @@ function gnfe_queue_nfe($invoice_id, $create_all = false)
$mount_item = floatval($item['amount']);
$mount = $mountDB + $mount_item;

$update_nfe = Capsule::table($_tableName)->where('id', '=', $service_code_row[0]->id)->update(['services_amount' => $mount]);
Capsule::table($_tableName)->where('id', '=', $service_code_row[0]->id)->update(['services_amount' => $mount]);
} else {
$save_nfe = Capsule::table($_tableName)->insert($data);
Capsule::table($_tableName)->insert($data);
}
} catch (\Exception $e) {
return $e->getMessage();
Expand Down Expand Up @@ -437,7 +435,7 @@ function gnfe_xml_nfe($nf)
return $result;
}

function gnfe_update_nfe($nfe, $user_id, $invoice_id, $pdf, $created_at, $updated_at, $id_gofasnfeio = false)
function gnfe_update_nfe($nfe, $user_id, $invoice_id, $pdf, $id_gofasnfeio = false)
{
$data = [
'invoice_id' => $invoice_id,
Expand All @@ -448,8 +446,6 @@ function gnfe_update_nfe($nfe, $user_id, $invoice_id, $pdf, $created_at, $update
'environment' => $nfe->environment,
'flow_status' => $nfe->flowStatus,
'pdf' => $pdf,
'created_at' => $created_at,
'updated_at' => $updated_at,
'rpsSerialNumber' => $nfe->rpsSerialNumber,
'rpsNumber' => $nfe->rpsNumber,
];
Expand Down
89 changes: 89 additions & 0 deletions modules/addons/NFEioServiceInvoices/lib/Migrations/Migrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,93 @@ public static function migrateProductCodes()

return false;
}

/**
* Creates and executes an SQL statement to alter a column in the specified table.
*
* @param PDO $pdo The PDO object for database connection
* @param string $columnName The name of the column to be altered
* @param string $alterStatement The ALTER statement for the column
* @return void
*/
private function createAlterColumnTimestampStatement($pdo, $columnName, $alterStatement, $tableName)
{
$statement = $pdo->prepare(
sprintf('ALTER TABLE %s CHANGE %s %s TIMESTAMP NOT NULL DEFAULT %s',
$tableName,
$columnName,
$columnName,
$alterStatement
)
);
$statement->execute();
}

/**
*
* Atualiza as colunas de timestamp na tabela de notas fiscais de serviço.
* Define a coluna `created_at` com o valor do timestamp atual, e
* a coluna `updated_at` com o valor do timestamp atual em caso de atualização.
*/
public static function migrateTimestampColumns(string $tableName)
{
if (Capsule::schema()->hasTable($tableName)) {
$pdo = Capsule::connection()->getPdo();
$pdo->beginTransaction();
try {
$self = new static();
$self->createAlterColumnTimestampStatement($pdo, 'created_at', 'CURRENT_TIMESTAMP', $tableName);
$self->createAlterColumnTimestampStatement($pdo, 'updated_at', 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', $tableName);
if ($pdo->inTransaction()) {
$pdo->commit();
}
} catch (\Exception $e) {
logModuleCall(
'nfeio_serviceinvoices',
'upgradeServiceInvoicesTimestampColumns_error',
$e->getMessage(),
$e->getTraceAsString()
);
if ($pdo->inTransaction()) {
$pdo->rollBack();
}

}
}
}

/**
* Altera as colunas da tabela mod_nfeio_si_productcode referente ao timestamp
* para created_at e updated_at.
*
* @return void
*/
public static function changeProductCodeTimestampColumnsName()
{

if (Capsule::schema()->hasTable('mod_nfeio_si_productcode')) {

$pdo = Capsule::connection()->getPdo();
$pdo->beginTransaction();
try {
$st1 = $pdo->prepare('ALTER TABLE mod_nfeio_si_productcode CHANGE create_at created_at TIMESTAMP');
$st2 = $pdo->prepare('ALTER TABLE mod_nfeio_si_productcode CHANGE update_at updated_at TIMESTAMP');
$st1->execute();
$st2->execute();
if ($pdo->inTransaction()) {
$pdo->commit();
}
} catch (\Exception $e) {
logModuleCall(
'nfeio_serviceinvoices',
'changeProductCodeTimestampColumnsName_error',
$e->getMessage(),
$e->getTraceAsString()
);
if ($pdo->inTransaction()) {
$pdo->rollBack();
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ function ($table) {
$table->string('code_service', 30);
// retenção de ISS
$table->float('iss_held', 5, 2)->nullable();
$table->timestamp('created_at');
$table->timestamp('updated_at');
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->useCurrentOnUpdate();
}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ function ($table) {
$table->increments('id');
$table->integer('product_id');
$table->string('code_service', 30);
$table->timestamp('create_at');
$table->timestamp('update_at');
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->useCurrentOnUpdate();
$table->integer('ID_user');
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ function ($table) {
$table->string('pdf');
$table->string('rpsSerialNumber');
$table->string('rpsNumber');
$table->timestamp('created_at');
$table->timestamp('updated_at');
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->useCurrentOnUpdate();
$table->string('service_code', 30)->nullable(true);
$table->string('tics')->nullable(true);
}
Expand Down
2 changes: 1 addition & 1 deletion modules/addons/NFEioServiceInvoices/lib/NFEio/Nfe.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ public function emit($data)
$nfeResponse = $this->legacyFunctions->gnfe_issue_nfe($postData);

if (!$nfeResponse->message) {
$gnfe_update_nfe = $this->legacyFunctions->gnfe_update_nfe($nfeResponse, $clientId, $invoiceId, 'n/a', date('Y-m-d H:i:s'), date('Y-m-d H:i:s'), $nfDbId);
$this->legacyFunctions->gnfe_update_nfe($nfeResponse, $clientId, $invoiceId, 'n/a', $nfDbId);
logModuleCall('nfeio_serviceinvoices', 'nf_emit', $postData, $nfeResponse);
} else {
logModuleCall('nfeio_serviceinvoices', 'nf_emit_error', $postData, $nfeResponse);
Expand Down

0 comments on commit 916f390

Please sign in to comment.