subtotaltitle/ajax/get_sections.php

43 lines
1.6 KiB
PHP
Executable file

<?php
define('NOTOKENRENEWAL', 1);
require '../../../main.inc.php';
require_once __DIR__.'/../lib/subtotaltitle.lib.php';
require_once __DIR__.'/../class/DocumentTypeHelper.class.php';
$facture_id = GETPOST('facture_id', 'int');
$docType = GETPOST('document_type', 'alpha');
subtotaltitle_debug_log('🔄 get_sections: facture=' . $facture_id . ', docType=' . $docType);
if (!$facture_id || !$docType) {
echo json_encode(array('success' => false, 'error' => 'Missing parameters'));
exit;
}
// Hole die richtigen Tabellennamen für diesen Dokumenttyp
$tables = DocumentTypeHelper::getTableNames($docType);
if (!$tables) {
echo json_encode(array('success' => false, 'error' => 'Invalid document type'));
exit;
}
// Hole ALLE Sections für diesen Dokumenttyp
$sql = "SELECT s.rowid, s.title, s.line_order, ";
$sql .= " (SELECT COUNT(*) FROM ".MAIN_DB_PREFIX."facture_lines_manager p WHERE p.parent_section = s.rowid AND p.line_type = 'product' AND p.document_type = '".$db->escape($docType)."') as product_count";
$sql .= " FROM ".MAIN_DB_PREFIX."facture_lines_manager s";
$sql .= " WHERE s.".$tables['fk_parent']." = ".(int)$facture_id;
$sql .= " AND s.document_type = '".$db->escape($docType)."'";
$sql .= " AND s.line_type = 'section'";
$sql .= " ORDER BY s.line_order";
$resql = $db->query($sql);
$sections = array();
while ($obj = $db->fetch_object($resql)) {
$sections[] = array(
'id' => $obj->rowid,
'title' => $obj->title,
'line_order' => $obj->line_order,
'is_empty' => ($obj->product_count == 0)
);
}
echo json_encode(array('success' => true, 'sections' => $sections));