def print_timesheets(period: str, path: str, uids: List = []) -> None: for uid in uids: qry = (t.tracking_date.year()==int(period[:4])) &(t.tracking_date.month()==int(period[5:])) &(t.user_id==uid) rows = db(qry).select() tothours = tot_hours(rows)[:-3] data = rows.as_list() if False: import sys print(tothours) sys.exit() user = db(u.id==uid).select().first() pdf_file = os.path.join(path, f"{period[:2]}{period[5:]}-timesheet {user.last_name} {user.first_name}.pdf") cols = [30, 130, 170, 210, 250, 310, 350] rpt = Report(datasource = data, detailband = Band([ Element((cols[0], 0), ("Helvetica", 10), key = 'tracking_date', format = lambda x: x.strftime("%Y-%m-%d %a")), Element((cols[1], 0), ("Helvetica", 10), key = 'time_start', format = lambda x: x.strftime("%H:%M")), Element((cols[2], 0), ("Helvetica", 10), key = 'time_end', format = lambda x: x.strftime("%H:%M")), Element((cols[3], 0), ("Helvetica", 10), key = 'time_break', format = lambda x: x.strftime("%H:%M")), Element((cols[4], 0), ("Helvetica-Bold", 10), key = 'total_time', format = lambda x: x[:-3]), Element((cols[5], 0), ("Helvetica", 10), key = 'place'), # Element((cols[6], 0), ("Helvetica", 10), key = 'comments'), ])) rpt.pageheader = Band([ Element((cols[0], 0), ("Times-Bold", 18), text = f"{user.last_name} {user.first_name}"), Element((cols[0]+150, 7), ("Helvetica", 11), text = "timesheet"), Element((cols[0]+150+52, 0), ("Times-Bold", 18), text = f"{period}"), Element((cols[0]+150+52+125, 7), ("Helvetica", 11), text = "total hours: "), Element((cols[0]+150+52+125+60, 0), ("Times-Bold", 18), text = f"{tothours}"), Element((cols[0], 45), ("Helvetica", 11), text = "Date"), Element((cols[1], 45), ("Helvetica", 11), text = "Start"), Element((cols[2], 45), ("Helvetica", 11), text = "End"), Element((cols[3], 45), ("Helvetica", 11), text = "Break"), Element((cols[4], 45), ("Helvetica", 11), text = "Total"), Element((cols[5], 45), ("Helvetica", 11), text = "Place"), Rule((cols[0], 63), 6*72, thickness = 1), Element((cols[0], 67), ("Helvetica", 6), text = " "), ]) if False: rpt.pagefooter = Band([ Element((36, 16), ("Helvetica-Bold", 12), sysvar = "pagenumber", format = lambda x: "Page %d" % x), Element((300, 16), ("Helvetica-Bold", 12), text = f"{tothours}", align = "right") ]) canvas = Canvas(pdf_file, pagesize=A4) rpt.generate(canvas) canvas.save()
def print_timesheets(yyyy: str, mm:str, path: str, uids: List = []) -> None: for uid in uids: qry = (t.tracking_date.year()==int(yyyy)) &(t.tracking_date.month()==int(mm)) &(t.user_id==uid) rows = db(qry).select() tothours = tot_hours(rows)[:-3] data = rows.as_list() if False: import sys print(tothours) sys.exit() user = db(u.id==uid).select().first() pdf_file = Path(path) / f"{yyyy[2:4]}{mm}-timesheet {user.last_name} {user.first_name}.pdf" cols = [30, 130, 170, 210, 250, 310, 350] rpt = Report(datasource = data, detailband = Band([ Element((cols[0], 0), ("Helvetica", 10), key = 'tracking_date', format = lambda x: x.strftime("%Y-%m-%d %a")), Element((cols[1], 0), ("Helvetica", 10), key = 'time_start', format = lambda x: x.strftime("%H:%M")), Element((cols[2], 0), ("Helvetica", 10), key = 'time_end', format = lambda x: x.strftime("%H:%M")), Element((cols[3], 0), ("Helvetica", 10), key = 'time_break', format = lambda x: x.strftime("%H:%M")), Element((cols[4], 0), ("Helvetica-Bold", 10), key = 'total_time', format = lambda x: x[:-3]), Element((cols[5], 0), ("Helvetica", 10), key = 'place'), # Element((cols[6], 0), ("Helvetica", 10), key = 'comments'), ])) rpt.pageheader = Band([ Element((cols[0], 0), ("Times-Bold", 18), text = f"{user.last_name} {user.first_name}"), Element((cols[0]+150, 7), ("Helvetica", 11), text = "timesheet"), Element((cols[0]+150+52, 0), ("Times-Bold", 18), text = f"{yyyy}-{mm}"), Element((cols[0]+150+52+125, 7), ("Helvetica", 11), text = "total hours: "), Element((cols[0]+150+52+125+60, 0), ("Times-Bold", 18), text = f"{tothours}"), Element((cols[0], 45), ("Helvetica", 11), text = "Date"), Element((cols[1], 45), ("Helvetica", 11), text = "Start"), Element((cols[2], 45), ("Helvetica", 11), text = "End"), Element((cols[3], 45), ("Helvetica", 11), text = "Break"), Element((cols[4], 45), ("Helvetica", 11), text = "Total"), Element((cols[5], 45), ("Helvetica", 11), text = "Place"), Rule((cols[0], 63), 6*72, thickness = 1), Element((cols[0], 67), ("Helvetica", 6), text = " "), ]) canvas = Canvas(str(pdf_file.absolute(),) pagesize=A4) rpt.generate(canvas) canvas.save()
def print_summary(yyyy: str, mm: str, path: str, uids: List = []) -> None: data = [] for uid in uids: user = db(u.id == uid).select().first() name = f'{user.last_name} {user.first_name}' qry = (t.tracking_date.year() == int(yyyy)) & ( t.tracking_date.month() == int(mm)) & (t.user_id == uid) rows = db(qry).select() data.append({"name": name, "hours": tot_hours(rows)[:-3]}) data = sorted(data, key=lambda x: x['name']) txt_file = Path(path) / f"{yyyy[2:4]}{mm}-timesheet summary.txt" pdf_file = Path(path) / f"{yyyy[2:4]}{mm}-timesheet summary.pdf" with open(txt_file, "w") as f: f.write(f"Summary Timesheet {yyyy}-{mm}\n\n") for dt in data: f.write(f'{dt["name"]}: \t{dt["hours"]}\n')
def print_summary(period: str, path: str, uids: List = []) -> None: data = [] for uid in uids: user = db(u.id==uid).select().first() name = f'{user.last_name} {user.first_name}' qry = (t.tracking_date.year()==period[:4]) &(t.tracking_date.month()==int(period[5:])) &(t.user_id==uid) rows = db(qry).select() data.append({"name": name, "hours": tot_hours(rows)[:-3]}) data = sorted(data, key=lambda x: x['name']) txt_file = os.path.join(path, f"{period[:2]}{period[5:]}-timesheet summary.txt") pdf_file = os.path.join(path, f"{period[:2]}{period[5:]}-timesheet summary.pdf") with open(txt_file, "w") as f: f.write(f"Summary Timesheet {period}\n\n") for dt in data: f.write(f'{dt["name"]}: \t{dt["hours"]}\n') generate_pdf_summary(data, period, pdf_file)