dolibarr.preisbot/admin/setup.php
data c478e5003b feat: Initial release of PreisBot module v1.0
Automatic sales price adjustment based on purchase prices and profit margin.

Features:
- Weekly cronjob for automatic price updates
- Configurable price source (cheapest/newest supplier)
- Configurable price direction (increase only / both)
- Minimum margin setting (default 20%)
- GlobalNotify integration for dashboard notifications
- Email reports with detailed price changes
- Extrafield "Gewinnaufschlag %" on products
- Price history entries with "PreisBot" label

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-03 12:13:44 +01:00

215 lines
7 KiB
PHP
Executable file

<?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.
*/
/**
* \file preisbot/admin/setup.php
* \ingroup preisbot
* \brief PreisBot setup page.
*/
// Load Dolibarr environment
$res = 0;
if (!$res && !empty($_SERVER["CONTEXT_DOCUMENT_ROOT"])) {
$res = @include $_SERVER["CONTEXT_DOCUMENT_ROOT"]."/main.inc.php";
}
$tmp = empty($_SERVER['SCRIPT_FILENAME']) ? '' : $_SERVER['SCRIPT_FILENAME'];
$tmp2 = realpath(__FILE__);
$i = strlen($tmp) - 1;
$j = strlen($tmp2) - 1;
while ($i > 0 && $j > 0 && isset($tmp[$i]) && isset($tmp2[$j]) && $tmp[$i] == $tmp2[$j]) {
$i--;
$j--;
}
if (!$res && $i > 0 && file_exists(substr($tmp, 0, ($i + 1))."/main.inc.php")) {
$res = @include substr($tmp, 0, ($i + 1))."/main.inc.php";
}
if (!$res && $i > 0 && file_exists(dirname(substr($tmp, 0, ($i + 1)))."/main.inc.php")) {
$res = @include dirname(substr($tmp, 0, ($i + 1)))."/main.inc.php";
}
if (!$res && file_exists("../../main.inc.php")) {
$res = @include "../../main.inc.php";
}
if (!$res && file_exists("../../../main.inc.php")) {
$res = @include "../../../main.inc.php";
}
if (!$res) {
die("Include of main fails");
}
require_once DOL_DOCUMENT_ROOT."/core/lib/admin.lib.php";
require_once '../lib/preisbot.lib.php';
$langs->loadLangs(array("admin", "preisbot@preisbot"));
// Access control
if (!$user->admin) {
accessforbidden();
}
$action = GETPOST('action', 'aZ09');
/*
* Actions
*/
if ($action == 'update') {
$error = 0;
// Preisquelle
$priceSource = GETPOST('PREISBOT_PRICE_SOURCE', 'alpha');
if (!in_array($priceSource, array('cheapest', 'newest'))) {
$priceSource = 'cheapest';
}
dolibarr_set_const($db, 'PREISBOT_PRICE_SOURCE', $priceSource, 'chaine', 0, '', $conf->entity);
// Preisrichtung
$priceDirection = GETPOST('PREISBOT_PRICE_DIRECTION', 'alpha');
if (!in_array($priceDirection, array('up_only', 'both'))) {
$priceDirection = 'up_only';
}
dolibarr_set_const($db, 'PREISBOT_PRICE_DIRECTION', $priceDirection, 'chaine', 0, '', $conf->entity);
// Mindestaufschlag
$minMargin = GETPOSTINT('PREISBOT_MIN_MARGIN');
if ($minMargin < 20) {
$minMargin = 20;
}
dolibarr_set_const($db, 'PREISBOT_MIN_MARGIN', $minMargin, 'chaine', 0, '', $conf->entity);
// E-Mail Empfänger
$mailTo = GETPOST('PREISBOT_MAIL_TO', 'email');
dolibarr_set_const($db, 'PREISBOT_MAIL_TO', $mailTo, 'chaine', 0, '', $conf->entity);
if (!$error) {
setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
} else {
setEventMessages($langs->trans("Error"), null, 'errors');
}
}
/*
* View
*/
$form = new Form($db);
$title = "PreisBotSetup";
$help_url = '';
llxHeader('', $langs->trans($title), $help_url, '', 0, 0, '', '', '', 'mod-preisbot page-admin');
$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
print load_fiche_titre($langs->trans($title), $linkback, 'title_setup');
$head = preisbotAdminPrepareHead();
print dol_get_fiche_head($head, 'settings', $langs->trans($title), -1, "preisbot@preisbot");
print '<span class="opacitymedium">'.$langs->trans("PreisBotSetupDesc").'</span><br><br>';
// Formular
print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
print '<input type="hidden" name="token" value="'.newToken().'">';
print '<input type="hidden" name="action" value="update">';
print '<table class="noborder centpercent">';
// Überschrift: Preisberechnung
print '<tr class="liste_titre">';
print '<td colspan="2">'.$langs->trans("PreisBotPriceCalculation").'</td>';
print '</tr>';
// Preisquelle
print '<tr class="oddeven">';
print '<td class="titlefield">'.$langs->trans("PreisBotPriceSource").'</td>';
print '<td>';
$priceSourceOptions = array(
'cheapest' => $langs->trans("PreisBotPriceSourceCheapest"),
'newest' => $langs->trans("PreisBotPriceSourceNewest")
);
print $form->selectarray('PREISBOT_PRICE_SOURCE', $priceSourceOptions, getDolGlobalString('PREISBOT_PRICE_SOURCE', 'cheapest'), 0, 0, 0, '', 0, 0, 0, '', 'minwidth200');
print '<br><span class="opacitymedium small">'.$langs->trans("PreisBotPriceSourceHelp").'</span>';
print '</td>';
print '</tr>';
// Preisrichtung
print '<tr class="oddeven">';
print '<td>'.$langs->trans("PreisBotPriceDirection").'</td>';
print '<td>';
$priceDirectionOptions = array(
'up_only' => $langs->trans("PreisBotPriceDirectionUpOnly"),
'both' => $langs->trans("PreisBotPriceDirectionBoth")
);
print $form->selectarray('PREISBOT_PRICE_DIRECTION', $priceDirectionOptions, getDolGlobalString('PREISBOT_PRICE_DIRECTION', 'up_only'), 0, 0, 0, '', 0, 0, 0, '', 'minwidth200');
print '<br><span class="opacitymedium small">'.$langs->trans("PreisBotPriceDirectionHelp").'</span>';
print '</td>';
print '</tr>';
// Mindestaufschlag
print '<tr class="oddeven">';
print '<td>'.$langs->trans("PreisBotMinMargin").'</td>';
print '<td>';
print '<input type="number" name="PREISBOT_MIN_MARGIN" value="'.getDolGlobalInt('PREISBOT_MIN_MARGIN', 20).'" min="20" max="1000" class="width75"> %';
print '<br><span class="opacitymedium small">'.$langs->trans("PreisBotMinMarginHelp").'</span>';
print '</td>';
print '</tr>';
// Überschrift: Benachrichtigungen
print '<tr class="liste_titre">';
print '<td colspan="2">'.$langs->trans("PreisBotNotifications").'</td>';
print '</tr>';
// E-Mail Empfänger
print '<tr class="oddeven">';
print '<td>'.$langs->trans("PreisBotMailTo").'</td>';
print '<td>';
print '<input type="email" name="PREISBOT_MAIL_TO" value="'.getDolGlobalString('PREISBOT_MAIL_TO').'" class="minwidth300" placeholder="admin@example.com">';
print '<br><span class="opacitymedium small">'.$langs->trans("PreisBotMailToHelp").'</span>';
print '</td>';
print '</tr>';
// GlobalNotify Info
if (isModEnabled('globalnotify')) {
print '<tr class="oddeven">';
print '<td>'.$langs->trans("PreisBotGlobalNotify").'</td>';
print '<td>';
print '<span class="badge badge-status4">'.$langs->trans("Enabled").'</span> ';
print $langs->trans("PreisBotGlobalNotifyEnabled");
print '</td>';
print '</tr>';
} else {
print '<tr class="oddeven">';
print '<td>'.$langs->trans("PreisBotGlobalNotify").'</td>';
print '<td>';
print '<span class="badge badge-status8">'.$langs->trans("Disabled").'</span> ';
print $langs->trans("PreisBotGlobalNotifyDisabled");
print '</td>';
print '</tr>';
}
print '</table>';
print '<br>';
print '<div class="center">';
print '<input type="submit" class="button button-save" value="'.$langs->trans("Save").'">';
print '</div>';
print '</form>';
// Info-Box
print '<br><br>';
print '<div class="info">';
print '<strong>'.$langs->trans("PreisBotHowItWorks").'</strong><br><br>';
print $langs->trans("PreisBotHowItWorksDesc");
print '<br><br>';
print '<strong>'.$langs->trans("PreisBotExtrafield").':</strong> ';
print $langs->trans("PreisBotExtrafieldDesc");
print '</div>';
print dol_get_fiche_end();
llxFooter();
$db->close();