Skip to content

Instantly share code, notes, and snippets.

@codeinthehole
Created August 7, 2012 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save codeinthehole/3285411 to your computer and use it in GitHub Desktop.
Save codeinthehole/3285411 to your computer and use it in GitHub Desktop.
Models for double-book-keeping accounting for managed budgets.
from django.db import models
class Ledger(models.Model):
# There can only be one 'Labour' and 'Shop' ledger but lots of 'Budget'
# ledgers.
LABOUR, BUDGET, SHOP = 'Labour', 'Budget', 'Shop'
LEDGER_CHOICES = (
(LABOUR, LABOUR),
(BUDGET, BUDGET),
(SHOP, SHOP),
)
type = models.CharField(max_length=128, choices=LEDGER_CHOICES)
name = models.CharField(max_length=128, unique=True)
class Transaction(models.Model):
# Every transfer of money should create two rows in this table.
# (a) the debit from the source ledger
# (b) the credit to the destination ledger
ledger = models.CharField(max_length=128)
debit_amount = models.DecimalField(decimal_places=2, max_digits=12)
credit_amount = models.DecimalField(decimal_places=2, max_digits=12)
date_created = models.DateTimeField(auto_now_add=True)
class Budget(models.Model):
name = models.CharField(max_length=128, unique=True)
ledger = models.OneToOneField(Ledger, related_name='transactions')
# This is entered at checkout to pay using budget
code = models.CharField(max_length=128, unique=True)
# Audit information
initial_balance = models.DecimalField(decimal_places=2, max_digits=12)
current_balance = models.DecimalField(decimal_places=2, max_digits=12)
expiry_date = models.DateField()
users = models.ManyToManyField('auth.User')
products = models.ManyToManyField('catalogue.Product')
date_created = models.DateTimeField(auto_now_add=True)
date_last_transation = models.DateTimeField(null=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment