|
|
|
|
@ -8,7 +8,6 @@ from flask import Response
|
|
|
|
|
import io
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
# Configuratie laden
|
|
|
|
|
env_path = "./.env"
|
|
|
|
|
load_dotenv(env_path)
|
|
|
|
|
|
|
|
|
|
@ -17,15 +16,13 @@ ALLOWED_EXTENSIONS = {"xlsx"}
|
|
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
|
|
|
|
|
app.secret_key = "supersecretkey"
|
|
|
|
|
app.secret_key = "9f2a1d3e4c5b6a7d8e9f0a1b2c3d4e5f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Controleer of bestand toegestaan is
|
|
|
|
|
def allowed_file(filename):
|
|
|
|
|
return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Route voor de uploadpagina
|
|
|
|
|
@app.route("/", methods=["GET", "POST"])
|
|
|
|
|
def upload_file():
|
|
|
|
|
if request.method == "POST":
|
|
|
|
|
@ -70,14 +67,12 @@ def download_excel():
|
|
|
|
|
|
|
|
|
|
df = pd.DataFrame(result)
|
|
|
|
|
|
|
|
|
|
# Excel-bestand in-memory genereren
|
|
|
|
|
output = io.BytesIO()
|
|
|
|
|
with pd.ExcelWriter(output, engine="openpyxl") as writer:
|
|
|
|
|
df.to_excel(writer, index=False, sheet_name=f"Extrapolatie_{x}jaar")
|
|
|
|
|
|
|
|
|
|
output.seek(0)
|
|
|
|
|
|
|
|
|
|
# Dynamische bestandsnaam
|
|
|
|
|
filename = f"extrapolatie_{x}jaar.xlsx"
|
|
|
|
|
|
|
|
|
|
return Response(
|
|
|
|
|
@ -90,7 +85,6 @@ def download_excel():
|
|
|
|
|
def getextrapolation(extrapolateYears):
|
|
|
|
|
conn, cursor = get_db_connection()
|
|
|
|
|
|
|
|
|
|
# Haal de originele data op zonder extrapolatie
|
|
|
|
|
cursor.execute(
|
|
|
|
|
"""
|
|
|
|
|
SELECT
|
|
|
|
|
@ -112,41 +106,34 @@ def getextrapolation(extrapolateYears):
|
|
|
|
|
"""
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Haal de kolomnamen op
|
|
|
|
|
columns = [desc[0] for desc in cursor.description]
|
|
|
|
|
|
|
|
|
|
# Haal de ruwe data op
|
|
|
|
|
rows = cursor.fetchall()
|
|
|
|
|
|
|
|
|
|
# Sluit de cursor en de verbinding
|
|
|
|
|
cursor.close()
|
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
raw_data = [dict(zip(columns, row)) for row in rows]
|
|
|
|
|
|
|
|
|
|
# Huidige datum als pd.Timestamp (zonder tijd)
|
|
|
|
|
today = pd.Timestamp.today().normalize()
|
|
|
|
|
|
|
|
|
|
result = []
|
|
|
|
|
for row in raw_data:
|
|
|
|
|
vervaldatum = row.get("Vervaldatum")
|
|
|
|
|
|
|
|
|
|
# Converteer de Vervaldatum correct naar pd.Timestamp (of None bij fouten)
|
|
|
|
|
vervaldatum = pd.to_datetime(vervaldatum, errors="coerce")
|
|
|
|
|
|
|
|
|
|
# Controleer of de datum geldig is en in het verleden ligt
|
|
|
|
|
if pd.notna(vervaldatum) and vervaldatum < today:
|
|
|
|
|
vervaldatum = today # Zet op vandaag
|
|
|
|
|
|
|
|
|
|
for i in range(extrapolateYears + 1):
|
|
|
|
|
new_row = row.copy()
|
|
|
|
|
|
|
|
|
|
if pd.notna(vervaldatum): # Controleer of het een geldige datum is
|
|
|
|
|
if pd.notna(vervaldatum):
|
|
|
|
|
new_row["Vervaldatum"] = (
|
|
|
|
|
vervaldatum + pd.DateOffset(years=i)
|
|
|
|
|
).strftime("%Y-%m-%d")
|
|
|
|
|
else:
|
|
|
|
|
# Indien geen geldige datum, zet als None
|
|
|
|
|
new_row["Vervaldatum"] = None
|
|
|
|
|
|
|
|
|
|
result.append(new_row)
|
|
|
|
|
@ -154,22 +141,11 @@ def getextrapolation(extrapolateYears):
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def process_exceld(filepath):
|
|
|
|
|
df = pd.read_excel(filepath, engine="openpyxl")
|
|
|
|
|
print(df.columns)
|
|
|
|
|
df.columns = df.columns.str.strip()
|
|
|
|
|
# for _, row in df.iterrows():
|
|
|
|
|
# print(row["Object_omschrijving"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Verwerk het Excel-bestand en sla op in MariaDB
|
|
|
|
|
def process_excel(filepath):
|
|
|
|
|
df = pd.read_excel(filepath, engine="openpyxl")
|
|
|
|
|
df.columns = df.columns.str.strip()
|
|
|
|
|
# Databaseverbinding maken
|
|
|
|
|
conn, cursor = get_db_connection()
|
|
|
|
|
|
|
|
|
|
# Zorg dat de tabel bestaat
|
|
|
|
|
cursor.execute(
|
|
|
|
|
"""
|
|
|
|
|
CREATE TABLE IF NOT EXISTS onderhoud (
|
|
|
|
|
@ -192,7 +168,6 @@ def process_excel(filepath):
|
|
|
|
|
"""
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Data in de database invoegen
|
|
|
|
|
for _, row in df.iterrows():
|
|
|
|
|
for column in row.index:
|
|
|
|
|
if pd.isna(row[column]):
|
|
|
|
|
|