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 automaticly get the value from a custom field in sale order form to a filed in invoice form?

By
babis peroukas
on 10/15/13, 7:03 AM 4,975 views

Hello,

I use the developer mode in openerp 7 so i can create some fields that are needed. I 've created a char field in the sale order form (x_duty). Is there any way to create another field in the account.invoice.form so it automaticly gets the value of the x_duty field from the sales order form?

Thank you in advance

First add the field you want to the invoice the same way you did the partner and create an onchange function on the customer name in the invoice.

kaynis
on 10/15/13, 1:29 PM

hi and thanks for the answer but... what u mean with "the same way"? are there any parameters i gotta add?

babis peroukas
on 10/15/13, 1:56 PM

You have to create an onchange function based on another field, e.g, the partner name or the sales order id

kaynis
on 10/15/13, 4:56 PM

can u please explain me? in which file do i have to create that function and how? Also, does it matters what type of field it is? I mean can I send the value of a char field in sales.order form to a field in invoice form? Thanks for the answers :)

babis peroukas
on 10/23/13, 8:48 AM
1
kaynis
On 10/23/13, 8:45 AM

Use something of this nature which I copied form one of the modules

class sale_order(osv.osv):
_inherit = 'sale.order'
def _make_invoice(self, cr, uid, order, lines, context=None):
    inv_id = super(sale_order, self)._make_invoice(cr, uid, order, lines, context)
    inv_obj = self.pool.get('account.invoice')
    if order.name_of_your_field:
        inv_obj.write(cr, uid, [inv_id], {'name_of_your_field': name_of_your_field.id}, context=context)
    return inv_id

sale_order()

class account_invoice(osv.osv):    
_inherit = "account.invoice"
_columns = {
    'name_of_your_field': fields.many2one(your field characteristics),
}
 account_invoice()

this is used to take the delivery address in sales order to the invoice. This should help you. Add the code below in your xml view

   <?xml version="1.0" encoding="UTF-8"?>
   <openerp>
 <data>
    <record model="ir.ui.view" id="invoice_form_shipping_address">
        <field name="name">account.invoice.form.shipping.address</field>
        <field name="model">account.invoice</field>
        <field name="inherit_id" ref="account.invoice_form"/>
        <field name="type">form</field>
        <field name="arch" type="xml">
            <field name="address_invoice_id" position="after">
                <field colspan="4" name="name_of_your_field" groups="base.group_extended"/>
            </field>
        </field>
    </record>
</data>
</openerp>

that's the one I can understand. My problem is that I dont know in which file and where in the file I have to put this. I mean it's in the sales.py (obviously) but do I have to change the _make_invoice function?

babis peroukas
on 10/23/13, 8:56 AM

Ah, forgot to mention, I 've not made a custom module (I dont know how), so all I m trying to do is to modify the existing sales.py

babis peroukas
on 10/23/13, 9:03 AM

You have to create your own module for this. Never change the core module unless you are absolutely totally sure what you are doing.

kaynis
on 10/23/13, 10:47 AM

Create a new module call it xname_to_invoice, i.e create a new folder inside the addon folder and give it that name. Then create you __init__.py and inside the __init__.py write "import sales" then save this. Create a __openerp.py too and add the dependent modules too see examples online. then create sales.py and inside add your code similar to the example I gave above.

kaynis
on 10/23/13, 10:56 AM

Ok, I dont know how it workds though. If i override _make_invoice function how can it take place in sale.order?

babis peroukas
on 10/23/13, 10:58 AM

I ll give it a try and I ll inform ou. thank you very much

babis peroukas
on 10/23/13, 11:09 AM

I have made some changes that can point you in the direction you want.

kaynis
on 10/23/13, 11:12 AM

Thanks a lot :)

babis peroukas
on 10/23/13, 11:16 AM

if you are still having problems let me know. I am a little busy but will try and help.

kaynis
on 10/23/13, 11:25 AM

helo again :) i did this and now, when i m goin to install it it says: error while validating arch fields: Invalid XML for View Architecture!

babis peroukas
on 10/25/13, 7:28 AM

my xml is this:

<?xml version="1.0" encoding="UTF-8"?> <openerp> <data> <record model="ir.ui.view" id="invoice_form_shipping_address"> <field name="name">account.invoice.form.shipping.address</field> <field name="model">account.invoice</field> <field name="inherit_id" ref="account.invoice_form"/> <field name="type">form</field> <field name="arch" type="xml"> <field name="address_invoice_id" position="after"> <field colspan="4" name="x_custom_field" groups="base.group_extended"/> </field> </field>

babis peroukas
on 10/25/13, 7:29 AM

</field> </record> </data> </openerp>

wrote 2 comments cause i cant post it in one

babis peroukas
on 10/25/13, 7:30 AM

i replaced account.invoice_form with account.invoice.form and then it says: ValueError: too many values to unpack

babis peroukas
on 10/25/13, 9:24 AM
0
babis peroukas
On 10/23/13, 9:22 AM

Hi and thanks for all help :) I ' solved it by edtiting the sale.py in sale module. I just had to edit _prepare_invoice function and add my custom fields to its invoice_vals like this:

    invoice_vals = {
        'name': order.client_order_ref or '',
        'origin': order.name,
        'type': 'out_invoice',
        'reference': order.client_order_ref or order.name,
        'account_id': order.partner_id.property_account_receivable.id,
        'partner_id': order.partner_invoice_id.id,
        'journal_id': journal_ids[0],
        'invoice_line': [(6, 0, lines)],
        'currency_id': order.pricelist_id.currency_id.id,
        'comment': order.note,
        'payment_term': order.payment_term and order.payment_term.id or False,
        'fiscal_position': order.fiscal_position.id or order.partner_id.property_account_position.id,
        'date_invoice': context.get('date_invoice', False),
        'company_id': order.company_id.id,
        'user_id': order.user_id and order.user_id.id or False,
        'x_destplace': order.x_destplace,
        'x_loadplace': order.x_loadplace,
        'x_vehivle': order.x_vehicle
    }

Then I created those fileds in both models and my problem solved :)

Someone told you already but I repeat: Never ever change the core modules.

Pau Ubach
on 9/16/14, 5:01 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

2 follower(s)

Stats

Asked: 10/15/13, 7:03 AM
Seen: 4975 times
Last updated: 3/16/15, 8:10 AM