Skip to content

Commit

Permalink
[MIG] l10n_br_account: flexible document_type_id
Browse files Browse the repository at this point in the history
  • Loading branch information
rvalyi committed Aug 21, 2024
1 parent ddc6d53 commit ae93e04
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
23 changes: 23 additions & 0 deletions l10n_br_account/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ class AccountMove(models.Model):
string="Fiscal Document",
copy=False,
ondelete="cascade",
store=True,
compute="_compute_fiscal_document_id",
)

fiscal_document_ids = fields.One2many(
Expand All @@ -117,6 +119,27 @@ class AccountMove(models.Model):
compute="_compute_fiscal_operation_type",
)

@api.onchange("document_type_id")
def _inverse_document_type_id(self):
if (self.document_type_id and not self.fiscal_document_id) or (
not self.document_type_id and self.fiscal_document_id
):
self.env.add_to_compute(self._fields["fiscal_document_id"], self)

def _compute_fiscal_document_id(self):
for move in self:
if move.document_type_id and not move.fiscal_document_id:
fiscal_doc_vals = {}
for field in self._shadowed_fields():
fiscal_doc_vals[f"fiscal_{field}"] = getattr(move, field)
move.fiscal_document_id = (
self.env["l10n_br_fiscal.document"].create(fiscal_doc_vals).id
)
elif not move.document_type_id and move.fiscal_document_id:
bad_fiscal_doc = move.fiscal_document_id
move.fiscal_document_id = False
bad_fiscal_doc.action_document_cancel()

Check warning on line 141 in l10n_br_account/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_account/models/account_move.py#L139-L141

Added lines #L139 - L141 were not covered by tests

@api.constrains("fiscal_document_id", "document_type_id")
def _check_fiscal_document_type(self):
for rec in self:
Expand Down
5 changes: 5 additions & 0 deletions l10n_br_account/models/fiscal_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ class FiscalDocument(models.Model):
compute="_compute_date_in_out", inverse="_inverse_date_in_out", store=True
)

document_type_id = fields.Many2one(inverse="_inverse_document_type_id")

def _inverse_document_type_id(self):
pass # (meant to be overriden in account.move)

@api.depends("move_ids", "move_ids.invoice_date")
def _compute_document_date(self):
for record in self:
Expand Down

0 comments on commit ae93e04

Please sign in to comment.