Repository: OCA/l10n-brazil
Pull request #3653: [16.0][l10n_br_fiscal] l10n_br_fiscal.document.mixin.methods: convert _onchange_fiscal_operation_id to compute
Author: login: rvalyi
Labels: name: approved
name: merged 🎉
Created: 2025-03-15T01:30:57Z | Merged: 2025-03-17T16:32:14Z

como o modulo l10n_br_fiscal nasceu antes de existir o sistema dos campos compute do Odoo, o modulo ainda esta repleto de onchange. Isso não esta muito legal,  seria melhor ter campos compute.

Aqui eu matei o _onchange_fiscal_position_id nas camadas mais baixas. Tem outros objetos de mais alto nivel onde tem um onchange _onchange_fiscal_position_id, como o sale.order, purchase.order e l10n_br_fiscal.document. Aqui deixei de chamar o super (o onchange que eu removei) mas não procurei eliminar esses outros onchanges. Poderia ser feito depois, mas sera um um pouco mais complexo.

Fazendo esse refator, eu tb vi que o campo operation_name tava definido nos sale.order, purchase.order e stock.picking. Não precisava: eu joguei no l10n_br_fiscal.document.mixin.fields onde ele fica herdado então. (O campo operation_name não precisa ser ser store=True).

Converter tudo iria tomar bastante esforço/tempo, temos que balancear isso com outras prioridades...
Aqui eu comecei convertando _onchange_fiscal_operation_id do l10n_br_fiscal.document.mixin.methods porque era  relativamente facil...


Diff:
diff --git a/l10n_br_fiscal/models/document.py b/l10n_br_fiscal/models/document.py
index df362bc96508..eb741e3e9028 100644
--- a/l10n_br_fiscal/models/document.py
+++ b/l10n_br_fiscal/models/document.py
@@ -112,10 +112,6 @@ class Document(models.Model):
         default=lambda self: self.env.user,
     )
 
-    operation_name = fields.Char(
-        copy=False,
-    )
-
     document_electronic = fields.Boolean(
         related="document_type_id.electronic",
         string="Electronic?",
diff --git a/l10n_br_fiscal/models/document_mixin_fields.py b/l10n_br_fiscal/models/document_mixin_fields.py
index b45fbb967ea6..41cba9e5bd74 100644
--- a/l10n_br_fiscal/models/document_mixin_fields.py
+++ b/l10n_br_fiscal/models/document_mixin_fields.py
@@ -41,6 +41,11 @@ def _operation_domain(self):
         default=_default_operation,
     )
 
+    operation_name = fields.Char(
+        copy=False,
+        compute="_compute_operation_name",
+    )
+
     #
     # Company and Partner are defined here to avoid warnings on runbot
     #
@@ -72,6 +77,8 @@ def _operation_domain(self):
         column2="comment_id",
         string="Comments",
         domain=[("object", "=", FISCAL_COMMENT_DOCUMENT)],
+        compute="_compute_comment_ids",
+        store=True,
     )
 
     fiscal_additional_data = fields.Text()
diff --git a/l10n_br_fiscal/models/document_mixin_methods.py b/l10n_br_fiscal/models/document_mixin_methods.py
index eeb93f9a2e2e..2b2d372abe84 100644
--- a/l10n_br_fiscal/models/document_mixin_methods.py
+++ b/l10n_br_fiscal/models/document_mixin_methods.py
@@ -112,11 +112,21 @@ def _onchange_partner_id_fiscal(self):
                 # reload fiscal data, operation line, cfop, taxes, etc.
                 line._onchange_fiscal_operation_id()
 
-    @api.onchange("fiscal_operation_id")
-    def _onchange_fiscal_operation_id(self):
-        if self.fiscal_operation_id:
-            self.operation_name = self.fiscal_operation_id.name
-            self.comment_ids = self.fiscal_operation_id.comment_ids
+    @api.depends("fiscal_operation_id")
+    def _compute_operation_name(self):
+        for doc in self:
+            if doc.fiscal_operation_id:
+                doc.operation_name = doc.fiscal_operation_id.name
+            else:
+                doc.operation_name = False
+
+    @api.depends("fiscal_operation_id")
+    def _compute_comment_ids(self):
+        for doc in self:
+            if doc.fiscal_operation_id:
+                doc.comment_ids = doc.fiscal_operation_id.comment_ids
+            elif doc.comment_ids is None:
+                doc.comment_ids = []
 
     def _inverse_amount_freight(self):
         for record in self.filtered(lambda doc: doc._get_product_amount_lines()):
diff --git a/l10n_br_fiscal/models/document_move_mixin.py b/l10n_br_fiscal/models/document_move_mixin.py
index 3b2daed1ef51..a6e38a8023f3 100644
--- a/l10n_br_fiscal/models/document_move_mixin.py
+++ b/l10n_br_fiscal/models/document_move_mixin.py
@@ -232,7 +232,6 @@ def _onchange_document_serie_id(self):
 
     @api.onchange("fiscal_operation_id")
     def _onchange_fiscal_operation_id(self):
-        result = super()._onchange_fiscal_operation_id()
         if self.fiscal_operation_id:
             self.fiscal_operation_type = self.fiscal_operation_id.fiscal_operation_type
             self.edoc_purpose = self.fiscal_operation_id.edoc_purpose
@@ -258,4 +257,3 @@ def _onchange_fiscal_operation_id(self):
                     )
                 )
             self.document_subsequent_ids = subsequent_documents
-        return result
diff --git a/l10n_br_purchase/models/purchase_order.py b/l10n_br_purchase/models/purchase_order.py
index 19e723367170..ddd5fd69782a 100644
--- a/l10n_br_purchase/models/purchase_order.py
+++ b/l10n_br_purchase/models/purchase_order.py
@@ -57,10 +57,8 @@ def _fiscal_operation_domain(self):
         column1="purchase_id",
         column2="comment_id",
         string="Comments",
-    )
-
-    operation_name = fields.Char(
-        copy=False,
+        compute="_compute_comment_ids",
+        store=True,
     )
 
     @api.model
@@ -71,9 +69,7 @@ def _get_view(self, view_id=None, view_type="form", **options):
 
     @api.onchange("fiscal_operation_id")
     def _onchange_fiscal_operation_id(self):
-        result = super()._onchange_fiscal_operation_id()
         self.fiscal_position_id = self.fiscal_operation_id.fiscal_position_id
-        return result
 
     def _get_amount_lines(self):
         """Get object lines instaces used to compute fields"""
diff --git a/l10n_br_sale/models/sale_order.py b/l10n_br_sale/models/sale_order.py
index 1006f43634dc..29786322da7d 100644
--- a/l10n_br_sale/models/sale_order.py
+++ b/l10n_br_sale/models/sale_order.py
@@ -73,6 +73,8 @@ def _fiscal_operation_domain(self):
         column1="sale_id",
         column2="comment_id",
         string="Comments",
+        compute="_compute_comment_ids",
+        store=True,
     )
 
     amount_freight_value = fields.Monetary(
@@ -87,10 +89,6 @@ def _fiscal_operation_domain(self):
         inverse="_inverse_amount_other",
     )
 
-    operation_name = fields.Char(
-        copy=False,
-    )
-
     def _get_amount_lines(self):
         """Get object lines instaces used to compute fields"""
         return self.mapped("order_line")
@@ -113,7 +111,6 @@ def _get_view(self, view_id=None, view_type="form", **options):
 
     @api.onchange("fiscal_operation_id")
     def _onchange_fiscal_operation_id(self):
-        result = super()._onchange_fiscal_operation_id()
         if self.fiscal_operation_id:
             self.fiscal_position_id = self.fiscal_operation_id.fiscal_position_id
         else:
@@ -128,8 +125,6 @@ def _onchange_fiscal_operation_id(self):
             for line in self.order_line:
                 line.fiscal_operation_id = False
 
-        return result
-
     def _get_invoiceable_lines(self, final=False):
         lines = super()._get_invoiceable_lines(final=final)
         if not self.fiscal_operation_id:
diff --git a/l10n_br_stock_account/models/stock_picking.py b/l10n_br_stock_account/models/stock_picking.py
index c291f255a80b..f5ac1ff07730 100644
--- a/l10n_br_stock_account/models/stock_picking.py
+++ b/l10n_br_stock_account/models/stock_picking.py
@@ -21,16 +21,14 @@ def _fiscal_operation_domain(self):
         domain=lambda self: self._fiscal_operation_domain(),
     )
 
-    operation_name = fields.Char(
-        copy=False,
-    )
-
     comment_ids = fields.Many2many(
         comodel_name="l10n_br_fiscal.comment",
         relation="stock_picking_comment_rel",
         column1="picking_id",
         column2="comment_id",
         string="Comments",
+        compute="_compute_comment_ids",
+        store=True,
     )
 
     def _get_amount_lines(self):
diff --git a/l10n_br_stock_account/tests/common.py b/l10n_br_stock_account/tests/common.py
index c8bc0d298a9c..cf0286535d1c 100644
--- a/l10n_br_stock_account/tests/common.py
+++ b/l10n_br_stock_account/tests/common.py
@@ -16,7 +16,6 @@ def _change_user_company(self, company):
 
     def _run_picking_onchanges(self, record):
         result = super()._run_picking_onchanges(record)
-        record._onchange_fiscal_operation_id()
         return result
 
     def _run_line_onchanges(self, record):


Reviews:
user: login: antoniospneto
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-03-15T13:04:19Z
user: login: rvalyi
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-03-15T13:39:39Z
user: login: rvalyi
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-03-15T18:17:03Z
user: login: antoniospneto
state: APPROVED
author_association: CONTRIBUTOR
submitted_at: 2025-03-15T21:33:49Z
user: login: marcelsavegnago
state: APPROVED
author_association: MEMBER
submitted_at: 2025-03-17T12:06:48Z
user: login: mbcosta
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-03-17T22:22:45Z
user: login: rvalyi
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-03-17T22:28:05Z

Review comments:
diff_hunk: @@ -112,12 +112,21 @@ def _onchange_partner_id_fiscal(self):
                 # reload fiscal data, operation line, cfop, taxes, etc.
                 line._onchange_fiscal_operation_id()
 
-    @api.onchange("fiscal_operation_id")
-    def _onchange_fiscal_operation_id(self):
+    @api.depends("fiscal_operation_id")
+    def _compute_operation_name(self):
         if self.fiscal_operation_id:
             self.operation_name = self.fiscal_operation_id.name
+        else:
+            self.operation_name = False
             self.comment_ids = self.fiscal_operation_id.comment_ids
path: l10n_br_fiscal/models/document_mixin_methods.py
user: login: antoniospneto
body: ```suggestion

```
Acho que faltou remover essa linha, já que o comment_ids foi transformado no compute tbm
created_at: 2025-03-15T13:04:18Z
author_association: CONTRIBUTOR
diff_hunk: @@ -112,12 +112,21 @@ def _onchange_partner_id_fiscal(self):
                 # reload fiscal data, operation line, cfop, taxes, etc.
                 line._onchange_fiscal_operation_id()
 
-    @api.onchange("fiscal_operation_id")
-    def _onchange_fiscal_operation_id(self):
+    @api.depends("fiscal_operation_id")
+    def _compute_operation_name(self):
         if self.fiscal_operation_id:
             self.operation_name = self.fiscal_operation_id.name
+        else:
+            self.operation_name = False
             self.comment_ids = self.fiscal_operation_id.comment_ids
path: l10n_br_fiscal/models/document_mixin_methods.py
user: login: rvalyi
body: Erro de CTRl-C CTRL-V mesmo, já vou acertar valeu.
created_at: 2025-03-15T13:39:39Z
author_association: MEMBER
diff_hunk: @@ -112,12 +112,21 @@ def _onchange_partner_id_fiscal(self):
                 # reload fiscal data, operation line, cfop, taxes, etc.
                 line._onchange_fiscal_operation_id()
 
-    @api.onchange("fiscal_operation_id")
-    def _onchange_fiscal_operation_id(self):
+    @api.depends("fiscal_operation_id")
+    def _compute_operation_name(self):
         if self.fiscal_operation_id:
             self.operation_name = self.fiscal_operation_id.name
+        else:
+            self.operation_name = False
             self.comment_ids = self.fiscal_operation_id.comment_ids
path: l10n_br_fiscal/models/document_mixin_methods.py
user: login: rvalyi
body: arrumei
created_at: 2025-03-15T18:17:02Z
author_association: MEMBER
diff_hunk: @@ -16,7 +16,6 @@ def _change_user_company(self, company):
 
     def _run_picking_onchanges(self, record):
path: l10n_br_stock_account/tests/common.py
user: login: mbcosta
body: @rvalyi teve algum motivo para manter esse método? Porque com a remoção do onchange passou apenas a chamar o super então parece que todo esse método poderia ser removido, ou não?
created_at: 2025-03-17T22:22:44Z
author_association: CONTRIBUTOR
diff_hunk: @@ -16,7 +16,6 @@ def _change_user_company(self, company):
 
     def _run_picking_onchanges(self, record):
path: l10n_br_stock_account/tests/common.py
user: login: rvalyi
body: boa eu não tinha visto não foi mal. Se quiser mandar PR...
created_at: 2025-03-17T22:28:05Z
author_association: MEMBER

PR comments:
user: login: OCA-git-bot
created_at: 2025-03-15T01:31:02Z
body: Hi @renatonlima, @mbcosta,
some modules you are maintaining are being modified, check this out!
author_association: CONTRIBUTOR
user: login: rvalyi
created_at: 2025-03-17T16:07:44Z
body: /ocabot merge patch
author_association: MEMBER
user: login: OCA-git-bot
created_at: 2025-03-17T16:07:53Z
body: On my way to merge this fine PR!
Prepared branch [16.0-ocabot-merge-pr-3653-by-rvalyi-bump-patch](https://github.com/OCA/l10n-brazil/commits/16.0-ocabot-merge-pr-3653-by-rvalyi-bump-patch), awaiting test results.
author_association: CONTRIBUTOR
user: login: OCA-git-bot
created_at: 2025-03-17T16:32:16Z
body: Congratulations, your PR was merged at 39e74885e6f2cbb2010668a9be4483627700a428. Thanks a lot for contributing to OCA. ❤️
author_association: CONTRIBUTOR