Thank you Andry. Your suggested workaround was so invaluable. However, hard-coding IDs into domain might cause avoidable issues further down the line especially if you are not working with a final version of your database as the IDs will be regenerated if a new database is created.
My adopted approach was to create an sql view with init method containing sql statements using UNION and JOIN. That way, it became easy to grab the desired tables and fields.
See complete class below:
from odoo import models, fields, tools
class ExpenseAnalysisWithTaxes(models.Model):
_name = ".expense.analysis.with.taxes"
_description = "Expense Analysis"
_auto = False # This is a SQL view, not a normal table
id = fields.Integer("ID", readonly=True)
date = fields.Date("Date", readonly=True)
name = fields.Char("Description of Job", readonly=True)
move_name = fields.Char("Payment Reference", readonly=True)
partner_id = fields.Many2one("res.partner", "Partner", readonly=True)
account_id = fields.Many2one("account.account", "Account", readonly=True)
account_name = fields.Char("Account Name", readonly=True)
price_subtotal = fields.Monetary("Total Amount", readonly=True)
currency_id = fields.Many2one("res.currency", "Currency", readonly=True, default=lambda self: self.env.company.currency_id)
def init(self):
"""Create the SQL view dynamically when the module is installed or updated."""
tools.drop_view_if_exists(self._cr, "expense_analysis_with_taxes")
self._cr.execute("""
CREATE OR REPLACE VIEW expense_analysis_with_taxes AS (
-- Expense lines
SELECT
aml.id AS id,
aml.date AS date,
aml.name AS name,
am.name AS move_name,
aml.partner_id AS partner_id,
aml.account_id AS account_id,
aa.name AS account_name,
aml.price_total AS price_subtotal
FROM account_move_line aml
JOIN account_account aa ON aml.account_id = aa.id
JOIN account_move am ON aml.move_id = am.id
WHERE am.move_type IN ('in_invoice','in_receipt')
AND aa.internal_group = 'expense'
UNION ALL
-- Tax lines (identified via tax_line_id)
SELECT
aml.id AS id,
aml.date AS date,
CONCAT('Tax: ', at.name) AS name,
am.name AS move_name,
aml.partner_id AS partner_id,
aml.account_id AS account_id,
aa.name AS account_name,
ABS(aml.price_subtotal) AS price_subtotal
FROM account_move_line aml
JOIN account_account aa ON aml.account_id = aa.id
JOIN account_move am ON aml.move_id = am.id
JOIN account_tax at ON aml.tax_line_id = at.id
WHERE am.move_type IN ('in_invoice','in_receipt')
) ORDER BY id DESC;
""")
Based on this, I then defined a menu, window action and tree view.
Hope this helps someone.