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:
2026-01-24 03:21:56 +03:00
parent 9c91a99189
commit 38fbf36731
5 changed files with 248 additions and 2 deletions

View File

@@ -1923,6 +1923,7 @@ async function openEditKitModal(kitId) {
// По<D09F><D0BE>азываем кнопку "Разобрать" и блок добавления товаров
document.getElementById('disassembleKitBtn').style.display = 'block';
document.getElementById('writeOffKitBtn').style.display = 'block';
document.getElementById('showcaseKitQuantityBlock').style.display = 'none';
document.getElementById('addProductBlock').style.display = 'block';
@@ -2554,6 +2555,53 @@ document.getElementById('disassembleKitBtn').addEventListener('click', async ()
}
});
// Обработчик кнопки "Списать букет"
document.getElementById('writeOffKitBtn').addEventListener('click', async () => {
if (!isEditMode || !editingKitId) {
alert('Ошибка: режим редактирования не активен');
return;
}
// Запрос подтверждения
const confirmed = confirm(
'Вы уверены?\n\n' +
'Букет будет списан:\n' +
'• Будет создан документ списания с компонентами букета\n' +
'• Комплект будет помечен как "Снят"\n' +
'• Будет открыта страница документа для редактирования\n\n' +
'Продолжить?'
);
if (!confirmed) {
return;
}
try {
const response = await fetch(`/pos/api/product-kits/${editingKitId}/write-off/`, {
method: 'POST',
headers: {
'X-CSRFToken': getCsrfToken()
}
});
const data = await response.json();
if (data.success) {
// Закрываем модальное окно
const modal = bootstrap.Modal.getInstance(document.getElementById('createTempKitModal'));
modal.hide();
// Перенаправляем на страницу документа
window.location.href = data.redirect_url;
} else {
alert(`❌ Ошибка: ${data.error}`);
}
} catch (error) {
console.error('Error writing off kit:', error);
alert('Произошла ошибка при списании букета');
}
});
// Вспомогательная функция для определения мобильного устройства
function isMobileDevice() {
// Проверяем по юзер-агенту и размеру экрана
@@ -2616,8 +2664,9 @@ document.getElementById('createTempKitModal').addEventListener('hidden.bs.modal'
document.getElementById('createTempKitModalLabel').textContent = 'Создать витринный букет из корзины';
document.getElementById('confirmCreateTempKit').innerHTML = '<i class="bi bi-check-circle"></i> Создать и зарезервировать';
// Скрываем кнопку "Разобрать" и блок добавления товаров
// Скрываем кнопки "Разобрать" и "Списать" и блок добавления товаров
document.getElementById('disassembleKitBtn').style.display = 'none';
document.getElementById('writeOffKitBtn').style.display = 'none';
document.getElementById('showcaseKitQuantityBlock').style.display = 'block';
document.getElementById('addProductBlock').style.display = 'none';
}

View File

@@ -330,6 +330,11 @@
<i class="bi bi-scissors"></i> Разобрать букет
</button>
<!-- Кнопка "Списать" (отображается только в режиме редактирования) -->
<button type="button" class="btn btn-warning me-auto" id="writeOffKitBtn" style="display: none;">
<i class="bi bi-file-earmark-x"></i> Списать букет
</button>
<!-- Правая группа кнопок -->
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Отмена</button>
<button type="button" class="btn btn-primary" id="confirmCreateTempKit">

View File

@@ -33,6 +33,8 @@ urlpatterns = [
path('api/product-kits/<int:kit_id>/update/', views.update_product_kit, name='update-product-kit'),
# Разобрать витринный комплект (освободить резервы, установить статус discontinued) [POST]
path('api/product-kits/<int:kit_id>/disassemble/', views.disassemble_product_kit, name='disassemble-product-kit'),
# Списать витринный комплект (создать документ списания с компонентами) [POST]
path('api/product-kits/<int:kit_id>/write-off/', views.write_off_showcase_kit, name='write-off-showcase-kit'),
# Создать временный комплект и зарезервировать на витрину [POST]
path('api/create-temp-kit/', views.create_temp_kit_to_showcase, name='create-temp-kit-api'),
# Создать заказ и провести оплату в POS [POST]

View File

@@ -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):