Skip to content

Commit

Permalink
add free days to students
Browse files Browse the repository at this point in the history
  • Loading branch information
synsi23b committed Jan 29, 2023
1 parent 7f08361 commit 334da24
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
14 changes: 9 additions & 5 deletions export_student_journal.py → export_all_monthly_journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@

def main():
outfolder = Path(get_kimai_datafolder()) / "export"
gen_start = date(2022, 11, 23)
gen_end = date.today() - timedelta(days=1)
gen_start = date(2022, 12, 19)
#gen_end = date.today() - timedelta(days=1)
gen_end = date(2023, 1, 22)
empl = Angestellter.get_all_active("ANGESTELLTER")
stud = Angestellter.get_all_active("WERKSTUDENT")
schueler = Angestellter.get_all_active("SCHUELERAUSHILFE")
for ma in empl + stud + schueler:
if ma.export_monthly_journal(gen_start, gen_end, outfolder):
outfile = THIS_LOCATION / f"export/journal_{gen_start}_{gen_end}_{ma._alias}.pdf".replace(" ", "_")
copy(f"/var/www/kimai2/var/data/export/{date.today().strftime('%y%m%d')}-Leap_in_Time.pdf", str(outfile))
for invoice in ma.export_monthly_journal(gen_start, gen_end, outfolder):
dst = THIS_LOCATION / f"export/journal_{gen_start}_{gen_end}_{ma._alias}.pdf".replace(" ", "_")
dst = str(dst)
print(f"copy from {invoice} to {dst}")
logging.info(f"copy from {invoice} to {dst}")
copy(invoice, dst)


if __name__ == "__main__":
Expand Down
26 changes: 19 additions & 7 deletions kimai_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ def fill_missing_workday(self, workday:date):
logging.info(f"Insert free day for user {self._email}")
db_util.insert_timesheet(self._id, self._freeday_acti, self._project, start, end, "", 0, False)


def fill_missing_freeday(self, workday:date):
if self.has_not_worked(workday):
start = end = datetime(workday.year, workday.month, workday.day, tzinfo=UTC)
logging.info(f"Insert free day for user {self._email}")
db_util.insert_timesheet(self._id, self._freeday_acti, self._project, start, end, "", 0, False)


def _float_to_dt(self, day, value):
hours = int(value)
Expand Down Expand Up @@ -355,11 +362,11 @@ def update_flextime(self, day):
db_util.insert_timesheet(self._id, self._flex_acti, self._project, start, start, flexdesc, 0, True)
self.update_flextime(flex_sheets[0].date_tz)

def export_monthly_journal(self, start:date, end:date, outpath:Path):
def export_monthly_journal(self, start:date, end:date, outfolder:Path):
res = []
if db_util.check_timesheet_exists_range(self._id, start, end) > 0:
export_invoice(self._user, start, end, "monatsjournal", outpath)
return True
return False
res = export_invoice(self._user, start, end, "monatsjournal", outfolder)
return res


class Worker:
Expand Down Expand Up @@ -499,7 +506,7 @@ def get_gen_projects():
return [AutogenProjekt(*x) for x in db_util.get_generate_projects()]


def export_invoice(user:str, start:date, end:date, template:str, outpath:str|Path) -> str:
def export_invoice(user:str, start:date, end:date, template:str, outfolder:Path) -> str:
# Usage:
# kimai:invoice:create [options]

Expand All @@ -520,10 +527,15 @@ def export_invoice(user:str, start:date, end:date, template:str, outpath:str|Pat
# --preview[=PREVIEW] Absolute path for a rendered preview of the invoice, which will neither be saved nor the items be marked as exported.
# --preview-unique Adds a unique part to the filename of the generated invoice preview file, so there is no chance that they get overwritten on same project name.
#./bin/console kimai:invoice:create --user=presley85 --template=myinvoi --exported=all --by-customer
res = subprocess.run([get_console(), "kimai:invoice:create", f"--user={user}", f"--start={start.strftime('%Y-%m-%d')}", f"--end={end.strftime('%Y-%m-%d')}", f"--template={template}", f"--by-customer", "--exported=all", f"--preview={outpath}"], capture_output=True)
res = subprocess.run([get_console(), "kimai:invoice:create", f"--user={user}", f"--start={start.strftime('%Y-%m-%d')}", f"--end={end.strftime('%Y-%m-%d')}", f"--template={template}", f"--by-customer", "--exported=all", f"--preview={str(outfolder)}", "--preview-unique"], capture_output=True)
if res.returncode != 0:
raise RuntimeError(f"{res.stderr}\n\n{res.stdout}")
print(res.stdout)
output = str(res.stdout, "utf-8")
logging.info(output)
filelist = re.findall("\|\s(.+?\.pdf)\s\|", output)
for f in filelist:
logging.info(f)
return filelist


if __name__ == "__main__":
Expand Down
18 changes: 18 additions & 0 deletions nightly_timesheet_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ def update_breaktimes(employees:list[Angestellter], day:date):
ma.update_breaktime(day)


def insert_free_days_students(employees:list[Angestellter], day:date):
for ma in employees:
ma.fill_missing_freeday(day)


def stop_overnight_timesheets(employees:list[Angestellter], day:date):
mail_queue = []
for ma in employees:
Expand Down Expand Up @@ -57,6 +62,7 @@ def run_corrections_for_yesterday():
insert_public_holidays(empl + stud + schueler, day)
insert_auto_worktime(empl + stud + schueler, day)
stop_overnight_timesheets(empl + stud + schueler, day)
insert_free_days_students(stud + schueler, day)
check_not_worked(empl, day)
update_breaktimes(empl + stud + schueler, day)

Expand Down Expand Up @@ -105,11 +111,23 @@ def run_past_corrections_for_every_active_user():
day = day + timedelta(days=1)


def run_past_student_free_days_until(day:date):
empl = Angestellter.get_all_active("WERKSTUDENT")
empl += Angestellter.get_all_active("SCHUELERAUSHILFE")
for ma in empl:
# get user registering date
corday = ma.get_first_record_date()
while corday < day:
ma.fill_missing_freeday(corday)
corday += timedelta(days=1)


if __name__ == "__main__":
thisfile = Path(__file__)
logging.basicConfig(filename=str(thisfile.parent.parent.resolve() / f"kimai2_autohire_{thisfile.stem}.log"),
format="%(asctime)s %(levelname)s %(message)s", level=logging.INFO)

#run_past_corrections_for_every_active_user()
#run_past_breaktimes()
#run_past_student_free_days_until(date(2023,1,28))
run_corrections_for_yesterday()

0 comments on commit 334da24

Please sign in to comment.