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

add a field to the sale.order

By
mohamed abdelwahed
on 5/18/13, 2:53 PM 2,091 views

hello everybody i creted my module that inherit the sale.order.line and i modified tha product_id_change() method to allow it to fill my new field but i want to show this field in sale.order instead of the sale.order.line view here is my code so anyone can help me please :)

class sale_order_line(osv.osv):
_name='sale.order.line'
_inherit = ['sale.order.line','sale.order']
_columns={
    'x_discount': fields.char('promotion',size=255),
    }

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]
        result['name'] = result_name
        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})   

    #warning message pour les promotions quantitative


    #iddd=product_obj.browse(cr,uid,product,context=None)
    product_idd=product_obj['id']
    result['x_discount']=""
    cr.execute(
                'SELECT min_quantity,price_surcharge '\
                'FROM product_pricelist_item WHERE(product_id=%s)',(product_idd,))
    promotion = cr.dictfetchone()

    if promotion:

        qty_test=promotion['min_quantity']
        price_surchage_test=promotion['price_surcharge']
        prix_new=price+price_surchage_test
        result['x_discount']="ce produit contient une promotion sur "+str(qty_test)+" unites : nouveau prix est "+str(prix_new)+"\n"
        #raise osv.except_osv(_('Warning !'), _(qty_test))



    #end warning message 

    if warning_msgs:
        warning = {
                   'title': _('Configuration Error!'),
                   'message' : warning_msgs
                }
    return {'value': result, 'domain': domain, 'warning': warning}

sale_order_line()

and here is my view code

<record id="view_order_form_inherit" model="ir.ui.view">
        <field name="name">promotion.form</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">
            <xpath expr="/form/sheet/notebook/page/field[@name='order_line']" position="before">
                <field name="x_discount"/> 
            </xpath>
        </field>
    </record>

here is a screenshot (sorry icant's post link)

h t t p://farm3.staticflickr.com/2841/8750484275_cb498ed270_c.jpg

2
le_dilem
On 5/18/13, 8:55 PM

Hi If I understand your example ps : it's not worth it to copy all the program , res = super(sale_order_line, self).product_id_change(..... it is sufficient to execute your product_id_change,and allows you to add what you want

class sale_order_line(osv.osv): 
    _inherit = "sale.order.line"


    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):
            res = super(sale_order_line, self).product_id_change(cr, uid, ids, pricelist, product, qty=qty,
            uom=uom, qty_uos=qty_uos, uos=uos, name=name, partner_id=partner_id,
            lang=lang, update_tax=update_tax, date_order=date_order, packaging=packaging, fiscal_position=fiscal_position, flag=flag, context=context)

        if product:

           promotion =  self.pool.get('product_pricelist_item').browse(cr, uid, product, context=context)   

            if promotion.min_quantity: 

               res['value'].update({'x_discount': "ce que tu veux Ecrire" or False})


        return res

you can create a function that automatically. Like This. on class sale.order

def _promo(self, cr, uid, ids, field_name, arg, context=None):
        result = {}
        for sale in self.browse(cr, uid, ids, context=context):
            result[sale.id] = ''
            for line in sale.order_line:
                result[sale.id] = (line.x_discount) or  ""
        return result

'x_discount': fields.function(_promo, method=True, string=_("promo"),type="char", store = True, help = _("xxx"), translate=True),

thnaks l'ami that was really awsome but my probleme is here in this screenshot please take a look h t t p://farm3.staticflickr.com/2841/8750484275_cb498ed270_c.jpg

mohamed abdelwahed
on 5/18/13, 10:08 PM

you mean i must inherit the sale.order class instead of sale.order.line and i override the product_id_change() ??

mohamed abdelwahed
on 5/19/13, 8:27 AM
0
mohamed abdelwahed
On 5/19/13, 8:49 AM

thank you for your help it's really appreciated but when i inherit the sale.order and i define your function i get this error

    'x_discount': fields.function(_promo, method=True, string=_("promotion"),type="char", store = True, help = _("ce champ affiche les promotions"), translate=True),

NameError: name '_promo' is not defined

you must add your fonction on sale.order. ok ?

le_dilem
on 5/19/13, 8:51 AM

i added it to the original sale.order and i tried to inherit the sale.order also but no luck :/

mohamed abdelwahed
on 5/19/13, 8:55 AM

you must create an insert, never changes in the source

le_dilem
on 5/19/13, 9:04 AM

class sale_order(osv.osv):

def _promo(self, cr, uid, ids, x_discount, arg, context=None): 
    result = {} 
    for sale in self.browse(cr, uid, ids, context=context): 
        result[sale.id] = '' 
        for line in sale.order_line: 
            result[sale.id] = (line.x_discount) or "" 
    return result


_name='sale.order'
_inherit='sale.order'
_columns={
    'x_discount': fields.function(_promo, method=True, string=_("promotion"),type="char", store = True, help = _("ce champ affiche les promotions"), translate=True),
    }

sale_orde

mohamed abdelwahed
on 5/19/13, 9:23 AM

this my new code but when i fill this field from the product_change it wont get filled

mohamed abdelwahed
on 5/19/13, 9:24 AM

you have create 2 module inherit. 1 - on sale.order.line (onchange_product_id) it's Good. 2 - on sale order you have create fonction dating information that is in your line.

le_dilem
on 5/19/13, 9:35 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: 5/18/13, 2:53 PM
Seen: 2091 times
Last updated: 3/16/15, 8:10 AM