subtotaltitle/ajax/move_product.php
2026-01-26 19:48:26 +01:00

69 lines
2.2 KiB
PHP
Executable file

<?php
define('NOTOKENRENEWAL', 1);
require '../../../main.inc.php';
require_once __DIR__.'/../lib/subtotaltitle.lib.php';
$product_id = GETPOST('product_id', 'int');
$new_section_id = GETPOST('new_section_id', 'int');
$new_line_order = GETPOST('new_line_order', 'double'); // ← NEU! Kann Dezimalzahl sein (z.B. 3.5)
subtotaltitle_debug_log('🔍 move_product: product='.$product_id.', section='.$new_section_id.', line_order='.$new_line_order);
if (!$product_id) {
echo json_encode(array('success' => false, 'error' => 'Missing product_id'));
exit;
}
$db->begin();
// Hole Facture ID
$sql = "SELECT m.fk_facture, m.rowid as manager_id";
$sql .= " FROM ".MAIN_DB_PREFIX."facture_lines_manager m";
$sql .= " WHERE m.fk_facturedet = ".(int)$product_id;
$resql = $db->query($sql);
if (!$resql || $db->num_rows($resql) == 0) {
echo json_encode(array('success' => false, 'error' => 'Product not found'));
exit;
}
$obj = $db->fetch_object($resql);
$facture_id = $obj->fk_facture;
$manager_id = $obj->rowid;
// Update parent_section UND line_order
if ($new_section_id > 0) {
$sql = "UPDATE ".MAIN_DB_PREFIX."facture_lines_manager";
$sql .= " SET parent_section = ".(int)$new_section_id;
if ($new_line_order !== null) {
$sql .= ", line_order = ".(float)$new_line_order; // ← NEU!
}
$sql .= " WHERE rowid = ".(int)$manager_id;
} else {
$sql = "UPDATE ".MAIN_DB_PREFIX."facture_lines_manager";
$sql .= " SET parent_section = NULL";
if ($new_line_order !== null) {
$sql .= ", line_order = ".(float)$new_line_order; // ← NEU!
}
$sql .= " WHERE rowid = ".(int)$manager_id;
}
$db->query($sql);
subtotaltitle_debug_log('✅ parent_section und line_order gesetzt');
// Neuordnen (bereinigt Dezimalzahlen und Lücken)
require_once DOL_DOCUMENT_ROOT.'/custom/subtotaltitle/class/actions_subtotaltitle.class.php';
$hook = new ActionsSubtotalTitle($db);
$reflection = new ReflectionClass($hook);
$method = $reflection->getMethod('reorderLines');
$method->setAccessible(true);
$method->invoke($hook, $facture_id);
$method = $reflection->getMethod('syncRangFromManager');
$method->setAccessible(true);
$method->invoke($hook, $facture_id);
$db->commit();
echo json_encode(array('success' => true));