Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,51 @@ <h1>{% block title %}{% if form and form.id.data %}Edit settings for {% else %}A

{% if form %}
<div class="form-wrapper">
<script>
$(document).ready(function() {
// Selektoren für die Input-Felder
const $pricePerKwh = $('#pricePerKwh_ct');
const $realChargedKwh = $('#realCharged_kWh');
const $realCost = $('#realCost_ct');

/**
* Berechnet die Gesamtkosten (als gerundete Ganzzahl) basierend auf
* Preis pro kWh und geladener kWh.
*/
function calculateRealCost() {
// Bereinigen der Werte (entfernt Leerzeichen)
const pricePerKwhString = $pricePerKwh.val().trim();
const realChargedKwhString = $realChargedKwh.val().trim();

// Prüfen, ob beide Felder nicht leer sind
if (pricePerKwhString === '' || realChargedKwhString === '') {
return;
}

// Konvertieren in Zahlen
const price = parseFloat(pricePerKwhString);
const kwh = parseFloat(realChargedKwhString);

// Prüfen, ob die konvertierten Werte gültige Zahlen (nicht NaN) und nicht negativ sind
if (isNaN(price) || isNaN(kwh) || price < 0 || kwh < 0) {
return;
}

// Multiplikation der Werte
const result = price * kwh;

// **SCHLÜSSELÄNDERUNG:** Runden des Ergebnisses auf die nächste ganze Zahl
const roundedResult = Math.round(result);

// Das gerundete Ergebnis in das Zielfeld schreiben
$realCost.val(roundedResult);
}

// Event-Listener für beide Felder
$pricePerKwh.on('keyup change', calculateRealCost);
$realChargedKwh.on('keyup change', calculateRealCost);
});
</script>
<form method="POST">
{{ form.csrf_token }}

Expand Down Expand Up @@ -369,4 +414,4 @@ <h1>{% block title %}{% if form and form.id.data %}Edit settings for {% else %}A
</form>
</div>
{% endif %}
{% endblock %}
{% endblock %}