跳至内容
菜单
此问题已终结
2 回复
1071 查看

The requirement is to have a menu that will display account move line entries with expense account selected on the vendor bills. If taxes are applied, the listing should include all tax entries.  See the expected output: https://i.sstatic.net/2A4IgAM6.jpg.

See my current domain:

<record id="action_account_move_line" model="ir.actions.act_window">
    <field name="name">Expense Analysis</field>
    <field name="type">ir.actions.act_window</field>
    <field name="res_model">account.move.line</field>
    <field name="view_mode">tree,pivot,graph</field>
    <field name="domain">[('account_id.deprecated', '=', False),('account_id.internal_group', 'in', ['expense']),('exclude_from_invoice_tab','=',False)]</field>
    <field name="view_id" eval="expense_line_tree_view"/>
</record>

The above domain only fetched the move line entries without the taxes. See the current result: https://i.sstatic.net/JXR7852C.jpg

Gracia.

形象
丢弃
编写者 最佳答案

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.


形象
丢弃
最佳答案

Hi Raplh,

I have a workaround for your case. First you need to know few things:

  1. Tax payable is a liabilities account which have a different account_type than expense
  2. There is no grouping on tax payable accounts except you create one for them
  3. You used filter on internal_group, this includes 3 account types: Expenses, Depreciation, and Cost of Revenue. Double check if you really need "internal_group" or "account_type"

For tax accounts, you might need to hard code the ids into the domain tuples.

Hope this helps

形象
丢弃
相关帖文 回复 查看 活动
0
1月 25
581
1
11月 22
2163
1
7月 25
141
0
7月 25
129
1
7月 25
198