Skip to content

Commit

Permalink
Merge pull request #158 from nfe/issue-156
Browse files Browse the repository at this point in the history
WIP: corrige propriedades de colunas de timestamp
  • Loading branch information
andrekutianski authored Apr 26, 2024
2 parents 60687a5 + 01afe42 commit 2e5489a
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 22 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 self();
$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 @@ -81,19 +81,26 @@ public function drop()
*/
public function createAliquotsTable()
{
if (!Capsule::schema()->hasTable($this->tableName)) {
Capsule::schema()->create(
$db = Capsule::connection();
$schema = Capsule::schema();

if (!$schema->hasTable($this->tableName)) {
$schema->create(
$this->tableName,
function ($table) {
$table->increments('id');
//codigo o serviço que será viculado
$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')->useCurrent();
}
);

// Adiciona a coluna updated_at com a configuração de auto update #156
$db->statement(sprintf('ALTER TABLE %s CHANGE updated_at updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', $this->tableName));

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,25 @@ public function dropProductCodeTable()
*/
public function createProductCodeTable()
{
if (!Capsule::schema()->hasTable($this->tableName)) {
Capsule::schema()->create(
$db = Capsule::connection();
$schema = Capsule::schema();

if (!$schema->hasTable($this->tableName)) {
$schema->create(
$this->tableName,
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')->useCurrent();
$table->integer('ID_user');
}
);

// Adiciona a coluna updated_at com a configuração de auto update #156
$db->statement(sprintf('ALTER TABLE %s CHANGE updated_at updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', $this->tableName));

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,11 @@ public function dataTable()
*/
public function createServiceInvoicesTable()
{
if (!Capsule::schema()->hasTable($this->tableName)) {
Capsule::schema()->create(
$db = Capsule::connection();
$schema = Capsule::schema();

if (!$schema->hasTable($this->tableName)) {
$schema->create(
$this->tableName,
function ($table) {
// incremented id
Expand All @@ -115,13 +118,17 @@ 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')->useCurrent();
$table->string('service_code', 30)->nullable(true);
$table->string('tics')->nullable(true);
}
);
}

// Adiciona a coluna updated_at com a configuração de auto update #156
$db->statement(sprintf('ALTER TABLE %s CHANGE updated_at updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', $this->tableName));

}

/**
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 2e5489a

Please sign in to comment.