2022-12-24 12:31:45 +11:00
|
|
|
# DrCr: Web-based double-entry bookkeeping framework
|
|
|
|
# Copyright (C) 2022 Lee Yingtong Li (RunasSudo)
|
|
|
|
#
|
|
|
|
# This program 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.
|
|
|
|
#
|
|
|
|
# This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
from sqlalchemy import Column, DateTime, ForeignKey, Integer, String
|
|
|
|
from sqlalchemy.orm import relationship
|
|
|
|
|
2022-12-24 16:16:47 +11:00
|
|
|
from ..database import Base, db_session
|
2022-12-24 12:31:45 +11:00
|
|
|
from ..models import Amount, Posting, Transaction
|
|
|
|
|
|
|
|
class StatementLine(Base):
|
|
|
|
__tablename__ = 'statement_lines'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
|
|
|
|
source_account = Column(String)
|
|
|
|
dt = Column(DateTime)
|
|
|
|
description = Column(String)
|
|
|
|
quantity = Column(Integer)
|
|
|
|
balance = Column(Integer)
|
|
|
|
commodity = Column(String)
|
|
|
|
|
|
|
|
postings = relationship('StatementLinePosting', back_populates='statement_line')
|
|
|
|
|
|
|
|
def amount(self):
|
|
|
|
return Amount(self.quantity, self.commodity)
|
|
|
|
|
|
|
|
def into_transaction(self):
|
|
|
|
if len(self.postings) > 0:
|
|
|
|
# Will already be accounted for in a StatementLineTransaction
|
|
|
|
#return None
|
|
|
|
raise Exception('Should not call into_transaction on a StatementLine with associated StatementLinePosting')
|
|
|
|
|
|
|
|
# Not classified
|
|
|
|
unclassified_name = 'Unclassified Statement Line Debits' if -self.quantity >= 0 else 'Unclassified Statement Line Credits'
|
|
|
|
|
|
|
|
return Transaction(
|
|
|
|
dt=self.dt,
|
|
|
|
description=self.description,
|
|
|
|
postings=[
|
|
|
|
Posting(account=self.source_account, quantity=self.quantity, commodity=self.commodity),
|
|
|
|
Posting(account=unclassified_name, quantity=-self.quantity, commodity=self.commodity)
|
|
|
|
]
|
|
|
|
)
|
2022-12-24 13:22:18 +11:00
|
|
|
|
|
|
|
def is_complex(self):
|
|
|
|
if len(self.postings) > 1:
|
|
|
|
return True
|
|
|
|
|
|
|
|
if any(len(p.transaction.postings) > 2 for p in self.postings):
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
2022-12-24 16:16:47 +11:00
|
|
|
|
|
|
|
def delete_postings(self):
|
|
|
|
"""Delete existing StatementLineTransactions with postings for this StatementLine"""
|
|
|
|
|
|
|
|
for posting in self.postings:
|
|
|
|
# TODO: Will be wonky if transaction covers multiple StatementLines
|
|
|
|
db_session.delete(posting.transaction)
|
2022-12-24 12:31:45 +11:00
|
|
|
|
|
|
|
class StatementLineTransaction(Base, Transaction):
|
|
|
|
__tablename__ = 'statement_line_transactions'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
|
|
|
|
dt = Column(DateTime)
|
|
|
|
description = Column(String)
|
|
|
|
|
|
|
|
postings = relationship('StatementLinePosting', back_populates='transaction', cascade='all, delete')
|
|
|
|
|
|
|
|
def charge_account(self, source_account):
|
|
|
|
if len(self.postings) > 2:
|
|
|
|
raise Exception('Cannot call charge_account on StatementLineTransaction with more than 2 postings')
|
|
|
|
|
|
|
|
for posting in self.postings:
|
|
|
|
if posting.account != source_account:
|
|
|
|
return posting.account
|
|
|
|
|
|
|
|
class StatementLinePosting(Base, Posting):
|
|
|
|
__tablename__ = 'statement_line_postings'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
transaction_id = Column(Integer, ForeignKey('statement_line_transactions.id'))
|
|
|
|
line_id = Column(Integer, ForeignKey('statement_lines.id'))
|
|
|
|
|
|
|
|
description = Column(String)
|
|
|
|
account = Column(String)
|
|
|
|
quantity = Column(Integer)
|
|
|
|
commodity = Column(String)
|
|
|
|
|
|
|
|
transaction = relationship('StatementLineTransaction', back_populates='postings')
|
|
|
|
statement_line = relationship('StatementLine', back_populates='postings')
|
|
|
|
|
|
|
|
def amount(self):
|
|
|
|
return Amount(self.quantity, self.commodity)
|