create report every seven days
This commit is contained in:
61
src/utils/createReport.py
Normal file
61
src/utils/createReport.py
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
import os
|
||||||
|
from prettytable import PrettyTable
|
||||||
|
from src.logic import Database
|
||||||
|
from src.utils import stringToDate
|
||||||
|
import sqlite3 as sql
|
||||||
|
from PyQt6.QtCore import QDate
|
||||||
|
from src import config
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
# query all loans that happened in the last 7 days
|
||||||
|
def generate_report():
|
||||||
|
'''Generate an excel report for all actions that happened in the last seven days
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: a string represeting the generated table
|
||||||
|
'''
|
||||||
|
db = Database()
|
||||||
|
path = db.db_path
|
||||||
|
year = datetime.datetime.now().year
|
||||||
|
week = datetime.datetime.now().isocalendar()[1]
|
||||||
|
if not os.path.exists(config.report.path):
|
||||||
|
os.makedirs(config.report.path)
|
||||||
|
report_path = os.path.join(config.report.path, f"report_{year}_{week}.tsv")
|
||||||
|
day = QDate.currentDate().addDays(-7).toString("yyyy-MM-dd")
|
||||||
|
query = f"""SELECT * FROM loans WHERE loan_date >= '{day}';"""
|
||||||
|
# print(query)
|
||||||
|
|
||||||
|
colnames = ["UserId", "Title", "Action", "Datum"]
|
||||||
|
table = PrettyTable(colnames)
|
||||||
|
table.align[colnames[0]] = "l"
|
||||||
|
table.align[colnames[1]] = "l"
|
||||||
|
table.align[colnames[2]] = "l"
|
||||||
|
|
||||||
|
with sql.connect(path) as conn:
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute(query)
|
||||||
|
loans = cursor.fetchall()
|
||||||
|
|
||||||
|
for loan in loans:
|
||||||
|
loan_action = "Ausleihe" if loan[5] == 0 else "Rückgabe"
|
||||||
|
loan_action_date = stringToDate(
|
||||||
|
loan[3] if loan[5] == 0 else loan[6]
|
||||||
|
).toString("dd.MM.yyyy")
|
||||||
|
table.add_row(
|
||||||
|
[
|
||||||
|
loan[1],
|
||||||
|
db.getMedia(loan[2]).title,
|
||||||
|
loan_action,
|
||||||
|
loan_action_date,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
# # print(table)
|
||||||
|
# # wruitng the table to a file
|
||||||
|
# with open("report.txt", "w", encoding="utf-8") as f:
|
||||||
|
# f.write(str(table))
|
||||||
|
|
||||||
|
tsv_table = table.get_csv_string().replace(",", "\t")
|
||||||
|
# write the file
|
||||||
|
with open(report_path, "w", encoding="utf-8") as f:
|
||||||
|
f.write(tsv_table)
|
||||||
|
return table
|
||||||
Reference in New Issue
Block a user