Упрощение системы получателей доставки
- Удалено избыточное поле customer_is_recipient из модели Order - Добавлено свойство @property is_customer_recipient для обратной совместимости - Заменены радиокнопки recipient_mode на чекбокс 'Другой получатель' в форме - Добавлено поле recipient_source для выбора между историей и новым получателем - Обновлен AddressService.process_recipient_from_form() для работы с чекбоксом - Обновлены шаблоны: order_form.html (чекбокс вместо радиокнопок) и order_detail.html - Удалено customer_is_recipient из admin и demo команды - Создана миграция для удаления поля customer_is_recipient Логика упрощена: recipient is None = получатель = покупатель, иначе - отдельный получатель
This commit is contained in:
@@ -11,16 +11,24 @@ class OrderForm(forms.ModelForm):
|
||||
"""Форма для создания и редактирования заказа"""
|
||||
|
||||
# Поля для работы с получателем
|
||||
recipient_mode = forms.ChoiceField(
|
||||
other_recipient = forms.BooleanField(
|
||||
required=False,
|
||||
initial=False,
|
||||
widget=forms.CheckboxInput(attrs={'class': 'form-check-input'}),
|
||||
label='Другой получатель',
|
||||
help_text='Если отмечено, получатель отличается от покупателя'
|
||||
)
|
||||
|
||||
# Режим выбора другого получателя (история или новый)
|
||||
recipient_source = forms.ChoiceField(
|
||||
choices=[
|
||||
('customer', 'Покупатель является получателем'),
|
||||
('history', 'Выбрать из истории'),
|
||||
('new', 'Другой получатель'),
|
||||
('new', 'Новый получатель'),
|
||||
],
|
||||
initial='customer',
|
||||
initial='new',
|
||||
widget=forms.RadioSelect(attrs={'class': 'form-check-input'}),
|
||||
required=False,
|
||||
label='Получатель'
|
||||
label='Способ указания получателя'
|
||||
)
|
||||
|
||||
# Выбор получателя из истории
|
||||
@@ -170,7 +178,6 @@ class OrderForm(forms.ModelForm):
|
||||
model = Order
|
||||
fields = [
|
||||
'customer',
|
||||
'customer_is_recipient',
|
||||
'recipient',
|
||||
'status',
|
||||
'is_anonymous',
|
||||
@@ -232,28 +239,47 @@ class OrderForm(forms.ModelForm):
|
||||
'data-placeholder': 'Начните вводить имя, телефон или email'
|
||||
})
|
||||
|
||||
# Подсказки
|
||||
self.fields['customer_is_recipient'].label = 'Покупатель = получатель'
|
||||
|
||||
# Поле получателя опционально
|
||||
self.fields['recipient'].required = False
|
||||
|
||||
# Инициализируем чекбокс other_recipient на основе наличия recipient
|
||||
if self.instance.pk:
|
||||
# При редактировании заказа: если есть recipient, чекбокс включен
|
||||
if self.instance.recipient:
|
||||
self.fields['other_recipient'].initial = True
|
||||
# Определяем источник получателя
|
||||
# Проверяем, есть ли этот recipient в истории клиента
|
||||
if self.instance.customer:
|
||||
customer_orders = Order.objects.filter(
|
||||
customer=self.instance.customer,
|
||||
recipient__isnull=False
|
||||
).exclude(pk=self.instance.pk).order_by('-created_at')
|
||||
history_recipients = Recipient.objects.filter(
|
||||
orders__in=customer_orders
|
||||
).distinct()
|
||||
if self.instance.recipient in history_recipients:
|
||||
self.fields['recipient_source'].initial = 'history'
|
||||
self.fields['recipient_from_history'].initial = self.instance.recipient.pk
|
||||
else:
|
||||
self.fields['recipient_source'].initial = 'new'
|
||||
self.fields['recipient_name'].initial = self.instance.recipient.name or ''
|
||||
self.fields['recipient_phone'].initial = self.instance.recipient.phone or ''
|
||||
else:
|
||||
self.fields['other_recipient'].initial = False
|
||||
|
||||
# Инициализируем queryset для recipient_from_history
|
||||
if self.instance.pk and self.instance.customer:
|
||||
# При редактировании заказа загружаем историю получателей этого клиента
|
||||
customer_orders = Order.objects.filter(
|
||||
customer=self.instance.customer,
|
||||
recipient__isnull=False
|
||||
).order_by('-created_at')
|
||||
).exclude(pk=self.instance.pk).order_by('-created_at')
|
||||
self.fields['recipient_from_history'].queryset = Recipient.objects.filter(
|
||||
orders__in=customer_orders
|
||||
).distinct().order_by('-created_at')
|
||||
|
||||
# Инициализируем поля получателя из существующего recipient
|
||||
if self.instance.pk and self.instance.recipient:
|
||||
recipient = self.instance.recipient
|
||||
self.fields['recipient_name'].initial = recipient.name or ''
|
||||
self.fields['recipient_phone'].initial = recipient.phone or ''
|
||||
elif not self.instance.pk:
|
||||
# При создании нового заказа queryset пустой (будет заполнен через JS при выборе клиента)
|
||||
self.fields['recipient_from_history'].queryset = Recipient.objects.none()
|
||||
|
||||
# Инициализируем queryset для pickup_warehouse
|
||||
from inventory.models import Warehouse
|
||||
|
||||
Reference in New Issue
Block a user