如何在Odoo12的销售模块中添加智能按钮

时间:2019-06-25 12:45:59

标签: odoo odoo-12

我想在Odoo 12的销售模块中添加一个智能按钮,当我单击它时,它会显示该客户的过期发票

我已经尝试通过继承销售模块来做到这一点,但是它不起作用

2 个答案:

答案 0 :(得分:0)

首先,您需要继承合作伙伴视图并添加智能按钮:

<record model="ir.ui.view" id="partner_view_buttons">
        <field name="name">partner.view.buttons</field>
        <field name="model">res.partner</field>
        <field name="inherit_id" ref="base.view_partner_form" />
        <field name="arch" type="xml">
            <div name="button_box" position="inside">
                <button type="object" class="oe_stat_button" icon="fa-pencil-square-o" name="action_view_partner_overdue_invoices"
                    attrs="{'invisible': [('customer', '=', False)]}" context="{'default_partner_id': active_id}">
                    <div class="o_form_field o_stat_info">
                        <span class="o_stat_value">
                            <field name="total_overdue" widget='monetary' options="{'currency_field': 'currency_id'}"/>
                        </span>
                        <span class="o_stat_text">Total Overdue</span>
                    </div>
                </button>
            </div>
          </field>
        </record>

继承res.partner模型并添加action_view_partner_overdue_invoices方法,该方法返回action with domain来过滤发票

添加一个计算字段total_overdue以显示总逾期。

答案 1 :(得分:0)

下面的代码可以计算出客户的发票数量并显示其帐单清单,它可以帮助您显示智能按钮并测试功能:

在XML中:

<record model="ir.ui.view" id="partner_view_buttons">
    <field name="name">partner.view.inherit</field>
    <field name="model">res.partner</field>
    <field name="inherit_id" ref="base.view_partner_form" />
        <field name="arch" type="xml">

            <div name="button_box" position="inside">

             <button name="act_show_invoice" type="object" icon="fa-usd" class="oe_stat_button"
                help="courriel">
                <field name="invoice_count" widget="statinfo"/>
             </button>

            </div>

    </field>
</record>

在python中:

invoice_count = fields.Integer(string='Nbr Facture', compute='_compute_invoice_count',)

获取计数的计算方法

def _compute_invoice_count(self):
    invoice_data = self.env['account.invoice'].read_group(domain=[('partner_id', 
                'child_of', self.ids)],fields=['partner_id'], groupby=['partner_id'])
    partner_child_ids = self.read(['child_ids'])
    mapped_data = dict([(m['partner_id'][0], m['partner_id_count']) for m in 
                                                                     invoice_data])
    for partner in self:
        # let's obtain the partner id and all its child ids from the read up there
        item = next(p for p in partner_child_ids if p['id'] == partner.id)
        partner_ids = [partner.id] + item.get('child_ids')
        # then we can sum for all the partner's child
        partner.invoice_count = sum(mapped_data.get(child, 0) for child in 
        partner_ids)

用于导航到树视图或相关记录

 def act_show_invoice(self):
    self.ensure_one()
    res = self.env['ir.actions.act_window'].for_xml_id(
        'account', 'action_invoice_tree1')
    res.update(
        context=dict(
            self.env.context,
            search_default_partner_id=self.id,
        ),
    )

    return res

我希望它可以为您提供帮助