53 lines
2.4 KiB
PHP
Executable file
53 lines
2.4 KiB
PHP
Executable file
<?php
|
|
define('NOTOKENRENEWAL', 1);
|
|
require '../../../main.inc.php';
|
|
require_once __DIR__.'/../class/DocumentTypeHelper.class.php';
|
|
|
|
$facture_id = GETPOST('facture_id', 'int');
|
|
$title = GETPOST('title', 'alpha');
|
|
$docType = GETPOST('document_type', 'alpha'); // NEU!
|
|
|
|
if (!$facture_id || !$title || !$docType) {
|
|
echo json_encode(['success' => false, 'error' => 'Missing parameters']);
|
|
exit;
|
|
}
|
|
|
|
// Hole die richtigen Tabellennamen für diesen Dokumenttyp
|
|
$tables = DocumentTypeHelper::getTableNames($docType);
|
|
if (!$tables) {
|
|
echo json_encode(['success' => false, 'error' => 'Invalid document type']);
|
|
exit;
|
|
}
|
|
|
|
// Hole nächste line_order
|
|
$sql = "SELECT MAX(line_order) as max_order";
|
|
$sql .= " FROM ".MAIN_DB_PREFIX."facture_lines_manager";
|
|
$sql .= " WHERE ".$tables['fk_parent']." = ".(int)$facture_id;
|
|
$sql .= " AND document_type = '".$db->escape($docType)."'";
|
|
$resql = $db->query($sql);
|
|
$obj = $db->fetch_object($resql);
|
|
$next_order = ($obj && $obj->max_order ? $obj->max_order + 1 : 1);
|
|
|
|
// Erstelle Section - setze alle FK-Felder explizit (NULL für nicht genutzte)
|
|
$fk_facture = ($docType === 'invoice') ? (int)$facture_id : 'NULL';
|
|
$fk_propal = ($docType === 'propal') ? (int)$facture_id : 'NULL';
|
|
$fk_commande = ($docType === 'order') ? (int)$facture_id : 'NULL';
|
|
|
|
$sql = "INSERT INTO ".MAIN_DB_PREFIX."facture_lines_manager";
|
|
$sql .= " (fk_facture, fk_propal, fk_commande, document_type, line_type, title, line_order, date_creation)";
|
|
$sql .= " VALUES (".$fk_facture.", ".$fk_propal.", ".$fk_commande.", '".$db->escape($docType)."', 'section', '".$db->escape($title)."', ".$next_order.", NOW())";
|
|
|
|
if ($db->query($sql)) {
|
|
$section_id = $db->last_insert_id(MAIN_DB_PREFIX."facture_lines_manager");
|
|
|
|
// Erstelle automatisch auch eine Zwischensumme für diese Section
|
|
$subtotal_order = $next_order + 1000; // Hohe Nummer, wird später normalisiert
|
|
$sql_subtotal = "INSERT INTO ".MAIN_DB_PREFIX."facture_lines_manager";
|
|
$sql_subtotal .= " (fk_facture, fk_propal, fk_commande, document_type, line_type, title, parent_section, line_order, date_creation)";
|
|
$sql_subtotal .= " VALUES (".$fk_facture.", ".$fk_propal.", ".$fk_commande.", '".$db->escape($docType)."', 'subtotal', 'Zwischensumme', ".(int)$section_id.", ".$subtotal_order.", NOW())";
|
|
$db->query($sql_subtotal);
|
|
|
|
echo json_encode(['success' => true, 'section_id' => $section_id]);
|
|
} else {
|
|
echo json_encode(['success' => false, 'error' => $db->lasterror()]);
|
|
}
|