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

getting price of product.

By
charlesraj
on 5/24/13, 3:00 AM 929 views

hi i created two seperate field for product and its price . now i am able to select the product from the list.

i want to add an additional feature that when i select the product in the product field the price of the product will be came under the price field automatically

field
x_product(manytoone) , x_price(float)

what should i do in the onchange of xml form view.

send me a created code i will updated there...

sridhar
on 5/24/13, 3:34 AM

<?xml version="1.0"?> <form string="Purchase Order" version="7.0"> <group> <field name="x_product"/> <field name="x_price"/> </group> </form>

charlesraj
on 5/24/13, 3:46 AM

model= x_product, fields= x_product->many to one relation , x_price -float .. with onchange function what should i do.

charlesraj
on 5/24/13, 3:48 AM

which part i update in the form view using onchange function.

charlesraj
on 5/24/13, 3:51 AM

here no need for py file just i do with the xml form view.

charlesraj
on 5/24/13, 3:56 AM

Product name is a char how u create a many2one function... what u want here in product master creating a product, after u selecting a product then price will come automatically correct...

sridhar
on 5/24/13, 4:08 AM

onchange function is first create in py file then come to .xml file. that is the procedure.

sridhar
on 5/24/13, 4:10 AM

is there any way to implement onchange function on the xml file without going to py file. , many to one function is used to list the product in the list automatically when we press on the drop down arrow..

charlesraj
on 5/24/13, 4:18 AM

when selecting the product the price will appear on the price field..

charlesraj
on 5/24/13, 4:25 AM

sorry sir . you are right. thanks for your valuable comments..

charlesraj
on 5/24/13, 5:00 AM
0
sridhar
On 5/24/13, 4:52 AM

In the sale Order selecing the product it will automatically calling, try this,

def product_id_change(self, cr, uid, ids, pricelist, product, qty=0, uom=False, qty_uos=0, uos=False, name='', partner_id=False, lang=False, update_tax=True, date_order=False, packaging=False, fiscal_position=False, flag=False, context=None): context = context or {} lang = lang or context.get('lang',False) if not partner_id: raise osv.except_osv(_('No Customer Defined !'), _('Before choosing a product,\n select a customer in the sales form.')) warning = {} product_uom_obj = self.pool.get('product.uom') partner_obj = self.pool.get('res.partner') product_obj = self.pool.get('product.product') context = {'lang': lang, 'partner_id': partner_id} if partner_id: lang = partner_obj.browse(cr, uid, partner_id).lang context_partner = {'lang': lang, 'partner_id': partner_id}

    if not product:
        return {'value': {'th_weight': 0,
            'product_uos_qty': qty}, 'domain': {'product_uom': [],
               'product_uos': []}}
    if not date_order:
        date_order = time.strftime(DEFAULT_SERVER_DATE_FORMAT)

    result = {}
    warning_msgs = ''
    product_obj = product_obj.browse(cr, uid, product, context=context_partner)

    uom2 = False
    if uom:
        uom2 = product_uom_obj.browse(cr, uid, uom)
        if product_obj.uom_id.category_id.id != uom2.category_id.id:
            uom = False
    if uos:
        if product_obj.uos_id:
            uos2 = product_uom_obj.browse(cr, uid, uos)
            if product_obj.uos_id.category_id.id != uos2.category_id.id:
                uos = False
        else:
            uos = False
    fpos = fiscal_position and self.pool.get('account.fiscal.position').browse(cr, uid, fiscal_position) or False
    if update_tax: #The quantity only have changed
        result['tax_id'] = self.pool.get('account.fiscal.position').map_tax(cr, uid, fpos, product_obj.taxes_id)

    if not flag:
        result['name'] = self.pool.get('product.product').name_get(cr, uid, [product_obj.id], context=context_partner)[0][1]
        if product_obj.description_sale:
            result['name'] += '\n'+product_obj.description_sale
    domain = {}
    if (not uom) and (not uos):
        result['product_uom'] = product_obj.uom_id.id
        if product_obj.uos_id:
            result['product_uos'] = product_obj.uos_id.id
            result['product_uos_qty'] = qty * product_obj.uos_coeff
            uos_category_id = product_obj.uos_id.category_id.id
        else:
            result['product_uos'] = False
            result['product_uos_qty'] = qty
            uos_category_id = False
        result['th_weight'] = qty * product_obj.weight
        domain = {'product_uom':
                    [('category_id', '=', product_obj.uom_id.category_id.id)],
                    'product_uos':
                    [('category_id', '=', uos_category_id)]}
    elif uos and not uom: # only happens if uom is False
        result['product_uom'] = product_obj.uom_id and product_obj.uom_id.id
        result['product_uom_qty'] = qty_uos / product_obj.uos_coeff
        result['th_weight'] = result['product_uom_qty'] * product_obj.weight
    elif uom: # whether uos is set or not
        default_uom = product_obj.uom_id and product_obj.uom_id.id
        q = product_uom_obj._compute_qty(cr, uid, uom, qty, default_uom)
        if product_obj.uos_id:
            result['product_uos'] = product_obj.uos_id.id
            result['product_uos_qty'] = qty * product_obj.uos_coeff
        else:
            result['product_uos'] = False
            result['product_uos_qty'] = qty
        result['th_weight'] = q * product_obj.weight        # Round the quantity up

    if not uom2:
        uom2 = product_obj.uom_id
    # get unit price

    if not pricelist:
        warn_msg = _('You have to select a pricelist or a customer in the sales form !\n'
                'Please set one before choosing a product.')
        warning_msgs += _("No Pricelist ! : ") + warn_msg +"\n\n"
    else:
        price = self.pool.get('product.pricelist').price_get(cr, uid, [pricelist],
                product, qty or 1.0, partner_id, {
                    'uom': uom or result.get('product_uom'),
                    'date': date_order,
                    })[pricelist]
        if price is False:
            warn_msg = _("Cannot find a pricelist line matching this product and quantity.\n"
                    "You have to change either the product, the quantity or the pricelist.")

            warning_msgs += _("No valid pricelist line found ! :") + warn_msg +"\n\n"
        else:
            result.update({'price_unit': price})
    if warning_msgs:
        warning = {
                   'title': _('Configuration Error!'),
                   'message' : warning_msgs
                }
    return {'value': result, 'domain': domain, 'warning': warning}

sale_view.xml

on_change="product_id_change(parent.pricelist_id, product_id, product_uom_qty, product_uom, product_uos_qty, product_uos, name, parent.partner_id, False, True, parent.date_order, False, parent.fiscal_position, False, context)"/>

try this procedure it will be usefull.

Looks like you want to copy the complete on_change method in your own module. Is that true? Or do you want to modify the existing on_change method? Both options are dangerous...

patrick
on 5/24/13, 7:55 PM
0

Andreas Maertens

--Andreas Maertens--
912
| 5 5 7
Finsterwalde, Germany
--Andreas Maertens--

I develop odoo8.0 modules

Andreas Maertens
On 5/24/13, 3:39 AM

write an onchange method in the model and use that one from the xml form view to alter your fields.

here's an example from sale.py:

def onchange_pricelist_id(self, cr, uid, ids, pricelist_id, order_lines, context=None):
        context = context or {}
        if not pricelist_id:
            return {}
        value = {
            'currency_id': self.pool.get('product.pricelist').browse(cr, uid, pricelist_id, context=context).currency_id.id
        }
        if not order_lines:
            return {'value': value}
        warning = {
            'title': _('Pricelist Warning!'),
            'message' : _('If you change the pricelist of this order (and eventually the currency), prices of existing order lines will not be updated.')
        }
        return {'warning': warning, 'value': value}

while installing the created module an error message will be show like this.... .AttributeError: 'module' object has no attribute 'model'

charlesraj
on 5/30/13, 9:56 AM

The Code above is a copy from "sale.py" in "stock"-module. I guess the failure is somewhere else in your addon. I probably could help you more if you post your source code from reasonable files. ;)

Andreas Maertens
on 5/30/13, 10:44 AM

from openerp.osv import fields,osv

class price(osv.Model): _name = "x_product" _description ="productlist" _coloums = {

charlesraj
on 5/30/13, 10:53 AM

'x_product': fields.many2one('Product.product','product'), 'x_price':fields.float('price',required="false"), } price() def onchange_pricelist_id(self, cr, uid, ids, pricelist_id, order_lines, context=None):

charlesraj
on 5/30/13, 10:53 AM

context = context or {} if not pricelist_id: return {} value = { 'currency_id': self.pool.get('product.pricelist').browse(cr, uid, pricelist_id, context=context).currency_id.id } if not order_lines: return {'value': value} warning = {

charlesraj
on 5/30/13, 10:53 AM

'title': _('Pricelist Warning!'), 'message' : _('If you change the pricelist of this order (and eventually the currency), prices of existing order lines will not be updated.') } return {'warning': warning, 'value': value}

charlesraj
on 5/30/13, 10:54 AM

here it is: class price(osv.Model): it should be: class price(osv.osv):

Andreas Maertens
on 5/30/13, 12:23 PM

sir i try with your suggestion in the eclipseIDE but the error shown in console is ...ImportError: No module named openerp.osv.

charlesraj
on 5/31/13, 2:12 AM

another error displayed while trying to install the module....File "C:\Program Files\OpenERP 7.0-20130509-231040\Server\server\openerp\addons\base\ir\ir_ui_view.py", line 101, in create KeyError: 'arch'

charlesraj
on 5/31/13, 3:31 AM

Please try: from osv import osv, fields . If it's not working you might not have added your server as lib correctly? The osv comes from server/openerp/osv. Maybe just Eclipse cannot solve the import but openerp can do. Your KeyError comes from a view. To help with that please add your view to let me take a view at.

Andreas Maertens
on 5/31/13, 3:52 AM

<?xml version ="1.0" encoding="utf-8"?> <openerp> <data> <record id="customer_order_form" model="ir.ui.view"> <field name="x_product">x_product.form</field> <field name="x_price"/> <field name="arch" type="xml"/> </record> </data> </openerp>

charlesraj
on 5/31/13, 4:58 AM

i tried ..from osv import osv, fields .. but the same error occurs.. so i just created an another one while try to installing that the error shown is like ..Syntax Error: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128).. is it a problem of my compiler. any solutions please.

charlesraj
on 5/31/13, 5:02 AM

Try #-*- coding: utf-8 -*- in the first row of your .py file

Andreas Maertens
on 5/31/13, 5:14 AM

ok that problem is solved the module is to be installed . But the form is not displayed. parent menu and submenu are displayed.

charlesraj
on 5/31/13, 8:07 AM

Did you installed a ir.action_window entry? Checked user permissions?

Andreas Maertens
on 5/31/13, 8:35 AM

sir this i my form view..<?xml version="1.0" encoding="utf-8"?>

<openerp>

<data>

   <record model="ir.ui.view" id="test_base_form">

       <field name="name">test.base.form</field>

           <field name="model">test.base</field>

           <field name="type">form</field>

           <field name="arch" type="xml">

               <form string="Test Base">

                   <field name="name"/>

                   <field name="code"/>

               </form>
charlesraj
on 5/31/13, 9:35 AM

</field>

    </record>

    <record model="ir.ui.view" id="test_base_tree">

        <field name="name">test.base.tree</field>

        <field name="model">test.base</field>

        <field name="type">tree</field>

        <field name="arch" type="xml">

            <tree string="Test Base">

                <field name="name"/>

                <field name="code"/>

            </tree>

        </field>
charlesraj
on 5/31/13, 9:36 AM

</record>

    <record model="ir.actions.act_window" id="action_test_seq">

        <field name="name">Test Base</field>

        <field name="res_model">test.base</field>

        <field name="view_type">form</field>

        <field name="view_mode"> tree,form</field>

    </record>

    <menuitem id="menu_test_base_main" name="Test Base"/>

    <menuitem id="menu_test_base" parent="menu_test_base_main" name="Test Base" action="action_test_seq"/>

</data>

</openerp>

charlesraj
on 5/31/13, 9:36 AM

will you please mention is there any error int the form view.

charlesraj
on 5/31/13, 9:37 AM

So far I cannot see any wrong code in your view. Seems legit to me. Only things I would recomment is to delete the <field name="type"> Tag from your view. This is deprecated. You mention the type in <tree>, <form> or <search> Tag. 2nd thing: add "version="7.0" attribute to your view tag e.g. <form string="foo" version="7.0">. This may not solve the problem but is better code.

Andreas Maertens
on 5/31/13, 9:49 AM

thanks for your valuable suggestions. i delete the things that you mention. i specify the action for the parent menu through the UI. then when click on the menu some error msg shown like this.. Uncaught TypeError: object is not a function http://localhost:8069/web/webclient/js?db=demo_c:3225. when pressing the ok button . the page will appear with the search bar and form view buttton on the right side. Edit ,create button on the left and 'more' button on the middle other fields are empty. when click on edit save and discard button is appeared,no other fields will be present. .

charlesraj
on 5/31/13, 10:21 AM

I Could help better if you could put your code in a separate answer on your question. On bottom you find a button "Answer". Please insert your code here. I'll need your modelfile and the viewfile. If that's provided maybe I can help you on that. Please take care to make it readable (preformated text).

Andreas Maertens
on 6/3/13, 4:09 AM

thanks for your contribution sir .. i just now found a solutions for the problem. "i add the menu action for the parent menu in the xml view" now the view problem is ok. then only one problem in the .py file there display an error that no module found..

charlesraj
on 6/3/13, 4:17 AM

shall i put the .py file.

charlesraj
on 6/3/13, 4:18 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

0 follower(s)

Stats

Asked: 5/24/13, 3:00 AM
Seen: 929 times
Last updated: 3/16/15, 8:10 AM