我创建了一个计算一天早餐,午餐和晚餐费用的模块,我需要在其他日子(第2天,第3天......)再次计算它,而无需再次复制代码,所以我们可以输入我们想要的任意天数,是否有任何建议可以做到这一点 我的模块看起来像:
from openerp.osv import fields, orm
class daily_allowances(orm.Model):
def _compute_breakfast(self, cr, uid, ids, name, args, context=None):
x = {}
for record in self.browse(cr,uid,ids,context):
x[record.id] = record.breakfast_allowances - record.breakfast_cost
return x
def _compute_lunch(self, cr, uid, ids, name, args, context=None):
x = {}
for record in self.browse(cr,uid,ids,context):
x[record.id] = record.lunch_allowances - record.lunch_cost
return x
def _compute_dinner(self, cr, uid, ids, name, args, context=None):
x = {}
for record in self.browse(cr,uid,ids,context):
x[record.id] = record.dinner_allowances - record.dinner_cost
return x
_name = 'daily.allowances'
_columns = {
'employee_id': fields.many2one('hr.employee', string="Employee Name", required=True),
'trip_date': fields.date('date of the trip', required=False),
'daily_allowances': fields.float('Daily Allowances', required=False),
'breakfast_allowances': fields.float('Breakfast Allowances', required=False),
'breakfast_date': fields.datetime('Date', required=False),
'brekfast_cost': fields.float('Costed', required=False),
'breakfast_remaining': fields.function(_compute_breakfast, store=True,string='Breakfast Balance'),
'lunch_allowances': fields.float('Lunch Allowances', required=False),
'lunch_date': fields.datetime('Date', required=False),
'lunch_cost': fields.float('Costed', required=False),
'lunch_remaining': fields.function(_compute_breakfast, store=True,string='Breakfast Balance'),
'dinner_allowances': fields.float('Dinner Allowances', required=False),
'dinner_date': fields.datetime('Date', required=False),
'dinner_cost': fields.float('Costed', required=False),
'dinner_remaining': fields.function(_compute_breakfast, store=True,string='Breakfast Balance'),
}
答案 0 :(得分:1)
看起来您的模块设置为每天跟踪每日津贴。这意味着您需要为每位员工创建每天的新记录,并输入他们每餐所花费的金额。
这似乎很好。
一个观察结果:您有一个trip_date
字段,因此您不需要breakfast_date
,lunch_date
和dinner_date
字段 - 它们都应该是相同。
如果您需要更多帮助,您将向我们展示您如何调用此代码以及xml视图文件。