Skip to content

Commit

Permalink
Made product link in orderitem optional
Browse files Browse the repository at this point in the history
(Closes #252)
  • Loading branch information
rafaelurben committed Jan 14, 2024
1 parent c99c4f2 commit 5d42200
Show file tree
Hide file tree
Showing 5 changed files with 247 additions and 90 deletions.
107 changes: 107 additions & 0 deletions kmuhelper/migrations/0110_orderitem_made_product_optional.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Generated by Django 5.0 on 2024-01-14 16:59
from datetime import datetime

from django.db import migrations, models
from django.utils import timezone

VAT_RATE_REVERSE_MIGRATION = {2.6: 2.5, 3.8: 3.7, 8.1: 7.7}


def copy_values(apps, schema_editor):
db_alias = schema_editor.connection.alias
OrderItem = apps.get_model("kmuhelper", "OrderItem")

for item in (
OrderItem.objects.using(db_alias).all().select_related("order", "product")
):
item.article_number = item.product.article_number
item.quantity_description = item.product.quantity_description
item.name = item.product.name
item.vat_rate = item.product.vat_rate

if (
item.order.date
< timezone.make_aware(
datetime(year=2024, month=1, day=1, hour=0, minute=0, second=0)
)
and item.vat_rate in VAT_RATE_REVERSE_MIGRATION
):
item.vat_rate = VAT_RATE_REVERSE_MIGRATION[item.vat_rate]

item.save()


class Migration(migrations.Migration):
dependencies = [
("kmuhelper", "0109_contactperson_is_default_paymentreceiver_is_default"),
]

operations = [
migrations.AddField(
model_name="orderitem",
name="article_number",
field=models.CharField(
default="", max_length=25, verbose_name="Artikelnummer"
),
preserve_default=False,
),
migrations.AddField(
model_name="orderitem",
name="name",
field=models.CharField(
default="",
help_text="Unterstützt i18n: '[:de]Deutsch[:fr]Français[:it]Italiano[:en]English[:]'",
max_length=500,
verbose_name="Name",
),
),
migrations.AddField(
model_name="orderitem",
name="quantity_description",
field=models.CharField(
blank=True,
default="Stück",
help_text="Unterstützt i18n: '[:de]Deutsch[:fr]Français[:it]Italiano[:en]English[:]'",
max_length=100,
verbose_name="Mengenbezeichnung",
),
),
migrations.AddField(
model_name="orderitem",
name="vat_rate",
field=models.FloatField(
choices=[
(0.0, "0.0% (Mehrwertsteuerfrei)"),
(2.6, "2.6% (Reduzierter Satz)"),
(3.8, "3.8% (Sondersatz für Beherbergung)"),
(8.1, "8.1% (Normalsatz)"),
(2.5, "2.5% (Bis 2023: Reduzierter Satz)"),
(3.7, "3.7% (Bis 2023: Sondersatz für Beherbergung)"),
(7.7, "7.7% (Bis 2023: Normalsatz)"),
],
default=8.1,
verbose_name="MwSt-Satz",
),
),
migrations.RunPython(
# This migration cannot be reverted!
copy_values,
reverse_code=None,
elidable=True,
),
migrations.RenameField(
model_name="orderitem", old_name="product", new_name="linked_product"
),
migrations.AlterField(
model_name="orderitem",
name="linked_product",
field=models.ForeignKey(
blank=True,
default=None,
null=True,
on_delete=models.deletion.SET_NULL,
to="kmuhelper.product",
verbose_name="Verknüpftes Produkt",
),
),
]
16 changes: 9 additions & 7 deletions kmuhelper/modules/integrations/woocommerce/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,10 @@ def category_bulk_update(cls, categories, api=None):

@classmethod
def order_create(cls, order, api=None, sendstockwarning=True):
"""Create a new order from WooCommerce data"""
"""Create a new order from WooCommerce data
Notes: TAX imports and discounts might NOT be imported correctly.
"""
wcapi = api or cls.get_api()

neworder = Order.objects.create(
Expand Down Expand Up @@ -586,12 +589,11 @@ def order_create(cls, order, api=None, sendstockwarning=True):
if created:
product = cls.product_update(product, api=wcapi)

neworder.products.add(
product,
through_defaults={
"quantity": int(item["quantity"]),
"product_price": runden(float(item["price"])),
},
neworder.products.through.objects.create(
order=neworder,
linked_product=product,
quantity=int(item["quantity"]),
product_price=runden(float(item["price"])),
)
for item in order["shipping_lines"]:
neworder.fees.through.objects.create(
Expand Down
40 changes: 26 additions & 14 deletions kmuhelper/modules/main/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,39 @@ class OrderAdminOrderItemInline(CustomTabularInline):
extra = 0

fields = (
"product",
"note",
"linked_product",
"article_number",
"name",
"product_price",
"quantity",
"quantity_description",
"discount",
"note",
"display_subtotal",
"display_vat_rate",
"vat_rate",
)

readonly_fields = (
"linked_product",
"display_subtotal",
"display_vat_rate",
"product",
)

def get_additional_readonly_fields(self, request, obj=None):
fields = ["product_price"]
fields = []
if obj and (obj.is_shipped or obj.is_paid):
fields += ["quantity"]
if obj and obj.is_paid:
fields += ["discount"]
fields += ["product_price", "vat_rate", "discount"]
return fields

# Permissions

NO_ADD = True
def has_add_permission(self, request, obj=None):
return (
False
if (obj and (obj.is_paid or obj.is_shipped))
else super().has_add_permission(request, obj)
)

def has_delete_permission(self, request, obj=None):
return (
Expand All @@ -97,18 +104,23 @@ def has_delete_permission(self, request, obj=None):

def get_queryset(self, request):
qs = super().get_queryset(request)
return qs.select_related("product")
return qs.select_related("linked_product")


class OrderAdminOrderItemInlineAdd(CustomTabularInline):
class OrderAdminOrderItemInlineImport(CustomTabularInline):
model = Order.products.through
verbose_name = _("Bestellungsposten")
verbose_name_plural = _("Bestellungsposten hinzufügen")
verbose_name_plural = _("Bestellungsposten importieren")
extra = 0

autocomplete_fields = ("product",)
autocomplete_fields = ("linked_product",)

fieldsets = [(None, {"fields": ["product", "note", "quantity", "discount"]})]
fields = (
"linked_product",
"note",
"quantity",
"discount",
)

# Permissions

Expand Down Expand Up @@ -356,7 +368,7 @@ def get_additional_readonly_fields(self, request, obj=None):
def get_inlines(self, request, obj=None):
inlines = [OrderAdminOrderItemInline]
if request.user.has_perm("kmuhelper.view_product"):
inlines += [OrderAdminOrderItemInlineAdd]
inlines += [OrderAdminOrderItemInlineImport]
inlines += [OrderAdminOrderFeeInline]
if request.user.has_perm("kmuhelper.view_fee"):
inlines += [OrderAdminOrderFeeInlineImport]
Expand Down
Loading

0 comments on commit 5d42200

Please sign in to comment.