diff --git a/accountant/models/accounts.py b/accountant/models/accounts.py index 2db5157..3f63fa5 100644 --- a/accountant/models/accounts.py +++ b/accountant/models/accounts.py @@ -53,22 +53,20 @@ class Account(db.Model): Operation.account_id == self.id ).one() - def flow(self, begin, end): - """Return the flow for a specific time period.""" + def income(self, begin, end): + """Return the income for a specific time period.""" query = db.session.query( db.func.sum( - db.case( - [(db.func.sign(Operation.value) == -1, Operation.value)], - else_=0 - ) + Operation.value + ).filter( + db.func.sign(Operation.value) == -1 ).label("expenses"), db.func.sum( - db.case( - [(db.func.sign(Operation.value) == 1, Operation.value)], - else_=0 - ) + Operation.value + ).filter( + db.func.sign(Operation.value) == 1 ).label("revenues"), - db.func.sum(Operation.value).label("balance") + db.func.sum(Operation.value).label("income") ).filter( Operation.account_id == self.id, ) diff --git a/accountant/views/accounts.py b/accountant/views/accounts.py index b9cab8e..83e5ba8 100644 --- a/accountant/views/accounts.py +++ b/accountant/views/accounts.py @@ -65,7 +65,10 @@ category_model = ns.model('Category', { description='Total amount of expenses for the category'), 'revenues': fields.Float( readonly=True, - description='Total amount of revenues for the category') + description='Total amount of revenues for the category'), + 'income': fields.Float( + readonly=True, + description='Total income for the category') }) # OHLC model. @@ -238,24 +241,25 @@ class AccountResource(Resource): return None, 204 -@ns.route('//solds') +@ns.route('//balances') @ns.doc( security='apikey', params={ 'account_id': 'Id of the account to manage' }, responses={ - 200: ('OK', solds_model), + 200: ('OK', balances_model), 401: 'Unauthorized', 404: 'Account not found' }) -class SoldsResource(Resource): - """Resource to expose solds.""" +class BalancesResource(Resource): + """Resource to expose current, pointed and future balances.""" - @ns.marshal_with(solds_model) + @ns.marshal_with(balances_model) @jwt_required def get(self, account_id): - """Get solds for a specific account and date range.""" + """Get current, pointed and future balances for a specific account and + date range.""" account = Account.query().get(account_id) @@ -296,7 +300,7 @@ class BalanceResource(Resource): # Note: if we don't pass the code, the result is seen as a tuple and # causes error on marshalling. - return account.flow(**data), 200 + return account.income(**data), 200 @ns.route("//category")