feat: Attendance批量创建时执行覆盖操作
This commit is contained in:
parent
31814f2495
commit
d1f183237d
|
@ -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')},
|
||||
),
|
||||
]
|
|
@ -107,6 +107,9 @@ class Attendance(CommonADModel):
|
|||
choices=ATT_STATE_CHOICES, default='pending', help_text=str(ATT_STATE_CHOICES))
|
||||
note = models.TextField('备注信息', default='')
|
||||
|
||||
class Meta:
|
||||
unique_together = ('employee', 'work_date', 'shift')
|
||||
|
||||
|
||||
class ClockRecord(BaseModel):
|
||||
"""
|
||||
|
|
|
@ -280,19 +280,27 @@ class AttendanceSerializer(CustomModelSerializer):
|
|||
def create(self, validated_data):
|
||||
shift = validated_data['shift']
|
||||
work_date = validated_data['work_date']
|
||||
|
||||
start_time_o = shift.start_time_o
|
||||
end_time_o = shift.end_time_o
|
||||
if end_time_o >= start_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)
|
||||
att = Attendance.objects.filter(
|
||||
employee=validated_data['employee'], work_date=work_date, shift=shift).first()
|
||||
if att:
|
||||
att.note = validated_data.get('note', '')
|
||||
att.state = validated_data['state']
|
||||
att.update_by = self.context['request'].user
|
||||
att.save()
|
||||
return att
|
||||
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)
|
||||
start_time_o = shift.start_time_o
|
||||
end_time_o = shift.end_time_o
|
||||
if end_time_o >= start_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)
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
|
|
Loading…
Reference in New Issue