Добавлена автозагрузка товаров в Select2 при создании заказа из POS
This commit is contained in:
@@ -2150,9 +2150,65 @@ if (!document.getElementById('notification-styles')) {
|
||||
const items = document.querySelectorAll('.select2-order-item');
|
||||
console.log('[Order Items] Найдено элементов для инициализации:', items.length);
|
||||
|
||||
items.forEach((item, index) => {
|
||||
items.forEach((selectElement, index) => {
|
||||
console.log(`[Order Items] Инициализация элемента ${index + 1}/${items.length}`);
|
||||
window.initOrderItemSelect2(item);
|
||||
|
||||
// Сначала инициализируем Select2
|
||||
window.initOrderItemSelect2(selectElement);
|
||||
|
||||
// Затем проверяем, есть ли предзаполненные данные в скрытых полях
|
||||
const form = selectElement.closest('.order-item-form');
|
||||
const productField = form.querySelector('[name$="-product"]');
|
||||
const kitField = form.querySelector('[name$="-product_kit"]');
|
||||
const priceField = form.querySelector('[name$="-price"]');
|
||||
|
||||
// Если есть предзаполненный product или kit, но select пустой
|
||||
const hasProduct = productField && productField.value;
|
||||
const hasKit = kitField && kitField.value;
|
||||
const selectValue = $(selectElement).val();
|
||||
|
||||
if ((hasProduct || hasKit) && !selectValue) {
|
||||
console.log(`[Order Items] Найдены предзаполненные данные в форме ${index}`);
|
||||
|
||||
// Определяем тип и ID
|
||||
const type = hasProduct ? 'product' : 'kit';
|
||||
const id = hasProduct ? productField.value : kitField.value;
|
||||
const price = priceField ? priceField.value : '';
|
||||
|
||||
// Загружаем данные товара через API
|
||||
const apiUrl = '{% url "products:api-search-products-variants" %}';
|
||||
$.ajax({
|
||||
url: apiUrl,
|
||||
type: 'GET',
|
||||
dataType: 'json',
|
||||
data: {
|
||||
id: `${type}_${id}` // Передаём в формате "product_123" или "kit_456"
|
||||
},
|
||||
success: function(data) {
|
||||
if (data.results && data.results.length > 0) {
|
||||
const item = data.results[0];
|
||||
|
||||
// Создаём option с данными товара
|
||||
const optionValue = `${type}_${id}`;
|
||||
const optionText = item.text || item.name || 'Товар';
|
||||
|
||||
// Добавляем option в select
|
||||
const newOption = new Option(optionText, optionValue, true, true);
|
||||
$(selectElement).append(newOption).trigger('change');
|
||||
|
||||
// Сохраняем оригинальную цену
|
||||
if (priceField && item.actual_price) {
|
||||
priceField.dataset.originalPrice = item.actual_price;
|
||||
}
|
||||
|
||||
console.log(`[Order Items] Товар загружен: ${optionText}`);
|
||||
}
|
||||
},
|
||||
error: function(xhr, status, error) {
|
||||
console.error(`[Order Items] Ошибка загрузки товара:`, error);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
console.log('[Order Items] Инициализация всех существующих элементов завершена');
|
||||
|
||||
Reference in New Issue
Block a user