Skip to content
Open
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
31 changes: 30 additions & 1 deletion budget_control/models/base_budget_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,22 @@ def _get_amount_convert_currency(
amount_currency, company.currency_id, company, date_commit
)

def _get_origin_record(self, field, budget_vals):
comodel = field.comodel_name
inverse_name = field.inverse_name

if not (comodel and inverse_name):
return False

inverse_field = self.env[comodel]._fields.get(inverse_name)
if not inverse_field:
return False

target_model = inverse_field.comodel_name
origin_id = budget_vals.get(inverse_name)

return self.env[target_model].browse(origin_id).exists() if origin_id else False

def _update_budget_commitment(self, budget_vals, reverse=False):
self.ensure_one()
company = self.env.user.company_id
Expand All @@ -306,6 +322,16 @@ def _update_budget_commitment(self, budget_vals, reverse=False):
if not analytic_account:
analytic_account = self[self._budget_analytic_field]
budget_moves = self[self._budget_field()]
field_name = self._budget_field()
field = self._fields.get(field_name)
currency_date = getattr(self, "date_commit", False)

if field and field.type == "one2many":
origin = self._get_origin_record(field, budget_vals)

if origin and "date_commit" in origin._fields:
currency_date = origin.date_commit

date_commit = budget_vals.get(
"date",
max(budget_moves.mapped("date")) if budget_moves else self.date_commit,
Expand All @@ -319,7 +345,10 @@ def _update_budget_commitment(self, budget_vals, reverse=False):
and currency != company.currency_id
):
amount = self._get_amount_convert_currency(
budget_vals["amount_currency"], currency, company, date_commit or today
budget_vals["amount_currency"],
currency,
company,
currency_date or today,
)

# NOTE: This is to handle the case of budget revenue.
Expand Down
Loading