DrCr/drcr/statements/models.py

70 lines
2.5 KiB
Python
Raw Normal View History

2022-12-24 12:31:45 +11:00
# DrCr: Web-based double-entry bookkeeping framework
2023-01-02 18:08:30 +11:00
# Copyright (C) 2022–2023 Lee Yingtong Li (RunasSudo)
2022-12-24 12:31:45 +11:00
#
# 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
2022-12-24 12:31:45 +11:00
from ..models import Amount, Posting, Transaction
class StatementLine(db.Model):
2022-12-24 12:31:45 +11:00
__tablename__ = 'statement_lines'
id = db.Column(db.Integer, primary_key=True)
2022-12-24 12:31:45 +11:00
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)
2022-12-24 12:31:45 +11:00
2023-01-02 18:08:30 +11:00
reconciliation = db.relationship('StatementLineReconciliation', back_populates='statement_line', uselist=False)
2022-12-24 12:31:45 +11:00
def amount(self):
return Amount(self.quantity, self.commodity)
def into_transaction(self):
2023-01-02 18:08:30 +11:00
if len(self.reconciliations) > 0:
2022-12-24 12:31:45 +11:00
# 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):
2023-01-02 18:08:30 +11:00
if self.reconciliation and len(self.reconciliation.posting.transaction.postings) > 2:
return True
return False
2022-12-24 12:31:45 +11:00
2023-01-02 18:08:30 +11:00
class StatementLineReconciliation(db.Model):
__tablename__ = 'statement_line_reconciliations'
2022-12-24 12:31:45 +11:00
id = db.Column(db.Integer, primary_key=True)
2022-12-24 12:31:45 +11:00
2023-01-02 18:08:30 +11:00
statement_line_id = db.Column(db.Integer, db.ForeignKey('statement_lines.id'))
posting_id = db.Column(db.Integer, db.ForeignKey('postings.id'))
2022-12-24 12:31:45 +11:00
2023-01-02 18:08:30 +11:00
statement_line = db.relationship('StatementLine', back_populates='reconciliation')
posting = db.relationship('Posting')