Skip to content

Commit 6d43d4c

Browse files
authored
Merge pull request #550 from bruecksen/549-imp-select-point-of-sales-per-production-day
Add point of sale to production day
2 parents 0cc5c9a + 9c32384 commit 6d43d4c

11 files changed

Lines changed: 370 additions & 219 deletions

File tree

bakeup/pages/models.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
CustomerOrder,
1616
CustomerOrderPosition,
1717
CustomerOrderTemplatePosition,
18-
PointOfSale,
1918
ProductionDay,
2019
ProductionDayProduct,
2120
)
@@ -171,10 +170,10 @@ def get_context(self, request, *args, **kwargs):
171170
)
172171
)
173172
context["production_day_products"] = production_day_products
173+
context["point_of_sales"] = self.production_day.point_of_sales.all()
174174
context["show_remaining_products"] = (
175175
request.tenant.clientsetting.show_remaining_products
176176
)
177-
context["point_of_sales"] = PointOfSale.objects.all()
178177
context["all_production_days"] = list(
179178
ProductionDay.objects.published()
180179
.available_to_user(request.user)

bakeup/shop/forms.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from bakeup.shop.models import (
1212
Customer,
1313
CustomerOrderPosition,
14+
PointOfSale,
1415
ProductionDay,
1516
ProductionDayProduct,
1617
)
@@ -120,7 +121,7 @@ def clean(self):
120121
class ProductionDayForm(forms.ModelForm):
121122
class Meta:
122123
model = ProductionDay
123-
fields = ["day_of_sale", "description"]
124+
fields = ["day_of_sale", "description", "point_of_sales"]
124125
widgets = {
125126
"day_of_sale": forms.DateInput(
126127
format="%Y-%m-%d",
@@ -131,8 +132,18 @@ class Meta:
131132
},
132133
),
133134
"description": forms.Textarea(attrs={"rows": 3}),
135+
"point_of_sales": forms.CheckboxSelectMultiple(
136+
attrs={"class": "form-control"}
137+
),
134138
}
135139

140+
def __init__(self, *args, **kwargs):
141+
super().__init__(*args, **kwargs)
142+
if not PointOfSale.objects.exists():
143+
del self.fields["point_of_sales"]
144+
elif not self.instance.pk:
145+
self.fields["point_of_sales"].initial = PointOfSale.objects.all()
146+
136147

137148
class ProductionDayProductForm(forms.ModelForm):
138149
class Meta:
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Generated by Django 4.2.23 on 2025-10-28 10:40
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("shop", "0004_alter_pointofsale_is_primary_alter_pointofsale_name_and_more"),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name="productionday",
15+
name="point_of_sales",
16+
field=models.ManyToManyField(
17+
blank=True,
18+
help_text="Select the point of sales where this production day is available.",
19+
related_name="production_days",
20+
to="shop.pointofsale",
21+
verbose_name="Point of Sales",
22+
),
23+
),
24+
]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Generated by Django 4.2.23 on 2025-10-29 19:09
2+
3+
from django.db import migrations
4+
5+
6+
def create_pos(apps, schema_editor):
7+
ProductionDay = apps.get_model("shop", "ProductionDay")
8+
PointOfSale = apps.get_model("shop", "PointOfSale")
9+
point_of_sales = PointOfSale.objects.all()
10+
for production_day in ProductionDay.objects.all():
11+
if not production_day.point_of_sales.exists():
12+
production_day.point_of_sales.set(point_of_sales)
13+
14+
def remove_pos(apps, schema_editor):
15+
ProductionDay = apps.get_model("shop", "ProductionDay")
16+
for production_day in ProductionDay.objects.all():
17+
production_day.point_of_sales.clear()
18+
19+
20+
class Migration(migrations.Migration):
21+
22+
dependencies = [
23+
("shop", "0005_productionday_point_of_sales"),
24+
]
25+
26+
operations = [
27+
migrations.RunPython(create_pos, remove_pos),
28+
]

bakeup/shop/models.py

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ class ProductionDay(CommonBaseClass):
8383
unique=True, verbose_name=_("Day of Sale"), db_index=True
8484
)
8585
description = models.TextField(blank=True, null=True, verbose_name=_("Description"))
86+
point_of_sales = models.ManyToManyField(
87+
"shop.PointOfSale",
88+
blank=False,
89+
related_name="production_days",
90+
verbose_name=_("Point of Sales"),
91+
help_text=_(
92+
"Select the point of sales where this production day is available. At least"
93+
" one must be selected."
94+
),
95+
)
8696

8797
objects = ProductionDayQuerySet.as_manager()
8898

@@ -1215,41 +1225,50 @@ def create_abo_orders_for_production_days(
12151225
production_day.production_day_products.filter(product=product)
12161226
)
12171227
if not customer_order_position.exists() and production_day_product:
1218-
production_day_product = production_day_product.get()
1219-
max_quantity = production_day_product.calculate_max_quantity(
1220-
customer
1221-
)
1222-
quantity = min(
1223-
customer_order_template_position.quantity, max_quantity
1224-
)
1225-
if not production_day_product.is_locked and quantity > 0:
1226-
print(
1227-
"Create Abo order: ", production_day, product, quantity
1228+
if (
1229+
not production_day.point_of_sales.exists()
1230+
or customer.point_of_sale
1231+
in production_day.point_of_sales.all()
1232+
):
1233+
# check if pos is available for this production day
1234+
production_day_product = production_day_product.get()
1235+
max_quantity = (
1236+
production_day_product.calculate_max_quantity(customer)
12281237
)
1229-
customer_order, created = (
1230-
CustomerOrder.objects.get_or_create(
1231-
production_day=production_day,
1232-
customer=customer,
1233-
defaults={
1234-
"point_of_sale": customer.point_of_sale,
1235-
},
1236-
)
1237-
)
1238-
price = None
1239-
price_total = None
1240-
if product.sale_price:
1241-
price = product.sale_price.price.amount
1242-
price_total = price * quantity
1243-
position = CustomerOrderPosition.objects.create(
1244-
order=customer_order,
1245-
product=product,
1246-
quantity=quantity,
1247-
price=price,
1248-
price_total=price_total,
1238+
quantity = min(
1239+
customer_order_template_position.quantity, max_quantity
12491240
)
1250-
customer_order_template_position.orders.add(position)
1251-
customer_order_template_position.order_template.set_locked()
1252-
is_order_created = True
1241+
if not production_day_product.is_locked and quantity > 0:
1242+
print(
1243+
"Create Abo order: ",
1244+
production_day,
1245+
product,
1246+
quantity,
1247+
)
1248+
customer_order, created = (
1249+
CustomerOrder.objects.get_or_create(
1250+
production_day=production_day,
1251+
customer=customer,
1252+
defaults={
1253+
"point_of_sale": customer.point_of_sale,
1254+
},
1255+
)
1256+
)
1257+
price = None
1258+
price_total = None
1259+
if product.sale_price:
1260+
price = product.sale_price.price.amount
1261+
price_total = price * quantity
1262+
position = CustomerOrderPosition.objects.create(
1263+
order=customer_order,
1264+
product=product,
1265+
quantity=quantity,
1266+
price=price,
1267+
price_total=price_total,
1268+
)
1269+
customer_order_template_position.orders.add(position)
1270+
customer_order_template_position.order_template.set_locked()
1271+
is_order_created = True
12531272
from bakeup.pages.models import EmailSettings
12541273

12551274
if (

bakeup/shop/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,10 +377,10 @@ def get_context_data(self, **kwargs):
377377
)
378378
)
379379
context["production_day_products"] = production_day_products
380+
context["point_of_sales"] = self.production_day.point_of_sales.all()
380381
context["show_remaining_products"] = (
381382
self.request.tenant.clientsetting.show_remaining_products
382383
)
383-
context["point_of_sales"] = PointOfSale.objects.all()
384384
context["production_days"] = ProductionDay.objects.upcoming().exclude(
385385
id=self.production_day.pk
386386
)

bakeup/templates/workshop/productionday_detail.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,14 @@
4848
</ul>
4949
<div class="tab-content">
5050
<div class="tab-pane fade show active" id="summary" role="tabpanel" aria-labelledby="summary-tab">
51-
<p class="mt-4"><strong>{% trans "Description" %}: </strong> {{ object.description }}</p>
51+
<p class="mt-4 mb-0"><strong>{% trans "Description" %}: </strong> {{ object.description }}</p>
52+
{% if object.point_of_sales.exists %}
53+
<p class="mb-0"><strong>{% trans "Point of sales" %}: </strong>
54+
{% for pos in object.point_of_sales.all %}
55+
{{ pos }}{% if not forloop.last %}, {% endif %}
56+
{% endfor %}
57+
</p>
58+
{% endif %}
5259

5360
<h2 class="mt-4">{% trans "Summary" %}
5461

bakeup/templates/workshop/productionday_form.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
{{ formset.management_form }}
1818
<div class="card p-3 mb-3">
1919
{% bootstrap_field form.day_of_sale %}
20+
{% if form.point_of_sales %}
21+
{% bootstrap_field form.point_of_sales %}
22+
{% endif %}
2023
{% bootstrap_field form.description %}
2124
</div>
2225
</div>

locale/de_DE/LC_MESSAGES/django.mo

424 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)