API updadte
This commit is contained in:
112
public/js/calculator.js
Normal file
112
public/js/calculator.js
Normal file
@@ -0,0 +1,112 @@
|
||||
$(document).ready(function() {
|
||||
// Live calculation function
|
||||
function calculate() {
|
||||
const kaufpreis = parseFloat($('#kaufpreis').val()) || 0;
|
||||
const wohnflaeche = parseFloat($('#wohnflaeche').val()) || 0;
|
||||
const nutzflaeche = parseFloat($('#nutzflaeche').val()) || 0;
|
||||
const baujahr = parseInt($('#baujahr').val()) || 0;
|
||||
const abschreibungszeit = parseFloat($('#abschreibungszeit').val()) || 50;
|
||||
const bundeslandSteuer = parseFloat($('#bundesland_id option:selected').data('steuer')) || 0;
|
||||
|
||||
// Gesamtfläche
|
||||
const gesamtflaeche = wohnflaeche + nutzflaeche;
|
||||
$('#result-gesamtflaeche').text(gesamtflaeche.toLocaleString('de-DE') + ' m²');
|
||||
|
||||
// Preis pro m²
|
||||
const preisProQm = wohnflaeche > 0 ? kaufpreis / wohnflaeche : 0;
|
||||
$('#result-preis-pro-qm').text(preisProQm.toLocaleString('de-DE', {minimumFractionDigits: 2, maximumFractionDigits: 2}) + ' €');
|
||||
|
||||
// Grunderwerbsteuer
|
||||
const grunderwerbsteuer = kaufpreis * (bundeslandSteuer / 100);
|
||||
$('#result-grunderwerbsteuer').text(grunderwerbsteuer.toLocaleString('de-DE', {minimumFractionDigits: 2, maximumFractionDigits: 2}) + ' €');
|
||||
|
||||
// Gesamtkosten
|
||||
const gesamtkosten = kaufpreis + grunderwerbsteuer;
|
||||
$('#result-gesamtkosten').text(gesamtkosten.toLocaleString('de-DE', {minimumFractionDigits: 2, maximumFractionDigits: 2}) + ' €');
|
||||
|
||||
// Jährliche Abschreibung
|
||||
const abschreibung = abschreibungszeit > 0 ? kaufpreis / abschreibungszeit : 0;
|
||||
$('#result-abschreibung').text(abschreibung.toLocaleString('de-DE', {minimumFractionDigits: 2, maximumFractionDigits: 2}) + ' €');
|
||||
|
||||
// Alter der Immobilie
|
||||
const currentYear = new Date().getFullYear();
|
||||
const alter = baujahr > 0 ? currentYear - baujahr : 0;
|
||||
$('#result-alter').text(alter + ' Jahre');
|
||||
}
|
||||
|
||||
// Trigger calculation on any input change
|
||||
$('#immo-calculator-form input, #immo-calculator-form select').on('input change', function() {
|
||||
calculate();
|
||||
});
|
||||
|
||||
// Generate shareable link
|
||||
$('#share-link-btn').click(function() {
|
||||
const formData = $('#immo-calculator-form').serializeArray();
|
||||
const params = new URLSearchParams();
|
||||
|
||||
formData.forEach(item => {
|
||||
if (item.value) {
|
||||
params.append(item.name, item.value);
|
||||
}
|
||||
});
|
||||
|
||||
const shareUrl = window.location.origin + window.location.pathname + '?' + params.toString();
|
||||
$('#share-url').val(shareUrl);
|
||||
$('#share-link-container').slideDown();
|
||||
});
|
||||
|
||||
// Copy link to clipboard
|
||||
$('#copy-link-btn').click(function() {
|
||||
const shareUrl = $('#share-url');
|
||||
shareUrl.select();
|
||||
document.execCommand('copy');
|
||||
alert('Link wurde in die Zwischenablage kopiert!');
|
||||
});
|
||||
|
||||
// Reset form
|
||||
$('#reset-btn').click(function() {
|
||||
$('#immo-calculator-form')[0].reset();
|
||||
$('#share-link-container').slideUp();
|
||||
calculate();
|
||||
});
|
||||
|
||||
// Save immobilie (for logged in users)
|
||||
$('#save-immobilie-btn').click(function() {
|
||||
const formData = $('#immo-calculator-form').serializeArray();
|
||||
const data = {};
|
||||
|
||||
formData.forEach(item => {
|
||||
if (item.name === 'garage') {
|
||||
data[item.name] = $('#garage').is(':checked');
|
||||
} else {
|
||||
data[item.name] = item.value;
|
||||
}
|
||||
});
|
||||
|
||||
$.ajax({
|
||||
url: '/immobilie/save',
|
||||
method: 'POST',
|
||||
contentType: 'application/json',
|
||||
data: JSON.stringify(data),
|
||||
success: function(response) {
|
||||
if (response.success) {
|
||||
alert(response.message + '\n\nSie können Ihre gespeicherten Immobilien unter "Meine Immobilien" einsehen.');
|
||||
} else {
|
||||
alert('Fehler: ' + response.message);
|
||||
}
|
||||
},
|
||||
error: function(xhr) {
|
||||
if (xhr.status === 401) {
|
||||
alert('Sie müssen angemeldet sein, um Immobilien zu speichern.');
|
||||
window.location.href = '/login';
|
||||
} else {
|
||||
const response = xhr.responseJSON;
|
||||
alert('Fehler: ' + (response ? response.message : 'Unbekannter Fehler'));
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Initial calculation on page load
|
||||
calculate();
|
||||
});
|
||||
Reference in New Issue
Block a user