Odoo Help


how to modify on change sale order?

wendy christine
on 11/25/13, 1:20 PM 2,884 views

Hi everybody,

I'm trying to modify the form view of sale order to add two columns automaticaly filled when the product is selected with the informations of two fields i've added in the product creation. I've identified into the sale.order.form view that there is an on change option linked with the product_id_change function but i want to know where is the python file containing this function. Thanks for youre help.


Gopakumar N G

--Gopakumar N G--
| 5 4 7
Cochin, India
--Gopakumar N G--

Project Lead at Bizweaver Technologies Pvt. Ltd.

Gopakumar N G
On 11/26/13, 9:01 AM


The onchange method product_id_change is defined in sale module in sale.py file's sale.order.line model.

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):
        if not  partner_id:
            raise osv.except_osv(_('No Customer Defined !'), _('You have to select a customer in the sales form !\nPlease set one customer before choosing a product.'))
        warning = {}
        product_uom_obj = self.pool.get('product.uom')
        partner_obj = self.pool.get('res.partner')
        product_obj = self.pool.get('product.product')
        if partner_id:
            lang = partner_obj.browse(cr, uid, partner_id).lang
        context = {'lang': lang, 'partner_id': partner_id}

        if not product:
            return {'value': {'th_weight': 0, 'product_packaging': False,
                'product_uos_qty': qty, 'tax_id':[]}, 'domain': {'product_uom': [],
                   'product_uos': []}}
        if not date_order:
            date_order = time.strftime('%Y-%m-%d')

        result = {}
        product_obj = product_obj.browse(cr, uid, product, context=context)
        if not packaging and product_obj.packaging:
            packaging = product_obj.packaging[0].id
            result['product_packaging'] = packaging

        if packaging:
            default_uom = product_obj.uom_id and product_obj.uom_id.id
            pack = self.pool.get('product.packaging').browse(cr, uid, packaging, context=context)
            q = product_uom_obj._compute_qty(cr, uid, uom, pack.qty, default_uom)
#            qty = qty - qty % q + q
            if qty and (q and not (qty % q) == 0):
                ean = pack.ean or _('(n/a)')
                qty_pack = pack.qty
                type_ul = pack.ul
                warn_msg = _("You selected a quantity of %d Units.\n"
                            "But it's not compatible with the selected packaging.\n"
                            "Here is a proposition of quantities according to the packaging:\n\n"
                            "EAN: %s Quantity: %s Type of ul: %s") % \
                                (qty, ean, qty_pack, type_ul.name)
                warning = {
                    'title': _('Picking Information !'),
                    'message': warn_msg
            result['product_uom_qty'] = qty

        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
                uos = False
        if product_obj.description_sale:
            result['notes'] = product_obj.description_sale
        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['delay'] = (product_obj.sale_delay or 0.0)
            result['tax_id'] = self.pool.get('account.fiscal.position').map_tax(cr, uid, fpos, product_obj.taxes_id)
            result.update({'type': product_obj.procure_method})

        if not flag:
            result['name'] = self.pool.get('product.product').name_get(cr, uid, [product_obj.id], context=context)[0][1]
        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
                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)],
                        [('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
                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
        if (product_obj.type=='product') and (product_obj.virtual_available * uom2.factor < qty * product_obj.uom_id.factor) \
          and (product_obj.procure_method=='make_to_stock'):
            warning = {
                'title': _('Not enough stock !'),
                'message': _('You plan to sell %.2f %s but you only have %.2f %s available !\nThe real stock is %.2f %s. (without reservations)') %
                    (qty, uom2 and uom2.name or product_obj.uom_id.name,
                     max(0,product_obj.virtual_available), product_obj.uom_id.name,
                     max(0,product_obj.qty_available), product_obj.uom_id.name)
        # get unit price
        if not pricelist:
            warning = {
                'title': 'No Pricelist !',
                    'You have to select a pricelist or a customer in the sales form !\n'
                    'Please set one before choosing a product.'
            price = self.pool.get('product.pricelist').price_get(cr, uid, [pricelist],
                    product, qty or 1.0, partner_id, {
                        'uom': uom,
                        'date': date_order,
            if price is False:
                warning = {
                    'title': 'No valid pricelist line found !',
                        "Couldn't find a pricelist line matching this product and quantity.\n"
                        "You have to change either the product, the quantity or the pricelist."
                result.update({'price_unit': price})
        return {'value': result, 'domain': domain, 'warning': warning}
wendy christine
On 11/26/13, 9:09 AM

Yes i know i've already find this method, the problem is that i want to add into this method the two parameters i've added into the product.product model to return them into the result of the product_id_change method and permit to fill automatically two colums i've added to the sale.order.line.

Which are the fields and what are their types? You can add them to the result dictionary in the above method.

Gopakumar N G
on 11/27/13, 12:52 AM

this is two fields i've created named x_departement and x_support, the type of the two fields is Many2one and they are each related with two models (x_departement_model and x_support_model) that i've created. Each of the two models are containing one char field.

Pop-IT, wendy christine
on 11/27/13, 4:25 AM

Did you inherit the sale.order model. If yes write the product_id_change function in your inherited model and in the product_id_change function's result dictionary add the two new fields and their value.

Gopakumar N G
on 11/27/13, 4:45 AM

Why do i have to inherit the sale.order model? If i only modify the sale.order.view adding two columns at the sale order line and addind my two new product fields as parameters of the product_id_change function and into the result dictionnary, this isn't good?

Pop-IT, wendy christine
on 11/27/13, 4:54 AM

i write you the steps i think i had to make: -create two new fields into the sale.order.line model with type many2one linked with the two fields i've created into the product.product model -add these two new fields as columns into the sale order view xml and into the onchange of the field product_id -add the two fields as parameters of the product_id_change function of the sale.py and retrieve their value to return it into the result dictionnary

Pop-IT, wendy christine
on 11/27/13, 5:01 AM

You have two models x_departement_model and x_support_model and you have added two fields in sale order model named x_departement and x_support. You can directly add the fields to the product_id_change method in sale.order model without inheriting. But it is not recommended because if you need to migrate to a new openerp version it will cause problems.

Gopakumar N G
on 11/27/13, 5:28 AM

oki i have understood the problem, i'm going to try what you've said and will come later to share the result. Thanks for your help

Pop-IT, wendy christine
on 11/27/13, 5:31 AM

About This Community

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


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

0 follower(s)


Asked: 11/25/13, 1:20 PM
Seen: 2884 times
Last updated: 3/16/15, 8:10 AM