Fix account status queries.

This commit is contained in:
Alexis Lahouze 2015-07-15 18:15:38 +02:00
parent 6a2d796cdb
commit 53cdeabc90

View File

@ -14,14 +14,11 @@
You should have received a copy of the GNU Affero General Public License You should have received a copy of the GNU Affero General Public License
along with Accountant. If not, see <http://www.gnu.org/licenses/>. along with Accountant. If not, see <http://www.gnu.org/licenses/>.
""" """
from datetime import date from sqlalchemy import func, case, literal_column
from sqlalchemy import func, case, cast
from sqlalchemy.orm import validates from sqlalchemy.orm import validates
from accountant import db from accountant import db
from .entries import Entry
from .operations import Operation from .operations import Operation
@ -40,24 +37,53 @@ class Account(db.Model):
cls.id.label("id"), cls.id.label("id"),
cls.name.label("name"), cls.name.label("name"),
cls.authorized_overdraft.label("authorized_overdraft"), cls.authorized_overdraft.label("authorized_overdraft"),
func.sum(Entry.value).label("future"), func.sum(Operation.value).label("future"),
func.sum(case([(Entry.pointed, Entry.value,)], func.sum(
else_=cast(0, db.Numeric(15, 2)))).label("pointed"), case(
func.sum(case([(Entry.operation_date < func.now(), Entry.value,)], [(Operation.pointed, Operation.value)],
else_=cast(0, db.Numeric(15, 2)))).label("current") else_=0
).outerjoin(Entry).group_by(cls.id).order_by(cls.id) )
).label("pointed"),
func.sum(
case(
[(Operation.operation_date <= func.current_date(),
Operation.value)],
else_=0
)
).label("current")
).outerjoin(Operation).group_by(cls.id).order_by(cls.id)
return query return query
@classmethod @classmethod
def get(cls, session, account, begin=date.today(), end=None): def get(cls, session, account, begin=None, end=None):
if isinstance(account, int) or isinstance(account, str): if isinstance(account, int) or isinstance(account, str):
account_id = account account_id = account
else: else:
account_id = account.id account_id = account.id
end = end if end else begin status_query = session.query(
Operation.account_id,
func.sum(Operation.value).label("future"),
func.sum(
case(
[(Operation.pointed,
Operation.value)],
else_=0
)
).label("pointed"),
func.sum(
case(
[(Operation.operation_date <= func.current_date(),
Operation.value)],
else_=0
)
).label("current"),
).group_by(
Operation.account_id
).subquery()
if begin and end:
balance_query = session.query( balance_query = session.query(
Operation.account_id, Operation.account_id,
func.sum( func.sum(
@ -80,25 +106,6 @@ class Account(db.Model):
Operation.account_id Operation.account_id
).subquery() ).subquery()
status_query = session.query(
cls.id,
func.sum(Operation.value).label("future"),
func.sum(
case(
[(Operation.pointed, Operation.value,)],
else_=0
)
).label("pointed"),
func.sum(
case(
[(Operation.operation_date <= str(end), Operation.value,)],
else_=0
)
).label("current"),
).group_by(
cls.id
).subquery()
query = session.query( query = session.query(
cls.id, cls.id,
cls.name, cls.name,
@ -110,12 +117,28 @@ class Account(db.Model):
func.coalesce(balance_query.c.revenues, 0).label('revenues'), func.coalesce(balance_query.c.revenues, 0).label('revenues'),
func.coalesce(balance_query.c.balance, 0).label('balance'), func.coalesce(balance_query.c.balance, 0).label('balance'),
).outerjoin( ).outerjoin(
status_query, status_query.c.id == cls.id status_query, status_query.c.account_id == cls.id
).outerjoin( ).outerjoin(
balance_query, balance_query.c.account_id == cls.id balance_query, balance_query.c.account_id == cls.id
).filter( ).filter(
cls.id == account_id cls.id == account_id
) )
else:
query = session.query(
cls.id,
cls.name,
cls.authorized_overdraft,
func.coalesce(status_query.c.current, 0).label('current'),
func.coalesce(status_query.c.pointed, 0).label('pointed'),
func.coalesce(status_query.c.future, 0).label('future'),
literal_column("0").label('expenses'),
literal_column("0").label('revenues'),
literal_column("0").label('balance'),
).outerjoin(
status_query, status_query.c.account_id == cls.id
).filter(
cls.id == account_id
)
return query.one() return query.one()