forked from erp-dev/erp
57 lines
2.1 KiB
Python
57 lines
2.1 KiB
Python
from django.contrib import admin
|
|
from .models import Shipment, SalesItem
|
|
|
|
|
|
class SalesItemInline(admin.TabularInline):
|
|
model = SalesItem
|
|
extra = 1
|
|
fields = ['name', 'quantity', 'unit', 'position', 'remark', 'printing_job_id', 'customer_id', 'created_by']
|
|
readonly_fields = ['created_by']
|
|
|
|
|
|
@admin.register(Shipment)
|
|
class ShipmentAdmin(admin.ModelAdmin):
|
|
list_display = ['id', 'customer', 'shipment_date', 'items_count', 'created_by', 'created_at']
|
|
list_filter = ['shipment_date', 'created_at']
|
|
search_fields = ['customer__name', 'remark']
|
|
readonly_fields = ['created_at', 'updated_at', 'created_by']
|
|
date_hierarchy = 'shipment_date'
|
|
inlines = [SalesItemInline]
|
|
|
|
def items_count(self, obj):
|
|
return obj.items.count()
|
|
items_count.short_description = '销售品数量'
|
|
|
|
def save_model(self, request, obj, form, change):
|
|
if not change:
|
|
obj.created_by = request.user
|
|
super().save_model(request, obj, form, change)
|
|
|
|
def save_formset(self, request, form, formset, change):
|
|
instances = formset.save(commit=False)
|
|
for instance in instances:
|
|
if isinstance(instance, SalesItem) and not instance.pk:
|
|
instance.created_by = request.user
|
|
instance.save()
|
|
formset.save_m2m()
|
|
|
|
|
|
@admin.register(SalesItem)
|
|
class SalesItemAdmin(admin.ModelAdmin):
|
|
list_display = ['id', 'name', 'quantity', 'unit', 'position', 'shipment_status', 'printing_job_id', 'customer_id', 'created_by', 'created_at']
|
|
list_filter = ['unit', 'created_at', ('shipment', admin.EmptyFieldListFilter)]
|
|
search_fields = ['name', 'shipment__customer__name', 'position', 'remark']
|
|
readonly_fields = ['created_at', 'updated_at', 'created_by']
|
|
raw_id_fields = ['shipment']
|
|
|
|
def shipment_status(self, obj):
|
|
if obj.shipment:
|
|
return f'出货单 #{obj.shipment.id}'
|
|
return '待分配'
|
|
shipment_status.short_description = '出货单'
|
|
|
|
def save_model(self, request, obj, form, change):
|
|
if not change:
|
|
obj.created_by = request.user
|
|
super().save_model(request, obj, form, change)
|