如何获得odoo 12中特定字段的总和



我试图获得员工所有请求的loan_amount字段的总和,然后从限制中减去它,我使用以下查询获取总数和employee_id:

self._cr.execute("SELECT sum(h.loan_amount) AS amount ,h.employee_id   From hr_loan h GROUP BY employee_id")

然后将其存储在字典中:

for i, row in enumerate(self._cr.dictfetchall()):
result = {}
result['employee_id'] = row['employee_id']
result['amount'] = row['amount']
i + 1

当调用函数时,它只返回字典的最后一个元素

这是功能:

@api.onchange('loan_amount', 'employee_id', 'limit')
def _check_contract_limitation(self):
for rec in self:

self._cr.execute("SELECT sum(h.loan_amount) AS amount ,h.employee_id   From hr_loan h GROUP BY employee_id")

for i, row in enumerate(self._cr.dictfetchall()):
result = {}
result['employee_id'] = row['employee_id']
result['amount'] = row['amount']
if rec.employee_id:
summation = result['amount']
print(type(summation))
i + 1

hr_contract = self.env['hr.contract'].search([('employee_id.name', '=', self.employee_id.name)], limit=1)
if not hr_contract:
raise ValidationError("you dont have contract record.")
elif hr_contract:
rec.limit = hr_contract.wage
if hr_contract.date_start:
fmt = '%Y-%m-%d'
d1 = hr_contract.date_start
d2 = datetime.datetime.now().date()
r = relativedelta.relativedelta(d2, d1)
contract_time = r.years
rec.a = contract_time
if contract_time == None:
raise ValidationError("check contract duration.")
else:
if contract_time >= 5 and datetime.datetime.now().date().year and rec.employee_id:
if rec.employee_id.loan_count == 0:
rec.limit = hr_contract.wage * 5
else:
rec.limit = hr_contract.wage * 5 - summation
elif contract_time >= 0 and contract_time < 5 and datetime.datetime.now().date().year and rec.employee_id:
if rec.employee_id.loan_count == 0:
rec.limit = hr_contract.wage * contract_time
else:
rec.limit = hr_contract.wage * contract_time - summation

问题是,每次它根据最后一名员工计算结果时,我如何将employee_id与他的loan_amount 映射

正如Kenly所说,在每个循环中都会重新初始化result变量。

另外,每次都设置两个相同的关键点,所以无论如何都会用迭代的最后一个值覆盖这两个关键点。

你可能想要这两种可能性中的一种:

1.

result = {}
for i, row in enumerate(self._cr.dictfetchall()):
result[i] = {}
result[i]['employee_id'] = row['employee_id']
result[i]['amount'] = row['amount']
# Here you will end with a result like
#{
#   0: {
#      'employee_id': 4,
#      'amount': 123
#   },
#   1: {...}
#}

否则,请查看dicts列表(2.(:

result = []
for row in self._cr.dictfetchall():
result.append({
'employee_id': row['employee_id'],
'amount': row['amount']
})
# Here you will end with a result like
#[
#   {
#      'employee_id': 4,
#      'amount': 123
#   },
#   {...}
#]

相关内容

  • 没有找到相关文章

最新更新