@@ -59,6 +59,15 @@ def upcoming(self):
59
59
today = timezone .now ().date ()
60
60
return self .filter (day_of_sale__gte = today ).order_by ('day_of_sale' )
61
61
62
+ def available_to_user (self , user ):
63
+ return self .filter (
64
+ Q (production_day_products__group__isnull = True ) |
65
+ Q (production_day_products__group__in = [group .pk for group in user .groups .all ()])
66
+ )
67
+
68
+ def available (self ):
69
+ return self .filter (production_day_products__group__isnull = True )
70
+
62
71
63
72
class ProductionDay (CommonBaseClass ):
64
73
day_of_sale = models .DateField (unique = True , verbose_name = _ ("Day of Sale" ), db_index = True )
@@ -223,6 +232,15 @@ def update_order_positions_product(self, production_plan_product):
223
232
positions .update (product = production_plan_product )
224
233
225
234
class ProductionDayProductQuerySet (models .QuerySet ):
235
+ def available_to_user (self , user ):
236
+ return self .filter (
237
+ Q (group__isnull = True ) |
238
+ Q (group__in = [group .pk for group in user .groups .all ()])
239
+ )
240
+
241
+ def available (self ):
242
+ return self .filter (group__isnull = True )
243
+
226
244
def published (self ):
227
245
return self .filter (is_published = True )
228
246
@@ -245,6 +263,7 @@ class ProductionDayProduct(CommonBaseClass):
245
263
max_quantity = models .PositiveSmallIntegerField (blank = False , null = False , verbose_name = _ ("Max quantity" ))
246
264
production_plan = models .ForeignKey ('workshop.ProductionPlan' , on_delete = models .SET_NULL , blank = True , null = True )
247
265
is_published = models .BooleanField (default = False , verbose_name = _ ("Published?" ))
266
+ group = models .ForeignKey ('auth.Group' , blank = True , null = True , verbose_name = _ ('Group' ), on_delete = models .SET_NULL )
248
267
249
268
objects = ProductionDayProductQuerySet .as_manager ()
250
269
@@ -318,6 +337,13 @@ def calculate_max_quantity(self, exclude_customer=None):
318
337
ordered_quantity = orders .aggregate (quantity_sum = Sum ('quantity' ))['quantity_sum' ] or 0
319
338
return max (self .max_quantity - ordered_quantity , 0 )
320
339
340
+ def is_available_to_customer (self , customer ):
341
+ if not self .group :
342
+ return True
343
+ if self .group and self .group in customer .user .groups .all ():
344
+ return True
345
+ return False
346
+
321
347
322
348
class PointOfSale (CommonBaseClass ):
323
349
name = models .CharField (max_length = 255 )
@@ -427,7 +453,6 @@ def has_abo(self):
427
453
428
454
@classmethod
429
455
def create_or_update_customer_order (cls , request , production_day , customer , products , point_of_sale = None ):
430
- # TODO order_nr, address, should point of sale really be saved in order?
431
456
# TODO check quantity with availalbe quantity
432
457
with transaction .atomic ():
433
458
point_of_sale = point_of_sale and PointOfSale .objects .get (pk = point_of_sale ) or customer .point_of_sale
@@ -441,6 +466,9 @@ def create_or_update_customer_order(cls, request, production_day, customer, prod
441
466
for product , quantity in products .items ():
442
467
# print(product, quantity)
443
468
production_day_product = ProductionDayProduct .objects .get (production_day = production_day , product = product )
469
+ if not production_day_product .is_available_to_customer (customer ):
470
+ messages .add_message (request , messages .INFO , "Leider ist das Produkt {} nicht verfügbar." .format (product .get_display_name ()))
471
+ continue
444
472
max_quantity = production_day_product .calculate_max_quantity (customer )
445
473
if quantity > 0 and production_day_product .is_sold_out and max_quantity <= quantity :
446
474
messages .add_message (request , messages .INFO , "Leider ist das Produkt {} schon ausverkauft und konnte nicht mehr bestellt werden." .format (product .get_display_name ()))
@@ -478,7 +506,7 @@ def create_or_update_customer_order(cls, request, production_day, customer, prod
478
506
return customer_order , created
479
507
480
508
def get_production_day_products_ordered_list (self ):
481
- production_day_products = self .production_day .production_day_products .published ()
509
+ production_day_products = self .production_day .production_day_products .published (). available_to_user ( self . customer . user )
482
510
production_day_products = production_day_products .annotate (
483
511
ordered_quantity = Subquery (self .positions .filter (Q (product = OuterRef ('product__pk' )) | Q (product__product_template = OuterRef ('product__pk' )),).values ("quantity" ))
484
512
).annotate (
0 commit comments