Search code examples
odooodoo-10

Load invoices of partner,parent and all childs


I have selected partner and I have field invoice_ids. when I click add object I want that only invoices of partner, parent and all his child_ids invoice would be loaded. I tried to add the domain as you see above but I get an error

Uncaught Error: AttributeError: object has no attribute 'parent_id'

and I tried it without domain but with onchange but problem is that when i create record and chose partner onchnage starts to work but the is no partner_id yet when i select partner it's still no partner because it is not saved.. so need some help here.

'invoice_ids': fields.many2many(
            'account.invoice', 'cash_receipt_invoce_rel',
            'cash_receipt_id', 'invoice_id', "Invoices", 
            domain="[('partner_id','in', partner_id.parent_id.child_ids)]",

def onchange_field_id(self, cr, uid, ids, name, context=None):
    cash_rep = self.browse(cr,uid, ids,context=context)
    relation_ids = [x.id for x in cash_rep.partner_id.child_ids]
    return {'domain': {'invoice_ids': [('partner_id', 'in', relation_ids)]}}


Solution

  • To get invoices of selected partner, parent of that partner and child of that partner then you can write as following.

    'invoice_ids': fields.many2many(
                'account.invoice', 'cash_receipt_invoce_rel',
                'cash_receipt_id', 'invoice_id', "Invoices", 
               domain="['|','|',('partner_id.child_ids','in',[partner_id]),('partner_id','=', partner_id),('partner_id','child_of',partner_id)]",