Skip to content

Commit

Permalink
Merge PR #3327 into 14.0
Browse files Browse the repository at this point in the history
Signed-off-by rvalyi
  • Loading branch information
OCA-git-bot committed Oct 3, 2024
2 parents 4809075 + 0a4cfe0 commit 3baa7ae
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 27 deletions.
1 change: 1 addition & 0 deletions l10n_br_purchase_stock/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
from . import stock_rule
from . import res_config_settings
from . import stock_move
from . import stock_picking
23 changes: 23 additions & 0 deletions l10n_br_purchase_stock/models/stock_picking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright (C) 2024-Today - Akretion (<http://www.akretion.com>).
# @author Magno Costa <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models


class StockPicking(models.Model):
_inherit = "stock.picking"

def _get_default_fiscal_operation(self):
fiscal_operation = super()._get_default_fiscal_operation()
if self.purchase_id:
if self.purchase_id.fiscal_operation_id:
# Evita a inconsistência de ter o Pedido de Compras com uma
# OP Fiscal e a Ordem de Seleção outra, quando o campo
# invoice_state é alterado, o usuário pode alterar o campo
# mas dessa forma forçamos a decisão de não usar a mesma
# do Pedido.
if fiscal_operation != self.purchase_id.fiscal_operation_id:
fiscal_operation = self.purchase_id.fiscal_operation_id

return fiscal_operation
21 changes: 21 additions & 0 deletions l10n_br_purchase_stock/tests/test_l10n_br_purchase_stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# Renato Lima <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo.tests import Form

from odoo.addons.l10n_br_stock_account.tests.common import TestBrPickingInvoicingCommon


Expand Down Expand Up @@ -446,3 +448,22 @@ def test_purchase_with_partner_to_shipping(self):
invoice.partner_id,
"The Invoice Partner and Partner to Shipping should be the same.",
)

def test_form_stock_picking(self):
"""Test Stock Picking with Form"""
purchase = self.env.ref("l10n_br_purchase_stock.main_po_only_products_1")
purchase.button_confirm()
picking = purchase.picking_ids
self.picking_move_state(picking)
picking_form = Form(picking)

# Alterando a OP Fiscal apenas para forçar a diferença
picking.company_id.stock_in_fiscal_operation_id = False

# Apesar do metodo onchange retornar uma OP Fiscal padrão,
# quando existe um Pedido de Venda associado deve usar retornar
# a mesma OP Fiscal do Pedido.
picking_form.invoice_state = "none"
picking_form.invoice_state = "2binvoiced"
self.assertEqual(purchase.fiscal_operation_id, picking.fiscal_operation_id)
picking_form.save()
14 changes: 14 additions & 0 deletions l10n_br_sale_stock/models/stock_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,17 @@ def _get_fiscal_partner(self):
if partner.id != partner_to_invoice:
partner = self.env["res.partner"].browse(partner_to_invoice)
return partner

def _get_default_fiscal_operation(self):
fiscal_operation = super()._get_default_fiscal_operation()
if self.sale_id:
if self.sale_id.fiscal_operation_id:
# Evita a inconsistência de ter o Pedido de Vendas com uma
# OP Fiscal e a Ordem de Seleção outra, quando o campo
# invoice_state é alterado, o usuário pode alterar o campo
# mas dessa forma forçamos a decisão de não usar a mesma
# do Pedido.
if fiscal_operation != self.sale_id.fiscal_operation_id:
fiscal_operation = self.sale_id.fiscal_operation_id

return fiscal_operation
51 changes: 42 additions & 9 deletions l10n_br_sale_stock/tests/test_sale_stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,15 +419,6 @@ def test_lucro_presumido_company(self):
sale_order = sale_order_form.save()
sale_order.incoterm = self.env.ref("account.incoterm_FOB")

if hasattr(self.env["sale.order"], "payment_mode_id"):
payment_mode = self.env["account.payment.mode"].search(
[
("payment_type", "=", "inbound"),
("company_id", "=", self.env.company.id),
],
limit=1,
)
sale_order.payment_mode_id = payment_mode
sale_order.action_confirm()
picking = sale_order_1.picking_ids
self.picking_move_state(picking)
Expand Down Expand Up @@ -479,3 +470,45 @@ def test_button_create_bill_in_view(self):
"Field to make invisible the Button Create Bill should be"
" False when the Sale Order has Service and Product.",
)

def test_compatible_with_international_case(self):
"""
Test compatibility with international cases or
without Fiscal Operation.
"""
so_international = self.env.ref("sale.sale_order_3")
so_international.fiscal_operation_id = False
so_international.action_confirm()
picking = so_international.picking_ids
self.picking_move_state(picking)
invoice = self.create_invoice_wizard(picking)
invoice.action_post()
# Caso Internacional não deve ter Documento Fiscal associado
self.assertFalse(
invoice.fiscal_document_id,
"International case should not has Fiscal Document.",
)
# Teste Retorno
picking_devolution = self.return_picking_wizard(picking)
invoice_devolution = self.create_invoice_wizard(picking_devolution)
self.assertFalse(
invoice_devolution.fiscal_document_id,
"International case should not has Fiscal Document.",
)

def test_form_stock_picking(self):
"""Test Stock Picking with Form"""

sale_order = self.env.ref("l10n_br_sale_stock.main_so_l10n_br_sale_stock_1")
sale_order.action_confirm()
picking = sale_order.picking_ids
self.picking_move_state(picking)
picking_form = Form(picking)

# Apesar do metodo onchange retornar uma OP Fiscal padrão,
# quando existe um Pedido de Venda associado deve usar retornar
# a mesma OP Fiscal do Pedido.
picking_form.invoice_state = "none"
picking_form.invoice_state = "2binvoiced"
self.assertEqual(sale_order.fiscal_operation_id, picking.fiscal_operation_id)
picking_form.save()
69 changes: 51 additions & 18 deletions l10n_br_stock_account/models/stock_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,6 @@ class StockPicking(models.Model):
_name = "stock.picking"
_inherit = [_name, "l10n_br_fiscal.document.mixin"]

@api.model
def _default_fiscal_operation(self):
company = self.env.company
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
)

return fiscal_operation

@api.model
def _fiscal_operation_domain(self):
# TODO Check in context to define in or out move default.
Expand All @@ -35,7 +18,6 @@ def _fiscal_operation_domain(self):
comodel_name="l10n_br_fiscal.operation",
readonly=True,
states={"draft": [("readonly", False)]},
default=_default_fiscal_operation,
domain=lambda self: self._fiscal_operation_domain(),
)

Expand Down Expand Up @@ -139,3 +121,54 @@ def _get_fiscal_partner(self):
partner.address_get(["invoice"]).get("invoice")
)
return partner

def _get_default_fiscal_operation(self):
"""
Meant to be overriden in modules such as
l10n_br_sale_stock
l10n_br_purchase_stock
"""
company = self.env.company
fiscal_operation = company.stock_fiscal_operation_id
picking_type_id = self.picking_type_id.id
if not picking_type_id:
# Quando isso é necessário? Testes não passam aqui,
# dependendo ver de remover
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
)

return fiscal_operation

@api.onchange("invoice_state")
def _onchange_invoice_state(self):
for record in self:
# TODO: Na v16 chamar o super() o update_invoice_state já é
# chamado https://github.com/OCA/account-invoicing/blob/16.0/
# stock_picking_invoicing/models/stock_picking.py#L47
record._update_invoice_state(record.invoice_state)
record.mapped("move_lines")._update_invoice_state(record.invoice_state)

if record.partner_id and record.env.company.country_id == record.env.ref(
"base.br"
):
fiscal_operation = record._get_default_fiscal_operation()
if fiscal_operation:
record.fiscal_operation_id = fiscal_operation

def set_to_be_invoiced(self):
"""
Update invoice_state of current pickings to "2binvoiced".
:return: dict
"""
# Necessário para preencher a OP Fiscal Padrão quando
# ao inves de selecionar o invoice_state pelo campo
# é feito pelo botão
self._onchange_invoice_state()

return super().set_to_be_invoiced()

0 comments on commit 3baa7ae

Please sign in to comment.