Files
power-consumption/public/production.php

100 lines
4.4 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__ . '/../functions/functions.php');
if (isset($_POST['action']) && $_POST['action'] == 'set_values') {
$sum_eg = 0;
$sum_og = 0;
foreach ($_POST['values'] as $key => $value) {
$update_query = 'UPDATE ' . $table_aggregation . ' SET production_eg = ' . $value['production_eg'] . ', production_og = ' . $value['production_og'] . ' WHERE `date` = "' . $key . '" LIMIT 1;';
$local_db->query($update_query);
$sum_eg += $value['production_eg'] / 1000;
$sum_og += $value['production_og'] / 1000;
}
$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", ' . $sum_eg . ', ' . $sum_og . ', ' . $_POST['price'] . ');';
} else {
$query = 'UPDATE ' . $table_production . ' SET eg = "' . $sum_eg . '", og = "' . $sum_og . '", price = "' . $_POST['price'] . '" WHERE date = "' . $_POST['date'] . '-01";';
}
$local_db->query($query);
}
$dates = get_month($local_db, $table_aggregation);
$chosen_date = (isset($_POST['date'])) ? $_POST['date'] : $dates[0];
$production_values = get_production_values_for_month($local_db, $table_aggregation, $chosen_date);
$month_production = get_month_production($local_db, $table_production, $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="/production.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="index.php">Monat</a>
<a class="button" href="year.php">Jahr</a>
<input type="hidden" name="date" id="date" value=""/>
</form>
<form action="production.php" method="post"
style="display: grid; grid-template-columns: 15% 12% 10% 10% 12%; gap: 2px 10px; margin-top: 10px; font-size: 1.3rem;">
<?php
foreach ($production_values as $daily_production) {
?>
<span><?php echo $daily_production['date']; ?></span>
<label style="text-align: right;" for="production_eg[<?php echo $daily_production['date']; ?>]">EG</label>
<input type="number" step="0.01" name="values[<?php echo $daily_production['date']; ?>][production_eg]"
id="production_eg[<?php echo $daily_production['date']; ?>]"
value="<?php echo $daily_production['production_eg']; ?>"/>
<label style="text-align: right;" for="production_og[<?php echo $daily_production['date']; ?>]">OG</label>
<input type="number" step="0.01" name="values[<?php echo $daily_production['date']; ?>][production_og]"
id="production_og[<?php echo $daily_production['date']; ?>]"
value="<?php echo $daily_production['production_og']; ?>"/>
<?php
}
?>
<span>Preis</span>
<label style="text-align: right;">Cent / kWh</label>
<input type="number" step="0.01" name="price" value="<?php echo $month_production['price']; ?>"/>
<span></span>
<button type="submit">Speichern</button>
<input type="hidden" name="action" value="set_values"/>
<input type="hidden" name="date" value="<?php echo $chosen_date; ?>"/>
</form>
<script>
function submit_form(value) {
document.getElementById('date').value = value;
document.getElementById('date_form').submit();
}
</script>
</body>
</html>