|
|
|
|
@ -9,7 +9,6 @@ from werkzeug.utils import secure_filename
|
|
|
|
|
|
|
|
|
|
from database import get_db_connection
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
env_path = "./.env"
|
|
|
|
|
load_dotenv(env_path)
|
|
|
|
|
|
|
|
|
|
@ -41,13 +40,12 @@ def upload_file():
|
|
|
|
|
filename = secure_filename(file.filename)
|
|
|
|
|
filepath = os.path.join(app.config["UPLOAD_FOLDER"], filename)
|
|
|
|
|
file.save(filepath)
|
|
|
|
|
process_excel(filepath)
|
|
|
|
|
flash(
|
|
|
|
|
"Bestand geüpload en verwerkt! <a href='"
|
|
|
|
|
+ url_for("show_result")
|
|
|
|
|
+ "'>Bekijk de resultaten</a>",
|
|
|
|
|
"Bestand geüpload en verwerkt! <a href='" +
|
|
|
|
|
url_for("show_result") + "'>Bekijk de resultaten</a>",
|
|
|
|
|
"success",
|
|
|
|
|
)
|
|
|
|
|
process_excel(filepath)
|
|
|
|
|
return redirect(url_for("upload_file"))
|
|
|
|
|
|
|
|
|
|
return render_template("index.html")
|
|
|
|
|
@ -55,28 +53,37 @@ def upload_file():
|
|
|
|
|
|
|
|
|
|
@app.route("/extrapolate", methods=["GET", "POST"])
|
|
|
|
|
def show_result():
|
|
|
|
|
x = request.form.get("x", 1, type=int)
|
|
|
|
|
result = getextrapolation(x)
|
|
|
|
|
x_str = request.form.get("selected_date")
|
|
|
|
|
print(x_str)
|
|
|
|
|
try:
|
|
|
|
|
if x_str is not None:
|
|
|
|
|
x = datetime.strptime(x_str, "%Y-%m-%d")
|
|
|
|
|
else:
|
|
|
|
|
# Handle the case where x_str is None, for example:
|
|
|
|
|
x = None # or raise an exception or use a default date
|
|
|
|
|
flash("Ongeldige datum ingevoerd.", "danger")
|
|
|
|
|
except ValueError:
|
|
|
|
|
flash("Ongeldige datum ingevoerd.", "danger")
|
|
|
|
|
return redirect(url_for("upload_file"))
|
|
|
|
|
|
|
|
|
|
# Geef de resultaten door aan het HTML-template
|
|
|
|
|
return render_template("resultaat.html", result=result, x=x)
|
|
|
|
|
result = getextrapolation(x)
|
|
|
|
|
return render_template("resultaat.html", result=result, selected_date=x_str)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/download", methods=["GET"])
|
|
|
|
|
def download_excel():
|
|
|
|
|
x = request.args.get("x", 1, type=int)
|
|
|
|
|
x_str = request.args.get("x")
|
|
|
|
|
x = datetime.strptime(x_str, "%Y-%m-%d")
|
|
|
|
|
result = getextrapolation(x)
|
|
|
|
|
|
|
|
|
|
df = pd.DataFrame(result)
|
|
|
|
|
|
|
|
|
|
output = io.BytesIO()
|
|
|
|
|
with pd.ExcelWriter(output, engine="openpyxl") as writer:
|
|
|
|
|
df.to_excel(writer, index=False, sheet_name=f"Extrapolatie_{x}jaar")
|
|
|
|
|
|
|
|
|
|
df.to_excel(writer, index=False,
|
|
|
|
|
sheet_name=f"Extrapolatie_{x.strftime('%Y-%m-%d')}")
|
|
|
|
|
output.seek(0)
|
|
|
|
|
|
|
|
|
|
filename = f"extrapolatie_{x}jaar.xlsx"
|
|
|
|
|
|
|
|
|
|
filename = f"extrapolatie_{x.strftime('%Y-%m-%d')}.xlsx"
|
|
|
|
|
return Response(
|
|
|
|
|
output,
|
|
|
|
|
mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
|
|
|
@ -84,200 +91,72 @@ def download_excel():
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def extrapolate_vervaldatum(vervaldatum, uom, frequentie, extrapolate_years):
|
|
|
|
|
today = pd.Timestamp.today().normalize()
|
|
|
|
|
vervaldatum = pd.to_datetime(vervaldatum, errors="coerce")
|
|
|
|
|
|
|
|
|
|
result = []
|
|
|
|
|
for i in range(extrapolate_years + 1):
|
|
|
|
|
if uom == "D": # Dagelijks
|
|
|
|
|
months_to_add = round(frequentie * i / 30.44)
|
|
|
|
|
new_date = vervaldatum + pd.DateOffset(months=months_to_add)
|
|
|
|
|
# new_date = vervaldatum + pd.DateOffset(days=frequentie * i)
|
|
|
|
|
elif uom == "M": # Maandelijks
|
|
|
|
|
new_date = vervaldatum + pd.DateOffset(months=int(frequentie * i))
|
|
|
|
|
elif uom == "Y": # Jaarlijks
|
|
|
|
|
new_date = vervaldatum + pd.DateOffset(years=int(frequentie * i))
|
|
|
|
|
else:
|
|
|
|
|
continue # Onbekende UOM, overslaan
|
|
|
|
|
|
|
|
|
|
if new_date <= today + pd.DateOffset(years=extrapolate_years):
|
|
|
|
|
result.append(new_date.strftime("%Y-%m-%d"))
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
def extrapolate_vervaldatum(vervaldatum, uom, frequentie, x):
|
|
|
|
|
if vervaldatum is None or pd.isna(vervaldatum):
|
|
|
|
|
print("Ongeldige vervaldatum:", vervaldatum)
|
|
|
|
|
return [] # Geef een lege lijst terug in plaats van een fout te veroorzaken
|
|
|
|
|
|
|
|
|
|
vervaldatum = pd.to_datetime(str(vervaldatum), errors="coerce")
|
|
|
|
|
|
|
|
|
|
def extrapolate_vervaldatum_assheet(vervaldatum, uom, frequentie, extrapolate_years):
|
|
|
|
|
today = pd.Timestamp.today().normalize()
|
|
|
|
|
vervaldatum = pd.to_datetime(vervaldatum, errors="coerce")
|
|
|
|
|
if pd.isna(vervaldatum): # Extra controle als de conversie mislukt
|
|
|
|
|
print("Kon datum niet omzetten:", vervaldatum)
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
result = []
|
|
|
|
|
for i in range(extrapolate_years + 1):
|
|
|
|
|
if uom == "D": # Dagelijks
|
|
|
|
|
years_to_add = (frequentie * i) / 365 # Omrekenen naar jaren
|
|
|
|
|
# Afronden naar het dichtstbijzijnde jaar
|
|
|
|
|
rounded_years = round(years_to_add)
|
|
|
|
|
new_date = vervaldatum + pd.DateOffset(years=rounded_years)
|
|
|
|
|
|
|
|
|
|
elif uom == "M": # Maandelijks
|
|
|
|
|
years_to_add = (frequentie * i) / 12 # Omrekenen naar jaren
|
|
|
|
|
# Afronden naar het dichtstbijzijnde jaar
|
|
|
|
|
rounded_years = round(years_to_add)
|
|
|
|
|
new_date = vervaldatum + pd.DateOffset(years=rounded_years)
|
|
|
|
|
|
|
|
|
|
elif uom == "Y": # Jaarlijks
|
|
|
|
|
# Afronden naar het dichtstbijzijnde jaar
|
|
|
|
|
rounded_years = round(frequentie * i)
|
|
|
|
|
new_date = vervaldatum + pd.DateOffset(years=rounded_years)
|
|
|
|
|
|
|
|
|
|
while vervaldatum <= x:
|
|
|
|
|
result.append(vervaldatum.strftime("%Y-%m-%d"))
|
|
|
|
|
|
|
|
|
|
if uom == "D":
|
|
|
|
|
vervaldatum += pd.DateOffset(days=frequentie)
|
|
|
|
|
elif uom == "W":
|
|
|
|
|
vervaldatum += pd.DateOffset(weeks=frequentie)
|
|
|
|
|
elif uom == "M":
|
|
|
|
|
vervaldatum += pd.DateOffset(months=frequentie)
|
|
|
|
|
elif uom == "Y":
|
|
|
|
|
vervaldatum += pd.DateOffset(years=frequentie)
|
|
|
|
|
else:
|
|
|
|
|
continue # Onbekende UOM, overslaan
|
|
|
|
|
|
|
|
|
|
# Controle of de nieuwe datum binnen het bereik valt
|
|
|
|
|
if new_date <= today + pd.DateOffset(years=extrapolate_years):
|
|
|
|
|
result.append(new_date.strftime("%Y-%m-%d"))
|
|
|
|
|
print("Onbekende eenheid:", uom)
|
|
|
|
|
break # Voorkom oneindige lus bij een onbekende eenheid
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getextrapolation(extrapolateYears):
|
|
|
|
|
def getextrapolation(x):
|
|
|
|
|
if x is None:
|
|
|
|
|
print("Geen geldige datum opgegeven voor extrapolatie.")
|
|
|
|
|
return []
|
|
|
|
|
conn, cursor = get_db_connection()
|
|
|
|
|
|
|
|
|
|
cursor.execute(
|
|
|
|
|
"""
|
|
|
|
|
SELECT
|
|
|
|
|
Object_code,
|
|
|
|
|
Object_omschrijving,
|
|
|
|
|
Afdeling_object,
|
|
|
|
|
PO_Code,
|
|
|
|
|
Omschrijving_PO,
|
|
|
|
|
Vervaldatum,
|
|
|
|
|
Frequentie,
|
|
|
|
|
UOM,
|
|
|
|
|
PO_schema_Niet_gebruikt,
|
|
|
|
|
Cluster,
|
|
|
|
|
Locatie,
|
|
|
|
|
Locatie_omschrijving,
|
|
|
|
|
Klasse_object,
|
|
|
|
|
Categorie
|
|
|
|
|
cursor.execute("""
|
|
|
|
|
SELECT Object_code, Object_omschrijving, Afdeling_object, PO_Code, Omschrijving_PO, Vervaldatum, Frequentie, UOM,
|
|
|
|
|
PO_schema_Niet_gebruikt, Cluster, Locatie, Locatie_omschrijving, Klasse_object, Categorie
|
|
|
|
|
FROM onderhoud
|
|
|
|
|
"""
|
|
|
|
|
)
|
|
|
|
|
""")
|
|
|
|
|
|
|
|
|
|
columns = [desc[0] for desc in cursor.description]
|
|
|
|
|
|
|
|
|
|
rows = cursor.fetchall()
|
|
|
|
|
|
|
|
|
|
cursor.close()
|
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
raw_data = [dict(zip(columns, row)) for row in rows]
|
|
|
|
|
|
|
|
|
|
today = pd.Timestamp.today().normalize()
|
|
|
|
|
|
|
|
|
|
result = []
|
|
|
|
|
for row in raw_data:
|
|
|
|
|
|
|
|
|
|
for row in raw_data:
|
|
|
|
|
vervaldatum = row.get("Vervaldatum")
|
|
|
|
|
uom = row.get("UOM")
|
|
|
|
|
frequentie = row.get("Frequentie", 0)
|
|
|
|
|
|
|
|
|
|
if vervaldatum:
|
|
|
|
|
extrapolated_dates = extrapolate_vervaldatum(
|
|
|
|
|
vervaldatum, uom, frequentie, extrapolateYears)
|
|
|
|
|
|
|
|
|
|
vervaldatum, uom, frequentie, x)
|
|
|
|
|
for date in extrapolated_dates:
|
|
|
|
|
new_row = row.copy()
|
|
|
|
|
new_row["Vervaldatum"] = date
|
|
|
|
|
|
|
|
|
|
new_row["Overtijd"] = ""
|
|
|
|
|
if pd.to_datetime(date) < today:
|
|
|
|
|
new_row["Overtijd"] = "ja"
|
|
|
|
|
else:
|
|
|
|
|
new_row["Overtijd"] = "Nee"
|
|
|
|
|
result.append(new_row)
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def process_excel(filepath):
|
|
|
|
|
df = pd.read_excel(filepath, engine="openpyxl")
|
|
|
|
|
df.columns = df.columns.str.strip()
|
|
|
|
|
|
|
|
|
|
conn, cursor = get_db_connection()
|
|
|
|
|
|
|
|
|
|
cursor.execute(
|
|
|
|
|
"""
|
|
|
|
|
truncate table onderhoud
|
|
|
|
|
"""
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
cursor.execute(
|
|
|
|
|
"""
|
|
|
|
|
CREATE TABLE IF NOT EXISTS onderhoud (
|
|
|
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
|
|
|
Object_code VARCHAR(50),
|
|
|
|
|
Object_omschrijving TEXT,
|
|
|
|
|
Afdeling_object VARCHAR(100),
|
|
|
|
|
PO_Code VARCHAR(50),
|
|
|
|
|
Omschrijving_PO TEXT,
|
|
|
|
|
Vervaldatum DATE,
|
|
|
|
|
Frequentie INT,
|
|
|
|
|
UOM TEXT,
|
|
|
|
|
PO_schema_Niet_gebruikt TEXT,
|
|
|
|
|
Cluster TEXT,
|
|
|
|
|
Locatie TEXT,
|
|
|
|
|
Locatie_omschrijving TEXT,
|
|
|
|
|
Klasse_object TEXT,
|
|
|
|
|
Categorie TEXT
|
|
|
|
|
)
|
|
|
|
|
"""
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
cursor.execute(
|
|
|
|
|
"SELECT Object_code, Omschrijving_PO, Vervaldatum FROM onderhoud")
|
|
|
|
|
existing_records = set(cursor.fetchall())
|
|
|
|
|
|
|
|
|
|
for _, row in df.iterrows():
|
|
|
|
|
for column in row.index:
|
|
|
|
|
if pd.isna(row[column]):
|
|
|
|
|
row[column] = ""
|
|
|
|
|
|
|
|
|
|
record = (row["Object code"],
|
|
|
|
|
row["Omschrijving PO"], row["Vervaldatum"])
|
|
|
|
|
|
|
|
|
|
if record not in existing_records:
|
|
|
|
|
cursor.execute(
|
|
|
|
|
"""
|
|
|
|
|
INSERT INTO onderhoud
|
|
|
|
|
(Object_code, Object_omschrijving, Afdeling_object, PO_Code, Omschrijving_PO, Vervaldatum, Frequentie,
|
|
|
|
|
UOM, PO_schema_Niet_gebruikt, Cluster, Locatie, Locatie_omschrijving, Klasse_object, Categorie)
|
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
|
|
|
""",
|
|
|
|
|
(
|
|
|
|
|
row["Object code"],
|
|
|
|
|
row["Object omschrijving"],
|
|
|
|
|
row["Afdeling object"],
|
|
|
|
|
row["PO Code"],
|
|
|
|
|
row["Omschrijving PO"],
|
|
|
|
|
row["Vervaldatum"],
|
|
|
|
|
row["Frequentie"],
|
|
|
|
|
row["UOM"],
|
|
|
|
|
row["PO-schema Niet gebruikt"],
|
|
|
|
|
row["Cluster"],
|
|
|
|
|
row["Locatie"],
|
|
|
|
|
row["Locatie omschrijving"],
|
|
|
|
|
row["Klasse object"],
|
|
|
|
|
row["Categorie"],
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
existing_records.add(record)
|
|
|
|
|
|
|
|
|
|
conn.commit()
|
|
|
|
|
cursor.close()
|
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
|
|
|
|
app.run(debug=True)
|
|
|
|
|
|