public
Description: Django-beancounter is a simple app I built to track my income and expenses.
Homepage: http://github.com/lincolnloop/django-beancounter
Clone URL: git://github.com/lincolnloop/django-beancounter.git
100644 190 lines (153 sloc) 6.892 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import datetime
import decimal
 
from django.db import models
from django.contrib.localflavor.us.models import PhoneNumberField
from tagging.fields import TagField
 
TYPE_CHOICES = (
    ('INC', 'Income'),
    ('EXP', 'Expense'),
    ('COGS', 'Cost of Goods Sold'),
)
class Category(models.Model):
    type = models.CharField(max_length=4,choices=TYPE_CHOICES)
    name = models.CharField(max_length=50)
    income = models.ForeignKey('self', null=True, blank=True, limit_choices_to = {'type__exact':'INC'}, help_text='Use this to enable tracking your costs of goods vs. income')
 
    def __str__(self):
        return "%s: %s" % (self.type,self.name)
    class Meta:
        verbose_name_plural = 'Categories'
        ordering = ['type','name']
    
    class Admin:
        fields = (
            (None, {
                'fields': ('type','name')
            }),
            ('Associate Costs of Goods Sold to an Income Category', {
                'classes': 'collapse',
                'fields' : ('income',)
            }),
        )
    
class BankAccount(models.Model):
    type = models.CharField(max_length=50,help_text='Checking, Savings, Credit, etc.')
    name = models.CharField(max_length=50)
    initial_balance = models.DecimalField(max_digits=10, decimal_places=2, null=True,blank=True)
    track_balance = models.BooleanField(help_text='Generate reports of the balance of this account over time.')
 
    def __str__(self):
        return "%s (%s)" % (self.name,self.type)
    class Admin:
        list_display = ('name','type')
        ordering = ['-track_balance','name','type']
        
class AccountTransfer(models.Model):
    date = models.DateField()
    from_account = models.ForeignKey(BankAccount,related_name='transferred_from')
    to_account = models.ForeignKey(BankAccount,related_name='transferred_to')
    amount = models.DecimalField(max_digits=8, decimal_places=2)
    memo = models.CharField(max_length=100,null=True,blank=True)
    def __str__(self):
        return "$%.2f from %s to %s" % (self.amount,self.from_account,self.to_account)
        
    class Admin:
        list_display = ('date','amount','from_account','to_account')
        list_filter = ('to_account','from_account')
        date_hierarchy = 'date'
        
        
class Person(models.Model):
    name = models.CharField(max_length=100)
    contact = models.CharField(max_length=100,null=True,blank=True)
    phone = PhoneNumberField(null=True,blank=True)
    website = models.URLField(null=True,blank=True)
    email = models.EmailField(null=True,blank=True)
    notes = models.CharField(max_length=100,null=True,blank=True)
    
    def __unicode__(self):
        return self.name
    
    class Meta:
        verbose_name_plural = 'People'
        ordering = ['name']
        
        
class Employee(Person):
    PAYMENT_CHOICES = (
        ('paypal', 'PayPal'),
        ('check', 'Mail Check'),
        ('wire', 'Wire Transfer'),
        ('elance', 'Elance'),
        ('other', 'Other'),
    )
    gmt_offset = models.DecimalField(max_digits=3, decimal_places=1, null=True, blank=True)
    skills = TagField()
    payment_preference = models.CharField(blank=True, max_length=100, choices=PAYMENT_CHOICES)
    payment_notes = models.TextField(blank=True)
    contract = models.DateField(blank=True, null=True, help_text="Date contractor contract was signed and received.")
    hourly_rate = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True, help_text="If rate varies, enter average and note below.")
    currency = models.CharField(default="USD", max_length=3)
    rate_notes = models.TextField(blank=True, help_text="Additional notes regarding contractor rates.")
 
 
    def timezone(self):
        if self.gmt_offset == int(self.gmt_offset):
            gmt_offset = int(self.gmt_offset)
        else:
            gmt_offset = self.gmt_offset
        if gmt_offset > 0:
            gmt_offset = '+%s' % gmt_offset
        return 'GMT%s' % gmt_offset
 
    def under_contract(self):
        if self.contract:
            return True
        return False
    under_contract.boolean = True
 
    def rate(self):
        return "%s %s" % (self.hourly_rate, self.currency)
 
class Project(models.Model):
    name = models.CharField(max_length=100)
    employees = models.ManyToManyField(Employee)
    start_date = models.DateField()
    active = models.BooleanField(default=True)
 
    def __unicode__(self):
        return self.name
        
    def total_invoiced(self):
        #TODO aggregate support
        total = decimal.Decimal("0.00")
        for invoice in self.projectinvoice_set.all():
            total += invoice.amount
        return total
    
    def total_cost(self):
        #TODO aggregate support
        total = decimal.Decimal("0.00")
        for time in self.projecttime_set.all():
            total += time.cost_converted
        return total
    
    def profit(self):
        return self.total_invoiced() - self.total_cost()
            
 
class ProjectInvoice(models.Model):
    """
Invoice sent for project
"""
    project = models.ForeignKey(Project)
    date = models.DateField(default=datetime.date.today())
    amount = models.DecimalField(max_digits=8, decimal_places=2)
    
    def __unicode__(self):
        return "$%s for %s on %s" % (self.amount, self.project, self.date)
        
        
class ProjectTime(models.Model):
    """
Hours spent by an employee on a project
"""
    
    employee = models.ForeignKey(Employee)
    project = models.ForeignKey(Project)
    start_date = models.DateField(default=datetime.date.today())
    end_date = models.DateField(default=datetime.date.today())
    hours = models.DecimalField(max_digits=6, decimal_places=3)
    cost = models.DecimalField(max_digits=9, decimal_places=2, blank=True, null=True, help_text="Leave blank to automatically calculate")
    cost_converted = models.DecimalField(max_digits=9, decimal_places=2, blank=True, null=True, help_text="Cost converted to local currency")
 
    def __unicode__(self):
        return "%s on %s (%s-%s)" % (self.employee, self.project, self.start_date, self.end_date)
 
 
class Entry(models.Model):
    category = models.ForeignKey(Category)
    date = models.DateField()
    name = models.ForeignKey(Person)
    amount = models.DecimalField(max_digits=8, decimal_places=2)
    bank_account = models.ForeignKey(BankAccount,related_name='paid_from',null=True,blank=True)
    memo = models.CharField(max_length=100,null=True,blank=True)
    
    def __str__(self):
        return "$%.2f | %s | %s" % (self.amount,self.name,self.date)
    class Meta:
        verbose_name_plural = 'Entries'
            
    class Admin:
        list_display = ('date', 'name', 'category', 'amount')
        date_hierarchy = 'date'
        search_fields = ('name','memo')
        list_filter = ('category','name','bank_account')