* WIP: annotate_commission_rub

This commit is contained in:
Phil Zhitnikov 2023-11-11 09:51:17 +04:00
parent 0ba7a8e881
commit 34f9d08f9a

View File

@ -277,15 +277,33 @@ class ChecklistQuerySet(models.QuerySet):
) )
def annotate_commission_rub(self): def annotate_commission_rub(self):
# FIXME: implement category commission in DB query default_commission = GlobalSettings.load().commission_rub
return self over_150k_commission = F('_price_rub') * settings.COMMISSION_OVER_150K
commission = GlobalSettings.load().commission_rub category_commission_is_zero_and_parent_present = (
return self.annotate(_commission_rub=Case( (Q(category__commission__isnull=True) | Q(category__commission=0)) & Q(category__parent__isnull=False)
When(GreaterThan(F("_price_rub"), 150_000), then=F("_price_rub") * settings.COMMISSION_OVER_150K), )
default=commission,
output_field=DecimalField() return self.annotate(
)) _category_commission_percent=Case(
When(category_commission_is_zero_and_parent_present, then=F('category__parent__commission')),
default=F('category__commission')
),
_category_commission=F('_category_commission_percent') * F('_price_rub') / 100,
_over_150k_commission=Case(
When(GreaterThan(F("_price_rub"), 150_000), then=over_150k_commission),
default=0,
output_field=DecimalField()
),
_commission_rub=Case(
When(price_snapshot_id__isnull=False, then=F('price_snapshot__commission_rub')),
default=Max(default_commission, F('_over_150k_commission'), F('_category_commission')),
output_field=DecimalField()
),
)
class PriceSnapshot(models.Model): class PriceSnapshot(models.Model):
@ -485,10 +503,9 @@ class Checklist(models.Model):
@property @property
def commission_rub(self) -> Decimal: def commission_rub(self) -> Decimal:
# FIXME: implement category commission in DB query # Prefer annotated field
# # Prefer annotated field if hasattr(self, '_commission_rub'):
# if hasattr(self, '_commission_rub'): return self._commission_rub
# return self._commission_rub
# Prefer saved value # Prefer saved value
if self.price_snapshot_id: if self.price_snapshot_id: