| 1 |
from odoo import models |
| 2 |
from odoo.tools import float_compare, float_is_zero, float_round |
| 3 |
|
| 4 |
|
| 5 |
class AccountAnalyticPlan(models.Model): |
| 6 |
_inherit = 'account.analytic.plan' |
| 7 |
|
| 8 |
def _calculate_distribution_amount(self, amount, percentage, total_percentage, distribution_on_each_plan): |
| 9 |
""" |
| 10 |
Ensures that the total amount distributed across all lines always adds up to exactly `amount` per |
| 11 |
plan. We try to correct for compounding rounding errors by assigning the exact outstanding amount when |
| 12 |
we detect that a line will close out a plan's total percentage. However, since multiple plans can be |
| 13 |
assigned to a line, with different prior distributions, there is the possible edge case that one line |
| 14 |
closes out two (or more) tallies with different compounding errors. This means there is no one correct |
| 15 |
amount that we can assign to a line that will correctly close out both all plans. This is described in |
| 16 |
more detail in the commit message, under "concurrent closing line edge case". |
| 17 |
""" |
| 18 |
decimal_precision = self.env['decimal.precision'].precision_get('Percentage Analytic') |
| 19 |
distributed_percentage, distributed_amount = distribution_on_each_plan.get(self, (0, 0)) |
| 20 |
allocated_percentage = distributed_percentage + percentage |
| 21 |
if float_compare(allocated_percentage, total_percentage, precision_digits=decimal_precision) == 0: |
| 22 |
calculated_amount = (amount * total_percentage / 100) - distributed_amount |
| 23 |
else: |
| 24 |
calculated_amount = amount * percentage / 100 |
| 25 |
distributed_amount += float_round(calculated_amount, precision_digits=decimal_precision) |
| 26 |
distribution_on_each_plan[self] = (allocated_percentage, distributed_amount) |
| 27 |
return calculated_amount |
| 28 |
|
| 29 |
|
| 30 |
class AccountAnalyticAccount(models.Model): |
| 31 |
_inherit = 'account.analytic.account' |
| 32 |
|
| 33 |
def _perform_analytic_distribution(self, distribution, amount, unit_amount, lines, obj, additive=False): |
| 34 |
""" |
| 35 |
Redistributes the analytic lines to match the given distribution: |
| 36 |
- For account_ids where lines already exist, the amount and unit_amount of these lines get updated, |
| 37 |
lines where the updated amount becomes zero get unlinked. |
| 38 |
- For account_ids where lines don't exist yet, the line values to create them are returned, |
| 39 |
lines where the amount becomes zero are not included. |
| 40 |
|
| 41 |
:param distribution: the desired distribution to match the analytic lines to |
| 42 |
:param amount: the total amount to distribute over the analytic lines |
| 43 |
:param unit_amount: the total unit amount (will not be distributed) |
| 44 |
:param lines: the (current) analytic account lines that need to be matched to the new distribution |
| 45 |
:param obj: the object on which _prepare_analytic_line_values(account_id, amount, unit_amount) will be |
| 46 |
called to get the template for the values of new analytic line objects |
| 47 |
:param additive: if True, the unit_amount and (distributed) amount get added to the existing lines |
| 48 |
|
| 49 |
:returns: a list of dicts containing the values for new analytic lines that need to be created |
| 50 |
:rtype: dict |
| 51 |
""" |
| 52 |
if not distribution: |
| 53 |
lines.unlink() |
| 54 |
return [] |
| 55 |
|
| 56 |
|
| 57 |
distribution = { |
| 58 |
self.env['account.analytic.account'].browse(map(int, ids.split(','))).exists(): percentage |
| 59 |
for ids, percentage in distribution.items() |
| 60 |
} |
| 61 |
|
| 62 |
plans = self.env['account.analytic.plan'] |
| 63 |
plans = sum(plans._get_all_plans(), plans) |
| 64 |
line_columns = [p._column_name() for p in plans] |
| 65 |
|
| 66 |
lines_to_link = [] |
| 67 |
distribution_on_each_plan = {} |
| 68 |
total_percentages = {} |
| 69 |
|
| 70 |
for accounts, percentage in distribution.items(): |
| 71 |
for plan in accounts.root_plan_id: |
| 72 |
total_percentages[plan] = total_percentages.get(plan, 0) + percentage |
| 73 |
|
| 74 |
for existing_aal in lines: |
| 75 |
|
| 76 |
accounts = sum(map(existing_aal.mapped, line_columns), self.env['account.analytic.account']) |
| 77 |
if accounts in distribution: |
| 78 |
|
| 79 |
percentage = distribution[accounts] |
| 80 |
new_amount = 0 |
| 81 |
new_unit_amount = unit_amount |
| 82 |
for account in accounts: |
| 83 |
plan = account.root_plan_id |
| 84 |
new_amount = plan._calculate_distribution_amount(amount, percentage, total_percentages[plan], distribution_on_each_plan) |
| 85 |
if additive: |
| 86 |
new_amount += existing_aal.amount |
| 87 |
new_unit_amount += existing_aal.unit_amount |
| 88 |
currency = accounts[0].currency_id or obj.company_id.currency_id |
| 89 |
if float_is_zero(new_amount, precision_rounding=currency.rounding): |
| 90 |
existing_aal.unlink() |
| 91 |
else: |
| 92 |
existing_aal.amount = new_amount |
| 93 |
existing_aal.unit_amount = new_unit_amount |
| 94 |
|
| 95 |
del distribution[accounts] |
| 96 |
else: |
| 97 |
|
| 98 |
existing_aal.unlink() |
| 99 |
|
| 100 |
for accounts, percentage in distribution.items(): |
| 101 |
if not accounts: |
| 102 |
continue |
| 103 |
account_field_values = {} |
| 104 |
for account in accounts: |
| 105 |
new_amount = account.root_plan_id._calculate_distribution_amount(amount, percentage, total_percentages[plan], distribution_on_each_plan) |
| 106 |
account_field_values[account.plan_id._column_name()] = account.id |
| 107 |
currency = account.currency_id or obj.company_id.currency_id |
| 108 |
if not float_is_zero(new_amount, precision_rounding=currency.rounding): |
| 109 |
lines_to_link.append(obj._prepare_analytic_line_values(account_field_values, new_amount, unit_amount)) |
| 110 |
return lines_to_link |