DrCr/drcr/statements/models.py

70 lines
2.5 KiB
Python

# DrCr: Web-based double-entry bookkeeping framework
# Copyright (C) 2022–2023 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 ..database import db
from ..models import Amount, Posting, Transaction
class StatementLine(db.Model):
__tablename__ = 'statement_lines'
id = db.Column(db.Integer, primary_key=True)
source_account = db.Column(db.String)
dt = db.Column(db.DateTime)
description = db.Column(db.String)
quantity = db.Column(db.Integer)
balance = db.Column(db.Integer)
commodity = db.Column(db.String)
reconciliation = db.relationship('StatementLineReconciliation', back_populates='statement_line', uselist=False)
def amount(self):
return Amount(self.quantity, self.commodity)
def into_transaction(self):
if len(self.reconciliations) > 0:
# Will already be accounted for in a StatementLineTransaction
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)
]
)
def is_complex(self):
if self.reconciliation and len(self.reconciliation.posting.transaction.postings) > 2:
return True
return False
class StatementLineReconciliation(db.Model):
__tablename__ = 'statement_line_reconciliations'
id = db.Column(db.Integer, primary_key=True)
statement_line_id = db.Column(db.Integer, db.ForeignKey('statement_lines.id'))
posting_id = db.Column(db.Integer, db.ForeignKey('postings.id'))
statement_line = db.relationship('StatementLine', back_populates='reconciliation')
posting = db.relationship('Posting')