From: Vincent Le Gallic Date: Sun, 15 Feb 2015 16:38:21 +0000 (+0100) Subject: [depenses] Option bymonth working X-Git-Url: http://gitweb.pimeys.fr/?a=commitdiff_plain;h=77b38da0ad904934e128bfc461481b11eece3d50;p=scripts-20-100.git [depenses] Option bymonth working --- diff --git a/bde/stats/depenses.py b/bde/stats/depenses.py index 9246e17..bff85ea 100755 --- a/bde/stats/depenses.py +++ b/bde/stats/depenses.py @@ -25,16 +25,16 @@ create_temp_table = """ INTO past FROM transactions WHERE valide AND %%(idbde)s in (emetteur, destinataire) - GROUP BY annee%(groupby_month)s + GROUP BY annee%(comma_month)s ; """ select_month = """extract(month FROM date) AS mois,""" -groupby_month = """, month""" +comma_month = """, mois""" display_all = """ SELECT * FROM past - ORDER BY annee + ORDER BY annee%(comma_month)s ; """ @@ -53,24 +53,29 @@ def get_cursor(): def get_depenses(idbde, bymonth=False): """Récupère toutes les dépenses/gains du compte ``idbde``""" con, cur = get_cursor() - modifiers = {"select_month" : "", "groupby_month" : ""} + modifiers = {"select_month" : "", "comma_month" : ""} params = {"idbde" : idbde} if bymonth: - params["select_month"] = select_month - params["groupby_month"] = groupby_month + modifiers["select_month"] = select_month + modifiers["comma_month"] = comma_month cur.execute(create_temp_table % modifiers, params) - cur.execute(display_all) + cur.execute(display_all % modifiers) all = cur.fetchall() cur.execute(display_sum) tot = cur.fetchall()[0] return (all, tot) -def display_depenses(idbde, all, tot): +def display_depenses(idbde, all, tot, bymonth=False): """Pretty-printing des résultats.""" - print "Compte %s:\nannee\tdepenses\tgains" % (idbde,) + month = "\tmois" if bymonth else "" + print "Compte %s:\nannee%s\tdepenses\tgains" % (idbde, month) for li in all: - li = [int(li[0]), li[1], li[2]] - print "%s\t%s\t\t%s" % tuple(li) + if bymonth: + li = [int(li[0]), int(li[1]), li[2], li[3]] + print "%s\t%s\t%s\t\t%s" % tuple(li) + else: + li = [int(li[0]), li[1], li[2]] + print "%s\t%s\t\t%s" % tuple(li) print "\nTotal :\n\tdepenses\tgains" print "\t%s\t\t%s\n" % tuple(tot) @@ -81,5 +86,5 @@ if __name__ == "__main__": args = parser.parse_args() for idbde in args.idbdes: - all, tot = get_depenses(idbde) - display_depenses(idbde, all, tot) + all, tot = get_depenses(idbde, bymonth=args.months) + display_depenses(idbde, all, tot, bymonth=args.months)