47 lines
1.8 KiB
Python
47 lines
1.8 KiB
Python
"""
|
|
This file is part of Accountant.
|
|
|
|
Accountant is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
Accountant is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
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 sqlalchemy import func, case, cast
|
|
|
|
from accountant import db
|
|
|
|
from .entries import Entry
|
|
|
|
|
|
class Account(db.Model):
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
name = db.Column(db.String(200), nullable=False)
|
|
authorized_overdraft = db.Column(db.Integer, nullable=True, default=0)
|
|
|
|
def __init__(self, name, authorized_overdraft):
|
|
self.name = name
|
|
self.authorized_overdraft = authorized_overdraft
|
|
|
|
@classmethod
|
|
def get_accounts(cls, session):
|
|
query = session.query(
|
|
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)
|
|
|
|
return query
|