subtotaltitle/ajax/reorder_all.php

117 lines
No EOL
4.4 KiB
PHP
Executable file

<?php
define('NOTOKENRENEWAL', 1);
require '../../../main.inc.php';
require_once __DIR__.'/../lib/subtotaltitle.lib.php';
$facture_id = GETPOST('facture_id', 'int');
$new_order_json = GETPOST('new_order', 'none');
subtotaltitle_debug_log('🔄 reorder_all: facture=' . $facture_id);
if (!$facture_id || !$new_order_json) {
echo json_encode(['success' => false, 'error' => 'Missing parameters']);
exit;
}
$new_order = json_decode($new_order_json, true);
if (!$new_order) {
echo json_encode(['success' => false, 'error' => 'Invalid JSON']);
exit;
}
$db->begin();
// Für jede Zeile: line_order und parent_section updaten
foreach ($new_order as $item) {
if ($item['type'] == 'section') {
$sql = "UPDATE ".MAIN_DB_PREFIX."facture_lines_manager";
$sql .= " SET line_order = ".(int)$item['order'];
$sql .= " WHERE rowid = ".(int)$item['id'];
$db->query($sql);
subtotaltitle_debug_log(' Section #'.$item['id'].' → order='.$item['order']);
} else if ($item['type'] == 'product') {
$sql = "UPDATE ".MAIN_DB_PREFIX."facture_lines_manager";
$sql .= " SET line_order = ".(int)$item['order'];
$sql .= ", parent_section = ".($item['parent_section'] ? (int)$item['parent_section'] : "NULL");
$sql .= " WHERE fk_facturedet = ".(int)$item['id'];
$db->query($sql);
subtotaltitle_debug_log(' Produkt #'.$item['id'].' → order='.$item['order'].', section='.($item['parent_section'] ?: 'FREI'));
} else if ($item['type'] == 'text') {
$sql = "UPDATE ".MAIN_DB_PREFIX."facture_lines_manager";
$sql .= " SET line_order = ".(int)$item['order'];
$sql .= ", parent_section = ".($item['parent_section'] ? (int)$item['parent_section'] : "NULL");
$sql .= " WHERE rowid = ".(int)$item['id'];
$sql .= " AND line_type = 'text'";
$db->query($sql);
subtotaltitle_debug_log(' Text #'.$item['id'].' → order='.$item['order'].', section='.($item['parent_section'] ?: 'FREI'));
}
}
// ========== SUBTOTALS NEU POSITIONIEREN ==========
subtotaltitle_debug_log('🔢 Repositioniere Subtotals...');
$sql = "SELECT rowid, parent_section FROM ".MAIN_DB_PREFIX."facture_lines_manager
WHERE fk_facture = ".(int)$facture_id."
AND line_type = 'subtotal'";
$resql = $db->query($sql);
while ($subtotal = $db->fetch_object($resql)) {
// Finde höchste line_order der Produkte dieser Section
$sql_max = "SELECT MAX(line_order) as max_order
FROM ".MAIN_DB_PREFIX."facture_lines_manager
WHERE parent_section = ".(int)$subtotal->parent_section."
AND line_type = 'product'";
$res_max = $db->query($sql_max);
$obj_max = $db->fetch_object($res_max);
if ($obj_max && $obj_max->max_order) {
// Subtotal bekommt hohe Nummer (wird gleich normalisiert)
$temp_order = (int)$obj_max->max_order * 100 + 50;
$sql_upd = "UPDATE ".MAIN_DB_PREFIX."facture_lines_manager
SET line_order = ".$temp_order."
WHERE rowid = ".(int)$subtotal->rowid;
$db->query($sql_upd);
subtotaltitle_debug_log(' Subtotal #'.$subtotal->rowid.' → temp_order='.$temp_order.' (nach Section '.$subtotal->parent_section.')');
}
}
// ========== ALLES NEU DURCHNUMMERIEREN ==========
subtotaltitle_debug_log('🔢 Normalisiere line_order...');
$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_num = 1;
while ($obj = $db->fetch_object($resql)) {
$sql_upd = "UPDATE ".MAIN_DB_PREFIX."facture_lines_manager
SET line_order = ".$new_order_num."
WHERE rowid = ".(int)$obj->rowid;
$db->query($sql_upd);
$new_order_num++;
}
// ========== SYNC FACTUREDET.RANG ==========
$sql = "SELECT fk_facturedet FROM ".MAIN_DB_PREFIX."facture_lines_manager";
$sql .= " WHERE fk_facture = ".(int)$facture_id;
$sql .= " AND line_type = 'product'";
$sql .= " ORDER BY line_order";
$resql = $db->query($sql);
$rang = 1;
while ($obj = $db->fetch_object($resql)) {
$sql_upd = "UPDATE ".MAIN_DB_PREFIX."facturedet";
$sql_upd .= " SET rang = ".$rang;
$sql_upd .= " WHERE rowid = ".(int)$obj->fk_facturedet;
$db->query($sql_upd);
$rang++;
}
$db->commit();
echo json_encode(['success' => true, 'updated' => count($new_order)]);