<commit_before>

from odoo import fields, models, _
from odoo.exceptions import ValidationError


class AccountInvoice(models.Model):
    _inherit = "account.invoice"

    comunicazione_dati_iva_escludi = fields.Boolean(
        string='Exclude from invoices communication')

    def _compute_taxes_in_company_currency(self, vals):
        try:
            exchange_rate = (
                self.amount_total_signed /
                self.amount_total_company_signed)
        except ZeroDivisionError:
            exchange_rate = 1
        vals['ImponibileImporto'] = vals['ImponibileImporto'] / exchange_rate
        vals['Imposta'] = vals['Imposta'] / exchange_rate

    def _get_tax_comunicazione_dati_iva(self):
        self.ensure_one()
        fattura = self
        tax_model = self.env['account.tax']

        tax_lines = []
        tax_grouped = {}
        for tax_line in fattura.tax_line_ids:
            tax = tax_line.tax_id
            aliquota = tax.amount
            parent = tax_model.search([('children_tax_ids', 'in', [tax.id])])
            if parent:
                main_tax = parent
                aliquota = parent.amount
                if (
                    tax.cee_type and
                    tax.amount < 0 and
                    main_tax.kind_id.code == 'N6'
                ):
                    continue
            else:
                main_tax = tax
            kind_id = main_tax.kind_id.id
            payability = main_tax.payability
            imposta = tax_line.amount
            base = tax_line.base
            if main_tax.id not in tax_grouped:
                tax_grouped[main_tax.id] = {
                    'ImponibileImporto': 0,
                    'Imposta': imposta,
                    'Aliquota': aliquota,
                    'Natura_id': kind_id,
                    'EsigibilitaIVA': payability,
                    'Detraibile': 0.0,
                }
            else:
                tax_grouped[main_tax.id]['Imposta'] += imposta
            if tax.account_id:
                # account_id è valorizzato per la parte detraibile dell'imposta
                # In questa tax_line è presente il totale dell'imponibile
                # per l'imposta corrente
                tax_grouped[main_tax.id]['ImponibileImporto'] += base

        for tax_id in tax_grouped:
            tax = tax_model.browse(tax_id)
            vals = tax_grouped[tax_id]
            if tax.children_tax_ids:
                parte_detraibile = 0.0
                for child_tax in tax.children_tax_ids:
                    if child_tax.account_id:
                        parte_detraibile = child_tax.amount
                        break
                if vals['Aliquota'] and parte_detraibile:
                    vals['Detraibile'] = (
                        100 / (vals['Aliquota'] / parte_detraibile)
                    )
                else:
                    vals['Detraibile'] = 0.0<commit_msg>FIX l10n_it_invoices_data_communication: per le fatture passive con aliquota detraibile, il campo 'Detraibile' deve essere 100, invece di 0<commit_after>

from odoo import fields, models, _
from odoo.exceptions import ValidationError


class AccountInvoice(models.Model):
    _inherit = "account.invoice"

    comunicazione_dati_iva_escludi = fields.Boolean(
        string='Exclude from invoices communication')

    def _compute_taxes_in_company_currency(self, vals):
        try:
            exchange_rate = (
                self.amount_total_signed /
                self.amount_total_company_signed)
        except ZeroDivisionError:
            exchange_rate = 1
        vals['ImponibileImporto'] = vals['ImponibileImporto'] / exchange_rate
        vals['Imposta'] = vals['Imposta'] / exchange_rate

    def _get_tax_comunicazione_dati_iva(self):
        self.ensure_one()
        fattura = self
        tax_model = self.env['account.tax']

        tax_lines = []
        tax_grouped = {}
        for tax_line in fattura.tax_line_ids:
            tax = tax_line.tax_id
            aliquota = tax.amount
            parent = tax_model.search([('children_tax_ids', 'in', [tax.id])])
            if parent:
                main_tax = parent
                aliquota = parent.amount
                if (
                    tax.cee_type and
                    tax.amount < 0 and
                    main_tax.kind_id.code == 'N6'
                ):
                    continue
            else:
                main_tax = tax
            kind_id = main_tax.kind_id.id
            payability = main_tax.payability
            imposta = tax_line.amount
            base = tax_line.base
            if main_tax.id not in tax_grouped:
                tax_grouped[main_tax.id] = {
                    'ImponibileImporto': 0,
                    'Imposta': imposta,
                    'Aliquota': aliquota,
                    'Natura_id': kind_id,
                    'EsigibilitaIVA': payability,
                    'Detraibile': 0.0,
                }
                if fattura.type in ('in_invoice', 'in_refund'):
                    tax_grouped[main_tax.id]['Detraibile'] = 100.0
            else:
                tax_grouped[main_tax.id]['Imposta'] += imposta
            if tax.account_id:
                # account_id è valorizzato per la parte detraibile dell'imposta
                # In questa tax_line è presente il totale dell'imponibile
                # per l'imposta corrente
                tax_grouped[main_tax.id]['ImponibileImporto'] += base

        for tax_id in tax_grouped:
            tax = tax_model.browse(tax_id)
            vals = tax_grouped[tax_id]
            if tax.children_tax_ids:
                parte_detraibile = 0.0
                for child_tax in tax.children_tax_ids:
                    if child_tax.account_id:
                        parte_detraibile = child_tax.amount
                        break
                if vals['Aliquota'] and parte_detraibile:
                    vals['Detraibile'] = (
                        100 / (vals['Aliquota'] / parte_detraibile)
                    )
                else:
                    vals['Detraibile'] = 0.0
{'fraction_non_alphanumeric': 0.07819866688343359, 'fraction_numerical': 0.00585270687693058, 'mean_word_length': 1.4677095868431609, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3107583', 'n_tokens_mistral': 1835, 'n_tokens_neox': 1716, 'n_words': 343}