feat: Attendance批量创建时执行覆盖操作

This commit is contained in:
caoqianming 2023-11-17 17:25:46 +08:00
parent 31814f2495
commit d1f183237d
3 changed files with 41 additions and 12 deletions

View File

@ -0,0 +1,18 @@
# Generated by Django 3.2.12 on 2023-11-17 09:24
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('mtm', '0024_auto_20231116_1416'),
('hrm', '0017_auto_20231117_1700'),
]
operations = [
migrations.AlterUniqueTogether(
name='attendance',
unique_together={('employee', 'work_date', 'shift')},
),
]

View File

@ -107,6 +107,9 @@ class Attendance(CommonADModel):
choices=ATT_STATE_CHOICES, default='pending', help_text=str(ATT_STATE_CHOICES)) choices=ATT_STATE_CHOICES, default='pending', help_text=str(ATT_STATE_CHOICES))
note = models.TextField('备注信息', default='') note = models.TextField('备注信息', default='')
class Meta:
unique_together = ('employee', 'work_date', 'shift')
class ClockRecord(BaseModel): class ClockRecord(BaseModel):
""" """

View File

@ -280,19 +280,27 @@ class AttendanceSerializer(CustomModelSerializer):
def create(self, validated_data): def create(self, validated_data):
shift = validated_data['shift'] shift = validated_data['shift']
work_date = validated_data['work_date'] work_date = validated_data['work_date']
att = Attendance.objects.filter(
start_time_o = shift.start_time_o employee=validated_data['employee'], work_date=work_date, shift=shift).first()
end_time_o = shift.end_time_o if att:
if end_time_o >= start_time_o: att.note = validated_data.get('note', '')
validated_data['work_time_start'] = datetime.datetime.combine( att.state = validated_data['state']
work_date, start_time_o) att.update_by = self.context['request'].user
validated_data['work_time_end'] = datetime.datetime.combine( att.save()
work_date, end_time_o) return att
else: else:
validated_data['work_time_start'] = datetime.datetime.combine( start_time_o = shift.start_time_o
work_date, start_time_o) - datetime.timedelta(days=1) end_time_o = shift.end_time_o
validated_data['work_time_end'] = datetime.datetime.combine( if end_time_o >= start_time_o:
work_date, end_time_o) validated_data['work_time_start'] = datetime.datetime.combine(
work_date, start_time_o)
validated_data['work_time_end'] = datetime.datetime.combine(
work_date, end_time_o)
else:
validated_data['work_time_start'] = datetime.datetime.combine(
work_date, start_time_o) - datetime.timedelta(days=1)
validated_data['work_time_end'] = datetime.datetime.combine(
work_date, end_time_o)
return super().create(validated_data) return super().create(validated_data)
def update(self, instance, validated_data): def update(self, instance, validated_data):