Improve age management for getting products and make get_product a part of counter model

This commit is contained in:
2024-12-22 12:27:58 +01:00
parent 372470b44b
commit eed434aeb2
4 changed files with 33 additions and 44 deletions

View File

@ -659,6 +659,34 @@ class Counter(models.Model):
# but they share the same primary key
return self.type == "BAR" and any(b.pk == customer.pk for b in self.barmen_list)
def get_products_for(self, customer: Customer) -> list[Product]:
"""
Get all allowed products for the provided customer on this counter
Prices will be annotated
"""
products = self.products.select_related("product_type").prefetch_related(
"buying_groups"
)
# Only include age appropriate products
age = customer.user.age
if customer.user.is_banned_alcohol:
age = min(age, 17)
products = products.filter(limit_age__lte=age)
# Compute special price for customer if he is a barmen on that bar
if self.customer_is_barman(customer):
products = products.annotate(price=F("special_selling_price"))
else:
products = products.annotate(price=F("selling_price"))
return [
product
for product in products.all()
if product.can_be_sold_to(customer.user)
]
class RefillingQuerySet(models.QuerySet):
def annotate_total(self) -> Self: