<commit_before># -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, models, _
from odoo.exceptions import UserError


class AccountPartialReconcileCashBasis(models.Model):
    _inherit = 'account.partial.reconcile'

    def _get_tax_cash_basis_lines(self, value_before_reconciliation):
        # Search in account_move if we have any taxes account move lines
        tax_group = {}
        total_by_cash_basis_account = {}
        line_to_create = []
        move_date = self.debit_move_id.date
        for move in (self.debit_move_id.move_id, self.credit_move_id.move_id):
            if move_date < move.date:
                move_date = move.date
            for line in move.line_ids:
                #TOCHECK: normal and cash basis taxes shoudn't be mixed together (on the same invoice line for example) as it will
                #create reporting issues. Not sure of the behavior to implement in that case, though.
                # amount to write is the current cash_basis amount minus the one before the reconciliation
                matched_percentage = value_before_reconciliation[move.id]
                amount = line.currency_id.round((line.credit_cash_basis - line.debit_cash_basis) - (line.credit - line.debit) * matched_percentage)
                if not line.tax_exigible:
                    if line.tax_line_id and line.tax_line_id.use_cash_basis:
                        # group by line account
                        acc = line.account_id.id
                        if tax_group.get(acc, False):
                            tax_group[acc] += amount
                        else:
                            tax_group[acc] = amount
                        # Group by cash basis account and tax
                        acc = line.tax_line_id.cash_basis_account.id
                        key = (acc, line.tax_line_id.id)
                        if key in total_by_cash_basis_account:
                            total_by_cash_basis_account[key] += amount
                        else:
                            total_by_cash_basis_account[key] = amount
                    if any([tax.use_cash_basis for tax in line.tax_ids]):
                        for tax in line.tax_ids:
                            line_to_create.append((0, 0, {
                                'name': '/',
                                'debit': line.currency_id.round(line.debit_cash_basis - line.debit * matched_percentage),
                                'credit': line.currency_id.round(line.credit_cash_basis - line.credit * matched_percentage),
                                'account_id': line.account_id.id,
                                'tax_ids': [(6, 0, [tax.id])],
                                'tax_exigible': True,
                            }))
                            line_to_create.append((0, 0, {
                                'name': '/',
                                'credit': line.currency_id.round(line.debit_cash_basis - line.debit * matched_percentage),
                                'debit': line.currency_id.round(line.credit_cash_basis - line.credit * matched_percentage),
                                'account_id': line.account_id.id,
                                'tax_exigible': True,
                            }))

        for k, v in tax_group.items():
            line_to_create.append((0, 0, {
                'name': '/',
                'debit': v if v > 0 else 0.0,
                'credit': abs(v) if v < 0 else 0.0,
                'account_id': k,
                'tax_exigible': True,
            }))

        # Create counterpart vals
        for key, v in total_by_cash_basis_account.items():
            k, tax_id = key
            line_to_create.append((0, 0, {
                'name': '/',
                'debit': abs(v) if v < 0 else 0.0,
                'credit': v if v > 0 else 0.0,
                'account_id': k,
                'tax_line_id': tax_id,
                'tax_exigible': True,
            }))
        return line_to_create, move_date

    def create_tax_cash_basis_entry(self, value_before_reconciliation):
        line_to_create, move_date = self._get_tax_cash_basis_lines(value_before_reconciliation)
        if len(line_to_create) > 0:
            # Check if company_journal for cash basis is set if not, raise exception
            if not self.company_id.tax_cash_basis_journal_id:
                raise UserError(_('There is no tax cash basis journal defined '
                                  'for this company: "%s" \nConfigure it in Accounting/Configuration/Settings') %
                                (self.company_id.name))
            move_vals = {
                'journal_id': self.company_id.tax_cash_basis_journal_id.id,
                'line_ids': line_to_create,
                'tax_cash_basis_rec_id': self.id<commit_msg>[FIX] account_tax_cash_basis: _get_tax_cash_basis_lines<commit_after># -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, models, _
from odoo.exceptions import UserError


class AccountPartialReconcileCashBasis(models.Model):
    _inherit = 'account.partial.reconcile'

    def _get_tax_cash_basis_lines(self, value_before_reconciliation):
        # Search in account_move if we have any taxes account move lines
        tax_group = {}
        total_by_cash_basis_account = {}
        line_to_create = []
        move_date = self.debit_move_id.date
        for move in (self.debit_move_id.move_id, self.credit_move_id.move_id):
            if move_date < move.date:
                move_date = move.date
            for line in move.line_ids:
                #TOCHECK: normal and cash basis taxes shoudn't be mixed together (on the same invoice line for example) as it will
                #create reporting issues. Not sure of the behavior to implement in that case, though.
                # amount to write is the current cash_basis amount minus the one before the reconciliation
                matched_percentage = value_before_reconciliation[move.id]
                amount = line.currency_id.round((line.credit_cash_basis - line.debit_cash_basis) - (line.credit - line.debit) * matched_percentage)
                if not line.tax_exigible:
                    if line.tax_line_id and line.tax_line_id.use_cash_basis:
                        # group by line account
                        acc = line.account_id.id
                        if tax_group.get(acc, False):
                            tax_group[acc] += amount
                        else:
                            tax_group[acc] = amount
                        # Group by cash basis account and tax
                        acc = line.tax_line_id.cash_basis_account.id
                        key = (acc, line.tax_line_id.id)
                        if key in total_by_cash_basis_account:
                            total_by_cash_basis_account[key] += amount
                        else:
                            total_by_cash_basis_account[key] = amount
                    if any([tax.use_cash_basis for tax in line.tax_ids]):
                        for tax in line.tax_ids:
                            line_to_create.append((0, 0, {
                                'name': '/',
                                'debit': line.currency_id.round(line.debit_cash_basis - line.debit * matched_percentage),
                                'credit': line.currency_id.round(line.credit_cash_basis - line.credit * matched_percentage),
                                'account_id': line.account_id.id,
                                'tax_ids': [(6, 0, [tax.id])],
                                'tax_exigible': True,
                            }))
                            line_to_create.append((0, 0, {
                                'name': '/',
                                'credit': line.currency_id.round(line.debit_cash_basis - line.debit * matched_percentage),
                                'debit': line.currency_id.round(line.credit_cash_basis - line.credit * matched_percentage),
                                'account_id': line.account_id.id,
                                'tax_exigible': True,
                            }))

        for k, v in tax_group.items():
            line_to_create.append((0, 0, {
                'name': '/',
                'debit': v if v > 0 else 0.0,
                'credit': abs(v) if v < 0 else 0.0,
                'account_id': k,
                'tax_exigible': True,
            }))

        # Create counterpart vals
        for key, v in total_by_cash_basis_account.items():
            k, tax_id = key
            # Only entries with cash flow must be created
            if not self.company_id.currency_id.is_zero(v):
                line_to_create.append((0, 0, {
                    'name': '/',
                    'debit': abs(v) if v < 0 else 0.0,
                    'credit': v if v > 0 else 0.0,
                    'account_id': k,
                    'tax_line_id': tax_id,
                    'tax_exigible': True,
                }))
        return line_to_create, move_date

    def create_tax_cash_basis_entry(self, value_before_reconciliation):
        line_to_create, move_date = self._get_tax_cash_basis_lines(value_before_reconciliation)
        if len(line_to_create) > 0:
            # Check if company_journal for cash basis is set if not, raise exception
            if not self.company_id.tax_cash_basis_journal_id:
                raise UserError(_('There is no tax cash basis journal defined '
                                  'for this company: "%s" \nConfigure it in Accounting/Configuration/Settings') %
                                (self.company_id.name))
            move_vals = {
                'journal_id': self.company_id.tax_cash_basis_journal_id.id,
                'line_ids': line_to_create,
                'tax_cash_basis_rec_id': self.id
{'fraction_non_alphanumeric': 0.08182454736420926, 'fraction_numerical': 0.004801440432129639, 'mean_word_length': 1.4723046488625124, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3109009', 'n_tokens_mistral': 2922, 'n_tokens_neox': 2666, 'n_words': 642}