bericht/api/templates.php
Eduard Wisch 344f884a0f
All checks were successful
Deploy bericht / deploy (push) Successful in 1s
feat: Bericht-Vorlagen + Whisper-Transkription + Cron-Fix
Bericht-Vorlagen (Phase 5.5):
- DB: is_template, template_label in llx_bericht
- Bericht::fetchAllTemplates() und createFromTemplate()
- fetchAllForElement() blendet Vorlagen aus
- ajax/save_as_template.php erzeugt Vorlage aus aktuellem Bericht
- Desktop-Editor: '📋 Als Vorlage' Button im Action-Bereich
- Bericht-Übersicht: Vorlagen-Dropdown beim + Neu Button
- api/templates.php: GET list + POST create_from_template

Schnell-Bericht (Phase 4.a/4.i):
- api/reports.php?action=create POST-Endpoint: Titel, Format,
  Orientation, ODT-Template, optional template_id
- api/odt_templates.php: Liste der Deckblatt-Vorlagen

Whisper-Transkription (Phase 5.7):
- api/transcribe.php: POST mit relpath, nutzt externen Whisper-
  HTTP-Endpoint (whisper.cpp server ODER OpenAI-kompatibel)
- Konfiguration im Admin: BERICHT_WHISPER_URL/MODE/API_KEY/LANG
- Sprache default 'de'

Cron-Fix:
- BerichtUploadToken::cleanupExpired() ist jetzt Instanz-Methode
  (Dolibarr ruft new Klasse($db) bei jobtype=method auf)
- Returnwert für Cron-Success/Failure
- Statische Variante als cleanupExpiredStatic() für direkte Aufrufe
- Damit läuft der tägliche Cron 'Expired Upload-Tokens bereinigen'
  nicht mehr hängend

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
[deploy]
2026-04-09 08:27:45 +02:00

61 lines
2.2 KiB
PHP

<?php
/* GET /api/templates.php — Liste aller Bericht-Vorlagen
* POST /api/templates.php?action=create_from_template
* Body: { template_id, element_type, element_id, auftragsnummer }
* → legt einen neuen Bericht aus einer Vorlage an, gibt bericht_id zurück
*/
require_once __DIR__.'/_inc.php';
api_authenticate();
global $db, $user;
$method = $_SERVER['REQUEST_METHOD'];
$action = $_GET['action'] ?? '';
if ($method === 'GET' && !$action) {
$items = array();
foreach (Bericht::fetchAllTemplates($db) as $t) {
$pages = BerichtPage::fetchAllForBericht($db, $t->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);