Blame view

app/db_mgr.py 1.89 KB
8b3ab81d   hitier   All charges now i...
1
2
from pprint import pprint

1aeda847   hitier   Two db request wr...
3
4
5
6
from app.models import db, Agent, Charge


def agents():
e5449e3e   hitier   Fix wrong agents ...
7
8
9
10
11
12
13
14
15
    sql_txt = """
            select a.id, a.firstname, a.secondname, sum(tc.charge_rate) as total_charge
            from agent as a left join
               ( select c.agent_id, c.charge_rate from charge c )
            tc
            on a.id = tc.agent_id
            group by a.id
            order by total_charge desc;
            """
2cb0a345   hitier   Add 2 columns in ...
16
17
    all_agents = db.session.execute(sql_txt).fetchall()

1aeda847   hitier   Two db request wr...
18
19
20
    return all_agents


22c5ff60   hitier   New stacked_charg...
21
22
23
24
25
26
27
28
29
def stacked_charges_by_agent(agent_id):
    all_charges = {}
    for (period_id, period_name) in db.session.execute("select id, name from period order by id"):
        charge_by_project_req = """select  p.name, c.charge_rate from project p left join
                                    charge c on p.id = c.project_id and agent_id = {} and period_id={}
                                        order by p.id""".format(agent_id, period_id)
        period_charge = {}
        for project_name, project_rate in db.session.execute(charge_by_project_req):
            period_charge[project_name] = project_rate
e5449e3e   hitier   Fix wrong agents ...
30
        all_charges[period_name] = period_charge
22c5ff60   hitier   New stacked_charg...
31
32
33
34
35

    pprint(all_charges)
    return all_charges


1aeda847   hitier   Two db request wr...
36
37
def charges_by_agent(agent_id):
    # all_charges = db.session.query(Charge).all()
8b3ab81d   hitier   All charges now i...
38
39
    periods = db.session.execute("select name from period")
    sql_txt = "select p.name, sum(charge_rate)  from charge inner join period p on charge.period_id = p.id\
71b91c22   hitier   New json route fo...
40
41
                where agent_id={} group by period_id order by p.name" \
        .format(agent_id)
1aeda847   hitier   Two db request wr...
42
    request = db.session.execute(sql_txt)
8b3ab81d   hitier   All charges now i...
43
44
45
46
47
48
49
50
    all_charges = []
    charges_list = [(period, charge) for (period, charge) in request.fetchall()]
    charges_dict = dict(charges_list)
    for (p,) in periods:
        if p in charges_dict.keys():
            all_charges.append([p, charges_dict[p]])
        else:
            all_charges.append([p, 0])
71b91c22   hitier   New json route fo...
51
    return all_charges