-#!/bin/bash
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
-# Affiche les dépenses totales d'un compte
+"""Affiche les dépenses totales d'un compte par année + total"""
-psql bde -c "SELECT sum(montant*quantite) FROM transactions WHERE valide AND emetteur=$1;"
+create_temp_table = """
+ SELECT * --, sum(gains), sum(depenses)
+ INTO past
+ FROM
+ (SELECT extract(year FROM date) AS annee,
+ CAST(sum(
+ CASE WHEN emetteur=$1 THEN montant*quantite
+ ELSE 0
+ END
+ )/100.0 AS numeric(10,2)) AS depenses,
+ CAST(sum(
+ CASE WHEN destinataire=$1 THEN montant*quantite
+ ELSE 0
+ END
+ )/100.0 AS numeric(10,2)) AS gains
+ FROM transactions
+ WHERE valide AND $1 in (emetteur, destinataire)
+ GROUP BY annee) AS pif
+ ;
+"""
+
+display_all = """ SELECT * FROM past
+ ;
+"""
+
+display_sum = """ SELECT sum(gains) AS gains, sum(depenses) AS depenses
+ FROM past
+ ;
+"""