Blame view

app/db_mgr.py 1.09 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():
2cb0a345   hitier   Add 2 columns in ...
7
8
9
10
11
12
    sql_txt = """select a.id, a.firstname, a.secondname, sum(c.charge_rate) as total_charge
                from agent as a
                         inner join charge c on a.id = c.agent_id
                group by agent_id;"""
    all_agents = db.session.execute(sql_txt).fetchall()

1aeda847   hitier   Two db request wr...
13
14
15
16
17
    return all_agents


def charges_by_agent(agent_id):
    # all_charges = db.session.query(Charge).all()
8b3ab81d   hitier   All charges now i...
18
19
    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...
20
21
                where agent_id={} group by period_id order by p.name" \
        .format(agent_id)
1aeda847   hitier   Two db request wr...
22
    request = db.session.execute(sql_txt)
8b3ab81d   hitier   All charges now i...
23
24
25
26
27
28
29
30
    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...
31
    return all_charges