51 lines
2.1 KiB
PHP
Executable file
51 lines
2.1 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__.'/../class/DocumentTypeHelper.class.php';
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
$facture_id = GETPOST('facture_id', 'int');
|
|
$text = GETPOST('text', 'restricthtml');
|
|
$docType = GETPOST('document_type', 'alpha');
|
|
|
|
if (!$facture_id || !$text || !$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 nächste line_order
|
|
$sql = "SELECT MAX(line_order) as max_order 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->max_order ? $obj->max_order + 1 : 1);
|
|
|
|
// Füge Textzeile ein - 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)."', 'text', '".$db->escape($text)."', ".$next_order.", NOW())";
|
|
|
|
if ($db->query($sql)) {
|
|
$new_id = $db->last_insert_id(MAIN_DB_PREFIX."facture_lines_manager");
|
|
echo json_encode(array('success' => true, 'id' => $new_id));
|
|
} else {
|
|
echo json_encode(array('success' => false, 'error' => $db->lasterror()));
|
|
}
|