From 7d6cfc9b64ee0b18fc2b647c0fce8aa36b70b3be Mon Sep 17 00:00:00 2001 From: WorldTeacher <41587052+WorldTeacher@users.noreply.github.com> Date: Fri, 2 Aug 2024 08:44:53 +0200 Subject: [PATCH] create report every seven days --- src/utils/createReport.py | 61 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/utils/createReport.py diff --git a/src/utils/createReport.py b/src/utils/createReport.py new file mode 100644 index 0000000..0c94f16 --- /dev/null +++ b/src/utils/createReport.py @@ -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