diff --git a/store/models.py b/store/models.py index 6a47796..fdbd90f 100644 --- a/store/models.py +++ b/store/models.py @@ -14,7 +14,8 @@ from django.contrib.auth.models import AbstractUser, UserManager as _UserManager from django.core.files.base import ContentFile from django.core.validators import MinValueValidator, MaxValueValidator from django.db import models -from django.db.models import F, Case, When, DecimalField, Prefetch +from django.db.models import F, Case, When, DecimalField, Prefetch, Max, Q, ExpressionWrapper +from django.db.models.functions import Ceil from django.db.models.lookups import GreaterThan from django.utils import timezone from django.utils.translation import gettext_lazy as _ @@ -91,6 +92,7 @@ class Category(MPTTModel): @property def commission_price(self): + """ Get commission from object or from its parent """ if not self.commission and self.parent_id: return self.parent.commission_price else: @@ -266,11 +268,13 @@ class ChecklistQuerySet(models.QuerySet): return self.order_by(F('status_updated_at').desc(nulls_last=True)) def annotate_price_rub(self): - # FIXME: implement price_rub in DB query - return self - - yuan_rate = GlobalSettings.load().yuan_rate - return self.annotate(_price_rub=F('price_yuan') * yuan_rate) + return self.annotate( + _yuan_rate=Case( + When(price_snapshot_id__isnull=False, then=F('price_snapshot__yuan_rate')), + default=GlobalSettings.load().yuan_rate + ), + _price_rub=Ceil(F('_yuan_rate') * F('price_yuan')) + ) def annotate_commission_rub(self): # FIXME: implement category commission in DB query @@ -418,10 +422,9 @@ class Checklist(models.Model): @property def price_rub(self) -> int: - # FIXME: implement price_rub in DB query - # # Prefer annotated field for calculation - # if hasattr(self, '_price_rub'): - # return self._price_rub + # Prefer annotated field for calculation + if hasattr(self, '_price_rub'): + return self._price_rub # Get saved prices if self.price_snapshot_id: