简体   繁体   English

OpenErp中两个字段的简单乘法

[英]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; 首先将c4定义为user1576199的答案中指定的功能字段,然后创建Atul的答案中定义的on_change函数;

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... 但是,将on_change放在值c1到c3而不是c4上,就像这样...

 <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. c4是一个功能性字段,但是您仍然可以在其他字段的on_change value子句中返回它,并且屏幕上的值将刷新,但不会存储在数据库中,除非您在c4字段中添加store = True定义。

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. 如果用户更改c1,c2或c3字段中的任何一个,则将调用on_change方法,并且将重新计算总数并在显示屏上刷新。

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. 或以其他方式放置按钮,在按钮上单击将这两个字段值相乘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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