diff --git a/myproject/customers/services/wallet_service.py b/myproject/customers/services/wallet_service.py index 21b4d56..8b94c04 100644 --- a/myproject/customers/services/wallet_service.py +++ b/myproject/customers/services/wallet_service.py @@ -139,6 +139,46 @@ class WalletService: return usable_amount + @staticmethod + @transaction.atomic + def refund_wallet_payment(order, amount, user): + """ + Возврат средств в кошелёк при удалении платежа. + Увеличивает баланс кошелька и создаёт транзакцию deposit. + + Args: + order: Заказ, по которому был платёж + amount: Сумма возврата + user: Пользователь, инициировавший возврат + + Returns: + Decimal: Возвращённая сумма + """ + from customers.models import Customer, WalletTransaction + + amount = _quantize(amount) + if amount <= 0: + return None + + # Блокируем запись клиента + customer = Customer.objects.select_for_update().get(pk=order.customer_id) + + # Увеличиваем баланс + customer.wallet_balance = _quantize(customer.wallet_balance + amount) + customer.save(update_fields=['wallet_balance']) + + # Создаём транзакцию возврата + WalletTransaction.objects.create( + customer=customer, + amount=amount, + transaction_type='deposit', + order=order, + description=f'Возврат платежа по заказу #{order.order_number}', + created_by=user + ) + + return amount + @staticmethod @transaction.atomic def adjust_balance(customer_id, amount, description, user): diff --git a/myproject/orders/templates/orders/order_detail.html b/myproject/orders/templates/orders/order_detail.html index 051f030..460d4da 100644 --- a/myproject/orders/templates/orders/order_detail.html +++ b/myproject/orders/templates/orders/order_detail.html @@ -45,18 +45,12 @@