subtotaltitle/ajax/check_subtotal.php

48 lines
1.3 KiB
PHP
Executable file

<?php
/**
* Prüft ob ein Subtotal für eine Section existiert
*/
define('NOTOKENRENEWAL', 1);
require '../../../main.inc.php';
require_once __DIR__.'/../class/DocumentTypeHelper.class.php';
$section_id = GETPOST('section_id', 'int');
$docType = GETPOST('document_type', 'alpha');
if (!$section_id || !$docType) {
echo json_encode(['exists' => false, 'error' => 'Missing parameters']);
exit;
}
$tables = DocumentTypeHelper::getTableNames($docType);
if (!$tables) {
echo json_encode(['exists' => false, 'error' => 'Invalid document type']);
exit;
}
// Prüfe ob Subtotal in Manager-Tabelle existiert
$sql = "SELECT rowid, ".$tables['fk_line']." as detail_id, in_facturedet FROM ".MAIN_DB_PREFIX."facture_lines_manager";
$sql .= " WHERE parent_section = ".(int)$section_id;
$sql .= " AND line_type = 'subtotal'";
$sql .= " AND document_type = '".$db->escape($docType)."'";
$resql = $db->query($sql);
$exists = false;
$subtotal_id = null;
$detail_id = null;
$in_facturedet = false;
if ($resql && $db->num_rows($resql) > 0) {
$obj = $db->fetch_object($resql);
$exists = true;
$subtotal_id = $obj->rowid;
$detail_id = $obj->detail_id;
$in_facturedet = $obj->in_facturedet ? true : false;
}
echo json_encode([
'exists' => $exists,
'subtotal_id' => $subtotal_id,
'detail_id' => $detail_id,
'in_facturedet' => $in_facturedet
]);