// ==========================================
// 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('/dolibarr/custom/subtotaltitle/ajax/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?
Die Zeile bleibt in der Positionsgruppen-Verwaltung erhalten.',
function() {
debugLog('📥 Remove from facturedet: ' + lineType + ' #' + lineId);
var docType = getDocumentTypeForSync();
debugLog('Document type: ' + docType);
$.post('/dolibarr/custom/subtotaltitle/ajax/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();
$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,
document_type: docType
}, 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) {
showErrorAlert((total - errors) + ' von ' + total + ' Elementen hinzugefügt. ' + errors + ' Fehler aufgetreten.');
} else {
safeReload();
}
}
}, 'json').fail(function() {
done++;
errors++;
});
});
},
'Ja, hinzufügen',
'Abbrechen'
);
}
/**
* Entfernt ALLE Sections/Textzeilen/Subtotals aus facturedet
*/
function removeAllFromFacturedet() {
var lang = (typeof subtotalTitleLang !== 'undefined') ? subtotalTitleLang : {};
var $checked = $('.sync-checkbox:checked');
var total = $checked.length;
if (total === 0) {
showErrorAlert(lang.noElementsInInvoice || 'Keine Elemente in der Rechnung vorhanden.');
return;
}
showConfirmDialog(
'Alle aus Rechnung entfernen',
(lang.confirmRemoveAll || 'ALLE Positionsgruppen-Elemente aus der Rechnung entfernen?') + '
Die Elemente bleiben in der Verwaltung erhalten.',
function() {
debugLog('📥 Remove ALL from facturedet...');
var done = 0;
var errors = 0;
var docType = getDocumentTypeForSync();
$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,
document_type: docType
}, 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) {
showErrorAlert((total - errors) + ' von ' + total + ' Elementen entfernt. ' + errors + ' Fehler aufgetreten.');
} else {
safeReload();
}
}
}, 'json').fail(function() {
done++;
errors++;
});
});
},
'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('/dolibarr/custom/subtotaltitle/ajax/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');
});
}