如何计算openerp中的函数字段和数据库字段
how calculate function fields and db fields in openerp
我在获取函数字段以在一个列表中计算折扣、税金和总计时遇到问题。
我正在使用 openerp 7。这是我的代码:
def _total(self,cr,uid,ids,field_names,args,context=None):
res = {}
for record in self.browse(cr,uid,ids,context=context):
res[record.id]={
'total': record.cantidad * record.precioe,
'total1': record.total * record.descuento / 100,
'total2': record.total - record.total1,
'iva': record.total2 * 16 / 100,
'gran_total': record.iva + record.total2,
}
return res
和
'cantidad': fields.integer('Cantidad', multi='calc'),
'precioe': fields.float('Costo', multi='calc'),
#'precio_equipo': fields.related('equipo', 'precio', string='Precio', type='float', readonly=True),
'total': fields.function(_total, type='float', method=True, string='Total', store=True, multi='calc'),
'descuento': fields.integer('Descuento', multi='calc'),
'total1': fields.function(_total, type='float', method=True, string='Total descuento', store=True, multi='calc'),
'total2': fields.function(_total, type='float', method=True, string='subtotal', store=True, multi='calc'),
'iva': fields.function(_total, type='float', method=True, string='IVA', store=True, multi='calc'),
'gran_total': fields.function(_total, type='float', method=True, string='Total', store=True, multi='calc'),
}
而且我只能计算第一个总和,其他值都显示为0。我做错了什么?
您必须使用两种方式来管理此代码。
1)使您的代码不使用任何功能字段来计算任何其他功能字段,如下所示。
def _total(self,cr,uid,ids,field_names,args,context=None):
res = {}
for record in self.browse(cr,uid,ids,context=context):
total = record.cantidad * record.precioe
total1 = total * record.descuento / 100
total2 = total - record.total1
iva = total2 * 16 / 100
gran_total = iva + total2
res[record.id]={
'total':total,
'total1': total1,
'total2': total2,
'iva': iva,
'gran_total': gran_total,
}
return res
您将获得每个字段的完美数据。
在您的代码中,当系统将获得 total
而 total1
系统的计算始终为“0”(零),因为其值仍未设置。
2) 使所有函数不同,并赋予适当的优先级来调用它们中的每一个。
就是这样。
我在获取函数字段以在一个列表中计算折扣、税金和总计时遇到问题。
我正在使用 openerp 7。这是我的代码:
def _total(self,cr,uid,ids,field_names,args,context=None):
res = {}
for record in self.browse(cr,uid,ids,context=context):
res[record.id]={
'total': record.cantidad * record.precioe,
'total1': record.total * record.descuento / 100,
'total2': record.total - record.total1,
'iva': record.total2 * 16 / 100,
'gran_total': record.iva + record.total2,
}
return res
和
'cantidad': fields.integer('Cantidad', multi='calc'),
'precioe': fields.float('Costo', multi='calc'),
#'precio_equipo': fields.related('equipo', 'precio', string='Precio', type='float', readonly=True),
'total': fields.function(_total, type='float', method=True, string='Total', store=True, multi='calc'),
'descuento': fields.integer('Descuento', multi='calc'),
'total1': fields.function(_total, type='float', method=True, string='Total descuento', store=True, multi='calc'),
'total2': fields.function(_total, type='float', method=True, string='subtotal', store=True, multi='calc'),
'iva': fields.function(_total, type='float', method=True, string='IVA', store=True, multi='calc'),
'gran_total': fields.function(_total, type='float', method=True, string='Total', store=True, multi='calc'),
}
而且我只能计算第一个总和,其他值都显示为0。我做错了什么?
您必须使用两种方式来管理此代码。 1)使您的代码不使用任何功能字段来计算任何其他功能字段,如下所示。
def _total(self,cr,uid,ids,field_names,args,context=None):
res = {}
for record in self.browse(cr,uid,ids,context=context):
total = record.cantidad * record.precioe
total1 = total * record.descuento / 100
total2 = total - record.total1
iva = total2 * 16 / 100
gran_total = iva + total2
res[record.id]={
'total':total,
'total1': total1,
'total2': total2,
'iva': iva,
'gran_total': gran_total,
}
return res
您将获得每个字段的完美数据。
在您的代码中,当系统将获得 total
而 total1
系统的计算始终为“0”(零),因为其值仍未设置。
2) 使所有函数不同,并赋予适当的优先级来调用它们中的每一个。
就是这样。