/**
* SupplierLink3 - Replenish page enhancements
* Ersetzt die "Aktueller Lagerbestand"-Spalte mit Badge + Shop-Link
*/
console.log('SL3: replenish.js loaded');
$(document).ready(function() {
console.log('SL3: DOM ready, sl3Data exists:', typeof window.sl3Data !== 'undefined');
if (typeof window.sl3Data === 'undefined') {
console.log('SL3: No data found, exiting');
return;
}
var productSuppliers = window.sl3Data;
var shopIcon = window.sl3ShopIcon || 'fas fa-store';
console.log('SL3: Processing', Object.keys(productSuppliers).length, 'products');
// CSS einfügen
$('head').append('');
// Modal-Handler
$(document).on('click', '.sl3-modal-trigger', function(e) {
e.preventDefault();
var suppliers = $(this).data('suppliers');
if (!suppliers || suppliers.length === 0) return;
var content = '';
for (var i = 0; i < suppliers.length; i++) {
var sup = suppliers[i];
var isFirst = (i === 0);
var bgStyle = isFirst ? 'background-color: #e8f4fd;' : '';
var star = isFirst ? '' : '';
content += '
';
content += '
' + star + sup.supplier_name + '
';
content += '
';
content += '' + parseFloat(sup.price).toFixed(2).replace('.', ',') + ' EUR';
content += ' - Art.-Nr: ' + sup.ref_fourn;
if (sup.min_qty > 1) content += ' (ab ' + sup.min_qty + ' St.)';
content += '
';
content += '
Im Shop';
content += '
';
}
$('').html(content).dialog({
modal: true,
title: 'Lieferanten-Shops',
width: 400,
buttons: { 'Schließen': function() { $(this).dialog('close'); } }
});
});
// Spalten-Indizes aus der Header-Zeile ermitteln
var colIndex = { stock: -1, desired: -1, alert: -1 };
$('table.liste tr.liste_titre th, table.liste tr.liste_titre td').each(function(idx) {
var text = $(this).text().trim().toLowerCase();
// Stock-Spalte (Aktueller Lagerbestand / Physical stock / Physischer Bestand)
if (text.indexOf('lagerbestand') >= 0 || text.indexOf('physical stock') >= 0 ||
text.indexOf('stock physique') >= 0 || text.indexOf('physischer bestand') >= 0) {
colIndex.stock = idx;
}
// Desired Stock (Gewünschter Lagerbestand / Wunschbestand)
if (text.indexOf('gewünscht') >= 0 || text.indexOf('wunsch') >= 0 ||
text.indexOf('desired') >= 0 || text.indexOf('souhaité') >= 0) {
colIndex.desired = idx;
}
// Alert Stock (Grenzwert für Warnung / Mindestbestand / Stock limit)
if (text.indexOf('grenzwert') >= 0 || text.indexOf('warnung') >= 0 ||
text.indexOf('mindest') >= 0 || text.indexOf('limit') >= 0 ||
text.indexOf('alerte') >= 0 || text.indexOf('alert') >= 0) {
colIndex.alert = idx;
}
});
console.log('SL3: Column indices found:', colIndex);
// Fallback auf feste Indizes wenn nicht gefunden (für Produkte ohne Service-Spalte)
if (colIndex.stock < 0) colIndex.stock = 5;
if (colIndex.desired < 0) colIndex.desired = 3;
if (colIndex.alert < 0) colIndex.alert = 4;
// Jede Datenzeile in der Tabelle durchgehen
var rowsFound = 0;
var rowsProcessed = 0;
$('table.liste tr').not('.liste_titre').each(function() {
rowsFound++;
var $row = $(this);
// Produkt-Link finden um die Produkt-ID zu extrahieren
var $productLink = $row.find('td a[href*="product/card.php?id="]').first();
if ($productLink.length === 0) {
// Versuche alternative Selektoren
$productLink = $row.find('td a[href*="product.php?id="]').first();
}
if ($productLink.length === 0) {
console.log('SL3: Row', rowsFound, '- no product link found');
return;
}
var href = $productLink.attr('href');
var match = href.match(/id=(\d+)/);
if (!match) {
console.log('SL3: Row', rowsFound, '- no ID in href:', href);
return;
}
var productId = match[1];
rowsProcessed++;
// Stock-Spalte dynamisch finden
var $stockCell = $row.find('td').eq(colIndex.stock);
if ($stockCell.length === 0) return;
// Stock-Wert aus der Zelle lesen
var stock = parseFloat($stockCell.text().trim()) || 0;
// Desired-Stock und Alert-Stock
var desired = parseFloat($row.find('td').eq(colIndex.desired).text().trim()) || 0;
var alert = parseFloat($row.find('td').eq(colIndex.alert).text().trim()) || 0;
// Badge-Klasse bestimmen
var badgeClass = 'badge-success';
if (stock < 1) {
badgeClass = 'badge-danger';
} else if (alert > 0 && stock <= alert) {
badgeClass = 'badge-warning';
} else if (desired > 0 && stock < desired) {
badgeClass = 'badge-secondary';
}
// Shop-Link erstellen (immer mit Container für feste Breite)
var shopIconHtml = '';
var suppliers = productSuppliers[productId];
if (suppliers && suppliers.length > 0) {
if (suppliers.length === 1) {
var sup = suppliers[0];
shopIconHtml = '
' +
'';
} else {
shopIconHtml = '
' +
'';
}
}
// Stock-Zelle ersetzen: inline-flex wie im Kundenauftrag
var html = '
' +
'' + shopIconHtml + '' +
'' + Math.floor(stock) + '' +
'
';
$stockCell.html(html).addClass('right').css('text-align', 'right');
});
console.log('SL3: Processing complete - rows found:', rowsFound, 'rows processed:', rowsProcessed);
});