subtotaltitle/js/subtotaltitle_sync.js

262 lines
9.5 KiB
JavaScript
Executable file

// ==========================================
// SUBTOTALTITLE SYNC FUNKTIONEN
// Für Synchronisation mit facturedet
// ==========================================
/**
* Erkenne Dokumenttyp aus URL (lokale Kopie für Sync)
*/
function getDocumentTypeForSync() {
var url = window.location.href;
var docType = 'invoice'; // Default
if (url.indexOf('/comm/propal/') !== -1) {
docType = 'propal';
} else if (url.indexOf('/commande/') !== -1) {
docType = 'order';
}
return docType;
}
/**
* Synchronisiert eine Zeile mit facturedet (hinzufügen)
*/
function syncToFacturedet(lineId, lineType) {
var lang = (typeof subtotalTitleLang !== 'undefined') ? subtotalTitleLang : {};
debugLog('📤 Sync to facturedet: ' + lineType + ' #' + lineId);
var docType = getDocumentTypeForSync();
debugLog('Document type: ' + docType);
$.post(subtotaltitleAjaxUrl + 'sync_to_facturedet.php', {
action: 'add',
line_id: lineId,
line_type: lineType,
document_type: docType
}, function(response) {
debugLog('Sync response: ' + JSON.stringify(response));
if (response.success) {
updateSyncCheckbox(lineId, true);
debugLog('✅ Zeile zu Rechnung hinzugefügt');
} else {
showErrorAlert((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);
showErrorAlert((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 : {};
showConfirmDialog(
'Aus Rechnung entfernen',
lang.confirmRemoveLine || 'Zeile aus der Rechnung entfernen?<br><br><em>Die Zeile bleibt in der Positionsgruppen-Verwaltung erhalten.</em>',
function() {
debugLog('📥 Remove from facturedet: ' + lineType + ' #' + lineId);
var docType = getDocumentTypeForSync();
debugLog('Document type: ' + docType);
$.post(subtotaltitleAjaxUrl + 'sync_to_facturedet.php', {
action: 'remove',
line_id: lineId,
line_type: lineType,
document_type: docType
}, function(response) {
debugLog('Remove response: ' + JSON.stringify(response));
if (response.success) {
updateSyncCheckbox(lineId, false);
debugLog('✅ Zeile aus Rechnung entfernt');
} else {
showErrorAlert((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);
showErrorAlert((lang.errorSyncing || 'Fehler') + ': ' + error);
$('.sync-checkbox[data-line-id="' + lineId + '"]').prop('checked', true);
});
},
'Ja, entfernen',
'Abbrechen'
);
// Checkbox zurücksetzen bis Bestätigung
$('.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 : {};
var $unchecked = $('.sync-checkbox:not(:checked)');
var total = $unchecked.length;
if (total === 0) {
showErrorAlert(lang.allElementsAlreadyInInvoice || 'Alle Elemente sind bereits in der Rechnung.');
return;
}
showConfirmDialog(
'Alle zur Rechnung hinzufügen',
lang.confirmSyncAll || 'Alle Positionsgruppen-Elemente (Sections, Textzeilen, Zwischensummen) zur Rechnung hinzufügen?',
function() {
debugLog('📤 Sync ALL to facturedet...');
var done = 0;
var errors = 0;
var docType = getDocumentTypeForSync();
// Zeige Loading-Hinweis
$('body').append('<div id="sync-loading" style="position:fixed;top:0;left:0;right:0;bottom:0;background:rgba(0,0,0,0.3);z-index:9999;display:flex;align-items:center;justify-content:center;"><div style="background:#fff;padding:20px 40px;border-radius:8px;box-shadow:0 2px 10px rgba(0,0,0,0.2);">Synchronisiere... Bitte warten.</div></div>');
$unchecked.each(function() {
var lineId = $(this).data('line-id');
var lineType = $(this).data('line-type');
$.post(subtotaltitleAjaxUrl + 'sync_to_facturedet.php', {
action: 'add',
line_id: lineId,
line_type: lineType,
document_type: docType
}, function(response) {
done++;
if (!response.success) {
errors++;
}
if (done >= total) {
debugLog('✅ Sync abgeschlossen: ' + (total - errors) + ' erfolgreich, ' + errors + ' Fehler');
if (errors > 0) {
$('#sync-loading').remove();
showErrorAlert((total - errors) + ' von ' + total + ' Elementen hinzugefügt. ' + errors + ' Fehler aufgetreten.');
} else {
// Direkt reloaden ohne UI-Update
safeReload();
}
}
}, 'json').fail(function() {
done++;
errors++;
});
});
},
'Ja, hinzufügen',
'Abbrechen'
);
}
/**
* Entfernt ALLE Sections/Textzeilen/Subtotals aus facturedet
* Inkl. verwaister Einträge die nicht mehr in der Manager-Tabelle existieren
*/
function removeAllFromFacturedet() {
var lang = (typeof subtotalTitleLang !== 'undefined') ? subtotalTitleLang : {};
showConfirmDialog(
'Alle aus Rechnung entfernen',
(lang.confirmRemoveAll || 'ALLE Positionsgruppen-Elemente (Sections, Textzeilen, Zwischensummen) aus der Rechnung entfernen?') +
'<br><br><em>Inkl. verwaister Einträge. Die Elemente in der Verwaltung bleiben erhalten.</em>',
function() {
debugLog('📥 Remove ALL from facturedet (server-side)...');
var docType = getDocumentTypeForSync();
var documentId = getFactureId();
if (!documentId) {
showErrorAlert('Fehler: Keine Dokument-ID gefunden');
return;
}
$.post(subtotaltitleAjaxUrl + 'sync_to_facturedet.php', {
action: 'remove_all',
line_id: 1, // Dummy, wird benötigt wegen Parameter-Check
document_id: documentId,
document_type: docType
}, function(response) {
debugLog('Remove ALL response: ' + JSON.stringify(response));
if (response.success) {
debugLog('✅ Alle Spezialzeilen entfernt');
safeReload();
} else {
showErrorAlert((lang.errorSyncing || 'Fehler') + ': ' + (response.error || 'Unbekannter Fehler'));
}
}, 'json').fail(function(xhr, status, error) {
debugLog('AJAX Fehler: ' + status + ' ' + error);
showErrorAlert((lang.errorSyncing || 'Fehler') + ': ' + error);
});
},
'Ja, alle entfernen',
'Abbrechen'
);
}
/**
* 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;
}
var docType = getDocumentTypeForSync();
$subtotals.each(function() {
var lineId = $(this).data('line-id');
$.post(subtotaltitleAjaxUrl + 'sync_to_facturedet.php', {
action: 'update_subtotal',
line_id: lineId,
document_type: docType
}, function(response) {
done++;
debugLog('Subtotal #' + lineId + ' updated: ' + JSON.stringify(response));
if (done >= total) {
debugLog('✅ Alle ' + total + ' Subtotals aktualisiert');
}
}, 'json');
});
}