OpenERP - 显示合作伙伴的child_ids发票

时间:2014-08-19 11:03:43

标签: openerp-7

我想在父母合作伙伴表单视图中显示针对子伴侣的发票。

我已经是一个继承的res_partner模型,如下所示:

class res_partner(osv.osv):
    _inherit = 'res.partner'
    _columns = {
        'invoice_ids': fields.one2many('account.invoice', 'partner_id', 'Invoices'),
}

显示发票的视图如下:

<?xml version="1.0"?>
<openerp>
<data>

<!--  Partners inherited form -->
<record id="view_history_partner_info_form" model="ir.ui.view">
  <field name="name">res.partner.cap_history.form.inherit</field>
  <field name="inherit_id" ref="base.view_partner_form"/>
  <field name="model">res.partner</field>
  <field name="arch" type="xml">
    <page string="Accounting" position="after" version="7.0">
      <page string="History" name="cap_history_tab">
        <group name="grp_invoice_history" string="Invoices History">
          <field name="invoice_ids" colspan="4" nolabel="1">
            <tree string="Partner Invoices"  create="false" delete="false">
              <field name="number" readonly="True"/>
              <field name="origin" readonly="True"/>
              <field name="name" string="Reference" readonly="True"/>
              <field name="date_invoice" readonly="True"/>
              <field name="x_category" readonly="True"/>
              <field name="state" readonly="True"/>
              <field name="payment_term" readonly="True"/>
              <field name="amount_total" readonly="True"/>
            </tree>
          </field>
        </group>
      </page>
    </page>
  </field>
</record>

使用此代码,我可以查看直接在公司或个人表单视图中处理的发票。 但是,如果发票是贴在人身上的,而且没有一个发送给母公司,那么当我在公司表单视图中时,我就不会看到发送给子联系人的发票。

有没有办法在父母合作伙伴表单视图中显示联系人的发票?

感谢您的帮助!

干杯

1 个答案:

答案 0 :(得分:0)

执行此操作的一种方法是添加一个新字段all_invoice_ids作为函数字段,然后让该函数返回invoice_ids的内容以及任何子项的内容&#39 ; s invoice_ids

像这样(未经测试):

    'all_invoice_ids': fields.function(
        _get_invoice_ids,
        type='one2many',
        obj='account.invoice',
        method=True,
        string='Invoices',
        ),

_get_invoice_ids(应该在columns之前定义)像这样(也是未经测试的):

def _get_invoice_ids(self, cr, uid, ids, field_name, arg, context=None):
    res = {}
    if isinstance(ids, (int, long)):
        ids = [ids] # in case an id was passed in directly
    for main_partner in self.browse(cr, uid, ids, context=context):
        main_invoices = main_partner.invoice_ids or [] # in case it was False
        invoices = [inv.id for inv in main_invoices]
        for child_partner in main_partner.child_ids:
            child_invoices = child_partner.invoice_ids or []
            invoices.extend([inv.id for inv in child_invoices])
        # at this point we should have all the invoice ids
        # use a set to get rid of duplicates
        invoices = list(set(invoices))
        # and store in res to be returned
        res[main_partner.id] = invoices
    return res