// ========================================== // SUBTOTALTITLE SYNC FUNKTIONEN // Für Synchronisation mit facturedet // ========================================== /** * Synchronisiert eine Zeile mit facturedet (hinzufügen) */ function syncToFacturedet(lineId, lineType) { var lang = (typeof subtotalTitleLang !== 'undefined') ? subtotalTitleLang : {}; debugLog('📤 Sync to facturedet: ' + lineType + ' #' + lineId); $.post('/dolibarr/custom/subtotaltitle/ajax/sync_to_facturedet.php', { action: 'add', line_id: lineId, line_type: lineType }, function(response) { debugLog('Sync response: ' + JSON.stringify(response)); if (response.success) { updateSyncCheckbox(lineId, true); debugLog('✅ Zeile zu Rechnung hinzugefügt'); } else { alert((lang.errorSyncing || 'Fehler') + ': ' + (response.error || 'Unbekannter Fehler')); // Checkbox zurücksetzen $('.sync-checkbox[data-line-id="' + lineId + '"]').prop('checked', false); } }, 'json').fail(function(xhr, status, error) { debugLog('AJAX Fehler: ' + status + ' ' + error); alert((lang.errorSyncing || 'Fehler beim Synchronisieren') + ': ' + error); $('.sync-checkbox[data-line-id="' + lineId + '"]').prop('checked', false); }); } /** * Entfernt eine Zeile aus facturedet */ function removeFromFacturedet(lineId, lineType) { var lang = (typeof subtotalTitleLang !== 'undefined') ? subtotalTitleLang : {}; if (!confirm(lang.confirmRemoveLine || 'Zeile aus der Rechnung entfernen?\n\nDie Zeile bleibt in der Positionsgruppen-Verwaltung erhalten.')) { // Checkbox zurücksetzen $('.sync-checkbox[data-line-id="' + lineId + '"]').prop('checked', true); return; } debugLog('📥 Remove from facturedet: ' + lineType + ' #' + lineId); $.post('/dolibarr/custom/subtotaltitle/ajax/sync_to_facturedet.php', { action: 'remove', line_id: lineId, line_type: lineType }, function(response) { debugLog('Remove response: ' + JSON.stringify(response)); if (response.success) { updateSyncCheckbox(lineId, false); debugLog('✅ Zeile aus Rechnung entfernt'); } else { alert((lang.errorSyncing || 'Fehler') + ': ' + (response.error || 'Unbekannter Fehler')); $('.sync-checkbox[data-line-id="' + lineId + '"]').prop('checked', true); } }, 'json').fail(function(xhr, status, error) { debugLog('AJAX Fehler: ' + status + ' ' + error); alert((lang.errorSyncing || 'Fehler') + ': ' + error); $('.sync-checkbox[data-line-id="' + lineId + '"]').prop('checked', true); }); } /** * Toggle-Handler für Sync-Checkbox */ function toggleFacturedetSync(lineId, lineType, checkbox) { if (event) event.stopPropagation(); if (checkbox.checked) { syncToFacturedet(lineId, lineType); } else { removeFromFacturedet(lineId, lineType); } } /** * Aktualisiert den visuellen Status der Sync-Checkbox */ function updateSyncCheckbox(lineId, isInFacturedet) { var $checkbox = $('.sync-checkbox[data-line-id="' + lineId + '"]'); $checkbox.prop('checked', isInFacturedet); var $row = $checkbox.closest('tr'); if (isInFacturedet) { $row.addClass('in-facturedet'); } else { $row.removeClass('in-facturedet'); } } /** * Synchronisiert ALLE Sections/Textzeilen/Subtotals auf einmal */ function syncAllToFacturedet() { var lang = (typeof subtotalTitleLang !== 'undefined') ? subtotalTitleLang : {}; if (!confirm(lang.confirmSyncAll || 'Alle Positionsgruppen-Elemente (Sections, Textzeilen, Zwischensummen) zur Rechnung hinzufügen?')) { return; } debugLog('📤 Sync ALL to facturedet...'); var $unchecked = $('.sync-checkbox:not(:checked)'); var total = $unchecked.length; var done = 0; var errors = 0; if (total === 0) { alert(lang.allElementsAlreadyInInvoice || 'Alle Elemente sind bereits in der Rechnung.'); return; } $unchecked.each(function() { var lineId = $(this).data('line-id'); var lineType = $(this).data('line-type'); $.post('/dolibarr/custom/subtotaltitle/ajax/sync_to_facturedet.php', { action: 'add', line_id: lineId, line_type: lineType }, function(response) { done++; if (response.success) { updateSyncCheckbox(lineId, true); } else { errors++; } if (done >= total) { debugLog('✅ Sync abgeschlossen: ' + (total - errors) + ' erfolgreich, ' + errors + ' Fehler'); if (errors > 0) { var msg = (lang.elementsAddedWithErrors || '%s von %s Elementen hinzugefügt.\n%s Fehler aufgetreten.') .replace('%s', total - errors).replace('%s', total).replace('%s', errors); alert(msg); } else { var msg = (lang.elementsAddedToInvoice || '%s Elemente zur Rechnung hinzugefügt.').replace('%s', total); alert(msg); } } }, 'json').fail(function() { done++; errors++; }); }); } /** * Entfernt ALLE Sections/Textzeilen/Subtotals aus facturedet */ function removeAllFromFacturedet() { var lang = (typeof subtotalTitleLang !== 'undefined') ? subtotalTitleLang : {}; if (!confirm(lang.confirmRemoveAll || 'ALLE Positionsgruppen-Elemente aus der Rechnung entfernen?\n\nDie Elemente bleiben in der Verwaltung erhalten.')) { return; } debugLog('📥 Remove ALL from facturedet...'); var $checked = $('.sync-checkbox:checked'); var total = $checked.length; var done = 0; var errors = 0; if (total === 0) { alert(lang.noElementsInInvoice || 'Keine Elemente in der Rechnung vorhanden.'); return; } $checked.each(function() { var lineId = $(this).data('line-id'); var lineType = $(this).data('line-type'); $.post('/dolibarr/custom/subtotaltitle/ajax/sync_to_facturedet.php', { action: 'remove', line_id: lineId, line_type: lineType }, function(response) { done++; if (response.success) { updateSyncCheckbox(lineId, false); } else { errors++; } if (done >= total) { debugLog('✅ Remove abgeschlossen: ' + (total - errors) + ' erfolgreich, ' + errors + ' Fehler'); if (errors > 0) { var msg = (lang.elementsRemovedWithErrors || '%s von %s Elementen entfernt.\n%s Fehler aufgetreten.') .replace('%s', total - errors).replace('%s', total).replace('%s', errors); alert(msg); } else { var msg = (lang.elementsRemovedFromInvoice || '%s Elemente aus Rechnung entfernt.').replace('%s', total); alert(msg); } } }, 'json').fail(function() { done++; errors++; }); }); } /** * Aktualisiert alle Subtotals in facturedet (nach Preisänderungen) */ function updateAllSubtotals() { debugLog('🔄 Update all subtotals...'); var $subtotals = $('.sync-checkbox[data-line-type="subtotal"]:checked'); var total = $subtotals.length; var done = 0; if (total === 0) { debugLog('Keine Subtotals in facturedet'); return; } $subtotals.each(function() { var lineId = $(this).data('line-id'); $.post('/dolibarr/custom/subtotaltitle/ajax/sync_to_facturedet.php', { action: 'update_subtotal', line_id: lineId }, function(response) { done++; debugLog('Subtotal #' + lineId + ' updated: ' + JSON.stringify(response)); if (done >= total) { debugLog('✅ Alle ' + total + ' Subtotals aktualisiert'); } }, 'json'); }); }