-
-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] account_budget_oca: Add report for budget lines
- Loading branch information
Showing
11 changed files
with
298 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# Part of Odoo. See LICENSE file for full copyright and licensing details. | ||
|
||
from . import models | ||
from . import report | ||
from . import wizards |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). | ||
|
||
from . import report_budget_lines |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
# Copyright 2022 Simone Rubino - TAKOBI | ||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). | ||
|
||
from odoo import fields | ||
from odoo.models import TransientModel | ||
|
||
|
||
class ReportBudgetLinesHeader (TransientModel): | ||
_name = 'account_budget_oca.budget_lines_report.header' | ||
_description = "Budget Report Header" | ||
|
||
date = fields.Date() | ||
line_ids = fields.One2many( | ||
comodel_name='account_budget_oca.budget_lines_report', | ||
inverse_name='report_header_id', | ||
) | ||
|
||
def _prepare_report_lines(self): | ||
self.ensure_one() | ||
budget_lines = self.env['crossovered.budget.lines'].search([]) | ||
budget_lines_values = budget_lines \ | ||
.with_context( | ||
budget_date=self.date, | ||
) \ | ||
.read( | ||
fields=[ | ||
'id', | ||
'crossovered_budget_id', | ||
'analytic_account_id', | ||
'general_budget_id', | ||
'date_from', | ||
'date_to', | ||
'planned_amount', | ||
'practical_amount', | ||
'theoretical_amount', | ||
'percentage', | ||
'company_id', | ||
], | ||
load=None, | ||
) | ||
report_id = self.id | ||
for budget_line_values in budget_lines_values: | ||
line_id = budget_line_values.pop('id') | ||
budget_line_values.update({ | ||
'crossovered_budget_line_id': line_id, | ||
'report_header_id': report_id | ||
}) | ||
return budget_lines_values | ||
|
||
def compute_report_data(self): | ||
self.ensure_one() | ||
report_lines_values = self._prepare_report_lines() | ||
report_lines = self.env['account_budget_oca.budget_lines_report'] \ | ||
.create(report_lines_values) | ||
return report_lines | ||
|
||
def show_result(self): | ||
lines = self.mapped('line_ids') | ||
lines_action = self.env['ir.actions.act_window'] \ | ||
.for_xml_id('account_budget_oca', 'budget_lines_report_action') | ||
lines_action['domain'] = [ | ||
('id', 'in', lines.ids), | ||
] | ||
return lines_action | ||
|
||
|
||
class ReportBudgetLines (TransientModel): | ||
_name = 'account_budget_oca.budget_lines_report' | ||
_description = "Budget Report Lines" | ||
|
||
report_header_id = fields.Many2one( | ||
comodel_name='account_budget_oca.budget_lines_report.header', | ||
string="Report Header", | ||
readonly=True, | ||
) | ||
crossovered_budget_line_id = fields.Many2one( | ||
comodel_name='crossovered.budget.lines', | ||
string="Budget Line", | ||
readonly=True, | ||
) | ||
crossovered_budget_id = fields.Many2one( | ||
comodel_name='crossovered.budget', | ||
string="Budget", | ||
readonly=True, | ||
) | ||
analytic_account_id = fields.Many2one( | ||
comodel_name='account.analytic.account', | ||
string='Analytic Account', | ||
readonly=True, | ||
) | ||
general_budget_id = fields.Many2one( | ||
comodel_name='account.budget.post', | ||
string='Budgetary Position', | ||
readonly=True, | ||
) | ||
date_from = fields.Date( | ||
string='Start Date', | ||
readonly=True, | ||
) | ||
date_to = fields.Date( | ||
string='End Date', | ||
readonly=True, | ||
) | ||
planned_amount = fields.Float( | ||
readonly=True, | ||
) | ||
practical_amount = fields.Float( | ||
readonly=True, | ||
) | ||
theoretical_amount = fields.Float( | ||
readonly=True, | ||
) | ||
percentage = fields.Float( | ||
string='Achievement', | ||
readonly=True, | ||
) | ||
company_id = fields.Many2one( | ||
comodel_name='res.company', | ||
string="Company", | ||
readonly=True, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<!-- | ||
~ Copyright 2022 Simone Rubino - TAKOBI | ||
~ License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
--> | ||
<odoo> | ||
<record model="ir.ui.view" id="budget_lines_report_view_pivot"> | ||
<field name="name">Budget Report Lines Pivot view</field> | ||
<field name="model">account_budget_oca.budget_lines_report</field> | ||
<field name="arch" type="xml"> | ||
<pivot> | ||
<field name="analytic_account_id" groups="analytic.group_analytic_accounting" type="row"/> | ||
<field name="date_to" type="col" interval="month"/> | ||
<field name="practical_amount" type="measure" widget="monetary"/> | ||
</pivot> | ||
</field> | ||
</record> | ||
|
||
<record model="ir.ui.view" id="budget_lines_report_view_tree"> | ||
<field name="name">Budget Report Lines Tree view</field> | ||
<field name="model">account_budget_oca.budget_lines_report</field> | ||
<field name="arch" type="xml"> | ||
<tree string="Budget Lines"> | ||
<field name="analytic_account_id" groups="analytic.group_analytic_accounting"/> | ||
<field name="date_from"/> | ||
<field name="date_to"/> | ||
<field name="planned_amount" widget="monetary"/> | ||
<field name="practical_amount" widget="monetary"/> | ||
<field name="theoretical_amount" widget="monetary"/> | ||
<field name="percentage"/> | ||
</tree> | ||
</field> | ||
</record> | ||
|
||
<record model="ir.actions.act_window" id="budget_lines_report_action"> | ||
<field name="name">Budgets</field> | ||
<field name="res_model">account_budget_oca.budget_lines_report</field> | ||
<field name="view_type">form</field> | ||
<field name="view_mode">pivot,tree,form</field> | ||
</record> | ||
</odoo> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Copyright 2022 Simone Rubino - TAKOBI | ||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). | ||
|
||
from dateutil.relativedelta import relativedelta | ||
|
||
from odoo import fields | ||
from odoo.tests import Form | ||
from .common import TestAccountBudgetCommon | ||
|
||
|
||
class TestLinesReport (TestAccountBudgetCommon): | ||
|
||
def test_report_theoretical_amount(self): | ||
""" | ||
The report shows the budget lines amounts in a specified date. | ||
""" | ||
# Clean any other budget line that might concur in the report | ||
self.env['crossovered.budget.lines'].search([]).unlink() | ||
# Arrange: Create a budget of 1000 for the next 10 days | ||
date_from = fields.Date.today() + relativedelta(days=1) | ||
date_to = date_from + relativedelta(days=10) | ||
budget_form = Form(self.env['crossovered.budget']) | ||
budget_form.name = "Test budget" | ||
budget_form.date_from = date_from | ||
budget_form.date_to = date_to | ||
with budget_form.crossovered_budget_line_ids.new() as line: | ||
line.planned_amount = 1000 | ||
line.general_budget_id = self.account_budget_post_sales0 | ||
budget = budget_form.save() | ||
# pre-condition: Right now, the theoretical amount is 0 | ||
budget_lines = budget.mapped('crossovered_budget_line_ids') | ||
budget_theoretical_amount = sum(budget_lines.mapped('theoretical_amount')) | ||
self.assertEqual(budget_theoretical_amount, 0) | ||
|
||
# Act: Create the report for 5 days from now | ||
wizard_form = Form(self.env['account_budget_oca.budget_lines_report.wizard']) | ||
wizard_form.date = date_from + relativedelta(days=5) | ||
wizard = wizard_form.save() | ||
report_action = wizard.generate() | ||
report_lines_model = report_action.get('res_model') | ||
report_lines_domain = report_action.get('domain') | ||
report_lines = self.env[report_lines_model].search(report_lines_domain) | ||
|
||
# Assert: The report show that the theoretical amount is 500 | ||
report_theoretical_amount = sum(report_lines.mapped('theoretical_amount')) | ||
self.assertEqual(report_theoretical_amount, 500) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). | ||
|
||
from . import report_budget_lines_wizard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Copyright 2022 Simone Rubino - TAKOBI | ||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). | ||
|
||
from odoo import fields | ||
from odoo.models import TransientModel | ||
|
||
|
||
class ReportBudgetLinesWizard (TransientModel): | ||
_name = 'account_budget_oca.budget_lines_report.wizard' | ||
_description = "Budget Report Wizard" | ||
|
||
date = fields.Date( | ||
default=fields.Date.today, | ||
) | ||
|
||
def generate(self): | ||
self.ensure_one() | ||
report_header = \ | ||
self.env['account_budget_oca.budget_lines_report.header'] \ | ||
.create({ | ||
'date': self.date, | ||
}) | ||
report_header.compute_report_data() | ||
return report_header.show_result() |
48 changes: 48 additions & 0 deletions
48
account_budget_oca/wizards/report_budget_lines_wizard_views.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<!-- | ||
~ Copyright 2022 Simone Rubino - TAKOBI | ||
~ License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). | ||
--> | ||
<odoo> | ||
<record id="report_budget_lines_wizard_view_form" model="ir.ui.view"> | ||
<field name="name">Form view</field> | ||
<field name="model">account_budget_oca.budget_lines_report.wizard</field> | ||
<field name="arch" type="xml"> | ||
<form> | ||
<sheet> | ||
<group> | ||
<group name="description"> | ||
<p> | ||
Show a report on the budget lines. | ||
<br/> | ||
Time-sensitive fields are computed injecting <strong>Date</strong>. | ||
</p> | ||
</group> | ||
<group name="configuration"> | ||
<field name="date"/> | ||
</group> | ||
</group> | ||
</sheet> | ||
<footer> | ||
<button name="generate" type="object" string="Show" class="btn-primary"/> | ||
or | ||
<button string="Cancel" special="cancel" /> | ||
</footer> | ||
</form> | ||
</field> | ||
</record> | ||
|
||
<record model="ir.actions.act_window" id="report_budget_lines_wizard_action"> | ||
<field name="name">Budget to date</field> | ||
<field name="res_model">account_budget_oca.budget_lines_report.wizard</field> | ||
<field name="view_type">form</field> | ||
<field name="view_mode">form</field> | ||
<field name="target">new</field> | ||
</record> | ||
|
||
<menuitem | ||
id="report_budget_lines_wizard_menu" | ||
parent="account.account_reports_management_menu" | ||
action="report_budget_lines_wizard_action" | ||
/> | ||
</odoo> |