diff --git a/core/management/commands/populate.py b/core/management/commands/populate.py index 7bb2d0ef..76a145ae 100644 --- a/core/management/commands/populate.py +++ b/core/management/commands/populate.py @@ -41,7 +41,14 @@ from com.ics_calendar import IcsCalendar from com.models import News, NewsDate, Sith, Weekmail from core.models import BanGroup, Group, Page, PageRev, SithFile, User from core.utils import resize_image -from counter.models import Counter, Product, ProductType, ReturnableProduct, StudentCard +from counter.models import ( + Counter, + Price, + Product, + ProductType, + ReturnableProduct, + StudentCard, +) from election.models import Candidature, Election, ElectionList, Role from forum.models import Forum from pedagogy.models import UE @@ -368,125 +375,15 @@ class Command(BaseCommand): end_date=localdate() - timedelta(days=100), ) - p = ProductType.objects.create(name="Bières bouteilles") - c = ProductType.objects.create(name="Cotisations") - r = ProductType.objects.create(name="Rechargements") - verre = ProductType.objects.create(name="Verre") - cotis = Product.objects.create( - name="Cotis 1 semestre", - code="1SCOTIZ", - product_type=c, - purchase_price="15", - selling_price="15", - special_selling_price="15", - club=main_club, - ) - cotis2 = Product.objects.create( - name="Cotis 2 semestres", - code="2SCOTIZ", - product_type=c, - purchase_price="28", - selling_price="28", - special_selling_price="28", - club=main_club, - ) - refill = Product.objects.create( - name="Rechargement 15 €", - code="15REFILL", - product_type=r, - purchase_price="15", - selling_price="15", - special_selling_price="15", - club=main_club, - ) - barb = Product.objects.create( - name="Barbar", - code="BARB", - product_type=p, - purchase_price="1.50", - selling_price="1.7", - special_selling_price="1.6", - club=main_club, - limit_age=18, - ) - cble = Product.objects.create( - name="Chimay Bleue", - code="CBLE", - product_type=p, - purchase_price="1.50", - selling_price="1.7", - special_selling_price="1.6", - club=main_club, - limit_age=18, - ) - cons = Product.objects.create( - name="Consigne Eco-cup", - code="CONS", - product_type=verre, - purchase_price="1", - selling_price="1", - special_selling_price="1", - club=main_club, - ) - dcons = Product.objects.create( - name="Déconsigne Eco-cup", - code="DECO", - product_type=verre, - purchase_price="-1", - selling_price="-1", - special_selling_price="-1", - club=main_club, - ) - cors = Product.objects.create( - name="Corsendonk", - code="CORS", - product_type=p, - purchase_price="1.50", - selling_price="1.7", - special_selling_price="1.6", - club=main_club, - limit_age=18, - ) - carolus = Product.objects.create( - name="Carolus", - code="CARO", - product_type=p, - purchase_price="1.50", - selling_price="1.7", - special_selling_price="1.6", - club=main_club, - limit_age=18, - ) - Product.objects.create( - name="remboursement", - code="REMBOURS", - purchase_price="0", - selling_price="0", - special_selling_price="0", - club=refound, - ) - groups.subscribers.products.add( - cotis, cotis2, refill, barb, cble, cors, carolus - ) - groups.old_subscribers.products.add(cotis, cotis2) - - mde = Counter.objects.get(name="MDE") - mde.products.add(barb, cble, cons, dcons) - - eboutic = Counter.objects.get(name="Eboutic") - eboutic.products.add(barb, cotis, cotis2, refill) + self._create_products(groups, main_club, refound) Counter.objects.create(name="Carte AE", club=refound, type="OFFICE") - ReturnableProduct.objects.create( - product=cons, returned_product=dcons, max_return=3 - ) - # Add barman to counter Counter.sellers.through.objects.bulk_create( [ - Counter.sellers.through(counter_id=2, user=krophil), - Counter.sellers.through(counter=mde, user=skia), + Counter.sellers.through(counter_id=1, user=skia), # MDE + Counter.sellers.through(counter_id=2, user=krophil), # Foyer ] ) @@ -742,6 +639,131 @@ class Command(BaseCommand): ] ) + def _create_products( + self, groups: PopulatedGroups, main_club: Club, refound_club: Club + ): + beers_type, cotis_type, refill_type, verre_type = ( + ProductType.objects.bulk_create( + [ + ProductType(name="Bières bouteilles"), + ProductType(name="Cotisations"), + ProductType(name="Rechargements"), + ProductType(name="Verre"), + ] + ) + ) + cotis = Product.objects.create( + name="Cotis 1 semestre", + code="1SCOTIZ", + product_type=cotis_type, + purchase_price=15, + club=main_club, + ) + cotis2 = Product.objects.create( + name="Cotis 2 semestres", + code="2SCOTIZ", + product_type=cotis_type, + purchase_price="28", + club=main_club, + ) + refill = Product.objects.create( + name="Rechargement 15 €", + code="15REFILL", + product_type=refill_type, + purchase_price=15, + club=main_club, + ) + barb = Product.objects.create( + name="Barbar", + code="BARB", + product_type=beers_type, + purchase_price="1.50", + club=main_club, + limit_age=18, + ) + cble = Product.objects.create( + name="Chimay Bleue", + code="CBLE", + product_type=beers_type, + purchase_price="1.50", + club=main_club, + limit_age=18, + ) + cons = Product.objects.create( + name="Consigne Eco-cup", + code="CONS", + product_type=verre_type, + purchase_price="1", + club=main_club, + ) + dcons = Product.objects.create( + name="Déconsigne Eco-cup", + code="DECO", + product_type=verre_type, + purchase_price="-1", + club=main_club, + ) + cors = Product.objects.create( + name="Corsendonk", + code="CORS", + product_type=beers_type, + purchase_price="1.50", + club=main_club, + limit_age=18, + ) + carolus = Product.objects.create( + name="Carolus", + code="CARO", + product_type=beers_type, + purchase_price="1.50", + club=main_club, + limit_age=18, + ) + Product.objects.create( + name="remboursement", + code="REMBOURS", + purchase_price=0, + club=refound_club, + ) + ReturnableProduct.objects.create( + product=cons, returned_product=dcons, max_return=3 + ) + mde = Counter.objects.get(name="MDE") + mde.products.add(barb, cble, cons, dcons) + eboutic = Counter.objects.get(name="Eboutic") + eboutic.products.add(barb, cotis, cotis2, refill) + + cotis, cotis2, refill, barb, cble, cors, carolus, cons, dcons = ( + Price.objects.bulk_create( + [ + Price(product=cotis, amount=15), + Price(product=cotis2, amount=28), + Price(product=refill, amount=15), + Price(product=barb, amount=1.7), + Price(product=cble, amount=1.7), + Price(product=cors, amount=1.7), + Price(product=carolus, amount=1.7), + Price(product=cons, amount=1), + Price(product=dcons, amount=-1), + ] + ) + ) + Price.groups.through.objects.bulk_create( + [ + Price.groups.through(price=cotis, group=groups.subscribers), + Price.groups.through(price=cotis2, group=groups.subscribers), + Price.groups.through(price=refill, group=groups.subscribers), + Price.groups.through(price=barb, group=groups.subscribers), + Price.groups.through(price=cble, group=groups.subscribers), + Price.groups.through(price=cors, group=groups.subscribers), + Price.groups.through(price=carolus, group=groups.subscribers), + Price.groups.through(price=cotis, group=groups.old_subscribers), + Price.groups.through(price=cotis2, group=groups.old_subscribers), + Price.groups.through(price=cons, group=groups.old_subscribers), + Price.groups.through(price=dcons, group=groups.old_subscribers), + ] + ) + def _create_profile_pict(self, user: User): path = self.SAS_FIXTURE_PATH / "Family" / f"{user.username}.jpg" file = resize_image(Image.open(path), 400, "WEBP") diff --git a/core/management/commands/populate_more.py b/core/management/commands/populate_more.py index 562a46ad..47cb75d5 100644 --- a/core/management/commands/populate_more.py +++ b/core/management/commands/populate_more.py @@ -17,6 +17,7 @@ from counter.models import ( Counter, Customer, Permanency, + Price, Product, ProductType, Refilling, @@ -278,6 +279,7 @@ class Command(BaseCommand): # 2/3 of the products are owned by AE clubs = [ae, ae, ae, ae, ae, ae, *other_clubs] products = [] + prices = [] buying_groups = [] selling_places = [] for _ in range(200): @@ -288,25 +290,28 @@ class Command(BaseCommand): product_type=random.choice(categories), code="".join(self.faker.random_letters(length=random.randint(4, 8))), purchase_price=price, - selling_price=price, - special_selling_price=price - min(0.5, price), club=random.choice(clubs), limit_age=0 if random.random() > 0.2 else 18, - archived=bool(random.random() > 0.7), + archived=self.faker.boolean(60), ) products.append(product) - # there will be products without buying groups - # but there are also such products in the real database - buying_groups.extend( - Product.buying_groups.through(product=product, group=group) - for group in random.sample(groups, k=random.randint(0, 3)) - ) + for i in range(random.randint(0, 3)): + product_price = Price( + amount=price, product=product, is_always_shown=self.faker.boolean() + ) + # prices for non-subscribers will be higher than for subscribers + price *= 1.2 + prices.append(product_price) + buying_groups.append( + Price.groups.through(price=product_price, group=groups[i]) + ) selling_places.extend( Counter.products.through(counter=counter, product=product) for counter in random.sample(counters, random.randint(0, 4)) ) Product.objects.bulk_create(products) - Product.buying_groups.through.objects.bulk_create(buying_groups) + Price.objects.bulk_create(prices) + Price.groups.through.objects.bulk_create(buying_groups) Counter.products.through.objects.bulk_create(selling_places) def create_sales(self, sellers: list[User]): @@ -320,7 +325,7 @@ class Command(BaseCommand): ) ) ) - products = list(Product.objects.all()) + prices = list(Price.objects.select_related("product").all()) counters = list( Counter.objects.filter(name__in=["Foyer", "MDE", "La Gommette"]) ) @@ -330,14 +335,14 @@ class Command(BaseCommand): # the longer the customer has existed, the higher the mean of nb_products mu = 5 + (now().year - customer.since.year) * 2 nb_sales = max(0, int(random.normalvariate(mu=mu, sigma=mu * 5))) - favoured_products = random.sample(products, k=(random.randint(1, 5))) + favoured_prices = random.sample(prices, k=(random.randint(1, 5))) favoured_counter = random.choice(counters) this_customer_sales = [] for _ in range(nb_sales): - product = ( - random.choice(favoured_products) + price = ( + random.choice(favoured_prices) if random.random() > 0.7 - else random.choice(products) + else random.choice(prices) ) counter = ( favoured_counter @@ -346,11 +351,11 @@ class Command(BaseCommand): ) this_customer_sales.append( Selling( - product=product, + product=price.product, counter=counter, - club_id=product.club_id, + club_id=price.product.club_id, quantity=random.randint(1, 5), - unit_price=product.selling_price, + unit_price=price.amount, seller=random.choice(sellers), customer=customer, date=make_aware(