167 lines
7.8 KiB
JavaScript
Executable file
167 lines
7.8 KiB
JavaScript
Executable file
/**
|
|
* 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('<style>' +
|
|
'.badge.badge-danger { background-color: #dc3545 !important; color: #fff !important; }' +
|
|
'.badge.badge-warning { background-color: #fd7e14 !important; color: #fff !important; }' +
|
|
'.badge.badge-secondary { background-color: #6c757d !important; color: #fff !important; }' +
|
|
'.badge.badge-success { background-color: #28a745 !important; color: #fff !important; }' +
|
|
'.sl3-stock-wrapper { display: inline-flex; align-items: center; justify-content: flex-end; }' +
|
|
'.sl3-shop-col { display: inline-block; width: 28px; text-align: center; }' +
|
|
'.sl3-badge-col { display: inline-block; min-width: 55px; text-align: right; }' +
|
|
'.sl3-modal-item { padding: 10px; border-bottom: 1px solid #eee; }' +
|
|
'.sl3-modal-item:hover { background-color: #f5f5f5; }' +
|
|
'.sl3-modal-item:last-child { border-bottom: none; }' +
|
|
'</style>');
|
|
|
|
// 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 ? '<i class="fas fa-star" style="color: gold; margin-right: 5px;"></i>' : '';
|
|
|
|
content += '<div class="sl3-modal-item" style="' + bgStyle + '">';
|
|
content += '<div style="font-weight: ' + (isFirst ? 'bold' : 'normal') + '; margin-bottom: 5px;">' + star + sup.supplier_name + '</div>';
|
|
content += '<div style="font-size: 12px; color: #666; margin-bottom: 8px;">';
|
|
content += '<strong>' + parseFloat(sup.price).toFixed(2).replace('.', ',') + ' EUR</strong>';
|
|
content += ' - Art.-Nr: ' + sup.ref_fourn;
|
|
if (sup.min_qty > 1) content += ' (ab ' + sup.min_qty + ' St.)';
|
|
content += '</div>';
|
|
content += '<a href="' + sup.full_url + '" target="supplier_' + sup.supplier_id + '" class="button small">Im Shop</a>';
|
|
content += '</div>';
|
|
}
|
|
|
|
$('<div>').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 = '<a href="' + sup.full_url + '" target="supplier_' + sup.supplier_id + '" ' +
|
|
'class="classfortooltip" title="' + sup.supplier_name + '" style="color: #0077b6; font-size: 14px;">' +
|
|
'<i class="' + shopIcon + '"></i></a>';
|
|
} else {
|
|
shopIconHtml = '<a href="#" class="sl3-modal-trigger" data-suppliers=\'' + JSON.stringify(suppliers) + '\' ' +
|
|
'title="' + suppliers.length + ' Lieferanten" style="color: #0077b6; font-size: 14px;">' +
|
|
'<i class="' + shopIcon + '"></i><i class="fas fa-caret-down" style="font-size:8px;margin-left:2px;"></i></a>';
|
|
}
|
|
}
|
|
|
|
// Stock-Zelle ersetzen: inline-flex wie im Kundenauftrag
|
|
var html = '<div class="sl3-stock-wrapper">' +
|
|
'<span class="sl3-shop-col">' + shopIconHtml + '</span>' +
|
|
'<span class="sl3-badge-col"><span class="badge ' + badgeClass + '">' + Math.floor(stock) + '</span></span>' +
|
|
'</div>';
|
|
$stockCell.html(html).addClass('right').css('text-align', 'right');
|
|
});
|
|
|
|
console.log('SL3: Processing complete - rows found:', rowsFound, 'rows processed:', rowsProcessed);
|
|
});
|