# 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 . 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) postings = db.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) ] ) 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 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) class StatementLineTransaction(db.Model, Transaction): __tablename__ = 'statement_line_transactions' id = db.Column(db.Integer, primary_key=True) dt = db.Column(db.DateTime) description = db.Column(db.String) postings = db.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(db.Model, Posting): __tablename__ = 'statement_line_postings' id = db.Column(db.Integer, primary_key=True) transaction_id = db.Column(db.Integer, db.ForeignKey('statement_line_transactions.id')) line_id = db.Column(db.Integer, db.ForeignKey('statement_lines.id')) description = db.Column(db.String) account = db.Column(db.String) quantity = db.Column(db.Integer) commodity = db.Column(db.String) transaction = db.relationship('StatementLineTransaction', back_populates='postings') statement_line = db.relationship('StatementLine', back_populates='postings') def amount(self): return Amount(self.quantity, self.commodity)