subtotaltitle/ajax/delete_textline.php

104 lines
3.4 KiB
PHP
Executable file

<?php
define('NOTOKENRENEWAL', 1);
$res = 0;
if (!$res && file_exists("../../../main.inc.php")) $res = @include "../../../main.inc.php";
if (!$res && file_exists("../../../../main.inc.php")) $res = @include "../../../../main.inc.php";
if (!$res) die("Include of main fails");
require_once __DIR__.'/../lib/subtotaltitle.lib.php';
require_once __DIR__.'/../class/DocumentTypeHelper.class.php';
header('Content-Type: application/json');
$textline_id = GETPOST('textline_id', 'int');
$docType = GETPOST('document_type', 'alpha');
// Fallback: Wenn kein docType, versuche aus der DB zu ermitteln
if (!$docType) {
$sql_type = "SELECT document_type FROM ".MAIN_DB_PREFIX."facture_lines_manager WHERE rowid = ".(int)$textline_id;
$res_type = $db->query($sql_type);
if ($res_type && $obj_type = $db->fetch_object($res_type)) {
$docType = $obj_type->document_type;
}
}
if (!$docType) {
$docType = 'invoice'; // Fallback
}
subtotaltitle_debug_log('delete_textline: id=' . $textline_id . ', docType=' . $docType);
if (!$textline_id) {
echo json_encode(array('success' => false, 'error' => 'Missing parameters'));
exit;
}
// Hole die richtigen Tabellennamen fuer diesen Dokumenttyp
$tables = DocumentTypeHelper::getTableNames($docType);
if (!$tables) {
echo json_encode(array('success' => false, 'error' => 'Invalid document type'));
exit;
}
// 1. Hole document_id BEVOR wir loeschen
$sql = "SELECT ".$tables['fk_parent']." as doc_id FROM ".MAIN_DB_PREFIX."facture_lines_manager";
$sql .= " WHERE rowid = ".(int)$textline_id;
$resql = $db->query($sql);
if (!$resql || $db->num_rows($resql) == 0) {
echo json_encode(array('success' => false, 'error' => 'Textline not found'));
exit;
}
$obj = $db->fetch_object($resql);
$document_id = $obj->doc_id;
$db->begin();
// 2. DELETE ausfuehren
$sql = "DELETE FROM ".MAIN_DB_PREFIX."facture_lines_manager";
$sql .= " WHERE rowid = ".(int)$textline_id;
$sql .= " AND line_type = 'text'";
if (!$db->query($sql)) {
$db->rollback();
echo json_encode(array('success' => false, 'error' => $db->lasterror()));
exit;
}
// 3. line_order neu durchnummerieren
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."facture_lines_manager";
$sql .= " WHERE ".$tables['fk_parent']." = ".(int)$document_id;
$sql .= " AND document_type = '".$db->escape($docType)."'";
$sql .= " ORDER BY line_order";
$resql = $db->query($sql);
$new_order = 1;
while ($obj = $db->fetch_object($resql)) {
$sql_upd = "UPDATE ".MAIN_DB_PREFIX."facture_lines_manager SET line_order = ".$new_order." WHERE rowid = ".(int)$obj->rowid;
$db->query($sql_upd);
$new_order++;
}
// 4. rang in Detail-Tabelle synchronisieren
$sql = "SELECT ".$tables['fk_line']." as detail_id FROM ".MAIN_DB_PREFIX."facture_lines_manager";
$sql .= " WHERE ".$tables['fk_parent']." = ".(int)$document_id;
$sql .= " AND document_type = '".$db->escape($docType)."'";
$sql .= " AND ".$tables['fk_line']." IS NOT NULL";
$sql .= " ORDER BY line_order";
$resql = $db->query($sql);
$rang = 1;
while ($obj = $db->fetch_object($resql)) {
if ($obj->detail_id) {
$sql_upd = "UPDATE ".MAIN_DB_PREFIX.$tables['lines_table']." SET rang = ".$rang." WHERE rowid = ".(int)$obj->detail_id;
$db->query($sql_upd);
$rang++;
}
}
subtotaltitle_debug_log('delete_textline: rang synchronisiert, ' . ($rang - 1) . ' Zeilen');
$db->commit();
echo json_encode(array('success' => true));