'This function returns an action that display existing vendor bills of given purchase order ids.
When only one found, show the vendor bill immediately.'

@api.multi
def action_view_invoice(self):

    action = self.env.ref('account.action_invoice_tree2')
    result = action.read()[0]
    result['context'] = {'type': 'in_invoice', 'default_purchase_id': self.id}
    if (not self.invoice_ids):
        journal_domain = [('type', '=', 'purchase'), ('company_id', '=', self.company_id.id), ('currency_id', '=', self.currency_id.id)]
        default_journal_id = self.env['account.journal'].search(journal_domain, limit=1)
        if default_journal_id:
            result['context']['default_journal_id'] = default_journal_id.id
    else:
        result['context']['default_journal_id'] = self.invoice_ids[0].journal_id.id
    if (len(self.invoice_ids) != 1):
        result['domain'] = (("[('id',    'in',    " + str(self.invoice_ids.ids)) + ')]')
    elif (len(self.invoice_ids) == 1):
        res = self.env.ref('account.invoice_supplier_form', False)
        result['views'] = [(((res and res.id) or False), 'form')]
        result['res_id'] = self.invoice_ids.id
    return result
