80 lines
2.6 KiB
PHP
80 lines
2.6 KiB
PHP
<?php
|
||
|
||
/**
|
||
* Nextcloud-Belegimport – CLI-Einstiegspunkt.
|
||
*
|
||
* Liest Belege aus dem in den Einstellungen konfigurierten Nextcloud-Ordner
|
||
* (WebDAV) und legt sie in Steuerkram an. Danach werden die Dateien in
|
||
* Nextcloud in den Erledigte-Ordner verschoben (oder gelöscht, wenn dieser
|
||
* nicht konfiguriert ist).
|
||
*
|
||
* Konfiguration: Einstellungen → Nextcloud-Belegimport in der Steuerkram-UI.
|
||
*
|
||
* Aufruf:
|
||
* docker compose exec web php scripts/nextcloud-import.php
|
||
*
|
||
* Für Cronjob (auf dem Host):
|
||
* *\/15 * * * * docker compose -f /opt/steuerkram/docker-compose.yml exec -T web \
|
||
* php scripts/nextcloud-import.php >> /var/log/steuerkram-import.log 2>&1
|
||
*/
|
||
|
||
declare(strict_types=1);
|
||
|
||
chdir(dirname(__DIR__));
|
||
|
||
// DB-Zugangsdaten aus .env laden (für CLI-Kontext ohne Apache-Umgebung)
|
||
if (file_exists('.env')) {
|
||
foreach (file('.env', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
|
||
if (str_starts_with(trim($line), '#') || !str_contains($line, '=')) {
|
||
continue;
|
||
}
|
||
[$key, $value] = explode('=', $line, 2);
|
||
$key = trim($key);
|
||
$value = trim($value, " \t\n\r\0\x0B\"'");
|
||
if (!array_key_exists($key, $_ENV)) {
|
||
$_ENV[$key] = $value;
|
||
putenv("{$key}={$value}");
|
||
}
|
||
}
|
||
}
|
||
|
||
require_once 'vendor/autoload.php';
|
||
|
||
use Steuerkram\Config;
|
||
use Steuerkram\Models\Setting;
|
||
use Steuerkram\Services\NextcloudImportService;
|
||
|
||
Config::load(); // Database verbindet sich lazy über Config::get()
|
||
|
||
$start = microtime(true);
|
||
echo '[nextcloud-import] Start: ' . date('Y-m-d H:i:s') . PHP_EOL;
|
||
|
||
$svc = new NextcloudImportService();
|
||
$result = $svc->import();
|
||
|
||
foreach ($result['details'] as $line) {
|
||
echo '[nextcloud-import] ' . $line . PHP_EOL;
|
||
}
|
||
|
||
$elapsed = round(microtime(true) - $start, 2);
|
||
echo sprintf(
|
||
'[nextcloud-import] Fertig in %ss – importiert: %d, übersprungen: %d, Fehler: %d' . PHP_EOL,
|
||
$elapsed,
|
||
$result['imported'],
|
||
$result['skipped'],
|
||
$result['errors'],
|
||
);
|
||
|
||
// KI-Vorschlag für jeden neu importierten Beleg anstoßen (wenn KI-Erkennung aktiv)
|
||
if (!empty($result['imported_ids'])) {
|
||
$settings = Setting::get();
|
||
if (!empty($settings['ki_vorschlag_aktiv'])) {
|
||
$kiBatch = __DIR__ . '/ki-batch.php';
|
||
echo '[nextcloud-import] Starte KI-Erkennung für ' . count($result['imported_ids']) . ' importierte(n) Beleg(e) ...' . PHP_EOL;
|
||
foreach ($result['imported_ids'] as $belegId) {
|
||
passthru('php ' . escapeshellarg($kiBatch) . ' ' . (int)$belegId . ' 2>&1');
|
||
}
|
||
}
|
||
}
|
||
|
||
exit($result['errors'] > 0 ? 1 : 0);
|