mahnung/core/modules/modMahnung.class.php
Eduard Wisch 56954d68f3
Some checks failed
Deploy mahnung / deploy (push) Failing after 4s
Konfigurierbare Tracking-Patterns mit Live-Vorschau [deploy]
Datenmodell:
- Neue Tabelle llx_mahnung_trackingpattern (provider, label, regex,
  url_template, priority, active). Auto-Anlage + Default-Seed im setup.php
  und in modMahnung::init() — idempotent.

Default-Patterns (priority hoeher = spezifischer, zuerst gepruef):
- DHL Paket 20-stellig (90), DPAG Einschreiben RR...DE (85), UPS 1Z... (80),
- DHL 11-stellig Online-Frankierung (30), Hermes 14-stellig (25), DPD (20).

Setup-Seite admin/tracking_patterns.php:
- CRUD (Anlegen/Bearbeiten/Aktivieren-Deaktivieren/Loeschen)
- Live-Vorschau: Regex + Beispieltext + URL-Template werden waehrend
  des Tippens (debounce 300ms) via ajax/regex_preview.php ausgewertet.
  UI zeigt: Regex-Syntax-Status, gefundene Sendungsnummer, vollstaendige
  Tracking-URL (anklickbar).
- Validierung: Delimiter / # ~ Whitelist, https://-Pflicht, {nr}-Platzhalter,
  max 255 Zeichen Regex.

AJAX-Endpoint ajax/regex_preview.php:
- ReDoS-Schutz: max 10 KB Sample, pcre.backtrack_limit=100k.
- POST-only (mit Setup-Recht), JSON-Response.

card.php:
- tracking-URL kommt jetzt aus MahnungTrackingPattern::urlFor() (DB-Lookup
  nach Provider) statt hardcoded Mahnung::trackingUrl() — letztere bleibt
  als Fallback.

Setup-Seite: neuer Button "Tracking-Muster (Regex)" oben rechts.

Lang-Keys: 23 neue (de_DE + en_US) fuer Pattern-CRUD + Live-Vorschau.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-11 12:04:42 +02:00

383 lines
11 KiB
PHP

<?php
/* Copyright (C) 2026 Eduard Wisch <data@data-it-solution.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
/**
* \defgroup mahnung Modul Mahnwesen
* \brief Mahnwesen-Modul (3-stufig nach BGB §288)
* \file htdocs/custom/mahnung/core/modules/modMahnung.class.php
* \ingroup mahnung
*/
include_once DOL_DOCUMENT_ROOT.'/core/modules/DolibarrModules.class.php';
/**
* Beschreibungs- und Aktivierungsklasse fuer Modul Mahnung
*/
class modMahnung extends DolibarrModules
{
/**
* @param DoliDB $db Datenbank-Handler
*/
public function __construct($db)
{
global $conf, $langs;
$this->db = $db;
// Eindeutige Modul-ID. 500034..500036 sind durch das Bericht-Modul belegt
// (numero=500033, dessen rights id-Range 500033..500036 abdeckt).
// 500037 ist durch Eplan belegt, daher 500038.
$this->numero = 500038;
// Schluessel fuer Rechte und Menues
$this->rights_class = 'mahnung';
$this->family = 'financial';
$this->module_position = '50';
$this->name = preg_replace('/^mod/i', '', get_class($this));
$this->description = 'MahnungDescription';
$this->descriptionlong = 'MahnungDescription';
$this->editor_name = 'Alles Watt laeuft';
$this->editor_url = '';
$this->version = '0.2.0';
$this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
// FontAwesome 5 Free (Dolibarr-Bundle, KB #435). 'fa-envelope-open-o' ist FA4-Notation
// und rendert in Dolibarr lautlos kein Glyph; 'fa-envelope-open-text' ist FA5-Free.
$this->picto = 'fa-envelope-open-text';
$this->module_parts = array(
'triggers' => 1,
'login' => 0,
'substitutions' => 0,
'menus' => 0,
'tpl' => 0,
'barcode' => 0,
'models' => 1,
'printing' => 0,
'theme' => 0,
'css' => array(),
'js' => array(),
// Hook-Klasse: class/actions_mahnung.class.php (Standard-Lookup-Pfad)
'hooks' => array(
'data' => array(
'invoicecard',
'thirdpartycard',
),
'entity' => '0',
),
'moduleforexternal' => 0,
'websitetemplates' => 0,
'captcha' => 0,
);
// Datenverzeichnisse bei Modul-Aktivierung.
// $conf->mahnung->dir_output und multidir_output werden von Dolibarrs
// Conf-Klasse beim Bootstrap automatisch auf DOL_DATA_ROOT/mahnung gesetzt
// (siehe core/class/conf.class.php:744). Damit funktioniert FormFile->showdocuments('mahnung', ...)
// und document.php?modulepart=mahnung out-of-the-box.
$this->dirs = array('/mahnung', '/mahnung/temp');
// Konfigurationsseite
$this->config_page_url = array('setup.php@mahnung');
$this->hidden = getDolGlobalInt('MODULE_MAHNUNG_DISABLED');
$this->depends = array();
$this->requiredby = array();
$this->conflictwith = array();
$this->langfiles = array('mahnung@mahnung');
$this->phpmin = array(7, 4);
$this->need_dolibarr_version = array(19, -3);
$this->need_javascript_ajax = 1;
$this->warnings_activation = array();
$this->warnings_activation_ext = array();
// Modul-Konstanten
$this->const = array(
0 => array(
'MAHNUNG_BASISZINS',
'chaine',
'1.27',
'BGB-Basiszins in Prozent (manuell halbjaehrlich pflegen)',
0,
'allentities',
1,
),
1 => array(
'MAHNUNG_NTFY_TOPIC',
'chaine',
'vk-builds',
'Ntfy-Topic fuer Mahnungs-Benachrichtigungen',
0,
'current',
1,
),
2 => array(
'MAHNUNG_AUFSCHLAG_B2C',
'chaine',
'5.0',
'Verzugszins-Aufschlag B2C in Prozent (BGB §288 Abs. 1)',
0,
'allentities',
1,
),
3 => array(
'MAHNUNG_AUFSCHLAG_B2B',
'chaine',
'9.0',
'Verzugszins-Aufschlag B2B in Prozent (BGB §288 Abs. 2)',
0,
'allentities',
1,
),
4 => array(
'MAHNUNG_PAUSCHALE_B2B',
'chaine',
'40.00',
'Pauschale B2B nach BGB §288 Abs. 5 in EUR',
0,
'allentities',
1,
),
5 => array(
'MAHNUNG_ADDON_PDF',
'chaine',
'standard_mahnung',
'Standard-Dokumentenmodell fuer Mahnungen',
0,
'current',
1,
),
6 => array(
'MAHNUNG_ADDON_PDF_ODT_PATH',
'chaine',
'DOL_DATA_ROOT/doctemplates/mahnung',
'Verzeichnis fuer ODT-Templates',
0,
'current',
1,
),
);
if (!isModEnabled('mahnung')) {
$conf->mahnung = new stdClass();
$conf->mahnung->enabled = 0;
}
// Tabs auf bestehenden Karten (Phase 5: aktivieren)
$this->tabs = array();
$this->dictionaries = array();
$this->boxes = array(
0 => array(
'file' => 'box_mahnung_offen@mahnung',
'enabledbydefaulton' => 'Home',
),
);
// Cron-Job: Vorschlagsliste taeglich 06:00
$this->cronjobs = array(
0 => array(
'label' => 'MahnungCronBuildVorschlag',
'jobtype' => 'method',
'class' => '/mahnung/class/mahnungcron.class.php',
'objectname' => 'MahnungCron',
'method' => 'buildVorschlagsliste',
'parameters' => '',
'comment' => 'Sucht ueberfaellige Rechnungen, ermittelt vorgeschlagene Mahnstufen, sendet Ntfy-Push',
'frequency' => 1,
'unitfrequency' => 86400,
'status' => 1,
'test' => 'isModEnabled("mahnung")',
'priority' => 50,
),
);
// Berechtigungen
$this->rights = array();
$r = 0;
$this->rights[$r][0] = $this->numero.'01';
$this->rights[$r][1] = 'PermMahnungRead';
$this->rights[$r][2] = 'r';
$this->rights[$r][3] = 1;
$this->rights[$r][4] = 'read';
$r++;
$this->rights[$r][0] = $this->numero.'02';
$this->rights[$r][1] = 'PermMahnungWrite';
$this->rights[$r][2] = 'w';
$this->rights[$r][3] = 0;
$this->rights[$r][4] = 'write';
$r++;
$this->rights[$r][0] = $this->numero.'03';
$this->rights[$r][1] = 'PermMahnungSend';
$this->rights[$r][2] = 'w';
$this->rights[$r][3] = 0;
$this->rights[$r][4] = 'send';
$r++;
$this->rights[$r][0] = $this->numero.'04';
$this->rights[$r][1] = 'PermMahnungDelete';
$this->rights[$r][2] = 'd';
$this->rights[$r][3] = 0;
$this->rights[$r][4] = 'delete';
$r++;
$this->rights[$r][0] = $this->numero.'05';
$this->rights[$r][1] = 'PermMahnungSetup';
$this->rights[$r][2] = 'w';
$this->rights[$r][3] = 0;
$this->rights[$r][4] = 'setup';
$r++;
// Linkes Menue unter "Rechnungen" (mainmenu=billing)
$this->menu = array();
$r = 0;
$this->menu[$r++] = array(
'fk_menu' => 'fk_mainmenu=billing',
'type' => 'left',
'titre' => 'MahnungMenu',
'prefix' => img_picto('', 'fa-envelope-open-text', 'class="pictofixedwidth valignmiddle paddingright"'),
'mainmenu' => 'billing',
'leftmenu' => 'mahnung',
'url' => '/custom/mahnung/list.php?mainmenu=billing&leftmenu=mahnung',
'langs' => 'mahnung@mahnung',
'position' => 300,
'enabled' => 'isModEnabled("mahnung")',
'perms' => '$user->hasRight("mahnung", "read")',
'target' => '',
'user' => 2,
);
$this->menu[$r++] = array(
'fk_menu' => 'fk_mainmenu=billing,fk_leftmenu=mahnung',
'type' => 'left',
'titre' => 'MahnungVorschlagsliste',
'mainmenu' => 'billing',
'leftmenu' => 'mahnung_vorschlag',
'url' => '/custom/mahnung/list.php?mainmenu=billing&leftmenu=mahnung&mode=vorschlag',
'langs' => 'mahnung@mahnung',
'position' => 301,
'enabled' => 'isModEnabled("mahnung")',
'perms' => '$user->hasRight("mahnung", "read")',
'target' => '',
'user' => 2,
);
$this->menu[$r++] = array(
'fk_menu' => 'fk_mainmenu=billing,fk_leftmenu=mahnung',
'type' => 'left',
'titre' => 'MahnungArchiv',
'mainmenu' => 'billing',
'leftmenu' => 'mahnung_archiv',
'url' => '/custom/mahnung/list.php?mainmenu=billing&leftmenu=mahnung&mode=archiv',
'langs' => 'mahnung@mahnung',
'position' => 302,
'enabled' => 'isModEnabled("mahnung")',
'perms' => '$user->hasRight("mahnung", "read")',
'target' => '',
'user' => 2,
);
}
/**
* Aufruf bei Modul-Aktivierung: Tabellen anlegen, Konstanten/Rechte/Menues schreiben.
*
* @param string $options Optionen ('', 'noboxes')
* @return int<-1,1> 1 = OK, <=0 = Fehler
*/
public function init($options = '')
{
global $conf;
// Tabellen anlegen aus sql/-Verzeichnis
$result = $this->_load_tables('/mahnung/sql/');
if ($result < 0) {
return -1;
}
// Migration: Versand-Felder ergaenzen, falls Tabelle aus alter Version stammt
$this->migrateVersandFelder();
// Default-Tracking-Patterns seeden (idempotent — nur beim ersten Mal)
require_once DOL_DOCUMENT_ROOT.'/custom/mahnung/class/mahnungtrackingpattern.class.php';
MahnungTrackingPattern::seedDefaults($this->db);
// Dokumentenmodelle registrieren
$sql = array();
$sql[] = "DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = 'standard_mahnung' AND type = 'mahnung' AND entity = ".((int) $conf->entity);
$sql[] = "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity, libelle, description) VALUES ('standard_mahnung', 'mahnung', ".((int) $conf->entity).", 'Standard PDF (DIN 5008)', NULL)";
$sql[] = "DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = 'generic_mahnung_odt' AND type = 'mahnung' AND entity = ".((int) $conf->entity);
$sql[] = "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity, libelle, description) VALUES ('generic_mahnung_odt', 'mahnung', ".((int) $conf->entity).", 'ODT templates', 'MAHNUNG_ADDON_PDF_ODT_PATH')";
// ODT-Template-Verzeichnis anlegen
$doctemplatedir = DOL_DATA_ROOT.'/doctemplates/mahnung';
dol_mkdir($doctemplatedir);
return $this->_init($sql, $options);
}
/**
* Aufruf bei Modul-Deaktivierung. Tabellen bleiben erhalten (Datensicherheit).
*
* @param string $options Optionen
* @return int<-1,1> 1 = OK, <=0 = Fehler
*/
public function remove($options = '')
{
$sql = array();
return $this->_remove($sql, $options);
}
/**
* Ergaenzt Versand- und Tracking-Felder an llx_mahnung_mahnung, wenn sie
* in einer aelteren Schema-Version noch fehlen. Idempotent — fehlende
* Spalten werden geprueft via SHOW COLUMNS und nur dann hinzugefuegt.
*
* @return void
*/
public function migrateVersandFelder()
{
global $db;
$alter = array();
$cols = array(
'date_versand' => "ADD COLUMN date_versand DATETIME NULL",
'versandweg' => "ADD COLUMN versandweg VARCHAR(30) NULL",
'tracking_nr' => "ADD COLUMN tracking_nr VARCHAR(50) NULL",
'tracking_provider' => "ADD COLUMN tracking_provider VARCHAR(20) NULL",
);
foreach ($cols as $col => $clause) {
$res = $db->query("SHOW COLUMNS FROM ".MAIN_DB_PREFIX."mahnung_mahnung LIKE '".$db->escape($col)."'");
if ($res && $db->num_rows($res) == 0) {
$alter[] = $clause;
}
if ($res) {
$db->free($res);
}
}
if (!empty($alter)) {
$db->query("ALTER TABLE ".MAIN_DB_PREFIX."mahnung_mahnung ".implode(', ', $alter));
}
}
}