73 lines
2.8 KiB
PHP
Executable file
73 lines
2.8 KiB
PHP
Executable file
<?php
|
|
/**
|
|
* Repariert fehlende Subtotal-Zeilen für existierende Sections
|
|
*/
|
|
|
|
define('NOTOKENRENEWAL', 1);
|
|
require '../../../main.inc.php';
|
|
require_once __DIR__.'/../lib/subtotaltitle.lib.php';
|
|
require_once __DIR__.'/../class/DocumentTypeHelper.class.php';
|
|
|
|
$facture_id = GETPOST('facture_id', 'int');
|
|
$docType = GETPOST('document_type', 'alpha');
|
|
|
|
if (!$facture_id || !$docType) {
|
|
echo json_encode(['success' => false, 'error' => 'Missing parameters']);
|
|
exit;
|
|
}
|
|
|
|
$tables = DocumentTypeHelper::getTableNames($docType);
|
|
if (!$tables) {
|
|
echo json_encode(['success' => false, 'error' => 'Invalid document type']);
|
|
exit;
|
|
}
|
|
|
|
$repaired = 0;
|
|
|
|
// Finde alle Sections, die keine Subtotal-Zeile haben
|
|
$sql = "SELECT s.rowid, s.title";
|
|
$sql .= " FROM ".MAIN_DB_PREFIX."facture_lines_manager s";
|
|
$sql .= " WHERE s.".$tables['fk_parent']." = ".(int)$facture_id;
|
|
$sql .= " AND s.document_type = '".$db->escape($docType)."'";
|
|
$sql .= " AND s.line_type = 'section'";
|
|
$sql .= " AND NOT EXISTS (";
|
|
$sql .= " SELECT 1 FROM ".MAIN_DB_PREFIX."facture_lines_manager sub";
|
|
$sql .= " WHERE sub.parent_section = s.rowid";
|
|
$sql .= " AND sub.line_type = 'subtotal'";
|
|
$sql .= " AND sub.document_type = '".$db->escape($docType)."'";
|
|
$sql .= " )";
|
|
|
|
$resql = $db->query($sql);
|
|
|
|
while ($section = $db->fetch_object($resql)) {
|
|
subtotaltitle_debug_log('🔧 Repariere Section #'.$section->rowid.' ('.$section->title.') - erstelle Subtotal');
|
|
|
|
// Hole nächste line_order
|
|
$sql_max = "SELECT MAX(line_order) as max_order FROM ".MAIN_DB_PREFIX."facture_lines_manager";
|
|
$sql_max .= " WHERE ".$tables['fk_parent']." = ".(int)$facture_id;
|
|
$sql_max .= " AND document_type = '".$db->escape($docType)."'";
|
|
$res_max = $db->query($sql_max);
|
|
$obj_max = $db->fetch_object($res_max);
|
|
$next_order = ($obj_max && $obj_max->max_order) ? $obj_max->max_order + 1 : 9999;
|
|
|
|
$fk_facture = ($docType === 'invoice') ? (int)$facture_id : 'NULL';
|
|
$fk_propal = ($docType === 'propal') ? (int)$facture_id : 'NULL';
|
|
$fk_commande = ($docType === 'order') ? (int)$facture_id : 'NULL';
|
|
|
|
$sql_ins = "INSERT INTO ".MAIN_DB_PREFIX."facture_lines_manager";
|
|
$sql_ins .= " (fk_facture, fk_propal, fk_commande, document_type, line_type, title, parent_section, line_order, date_creation)";
|
|
$sql_ins .= " VALUES (".$fk_facture.", ".$fk_propal.", ".$fk_commande.", '".$db->escape($docType)."', 'subtotal', 'Zwischensumme', ".(int)$section->rowid.", ".$next_order.", NOW())";
|
|
|
|
if ($db->query($sql_ins)) {
|
|
$repaired++;
|
|
subtotaltitle_debug_log('✅ Subtotal-Zeile erstellt für Section #'.$section->rowid);
|
|
} else {
|
|
subtotaltitle_debug_log('❌ Fehler beim Erstellen der Subtotal-Zeile für Section #'.$section->rowid.': '.$db->lasterror());
|
|
}
|
|
}
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'repaired' => $repaired,
|
|
'message' => $repaired.' Subtotal-Zeilen erstellt'
|
|
]);
|