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

[16.0][FIX] account_reconcile_oca: Fixed exchange move lines never input analytic #772

Closed
wants to merge 1 commit into from
Closed
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
66 changes: 66 additions & 0 deletions account_reconcile_oca/models/account_move_line.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright 2023 Dixmit
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

import json

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

Expand Down Expand Up @@ -32,3 +34,67 @@
lambda r: not r.reconciled
).ids
return action

def _prepare_exchange_difference_move_vals(
self, amounts_list, company=None, exchange_date=None, **kwargs
):
# This updates the analytic_distribution of the exchange lines,
# otherwise the move lines originated from this function
# Will never have analytic
move_vals = super()._prepare_exchange_difference_move_vals(
amounts_list, company=company, exchange_date=exchange_date, **kwargs
)

exchange_analytic = {}

for move, _sequence in move_vals["to_reconcile"]:
# Looks for the data of account.bank.statement.line
# That has the analytic_distribution of the exchange move_line
self.env.cr.execute(
r"""
SELECT reconcile_data
FROM account_bank_statement_line
WHERE EXISTS (
SELECT 1
FROM jsonb_array_elements(reconcile_data::jsonb->'data') AS elem
WHERE (elem->>'id') ~ '^\d+$' -- Verify that it's a number
AND (elem->>'id')::int = %s
);
""",
(move.id,),
)
reconcile_data = self.env.cr.fetchall()
if reconcile_data:
parsed_data = json.loads(reconcile_data[0][0])
data_items = parsed_data.get("data", [])

Check warning on line 69 in account_reconcile_oca/models/account_move_line.py

View check run for this annotation

Codecov / codecov/patch

account_reconcile_oca/models/account_move_line.py#L68-L69

Added lines #L68 - L69 were not covered by tests

# Checks the exchange move of the reconcile_data
result = next(

Check warning on line 72 in account_reconcile_oca/models/account_move_line.py

View check run for this annotation

Codecov / codecov/patch

account_reconcile_oca/models/account_move_line.py#L72

Added line #L72 was not covered by tests
(
item
for item in data_items
if item.get("is_exchange_counterpart")
and item.get("original_exchange_line_id") == move.id
),
None,
)

if result:
# Maps the financial account and amount with his analytic
key = f"{result['account_id'][0]}|{abs(result['net_amount'])}"
exchange_analytic[key] = result["analytic_distribution"]

Check warning on line 85 in account_reconcile_oca/models/account_move_line.py

View check run for this annotation

Codecov / codecov/patch

account_reconcile_oca/models/account_move_line.py#L84-L85

Added lines #L84 - L85 were not covered by tests

for line in move_vals["move_vals"]["line_ids"]:
line_data = line[2]
account_id = line_data["account_id"]
credit = line_data["credit"]
debit = line_data["debit"]

key = f"{account_id}|{credit if credit > 0 else debit}"

analytic_distribution = exchange_analytic.get(key, None)

if analytic_distribution:
line_data.update({"analytic_distribution": analytic_distribution})

Check warning on line 98 in account_reconcile_oca/models/account_move_line.py

View check run for this annotation

Codecov / codecov/patch

account_reconcile_oca/models/account_move_line.py#L98

Added line #L98 was not covered by tests

return move_vals
Loading