*
* 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 '
';
if ($latestCU || $latestAL) {
print '
';
// Kupfer
print '
';
print '
';
print img_picto('', 'fa-coins', 'class="fa-2x"');
print '
';
print '
';
print ''.$langs->trans("MetallzuschlagCUShort").'
';
if ($latestCU) {
print ''.price($latestCU->value, 0, '', 1, -1, 2).' €/100kg
';
if (!empty($latestCU->value_month_avg)) {
print ''.$langs->trans("MetallzuschlagMonthAvg").': '.price($latestCU->value_month_avg, 0, '', 1, -1, 2).' €/100kg
';
}
print ''.dol_print_date($db->jdate($latestCU->date_notiz), 'day').'';
} else {
print '-';
}
print '
';
print '
';
print '
';
print '
';
// Aluminium
print '
';
print '
';
print img_picto('', 'fa-coins', 'class="fa-2x"');
print '
';
print '
';
print ''.$langs->trans("MetallzuschlagALShort").'
';
if ($latestAL) {
print ''.price($latestAL->value, 0, '', 1, -1, 2).' €/100kg
';
if (!empty($latestAL->value_month_avg)) {
print ''.$langs->trans("MetallzuschlagMonthAvg").': '.price($latestAL->value_month_avg, 0, '', 1, -1, 2).' €/100kg
';
}
print ''.dol_print_date($db->jdate($latestAL->date_notiz), 'day').'';
} else {
print '-';
}
print '
';
print '
';
print '
';
print '
';
// Button: Jetzt abrufen
if ($user->hasRight('metallzuschlag', 'config')) {
print '
';
}
print '
';
} else {
// Keine Daten vorhanden
print '
'.$langs->trans("MetallzuschlagNoData").'
';
print '
';
if ($user->hasRight('metallzuschlag', 'config')) {
print '
';
print img_picto('', 'fa-sync', 'class="paddingright"');
print $langs->trans("MetallzuschlagFetchNow");
print '';
}
}
print '
';
print '';
// Diagramm: Notierungsverlauf
$chartDays = GETPOSTINT('chart_days') ?: 90;
$chartData = $api->getChartData($chartDays);
if (!empty($chartData['labels'])) {
print '
';
// Zeitraum-Auswahl
$chartButtons = '';
foreach (array(30, 90, 365) as $d) {
$active = ($chartDays == $d) ? ' butActionActive' : '';
$label = $langs->trans('MetallzuschlagLast'.$d.'Days');
$chartButtons .= ''.$label.' ';
}
print load_fiche_titre($langs->trans("MetallzuschlagChart"), $chartButtons, '');
print '';
print '';
print '
';
// 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 '';
print '';
}
// Kabel-Diagramm: Kupferzuschlag pro Kabel
$cablesWithKupfer = $api->getProductsWithKupfergehalt();
if (!empty($cablesWithKupfer) && !empty($chartData['labels'])) {
print '
';
// Parameter auslesen
$cableMode = GETPOST('cable_mode', 'alpha') ?: 'per_meter';
// Beide Modi Daten laden (fuer JavaScript-Wechsel)
$cableChartDataPerMeter = $api->getCableChartData($chartDays, array(), 'per_meter');
$cableChartDataTotal = $api->getCableChartData($chartDays, array(), 'total');
print load_fiche_titre($langs->trans("MetallzuschlagCableChart"), '', '');
// Steuerelemente (Modus, Differenz, Alle ein/aus)
print '';
// Kabel-Auswahl (Checkboxen)
print '';
print '
'.$langs->trans("MetallzuschlagSelectCables").'
';
print '
';
$colorIndex = 0;
$colors = array(
'#e6194B', '#3cb44b', '#ffe119', '#4363d8', '#f58231',
'#911eb4', '#42d4f4', '#f032e6', '#bfef45', '#fabed4',
'#469990', '#dcbeff', '#9A6324', '#fffac8', '#800000',
);
foreach ($cablesWithKupfer as $cable) {
$color = $colors[$colorIndex % count($colors)];
print '';
$colorIndex++;
}
print '
';
print '
';
// Chart Canvas
if (!empty($cableChartDataPerMeter['products'])) {
print '';
print '';
print '
';
// Datasets fuer beide Modi vorbereiten
$jsLabels = json_encode($cableChartDataPerMeter['labels']);
$datasetsPerMeter = array();
$datasetsTotal = array();
$colorIndex = 0;
foreach ($cableChartDataPerMeter['products'] as $productId => $productData) {
$color = $colors[$colorIndex % count($colors)];
$datasetsPerMeter[] = array(
'label' => $productData['label'],
'data' => $productData['data'],
'borderColor' => $color,
'backgroundColor' => 'transparent',
'borderWidth' => 2,
'tension' => 0.3,
'spanGaps' => true,
'productId' => $productId,
);
$colorIndex++;
}
$colorIndex = 0;
foreach ($cableChartDataTotal['products'] as $productId => $productData) {
$color = $colors[$colorIndex % count($colors)];
$datasetsTotal[] = array(
'label' => $productData['label'],
'data' => $productData['data'],
'borderColor' => $color,
'backgroundColor' => 'transparent',
'borderWidth' => 2,
'tension' => 0.3,
'spanGaps' => true,
'productId' => $productId,
);
$colorIndex++;
}
$jsDatasetsPerMeter = json_encode($datasetsPerMeter);
$jsDatasetsTotal = json_encode($datasetsTotal);
$yAxisLabelPerMeter = $langs->trans("MetallzuschlagPerMeterEUR");
$yAxisLabelTotal = $langs->trans("MetallzuschlagTotalAmountEUR");
print '';
} else {
print ''.$langs->trans("MetallzuschlagNoData").'
';
}
}
// Verlaufstabelle
print '
';
print load_fiche_titre($langs->trans("MetallzuschlagHistory"), '', '');
$history = $api->getHistory('sonepar', 60);
if (!empty($history)) {
print '';
print '';
print '| '.$langs->trans("MetallzuschlagDate").' | ';
print ''.$langs->trans("MetallzuschlagMetal").' | ';
print ''.$langs->trans("MetallzuschlagValue").' | ';
print ''.$langs->trans("MetallzuschlagMonthAvg").' | ';
print ''.$langs->trans("MetallzuschlagSourceCol").' | ';
print '
';
foreach ($history as $row) {
print '';
print '| '.dol_print_date($db->jdate($row->date_notiz), 'day').' | ';
print ''.metallzuschlagGetMetalLabel($row->metal).' | ';
print ''.price($row->value, 0, '', 1, -1, 2).' €/100kg | ';
print ''.(!empty($row->value_month_avg) ? price($row->value_month_avg, 0, '', 1, -1, 2).' €/100kg' : '-').' | ';
print ''.dol_escape_htmltag($row->source).' | ';
print '
';
}
print '
';
} else {
print ''.$langs->trans("MetallzuschlagNoData").'
';
}
llxFooter();
$db->close();