add discount models

This commit is contained in:
Tobias Brunner 2025-05-22 16:52:34 +02:00
parent a6a15150ea
commit 836187f2aa
No known key found for this signature in database
3 changed files with 195 additions and 2 deletions

View file

@ -508,6 +508,68 @@ class VSHNAppCatBaseFee(models.Model):
return f"{self.vshn_appcat_price_config.service.name} Base Fee - {self.amount} {self.currency}"
class ProgressiveDiscountModel(models.Model):
name = models.CharField(max_length=100)
description = models.TextField(blank=True)
active = models.BooleanField(default=True)
def __str__(self):
return self.name
def calculate_discount(self, base_rate, units):
"""Calculate price using progressive percentage discounts."""
final_price = 0
remaining_units = units
processed_units = 0
# Get all tiers sorted by threshold
discount_tiers = self.tiers.all().order_by("threshold")
for i, tier in enumerate(discount_tiers):
# Calculate how many units fall into this tier
if i < discount_tiers.count() - 1:
next_threshold = discount_tiers[i + 1].threshold
tier_units = min(remaining_units, next_threshold - processed_units)
else:
# Last tier handles all remaining units
tier_units = remaining_units
# Calculate discounted rate for this tier
discounted_rate = base_rate * (1 - tier.discount_percent / 100)
# Add the price for units in this tier
final_price += discounted_rate * tier_units
# Update tracking variables
remaining_units -= tier_units
processed_units += tier_units
# Exit if all units have been processed
if remaining_units <= 0:
break
return final_price
class DiscountTier(models.Model):
discount_model = models.ForeignKey(
ProgressiveDiscountModel, on_delete=models.CASCADE, related_name="tiers"
)
threshold = models.PositiveIntegerField(
help_text="Starting unit count for this tier"
)
discount_percent = models.DecimalField(
max_digits=5, decimal_places=2, help_text="Percentage discount applied (0-100)"
)
class Meta:
ordering = ["threshold"]
unique_together = ["discount_model", "threshold"]
def __str__(self):
return f"{self.discount_model.name}: {self.threshold}+ units → {self.discount_percent}% discount"
class VSHNAppCatPrice(models.Model):
class VariableUnit(models.TextChoices):
RAM = "RAM", "Memory (RAM)"
@ -538,6 +600,17 @@ class VSHNAppCatPrice(models.Model):
choices=Term.choices,
)
valid_from = models.DateTimeField(blank=True, null=True)
valid_to = models.DateTimeField(blank=True, null=True)
discount_model = models.ForeignKey(
ProgressiveDiscountModel,
on_delete=models.SET_NULL,
null=True,
blank=True,
related_name="price_configs",
)
def __str__(self):
return f"{self.service.name} - {self.get_variable_unit_display()} based pricing"
@ -567,7 +640,15 @@ class VSHNAppCatPrice(models.Model):
if number_of_units < 0:
raise ValueError("Number of units cannot be negative")
total_price = base_fee + (unit_rate * number_of_units)
# Apply discount model if available
if self.discount_model and self.discount_model.active:
discounted_price = self.discount_model.calculate_discount(
unit_rate, number_of_units
)
total_price = base_fee + discounted_price
else:
total_price = base_fee + (unit_rate * number_of_units)
return total_price