-
-
Notifications
You must be signed in to change notification settings - Fork 283
[16.0][REF] l10n_br_fiscal: Melhorias na inutilização de numeração #4141
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
base: 16.0
Are you sure you want to change the base?
Changes from all commits
e98996e
0828a21
85cf1ac
9068acb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -81,24 +81,23 @@ def create(self, vals_list): | |
| def name_get(self): | ||
| return [(r.id, f"{r.name}") for r in self] | ||
|
|
||
| def _is_invalid_number(self, document_number): | ||
| def is_invalid_number(self, document_number): | ||
| self.ensure_one() | ||
| is_invalid_number = True | ||
| # TODO Improve this implementation! | ||
| invalids = self.env["l10n_br_fiscal.invalidate.number"].search( | ||
| [("state", "=", "done"), ("document_serie_id", "=", self.id)] | ||
| return self.env["l10n_br_fiscal.invalidate.number"].search( | ||
| [ | ||
| ("state", "=", "done"), | ||
| ("document_type_id", "=", self.document_type_id.id), | ||
| ("document_serie_id", "=", self.id), | ||
| ("number_start", "<=", document_number), | ||
| ("number_end", ">=", document_number), | ||
| ], | ||
| limit=1, | ||
| ) | ||
| invalid_numbers = [] | ||
| for invalid in invalids: | ||
| invalid_numbers += range(invalid.number_start, invalid.number_end + 1) | ||
| if int(document_number) not in invalid_numbers: | ||
| is_invalid_number = False | ||
| return is_invalid_number | ||
|
|
||
| def next_seq_number(self): | ||
| self.ensure_one() | ||
| document_number = self.internal_sequence_id._next() | ||
| if self._is_invalid_number(document_number) or self.check_number_in_use( | ||
| if self.is_invalid_number(document_number) or self.check_number_in_use( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. eu so acho melhor conservar o _ na frente, assim não fica exposto pro RPC. Dificilmente esse método seria uma falha de segurança mas por padrão é melhor expor apenas o necessário. |
||
| document_number | ||
| ): | ||
| document_number = self.next_seq_number() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,8 +5,6 @@ | |
| from odoo import _, api, fields, models | ||
| from odoo.exceptions import UserError, ValidationError | ||
|
|
||
| from ..constants.fiscal import SITUACAO_EDOC_INUTILIZADA | ||
|
|
||
|
|
||
| class InvalidateNumber(models.Model): | ||
| _name = "l10n_br_fiscal.invalidate.number" | ||
|
|
@@ -39,7 +37,9 @@ class InvalidateNumber(models.Model): | |
| ) | ||
|
|
||
| document_electronic = fields.Boolean( | ||
| related="document_type_id.electronic", string="Electronic?", readonly=True | ||
| related="document_type_id.electronic", | ||
| string="Electronic?", | ||
| readonly=True, | ||
| ) | ||
|
|
||
| document_serie_id = fields.Many2one( | ||
|
|
@@ -117,31 +117,4 @@ def unlink(self): | |
| return super().unlink() | ||
|
|
||
| def action_invalidate(self): | ||
| for record in self: | ||
| record._invalidate() | ||
|
|
||
| def _create_invalidate_document(self, document_number): | ||
| self.env["l10n_br_fiscal.document"].create( | ||
| { | ||
| "document_serie_id": self.document_serie_id.id, | ||
| "document_type_id": self.document_serie_id.document_type_id.id, | ||
| "company_id": self.company_id.id, | ||
| "state_edoc": SITUACAO_EDOC_INUTILIZADA, | ||
| "issuer": "company", | ||
| "document_number": str(document_number), | ||
| "invalidate_event_id": self.authorization_event_id.id, | ||
| } | ||
| ) | ||
|
|
||
| def _update_document_status(self, document_id=None): | ||
| if document_id: | ||
| document_id.state_edoc = SITUACAO_EDOC_INUTILIZADA | ||
| document_id.invalidate_event_id = self.authorization_event_id | ||
| else: | ||
| for document_number in range(self.number_start, self.number_end + 1): | ||
| self._create_invalidate_document(document_number) | ||
|
|
||
| def _invalidate(self, document_id=None): | ||
| self.ensure_one() | ||
| self._update_document_status(document_id) | ||
| self.state = "done" | ||
| self.write({"state": "done"}) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. aqui é mais idiomática usar self.state = "done". Isso vai chamar o write automaticamente ainda. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # Copyright (C) 2025 Renato Lima - Akretion <[email protected]> | ||
| # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
|
|
||
| from odoo.exceptions import ValidationError | ||
| from odoo.tests.common import TransactionCase | ||
|
|
||
|
|
||
| class TestFiscalInvalidateNumber(TransactionCase): | ||
| @classmethod | ||
| def setUpClass(cls): | ||
| super().setUpClass() | ||
| cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True)) | ||
|
|
||
| cls.company = cls.env.ref("l10n_br_base.empresa_simples_nacional") | ||
| cls.document_type_nfe = cls.env.ref("l10n_br_fiscal.document_55") | ||
| cls.document_serie_nfe = cls.env.ref( | ||
| "l10n_br_fiscal.empresa_sn_document_55_serie_1" | ||
| ) | ||
|
|
||
| invalidate_number_100_200 = cls.env["l10n_br_fiscal.invalidate.number"].create( | ||
| { | ||
| "company_id": cls.company.id, | ||
| "document_type_id": cls.document_type_nfe.id, | ||
| "document_serie_id": cls.document_serie_nfe.id, | ||
| "number_start": 100, | ||
| "number_end": 200, | ||
| "justification": "Just a invalidate numbers test", | ||
| } | ||
| ) | ||
| invalidate_number_100_200.action_invalidate() | ||
|
|
||
| def test_fiscal_invalidate_number_overlap(self): | ||
| """Test Invalidate Number Overlap.""" | ||
| with self.assertRaises(ValidationError): | ||
| self.env["l10n_br_fiscal.invalidate.number"].create( | ||
| { | ||
| "company_id": self.company.id, | ||
| "document_type_id": self.document_type_nfe.id, | ||
| "document_serie_id": self.document_serie_nfe.id, | ||
| "number_start": 100, | ||
| "number_end": 200, | ||
| "justification": "Just a invalidate numbers test", | ||
| } | ||
| ) | ||
|
|
||
| def test_fiscal_document_with_invalidated_number(self): | ||
| """Test Fiscal Document with Invalidate Number.""" | ||
| with self.assertRaises(ValidationError): | ||
| self.env["l10n_br_fiscal.document"].create( | ||
| { | ||
| "company_id": self.company.id, | ||
| "document_type_id": self.document_type_nfe.id, | ||
| "document_serie_id": self.document_serie_nfe.id, | ||
| "document_number": 150, | ||
| } | ||
| ) |
Uh oh!
There was an error while loading. Please reload this page.