- Sonepar Metal Note API: CU/AL Tageswerte + Monatsdurchschnitte - Dashboard mit Chart.js Verlaufsdiagramm (30/90/365 Tage) - Trigger: Kupfergehalt (kg/km) = Aderanzahl × Querschnitt × 8,89 - Trigger: Kupferzuschlag (€/m) auf Einkaufspreisen berechnen - CU-Logik: Lieferant-eigener Wert oder aktuellster aus History - Cronjobs: Wöchentlicher API-Abruf + Kupferzuschlag-Neuberechnung - Extrafields: Lieferantenkarte (CU/AL/Datum/Quelle), Produkt (Aderanzahl/Querschnitt/Kupfergehalt) - Admin-Seite mit API-URL, Auto-Fetch, Lieferantenübersicht - Mehrsprachig (de_DE, en_US) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
320 lines
11 KiB
PHP
Executable file
320 lines
11 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 metallzuschlag/metallzuschlagindex.php
|
|
* \ingroup metallzuschlag
|
|
* \brief Metallzuschlag Dashboard - Aktuelle Notierungen und Verlauf
|
|
*/
|
|
|
|
// Dolibarr Umgebung laden
|
|
$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 && file_exists("../../../main.inc.php")) {
|
|
$res = @include "../../../main.inc.php";
|
|
}
|
|
if (!$res) {
|
|
die("Include of main fails");
|
|
}
|
|
|
|
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
|
|
require_once './class/metallzuschlagapi.class.php';
|
|
require_once './lib/metallzuschlag.lib.php';
|
|
|
|
/**
|
|
* @var Conf $conf
|
|
* @var DoliDB $db
|
|
* @var HookManager $hookmanager
|
|
* @var Translate $langs
|
|
* @var User $user
|
|
*/
|
|
|
|
$langs->loadLangs(array("metallzuschlag@metallzuschlag"));
|
|
|
|
$action = GETPOST('action', 'aZ09');
|
|
|
|
// Zugriffskontrolle
|
|
if (!$user->hasRight('metallzuschlag', 'read')) {
|
|
accessforbidden();
|
|
}
|
|
|
|
$api = new MetallzuschlagApi($db);
|
|
|
|
|
|
/*
|
|
* Actions
|
|
*/
|
|
|
|
// Manueller Abruf
|
|
if ($action == 'fetch' && $user->hasRight('metallzuschlag', 'config')) {
|
|
if (!verifCond(GETPOST('token', 'alpha') == newToken())) {
|
|
accessforbidden('Bad CSRF token');
|
|
}
|
|
|
|
$result = $api->fetchMetalNotes();
|
|
if ($result >= 0) {
|
|
setEventMessages($langs->trans("MetallzuschlagFetchSuccess"), null, 'mesgs');
|
|
if (!empty($api->output)) {
|
|
setEventMessages($api->output, null, 'mesgs');
|
|
}
|
|
} else {
|
|
setEventMessages($langs->trans("MetallzuschlagFetchError"), null, 'errors');
|
|
if (!empty($api->error)) {
|
|
setEventMessages($api->error, null, 'errors');
|
|
}
|
|
}
|
|
|
|
header("Location: ".$_SERVER["PHP_SELF"]);
|
|
exit;
|
|
}
|
|
|
|
|
|
/*
|
|
* View
|
|
*/
|
|
|
|
llxHeader("", $langs->trans("MetallzuschlagDashboard"), '', '', 0, 0, '', '', '', 'mod-metallzuschlag page-index');
|
|
|
|
print load_fiche_titre($langs->trans("MetallzuschlagDashboard"), '', 'fa-coins');
|
|
|
|
// Aktuelle Werte abrufen
|
|
$latestCU = $api->getLatest('CU');
|
|
$latestAL = $api->getLatest('AL');
|
|
|
|
// Info-Panels: Aktuelle Notierungen
|
|
print '<div class="fichecenter">';
|
|
|
|
if ($latestCU || $latestAL) {
|
|
print '<div class="fichethirdleft">';
|
|
|
|
// Kupfer
|
|
print '<div class="info-box">';
|
|
print '<div class="info-box-icon bg-infobox-action">';
|
|
print img_picto('', 'fa-coins', 'class="fa-2x"');
|
|
print '</div>';
|
|
print '<div class="info-box-content">';
|
|
print '<span class="info-box-title">'.$langs->trans("MetallzuschlagCUShort").'</span><br>';
|
|
if ($latestCU) {
|
|
print '<span class="info-box-number">'.price($latestCU->value, 0, '', 1, -1, 2).' <small>€/100kg</small></span><br>';
|
|
if (!empty($latestCU->value_month_avg)) {
|
|
print '<span class="opacitymedium">'.$langs->trans("MetallzuschlagMonthAvg").': '.price($latestCU->value_month_avg, 0, '', 1, -1, 2).' €/100kg</span><br>';
|
|
}
|
|
print '<span class="opacitymedium">'.dol_print_date($db->jdate($latestCU->date_notiz), 'day').'</span>';
|
|
} else {
|
|
print '<span class="opacitymedium">-</span>';
|
|
}
|
|
print '</div>';
|
|
print '</div>';
|
|
|
|
print '</div>';
|
|
print '<div class="fichethirdleft">';
|
|
|
|
// Aluminium
|
|
print '<div class="info-box">';
|
|
print '<div class="info-box-icon bg-infobox-project">';
|
|
print img_picto('', 'fa-coins', 'class="fa-2x"');
|
|
print '</div>';
|
|
print '<div class="info-box-content">';
|
|
print '<span class="info-box-title">'.$langs->trans("MetallzuschlagALShort").'</span><br>';
|
|
if ($latestAL) {
|
|
print '<span class="info-box-number">'.price($latestAL->value, 0, '', 1, -1, 2).' <small>€/100kg</small></span><br>';
|
|
if (!empty($latestAL->value_month_avg)) {
|
|
print '<span class="opacitymedium">'.$langs->trans("MetallzuschlagMonthAvg").': '.price($latestAL->value_month_avg, 0, '', 1, -1, 2).' €/100kg</span><br>';
|
|
}
|
|
print '<span class="opacitymedium">'.dol_print_date($db->jdate($latestAL->date_notiz), 'day').'</span>';
|
|
} else {
|
|
print '<span class="opacitymedium">-</span>';
|
|
}
|
|
print '</div>';
|
|
print '</div>';
|
|
|
|
print '</div>';
|
|
print '<div class="fichethirdleft">';
|
|
|
|
// Button: Jetzt abrufen
|
|
if ($user->hasRight('metallzuschlag', 'config')) {
|
|
print '<div class="info-box">';
|
|
print '<div class="info-box-content" style="padding: 15px;">';
|
|
print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=fetch&token='.newToken().'">';
|
|
print img_picto('', 'fa-sync', 'class="paddingright"');
|
|
print $langs->trans("MetallzuschlagFetchNow");
|
|
print '</a>';
|
|
if ($latestCU) {
|
|
print '<br><br><span class="opacitymedium">'.$langs->trans("MetallzuschlagLastUpdate").': '.dol_print_date($db->jdate($latestCU->date_notiz), 'day').'</span>';
|
|
}
|
|
print '</div>';
|
|
print '</div>';
|
|
}
|
|
|
|
print '</div>';
|
|
} else {
|
|
// Keine Daten vorhanden
|
|
print '<div class="opacitymedium">'.$langs->trans("MetallzuschlagNoData").'</div>';
|
|
print '<br>';
|
|
if ($user->hasRight('metallzuschlag', 'config')) {
|
|
print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=fetch&token='.newToken().'">';
|
|
print img_picto('', 'fa-sync', 'class="paddingright"');
|
|
print $langs->trans("MetallzuschlagFetchNow");
|
|
print '</a>';
|
|
}
|
|
}
|
|
|
|
print '</div>';
|
|
print '<div class="clearboth"></div>';
|
|
|
|
// Diagramm: Notierungsverlauf
|
|
$chartDays = GETPOSTINT('chart_days') ?: 90;
|
|
$chartData = $api->getChartData($chartDays);
|
|
|
|
if (!empty($chartData['labels'])) {
|
|
print '<br>';
|
|
|
|
// Zeitraum-Auswahl
|
|
$chartButtons = '';
|
|
foreach (array(30, 90, 365) as $d) {
|
|
$active = ($chartDays == $d) ? ' butActionActive' : '';
|
|
$label = $langs->trans('MetallzuschlagLast'.$d.'Days');
|
|
$chartButtons .= '<a class="butAction'.$active.' small" href="'.$_SERVER["PHP_SELF"].'?chart_days='.$d.'">'.$label.'</a> ';
|
|
}
|
|
|
|
print load_fiche_titre($langs->trans("MetallzuschlagChart"), $chartButtons, '');
|
|
|
|
print '<div style="max-width: 100%; height: 400px;">';
|
|
print '<canvas id="metallzuschlagChart"></canvas>';
|
|
print '</div>';
|
|
|
|
// Chart.js-Daten vorbereiten
|
|
$jsLabels = json_encode($chartData['labels']);
|
|
$jsCU = json_encode($chartData['cu']);
|
|
$jsAL = json_encode($chartData['al']);
|
|
$labelCU = $langs->trans("MetallzuschlagChartCU");
|
|
$labelAL = $langs->trans("MetallzuschlagChartAL");
|
|
|
|
print '<script src="'.DOL_URL_ROOT.'/includes/nnnick/chartjs/dist/chart.min.js"></script>';
|
|
print '<script>';
|
|
print 'document.addEventListener("DOMContentLoaded", function() {';
|
|
print ' var ctx = document.getElementById("metallzuschlagChart").getContext("2d");';
|
|
print ' new Chart(ctx, {';
|
|
print ' type: "line",';
|
|
print ' data: {';
|
|
print ' labels: '.$jsLabels.',';
|
|
print ' datasets: [{';
|
|
print ' label: "'.dol_escape_js($labelCU).'",';
|
|
print ' data: '.$jsCU.',';
|
|
print ' borderColor: "#b87333",'; // Kupferfarbe
|
|
print ' backgroundColor: "rgba(184, 115, 51, 0.1)",';
|
|
print ' borderWidth: 2,';
|
|
print ' fill: true,';
|
|
print ' tension: 0.3,';
|
|
print ' spanGaps: true,';
|
|
print ' yAxisID: "yCU"';
|
|
print ' }, {';
|
|
print ' label: "'.dol_escape_js($labelAL).'",';
|
|
print ' data: '.$jsAL.',';
|
|
print ' borderColor: "#a8a9ad",'; // Aluminiumfarbe
|
|
print ' backgroundColor: "rgba(168, 169, 173, 0.1)",';
|
|
print ' borderWidth: 2,';
|
|
print ' fill: true,';
|
|
print ' tension: 0.3,';
|
|
print ' spanGaps: true,';
|
|
print ' yAxisID: "yAL"';
|
|
print ' }]';
|
|
print ' },';
|
|
print ' options: {';
|
|
print ' responsive: true,';
|
|
print ' maintainAspectRatio: false,';
|
|
print ' interaction: { mode: "index", intersect: false },';
|
|
print ' plugins: {';
|
|
print ' tooltip: {';
|
|
print ' callbacks: {';
|
|
print ' label: function(ctx) {';
|
|
print ' return ctx.dataset.label + ": " + (ctx.parsed.y !== null ? ctx.parsed.y.toFixed(2) : "-") + " €/100kg";';
|
|
print ' }';
|
|
print ' }';
|
|
print ' }';
|
|
print ' },';
|
|
print ' scales: {';
|
|
print ' x: {';
|
|
print ' ticks: { maxTicksLimit: 12 }';
|
|
print ' },';
|
|
print ' yCU: {';
|
|
print ' type: "linear",';
|
|
print ' position: "left",';
|
|
print ' title: { display: true, text: "'.dol_escape_js($labelCU).'" },';
|
|
print ' ticks: { callback: function(v) { return v.toFixed(0) + " €"; } }';
|
|
print ' },';
|
|
print ' yAL: {';
|
|
print ' type: "linear",';
|
|
print ' position: "right",';
|
|
print ' title: { display: true, text: "'.dol_escape_js($labelAL).'" },';
|
|
print ' ticks: { callback: function(v) { return v.toFixed(0) + " €"; } },';
|
|
print ' grid: { drawOnChartArea: false }';
|
|
print ' }';
|
|
print ' }';
|
|
print ' }';
|
|
print ' });';
|
|
print '});';
|
|
print '</script>';
|
|
}
|
|
|
|
// Verlaufstabelle
|
|
print '<br>';
|
|
print load_fiche_titre($langs->trans("MetallzuschlagHistory"), '', '');
|
|
|
|
$history = $api->getHistory('sonepar', 60);
|
|
|
|
if (!empty($history)) {
|
|
print '<table class="noborder centpercent">';
|
|
print '<tr class="liste_titre">';
|
|
print '<td>'.$langs->trans("MetallzuschlagDate").'</td>';
|
|
print '<td>'.$langs->trans("MetallzuschlagMetal").'</td>';
|
|
print '<td class="right">'.$langs->trans("MetallzuschlagValue").'</td>';
|
|
print '<td class="right">'.$langs->trans("MetallzuschlagMonthAvg").'</td>';
|
|
print '<td>'.$langs->trans("MetallzuschlagSourceCol").'</td>';
|
|
print '</tr>';
|
|
|
|
foreach ($history as $row) {
|
|
print '<tr class="oddeven">';
|
|
print '<td>'.dol_print_date($db->jdate($row->date_notiz), 'day').'</td>';
|
|
print '<td>'.metallzuschlagGetMetalLabel($row->metal).'</td>';
|
|
print '<td class="right">'.price($row->value, 0, '', 1, -1, 2).' €/100kg</td>';
|
|
print '<td class="right">'.(!empty($row->value_month_avg) ? price($row->value_month_avg, 0, '', 1, -1, 2).' €/100kg' : '-').'</td>';
|
|
print '<td>'.dol_escape_htmltag($row->source).'</td>';
|
|
print '</tr>';
|
|
}
|
|
|
|
print '</table>';
|
|
} else {
|
|
print '<div class="opacitymedium">'.$langs->trans("MetallzuschlagNoData").'</div>';
|
|
}
|
|
|
|
llxFooter();
|
|
$db->close();
|