<commit_before># Copyright (C) 2019  <NAME> - Akretion <<EMAIL>>
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from lxml import etree

from odoo import api, models
from odoo.osv.orm import setup_modifiers

from ..constants.icms import ICMS_BASE_TYPE_DEFAULT, ICMS_ST_BASE_TYPE_DEFAULT
from .tax import TAX_DICT_VALUES

FISCAL_TAX_ID_FIELDS = [
    "cofins_tax_id",
    "cofins_wh_tax_id",
    "cofinsst_tax_id",
    "csll_tax_id",
    "csll_wh_tax_id",
    "icms_tax_id",
    "icmsfcp_tax_id",
    "icmssn_tax_id",
    "icmsst_tax_id",
    "ii_tax_id",
    "inss_tax_id",
    "inss_wh_tax_id",
    "ipi_tax_id",
    "irpj_tax_id",
    "irpj_wh_tax_id",
    "issqn_tax_id",
    "issqn_wh_tax_id",
    "pis_tax_id",
    "pis_wh_tax_id",
    "pisst_tax_id",
]

FISCAL_CST_ID_FIELDS = [
    "icms_cst_id",
    "ipi_cst_id",
    "pis_cst_id",
    "pisst_cst_id",
    "cofins_cst_id",
    "cofinsst_cst_id",
]


class FiscalDocumentLineMixinMethods(models.AbstractModel):
    _name = "l10n_br_fiscal.document.line.mixin.methods"
    _description = "Document Fiscal Mixin Methods"

    @api.model
    def fiscal_form_view(self, form_view_arch):
        try:

            fiscal_view = self.env.ref("l10n_br_fiscal.document_fiscal_line_mixin_form")

            view_template_tags = {
                "group": ["fiscal_fields"],
                "page": ["fiscal_taxes", "fiscal_line_extra_info"],
            }

            fsc_doc = etree.fromstring(fiscal_view["arch"])
            doc = etree.fromstring(form_view_arch)

            for tag, tag_names in view_template_tags.items():
                for tag_name in tag_names:
                    fiscal_node = fsc_doc.xpath(
                        "//{}[@name='{}']".format(tag, tag_name)
                    )[0]

                    doc_node = doc.xpath("//{}[@name='{}']".format(tag, tag_name))[0]

                    setup_modifiers(doc_node)
                    for n in doc_node.getiterator():
                        setup_modifiers(n)

                    doc_node.getparent().replace(doc_node, fiscal_node)

            form_view_arch = etree.tostring(doc, encoding="unicode")
        except Exception:
            return form_view_arch

        return form_view_arch

    @api.model
    def fields_view_get(
        self, view_id=None, view_type="form", toolbar=False, submenu=False
    ):
        model_view = super().fields_view_get(view_id, view_type, toolbar, submenu)

        if view_type == "form":
            model_view["arch"] = self.fiscal_form_view(model_view["arch"])

        View = self.env["ir.ui.view"]
        # Override context for postprocessing
        if view_id and model_view.get("base_model", self._name) != self._name:
            View = View.with_context(base_model_name=model_view["base_model"])

        # Apply post processing, groups and modifiers etc...
        xarch, xfields = View.postprocess_and_fields(
            self._name, etree.fromstring(model_view["arch"]), view_id
        )
        model_view["arch"] = xarch
        model_view["fields"] = xfields
        return model_view

    @api.depends(
        "fiscal_price",
        "discount_value",
        "insurance_value",
        "other_value",
        "freight_value",
        "fiscal_quantity",
        "amount_tax_not_included",
        "uot_id",
        "product_id",
        "partner_id",
        "company_id",
    )
    def _compute_amounts(self):
        for record in self:
            round_curr = record.currency_id.round
            # Valor dos produtos
            record.price_gross = round_curr(record.price_unit * record.quantity)

            record.amount_untaxed = record.price_gross - record.discount_value

            record.amount_fiscal = (
                round_curr(record.fiscal_price * record.fiscal_quantity)
                - record.discount_value
            )<commit_msg>add update_taxes() call in fiscal document<commit_after># Copyright (C) 2019  <NAME> - Akretion <<EMAIL>>
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from lxml import etree

from odoo import api, models
from odoo.osv.orm import setup_modifiers

from ..constants.icms import ICMS_BASE_TYPE_DEFAULT, ICMS_ST_BASE_TYPE_DEFAULT
from .tax import TAX_DICT_VALUES

FISCAL_TAX_ID_FIELDS = [
    "cofins_tax_id",
    "cofins_wh_tax_id",
    "cofinsst_tax_id",
    "csll_tax_id",
    "csll_wh_tax_id",
    "icms_tax_id",
    "icmsfcp_tax_id",
    "icmssn_tax_id",
    "icmsst_tax_id",
    "ii_tax_id",
    "inss_tax_id",
    "inss_wh_tax_id",
    "ipi_tax_id",
    "irpj_tax_id",
    "irpj_wh_tax_id",
    "issqn_tax_id",
    "issqn_wh_tax_id",
    "pis_tax_id",
    "pis_wh_tax_id",
    "pisst_tax_id",
]

FISCAL_CST_ID_FIELDS = [
    "icms_cst_id",
    "ipi_cst_id",
    "pis_cst_id",
    "pisst_cst_id",
    "cofins_cst_id",
    "cofinsst_cst_id",
]


class FiscalDocumentLineMixinMethods(models.AbstractModel):
    _name = "l10n_br_fiscal.document.line.mixin.methods"
    _description = "Document Fiscal Mixin Methods"

    @api.model
    def fiscal_form_view(self, form_view_arch):
        try:

            fiscal_view = self.env.ref("l10n_br_fiscal.document_fiscal_line_mixin_form")

            view_template_tags = {
                "group": ["fiscal_fields"],
                "page": ["fiscal_taxes", "fiscal_line_extra_info"],
            }

            fsc_doc = etree.fromstring(fiscal_view["arch"])
            doc = etree.fromstring(form_view_arch)

            for tag, tag_names in view_template_tags.items():
                for tag_name in tag_names:
                    fiscal_node = fsc_doc.xpath(
                        "//{}[@name='{}']".format(tag, tag_name)
                    )[0]

                    doc_node = doc.xpath("//{}[@name='{}']".format(tag, tag_name))[0]

                    setup_modifiers(doc_node)
                    for n in doc_node.getiterator():
                        setup_modifiers(n)

                    doc_node.getparent().replace(doc_node, fiscal_node)

            form_view_arch = etree.tostring(doc, encoding="unicode")
        except Exception:
            return form_view_arch

        return form_view_arch

    @api.model
    def fields_view_get(
        self, view_id=None, view_type="form", toolbar=False, submenu=False
    ):
        model_view = super().fields_view_get(view_id, view_type, toolbar, submenu)

        if view_type == "form":
            model_view["arch"] = self.fiscal_form_view(model_view["arch"])

        View = self.env["ir.ui.view"]
        # Override context for postprocessing
        if view_id and model_view.get("base_model", self._name) != self._name:
            View = View.with_context(base_model_name=model_view["base_model"])

        # Apply post processing, groups and modifiers etc...
        xarch, xfields = View.postprocess_and_fields(
            self._name, etree.fromstring(model_view["arch"]), view_id
        )
        model_view["arch"] = xarch
        model_view["fields"] = xfields
        return model_view

    @api.depends(
        "fiscal_price",
        "discount_value",
        "insurance_value",
        "other_value",
        "freight_value",
        "fiscal_quantity",
        "amount_tax_not_included",
        "uot_id",
        "product_id",
        "partner_id",
        "company_id",
    )
    def _compute_amounts(self):
        for record in self:
            record._update_taxes()
            round_curr = record.currency_id.round
            # Valor dos produtos
            record.price_gross = round_curr(record.price_unit * record.quantity)

            record.amount_untaxed = record.price_gross - record.discount_value

            record.amount_fiscal = (
                round_curr(record.fiscal_price * record.fiscal_quantity)
                - record.discount_value
            )
{'fraction_non_alphanumeric': 0.10998719590268886, 'fraction_numerical': 0.0033290653008962866, 'mean_word_length': 2.340889649272883, 'pattern_counts': {'":': 6, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2650582', 'n_tokens_mistral': 2789, 'n_tokens_neox': 2703, 'n_words': 416}