📚 Improved backend structure & documentation, added new features, and refactored scripts. 🚀🔧📝💻🖥️

This commit is contained in:
2025-06-01 00:47:00 +02:00
parent 7f38f8a7e5
commit 070f4a6165
20 changed files with 3336 additions and 403 deletions

View File

@ -1117,7 +1117,7 @@ document.addEventListener('DOMContentLoaded', function() {
};
window.exportCalendar = function() {
alert('Export-Funktion wird implementiert...');
showExportModal();
};
window.deleteEvent = function() {
@ -1127,166 +1127,291 @@ document.addEventListener('DOMContentLoaded', function() {
}
};
// Intelligente Druckerempfehlung
function updatePrinterRecommendation() {
const printerSelect = document.getElementById('eventPrinter');
const startTime = document.getElementById('eventStart').value;
const endTime = document.getElementById('eventEnd').value;
const priority = document.getElementById('eventPriority').value;
// Export-Modal Funktionen
function showExportModal() {
// Modal HTML erstellen falls nicht vorhanden
let exportModal = document.getElementById('exportModal');
if (!exportModal) {
createExportModal();
exportModal = document.getElementById('exportModal');
}
if (printerSelect.value === '' && startTime && endTime) {
// Echte API-Empfehlung abrufen
fetchSmartRecommendation(startTime, endTime, priority);
} else {
hideSmartRecommendation();
// Modal anzeigen
exportModal.classList.remove('hidden');
setTimeout(() => {
const modalContent = exportModal.querySelector('.modal-content');
modalContent.classList.remove('scale-95', 'opacity-0');
modalContent.classList.add('scale-100', 'opacity-100');
}, 10);
}
function hideExportModal() {
const exportModal = document.getElementById('exportModal');
if (exportModal) {
const modalContent = exportModal.querySelector('.modal-content');
modalContent.classList.remove('scale-100', 'opacity-100');
modalContent.classList.add('scale-95', 'opacity-0');
setTimeout(() => {
exportModal.classList.add('hidden');
}, 200);
}
}
async function fetchSmartRecommendation(start, end, priority) {
function createExportModal() {
const modalHTML = `
<div id="exportModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 hidden">
<div class="modal-content bg-white dark:bg-slate-800 rounded-xl shadow-2xl w-full max-w-md mx-4 transform transition-all duration-200 scale-95 opacity-0">
<div class="flex items-center justify-between p-6 border-b border-slate-200 dark:border-slate-600">
<div>
<h3 class="text-xl font-bold text-slate-900 dark:text-white mb-2">
📊 Schichtplan Export
</h3>
<p class="text-slate-500 dark:text-slate-400">Exportieren Sie Ihre Produktionsplanung</p>
</div>
<button onclick="hideExportModal()" class="p-2 hover:bg-gray-100 dark:hover:bg-slate-700 rounded-lg transition-colors">
<svg class="w-6 h-6 text-slate-500 dark:text-slate-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
</svg>
</button>
</div>
<div class="p-6 space-y-6">
<!-- Export-Format -->
<div>
<label class="block text-sm font-medium text-slate-700 dark:text-slate-300 mb-3">
📋 Export-Format
</label>
<div class="grid grid-cols-3 gap-3">
<label class="flex items-center p-3 border-2 border-slate-200 dark:border-slate-600 rounded-lg cursor-pointer hover:border-blue-300 dark:hover:border-blue-500 transition-colors export-format-option">
<input type="radio" name="exportFormat" value="csv" checked class="text-blue-600 focus:ring-blue-500">
<div class="ml-2">
<div class="text-sm font-medium text-slate-900 dark:text-white">CSV</div>
<div class="text-xs text-slate-500 dark:text-slate-400">Excel-kompatibel</div>
</div>
</label>
<label class="flex items-center p-3 border-2 border-slate-200 dark:border-slate-600 rounded-lg cursor-pointer hover:border-blue-300 dark:hover:border-blue-500 transition-colors export-format-option">
<input type="radio" name="exportFormat" value="json" class="text-blue-600 focus:ring-blue-500">
<div class="ml-2">
<div class="text-sm font-medium text-slate-900 dark:text-white">JSON</div>
<div class="text-xs text-slate-500 dark:text-slate-400">Strukturiert</div>
</div>
</label>
<label class="flex items-center p-3 border-2 border-slate-200 dark:border-slate-600 rounded-lg cursor-pointer hover:border-blue-300 dark:hover:border-blue-500 transition-colors export-format-option">
<input type="radio" name="exportFormat" value="excel" class="text-blue-600 focus:ring-blue-500">
<div class="ml-2">
<div class="text-sm font-medium text-slate-900 dark:text-white">Excel</div>
<div class="text-xs text-slate-500 dark:text-slate-400">Mit Statistiken</div>
</div>
</label>
</div>
</div>
<!-- Zeitraum -->
<div>
<label class="block text-sm font-medium text-slate-700 dark:text-slate-300 mb-3">
📅 Zeitraum
</label>
<div class="grid grid-cols-2 gap-3">
<div>
<label class="block text-xs text-slate-500 dark:text-slate-400 mb-1">Von</label>
<input type="date" id="exportStartDate" class="w-full px-3 py-2 border border-slate-300 dark:border-slate-600 rounded-lg bg-white dark:bg-slate-700 text-slate-900 dark:text-white text-sm">
</div>
<div>
<label class="block text-xs text-slate-500 dark:text-slate-400 mb-1">Bis</label>
<input type="date" id="exportEndDate" class="w-full px-3 py-2 border border-slate-300 dark:border-slate-600 rounded-lg bg-white dark:bg-slate-700 text-slate-900 dark:text-white text-sm">
</div>
</div>
<div class="mt-2">
<button onclick="setExportDateRange('week')" class="text-xs px-2 py-1 bg-blue-100 dark:bg-blue-900 text-blue-700 dark:text-blue-300 rounded mr-2 hover:bg-blue-200 dark:hover:bg-blue-800">Diese Woche</button>
<button onclick="setExportDateRange('month')" class="text-xs px-2 py-1 bg-blue-100 dark:bg-blue-900 text-blue-700 dark:text-blue-300 rounded mr-2 hover:bg-blue-200 dark:hover:bg-blue-800">Dieser Monat</button>
<button onclick="setExportDateRange('quarter')" class="text-xs px-2 py-1 bg-blue-100 dark:bg-blue-900 text-blue-700 dark:text-blue-300 rounded hover:bg-blue-200 dark:hover:bg-blue-800">Dieses Quartal</button>
</div>
</div>
<!-- Filter -->
<div>
<label class="block text-sm font-medium text-slate-700 dark:text-slate-300 mb-3">
🔍 Filter (Optional)
</label>
<div class="space-y-3">
<div>
<label class="block text-xs text-slate-500 dark:text-slate-400 mb-1">Drucker</label>
<select id="exportPrinterFilter" class="w-full px-3 py-2 border border-slate-300 dark:border-slate-600 rounded-lg bg-white dark:bg-slate-700 text-slate-900 dark:text-white text-sm">
<option value="">Alle Drucker</option>
{% for printer in printers %}
<option value="{{ printer.id }}">{{ printer.name }}{% if printer.location %} ({{ printer.location }}){% endif %}</option>
{% endfor %}
</select>
</div>
<div>
<label class="block text-xs text-slate-500 dark:text-slate-400 mb-1">Status</label>
<select id="exportStatusFilter" class="w-full px-3 py-2 border border-slate-300 dark:border-slate-600 rounded-lg bg-white dark:bg-slate-700 text-slate-900 dark:text-white text-sm">
<option value="">Alle Status</option>
<option value="scheduled">Geplant</option>
<option value="running">Läuft</option>
<option value="finished">Abgeschlossen</option>
<option value="cancelled">Abgebrochen</option>
<option value="failed">Fehlgeschlagen</option>
</select>
</div>
</div>
</div>
</div>
<div class="flex items-center justify-end gap-3 p-6 border-t border-slate-200 dark:border-slate-600">
<button onclick="hideExportModal()" class="px-4 py-2 text-slate-600 dark:text-slate-400 hover:text-slate-800 dark:hover:text-slate-200 transition-colors">
Abbrechen
</button>
<button onclick="performExport()" class="px-6 py-2 bg-gradient-to-r from-blue-600 to-blue-700 hover:from-blue-700 hover:to-blue-800 text-white rounded-lg transition-colors flex items-center gap-2">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
</svg>
Export starten
</button>
</div>
</div>
</div>
`;
document.body.insertAdjacentHTML('beforeend', modalHTML);
// Format-Radio-Button-Styling
document.querySelectorAll('.export-format-option input[type="radio"]').forEach(radio => {
radio.addEventListener('change', function() {
document.querySelectorAll('.export-format-option').forEach(option => {
option.classList.remove('border-blue-500', 'bg-blue-50', 'dark:bg-blue-900/20');
});
if (this.checked) {
this.closest('.export-format-option').classList.add('border-blue-500', 'bg-blue-50', 'dark:bg-blue-900/20');
}
});
});
// Standard-Zeitraum setzen (nächste 4 Wochen)
setExportDateRange('month');
}
window.setExportDateRange = function(range) {
const startDate = new Date();
let endDate = new Date();
if (range === 'week') {
// Diese Woche (Montag bis Sonntag)
const dayOfWeek = startDate.getDay() || 7; // Sonntag = 7
startDate.setDate(startDate.getDate() - dayOfWeek + 1);
endDate.setDate(startDate.getDate() + 6);
} else if (range === 'month') {
// Dieser Monat
startDate.setDate(1);
endDate = new Date(startDate.getFullYear(), startDate.getMonth() + 1, 0);
} else if (range === 'quarter') {
// Dieses Quartal
const quarter = Math.floor(startDate.getMonth() / 3);
startDate.setMonth(quarter * 3, 1);
endDate = new Date(startDate.getFullYear(), quarter * 3 + 3, 0);
}
document.getElementById('exportStartDate').value = startDate.toISOString().split('T')[0];
document.getElementById('exportEndDate').value = endDate.toISOString().split('T')[0];
};
window.performExport = async function() {
try {
const response = await fetch('/api/calendar/smart-recommendation', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': document.querySelector('meta[name="csrf-token"]').getAttribute('content')
},
body: JSON.stringify({
start: start,
end: end,
priority: priority
})
// Export-Parameter sammeln
const format = document.querySelector('input[name="exportFormat"]:checked').value;
const startDate = document.getElementById('exportStartDate').value;
const endDate = document.getElementById('exportEndDate').value;
const printerId = document.getElementById('exportPrinterFilter').value;
const status = document.getElementById('exportStatusFilter').value;
// Validierung
if (!startDate || !endDate) {
showErrorNotification('Bitte wählen Sie einen gültigen Zeitraum aus.');
return;
}
if (new Date(startDate) > new Date(endDate)) {
showErrorNotification('Das Startdatum darf nicht nach dem Enddatum liegen.');
return;
}
// URL-Parameter zusammenstellen
const params = new URLSearchParams({
format: format,
start_date: startDate + 'T00:00:00',
end_date: endDate + 'T23:59:59'
});
const data = await response.json();
if (printerId) params.append('printer_id', printerId);
if (status) params.append('status', status);
if (data.success && data.recommendation) {
showSmartRecommendation(data.recommendation);
// Loading-State anzeigen
const exportButton = document.querySelector('button[onclick="performExport()"]');
const originalHTML = exportButton.innerHTML;
exportButton.disabled = true;
exportButton.innerHTML = `
<svg class="animate-spin w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
</svg>
Exportiere...
`;
// Export-Request
const response = await fetch(`/api/calendar/export?${params.toString()}`);
if (response.ok) {
// Datei-Download initiieren
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const link = document.createElement('a');
// Dateiname aus Content-Disposition-Header extrahieren
const contentDisposition = response.headers.get('Content-Disposition');
let filename = `schichtplan_export_${new Date().toISOString().split('T')[0]}.${format}`;
if (contentDisposition) {
const filenameMatch = contentDisposition.match(/filename="(.+)"/);
if (filenameMatch) {
filename = filenameMatch[1];
}
}
link.href = url;
link.download = filename;
link.style.display = 'none';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(url);
// Erfolg anzeigen
showSuccessNotification(`📊 ${format.toUpperCase()}-Export erfolgreich heruntergeladen`);
hideExportModal();
} else {
showNoRecommendationMessage(data.message || 'Keine Empfehlung verfügbar');
const errorData = await response.json();
throw new Error(errorData.error || 'Export fehlgeschlagen');
}
} catch (error) {
console.error('Fehler beim Abrufen der Empfehlung:', error);
showNoRecommendationMessage('Fehler beim Abrufen der Empfehlung');
console.error('Export-Fehler:', error);
showErrorNotification(`Export fehlgeschlagen: ${error.message}`);
} finally {
// Loading-State zurücksetzen
const exportButton = document.querySelector('button[onclick="performExport()"]');
if (exportButton) {
exportButton.disabled = false;
exportButton.innerHTML = originalHTML;
}
}
}
};
function showSmartRecommendation(recommendation) {
let existingRecommendation = document.getElementById('smart-recommendation');
if (existingRecommendation) {
existingRecommendation.remove();
}
const printerContainer = document.getElementById('eventPrinter').parentElement;
const recommendationDiv = document.createElement('div');
recommendationDiv.id = 'smart-recommendation';
recommendationDiv.className = 'mt-3 p-4 bg-gradient-to-r from-green-50 to-blue-50 dark:from-green-900/20 dark:to-blue-900/20 rounded-lg border border-green-200 dark:border-green-800 transition-all duration-300';
// Optimierungsindikator
const optimizedBadge = recommendation.priority_optimized
? '<span class="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-green-100 text-green-800 dark:bg-green-900/50 dark:text-green-300 ml-2">✨ Priorität optimiert</span>'
: '';
recommendationDiv.innerHTML = `
<div class="flex items-start gap-3">
<div class="w-8 h-8 bg-gradient-to-br from-green-400 to-blue-500 rounded-full flex items-center justify-center animate-pulse">
<svg class="w-4 h-4 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m3.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.374 3.374 0 0014 18.469V19a2 2 0 11-4 0v-.531c0-.895-.356-1.754-.988-2.386l-.548-.547z"/>
</svg>
</div>
<div class="flex-1">
<h4 class="font-semibold text-green-800 dark:text-green-300 mb-2 flex items-center">
🎯 Intelligente Empfehlung${optimizedBadge}
</h4>
<div class="text-sm text-green-700 dark:text-green-400">
<div class="flex items-center gap-2 mb-1">
<span class="font-medium">Empfohlener Drucker:</span>
<span class="px-2 py-1 bg-white dark:bg-slate-800 rounded-full border border-green-300 dark:border-green-600 font-medium">
🖨️ ${recommendation.printer_name}
</span>
${recommendation.location ? `<span class="text-xs text-green-600 dark:text-green-500">(📍 ${recommendation.location})</span>` : ''}
</div>
<div class="text-xs text-green-600 dark:text-green-500 mt-2">
💡 ${recommendation.reason}
</div>
<div class="flex items-center gap-4 mt-2 text-xs">
<span class="flex items-center gap-1">
<span class="w-2 h-2 bg-green-500 rounded-full"></span>
Verfügbarkeit: ${recommendation.availability}
</span>
<span class="flex items-center gap-1">
<span class="w-2 h-2 bg-blue-500 rounded-full"></span>
Auslastung: ${recommendation.utilization}
</span>
<span class="flex items-center gap-1">
<span class="w-2 h-2 bg-purple-500 rounded-full"></span>
Eignung: ${recommendation.suitability}
</span>
</div>
</div>
</div>
</div>
`;
printerContainer.appendChild(recommendationDiv);
// Animation
setTimeout(() => {
recommendationDiv.classList.add('animate-pulse');
setTimeout(() => recommendationDiv.classList.remove('animate-pulse'), 1500);
}, 100);
}
function showNoRecommendationMessage(message) {
let existingRecommendation = document.getElementById('smart-recommendation');
if (existingRecommendation) {
existingRecommendation.remove();
}
const printerContainer = document.getElementById('eventPrinter').parentElement;
const recommendationDiv = document.createElement('div');
recommendationDiv.id = 'smart-recommendation';
recommendationDiv.className = 'mt-3 p-4 bg-gradient-to-r from-yellow-50 to-orange-50 dark:from-yellow-900/20 dark:to-orange-900/20 rounded-lg border border-yellow-200 dark:border-yellow-800 transition-all duration-300';
recommendationDiv.innerHTML = `
<div class="flex items-start gap-3">
<div class="w-8 h-8 bg-gradient-to-br from-yellow-400 to-orange-500 rounded-full flex items-center justify-center">
<svg class="w-4 h-4 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.864-.833-2.634 0L4.18 16.5c-.77.833.192 2.5 1.732 2.5z"/>
</svg>
</div>
<div class="flex-1">
<h4 class="font-semibold text-yellow-800 dark:text-yellow-300 mb-2">⚠️ Keine automatische Zuweisung möglich</h4>
<div class="text-sm text-yellow-700 dark:text-yellow-400">
${message}
<div class="mt-2 text-xs">
Bitte wählen Sie einen spezifischen Drucker aus der Liste oder ändern Sie den Zeitraum.
</div>
</div>
</div>
</div>
`;
printerContainer.appendChild(recommendationDiv);
}
function hideSmartRecommendation() {
const existingRecommendation = document.getElementById('smart-recommendation');
if (existingRecommendation) {
existingRecommendation.style.opacity = '0';
existingRecommendation.style.transform = 'translateY(-10px)';
setTimeout(() => existingRecommendation.remove(), 300);
}
}
// Event Listeners für dynamische Empfehlung
document.getElementById('eventStart').addEventListener('change', updatePrinterRecommendation);
document.getElementById('eventEnd').addEventListener('change', updatePrinterRecommendation);
document.getElementById('eventPriority').addEventListener('change', updatePrinterRecommendation);
document.getElementById('eventPrinter').addEventListener('change', function() {
if (this.value !== '') {
hideSmartRecommendation();
} else {
updatePrinterRecommendation();
// Modal schließen bei Klick außerhalb
document.addEventListener('click', function(event) {
const exportModal = document.getElementById('exportModal');
if (exportModal && event.target === exportModal) {
hideExportModal();
}
});
});