209 lines
5.7 KiB
Python
209 lines
5.7 KiB
Python
"""Module containing operation related models."""
|
|
|
|
# vim: set tw=80 ts=4 sw=4 sts=4:
|
|
|
|
from datetime import date
|
|
|
|
import arrow
|
|
|
|
from . import db
|
|
|
|
|
|
# pylint: disable=no-member,invalid-name,too-many-arguments
|
|
# pylint: disable=too-few-public-methods,too-many-instance-attributes
|
|
class Operation(db.Model):
|
|
"""Class used to handle operations."""
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
operation_date = db.Column(
|
|
db.Date,
|
|
nullable=False,
|
|
default=date.today,
|
|
server_default=db.func.current_date(),
|
|
index=True
|
|
)
|
|
label = db.Column(db.String(500), nullable=False)
|
|
value = db.Column(db.Numeric(15, 2), nullable=False)
|
|
|
|
scheduled_operation_id = db.Column(
|
|
db.Integer,
|
|
db.ForeignKey('scheduled_operation.id'),
|
|
index=True
|
|
)
|
|
|
|
scheduled_operation = db.relationship(
|
|
"ScheduledOperation", backref=db.backref('operations', lazy="dynamic")
|
|
)
|
|
|
|
account_id = db.Column(db.Integer, db.ForeignKey('account.id'), index=True)
|
|
|
|
account = db.relationship(
|
|
'Account', backref=db.backref('operations', lazy="dynamic")
|
|
)
|
|
|
|
category = db.Column(db.String(100), nullable=True, index=True)
|
|
|
|
pointed = db.Column(
|
|
db.Boolean,
|
|
nullable=False,
|
|
default=False,
|
|
server_default=db.false()
|
|
)
|
|
|
|
confirmed = db.Column(
|
|
db.Boolean,
|
|
nullable=False,
|
|
default=True,
|
|
server_default=db.true()
|
|
)
|
|
|
|
canceled = db.Column(
|
|
db.Boolean,
|
|
nullable=False,
|
|
default=False,
|
|
server_default=db.false()
|
|
)
|
|
|
|
def __init__(self, label, value, account_id, operation_date=None,
|
|
category=None, pointed=False, confirmed=True, canceled=False,
|
|
scheduled_operation_id=None):
|
|
self.operation_date = operation_date
|
|
self.label = label
|
|
self.value = value
|
|
self.category = category
|
|
self.account_id = account_id
|
|
self.scheduled_operation_id = scheduled_operation_id
|
|
self.pointed = pointed
|
|
self.confirmed = confirmed
|
|
self.canceled = canceled
|
|
|
|
@classmethod
|
|
def query(cls):
|
|
"""Return query for this class."""
|
|
|
|
query = db.session.query(
|
|
cls
|
|
)
|
|
|
|
return query
|
|
|
|
@classmethod
|
|
def query_with_balance(cls, account_id, begin=None, end=None):
|
|
"""Get operations with cumulated balance for a speciific account,
|
|
optionally for a specific time period."""
|
|
query = db.session.query(
|
|
*cls.__table__.columns.keys(),
|
|
db.func.sum(
|
|
cls.value
|
|
).filter(
|
|
db.not_(cls.canceled)
|
|
).over(
|
|
partition_by=[cls.account_id],
|
|
order_by=[
|
|
cls.operation_date,
|
|
db.desc(cls.value),
|
|
db.desc(cls.label),
|
|
cls.id
|
|
]
|
|
).label("balance")
|
|
).filter(
|
|
cls.account_id == account_id
|
|
)
|
|
|
|
if begin:
|
|
base_query = query.subquery()
|
|
|
|
query = db.session.query(
|
|
base_query
|
|
).filter(
|
|
cls.operation_date >= str(begin)
|
|
).join(
|
|
cls, base_query.c.id == cls.id
|
|
)
|
|
|
|
if end:
|
|
query = query.filter(cls.operation_date <= str(end))
|
|
|
|
query = query.order_by(
|
|
cls.operation_date,
|
|
db.desc(cls.value),
|
|
db.desc(cls.label),
|
|
cls.id
|
|
)
|
|
|
|
return query
|
|
|
|
@classmethod
|
|
def query_daily_balances(cls, account, begin=None, end=None):
|
|
"""Get expenses, revenues, income and balance per day for a specific
|
|
range and a specific account."""
|
|
if isinstance(account, (int, str)):
|
|
account_id = account
|
|
else:
|
|
account_id = account.id
|
|
|
|
end = end if end else arrow.now().ceil('month').date()
|
|
|
|
query = db.session.query(
|
|
cls.operation_date,
|
|
db.func.coalesce(
|
|
db.func.sum(
|
|
cls.value
|
|
).filter(
|
|
db.func.sign(cls.value) == -1
|
|
).over(
|
|
partition_by=[cls.account_id, cls.operation_date],
|
|
),
|
|
0
|
|
).label("expenses"),
|
|
db.func.coalesce(
|
|
db.func.sum(
|
|
cls.value
|
|
).filter(
|
|
db.func.sign(cls.value) == 1
|
|
).over(
|
|
partition_by=[cls.account_id, cls.operation_date],
|
|
),
|
|
0
|
|
).label("revenues"),
|
|
db.func.coalesce(
|
|
db.func.sum(
|
|
cls.value
|
|
).over(
|
|
partition_by=[cls.account_id, cls.operation_date],
|
|
)
|
|
).label("income"),
|
|
db.func.coalesce(
|
|
db.func.sum(
|
|
cls.value
|
|
).over(
|
|
partition_by=cls.account_id,
|
|
order_by=cls.operation_date
|
|
)
|
|
).label("balance")
|
|
).distinct(
|
|
).order_by(
|
|
cls.operation_date
|
|
).filter(
|
|
cls.account_id == account_id
|
|
)
|
|
|
|
if begin:
|
|
base_query = query.subquery()
|
|
|
|
query = db.session.query(
|
|
base_query
|
|
).filter(
|
|
base_query.c.operation_date >= str(begin)
|
|
).order_by(
|
|
base_query.c.operation_date
|
|
)
|
|
|
|
if end:
|
|
query = query.filter(query.c.operation_date <= str(end))
|
|
|
|
elif end:
|
|
query = query.filter(cls.operation_date <= str(end))
|
|
|
|
return query
|