This question has been flagged
2 Replies
8295 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.

Avatar
Discard

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

Author

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

Author

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

Author

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

Author

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

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...

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

Author

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..

Author

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

Author

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

Best Answer

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}
Avatar
Discard
Author

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

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. ;)

Author

from openerp.osv import fields,osv

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

Author

'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):

Author

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 = {

Author

'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}

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

Author

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

Author

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'

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.

Author

<?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>

Author

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.

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

Author

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

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

Author

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>
Author

</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>
Author

</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>

Author

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

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.

Author

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. .

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).

Author

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..

Author

shall i put the .py file.

Best Answer

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.

Avatar
Discard

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...