|
|
|
|
@ -2,14 +2,14 @@ import streamlit as st
|
|
|
|
|
import pandas as pd
|
|
|
|
|
import subprocess
|
|
|
|
|
import mysql.connector
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
import os
|
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
|
load_dotenv()
|
|
|
|
|
total_time_unit = []
|
|
|
|
|
|
|
|
|
|
def get_filtered_users(customer_id):
|
|
|
|
|
def get_filtered_users(customer_id, start_date, end_date):
|
|
|
|
|
mydb = mysql.connector.connect(
|
|
|
|
|
host=os.getenv("MYSQL_HOST"),
|
|
|
|
|
user=os.getenv("MYSQL_USER"),
|
|
|
|
|
@ -17,24 +17,28 @@ def get_filtered_users(customer_id):
|
|
|
|
|
database=os.getenv("MYSQL_DATABASE")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Prepare the base query
|
|
|
|
|
# Prepare the base query with date filter
|
|
|
|
|
query = f"""
|
|
|
|
|
SELECT number,title,createdate,time FROM Kunden.tickets
|
|
|
|
|
WHERE 1=1
|
|
|
|
|
SELECT number, title, createdate, time FROM Kunden.tickets
|
|
|
|
|
WHERE createdate BETWEEN '{start_date}' AND '{end_date}'
|
|
|
|
|
"""
|
|
|
|
|
if customer_id:
|
|
|
|
|
query += f"AND customer_ID = {customer_id} order by createdate"
|
|
|
|
|
query += f" AND customer_ID = {customer_id}"
|
|
|
|
|
query += " ORDER BY createdate"
|
|
|
|
|
|
|
|
|
|
users = pd.read_sql_query(query, mydb)
|
|
|
|
|
mydb.close()
|
|
|
|
|
|
|
|
|
|
# Format date columns
|
|
|
|
|
for column in users.select_dtypes(include=['datetime64[ns]', 'datetime64[ns, UTC]']).columns:
|
|
|
|
|
users[column] = users[column].dt.strftime('%d.%m.%Y')
|
|
|
|
|
|
|
|
|
|
# Convert 'time' to integer
|
|
|
|
|
users.iloc[:, 3] = users.iloc[:, 3].str.split('.').str[0]
|
|
|
|
|
users['time'] = users['time'].astype(int)
|
|
|
|
|
|
|
|
|
|
base_url = "https://ticket.stines.de/#ticket/zoom/number/" # Replace with your actual base URL
|
|
|
|
|
# Create hyperlink for ticket number
|
|
|
|
|
base_url = "https://ticket.stines.de/#ticket/zoom/number/"
|
|
|
|
|
users['Link'] = users['number'].apply(lambda x: f'<a href="{base_url}{x}" target="_blank">{x}</a>')
|
|
|
|
|
|
|
|
|
|
return users
|
|
|
|
|
@ -65,28 +69,28 @@ def get_initial_data():
|
|
|
|
|
return service_ids, customers
|
|
|
|
|
|
|
|
|
|
def run_script_and_list_documents():
|
|
|
|
|
output_area = st.empty() # Platzhalter für die Ausgabe
|
|
|
|
|
document_paths = [] # Liste der erstellten Dokumente
|
|
|
|
|
output_area = st.empty() # Placeholder for output
|
|
|
|
|
document_paths = [] # List to store generated document paths
|
|
|
|
|
|
|
|
|
|
# Verzeichnis, in dem das Skript ausgeführt werden soll
|
|
|
|
|
# Directory where the script should be run
|
|
|
|
|
working_directory = "apps/ticket_export/exports"
|
|
|
|
|
|
|
|
|
|
# Führe das Skript in dem angegebenen Verzeichnis aus
|
|
|
|
|
# Run the script in the specified directory
|
|
|
|
|
result = subprocess.run(
|
|
|
|
|
["python3", "apps/ticket_export/main.py"],
|
|
|
|
|
capture_output=True,
|
|
|
|
|
text=True
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Suche nach den erstellten Dokumenten im Arbeitsverzeichnis
|
|
|
|
|
for filename in os.listdir("apps/ticket_export/exports"):
|
|
|
|
|
# Search for generated documents in the working directory
|
|
|
|
|
for filename in os.listdir(working_directory):
|
|
|
|
|
if filename.endswith(".docx"):
|
|
|
|
|
full_path = os.path.join(working_directory, filename)
|
|
|
|
|
document_paths.append(full_path)
|
|
|
|
|
|
|
|
|
|
# Zeige die Liste der erstellten Dokumente an und biete sie zum Download an
|
|
|
|
|
# Display the list of generated documents and offer them for download
|
|
|
|
|
if document_paths:
|
|
|
|
|
st.write("Erstellte Dokumente:")
|
|
|
|
|
st.write("Generated Documents:")
|
|
|
|
|
for doc_path in document_paths:
|
|
|
|
|
with open(doc_path, "rb") as file:
|
|
|
|
|
st.download_button(
|
|
|
|
|
@ -96,7 +100,7 @@ def run_script_and_list_documents():
|
|
|
|
|
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
st.write("Keine Dokumente gefunden.")
|
|
|
|
|
st.write("No documents found.")
|
|
|
|
|
|
|
|
|
|
def ticket_filter():
|
|
|
|
|
st.title("Ticket Filter :mag_right:")
|
|
|
|
|
@ -112,29 +116,37 @@ def ticket_filter():
|
|
|
|
|
# Extract customer_ID from selected option
|
|
|
|
|
selected_customer_id = None if selected_customer == "All" else int(selected_customer.split(' - ')[0])
|
|
|
|
|
|
|
|
|
|
if st.button("Rechnung erstellen"):
|
|
|
|
|
st.write("Das Skript wird ausgeführt...")
|
|
|
|
|
# Add date range picker
|
|
|
|
|
start_date = st.date_input("Start Date", datetime.now() - timedelta(days=30))
|
|
|
|
|
end_date = st.date_input("End Date", datetime.now())
|
|
|
|
|
|
|
|
|
|
if st.button("Create Invoice"):
|
|
|
|
|
st.write("Running the script...")
|
|
|
|
|
run_script_and_list_documents()
|
|
|
|
|
|
|
|
|
|
# Add a button to apply filters
|
|
|
|
|
if st.button('Apply Filters'):
|
|
|
|
|
# Fetch filtered data from the database
|
|
|
|
|
filtered_data = get_filtered_users(selected_customer_id)
|
|
|
|
|
if not filtered_data.empty:
|
|
|
|
|
# st.dataframe(filtered_data,hide_index=True)
|
|
|
|
|
st.markdown(filtered_data.to_html(escape=False), unsafe_allow_html=True)
|
|
|
|
|
# Convert DataFrame to CSV
|
|
|
|
|
csv = filtered_data.drop(columns=['Link']).to_csv(index=False)
|
|
|
|
|
|
|
|
|
|
st.write(f"Total Time Unit: {filtered_data['time'].sum()}")
|
|
|
|
|
|
|
|
|
|
# Create a download button with a custom file name
|
|
|
|
|
st.download_button(
|
|
|
|
|
label="Download CSV",
|
|
|
|
|
data=csv,
|
|
|
|
|
file_name=f"filtered_data_{selected_customer}.csv", # Custom file name
|
|
|
|
|
mime='text/csv',
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if start_date > end_date:
|
|
|
|
|
st.error("Error: End date must be greater than or equal to start date.")
|
|
|
|
|
else:
|
|
|
|
|
st.write("No data available for the selected filters.")
|
|
|
|
|
# Fetch filtered data from the database
|
|
|
|
|
filtered_data = get_filtered_users(selected_customer_id, start_date, end_date)
|
|
|
|
|
if not filtered_data.empty:
|
|
|
|
|
st.markdown(filtered_data.to_html(escape=False), unsafe_allow_html=True)
|
|
|
|
|
# Convert DataFrame to CSV
|
|
|
|
|
csv = filtered_data.drop(columns=['Link']).to_csv(index=False)
|
|
|
|
|
|
|
|
|
|
st.write(f"Total Time Unit: {filtered_data['time'].sum()}")
|
|
|
|
|
|
|
|
|
|
# Create a download button with a custom file name
|
|
|
|
|
st.download_button(
|
|
|
|
|
label="Download CSV",
|
|
|
|
|
data=csv,
|
|
|
|
|
file_name=f"filtered_data_{selected_customer}.csv", # Custom file name
|
|
|
|
|
mime='text/csv',
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
st.write("No data available for the selected filters.")
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
ticket_filter()
|
|
|
|
|
|