Skip to content
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

[14.0] nfe account move import runboat review #3397

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ repos:
language: fail
files: '[a-zA-Z0-9_]*/i18n/en\.po$'
- repo: https://github.com/oca/maintainer-tools
rev: d5fab7ee87fceee858a3d01048c78a548974d935
rev: bf9ecb9938b6a5deca0ff3d870fbd3f33341fded
hooks:
# update the NOT INSTALLABLE ADDONS section above
- id: oca-update-pre-commit-excluded-addons
Expand All @@ -60,12 +60,12 @@ repos:
- --if-source-changed
- --keep-source-digest
- repo: https://github.com/OCA/odoo-pre-commit-hooks
rev: v0.0.25
rev: v0.0.33
hooks:
- id: oca-checks-odoo-module
- id: oca-checks-po
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v2.1.2
rev: v4.0.0-alpha.8
hooks:
- id: prettier
name: prettier (with plugin-xml)
Expand All @@ -76,15 +76,15 @@ repos:
- --plugin=@prettier/plugin-xml
files: \.(css|htm|html|js|json|jsx|less|md|scss|toml|ts|xml|yaml|yml)$
- repo: https://github.com/pre-commit/mirrors-eslint
rev: v7.8.1
rev: v9.11.1
hooks:
- id: eslint
verbose: true
args:
- --color
- --fix
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
rev: v4.6.0
hooks:
- id: trailing-whitespace
# exclude autogenerated files
Expand All @@ -106,12 +106,12 @@ repos:
- id: mixed-line-ending
args: ["--fix=lf"]
- repo: https://github.com/asottile/pyupgrade
rev: v2.7.2
rev: v3.17.0
hooks:
- id: pyupgrade
args: ["--keep-percent-format"]
- repo: https://github.com/acsone/setuptools-odoo
rev: 3.1.8
rev: '3.3'
hooks:
- id: setuptools-odoo-make-default
- id: setuptools-odoo-get-requirements
Expand All @@ -121,13 +121,13 @@ repos:
- --header
- "# generated from manifests external_dependencies"
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.3
rev: v0.6.8
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- id: ruff-format
- repo: https://github.com/OCA/pylint-odoo
rev: 7.0.2
rev: v9.1.2
hooks:
- id: pylint_odoo
name: pylint with optional checks
Expand Down
1 change: 1 addition & 0 deletions l10n_br_account/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
# Wizards
"wizards/account_move_reversal_view.xml",
"wizards/wizard_document_status.xml",
"wizards/document_import_wizard_mixin.xml",
# Actions
"views/l10n_br_account_action.xml",
# Menus
Expand Down
1 change: 1 addition & 0 deletions l10n_br_account/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
from . import document_line
from . import account_incoterms
from . import ir_model_data
from . import account_journal
14 changes: 9 additions & 5 deletions l10n_br_account/models/account_journal.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# Copyright (C) 2009 - TODAY Renato Lima - Akretion
# Copyright 2023 Akretion (Raphaẽl Valyi <[email protected]>)
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from odoo import fields, models
from odoo import models


class AccountJournal(models.Model):
_inherit = "account.journal"

revenue_expense = fields.Boolean(
string="Gera Financeiro",
)
def create_invoice_from_attachment(self, attachment_ids=None):
if self.env.company.country_id.code != "BR" or len(attachment_ids) < 1:
return super().create_invoice_from_attachment(attachment_ids=attachment_ids)
attachments = self.env["ir.attachment"].browse(attachment_ids)
return self.env[
"l10n_br_fiscal.document.import.wizard.mixin"
]._get_importer_action(attachments)
17 changes: 17 additions & 0 deletions l10n_br_account/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,20 @@ def _compute_taxes_mapped(self, base_line):
icms_origin=base_line.icms_origin,
ind_final=base_line.ind_final,
)
if self.imported_document:
# then force the manual base and amount taxes:
for tax_item in balance_taxes_res["taxes"]:
if not tax_item.get("fiscal_tax_id") and not isinstance(
tax_item["fiscal_tax_id"], models.NewId
):
continue
tax_domain = (
self.env["l10n_br_fiscal.tax"]
.browse(tax_item["fiscal_tax_id"].origin)
.tax_domain
)
tax_item["base"] = getattr(base_line, "%s_base" % (tax_domain,))
tax_item["amount"] = getattr(base_line, "%s_value" % (tax_domain,))
return balance_taxes_res

def _preprocess_taxes_map(self, taxes_map):
Expand Down Expand Up @@ -791,4 +805,7 @@ def import_fiscal_document(
line_form.fiscal_operation_id = self.fiscal_operation_id
line_form.fiscal_document_line_id = line
move_form.save()
move = self.env["account.move"].browse(move_form.id)
for line in move.invoice_line_ids:
line.product_uom_id = line.fiscal_document_line_id.uom_id.id
return move_form
43 changes: 37 additions & 6 deletions l10n_br_account/models/account_move_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ def _inject_shadowed_fields(self, vals_list):

@api.model_create_multi
def create(self, vals_list):
inv_line_index = -1
for values in vals_list:
inv_line_index_old = inv_line_index
if values.get("product_id"):
inv_line_index += 1
if values.get("fiscal_document_line_id"):
fiscal_line_data = (
self.env["l10n_br_fiscal.document.line"]
Expand Down Expand Up @@ -198,17 +202,29 @@ def create(self, vals_list):
cfop_id = (
self.env["l10n_br_fiscal.cfop"].browse(cfop) if cfop else False
)

if move_id.imported_document and inv_line_index != inv_line_index_old:
# this will fix the the Debit side for imported fiscal documents
amount_tax_included = move_id.fiscal_document_id.fiscal_line_ids[
inv_line_index
].amount_tax_included_from_tax_values
amount_tax_not_included = (
move_id.fiscal_document_id.fiscal_line_ids[
inv_line_index
].amount_tax_excluded_from_tax_values
)
else:
amount_tax_included = values.get("amount_tax_included")
amount_tax_not_included = values.get("amount_tax_not_included")

values.update(
self._get_amount_credit_debit_model(
move_id,
exclude_from_invoice_tab=values.get(
"exclude_from_invoice_tab", False
),
amount_tax_included=values.get("amount_tax_included", 0),
amount_tax_not_included=values.get(
"amount_tax_not_included", 0
),
amount_tax_withholding=values.get("amount_tax_withholding", 0),
amount_tax_included=amount_tax_included,
amount_tax_not_included=amount_tax_not_included,
amount_total=fiscal_line.amount_total,
currency_id=move_id.currency_id,
company_id=move_id.company_id,
Expand Down Expand Up @@ -436,7 +452,7 @@ def _get_price_total_and_subtotal_model(

insurance_value = self.env.context.get("insurance_value", 0)
other_value = self.env.context.get("other_value", 0)
freight_value = self.env.context.get("other_value", 0)
freight_value = self.env.context.get("freight_value", 0)
ii_customhouse_charges = self.env.context.get("ii_customhouse_charges", 0)
icms_relief_value = self.env.context.get("icms_relief_value", 0)

Expand Down Expand Up @@ -498,6 +514,11 @@ def _onchange_fiscal_document_line_id(self):
# override the default product uom (set by the onchange):
self.product_uom_id = self.fiscal_document_line_id.uom_id.id

def _get_computed_taxes(self):
if self._is_imported() and self.fiscal_tax_ids and self.tax_ids:
return self.tax_ids
return super()._get_computed_taxes()

@api.onchange("fiscal_tax_ids")
def _onchange_fiscal_tax_ids(self):
"""Ao alterar o campo fiscal_tax_ids que contém os impostos fiscais,
Expand Down Expand Up @@ -604,6 +625,11 @@ def _get_amount_credit_debit(
# The formatting was a little strange, but I tried to make it as close as
# possible to the logic adopted by native Odoo.
# Example: _get_fields_onchange_subtotal
if self._is_imported():
# this will get the Credit side correct for imported fiscal documents:
amount_tax_included = self.amount_tax_included_from_tax_values
amount_tax_not_included = self.amount_tax_excluded_from_tax_values

return self._get_amount_credit_debit_model(
move_id=self.move_id if move_id is None else move_id,
exclude_from_invoice_tab=self.exclude_from_invoice_tab
Expand Down Expand Up @@ -675,3 +701,8 @@ def _get_amount_credit_debit_model(
"debit": balance > 0.0 and balance or 0.0,
"credit": balance < 0.0 and -balance or 0.0,
}

@api.constrains("product_uom_id")
def _check_product_uom_category_id(self):
not_imported = self.filtered(lambda line: not line._is_imported())
return super(AccountMoveLine, not_imported)._check_product_uom_category_id()
86 changes: 86 additions & 0 deletions l10n_br_account/models/fiscal_document_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Copyright (C) 2021 - TODAY Gabriel Cardoso de Faria - Kmee
# Copyright (C) 2023 - TODAY Raphaël Valyi - Akretion
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from odoo import api, fields, models


class FiscalDocumentLine(models.Model):
_inherit = "l10n_br_fiscal.document.line"

account_line_ids = fields.One2many(
comodel_name="account.move.line",
inverse_name="fiscal_document_line_id",
string="Invoice Lines",
)

# proxy fields to enable writing the related (shadowed) fields
# to the fiscal doc line from the aml through the _inherits system
# despite they have the same names.
fiscal_name = fields.Text(
string="Fiscal Name",
related="name",
readonly=False,
)
fiscal_product_id = fields.Many2one(
string="Fiscal Product",
related="product_id",
readonly=False,
)
fiscal_quantity = fields.Float(
string="Fiscal Quantity",
related="quantity",
readonly=False,
)
fiscal_price_unit = fields.Float(
string="Fiscal Price Unit",
related="price_unit",
readonly=False,
)
amount_tax_included_from_tax_values = fields.Float(
compute="_compute_amount_tax_from_tax_values",
help="Used on imported or edited fiscal documents",
)
amount_tax_excluded_from_tax_values = fields.Float(
compute="_compute_amount_tax_excluded_from_tax_values",
help="Used on imported or edited fiscal documents",
)

@api.model_create_multi
def create(self, vals_list):
"""
It's not allowed to create a fiscal document line without a document_id anyway.
But instead of letting Odoo crash in this case we simply avoid creating the
record. This makes it possible to create an account.move.line without
a fiscal_document_line_id: Odoo will write NULL as the value in this case.
This is a requirement to allow account moves without fiscal documents despite
the _inherits system.
"""

if self._context.get("create_from_move_line"):
if not any(vals.get("document_id") for vals in vals_list):
return []

return super().create(vals_list)

def _compute_amount_tax_from_tax_values(self, included=True):
included_taxes = (
self.env["l10n_br_fiscal.tax.group"]
.search([("tax_include", "=", included)])
.mapped("tax_domain")
)
for line in self:
for tax in included_taxes:
if not hasattr(line, "%s_value" % (tax,)):
continue
if included:
line.amount_tax_included_from_tax_values += getattr(
line, "%s_value" % (tax,)
)
else:
line.amount_tax_excluded_from_tax_values += getattr(
line, "%s_value" % (tax,)
)

def _compute_amount_tax_excluded_from_tax_values(self):
self._compute_amount_tax_from_tax_values(included=False)
4 changes: 4 additions & 0 deletions l10n_br_account/wizards/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from . import account_move_reversal
from . import wizard_document_cancel
from . import wizard_document_correction
from . import wizard_document_invalidate
from . import document_import_wizard_mixin
Loading
Loading