summaryrefslogtreecommitdiff
path: root/maconomy_hours.py
blob: 214a0e7b6c76a425dcac672cb41271ef0fa6beca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from maconomy import cli, create_db, TimeRegistrationRepository, Mailer
from maconomy.views import EmployeeEmailView, ManagerEmailView, CEOEmailView
from collections import defaultdict

def main(config, options):
    db = create_db(config)
    timereg_repo = TimeRegistrationRepository(db)
    timesheets = timereg_repo.all_active()
    mailer = Mailer(config) if not options.dry else None

    if options.manager:
        manager(timesheets, config, mailer)
    elif options.ceo:
        ceo(timesheets, config, mailer)
    elif options.summary:
        summary(timesheets)
    else:
        normal(timesheets, config, mailer)

    # Close stuff
    mailer.close()
    timereg_repo.close()
    db.close()


def normal(timesheets, config, mailer):
    view = EmployeeEmailView(config)
    for timesheet in timesheets:
        if not timesheet.is_submitted():
            mail = view.render(timesheet) 
            subject = u"Timesheet reminder for {}".format(timesheet.employee)
            to = timesheet.employee.email
            if mailer:
                if to and to.strip():
                    mailer.send(to, subject, mail)
                else:
                    print u"No email for: {}".format(timesheet.employee)
            else:
                print subject
                print mail

def manager(timesheets, config, mailer):
    view = ManagerEmailView(config)
    # extract employees for manager email lookup
    employees = dict([(t.employee.id, t.employee) for t in timesheets])
    per_manager = defaultdict(list)
    # filter timesheets per manager
    for timesheet in [t for t in timesheets if need_manager_mail(t)]:
        manager_id = timesheet.approver
        per_manager[manager_id].append(timesheet)
    # send mails to managers
    for manager_id, relevant_timesheets in per_manager.items():
        mail = view.render(relevant_timesheets)
        subject = "Warning: Timesheets overdue for {} employees".format(len(relevant_timesheets))
        manager = employees.get(manager_id)
        to = manager.email if manager else None
        if mailer:
            mailer.send(to, subject, mail)
        else:
            print "TO: {}".format(to)
            print subject
            print mail


def ceo(timesheets, config, mailer):
    to = config.get("mail", "ceo")
    # Filter only "bad" entries
    relevant = [t for t in timesheets if need_manager_mail(t)]
    view = CEOEmailView()
    mail = view.render(relevant)
    subject = "Warning: Timesheet overdue for {} employees".format(len(relevant))
    if mailer:
        mailer.send(to, subject, mail)
    else:
        print mail

def need_manager_mail(timesheet):
    return not timesheet.is_submitted() or not timesheet.is_approved()

def summary(timesheets):
    for timesheet in timesheets:
        if timesheet.is_missing():
            print u"[Not created] {}".format(timesheet.employee)
        elif not timesheet.is_submitted():
            print u"[Unsubmitted] {}".format(timesheet.employee)
        elif not timesheet.is_approved():
            print u"[Not approved] {}".format(timesheet.employee)

if __name__ == '__main__':
    args = cli.parse()
    config = cli.load_config(args.config)
    main(config, args)