import uuid from decimal import Decimal from django.conf import settings from django.core.validators import MinValueValidator, RegexValidator from django.db import models class PayrollOffCyclePayment(models.Model): """A cash fact; never a payroll input or a new employee statement.""" employee = models.ForeignKey( settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.PROTECT, related_name="off_cycle_payments", ) payee_name = models.CharField("Получатель", max_length=250) paid_on = models.DateField("Дата выдачи", db_index=True) amount = models.DecimalField( "Выдано", max_digits=19, decimal_places=2, validators=[MinValueValidator(Decimal("0.01"))], ) currency = models.CharField( max_length=3, default="RUB", validators=[RegexValidator(r"^[A-Z]{3}$")], ) source_period = models.ForeignKey( "finance.PayrollPeriod", null=True, blank=True, on_delete=models.PROTECT, related_name="off_cycle_payments", ) source_statement = models.ForeignKey( "finance.PayrollStatement", null=True, blank=True, on_delete=models.PROTECT, related_name="off_cycle_payments", ) note = models.CharField("Основание", max_length=1000) request_id = models.UUIDField(default=uuid.uuid4, unique=True, editable=False) created_by = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.PROTECT, related_name="off_cycle_payments_created", ) created_at = models.DateTimeField(auto_now_add=True) voided_at = models.DateTimeField(null=True, blank=True) voided_by = models.ForeignKey( settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.PROTECT, related_name="off_cycle_payments_voided", ) void_reason = models.CharField(max_length=1000, blank=True) class Meta: ordering = ["-paid_on", "-pk"] constraints = [ models.CheckConstraint( condition=models.Q(amount__gt=0), name="off_cycle_payment_positive", ), models.CheckConstraint( condition=( models.Q( voided_at__isnull=True, voided_by__isnull=True, void_reason="" ) | ( models.Q(voided_at__isnull=False, voided_by__isnull=False) & ~models.Q(void_reason="") ) ), name="off_cycle_payment_void_consistent", ), ] verbose_name = "Выплата вне периода" verbose_name_plural = "Выплаты вне периода"