feat(pos): add write-off functionality for showcase kits
Add support for writing off showcase kits by creating a write-off document with components, converting reservations, and updating statuses. - Add `write_off_from_showcase` static method to ShowcaseManager - Add API endpoint `/pos/api/product-kits/<int:kit_id>/write-off/` - Add write-off button to POS terminal UI - Implement confirmation dialog with detailed information - Add redirect to write-off document detail page after success The write-off process includes: 1. Creating a write-off document in draft state 2. Converting existing reservations to write-off document items 3. Marking the showcase item as dismantled 4. Setting the product kit status to discontinued (if not already) Breaking Changes: No
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from django.shortcuts import render, get_object_or_404
|
||||
from django.urls import reverse
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import JsonResponse
|
||||
from django.views.decorators.http import require_http_methods
|
||||
@@ -13,7 +14,7 @@ import json
|
||||
import logging
|
||||
|
||||
from products.models import Product, ProductCategory, ProductKit, KitItem
|
||||
from inventory.models import Showcase, Reservation, Warehouse, Stock
|
||||
from inventory.models import Showcase, Reservation, Warehouse, Stock, ShowcaseItem
|
||||
from inventory.services import ShowcaseManager
|
||||
from inventory.signals import skip_sale_creation, reset_sale_creation
|
||||
|
||||
@@ -1536,6 +1537,88 @@ def disassemble_product_kit(request, kit_id):
|
||||
}, status=500)
|
||||
|
||||
|
||||
@login_required
|
||||
@require_http_methods(["POST"])
|
||||
def write_off_showcase_kit(request, kit_id):
|
||||
"""
|
||||
Списывает витринный комплект с созданием документа списания.
|
||||
|
||||
Args:
|
||||
request: HTTP запрос
|
||||
kit_id: ID комплекта для списания
|
||||
|
||||
Returns:
|
||||
JSON: {
|
||||
'success': bool,
|
||||
'document_id': int,
|
||||
'document_number': str,
|
||||
'redirect_url': str,
|
||||
'message': str,
|
||||
'error': str (если failed)
|
||||
}
|
||||
"""
|
||||
try:
|
||||
# Получаем комплект с витриной (только временные комплекты)
|
||||
kit = ProductKit.objects.select_related('showcase').get(id=kit_id, is_temporary=True)
|
||||
|
||||
# Проверяем, что комплект ещё не разобран
|
||||
if kit.status == 'discontinued':
|
||||
return JsonResponse({
|
||||
'success': False,
|
||||
'error': 'Комплект уже разобран (статус: Снят)'
|
||||
}, status=400)
|
||||
|
||||
# Проверяем, что у комплекта есть привязанная витрина
|
||||
if not kit.showcase:
|
||||
return JsonResponse({
|
||||
'success': False,
|
||||
'error': 'Комплект не привязан к витрине'
|
||||
}, status=400)
|
||||
|
||||
# Находим экземпляр на витрине
|
||||
showcase_item = ShowcaseItem.objects.filter(
|
||||
showcase=kit.showcase,
|
||||
product_kit=kit,
|
||||
status='available'
|
||||
).first()
|
||||
|
||||
if not showcase_item:
|
||||
return JsonResponse({
|
||||
'success': False,
|
||||
'error': 'Экземпляр комплекта не найден на витрине'
|
||||
}, status=404)
|
||||
|
||||
# Создаём документ списания
|
||||
result = ShowcaseManager.write_off_from_showcase(
|
||||
showcase_item=showcase_item,
|
||||
reason='spoilage',
|
||||
notes=f'Витринный букет: {kit.name}',
|
||||
created_by=request.user
|
||||
)
|
||||
|
||||
if not result['success']:
|
||||
return JsonResponse({
|
||||
'success': False,
|
||||
'error': result['message']
|
||||
}, status=400)
|
||||
|
||||
# Формируем URL для перенаправления
|
||||
redirect_url = reverse('inventory:writeoff-document-detail', kwargs={'pk': result['document_id']})
|
||||
|
||||
return JsonResponse({
|
||||
'success': True,
|
||||
'document_id': result['document_id'],
|
||||
'document_number': result['document_number'],
|
||||
'redirect_url': redirect_url,
|
||||
'message': result['message']
|
||||
})
|
||||
|
||||
except ProductKit.DoesNotExist:
|
||||
return JsonResponse({'success': False, 'error': 'Комплект не найден'}, status=404)
|
||||
except Exception as e:
|
||||
return JsonResponse({'success': False, 'error': f'Ошибка: {str(e)}'}, status=500)
|
||||
|
||||
|
||||
@login_required
|
||||
@require_http_methods(["POST"])
|
||||
def pos_checkout(request):
|
||||
|
||||
Reference in New Issue
Block a user