from django.conf import settings from django.db import models class DailyAllocation(models.Model): employee = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.PROTECT, related_name="project_days" ) date = models.DateField() status = models.CharField( max_length=12, choices=[("draft", "Черновик"), ("confirmed", "Подтверждено")], default="draft", ) hours = models.DecimalField(max_digits=6, decimal_places=3, null=True, blank=True) hours_source = models.CharField(max_length=30, default="unknown") attendance_record = models.ForeignKey( "attendance.AttendanceRecord", on_delete=models.SET_NULL, null=True ) note = models.TextField(blank=True) version = models.PositiveIntegerField(default=0) updated_at = models.DateTimeField(auto_now=True) class Meta: ordering = ["-date"] constraints = [ models.UniqueConstraint( fields=["employee", "date"], name="project_day_unique" ) ] class TimeAllocation(models.Model): day = models.ForeignKey( DailyAllocation, on_delete=models.CASCADE, related_name="allocations" ) project = models.ForeignKey( "projects.Project", null=True, blank=True, on_delete=models.PROTECT, related_name="time_allocations", ) activity = models.CharField( max_length=20, blank=True, choices=[ ("", "Проект"), ("department", "Работа отдела"), ("training", "Обучение"), ("support", "Поддержка"), ], ) percent = models.DecimalField(max_digits=5, decimal_places=2) class Meta: constraints = [ models.CheckConstraint( condition=models.Q(percent__gt=0, percent__lte=100), name="project_allocation_percent", ), models.CheckConstraint( condition=( models.Q(project__isnull=False, activity="") | models.Q( project__isnull=True, activity__in=["department", "training", "support"], ) ), name="project_allocation_target", ), ] class AllocationRevision(models.Model): day = models.ForeignKey( DailyAllocation, on_delete=models.PROTECT, related_name="revisions" ) actor = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT) snapshot = models.JSONField() created_at = models.DateTimeField(auto_now_add=True) class AllocationReminder(models.Model): employee = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) last_claim_date = models.DateField(null=True) snoozed_until = models.DateTimeField(null=True) enabled = models.BooleanField(default=True)