Files
power-consumption/public/index.php
Christian Steinle 23e4aaefa8
Some checks failed
Build power-consumtion image / Build-and-release-image (push) Failing after 2m33s
Fix errors for first of month.
2025-04-01 08:27:25 +02:00

172 lines
7.9 KiB
PHP

<?php
$app = require(__DIR__ . '/../bootstrap.php');
$local_db = $app['local_db'];
$table_aggregation = $app['config']['local']['table_aggregation'];
$table_production = $app['config']['local']['table_production'];
require_once(__DIR__ . '/../jobs/aggregate.php');
require_once(__DIR__ . '/../functions/functions.php');
if (isset($_POST['action']) && $_POST['action'] == 'set_values') {
$check_query = 'SELECT date FROM ' . $table_production . ' WHERE date = "' . $_POST['date'] . '-01";';
$check_result = $local_db->query($check_query);
if ($check_result->num_rows === 0) {
$query = 'INSERT INTO ' . $table_production . ' (date, eg, og, price) VALUES ("' . $_POST['date'] . '-01", ' . $_POST['eg'] . ', ' . $_POST['og'] . ', ' . $_POST['price'] . ');';
} else {
$query = 'UPDATE ' . $table_production . ' SET eg = "' . $_POST['eg'] . '", og = "' . $_POST['og'] . '", price = "' . $_POST['price'] . '" WHERE date = "' . $_POST['date'] . '-01";';
}
$local_db->query($query);
}
$actual = new DateTime();
$dates = get_month($local_db, $table_aggregation);
$chosen_date = (isset($_POST['date'])) ? $_POST['date'] : $actual->modify('-1 day')->format('Y-m');
$data = get_aggregation($local_db, $table_aggregation, $chosen_date);
$month_values = get_month_aggregation($local_db, $table_aggregation, $chosen_date);
$year_values = get_year_aggregation($local_db, $table_aggregation, $chosen_date);
$grid_feed_by_month = get_grid_feed_by_month($local_db, $table_aggregation, $table_production, $chosen_date);
$month_production = get_month_production($local_db, $table_production, $chosen_date);
$year_production = get_year_production($local_db, $table_production, $chosen_date);
$last_value = get_last_value($local_db, $table_aggregation, $chosen_date);
$chosen_date_time = new DateTime($chosen_date);
?>
<!DOCTYPE html>
<html lang="de">
<head>
<title>Consumption values</title>
<link rel="stylesheet" href="/css/layout.css"/>
</head>
<body>
<form id="date_form" style="align-self: center; width: 500px; display: grid; grid-template-columns: 20% 20% 20% 20% 20%; gap: 10px;" action="/index.php" method="post">
<?php
if ($dates['first'] < $chosen_date_time) {
echo "<button type='button' onclick='submit_form(\"" . $chosen_date_time->modify('-1 month')->format('Y-m') . "\")'><</button>";
} else {
echo '<span></span>';
}
echo '<span style="text-align: center">' . $chosen_date_time->modify('+1 month')->format('Y-m') . '</span>';
if ($dates['last'] > $chosen_date_time->modify('+1 month')) {
echo "<button type='button' onclick='submit_form(\"" . $chosen_date_time->format('Y-m') . "\")'>></button>";
} else {
echo '<span></span>';
}
?>
<a class="button" href="year.php">Jahr</a>
<a class="button" href="production.php">Werte</a>
<input type="hidden" name="date" id="date" value=""/>
</form>
<div style="display: grid; grid-template-columns: 5% 15% 15% 15% 15% 15% 15%; gap: 2px 10px; margin-top: 5px; border-top: 2px solid gray; padding-top: 5px;">
<span class="color"></span>
<label><b>Messung</b></label>
<span><b>Monat</b></span>
<span><b>Jahr</b></span>
<span><b>Min</b></span>
<span><b>Max</b></span>
<span><b>Durchschnitt</b></span>
<span class="color color-1"></span>
<label>Verbrauch</label>
<span><?php echo number_format($month_values['meter_consumption'], 1, ',', '.'); ?> KWh</span>
<span><?php echo number_format($year_values['meter_consumption'], 1, ',', '.'); ?> KWh</span>
<span><?php echo number_format($data['min_meter'] / 1000, 1, ',', '.'); ?> KWh</span>
<span><?php echo number_format($data['max_meter'] / 1000, 1, ',', '.'); ?> KWh</span>
<span><?php echo number_format($data['avg_meter'] / 1000, 1, ',', '.'); ?> KWh</span>
<span class="color color-2"></span>
<label>Berechnet</label>
<span><?php echo number_format($month_values['power_sensor'], 1, ',', '.'); ?> KWh</span>
<span><?php echo number_format($year_values['power_sensor'], 1, ',', '.'); ?> KWh</span>
<span><?php echo number_format($data['min_power'] / 1000, 1, ',', '.'); ?> KWh</span>
<span><?php echo number_format($data['max_power'] / 1000, 1, ',', '.'); ?> KWh</span>
<span><?php echo number_format($data['avg_power'] / 1000, 1, ',', '.'); ?> KWh</span>
<span class="color color-3"></span>
<label>Einspeisung</label>
<span><?php echo number_format($month_values['grid_feed'], 1, ',', '.'); ?> KWh</span>
<span><?php echo number_format($year_values['grid_feed'], 1, ',', '.'); ?> KWh</span>
<span><?php echo number_format($data['min_feed'] / 1000, 1, ',', '.'); ?> KWh</span>
<span><?php echo number_format($data['max_feed'] / 1000, 1, ',', '.'); ?> KWh</span>
<span><?php echo number_format($data['avg_feed'] / 1000, 1, ',', '.'); ?> KWh</span>
<span class="color color-4"></span>
<label>Produktion</label>
<span><?php echo number_format($month_production['eg'] + $month_production['og'], 1, ',', '.'); ?> KWh</span>
<span><?php echo number_format($year_production['eg'] + $year_production['og'], 1, ',', '.'); ?> KWh</span>
<span><?php echo number_format($data['min_production'] / 1000, 1, ',', '.'); ?> KWh</span>
<span><?php echo number_format($data['max_production'] / 1000, 1, ',', '.'); ?> KWh</span>
<span><?php echo number_format($data['avg_production'] / 1000, 1, ',', '.'); ?> KWh</span>
<span class="color"></span>
<label><b>Ersparnis</b></label>
<span><b><?php echo number_format(($month_production['eg'] + $month_production['og'] - $month_values['grid_feed']) * $month_production['price'] / 100, 2, ',', '.'); ?> €</b></span>
<span><b><?php echo number_format(($year_production['eg_price'] + $year_production['og_price'] - array_sum($grid_feed_by_month)), 2, ',', '.'); ?> €</b></span>
<span></span>
<span></span>
<span></span>
<span class="color"></span>
<label><b>Eigenverbrauch</b></label>
<span><b><?php echo ($month_production['eg'] + $month_production['og'] > 0) ? number_format(100 - $month_values['grid_feed'] * 100 / ($month_production['eg'] + $month_production['og']), 2, ',', '.') : 0; ?> %</b></span>
<span><b><?php echo number_format(100 - $year_values['grid_feed'] * 100 / ($year_production['eg'] + $year_production['og']), 2, ',', '.'); ?> %</b></span>
<span></span>
<span></span>
<span></span>
<span class="color"></span>
<label><b>Zählerstand</b></label>
<span><b><?php echo number_format($last_value, 1, ',', '.'); ?> kWh</b></span>
</div>
<div id="chart-container">
<canvas id="chart"></canvas>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/4.4.1/chart.umd.js"></script>
<script>
function submit_form(value) {
document.getElementById('date').value = value;
document.getElementById('date_form').submit();
}
const chart = document.getElementById('chart');
new Chart(chart, {
type: 'bar',
data: {
labels: <?php echo json_encode($data['date']); ?>,
datasets: [
{
label: 'Verbrauch Zählerstand',
data: <?php echo json_encode($data['meter_consumption']); ?>,
backgroundColor: '#375BEB',
},
{
label: 'Verbrauch berechnet',
data: <?php echo json_encode($data['power_sensor']); ?>,
backgroundColor: '#90EB36',
},
{
label: 'Einspeisung',
data: <?php echo json_encode($data['grid_feed']); ?>,
backgroundColor: '#EB5F36',
},
{
label: 'Erzeugung',
data: <?php echo json_encode($data['production']); ?>,
backgroundColor: '#DAE32D',
}
]
},
options: {
aspectRatio: 2,
}
});
</script>
</body>
</html>