nk20/apps/sheets/models.py

290 lines
6.6 KiB
Python
Raw Normal View History

# Copyright (C) 2018-2021 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
from django.db import models
from django.urls import reverse_lazy
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from member.models import Club
from note.models import Note, Transaction
class Sheet(models.Model):
name = models.CharField(
max_length=255,
verbose_name=_("name"),
)
date = models.DateTimeField(
verbose_name=_("start date"),
default=timezone.now,
)
description = models.TextField(
verbose_name=_("description"),
)
visible = models.BooleanField(
default=False,
verbose_name=_("visible"),
help_text=_("the note sheet will be private until this field is checked."),
)
def get_absolute_url(self):
return reverse_lazy('sheets:sheet_detail', args=(self.pk,))
def __str__(self):
return self.name
class Meta:
verbose_name = _("note sheet")
verbose_name_plural = _("note sheets")
class Food(models.Model):
name = models.CharField(
max_length=255,
verbose_name=_("food"),
)
sheet = models.ForeignKey(
Sheet,
on_delete=models.CASCADE,
verbose_name=_("note sheet"),
)
price = models.IntegerField(
verbose_name=_("price"),
)
club = models.ForeignKey(
Club,
on_delete=models.PROTECT,
verbose_name=_("destination club"),
)
available = models.BooleanField(
default=True,
verbose_name=_("available"),
help_text=_("If set to false, this option won't be offered (in case of out of stock)"),
)
def __str__(self):
return self.name
class Meta:
verbose_name = _("food")
verbose_name_plural = _("food")
class FoodOption(models.Model):
name = models.CharField(
max_length=255,
verbose_name=_("name"),
)
food = models.ForeignKey(
Food,
on_delete=models.CASCADE,
verbose_name=_("food"),
)
extra_cost = models.IntegerField(
default=0,
verbose_name=_("extra cost"),
)
available = models.BooleanField(
default=True,
verbose_name=_("available"),
help_text=_("If set to false, this option won't be offered (in case of out of stock)"),
)
def __str__(self):
return self.name
class Meta:
verbose_name = _("food option")
verbose_name_plural = _("food options")
class Meal(models.Model):
sheet = models.ForeignKey(
Sheet,
on_delete=models.CASCADE,
verbose_name=_("note sheet"),
)
name = models.CharField(
max_length=255,
verbose_name=_("name"),
)
content = models.ManyToManyField(
Food,
verbose_name=_("content"),
)
price = models.IntegerField(
verbose_name=_("price"),
)
available = models.BooleanField(
default=True,
verbose_name=_("available"),
help_text=_("If set to false, this option won't be offered (in case of out of stock)"),
)
def __str__(self):
return _("meal").capitalize() + " " + self.name
class Meta:
verbose_name = _("meal")
verbose_name_plural = _("meals")
class Order(models.Model):
sheet = models.ForeignKey(
Sheet,
on_delete=models.PROTECT,
verbose_name=_("note sheet"),
)
note = models.ForeignKey(
Note,
on_delete=models.PROTECT,
verbose_name=_("note"),
)
date = models.DateTimeField(
verbose_name=_("date"),
auto_now_add=True,
)
class Meta:
verbose_name = _("order")
verbose_name_plural = _("orders")
class OrderedMeal(models.Model):
order = models.ForeignKey(
Order,
on_delete=models.PROTECT,
verbose_name=_("order"),
)
meal = models.ForeignKey(
Meal,
on_delete=models.PROTECT,
verbose_name=_("meal"),
)
gift = models.IntegerField(
verbose_name=_("gift"),
)
class Meta:
verbose_name = _("ordered meal")
verbose_name_plural = _("ordered meals")
class OrderedFood(models.Model):
order = models.ForeignKey(
Order,
on_delete=models.PROTECT,
verbose_name=_("order"),
)
meal = models.ForeignKey(
OrderedMeal,
on_delete=models.SET_NULL,
null=True,
default=None,
verbose_name=_("ordered meal"),
)
food = models.ForeignKey(
Food,
on_delete=models.PROTECT,
verbose_name=_("food"),
)
options = models.ManyToManyField(
FoodOption,
blank=True,
verbose_name=_("options"),
)
remark = models.TextField(
blank=True,
default="",
verbose_name=_("remark"),
)
priority = models.CharField(
max_length=64,
blank=True,
default="",
verbose_name=_("priority request"),
)
gift = models.IntegerField(
verbose_name=_("gift"),
)
number = models.IntegerField(
verbose_name=_("number"),
help_text=_("How many times the user ordered this."),
)
status = models.CharField(
max_length=8,
choices=[
('QUEUED', _("queued")),
('READY', _("ready")),
('SERVED', _("served")),
('CANCELED', _("canceled")),
],
default='QUEUED',
verbose_name=_("status"),
)
served_date = models.DateTimeField(
null=True,
default=None,
verbose_name=_("served date")
)
class Meta:
verbose_name = _("ordered food")
verbose_name_plural = _("ordered food")
class SheetOrderTransaction(Transaction):
ordered_food = models.ForeignKey(
OrderedFood,
on_delete=models.PROTECT,
verbose_name=_("ordered food"),
)
@property
def type(self):
return _("note sheet")
@property
def get_price(self):
if self.ordered_food.meal:
return self.ordered_food.meal.meal.price + self.ordered_food.meal.gift + sum(
sum(opt.extra_cost for opt in ordered_food.options.all())
for ordered_food in self.ordered_food.meal.orderedfood_set.exclude(status='CANCELED').all())
elif self.ordered_food.status == 'CANCELED':
return 0
else:
return self.ordered_food.food.price + self.ordered_food.gift \
+ sum(opt.extra_cost for opt in self.ordered_food.options.all())
class Meta:
verbose_name = _("sheet order transaction")
verbose_name_plural = _("sheet order transactions")