diff --git a/myproject/customers/admin.py b/myproject/customers/admin.py index e15fbc5..027a29d 100644 --- a/myproject/customers/admin.py +++ b/myproject/customers/admin.py @@ -30,7 +30,6 @@ class CustomerAdmin(admin.ModelAdmin): 'email', 'phone', 'wallet_balance_display', - 'total_spent', 'is_system_customer', 'created_at' ) @@ -45,7 +44,7 @@ class CustomerAdmin(admin.ModelAdmin): ) date_hierarchy = 'created_at' ordering = ('-created_at',) - readonly_fields = ('created_at', 'updated_at', 'total_spent', 'is_system_customer', 'wallet_balance') + readonly_fields = ('created_at', 'updated_at', 'is_system_customer', 'wallet_balance') fieldsets = ( ('Основная информация', { @@ -54,10 +53,6 @@ class CustomerAdmin(admin.ModelAdmin): ('Кошелёк', { 'fields': ('wallet_balance',), }), - ('Статистика покупок', { - 'fields': ('total_spent',), - 'classes': ('collapse',) - }), ('Заметки', { 'fields': ('notes',) }), @@ -82,7 +77,7 @@ class CustomerAdmin(admin.ModelAdmin): """Делаем все поля read-only для системного клиента""" if obj and obj.is_system_customer: # Для системного клиента все поля только для чтения - return ['name', 'email', 'phone', 'total_spent', 'is_system_customer', 'wallet_balance', 'notes', 'created_at', 'updated_at'] + return ['name', 'email', 'phone', 'is_system_customer', 'wallet_balance', 'notes', 'created_at', 'updated_at'] return self.readonly_fields def has_delete_permission(self, request, obj=None): diff --git a/myproject/customers/migrations/0005_remove_total_spent.py b/myproject/customers/migrations/0005_remove_total_spent.py new file mode 100644 index 0000000..6645250 --- /dev/null +++ b/myproject/customers/migrations/0005_remove_total_spent.py @@ -0,0 +1,17 @@ +# Generated by Django 5.0.10 on 2025-12-05 21:17 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('customers', '0004_customer_wallet_balance_wallettransaction'), + ] + + operations = [ + migrations.RemoveField( + model_name='customer', + name='total_spent', + ), + ] diff --git a/myproject/customers/models.py b/myproject/customers/models.py index 65955a4..2b565ce 100644 --- a/myproject/customers/models.py +++ b/myproject/customers/models.py @@ -24,13 +24,6 @@ class Customer(models.Model): # Temporary field to store raw phone number during initialization _raw_phone = None - - total_spent = models.DecimalField( - max_digits=10, - decimal_places=2, - default=0, - verbose_name="Общая сумма покупок" - ) # Wallet balance for overpayments wallet_balance = models.DecimalField( @@ -259,6 +252,56 @@ class Customer(models.Model): """ return self.wallet_transactions.all() + def get_successful_orders_total(self, start_date=None, end_date=None): + """ + Получить сумму успешных заказов за указанный период. + + Args: + start_date: Дата начала периода (DateField или None) + end_date: Дата окончания периода (DateField или None) + + Returns: + Decimal: Сумма успешных заказов + """ + from django.db.models import Sum, Value, DecimalField + from django.db.models.functions import Coalesce + from decimal import Decimal + + # Базовый queryset: только успешные заказы + queryset = self.orders.filter(status__is_positive_end=True) + + # Фильтрация по датам (используем delivery_date) + if start_date: + queryset = queryset.filter(delivery_date__gte=start_date) + if end_date: + queryset = queryset.filter(delivery_date__lte=end_date) + + # Агрегация суммы + result = queryset.aggregate( + total=Coalesce( + Sum('total_amount'), + Value(0), + output_field=DecimalField() + ) + ) + + return result['total'] or Decimal('0') + + def get_last_year_orders_total(self): + """ + Получить сумму успешных заказов за последний календарный год. + (С этой даты прошлого года по текущую дату) + + Returns: + Decimal: Сумма успешных заказов за год + """ + from datetime import date, timedelta + + today = date.today() + year_ago = today - timedelta(days=365) + + return self.get_successful_orders_total(start_date=year_ago, end_date=today) + class WalletTransaction(models.Model): """ diff --git a/myproject/customers/templates/customers/customer_detail.html b/myproject/customers/templates/customers/customer_detail.html index c80b92f..f8c4207 100644 --- a/myproject/customers/templates/customers/customer_detail.html +++ b/myproject/customers/templates/customers/customer_detail.html @@ -39,8 +39,12 @@