Skip to content

Commit

Permalink
[IMP][account_financial_report]: add custom intervals for aged partne…
Browse files Browse the repository at this point in the history
…r report
  • Loading branch information
ernesto-garcia-tecnativa authored and carolinafernandez-tecnativa committed Mar 13, 2024
1 parent 11d3bcf commit ab779ba
Show file tree
Hide file tree
Showing 11 changed files with 556 additions and 117 deletions.
2 changes: 2 additions & 0 deletions account_financial_report/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"depends": ["account", "date_range", "report_xlsx"],
"data": [
"security/ir.model.access.csv",
"security/security.xml",
"wizard/aged_partner_balance_wizard_view.xml",
"wizard/general_ledger_wizard_view.xml",
"wizard/journal_ledger_wizard_view.xml",
Expand All @@ -34,6 +35,7 @@
"report/templates/trial_balance.xml",
"report/templates/vat_report.xml",
"view/account_view.xml",
"view/account_age_report_configuration_views.xml",
"view/report_general_ledger.xml",
"view/report_journal_ledger.xml",
"view/report_trial_balance.xml",
Expand Down
1 change: 1 addition & 0 deletions account_financial_report/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from . import account_age_report_configuration
from . import account_group
from . import account
from . import account_move_line
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2023 Ernesto García
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import fields, models


class AccountAgeReportConfiguration(models.Model):
_name = "account.age.report.configuration"
_description = "Model to set intervals for Age partner balance report"

name = fields.Char()
company_id = fields.Many2one("res.company", default=lambda self: self.env.company)
line_ids = fields.One2many(
"account.age.report.configuration.line", "account_age_report_config_id"
)


class AccountAgeReportConfigurationLine(models.Model):
_name = "account.age.report.configuration.line"

name = fields.Char()
code = fields.Char()
account_age_report_config_id = fields.Many2one("account.age.report.configuration")
lower_limit = fields.Integer()
superior_limit = fields.Integer()
58 changes: 50 additions & 8 deletions account_financial_report/report/aged_partner_balance.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def _initialize_account(self, ag_pb_data, acc_id):
ag_pb_data[acc_id]["90_days"] = 0.0
ag_pb_data[acc_id]["120_days"] = 0.0
ag_pb_data[acc_id]["older"] = 0.0
for interval_line in self.env.context["age_partner_config"].line_ids:
ag_pb_data[acc_id][interval_line.code] = 0.0
return ag_pb_data

@api.model
Expand All @@ -39,6 +41,8 @@ def _initialize_partner(self, ag_pb_data, acc_id, prt_id):
ag_pb_data[acc_id][prt_id]["120_days"] = 0.0
ag_pb_data[acc_id][prt_id]["older"] = 0.0
ag_pb_data[acc_id][prt_id]["move_lines"] = []
for interval_line in self.env.context["age_partner_config"].line_ids:
ag_pb_data[acc_id][prt_id][interval_line.code] = 0.0
return ag_pb_data

@api.model
Expand All @@ -47,6 +51,7 @@ def _calculate_amounts(
):
ag_pb_data[acc_id]["residual"] += residual
ag_pb_data[acc_id][prt_id]["residual"] += residual
interval_lines = self.env.context["age_partner_config"].line_ids
today = date_at_object
if not due_date or today <= due_date:
ag_pb_data[acc_id]["current"] += residual
Expand All @@ -66,6 +71,15 @@ def _calculate_amounts(
else:
ag_pb_data[acc_id]["older"] += residual
ag_pb_data[acc_id][prt_id]["older"] += residual
for line in interval_lines.sorted("superior_limit"):
if today >= due_date + timedelta(
days=line.lower_limit
) and today <= due_date + timedelta(days=line.superior_limit):
ag_pb_data[acc_id][line.code] += residual
ag_pb_data[acc_id][prt_id][line.code] += residual
else:
ag_pb_data[acc_id][line.code] += 0
ag_pb_data[acc_id][prt_id][line.code] += 0
return ag_pb_data

def _get_account_partial_reconciled(self, company_id, date_at_object):
Expand Down Expand Up @@ -235,6 +249,9 @@ def _compute_maturity_date(self, ml, date_at_object):
"older": 0.0,
}
)
interval_lines = self.env.context["age_partner_config"].line_ids
for interval_line in interval_lines:
ml[interval_line.code] = 0.0
due_date = ml["due_date"]
amount = ml["residual"]
today = date_at_object
Expand All @@ -250,6 +267,12 @@ def _compute_maturity_date(self, ml, date_at_object):
ml["120_days"] += amount
else:
ml["older"] += amount
match_interval = interval_lines.filtered(
lambda line: today >= due_date + timedelta(days=line.lower_limit)
and today <= due_date + timedelta(days=line.superior_limit)
)
if match_interval:
ml[match_interval.code] += amount

def _create_account_list(
self,
Expand All @@ -261,6 +284,7 @@ def _create_account_list(
date_at_oject,
):
aged_partner_data = []
interval_lines = self.env.context["age_partner_config"].line_ids
for account in accounts_data.values():
acc_id = account["id"]
account.update(
Expand All @@ -275,6 +299,8 @@ def _create_account_list(
"partners": [],
}
)
for interval_line in interval_lines:
account[interval_line.code] = ag_pb_data[acc_id][interval_line.code]
for prt_id in ag_pb_data[acc_id]:
if isinstance(prt_id, int):
partner = {
Expand All @@ -287,6 +313,10 @@ def _create_account_list(
"120_days": ag_pb_data[acc_id][prt_id]["120_days"],
"older": ag_pb_data[acc_id][prt_id]["older"],
}
for interval_line in interval_lines:
partner[interval_line.code] = ag_pb_data[acc_id][prt_id][
interval_line.code
]
if show_move_line_details:
move_lines = []
for ml in ag_pb_data[acc_id][prt_id]["move_lines"]:
Expand All @@ -306,6 +336,7 @@ def _create_account_list(

@api.model
def _calculate_percent(self, aged_partner_data):
interval_lines = self.env.context["age_partner_config"].line_ids
for account in aged_partner_data:
if abs(account["residual"]) > 0.01:
total = account["residual"]
Expand All @@ -331,6 +362,10 @@ def _calculate_percent(self, aged_partner_data):
),
}
)
for interval_line in interval_lines:
account[f"percent_{interval_line.code}"] = abs(
round((account[interval_line.code] / total) * 100, 2)
)
else:
account.update(
{
Expand All @@ -342,6 +377,8 @@ def _calculate_percent(self, aged_partner_data):
"percent_older": 0.0,
}
)
for interval_line in interval_lines:
account[f"percent_{interval_line.code}"] = 0.0
return aged_partner_data

def _get_report_values(self, docids, data):
Expand All @@ -355,12 +392,12 @@ def _get_report_values(self, docids, data):
date_from = data["date_from"]
only_posted_moves = data["only_posted_moves"]
show_move_line_details = data["show_move_line_details"]
(
ag_pb_data,
accounts_data,
partners_data,
journals_data,
) = self._get_move_lines_data(
aged_partner_configuration = self.env[
"account.age.report.configuration"
].browse(data["age_partner_config_id"])
(ag_pb_data, accounts_data, partners_data, journals_data,) = self.with_context(
age_partner_config=aged_partner_configuration
)._get_move_lines_data(
company_id,
account_ids,
partner_ids,
Expand All @@ -369,15 +406,19 @@ def _get_report_values(self, docids, data):
only_posted_moves,
show_move_line_details,
)
aged_partner_data = self._create_account_list(
aged_partner_data = self.with_context(
age_partner_config=aged_partner_configuration
)._create_account_list(
ag_pb_data,
accounts_data,
partners_data,
journals_data,
show_move_line_details,
date_at_object,
)
aged_partner_data = self._calculate_percent(aged_partner_data)
aged_partner_data = self.with_context(
age_partner_config=aged_partner_configuration
)._calculate_percent(aged_partner_data)
return {
"doc_ids": [wizard_id],
"doc_model": "open.items.report.wizard",
Expand All @@ -388,6 +429,7 @@ def _get_report_values(self, docids, data):
"only_posted_moves": only_posted_moves,
"aged_partner_balance": aged_partner_data,
"show_move_lines_details": show_move_line_details,
"age_partner_config": aged_partner_configuration,
}

def _get_ml_fields(self):
Expand Down
Loading

0 comments on commit ab779ba

Please sign in to comment.