subtotaltitle/ajax/fix_sections.php

58 lines
1.6 KiB
PHP

<?php
/**
* Repariert fehlerhafte parent_section Werte
* - Sections sollten KEINE parent_section haben
* - parent_section = 0 sollte NULL sein
*/
define('NOTOKENRENEWAL', 1);
require '../../../main.inc.php';
$doc_id = GETPOST('doc_id', 'int');
$docType = GETPOST('document_type', 'alpha');
if (!$doc_id || !$docType) {
echo json_encode(['success' => false, 'error' => 'Missing parameters']);
exit;
}
$db->begin();
$fixed = 0;
// 1. Sections dürfen KEINE parent_section haben
$sql = "UPDATE ".MAIN_DB_PREFIX."facture_lines_manager";
$sql .= " SET parent_section = NULL";
$sql .= " WHERE line_type = 'section'";
$sql .= " AND parent_section IS NOT NULL";
$sql .= " AND document_type = '".$db->escape($docType)."'";
if ($docType == 'invoice') {
$sql .= " AND fk_facture = ".(int)$doc_id;
} elseif ($docType == 'propal') {
$sql .= " AND fk_propal = ".(int)$doc_id;
} elseif ($docType == 'order') {
$sql .= " AND fk_commande = ".(int)$doc_id;
}
$db->query($sql);
$fixed += $db->affected_rows();
// 2. parent_section = 0 sollte NULL sein
$sql = "UPDATE ".MAIN_DB_PREFIX."facture_lines_manager";
$sql .= " SET parent_section = NULL";
$sql .= " WHERE parent_section = 0";
$sql .= " AND document_type = '".$db->escape($docType)."'";
if ($docType == 'invoice') {
$sql .= " AND fk_facture = ".(int)$doc_id;
} elseif ($docType == 'propal') {
$sql .= " AND fk_propal = ".(int)$doc_id;
} elseif ($docType == 'order') {
$sql .= " AND fk_commande = ".(int)$doc_id;
}
$db->query($sql);
$fixed += $db->affected_rows();
$db->commit();
echo json_encode([
'success' => true,
'fixed' => $fixed,
'message' => $fixed . ' Einträge korrigiert'
]);