id); $items[] = array( 'id' => (int) $t->id, 'label' => $t->template_label ?: $t->titel ?: $t->ref, 'titel' => $t->titel, 'page_format' => $t->page_format, 'page_orientation' => $t->page_orientation, 'template_odt' => $t->template_odt, 'page_count' => count($pages), ); } api_ok(array('templates' => $items, 'count' => count($items))); } if ($method === 'POST' && $action === 'create_from_template') { if (!$user->hasRight('bericht', 'write')) api_fail('Permission denied', 403); $in = api_input(); $tpl_id = (int) ($in['template_id'] ?? 0); $el_type = (string) ($in['element_type'] ?? 'order'); $el_id = (int) ($in['element_id'] ?? 0); $auftrag = (string) ($in['auftragsnummer'] ?? ''); if (!$tpl_id || !$el_id) api_fail('template_id + element_id erforderlich'); if (!in_array($el_type, array('order', 'invoice', 'propal'), true)) api_fail('element_type ungültig'); // Auftragsnummer auto-füllen wenn leer if (empty($auftrag)) { $parent = bericht_fetch_parent($db, $el_type, $el_id); if ($parent) { if ($el_type === 'invoice') { $auftrag = $parent->array_options['options_auftragsnummer'] ?? $parent->ref_client ?? $parent->ref; } else { $auftrag = $parent->ref; } } } $new_id = Bericht::createFromTemplate($db, $user, $tpl_id, $el_type, $el_id, $auftrag); if (!$new_id) api_fail('Erstellung fehlgeschlagen', 500); api_ok(array('bericht_id' => $new_id)); } api_fail('Methode/Action nicht unterstützt', 405);