Backend (Django 4.2 + DRF): - accounts: phone OTP login, JWT, profile, addresses - catalog: categories, products, images, filters, search, hits/new/related - orders: creation with Idempotency-Key, stock reservation, status history, cancel - loyalty: bonus accounts, promo codes, gift certificates - payments: payment records and YooKassa webhook handling - content: static pages, banners, site settings - integration: 1C exchange (catalog sync, order push via outbox, statuses, bonuses) - common: error envelope, pagination, request-id, integration auth, admin dashboard Infrastructure: Dockerfile, docker-compose (Postgres, Redis, Celery worker/beat, Nginx), requirements split by environment, OpenAPI contracts for site and 1C, architecture doc. Tests: 22 pytest cases covering catalog, orders, idempotency, OTP, integration.
145 lines
4.9 KiB
Python
145 lines
4.9 KiB
Python
from decimal import Decimal
|
|
|
|
from django.conf import settings
|
|
from django.db import models
|
|
from django.utils import timezone
|
|
|
|
from apps.common.models import TimeStampedModel
|
|
|
|
|
|
class BonusAccount(TimeStampedModel):
|
|
user = models.OneToOneField(
|
|
settings.AUTH_USER_MODEL,
|
|
on_delete=models.CASCADE,
|
|
related_name="bonus_account",
|
|
)
|
|
balance = models.DecimalField(
|
|
max_digits=12, decimal_places=2, default=Decimal("0")
|
|
)
|
|
|
|
class Meta:
|
|
db_table = "loyalty_bonus_account"
|
|
verbose_name = "бонусный счёт"
|
|
verbose_name_plural = "бонусные счета"
|
|
|
|
def __str__(self) -> str:
|
|
return f"{self.user.phone}: {self.balance}"
|
|
|
|
|
|
class BonusTransaction(TimeStampedModel):
|
|
class Reason(models.TextChoices):
|
|
ACCRUAL = "accrual", "Начисление"
|
|
SPEND = "spend", "Списание"
|
|
REFUND = "refund", "Возврат"
|
|
ADJUSTMENT = "adjustment", "Корректировка"
|
|
|
|
account = models.ForeignKey(
|
|
BonusAccount, on_delete=models.CASCADE, related_name="transactions"
|
|
)
|
|
order = models.ForeignKey(
|
|
"orders.Order",
|
|
null=True,
|
|
blank=True,
|
|
on_delete=models.SET_NULL,
|
|
related_name="bonus_transactions",
|
|
)
|
|
amount = models.DecimalField(max_digits=12, decimal_places=2)
|
|
reason = models.CharField(max_length=20, choices=Reason.choices)
|
|
comment = models.CharField(max_length=255, blank=True)
|
|
|
|
class Meta:
|
|
db_table = "loyalty_bonus_transaction"
|
|
verbose_name = "бонусная операция"
|
|
verbose_name_plural = "бонусные операции"
|
|
ordering = ["-created_at"]
|
|
|
|
def __str__(self) -> str:
|
|
return f"{self.account.user.phone}: {self.amount}"
|
|
|
|
|
|
class PromoCode(TimeStampedModel):
|
|
class DiscountType(models.TextChoices):
|
|
PERCENT = "percent", "Процент"
|
|
FIXED = "fixed", "Фиксированная сумма"
|
|
|
|
code = models.CharField(max_length=64, unique=True)
|
|
discount_type = models.CharField(max_length=10, choices=DiscountType.choices)
|
|
value = models.DecimalField(max_digits=12, decimal_places=2)
|
|
min_order_total = models.DecimalField(
|
|
max_digits=12, decimal_places=2, default=Decimal("0")
|
|
)
|
|
valid_from = models.DateTimeField(null=True, blank=True)
|
|
valid_to = models.DateTimeField(null=True, blank=True)
|
|
max_uses = models.PositiveIntegerField(null=True, blank=True)
|
|
used_count = models.PositiveIntegerField(default=0)
|
|
is_active = models.BooleanField(default=True)
|
|
|
|
class Meta:
|
|
db_table = "loyalty_promo_code"
|
|
verbose_name = "промокод"
|
|
verbose_name_plural = "промокоды"
|
|
ordering = ["-created_at"]
|
|
|
|
def __str__(self) -> str:
|
|
return self.code
|
|
|
|
def is_valid(self, subtotal: Decimal) -> bool:
|
|
now = timezone.now()
|
|
if not self.is_active:
|
|
return False
|
|
if self.valid_from and now < self.valid_from:
|
|
return False
|
|
if self.valid_to and now > self.valid_to:
|
|
return False
|
|
if self.max_uses is not None and self.used_count >= self.max_uses:
|
|
return False
|
|
if subtotal < self.min_order_total:
|
|
return False
|
|
return True
|
|
|
|
def discount_for(self, subtotal: Decimal) -> Decimal:
|
|
if self.discount_type == self.DiscountType.PERCENT:
|
|
return (subtotal * self.value / Decimal("100")).quantize(
|
|
Decimal("0.01")
|
|
)
|
|
return min(self.value, subtotal)
|
|
|
|
|
|
class GiftCertificate(TimeStampedModel):
|
|
class Status(models.TextChoices):
|
|
ISSUED = "issued", "Выпущен"
|
|
ACTIVE = "active", "Активен"
|
|
USED = "used", "Использован"
|
|
EXPIRED = "expired", "Истёк"
|
|
|
|
code = models.CharField(max_length=64, unique=True)
|
|
nominal = models.DecimalField(max_digits=12, decimal_places=2)
|
|
balance = models.DecimalField(max_digits=12, decimal_places=2)
|
|
status = models.CharField(
|
|
max_length=20, choices=Status.choices, default=Status.ISSUED
|
|
)
|
|
issued_to = models.ForeignKey(
|
|
settings.AUTH_USER_MODEL,
|
|
null=True,
|
|
blank=True,
|
|
on_delete=models.SET_NULL,
|
|
related_name="certificates",
|
|
)
|
|
expires_at = models.DateTimeField(null=True, blank=True)
|
|
|
|
class Meta:
|
|
db_table = "loyalty_gift_certificate"
|
|
verbose_name = "подарочный сертификат"
|
|
verbose_name_plural = "подарочные сертификаты"
|
|
ordering = ["-created_at"]
|
|
|
|
def __str__(self) -> str:
|
|
return f"{self.code}: {self.balance}"
|
|
|
|
def is_valid(self) -> bool:
|
|
if self.status not in (self.Status.ISSUED, self.Status.ACTIVE):
|
|
return False
|
|
if self.expires_at and timezone.now() > self.expires_at:
|
|
return False
|
|
return self.balance > 0
|