sfx
sfx

Reputation: 1841

Odoo10 Hierachy view

I have to create a hierarchy view for my model in Odoo10. I have done the following. But I could get it. I want this to look like odoo8 chart of account view.

pyhon file:

class InconceExpenseDetails(models.Model):
    _name = "income.expense.details"

    parent_id = fields.Many2one("income.expense.details","Income ID")
    child_ids = fields.One2many("income.expense.details","parent_id",string="Income IDS",select=True)
    product_category = fields.Char("Category")
    planned_amount = fields.Float('Planned Amount', digits=0)
    actual_amount = fields.Float('Actual Amount', digits=0)
    variance = fields.Float('Variance', digits=0)
    currency_id = fields.Many2one('res.currency', string="Currency", default=lambda self: self.env.user.company_id.currency_id)
    company_id = fields.Many2one("res.company",string="Company",default=lambda self: self.env.user.company_id)
    type_seq = fields.Char("Sequence", select=1)
    type = fields.Selection([
        ('revenue', 'Revenue'),
        ('income', 'Income'),
        ('expense', 'Expense'),
    ], string="Type")

view file:

       <record id="view_budget_tree_view" model="ir.ui.view">
            <field name="name">income.expense.details.tree</field>
            <field name="model">income.expense.details</field>
            <field name="field_parent">child_ids</field>
            <field name="arch" type="xml">
                <tree colors="blue:type == 'income'" string="Income and Expense Details" toolbar="1">
                    <field name="type_seq"/>
                    <field name="type"/>
                    <field name="product_category"/>
                    <field name="planned_amount"/>
                    <field name="actual_amount"/>
                    <field name="variance"/>
                    <field name="currency_id"/>
                    <field name="company_id"/>
                    <field name="parent_id" invisible="1"/>
                </tree>
            </field>
        </record>

Act window:

<record id="action_budget_tree_view" model="ir.actions.act_window">
        <field name="name">income.expense.details.tree</field>
        <field name="res_model">income.expense.details</field>
        <field name="view_type">tree</field>
        <field name="domain">[('parent_id','=',False)]</field>
        <field name="view_id" ref="view_budget_tree_view"/>
    </record>

If I make it as normal tree view it works fine. But I want this as hierarchy view as it in Chart of Account of Odoo8

Upvotes: 1

Views: 258

Answers (2)

You need to create one Many2many field child_id. child_ids is already there for One2many field, keep it as it is. Create another field child_id (M2m - calculative)

@api.multi
def _get_child_ids(self):
    for record in self:
        result = []
        if record.child_ids:
            result = record.child_ids.ids
        record.child_id = [(6,0,result)]

child_id = fields.Many2many(compute=_get_child_ids, comodel_name="income.expense.details",                               relation='self_rel_child', column1='child_type_id_1', column2='child_type_id_2',string="Income / Expense")

Xml seems fine just replace One2many (child_ids) with Many2many (child_id).

   <record id="view_budget_tree_view" model="ir.ui.view">
        <field name="name">income.expense.details.tree</field>
        <field name="model">income.expense.details</field>
        <field name="field_parent">child_id</field>
        <field name="arch" type="xml">
            <tree colors="blue:type == 'income'" string="Income and Expense Details" toolbar="1">
                <field name="type_seq"/>
                <field name="type"/>
                <field name="product_category"/>
                <field name="planned_amount"/>
                <field name="actual_amount"/>
                <field name="variance"/>
                <field name="currency_id"/>
                <field name="company_id"/>
                <field name="parent_id" invisible="1"/>
            </tree>
        </field>
    </record>

Action should looks like,

    <record id="action_chart_tree" model="ir.actions.act_window">
        <field name="name">Chart of Income Expense</field>
        <field name="res_model">income.expense.details</field>
        <field name="view_type">tree</field>
        <field name="view_id" ref="view_budget_tree_view"/>
        <field name="domain">[('parent_id','=',False)]</field>
    </record>

Upvotes: 1

Charif DZ
Charif DZ

Reputation: 14751

you need compute field level :

   level = fields.Integer(compute='_get_level', string='Level', store=True)


   @api.multi
    @api.depends('parent_id', 'parent_id.level')
    def _get_level(self):
        '''Returns a dictionary with key=the ID of a record and value = the level of this  
           record in the tree structure.'''
        for report in self:
            level = 0
            if report.parent_id:
                level = report.parent_id.level + 1
            report.level = level

take a look at account.financial.report in account module .

Upvotes: 1

Related Questions