481 lines
16 KiB
PHP
Executable file
481 lines
16 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>';
|
|
}
|
|
|
|
// Kabel-Diagramm: Kupferzuschlag pro Kabel
|
|
$cablesWithKupfer = $api->getProductsWithKupfergehalt();
|
|
|
|
if (!empty($cablesWithKupfer) && !empty($chartData['labels'])) {
|
|
print '<br>';
|
|
|
|
// Parameter auslesen
|
|
$cableIds = GETPOST('cable_ids', 'array');
|
|
$cableMode = GETPOST('cable_mode', 'alpha') ?: 'per_meter';
|
|
|
|
// URL-Basis fuer Links
|
|
$baseUrl = $_SERVER["PHP_SELF"].'?chart_days='.$chartDays;
|
|
|
|
// Kabel-Auswahl und Modus-Buttons
|
|
$cableButtons = '';
|
|
|
|
// Modus-Buttons
|
|
$modePerMeter = ($cableMode == 'per_meter') ? ' butActionActive' : '';
|
|
$modeTotal = ($cableMode == 'total') ? ' butActionActive' : '';
|
|
$cableButtons .= '<a class="butAction'.$modePerMeter.' small" href="'.$baseUrl.'&cable_mode=per_meter'.(!empty($cableIds) ? '&cable_ids[]='.implode('&cable_ids[]=', $cableIds) : '').'">'.$langs->trans("MetallzuschlagPerMeter").'</a> ';
|
|
$cableButtons .= '<a class="butAction'.$modeTotal.' small" href="'.$baseUrl.'&cable_mode=total'.(!empty($cableIds) ? '&cable_ids[]='.implode('&cable_ids[]=', $cableIds) : '').'">'.$langs->trans("MetallzuschlagTotalAmount").'</a>';
|
|
|
|
print load_fiche_titre($langs->trans("MetallzuschlagCableChart"), $cableButtons, '');
|
|
|
|
// Kabel-Auswahl (Checkboxen)
|
|
print '<div class="div-table-responsive-no-min" style="margin-bottom: 15px;">';
|
|
print '<form method="get" action="'.$_SERVER["PHP_SELF"].'">';
|
|
print '<input type="hidden" name="chart_days" value="'.$chartDays.'">';
|
|
print '<input type="hidden" name="cable_mode" value="'.$cableMode.'">';
|
|
|
|
print '<table class="noborder centpercent">';
|
|
print '<tr class="liste_titre">';
|
|
print '<td colspan="4">'.$langs->trans("MetallzuschlagSelectCables").'</td>';
|
|
print '</tr>';
|
|
print '<tr class="oddeven">';
|
|
|
|
$colCount = 0;
|
|
foreach ($cablesWithKupfer as $cable) {
|
|
$checked = (empty($cableIds) || in_array($cable->rowid, $cableIds)) ? ' checked' : '';
|
|
print '<td style="width: 25%;">';
|
|
print '<label>';
|
|
print '<input type="checkbox" name="cable_ids[]" value="'.$cable->rowid.'"'.$checked.'> ';
|
|
print dol_escape_htmltag($cable->ref);
|
|
if (!empty($cable->label)) {
|
|
print ' <span class="opacitymedium">('.dol_trunc($cable->label, 30).')</span>';
|
|
}
|
|
print '</label>';
|
|
print '</td>';
|
|
|
|
$colCount++;
|
|
if ($colCount % 4 == 0) {
|
|
print '</tr><tr class="oddeven">';
|
|
}
|
|
}
|
|
|
|
// Leere Zellen auffuellen
|
|
while ($colCount % 4 != 0) {
|
|
print '<td></td>';
|
|
$colCount++;
|
|
}
|
|
|
|
print '</tr>';
|
|
print '<tr class="oddeven">';
|
|
print '<td colspan="4">';
|
|
print '<button type="submit" class="butAction small">'.$langs->trans("Refresh").'</button> ';
|
|
print '<a class="butAction small" href="#" onclick="metallzuschlagToggleAllCables(); return false;">'.$langs->trans("MetallzuschlagAllCables").'</a>';
|
|
print '</td>';
|
|
print '</tr>';
|
|
print '</table>';
|
|
print '</form>';
|
|
print '</div>';
|
|
|
|
// JavaScript fuer Toggle alle Kabel
|
|
print '<script>';
|
|
print 'function metallzuschlagToggleAllCables() {';
|
|
print ' var checkboxes = document.querySelectorAll(\'input[name="cable_ids[]"]\');';
|
|
print ' var allChecked = Array.from(checkboxes).every(cb => cb.checked);';
|
|
print ' checkboxes.forEach(cb => cb.checked = !allChecked);';
|
|
print ' checkboxes[0].form.submit();';
|
|
print '}';
|
|
print '</script>';
|
|
|
|
// Kabel-Chart-Daten holen
|
|
$cableChartData = $api->getCableChartData($chartDays, $cableIds, $cableMode);
|
|
|
|
if (!empty($cableChartData['products'])) {
|
|
print '<div style="max-width: 100%; height: 400px;">';
|
|
print '<canvas id="cableChart"></canvas>';
|
|
print '</div>';
|
|
|
|
// Farben fuer Kabel (verschiedene Farben)
|
|
$colors = array(
|
|
'#e6194B', '#3cb44b', '#ffe119', '#4363d8', '#f58231',
|
|
'#911eb4', '#42d4f4', '#f032e6', '#bfef45', '#fabed4',
|
|
'#469990', '#dcbeff', '#9A6324', '#fffac8', '#800000',
|
|
);
|
|
|
|
// Datasets fuer Chart.js vorbereiten
|
|
$jsLabels = json_encode($cableChartData['labels']);
|
|
$datasets = array();
|
|
$colorIndex = 0;
|
|
|
|
foreach ($cableChartData['products'] as $productId => $productData) {
|
|
$color = $colors[$colorIndex % count($colors)];
|
|
$datasets[] = array(
|
|
'label' => $productData['label'],
|
|
'data' => $productData['data'],
|
|
'borderColor' => $color,
|
|
'backgroundColor' => 'transparent',
|
|
'borderWidth' => 2,
|
|
'tension' => 0.3,
|
|
'spanGaps' => true,
|
|
);
|
|
$colorIndex++;
|
|
}
|
|
|
|
$jsDatasets = json_encode($datasets);
|
|
$yAxisLabel = ($cableMode == 'total') ? $langs->trans("MetallzuschlagTotalAmountEUR") : $langs->trans("MetallzuschlagPerMeterEUR");
|
|
|
|
print '<script>';
|
|
print 'document.addEventListener("DOMContentLoaded", function() {';
|
|
print ' var ctx = document.getElementById("cableChart").getContext("2d");';
|
|
print ' new Chart(ctx, {';
|
|
print ' type: "line",';
|
|
print ' data: {';
|
|
print ' labels: '.$jsLabels.',';
|
|
print ' datasets: '.$jsDatasets;
|
|
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) : "-") + " €";';
|
|
print ' }';
|
|
print ' }';
|
|
print ' }';
|
|
print ' },';
|
|
print ' scales: {';
|
|
print ' x: {';
|
|
print ' ticks: { maxTicksLimit: 12 }';
|
|
print ' },';
|
|
print ' y: {';
|
|
print ' type: "linear",';
|
|
print ' position: "left",';
|
|
print ' title: { display: true, text: "'.dol_escape_js($yAxisLabel).'" },';
|
|
print ' ticks: { callback: function(v) { return v.toFixed(2) + " €"; } }';
|
|
print ' }';
|
|
print ' }';
|
|
print ' }';
|
|
print ' });';
|
|
print '});';
|
|
print '</script>';
|
|
} else {
|
|
print '<div class="opacitymedium">'.$langs->trans("MetallzuschlagNoData").'</div>';
|
|
}
|
|
}
|
|
|
|
// 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();
|