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
along with Accountant. If not, see <http://www.gnu.org/licenses/>.
"""
from datetime import date
from sqlalchemy import func, case, cast
from sqlalchemy import func, case, literal_column
from sqlalchemy.orm import validates
from accountant import db
from .entries import Entry
from .operations import Operation
@ -40,24 +37,53 @@ class Account(db.Model):
cls.id.label("id"),
cls.name.label("name"),
cls.authorized_overdraft.label("authorized_overdraft"),
func.sum(Entry.value).label("future"),
func.sum(case([(Entry.pointed, Entry.value,)],
else_=cast(0, db.Numeric(15, 2)))).label("pointed"),
func.sum(case([(Entry.operation_date < func.now(), Entry.value,)],
else_=cast(0, db.Numeric(15, 2)))).label("current")
).outerjoin(Entry).group_by(cls.id).order_by(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 <= func.current_date(),
Operation.value)],
else_=0
)
).label("current")
).outerjoin(Operation).group_by(cls.id).order_by(cls.id)
return query
@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):
account_id = account
else:
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(
Operation.account_id,
func.sum(
@ -80,25 +106,6 @@ class Account(db.Model):
Operation.account_id
).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(
cls.id,
cls.name,
@ -110,12 +117,28 @@ class Account(db.Model):
func.coalesce(balance_query.c.revenues, 0).label('revenues'),
func.coalesce(balance_query.c.balance, 0).label('balance'),
).outerjoin(
status_query, status_query.c.id == cls.id
status_query, status_query.c.account_id == cls.id
).outerjoin(
balance_query, balance_query.c.account_id == cls.id
).filter(
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()