Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to connect button with function?

By
arcull
on 1/25/16, 1:33 AM 685 views

Hi everyone.

I'm trying to put a new button on sales invoice which will print my customized version of invoice. So I've created new addon"my" and now need to connect this button click with function in "my" module. Can you please suggest, how can I refernce function in my model when hitting my new print button. The "print_invoice_my" refers to account.invoice model, while it should refernce to "my" model. Therefore I get an error when trying to run, it says it can not find  "print_invoice_my" in account.invoice.Here is my xml:

<openerp>
<data>
<record id="invoice_form_my" model="ir.ui.view">
<field name="name">account.invoice.form.my</field>
<field name="model">account.invoice</field>
<field name="inherit_id" ref="account.invoice_form"/>
<field name="arch" type="xml">
<button name="invoice_print" position="after">
<button type="object" name="print_invoice_my" string="my Invoice"/>
</button>
</field>
</record>
</data>
</openerp>

Much thanks, Arcull

2

Anil R. Kesariya

--Anil R. Kesariya--
4321
| 6 5 8
Gandhinagar, India
--Anil R. Kesariya--
ERP Consultant

Key Skill
-
Technical & Functional Expert
Anil R. Kesariya
On 1/25/16, 4:38 AM

Hi Arcull,


Make sure you have the method or definition with this name print_invoice_my inside your account.invoice model,

When your button type="object" that means odoo framework will look for the method inside model provided on same view, with button name value.



Hope this might help


Regards,

Anil.

Thanks Anil. I don't want to modify existing model. I will inherit account.invoice model, but don't know how will my xml for custom invoice then look like? I mean the tags:model and inherit_id. Thanks for your help.

arcull
on 1/25/16, 5:00 AM
0

Temur

--Temur--
2896
| 6 5 7
Tbilisi, Georgia
--Temur--
Programmer
Temur
On 1/25/16, 4:11 AM

You've to define print_invoice_my python function in account.invoice model, because of name="print_invoice_my" the function with name print_invoice_my from the corresponding model (account.invoice in your case) is being called and as you have not defined it, you've got above error.



UPDATE:

if you're interested in changing view of report itself, or add additional fields to it, then you should inherit or change the report view defined here: https://github.com/odoo/odoo/blob/dc48744bf7438761e1f25d411ec61755f74c60df/addons/account/views/report_invoice.xml#L4


for example, if you add new field in account.invoice, named my_field, then something like this should do the trick:

<template id="report_invoice_document" inherit_id="account.report_invoice_document">

<xpath expr="//div/div/div/address" position="after">

<span t-field="o.my_field" /> 

</xpath> </template>

this way, "my_field" is added to the printed invoice.

Thanks Temur, but the point is, that I don' want to adjust the original account.invoice model, I want to inherit it in my module and add the function there. How can I achieve that? Thanks again.

arcull
on 1/25/16, 4:36 AM

So mean I. It's correct, you must inherit account.invoice model in your module and define your function print_invoice_my in the inherited model, but you're actually extending the existing account.invoice model, and so defining function in it. I just skipped over the details in my answer

Temur
on 1/25/16, 4:47 AM

Thanks Temur. Will do that, but how will my xml for custom invoice then look like? I guess account.invoice will change to my. What about the "inherit_id" tag? Thanks.

arcull
on 1/25/16, 4:57 AM

No, there is nothing to change in XML, account.invoice will stay same because you'll extend the account.invoice model by "Extension" inheritance, see documentation at https://www.odoo.com/documentation/8.0/reference/orm.html#inheritance-and-extension , there is explanation of a different inheritance approach, you'll need to apply the "Extension" one (scroll down a bit at the referred page); "inherit_id" refers to the XML record you're extending in your xml, it does not need to be changed either.

Temur
on 1/25/16, 5:10 AM

Thank you Temur, will try to impelment this and report back.

arcull
on 1/25/16, 5:14 AM

Unfortunatelly I can't make the inheritance to work. Please see my post at https://www.odoo.com/forum/help-1/question/cant-inherit-account-invoice-96458

arcull
on 1/26/16, 1:03 AM

Answered... We do not use _name or we use same _name as in a parent class in case of extension inheritance.

Temur
on 1/26/16, 4:36 AM

updated with view extension example... If you're going to customize invoice then it may halp.

Temur
on 1/27/16, 9:47 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 1/25/16, 1:33 AM
Seen: 685 times
Last updated: 4/8/16, 3:12 PM