diff --git a/modules/addons/NFEioServiceInvoices/callback.php b/modules/addons/NFEioServiceInvoices/callback.php index 75ab92e..972fbca 100644 --- a/modules/addons/NFEioServiceInvoices/callback.php +++ b/modules/addons/NFEioServiceInvoices/callback.php @@ -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 { diff --git a/modules/addons/NFEioServiceInvoices/lib/Configuration.php b/modules/addons/NFEioServiceInvoices/lib/Configuration.php index 4061071..a0f6054 100644 --- a/modules/addons/NFEioServiceInvoices/lib/Configuration.php +++ b/modules/addons/NFEioServiceInvoices/lib/Configuration.php @@ -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'); + + } } } diff --git a/modules/addons/NFEioServiceInvoices/lib/Legacy/Functions.php b/modules/addons/NFEioServiceInvoices/lib/Legacy/Functions.php index 7361a10..49b4915 100644 --- a/modules/addons/NFEioServiceInvoices/lib/Legacy/Functions.php +++ b/modules/addons/NFEioServiceInvoices/lib/Legacy/Functions.php @@ -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'], ]; @@ -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(); @@ -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, @@ -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, ]; diff --git a/modules/addons/NFEioServiceInvoices/lib/Migrations/Migrations.php b/modules/addons/NFEioServiceInvoices/lib/Migrations/Migrations.php index fd553ef..726605a 100644 --- a/modules/addons/NFEioServiceInvoices/lib/Migrations/Migrations.php +++ b/modules/addons/NFEioServiceInvoices/lib/Migrations/Migrations.php @@ -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(); + } + } + } + } } diff --git a/modules/addons/NFEioServiceInvoices/lib/Models/Aliquots/Repository.php b/modules/addons/NFEioServiceInvoices/lib/Models/Aliquots/Repository.php index 27e4304..d39ceea 100644 --- a/modules/addons/NFEioServiceInvoices/lib/Models/Aliquots/Repository.php +++ b/modules/addons/NFEioServiceInvoices/lib/Models/Aliquots/Repository.php @@ -81,8 +81,11 @@ 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'); @@ -90,10 +93,14 @@ 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')->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)); + } } diff --git a/modules/addons/NFEioServiceInvoices/lib/Models/ProductCode/Repository.php b/modules/addons/NFEioServiceInvoices/lib/Models/ProductCode/Repository.php index f02ced3..80985a8 100644 --- a/modules/addons/NFEioServiceInvoices/lib/Models/ProductCode/Repository.php +++ b/modules/addons/NFEioServiceInvoices/lib/Models/ProductCode/Repository.php @@ -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)); + } } diff --git a/modules/addons/NFEioServiceInvoices/lib/Models/ServiceInvoices/Repository.php b/modules/addons/NFEioServiceInvoices/lib/Models/ServiceInvoices/Repository.php index 1f14db7..60e5ef0 100644 --- a/modules/addons/NFEioServiceInvoices/lib/Models/ServiceInvoices/Repository.php +++ b/modules/addons/NFEioServiceInvoices/lib/Models/ServiceInvoices/Repository.php @@ -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 @@ -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)); + } /** diff --git a/modules/addons/NFEioServiceInvoices/lib/NFEio/Nfe.php b/modules/addons/NFEioServiceInvoices/lib/NFEio/Nfe.php index 5e6e5c2..25e7c01 100644 --- a/modules/addons/NFEioServiceInvoices/lib/NFEio/Nfe.php +++ b/modules/addons/NFEioServiceInvoices/lib/NFEio/Nfe.php @@ -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);