简体   繁体   中英

Simple Multiplication of two fields in OpenErp

I have two fields in a class, in a third field i need the multiplication result of the two fields declared before.

For example:

_columns = {
            'Item' : fields.integer('Items'),
            'Fecha': fields.date('Fecha del Documento', required=True, select=True),
            'Codigo Arancelario' : fields.integer('Codigo Arancelario'),
            'Descripcion Arancelaria' : fields.char('Descripcion Arancelaria', size=42, required = True, translate = True), 
            'Especificaciones Tecnicas' : fields.char('Especificaciones Tecnicas', size=60, required = True, translate = True), 
            'Cantidad' : fields.float('Cantidad'), 'Unidad de Medida': fields.many2one('product.uom', 'Unidad de Medida'),
            'Precio Unitario Declarado' : fields.float('Precio Unitario Declarado'), 'Moneda' : fields.many2one('res.currency', 'Moneda'),
            'Valor En Divisas' : Result of the multiplication of 'Precio Unitario Declarado' * 'Cantidad',
            'notas' : fields.text('Notas'),
            }

Should i use a function field for such a simple calculation?

Is there a simpler way to accomplish this?

function field will be the good option to do such kind of operation. here is the sample code for multiplication of two field using functional field.

def multi_a_b(self, cr, uid, ids, name, arg, context=None):
    res = {}
    for record in self.browse(cr, uid, ids,context):
        res[record.id] = record.field_a * record.field_b
    return res

_columns = {
    'field_a': fields.integer('A'),
    'field_b': fields.integer('B'),
    'field_c': fields.function(multi_a_b, type='integer', 'C'),
}

You need to do a combination of these answers.

First define c4 as a functional field as specified in the answer from user1576199, then create the on_change function as defined in the answer from Atul;

def onchange_value(self, cr, uid, ids, c1 = 0.0, c2 = 0.0, c3 = 0.0, context = None):
     return {'value': {'c4': c1 + c2 + c3}}

but, put the on_change on the values c1 to c3, not c4, like this...

 <field name="c1" on_change="onchange_value(c1, c2, c3, context) />
 <field name="c2" on_change="onchange_value(c1, c2, c3, context)/>
 <field name="c3" on_change="onchange_value(c1, c2, c3, context)/>
 <field name="c4" />

c4 is a functional field but you can still return it in an on_change value clause for other field(s) and the value on the screen will be refreshed but it will not be stored in the database unless you add store = True to the c4 field definition.

If the user changes any of the c1, c2 or c3 fields, the on_change method will be called and the total will be re-calculated and refreshed on the display.

Here is possible solution

from osv import osv, fields

def multi_price_qty(self, cr, uid, ids, name, arg, context=None):
    res = {}
    for product in self.browse(cr, uid, ids,context):
        res[product.id] = product.price * product.qty
    return res

class abs_product(osv.Model):
    _name = "abs.product"
    _columns = {
    'code': fields.char('Code', size=32),
    'description': fields.char('Description', size=64),
    'sizeunit': fields.char('Size /Unit', size=32),
    'qty': fields.integer('Quantity', size=32),
    'price': fields.integer('Price'),
    'pullout': fields.char('Pull-Out', size=32),
    'return': fields.char('Return', size=32),
    'offtake': fields.char('Offtake', size=32),
    'weeksales': fields.char('Weekly Sales', size=32),
    'date': fields.date('Date'),
    'totalprice': fields.function(multi_price_qty, type='integer', 'Total Price'),
    }
abs_product()

I think function field is ok for this type of calculation.

OR another way you can put a button, on button click multiply this two filed values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM