2024-07-15 00:19:37 +02:00
|
|
|
"""Admin for the accounting app."""
|
|
|
|
|
2024-08-03 17:55:32 +00:00
|
|
|
from django import forms
|
2018-06-23 21:08:56 +02:00
|
|
|
from django.contrib import admin
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
2024-08-03 17:55:32 +00:00
|
|
|
from . import models
|
2018-06-23 21:08:56 +02:00
|
|
|
|
|
|
|
|
2024-08-03 17:55:32 +00:00
|
|
|
class OrderProductInline(admin.TabularInline):
|
|
|
|
"""Administer contents of an order inline."""
|
|
|
|
|
|
|
|
model = models.OrderProduct
|
|
|
|
|
|
|
|
|
|
|
|
class OrderAdminForm(forms.ModelForm):
|
|
|
|
"""Special Form for the OrderAdmin so we don't need to require the account field."""
|
|
|
|
|
|
|
|
account = forms.ModelChoiceField(
|
|
|
|
required=False,
|
|
|
|
queryset=models.Account.objects.all(),
|
|
|
|
help_text=_("Leave empty to auto-choose the member's own account or to create one."),
|
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = models.Order
|
|
|
|
exclude = () # noqa: DJ006
|
|
|
|
|
|
|
|
def clean(self): # noqa: D102, ANN201
|
|
|
|
cd = super().clean()
|
|
|
|
if not cd["account"] and cd["member"]:
|
|
|
|
try:
|
|
|
|
cd["account"] = models.Account.objects.get_or_create(owner=cd["member"])[0]
|
|
|
|
except models.Account.MultipleObjectsReturned:
|
|
|
|
cd["account"] = models.Account.objects.filter(owner=cd["member"]).first()
|
|
|
|
return cd
|
|
|
|
|
|
|
|
|
|
|
|
@admin.register(models.Order)
|
2018-06-23 21:08:56 +02:00
|
|
|
class OrderAdmin(admin.ModelAdmin):
|
2024-07-15 00:19:37 +02:00
|
|
|
"""Admin for the Order model."""
|
|
|
|
|
2024-08-03 17:55:32 +00:00
|
|
|
inlines = (OrderProductInline,)
|
|
|
|
form = OrderAdminForm
|
2018-06-23 21:08:56 +02:00
|
|
|
|
2024-08-03 17:55:32 +00:00
|
|
|
list_display = ("member", "description", "created", "is_paid")
|
2019-09-01 00:27:36 +02:00
|
|
|
|
2018-06-23 21:08:56 +02:00
|
|
|
|
2024-08-03 17:55:32 +00:00
|
|
|
@admin.register(models.Payment)
|
2018-06-23 21:08:56 +02:00
|
|
|
class PaymentAdmin(admin.ModelAdmin):
|
2024-07-15 00:19:37 +02:00
|
|
|
"""Admin for the Payment model."""
|
|
|
|
|
2024-08-03 17:55:32 +00:00
|
|
|
list_display = ("order__member", "description", "order_id", "created")
|
2019-09-01 00:27:36 +02:00
|
|
|
|
2023-01-03 21:39:40 +01:00
|
|
|
@admin.display(description=_("Order ID"))
|
2024-08-03 17:55:32 +00:00
|
|
|
def order_id(self, instance: models.Payment) -> int:
|
2024-07-15 00:19:37 +02:00
|
|
|
"""Return the ID of the order."""
|
2018-06-23 21:08:56 +02:00
|
|
|
return instance.order.id
|
2024-08-03 17:55:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
@admin.register(models.Product)
|
|
|
|
class ProductAdmin(admin.ModelAdmin): # noqa: D101
|
|
|
|
list_display = ("name", "price", "vat")
|
|
|
|
|
|
|
|
|
|
|
|
class TransactionInline(admin.TabularInline): # noqa: D101
|
|
|
|
model = models.Transaction
|
|
|
|
|
|
|
|
|
|
|
|
@admin.register(models.Account)
|
|
|
|
class AccountAdmin(admin.ModelAdmin): # noqa: D101
|
|
|
|
list_display = ("owner", "balance")
|
|
|
|
inlines = (TransactionInline,)
|