2013-12-17 14:41:30 +00:00
|
|
|
# Part of accounting-api project:
|
|
|
|
# https://gitorious.org/conservancy/accounting-api
|
|
|
|
# License: AGPLv3-or-later
|
2013-12-20 23:24:37 +00:00
|
|
|
import datetime
|
2013-12-11 14:12:08 +00:00
|
|
|
import uuid
|
2013-12-20 23:24:37 +00:00
|
|
|
|
2013-12-11 08:23:44 +00:00
|
|
|
from decimal import Decimal
|
|
|
|
|
|
|
|
|
|
|
|
class Transaction:
|
2013-12-14 15:08:47 +00:00
|
|
|
def __init__(self, id=None, date=None, payee=None, postings=None,
|
|
|
|
metadata=None, _generate_id=False):
|
2013-12-20 23:24:37 +00:00
|
|
|
if type(date) == datetime.datetime:
|
|
|
|
date = date.date()
|
|
|
|
|
2013-12-14 15:08:47 +00:00
|
|
|
self.id = id
|
2013-12-11 08:23:44 +00:00
|
|
|
self.date = date
|
|
|
|
self.payee = payee
|
|
|
|
self.postings = postings
|
2013-12-11 14:12:08 +00:00
|
|
|
self.metadata = metadata if metadata is not None else {}
|
|
|
|
|
|
|
|
if _generate_id:
|
|
|
|
self.generate_id()
|
|
|
|
|
|
|
|
def generate_id(self):
|
2013-12-16 06:33:56 +00:00
|
|
|
self.id = str(uuid.uuid4())
|
2013-12-11 08:23:44 +00:00
|
|
|
|
2013-12-20 23:24:37 +00:00
|
|
|
def __eq__(self, other):
|
|
|
|
return self.__dict__ == other.__dict__
|
|
|
|
|
2013-12-11 08:23:44 +00:00
|
|
|
def __repr__(self):
|
2013-12-16 06:33:56 +00:00
|
|
|
return ('<{self.__class__.__name__} {self.id} {date}' +
|
2013-12-11 08:23:44 +00:00
|
|
|
' {self.payee} {self.postings}').format(
|
|
|
|
self=self,
|
|
|
|
date=self.date.strftime('%Y-%m-%d'))
|
|
|
|
|
|
|
|
|
|
|
|
class Posting:
|
2013-12-11 14:12:08 +00:00
|
|
|
def __init__(self, account=None, amount=None, metadata=None):
|
2013-12-11 08:23:44 +00:00
|
|
|
self.account = account
|
|
|
|
self.amount = amount
|
2013-12-11 14:12:08 +00:00
|
|
|
self.metadata = metadata if metadata is not None else {}
|
2013-12-11 08:23:44 +00:00
|
|
|
|
2013-12-20 23:24:37 +00:00
|
|
|
def __eq__(self, other):
|
|
|
|
return self.__dict__ == other.__dict__
|
|
|
|
|
2013-12-11 08:23:44 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return ('<{self.__class__.__name__} "{self.account}"' +
|
|
|
|
' {self.amount}>').format(self=self)
|
|
|
|
|
|
|
|
|
|
|
|
class Amount:
|
|
|
|
def __init__(self, amount=None, symbol=None):
|
|
|
|
self.amount = Decimal(amount)
|
|
|
|
self.symbol = symbol
|
|
|
|
|
2013-12-20 23:24:37 +00:00
|
|
|
def __eq__(self, other):
|
|
|
|
return self.__dict__ == other.__dict__
|
|
|
|
|
2013-12-11 08:23:44 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return ('<{self.__class__.__name__} {self.symbol}' +
|
|
|
|
' {self.amount}>').format(self=self)
|
|
|
|
|
|
|
|
|
|
|
|
class Account:
|
|
|
|
def __init__(self, name=None, amounts=None, accounts=None):
|
|
|
|
self.name = name
|
|
|
|
self.amounts = amounts
|
|
|
|
self.accounts = accounts
|
|
|
|
|
2013-12-20 23:24:37 +00:00
|
|
|
def __eq__(self, other):
|
|
|
|
return self.__dict__ == other.__dict__
|
|
|
|
|
2013-12-11 08:23:44 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return ('<{self.__class__.__name__} "{self.name}" {self.amounts}' +
|
|
|
|
' {self.accounts}>').format(self=self)
|