2017-04-24 15:51:12 +00:00
|
|
|
# -*- coding:utf-8 -*
|
|
|
|
#
|
|
|
|
# Copyright 2016,2017
|
|
|
|
# - Skia <skia@libskia.so>
|
|
|
|
#
|
|
|
|
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
|
|
|
|
# http://ae.utbm.fr.
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify it under
|
|
|
|
# the terms of the GNU General Public License a published by the Free Software
|
|
|
|
# Foundation; either version 3 of the License, or (at your option) any later
|
|
|
|
# version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along with
|
|
|
|
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
|
|
|
|
# Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
#
|
|
|
|
#
|
2022-09-25 19:29:42 +00:00
|
|
|
import typing
|
|
|
|
from typing import List
|
2017-04-24 15:51:12 +00:00
|
|
|
|
2022-09-25 19:29:42 +00:00
|
|
|
from django.conf import settings
|
2016-07-21 23:19:50 +00:00
|
|
|
from django.db import models, DataError
|
2022-09-25 19:29:42 +00:00
|
|
|
from django.db.models import Sum, F
|
2019-11-05 18:49:55 +00:00
|
|
|
from django.utils.functional import cached_property
|
2022-09-25 19:29:42 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2016-07-21 23:19:50 +00:00
|
|
|
|
|
|
|
from accounting.models import CurrencyField
|
2022-09-25 19:29:42 +00:00
|
|
|
from core.models import Group, User
|
2017-06-12 07:50:08 +00:00
|
|
|
from counter.models import Counter, Product, Selling, Refilling
|
2022-09-25 19:29:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_eboutic_products(user: User) -> List[Product]:
|
|
|
|
products = (
|
|
|
|
Counter.objects.get(type="EBOUTIC")
|
|
|
|
.products.filter(product_type__isnull=False)
|
|
|
|
.filter(archived=False)
|
|
|
|
.filter(limit_age__lte=user.age)
|
|
|
|
.annotate(category=F("product_type__name"))
|
|
|
|
)
|
|
|
|
return [p for p in products if p.can_be_sold_to(user)]
|
2017-06-12 07:50:08 +00:00
|
|
|
|
2016-07-21 23:19:50 +00:00
|
|
|
|
|
|
|
class Basket(models.Model):
|
|
|
|
"""
|
2016-07-26 17:39:19 +00:00
|
|
|
Basket is built when the user connects to an eboutic page
|
2016-07-21 23:19:50 +00:00
|
|
|
"""
|
2018-10-04 19:29:19 +00:00
|
|
|
|
|
|
|
user = models.ForeignKey(
|
2019-10-05 17:05:56 +00:00
|
|
|
User,
|
|
|
|
related_name="baskets",
|
|
|
|
verbose_name=_("user"),
|
|
|
|
blank=False,
|
|
|
|
on_delete=models.CASCADE,
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|
|
|
|
date = models.DateTimeField(_("date"), auto_now=True)
|
2016-07-21 23:19:50 +00:00
|
|
|
|
2022-09-25 19:29:42 +00:00
|
|
|
def add_product(self, p: Product, q: int = 1):
|
|
|
|
"""
|
|
|
|
Given p an object of the Product model and q an integer,
|
|
|
|
add q items corresponding to this Product from the basket.
|
|
|
|
|
|
|
|
If this function is called with a product not in the basket, no error will be raised
|
|
|
|
"""
|
2016-07-26 17:39:19 +00:00
|
|
|
item = self.items.filter(product_id=p.id).first()
|
|
|
|
if item is None:
|
2018-10-04 19:29:19 +00:00
|
|
|
BasketItem(
|
|
|
|
basket=self,
|
|
|
|
product_id=p.id,
|
|
|
|
product_name=p.name,
|
|
|
|
type_id=p.product_type.id,
|
|
|
|
quantity=q,
|
|
|
|
product_unit_price=p.selling_price,
|
|
|
|
).save()
|
2016-07-26 17:39:19 +00:00
|
|
|
else:
|
|
|
|
item.quantity += q
|
|
|
|
item.save()
|
|
|
|
|
2022-09-25 19:29:42 +00:00
|
|
|
def del_product(self, p: Product, q: int = 1):
|
|
|
|
"""
|
|
|
|
Given p an object of the Product model and q an integer,
|
|
|
|
remove q items corresponding to this Product from the basket.
|
|
|
|
|
|
|
|
If this function is called with a product not in the basket, no error will be raised
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
item = self.items.get(product_id=p.id)
|
|
|
|
except BasketItem.DoesNotExist:
|
|
|
|
return
|
|
|
|
item.quantity -= q
|
2016-07-26 17:39:19 +00:00
|
|
|
if item.quantity <= 0:
|
|
|
|
item.delete()
|
2022-10-31 15:15:16 +00:00
|
|
|
else:
|
|
|
|
item.save()
|
2016-07-26 17:39:19 +00:00
|
|
|
|
2022-09-25 19:29:42 +00:00
|
|
|
def clear(self) -> None:
|
|
|
|
"""
|
|
|
|
Remove all items from this basket without deleting the basket
|
|
|
|
"""
|
|
|
|
BasketItem.objects.filter(basket=self).delete()
|
|
|
|
|
2019-11-05 18:49:55 +00:00
|
|
|
@cached_property
|
2022-09-25 19:29:42 +00:00
|
|
|
def contains_refilling_item(self) -> bool:
|
2019-11-05 18:49:55 +00:00
|
|
|
return self.items.filter(
|
|
|
|
type_id=settings.SITH_COUNTER_PRODUCTTYPE_REFILLING
|
|
|
|
).exists()
|
|
|
|
|
2022-09-25 19:29:42 +00:00
|
|
|
def get_total(self) -> float:
|
|
|
|
total = self.items.aggregate(
|
|
|
|
total=Sum(F("quantity") * F("product_unit_price"))
|
|
|
|
)["total"]
|
|
|
|
return float(total) if total is not None else 0
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_session(cls, session) -> typing.Union["Basket", None]:
|
|
|
|
"""
|
|
|
|
Given an HttpRequest django object, return the basket used in the current session
|
|
|
|
if it exists else create a new one and return it
|
|
|
|
"""
|
|
|
|
if "basket_id" in session:
|
|
|
|
try:
|
|
|
|
return cls.objects.get(id=session["basket_id"])
|
|
|
|
except cls.DoesNotExist:
|
|
|
|
return None
|
|
|
|
return None
|
2016-07-21 23:19:50 +00:00
|
|
|
|
2016-07-26 17:39:19 +00:00
|
|
|
def __str__(self):
|
2016-12-08 14:16:42 +00:00
|
|
|
return "%s's basket (%d items)" % (self.user, self.items.all().count())
|
2016-07-26 17:39:19 +00:00
|
|
|
|
2017-06-12 07:50:08 +00:00
|
|
|
|
2016-07-21 23:19:50 +00:00
|
|
|
class Invoice(models.Model):
|
|
|
|
"""
|
|
|
|
Invoices are generated once the payment has been validated
|
|
|
|
"""
|
2018-10-04 19:29:19 +00:00
|
|
|
|
|
|
|
user = models.ForeignKey(
|
2019-10-05 17:05:56 +00:00
|
|
|
User,
|
|
|
|
related_name="invoices",
|
|
|
|
verbose_name=_("user"),
|
|
|
|
blank=False,
|
|
|
|
on_delete=models.CASCADE,
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|
|
|
|
date = models.DateTimeField(_("date"), auto_now=True)
|
2016-07-21 23:19:50 +00:00
|
|
|
validated = models.BooleanField(_("validated"), default=False)
|
|
|
|
|
2022-09-25 19:29:42 +00:00
|
|
|
def get_total(self) -> float:
|
|
|
|
total = self.items.aggregate(
|
|
|
|
total=Sum(F("quantity") * F("product_unit_price"))
|
|
|
|
)["total"]
|
|
|
|
return float(total) if total is not None else 0
|
2016-07-21 23:19:50 +00:00
|
|
|
|
|
|
|
def validate(self, *args, **kwargs):
|
|
|
|
if self.validated:
|
|
|
|
raise DataError(_("Invoice already validated"))
|
2016-07-26 17:58:36 +00:00
|
|
|
from counter.models import Customer
|
2018-10-04 19:29:19 +00:00
|
|
|
|
2016-07-26 17:58:36 +00:00
|
|
|
if not Customer.objects.filter(user=self.user).exists():
|
2016-08-18 17:52:20 +00:00
|
|
|
number = Customer.objects.count() + 1
|
2018-10-04 19:29:19 +00:00
|
|
|
Customer(
|
|
|
|
user=self.user,
|
|
|
|
account_id=Customer.generate_account_id(number),
|
|
|
|
amount=0,
|
|
|
|
).save()
|
2016-08-18 17:52:20 +00:00
|
|
|
eboutic = Counter.objects.filter(type="EBOUTIC").first()
|
|
|
|
for i in self.items.all():
|
|
|
|
if i.type_id == settings.SITH_COUNTER_PRODUCTTYPE_REFILLING:
|
|
|
|
new = Refilling(
|
2017-06-12 07:50:08 +00:00
|
|
|
counter=eboutic,
|
|
|
|
customer=self.user.customer,
|
|
|
|
operator=self.user,
|
|
|
|
amount=i.product_unit_price * i.quantity,
|
|
|
|
payment_method="CARD",
|
|
|
|
bank="OTHER",
|
|
|
|
date=self.date,
|
|
|
|
)
|
2016-08-18 17:52:20 +00:00
|
|
|
new.save()
|
|
|
|
else:
|
|
|
|
product = Product.objects.filter(id=i.product_id).first()
|
|
|
|
new = Selling(
|
2017-06-12 07:50:08 +00:00
|
|
|
label=i.product_name,
|
|
|
|
counter=eboutic,
|
|
|
|
club=product.club,
|
|
|
|
product=product,
|
|
|
|
seller=self.user,
|
|
|
|
customer=self.user.customer,
|
|
|
|
unit_price=i.product_unit_price,
|
|
|
|
quantity=i.quantity,
|
|
|
|
payment_method="CARD",
|
|
|
|
is_validated=True,
|
|
|
|
date=self.date,
|
|
|
|
)
|
2016-08-18 17:52:20 +00:00
|
|
|
new.save()
|
2016-07-26 13:10:48 +00:00
|
|
|
self.validated = True
|
|
|
|
self.save()
|
2016-07-21 23:19:50 +00:00
|
|
|
|
2022-09-25 19:29:42 +00:00
|
|
|
def __str__(self):
|
|
|
|
return "%s - %s - %s" % (self.user, self.get_total(), self.date)
|
|
|
|
|
2017-06-12 07:50:08 +00:00
|
|
|
|
2016-07-21 23:19:50 +00:00
|
|
|
class AbstractBaseItem(models.Model):
|
2018-10-04 19:29:19 +00:00
|
|
|
product_id = models.IntegerField(_("product id"))
|
|
|
|
product_name = models.CharField(_("product name"), max_length=255)
|
|
|
|
type_id = models.IntegerField(_("product type id"))
|
|
|
|
product_unit_price = CurrencyField(_("unit price"))
|
2022-09-25 19:29:42 +00:00
|
|
|
quantity = models.PositiveIntegerField(_("quantity"))
|
2016-07-21 23:19:50 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
def __str__(self):
|
2018-10-04 19:29:19 +00:00
|
|
|
return "Item: %s (%s) x%d" % (
|
|
|
|
self.product_name,
|
|
|
|
self.product_unit_price,
|
|
|
|
self.quantity,
|
|
|
|
)
|
2016-07-21 23:19:50 +00:00
|
|
|
|
2017-06-12 07:50:08 +00:00
|
|
|
|
2016-07-21 23:19:50 +00:00
|
|
|
class BasketItem(AbstractBaseItem):
|
2019-10-05 17:05:56 +00:00
|
|
|
basket = models.ForeignKey(
|
|
|
|
Basket, related_name="items", verbose_name=_("basket"), on_delete=models.CASCADE
|
|
|
|
)
|
2016-07-21 23:19:50 +00:00
|
|
|
|
2017-06-12 07:50:08 +00:00
|
|
|
|
2016-07-21 23:19:50 +00:00
|
|
|
class InvoiceItem(AbstractBaseItem):
|
2018-10-04 19:29:19 +00:00
|
|
|
invoice = models.ForeignKey(
|
2019-10-05 17:05:56 +00:00
|
|
|
Invoice,
|
|
|
|
related_name="items",
|
|
|
|
verbose_name=_("invoice"),
|
|
|
|
on_delete=models.CASCADE,
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|