|
@@ -1,135 +1,142 @@
|
|
|
import pandas as pd
|
|
|
-import pyodbc
|
|
|
import json
|
|
|
-from os import path
|
|
|
+from pathlib import Path
|
|
|
from datetime import date
|
|
|
from dateutil.relativedelta import relativedelta
|
|
|
from sqlalchemy import create_engine
|
|
|
from suds.client import Client
|
|
|
from cryptography.fernet import Fernet
|
|
|
|
|
|
-# Konfiguration
|
|
|
-fernet_key = b"YBckeKYt-8g7LFvpG7XqAAcEbsYESnI-yl8by9rjeQQ="
|
|
|
-fernet = Fernet(fernet_key)
|
|
|
-
|
|
|
-if path.exists("nasa_config.json"):
|
|
|
- with open("nasa_config.json", "r") as f:
|
|
|
- config = json.load(f)
|
|
|
- with open("nasa_config.crypt", "wb") as f:
|
|
|
- f.write(fernet.encrypt(json.dumps(config).encode()))
|
|
|
-else:
|
|
|
- with open("nasa_config.crypt", "rb") as f:
|
|
|
- config = json.loads(fernet.decrypt(f.read()).decode())
|
|
|
-
|
|
|
-args = ["2020", "05"]
|
|
|
-# args = []
|
|
|
-if len(args) >= 2:
|
|
|
- config["selected_year"] = args[0]
|
|
|
- config["selected_month"] = args[1]
|
|
|
-else:
|
|
|
- curr_date = date.today() - relativedelta(months=+1)
|
|
|
- config["selected_year"] = curr_date.strftime("%Y")
|
|
|
- config["selected_month"] = curr_date.strftime("%m")
|
|
|
-
|
|
|
-
|
|
|
-select_befehl_auftraege = (
|
|
|
- "SELECT * FROM [Auftraege_NASA_gruppiert] WHERE Periode = '"
|
|
|
- + config["selected_year"]
|
|
|
- + config["selected_month"]
|
|
|
- + "'"
|
|
|
-)
|
|
|
-select_befehl_mitarbeiter = (
|
|
|
- "SELECT * FROM [Mitarbeiter_NASA] WHERE Periode = '"
|
|
|
- + config["selected_year"]
|
|
|
- + "-"
|
|
|
- + config["selected_month"]
|
|
|
- + "'"
|
|
|
-)
|
|
|
-source_auftraege = "nasa/Auftraege_NASA_gruppiert.csv"
|
|
|
-source_mitarbeiter = "nasa/Mitarbeiter_NASA.csv"
|
|
|
-
|
|
|
-
|
|
|
-header = {
|
|
|
- "HaendlerNr": config["client_id"],
|
|
|
- "Filiale": "1",
|
|
|
- "Jahr": config["selected_year"],
|
|
|
- "Monat": config["selected_month"],
|
|
|
- "Fabrikat": "Mazda",
|
|
|
- "AnzahlMitarbeiter": "0",
|
|
|
- "AnzahlProduktiv": "0.0",
|
|
|
- "WerkstattDurchlaeufe": "0",
|
|
|
- "Token": config["credentials"]["token"],
|
|
|
-}
|
|
|
+
|
|
|
+def get_config():
|
|
|
+ fernet_key = b"YBckeKYt-8g7LFvpG7XqAAcEbsYESnI-yl8by9rjeQQ="
|
|
|
+ fernet = Fernet(fernet_key)
|
|
|
+
|
|
|
+ if Path("config/nasa_config.json").exists():
|
|
|
+ with open("config/nasa_config.json", "r") as f:
|
|
|
+ config = json.load(f)
|
|
|
+ with open("config/nasa_config.crypt", "wb") as f:
|
|
|
+ f.write(fernet.encrypt(json.dumps(config).encode()))
|
|
|
+ else:
|
|
|
+ with open("config/nasa_config.crypt", "rb") as f:
|
|
|
+ config = json.loads(fernet.decrypt(f.read()).decode())
|
|
|
+ return config
|
|
|
|
|
|
|
|
|
def conn_string(dsn):
|
|
|
return f"mssql+pyodbc://{dsn['user']}:{dsn['pass']}@{dsn['server']}/{dsn['database']}?driver=SQL+Server+Native+Client+11.0"
|
|
|
|
|
|
|
|
|
-# Datenbankverbindung
|
|
|
-source_db = create_engine(conn_string(config["source_dsn"]))
|
|
|
-
|
|
|
-# Abfrage in Array speichern
|
|
|
-df = pd.read_sql(select_befehl_auftraege, con=source_db)
|
|
|
-# df = pd.read_csv(source_auftraege, sep=";", encoding="latin-1", decimal=",")
|
|
|
-df.to_csv(
|
|
|
- "mazda/export/" + config["selected_year"] + "-" + config["selected_month"] + "_auftraege.csv",
|
|
|
- sep=";",
|
|
|
- encoding="latin-1",
|
|
|
- decimal=",",
|
|
|
- index=False,
|
|
|
-)
|
|
|
-
|
|
|
-# Array in gewünschtes Format bringen
|
|
|
-auftragsart = ["Inspektion", "Karosseriearbeit", "Lackierung", "Verschleißteile", "Sonstiges"]
|
|
|
-columns = [
|
|
|
- "AuftragsArt",
|
|
|
- "AuftragsArtId",
|
|
|
- "TeileUmsatz",
|
|
|
- "LohnUmsatz",
|
|
|
- "SonstigeUmsatz",
|
|
|
- "GesamtUmsatz",
|
|
|
- "AnzahlAuftraege",
|
|
|
-]
|
|
|
-
|
|
|
-df = df[columns]
|
|
|
-header["WerkstattDurchlaeufe"] = df["AnzahlAuftraege"].sum()
|
|
|
-header["AfterSalesPositionen"] = df.to_dict("records")
|
|
|
-
|
|
|
-# Mitarbeiter gesamt und produktiv
|
|
|
-df = pd.read_sql(select_befehl_mitarbeiter, con=source_db)
|
|
|
-# df = pd.read_csv(source_mitarbeiter, sep=";", encoding="latin-1", decimal=",")
|
|
|
-
|
|
|
-df.to_csv(
|
|
|
- "mazda/export/" + config["selected_year"] + "-" + config["selected_month"] + "_mitarbeiter.csv",
|
|
|
- sep=";",
|
|
|
- encoding="latin-1",
|
|
|
- decimal=",",
|
|
|
- index=False,
|
|
|
-)
|
|
|
-
|
|
|
-header["AnzahlMitarbeiter"] = df.shape[0]
|
|
|
-header["AnzahlProduktiv"] = df["Prod"].sum()
|
|
|
-
|
|
|
-# SOAP-Verbindung
|
|
|
-client = Client(
|
|
|
- url=config["service_url"], username=config["credentials"]["username"], password=config["credentials"]["password"]
|
|
|
-)
|
|
|
-
|
|
|
-try:
|
|
|
- result = client.service.MeldeAfterSalesDaten(header)
|
|
|
-except Exception as e:
|
|
|
- print(e)
|
|
|
- result = -1
|
|
|
-
|
|
|
-# Erfolg/Fehler protokollieren
|
|
|
-
|
|
|
-print("Periode: " + config["selected_year"] + "-" + config["selected_month"])
|
|
|
-if len(header["AfterSalesPositionen"]) == result:
|
|
|
- print("Erfolgreich " + str(result) + " Datensätze übertragen")
|
|
|
-else:
|
|
|
+def load_data(config, source, year=None, month=None):
|
|
|
+ if year is None:
|
|
|
+ curr_date = date.today() - relativedelta(months=+1)
|
|
|
+ year = curr_date.strftime("%Y")
|
|
|
+ month = curr_date.strftime("%m")
|
|
|
+ period = f"{year}-{month}"
|
|
|
+
|
|
|
+ select_befehl_auftraege = f"SELECT * FROM [Auftraege_NASA_gruppiert] WHERE Periode = '{period}'"
|
|
|
+ select_befehl_mitarbeiter = f"SELECT * FROM [Mitarbeiter_NASA] WHERE Periode = '{period}'"
|
|
|
+
|
|
|
+ source_auftraege = "data/Auftraege_NASA_gruppiert.csv"
|
|
|
+ source_mitarbeiter = "data/Mitarbeiter_NASA.csv"
|
|
|
+
|
|
|
+ payload = {
|
|
|
+ "HaendlerNr": config["client_id"],
|
|
|
+ "Filiale": "1",
|
|
|
+ "Jahr": year,
|
|
|
+ "Monat": month,
|
|
|
+ "Fabrikat": "Mazda",
|
|
|
+ "AnzahlMitarbeiter": "0",
|
|
|
+ "AnzahlProduktiv": "0.0",
|
|
|
+ "WerkstattDurchlaeufe": "0",
|
|
|
+ "Token": config["credentials"]["token"],
|
|
|
+ }
|
|
|
+
|
|
|
+ if source == "database":
|
|
|
+ source_db = create_engine(conn_string(config["source_dsn"]))
|
|
|
+ df = pd.read_sql(select_befehl_auftraege, con=source_db)
|
|
|
+ else:
|
|
|
+ df = pd.read_csv(source_auftraege, sep=";", encoding="latin-1", decimal=",")
|
|
|
+
|
|
|
+ df.to_csv(
|
|
|
+ f"export/{period}_auftraege.csv",
|
|
|
+ sep=";",
|
|
|
+ encoding="latin-1",
|
|
|
+ decimal=",",
|
|
|
+ index=False,
|
|
|
+ )
|
|
|
+
|
|
|
+ # Array in gewünschtes Format bringen
|
|
|
+ auftragsart = ["Extern", "Garantie", "Intern", "Theke"]
|
|
|
+ auftragstyp = ["Inspektion", "Karosseriearbeit", "Lackierung", "Verschleißteile", "Sonstiges"]
|
|
|
+
|
|
|
+ columns = [
|
|
|
+ "AuftragsArt",
|
|
|
+ "AuftragsArtId",
|
|
|
+ "TeileUmsatz",
|
|
|
+ "LohnUmsatz",
|
|
|
+ "SonstigeUmsatz",
|
|
|
+ "GesamtUmsatz",
|
|
|
+ "AnzahlAuftraege",
|
|
|
+ ]
|
|
|
+
|
|
|
+ df = df[columns]
|
|
|
+ payload["WerkstattDurchlaeufe"] = df["AnzahlAuftraege"].sum()
|
|
|
+ payload["AfterSalesPositionen"] = df.to_dict("records")
|
|
|
+
|
|
|
+ # Mitarbeiter gesamt und produktiv
|
|
|
+ if source == "database":
|
|
|
+ df = pd.read_sql(select_befehl_mitarbeiter, con=source_db)
|
|
|
+ else:
|
|
|
+ df = pd.read_csv(source_mitarbeiter, sep=";", encoding="latin-1", decimal=",")
|
|
|
+
|
|
|
+ df.to_csv(
|
|
|
+ f"export/{period}_mitarbeiter.csv",
|
|
|
+ sep=";",
|
|
|
+ encoding="latin-1",
|
|
|
+ decimal=",",
|
|
|
+ index=False,
|
|
|
+ )
|
|
|
+
|
|
|
+ payload["AnzahlMitarbeiter"] = df.shape[0]
|
|
|
+ payload["AnzahlProduktiv"] = df["Prod"].sum()
|
|
|
+ return payload
|
|
|
+
|
|
|
+
|
|
|
+def submit_data(config, payload):
|
|
|
+ client = Client(
|
|
|
+ url=config["service_url"],
|
|
|
+ username=config["credentials"]["username"],
|
|
|
+ password=config["credentials"]["password"],
|
|
|
+ )
|
|
|
+
|
|
|
+ try:
|
|
|
+ return client.service.MeldeAfterSalesDaten(payload)
|
|
|
+ except Exception as e:
|
|
|
+ print(e)
|
|
|
+ return -1
|
|
|
+
|
|
|
+
|
|
|
+def print_result(period, result, len_pos):
|
|
|
+ print("Periode: " + period)
|
|
|
+ if len_pos == result:
|
|
|
+ print(f"Erfolgreich {result} Datensätze übertragen")
|
|
|
+ return
|
|
|
+
|
|
|
print("Übertragung der Datensätze Fehlgeschlagen.")
|
|
|
if result == -1:
|
|
|
print("Fehler! Es waren keine Datensätze vorhanden.")
|
|
|
else:
|
|
|
- print(str(len(header["AfterSalesPositionen"]) - result) + " Datensätze nicht verarbeitet")
|
|
|
+ print(f"{len_pos - result} von {len_pos} Datensätzen nicht verarbeitet!")
|
|
|
+
|
|
|
+
|
|
|
+def main():
|
|
|
+ config = get_config()
|
|
|
+ payload = load_data(config, "csv", "2024", "01")
|
|
|
+ result = submit_data(config, payload)
|
|
|
+ len_pos = len(payload["AfterSalesPositionen"])
|
|
|
+ print_result("2024-01", result, len_pos)
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ main()
|