@@ -112,25 +112,34 @@ class HrAttendance(models.Model):
|
||||
record.is_last_month = (first_day_last_month <= record.current_data <= last_day_last_month)
|
||||
|
||||
def compute_company_id(self):
|
||||
employee_date_records = {}
|
||||
for att in self.env["hr.attendance"].browse(self._context.get('active_ids', self._context.get('active_id'))):
|
||||
date_records = {}
|
||||
# 获取记录的日期(忽略时间部分)
|
||||
record_date = att.current_data if att.current_data else None
|
||||
if record_date:
|
||||
# 将记录按日期分组
|
||||
if record_date not in date_records:
|
||||
date_records[record_date] = []
|
||||
date_records[record_date].append(att.id)
|
||||
if not (att.employee_id and att.current_data):
|
||||
continue # 跳过缺少员工或日期的记录
|
||||
|
||||
# 处理重复记录:每组保留第一条,删除其余
|
||||
for date, record_ids in date_records.items():
|
||||
if len(record_ids) > 1:
|
||||
# 保留第一条记录,删除其余
|
||||
ids_to_keep = record_ids[0]
|
||||
ids_to_delete = record_ids[1:]
|
||||
# 获取员工ID和记录日期(忽略时间部分)
|
||||
employee_id = att.employee_id.id
|
||||
# 创建唯一分组键 (员工ID, 日期)
|
||||
group_key = (employee_id, att.current_data)
|
||||
|
||||
# 将记录按员工+日期分组
|
||||
if group_key not in employee_date_records:
|
||||
employee_date_records[group_key] = []
|
||||
employee_date_records[group_key].append(att.id)
|
||||
|
||||
# 处理重复记录:每个员工每天只保留第一条记录
|
||||
for (employee_id, date), record_ids in employee_date_records.items():
|
||||
if len(record_ids) > 1:
|
||||
# 保留第一条记录,其余标记为删除
|
||||
ids_to_keep = record_ids[0]
|
||||
ids_to_delete = record_ids[1:]
|
||||
|
||||
# 执行删除操作
|
||||
if ids_to_delete:
|
||||
self.env["hr.attendance"].browse(ids_to_delete).unlink()
|
||||
|
||||
|
||||
|
||||
# @api.model
|
||||
# def web_read_group(self, domain, fields, groupby, limit=None, offset=0, orderby=False, lazy=True):
|
||||
# result = super().web_read_group(domain, fields, groupby, limit, offset, orderby, lazy)
|
||||
|
||||
Reference in New Issue
Block a user