feat: Add customer prefill from URL parameter in order creation

- Modified order_create view to read customer from GET parameter
- Pass preselected_customer to template context
- Template renders select with preselected option for Select2
- Fixed draft creation timing with callback after Select2 initialization
- Auto-create draft when customer is preselected from URL
- Graceful handling if customer not found or invalid ID
This commit is contained in:
2025-11-27 00:17:02 +03:00
parent 5ead7fdd2e
commit c62cdb0298
6 changed files with 463 additions and 19 deletions

View File

@@ -5,6 +5,7 @@ from .models import Order, OrderItem, Payment, Address, OrderStatus
from customers.models import Customer
from inventory.models import Warehouse
from products.models import Product, ProductKit
from decimal import Decimal
class OrderForm(forms.ModelForm):
@@ -481,6 +482,44 @@ class PaymentForm(forms.ModelForm):
# Делаем notes опциональным
self.fields['notes'].required = False
def clean(self):
"""Валидация платежа, особенно для оплаты из кошелька"""
cleaned = super().clean()
method = cleaned.get('payment_method')
amount = cleaned.get('amount')
order = getattr(self.instance, 'order', None)
# Пустые формы допустимы при удалении
if not method and not amount:
return cleaned
# Базовые проверки
if amount is None or amount <= 0:
self.add_error('amount', 'Введите сумму больше 0.')
if not order:
raise forms.ValidationError('Заказ не найден для платежа.')
# Проверка для оплаты из кошелька
if method and getattr(method, 'code', None) == 'account_balance':
wallet_balance = order.customer.wallet_balance if order.customer else Decimal('0')
amount_due = max(order.total_amount - order.amount_paid, Decimal('0'))
if wallet_balance <= 0:
self.add_error('payment_method', 'Недостаточно средств в кошельке клиента (баланс 0).')
if amount and amount > wallet_balance:
self.add_error('amount', f'Недостаточно средств в кошельке. Доступно {wallet_balance} руб.')
if amount and amount > amount_due:
self.add_error('amount', f'Сумма превышает остаток к оплате ({amount_due} руб.)')
if self.errors:
# Общее сообщение для блока формы
raise forms.ValidationError('Проверьте поля оплаты из кошелька.')
return cleaned
# Formset для множественных платежей
PaymentFormSet = inlineformset_factory(