subtotaltitle/ajax/delete_textline.php

59 lines
No EOL
1.8 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';
header('Content-Type: application/json');
$textline_id = GETPOST('textline_id', 'int');
subtotaltitle_debug_log('🗑️ delete_textline: id=' . $textline_id);
if (!$textline_id) {
echo json_encode(array('success' => false, 'error' => 'Missing parameters'));
exit;
}
// 1. Hole facture_id BEVOR wir löschen
$sql = "SELECT fk_facture 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);
$facture_id = $obj->fk_facture;
// 2. DELETE ausführen
$sql = "DELETE FROM ".MAIN_DB_PREFIX."facture_lines_manager";
$sql .= " WHERE rowid = ".(int)$textline_id;
$sql .= " AND line_type = 'text'";
if (!$db->query($sql)) {
echo json_encode(array('success' => false, 'error' => $db->lasterror()));
exit;
}
// 3. Lücken schließen
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."facture_lines_manager
WHERE fk_facture = ".(int)$facture_id."
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++;
}
echo json_encode(array('success' => true));