82 lines
2.5 KiB
Python
82 lines
2.5 KiB
Python
"""
|
|
Billing Business Logic Admin
|
|
"""
|
|
from django.contrib import admin
|
|
from django.utils.html import format_html
|
|
from .models import CreditCostConfig
|
|
|
|
|
|
@admin.register(CreditCostConfig)
|
|
class CreditCostConfigAdmin(admin.ModelAdmin):
|
|
list_display = [
|
|
'operation_type',
|
|
'display_name',
|
|
'credits_cost_display',
|
|
'unit',
|
|
'is_active',
|
|
'cost_change_indicator',
|
|
'updated_at',
|
|
'updated_by'
|
|
]
|
|
|
|
list_filter = ['is_active', 'unit', 'updated_at']
|
|
search_fields = ['operation_type', 'display_name', 'description']
|
|
|
|
fieldsets = (
|
|
('Operation', {
|
|
'fields': ('operation_type', 'display_name', 'description')
|
|
}),
|
|
('Cost Configuration', {
|
|
'fields': ('credits_cost', 'unit', 'is_active')
|
|
}),
|
|
('Audit Trail', {
|
|
'fields': ('previous_cost', 'updated_by', 'created_at', 'updated_at'),
|
|
'classes': ('collapse',)
|
|
}),
|
|
)
|
|
|
|
readonly_fields = ['created_at', 'updated_at', 'previous_cost']
|
|
|
|
def credits_cost_display(self, obj):
|
|
"""Show cost with color coding"""
|
|
if obj.credits_cost >= 20:
|
|
color = 'red'
|
|
elif obj.credits_cost >= 10:
|
|
color = 'orange'
|
|
else:
|
|
color = 'green'
|
|
return format_html(
|
|
'<span style="color: {}; font-weight: bold;">{} credits</span>',
|
|
color,
|
|
obj.credits_cost
|
|
)
|
|
credits_cost_display.short_description = 'Cost'
|
|
|
|
def cost_change_indicator(self, obj):
|
|
"""Show if cost changed recently"""
|
|
if obj.previous_cost is not None:
|
|
if obj.credits_cost > obj.previous_cost:
|
|
icon = '📈' # Increased
|
|
color = 'red'
|
|
elif obj.credits_cost < obj.previous_cost:
|
|
icon = '📉' # Decreased
|
|
color = 'green'
|
|
else:
|
|
icon = '➡️' # Same
|
|
color = 'gray'
|
|
|
|
return format_html(
|
|
'{} <span style="color: {};">({} → {})</span>',
|
|
icon,
|
|
color,
|
|
obj.previous_cost,
|
|
obj.credits_cost
|
|
)
|
|
return '—'
|
|
cost_change_indicator.short_description = 'Recent Change'
|
|
|
|
def save_model(self, request, obj, form, change):
|
|
"""Track who made the change"""
|
|
obj.updated_by = request.user
|
|
super().save_model(request, obj, form, change)
|