models.py 4.81 KB
Newer Older
Skia's avatar
Skia committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# -*- coding:utf-8 -*
#
# Copyright 2016,2017
# - Guillaume "Lo-J" Renaud <renaudg779@gmail.com>
# - 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.
#
#

guillaume-renaud's avatar
guillaume-renaud committed
26
from django.db import models
27
from django.utils.translation import ugettext_lazy as _
28
from django.urls import reverse
guillaume-renaud's avatar
guillaume-renaud committed
29
30
from django.conf import settings

31

guillaume-renaud's avatar
guillaume-renaud committed
32
from counter.models import Counter, ProductType
guillaume-renaud's avatar
guillaume-renaud committed
33

Sli's avatar
Sli committed
34

guillaume-renaud's avatar
guillaume-renaud committed
35
class Stock(models.Model):
Skia's avatar
Skia committed
36
37
38
    """
    The Stock class, this one is used to know how many products are left for a specific counter
    """
Sli's avatar
Sli committed
39
40
41

    name = models.CharField(_("name"), max_length=64)
    counter = models.OneToOneField(
42
43
44
45
        Counter,
        verbose_name=_("counter"),
        related_name="stock",
        on_delete=models.CASCADE,
Sli's avatar
Sli committed
46
    )
47

Skia's avatar
Skia committed
48
49
    def __str__(self):
        return "%s (%s)" % (self.name, self.counter)
50

Skia's avatar
Skia committed
51
    def get_absolute_url(self):
Sli's avatar
Sli committed
52
        return reverse("stock:list")
53

Skia's avatar
Skia committed
54
55
    def can_be_viewed_by(self, user):
        return user.is_in_group(settings.SITH_GROUP_COUNTER_ADMIN_ID)
guillaume-renaud's avatar
guillaume-renaud committed
56

Sli's avatar
Sli committed
57

58
class StockItem(models.Model):
Skia's avatar
Skia committed
59
60
61
    """
    The StockItem class, element of the stock
    """
Sli's avatar
Sli committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

    name = models.CharField(_("name"), max_length=64)
    unit_quantity = models.IntegerField(
        _("unit quantity"), default=0, help_text=_("number of element in one box")
    )
    effective_quantity = models.IntegerField(
        _("effective quantity"), default=0, help_text=_("number of box")
    )
    minimal_quantity = models.IntegerField(
        _("minimal quantity"),
        default=1,
        help_text=_(
            "if the effective quantity is less than the minimal, item is added to the shopping list"
        ),
    )
    type = models.ForeignKey(
        ProductType,
        related_name="stock_items",
        verbose_name=_("type"),
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
    )
85
86
87
    stock_owner = models.ForeignKey(
        Stock, related_name="items", on_delete=models.CASCADE
    )
Skia's avatar
Skia committed
88
89
90
91
92

    def __str__(self):
        return "%s" % (self.name)

    def get_absolute_url(self):
Sli's avatar
Sli committed
93
        return reverse("stock:items_list", kwargs={"stock_id": self.stock_owner.id})
Skia's avatar
Skia committed
94
95
96

    def can_be_viewed_by(self, user):
        return user.is_in_group(settings.SITH_GROUP_COUNTER_ADMIN_ID)
guillaume-renaud's avatar
guillaume-renaud committed
97

Sli's avatar
Sli committed
98

99
class ShoppingList(models.Model):
Skia's avatar
Skia committed
100
101
102
    """
    The ShoppingList class, used to make an history of the shopping lists
    """
Sli's avatar
Sli committed
103
104
105
106
107

    date = models.DateTimeField(_("date"))
    name = models.CharField(_("name"), max_length=64)
    todo = models.BooleanField(_("todo"))
    comment = models.TextField(_("comment"), null=True, blank=True)
108
109
110
    stock_owner = models.ForeignKey(
        Stock, null=True, related_name="shopping_lists", on_delete=models.CASCADE
    )
guillaume-renaud's avatar
guillaume-renaud committed
111

Skia's avatar
Skia committed
112
113
    def __str__(self):
        return "%s (%s)" % (self.name, self.date)
114

Skia's avatar
Skia committed
115
    def get_absolute_url(self):
Sli's avatar
Sli committed
116
        return reverse("stock:shoppinglist_list")
guillaume-renaud's avatar
guillaume-renaud committed
117

Skia's avatar
Skia committed
118
119
    def can_be_viewed_by(self, user):
        return user.is_in_group(settings.SITH_GROUP_COUNTER_ADMIN_ID)
120
121
122


class ShoppingListItem(models.Model):
Skia's avatar
Skia committed
123
124
    """
    """
Sli's avatar
Sli committed
125
126
127
128
129
130
131

    shopping_lists = models.ManyToManyField(
        ShoppingList,
        verbose_name=_("shopping lists"),
        related_name="shopping_items_to_buy",
    )
    stockitem_owner = models.ForeignKey(
132
        StockItem, related_name="shopping_item", null=True, on_delete=models.CASCADE
Sli's avatar
Sli committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
    )
    name = models.CharField(_("name"), max_length=64)
    type = models.ForeignKey(
        ProductType,
        related_name="shoppinglist_items",
        verbose_name=_("type"),
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
    )
    tobuy_quantity = models.IntegerField(
        _("quantity to buy"),
        default=6,
        help_text=_("quantity to buy during the next shopping session"),
    )
    bought_quantity = models.IntegerField(
        _("quantity bought"),
        default=0,
        help_text=_("quantity bought during the last shopping session"),
    )
Skia's avatar
Skia committed
153
154
155
156
157
158
159
160

    def __str__(self):
        return "%s - %s" % (self.name, self.shopping_lists.first())

    def can_be_viewed_by(self, user):
        return user.is_in_group(settings.SITH_GROUP_COUNTER_ADMIN_ID)

    def get_absolute_url(self):
Sli's avatar
Sli committed
161
        return reverse("stock:shoppinglist_list")