To add a custom dropdown filter (e.g., for currency selection) in Odoo 17.0 financial reports, you need to properly inherit the relevant XML templates and integrate the dropdown into the report filter system. Here's how you can achieve this:
Step-by-Step Solution
1. Identify the Correct Template to Inherit
Odoo financial reports use the account_reports.AccountReportFiltersCustomizable template for filters. Ensure you are targeting the correct template and its structure.
2. Create a Custom Module
If you haven't already, create a custom module to house your changes.
Directory structure:
custom_financial_report_filter/
├── __init__.py
├── __manifest__.py
├── views/
│ └── financial_report_filter.xml
3. Update the __manifest__.py File
Define your module and include the dependency on account_reports.
{
'name': 'Custom Financial Report Filters',
'version': '1.0',
'depends': ['account_reports'],
'author': 'Your Name',
'data': [
'views/financial_report_filter.xml',
],
'installable': True,
'application': False,
}
4. Inherit the Template and Add Your Dropdown
Create the financial_report_filter.xml file to extend the financial report filters.
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<template id="add_custom_currency_filter" inherit_id="account_reports.AccountReportFiltersCustomizable">
<xpath expr="//div[@id='filter_extra_options']" position="inside">
<div class="form-group" id="multi_currency_selection">
<label for="currency_dropdown">Select Currency:</label>
<select id="currency_dropdown" class="form-control">
<option value="USD">USD</option>
<option value="EUR">EUR</option>
<option value="GBP">GBP</option>
</select>
</div>
</xpath>
</template>
</odoo>
5. Add Backend Logic to Handle the Dropdown
To make the dropdown functional, you need to pass the selected value to the report:
-
Override Report Action:
Modify the action context to include the selected currency value.
-
Extend account.report Model:
Add custom logic to handle the selected currency in the report.
Example Python code:
from odoo import models, fields
class AccountReport(models.AbstractModel):
_inherit = 'account.report'
filter_currency = fields.Selection([
('USD', 'USD'),
('EUR', 'EUR'),
('GBP', 'GBP'),
], string="Currency", default='USD')
def _get_report_values(self, docids, data):
# Add custom logic to filter based on currency
if self.filter_currency:
data['currency'] = self.filter_currency
return super(AccountReport, self)._get_report_values(docids, data)
6. Update the JavaScript (Optional)
If you want the dropdown to dynamically update the report without reloading, you can use JavaScript to handle the interaction.
-
Add a JS File:
Create a JS file to add dynamic behavior.
Example:
odoo.define('custom_financial_report_filter.CurrencyDropdown', function (require) {
"use strict";
const publicWidget = require('web.public.widget');
publicWidget.registry.CurrencyDropdown = publicWidget.Widget.extend({
selector: '#multi_currency_selection',
events: {
'change #currency_dropdown': '_onCurrencyChange',
},
_onCurrencyChange: function (ev) {
const selectedCurrency = $(ev.target).val();
// Send an AJAX request to reload the report
console.log('Selected currency:', selectedCurrency);
},
});
});
-
Include the JS File in Your Module:
Update the __manifest__.py to include the JS file.
7. Test Your Changes
- Restart your Odoo server and upgrade the module:
./odoo-bin -u custom_financial_report_filter
- Open the financial reports (e.g., Balance Sheet) and verify that the dropdown is displayed and functional.
Key Notes
- Ensure Proper Dependencies: Your custom module must depend on account_reports for proper inheritance.
- Test in Developer Mode: Use Odoo's developer mode to debug template inheritance and confirm the dropdown is rendered correctly.
- Dynamic Behavior: If you need the dropdown to update results dynamically, you'll need to implement AJAX handling in your JavaScript.
By following these steps, you can add a custom dropdown to Odoo 17.0 financial reports. Let me know if you need further clarification or assistance!