Odoo Help

1

[SOLVED]Got a problem with ON CHANGE FUNCTION help please....How will i going to divide metric_tons to quantity?

By
Alcaline
on 10/17/14, 3:58 AM 1,901 views

 

I override the ONCHANGE_PRODUCT_ID and add a computation for metric_tons and product_qty in PURCHASE. It will display also the quotient in the field of metric_tons: It doesnt show the value of metric_tons it return 0.00. why is it like that?? Please point my mistake or guide me to solve this. Thanks

 

def onchange_product_id(self, cr, uid, ids, pricelist_id, product_id, qty, uom_id,
            partner_id, date_order=False, fiscal_position_id=False, date_planned=False,
            name=False, price_unit=False,m=None, mmm=0.00,context=None):
        """
        #onchange handler of product_id.
        """
        if context is None:
            context = {}
 
        res = {'value': {'price_unit': price_unit or 0.0, 'name': name or '', 'product_uom' : uom_id or False}}
        if not product_id:
            return res
 
        product_product = self.pool.get('product.product')
        product_uom = self.pool.get('product.uom')
        res_partner = self.pool.get('res.partner')
        product_supplierinfo = self.pool.get('product.supplierinfo')
        product_pricelist = self.pool.get('product.pricelist')
        account_fiscal_position = self.pool.get('account.fiscal.position')
        account_tax = self.pool.get('account.tax')
 
        # - check for the presence of partner_id and pricelist_id
        #if not partner_id:
        #    raise osv.except_osv(_('No Partner!'), _('Select a partner in purchase order to choose a product.'))
        #if not pricelist_id:
        #    raise osv.except_osv(_('No Pricelist !'), _('Select a price list in the purchase order form before choosing a product.'))
 
        # - determine name and notes based on product in partner lang.
        context_partner = context.copy()
        if partner_id:
            lang = res_partner.browse(cr, uid, partner_id).lang
            context_partner.update( {'lang': lang, 'partner_id': partner_id} )
        product = product_product.browse(cr, uid, product_id, context=context_partner)
        name = product.name
        if product.description_purchase:
            name += '\n' + product.description_purchase
        res['value'].update({'name': name})
 
        # - set a domain on product_uom
        res['domain'] = {'product_uom': [('category_id','=',product.uom_id.category_id.id)]}
 
        # - check that uom and product uom belong to the same category
        product_uom_po_id = product.uom_po_id.id
        if not uom_id:
            uom_id = product_uom_po_id
 
        if product.uom_id.category_id.id != product_uom.browse(cr, uid, uom_id, context=context).category_id.id:
            if self._check_product_uom_group(cr, uid, context=context):
                res['warning'] = {'title': ('Warning!'), 'message': ('Selected Unit of Measure does not belong to the same category as the product Unit of Measure.')}
            uom_id = product_uom_po_id
 
        res['value'].update({'product_uom': uom_id})
 
        # - determine product_qty and date_planned based on seller info
        if not date_order:
            date_order = fields.date.context_today(self,cr,uid,context=context)
 
 
        supplierinfo = False
        for supplier in product.seller_ids:
            if partner_id and (supplier.name.id == partner_id):
                supplierinfo = supplier
                if supplierinfo.product_uom.id != uom_id:
                    res['warning'] = {'title': _('Warning!'), 'message': _('The selected supplier only sells this product by %s') % supplierinfo.product_uom.name }
                min_qty = product_uom._compute_qty(cr, uid, supplierinfo.product_uom.id, supplierinfo.min_qty, to_uom_id=uom_id)
                if (qty or 0.0) < min_qty: # If the supplier quantity is greater than entered from user, set minimal.
                    if qty:
                        res['warning'] = {'title': _('Warning!'), 'message': _('The selected supplier has a minimal quantity set to %s %s, you should not purchase less.') % (supplierinfo.min_qty, supplierinfo.product_uom.name)}
                    qty = min_qty
        dt = self._get_date_planned(cr, uid, supplierinfo, date_order, context=context).strftime(DEFAULT_SERVER_DATETIME_FORMAT)
        qty = qty or 1.0
        res['value'].update({'date_planned': date_planned or dt})
        if qty:
            res['value'].update({'product_qty': qty})
 
        # - determine price_unit and taxes_id
        if pricelist_id:
            price = product_pricelist.price_get(cr, uid, [pricelist_id],
                    product.id, qty or 1.0, partner_id or False, {'uom': uom_id, 'date': date_order})[pricelist_id]
        else:
            price = product.standard_price
 
        taxes = account_tax.browse(cr, uid, map(lambda x: x.id, product.supplier_taxes_id))
        fpos = fiscal_position_id and account_fiscal_position.browse(cr, uid, fiscal_position_id, context=context) or False
        taxes_ids = account_fiscal_position.map_tax(cr, uid, fpos, taxes)
        res['value'].update({'price_unit': price, 'taxes_id': taxes_ids})
        
        if qty:
                    mmm = mmm / qty

        res['value'].update({'metric_tons':mmm})

          
          
        return res

0
Alcaline
On 10/20/14, 3:29 AM

My probelem got solved by adding new def onchange_name:

def onchange_rss2(self, cr, uid, ids,rss_l = None,rss_w = None ,b= None,metric = 0.0,qty = 0.0,context=None):
        
        l = 0.0
        w = 0.0
        
        if rss_l :
             l = rss_l / 25.40
             netsvc.Logger().notifyChannel("TESSSSSTTTwiiiittout444",netsvc.LOG_INFO, '' +'' + str(l))  
        if rss_w :
             w = rss_w / 25.40
             netsvc.Logger().notifyChannel("TESSSSSTTTwiiiittout444",netsvc.LOG_INFO, '' +'' + str(l))
        
        a = 0.00
           
        if w > 0 and l > 0 and b > 0:
            n = (round(w, 2) * round(l,2) * round(b,2))
            a = 1000000.00 / (n/ 1550.00 )
       
       m = 0.0    
        
        try:
            #netsvc.Logger().notifyChannel("TESSSSSTTTT11111111111111000000000000000000000000000000000AAAA",netsvc.LOG_INFO, '' +'' + str(mmm) + '  ::' + str(qty))
            m = round(a / qty)
            netsvc.Logger().notifyChannel("TESSSSSTTTTmmmmmmmmmmmmmmmmmmmmmm",netsvc.LOG_INFO, '' +'' + str(m))
        except ZeroDivisionError:
                m = 0.0
          
        value = {'value':{'rss_li': l,
                          'rss_wi': w,
                          'metric_tons':m}}
        return value

    def onchange_product_id(self, cr, uid, ids, pricelist_id = False, product_id=False, qty=0.0, uom_id=False,
            partner_id=False, date_order=False, fiscal_position_id=False, date_planned=False,
            name=False, price_unit=False, metric = 0.0,context=None):
        '''
        values = super(purchase_order_line, self).onchange_product_id(cr, uid, ids, pricelist_id, product_id, qty, uom_id,
                                                                      partner_id, date_order, fiscal_position_id, date_planned,
                                                                      name, price_unit, n, metric_tons, context=context)
        '''
        """
        #onchange handler of product_id.
        """
        if context is None:
            context = {}
 
        res = {'value': {'price_unit': price_unit or 0.0, 'name': name or '', 'product_uom' : uom_id or False}}
        if not product_id:
            return res
 
        product_product = self.pool.get('product.product')
        product_uom = self.pool.get('product.uom')
        res_partner = self.pool.get('res.partner')
        product_supplierinfo = self.pool.get('product.supplierinfo')
        product_pricelist = self.pool.get('product.pricelist')
        account_fiscal_position = self.pool.get('account.fiscal.position')
        account_tax = self.pool.get('account.tax')
 
        # - check for the presence of partner_id and pricelist_id
        #if not partner_id:
        #    raise osv.except_osv(_('No Partner!'), _('Select a partner in purchase order to choose a product.'))
        #if not pricelist_id:
        #    raise osv.except_osv(_('No Pricelist !'), _('Select a price list in the purchase order form before choosing a product.'))
 
        # - determine name and notes based on product in partner lang.
        context_partner = context.copy()
        if partner_id:
            lang = res_partner.browse(cr, uid, partner_id).lang
            context_partner.update( {'lang': lang, 'partner_id': partner_id} )
        product = product_product.browse(cr, uid, product_id, context=context_partner)
        name = product.name
        if product.description_purchase:
            name += '\n' + product.description_purchase
        res['value'].update({'name': name})
 
        # - set a domain on product_uom
        res['domain'] = {'product_uom': [('category_id','=',product.uom_id.category_id.id)]}
 
        # - check that uom and product uom belong to the same category
        product_uom_po_id = product.uom_po_id.id
        if not uom_id:
            uom_id = product_uom_po_id
 
        if product.uom_id.category_id.id != product_uom.browse(cr, uid, uom_id, context=context).category_id.id:
            if self._check_product_uom_group(cr, uid, context=context):
                res['warning'] = {'title': ('Warning!'), 'message': ('Selected Unit of Measure does not belong to the same category as the product Unit of Measure.')}
            uom_id = product_uom_po_id
 
        res['value'].update({'product_uom': uom_id})
 
        # - determine product_qty and date_planned based on seller info
        if not date_order:
            date_order = fields.date.context_today(self,cr,uid,context=context)
 
 
        supplierinfo = False
        for supplier in product.seller_ids:
            if partner_id and (supplier.name.id == partner_id):
                supplierinfo = supplier
                if supplierinfo.product_uom.id != uom_id:
                    res['warning'] = {'title': _('Warning!'), 'message': _('The selected supplier only sells this product by %s') % supplierinfo.product_uom.name }
                min_qty = product_uom._compute_qty(cr, uid, supplierinfo.product_uom.id, supplierinfo.min_qty, to_uom_id=uom_id)
                if (qty or 0.0) < min_qty: # If the supplier quantity is greater than entered from user, set minimal.
                    if qty:
                        res['warning'] = {'title': _('Warning!'), 'message': _('The selected supplier has a minimal quantity set to %s %s, you should not purchase less.') % (supplierinfo.min_qty, supplierinfo.product_uom.name)}
                    qty = min_qty
        dt = self._get_date_planned(cr, uid, supplierinfo, date_order, context=context).strftime(DEFAULT_SERVER_DATETIME_FORMAT)
        qty = qty or 1.0
        res['value'].update({'date_planned': date_planned or dt})
        if qty:
            res['value'].update({'product_qty': qty})
 
        # - determine price_unit and taxes_id
        if pricelist_id:
            price = product_pricelist.price_get(cr, uid, [pricelist_id],
                    product.id, qty or 1.0, partner_id or False, {'uom': uom_id, 'date': date_order})[pricelist_id]
        else:
            price = product.standard_price
 
        taxes = account_tax.browse(cr, uid, map(lambda x: x.id, product.supplier_taxes_id))
        fpos = fiscal_position_id and account_fiscal_position.browse(cr, uid, fiscal_position_id, context=context) or False
        taxes_ids = account_fiscal_position.map_tax(cr, uid, fpos, taxes)
        res['value'].update({'price_unit': price, 'taxes_id': taxes_ids})
          
        netsvc.Logger().notifyChannel("TESSSSSTTTTmmmmmmmmmmmmmmmmmmmmmmetric    -----      ",netsvc.LOG_INFO, '' +'' + str(metric))  
        '''
        try:
            #netsvc.Logger().notifyChannel("TESSSSSTTTT11111111111111000000000000000000000000000000000AAAA",netsvc.LOG_INFO, '' +'' + str(mmm) + '  ::' + str(qty))
            m = metric / qty
            netsvc.Logger().notifyChannel("TESSSSSTTTTmmmmmmmmmmmmmmmmmmmmmm",netsvc.LOG_INFO, '' +'' + str(m))
        except ZeroDivisionError:
            m = 0.0
        '''
        res['value'].update({'metric_tons2':qty})
          
          
        return res

 

In xml:

<record id="purchase_order_line_form" model="ir.ui.view">
        <field name="name">biz1.purchase.order.line.form</field>
        <field name="model">purchase.order.line</field>
        <field name="priority" eval="16"/>
        <field name="inherit_id" ref="purchase.purchase_order_line_form"/>
        <field name="arch" type="xml">
             <xpath expr="//field[@name='product_id']" position="after">
                     <field name = "item_number"/>
                    <field name = "openerp_part_number"/>
                    <field name = "sap_part_number"/>
                    <field name = "rss_l"  on_change = "onchange_rss2(rss_l, rss_w, rss_11,metric_tons,metric_tons2)"/>
                    <field name = "rss_w"  on_change = "onchange_rss2(rss_l, rss_w, rss_11,metric_tons,metric_tons2)"/>
                    <field name = "rss_11"  on_change = "onchange_rss2(rss_l, rss_w, rss_11,metric_tons,metric_tons2)"/> <!-- on_change="onchange_product_id(parent.pricelist_id,product_id,product_qty,product_uom,parent.partner_id,parent.date_order,parent.fiscal_position,date_planned,name,price_unit,rss_l,rss_w,rss_11,context)"/>-->
                    <field name = "rss_li" />
                    <field name = "rss_wi" />
                    <field name = "metric_tons"/>
                    <field name = "metric_tons2" invisible="True" on_change = "onchange_rss2(rss_l, rss_w, rss_11,metric_tons,metric_tons2)"/>
             </xpath>
        </field>
        </record>

 

0

Hi Lee,

There may be the chance that in the parameter mmm=0.00 do not pass any arguments from xml where you have call this on change.

def onchange_product_id(self, cr, uid, ids, pricelist_id, product_id, qty, uom_id,
            partner_id, date_order=False, fiscal_position_id=False, date_planned=False,
            name=False, price_unit=False,m=None, mmm=0.00,context=None):

so, in mmm=0.00 have no any argument passed at that time it have only number 0.00 and it will also set as 0.00 according to calculation : 

 if qty:
                    mmm = mmm / qty

        res['value'].update({'metric_tons':mmm})

Please have a look on the parameter passed on this on change method.

You can also wtire down xml part of this on change. It is helpful to resolve this.

Let me know your result.

then how will i going to do that? do you have any idea please help me??

Alcaline
on 10/18/14, 8:59 AM

can you put xml portion when you have written on_change for the product ? It will help us to resolve this issue.

Emipro Technologies Pvt. Ltd.
on 10/20/14, 1:03 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.

Register

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

1 follower(s)

Stats

Asked: 10/17/14, 3:58 AM
Seen: 1901 times
Last updated: 3/16/15, 8:10 AM