From 6dfc46e40a2d09694dd61d9346f3b6549054ee34 Mon Sep 17 00:00:00 2001 From: Magno Costa Date: Fri, 28 Jun 2024 18:01:21 -0300 Subject: [PATCH 1/2] [REF] l10n_br_stock_account: Adapt code for the cases without Fiscal Operation, if a Purchase or Sale Order are defined without it the Picking should be the same. --- l10n_br_stock_account/models/stock_move.py | 15 ++++---- l10n_br_stock_account/models/stock_picking.py | 38 ++++++++++++++----- 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/l10n_br_stock_account/models/stock_move.py b/l10n_br_stock_account/models/stock_move.py index 48dfd11b4c78..784e4c30e3b7 100644 --- a/l10n_br_stock_account/models/stock_move.py +++ b/l10n_br_stock_account/models/stock_move.py @@ -225,15 +225,16 @@ def _get_price_unit(self): def _onchange_product_id_fiscal(self): # Metodo super altera o price_unit # TODO: Isso deveria ser resolvido no metodo principal? - price_unit = self.price_unit - result = super()._onchange_product_id_fiscal() - # Valor informado pelo usuario tem prioridade - if self.product_id and price_unit == 0.0: - price_unit = self._get_price_unit() + if self.picking_id.fiscal_operation_id: + price_unit = self.price_unit + result = super()._onchange_product_id_fiscal() + # Valor informado pelo usuario tem prioridade + if self.product_id and price_unit == 0.0: + price_unit = self._get_price_unit() - self.price_unit = price_unit + self.price_unit = price_unit - return result + return result def _split(self, qty, restrict_partner_id=False): new_moves_vals = super()._split(qty, restrict_partner_id) diff --git a/l10n_br_stock_account/models/stock_picking.py b/l10n_br_stock_account/models/stock_picking.py index f13f0f372726..6367a774206c 100644 --- a/l10n_br_stock_account/models/stock_picking.py +++ b/l10n_br_stock_account/models/stock_picking.py @@ -11,15 +11,35 @@ class StockPicking(models.Model): @api.model def _default_fiscal_operation(self): company = self.env.company - fiscal_operation = company.stock_fiscal_operation_id - picking_type_id = self.env.context.get("default_picking_type_id") - if picking_type_id: - picking_type = self.env["stock.picking.type"].browse(picking_type_id) - fiscal_operation = picking_type.fiscal_operation_id or ( - company.stock_in_fiscal_operation_id - if picking_type.code == "incoming" - else company.stock_out_fiscal_operation_id - ) + fiscal_operation = False + if self.env.company.country_id == self.env.ref("base.br"): + fiscal_operation = company.stock_fiscal_operation_id + picking_type_id = self.env.context.get("default_picking_type_id") + if picking_type_id: + picking_type = self.env["stock.picking.type"].browse(picking_type_id) + fiscal_operation = picking_type.fiscal_operation_id or ( + company.stock_in_fiscal_operation_id + if picking_type.code == "incoming" + else company.stock_out_fiscal_operation_id + ) + + # Casos onde o usuário definiu o Pedido de Compra/Venda como + # 'Sem Operação Fiscal' + if hasattr(self, "purchase_id"): + if not self.purchase_id.fiscal_operation_id: + fiscal_operation = False + if hasattr(self, "sale_id"): + # Necessário para evitar o erro quando o l10n_br_purchase_stock + # é instalado: + # /l10n_br_stock_account/models/stock_picking.py", line 34, + # in _default_fiscal_operation + # if not self.sale_id.fiscal_operation_id: + # AttributeError: 'sale.order' object has no attribute + # 'fiscal_operation_id' + if hasattr(self.sale_id, "fiscal_operation_id"): + if not self.sale_id.fiscal_operation_id: + fiscal_operation = False + return fiscal_operation @api.model From c6f09e018ae2e51d821c6d9458caac48f53ac0d5 Mon Sep 17 00:00:00 2001 From: Magno Costa Date: Fri, 28 Jun 2024 18:03:21 -0300 Subject: [PATCH 2/2] [REF] l10n_br_stock_account: Unnecessary call methods because the _onchange_product_id_fiscal already call it. --- l10n_br_stock_account/tests/common.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/l10n_br_stock_account/tests/common.py b/l10n_br_stock_account/tests/common.py index c0a5b5767abb..b54790793991 100644 --- a/l10n_br_stock_account/tests/common.py +++ b/l10n_br_stock_account/tests/common.py @@ -23,8 +23,6 @@ def _run_fiscal_line_onchanges(self, record): # Stock Move record._onchange_product_id_fiscal() - record._onchange_fiscal_operation_id() - record._onchange_fiscal_operation_line_id() record._onchange_fiscal_taxes() record._onchange_product_quantity()