From d1f183237d198831b5fd23b11d7c1cd0f0ab29a2 Mon Sep 17 00:00:00 2001 From: caoqianming Date: Fri, 17 Nov 2023 17:25:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20Attendance=E6=89=B9=E9=87=8F=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E6=97=B6=E6=89=A7=E8=A1=8C=E8=A6=86=E7=9B=96=E6=93=8D?= =?UTF-8?q?=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0018_alter_attendance_unique_together.py | 18 +++++++++++ apps/hrm/models.py | 3 ++ apps/hrm/serializers.py | 32 ++++++++++++------- 3 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 apps/hrm/migrations/0018_alter_attendance_unique_together.py diff --git a/apps/hrm/migrations/0018_alter_attendance_unique_together.py b/apps/hrm/migrations/0018_alter_attendance_unique_together.py new file mode 100644 index 00000000..b23a9f51 --- /dev/null +++ b/apps/hrm/migrations/0018_alter_attendance_unique_together.py @@ -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')}, + ), + ] diff --git a/apps/hrm/models.py b/apps/hrm/models.py index ea3afbf2..0e8893bb 100755 --- a/apps/hrm/models.py +++ b/apps/hrm/models.py @@ -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): """ diff --git a/apps/hrm/serializers.py b/apps/hrm/serializers.py index 53facc2c..506c7d08 100755 --- a/apps/hrm/serializers.py +++ b/apps/hrm/serializers.py @@ -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):