2016-04-20 01:01:14 +00:00
|
|
|
from django.core.urlresolvers import reverse
|
2016-01-28 15:53:37 +00:00
|
|
|
from django.db import models
|
2016-04-20 00:07:01 +00:00
|
|
|
from django.conf import settings
|
2016-01-28 15:53:37 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
|
|
|
from decimal import Decimal
|
|
|
|
from core.models import User
|
2016-04-20 00:07:01 +00:00
|
|
|
from club.models import Club
|
2016-01-28 15:53:37 +00:00
|
|
|
|
|
|
|
class CurrencyField(models.DecimalField):
|
|
|
|
"""
|
|
|
|
This is a custom database field used for currency
|
|
|
|
"""
|
|
|
|
__metaclass__ = models.SubfieldBase
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
kwargs['max_digits'] = 12
|
|
|
|
kwargs['decimal_places'] = 2
|
|
|
|
super(CurrencyField, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def to_python(self, value):
|
|
|
|
try:
|
|
|
|
return super(CurrencyField, self).to_python(value).quantize(Decimal("0.01"))
|
|
|
|
except AttributeError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2016-05-09 09:49:01 +00:00
|
|
|
class BankAccount(models.Model):
|
2016-04-20 00:07:01 +00:00
|
|
|
name = models.CharField(_('name'), max_length=30)
|
2016-06-20 13:47:19 +00:00
|
|
|
iban = models.CharField(_('iban'), max_length=255, blank=True)
|
2016-04-20 00:07:01 +00:00
|
|
|
number = models.CharField(_('account number'), max_length=255, blank=True)
|
2016-05-09 09:49:01 +00:00
|
|
|
club = models.ForeignKey(Club, related_name="bank_accounts")
|
|
|
|
|
|
|
|
def is_owned_by(self, user):
|
|
|
|
"""
|
|
|
|
Method to see if that object can be edited by the given user
|
|
|
|
"""
|
|
|
|
if user.is_in_group(settings.SITH_GROUPS['accounting-admin']['name']):
|
|
|
|
return True
|
|
|
|
m = self.club.get_membership_for(user)
|
|
|
|
if m is not None and m.role >= 7:
|
|
|
|
return True
|
|
|
|
return False
|
2016-04-20 00:07:01 +00:00
|
|
|
|
2016-04-20 01:01:14 +00:00
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse('accounting:bank_details', kwargs={'b_account_id': self.id})
|
|
|
|
|
2016-04-20 00:07:01 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2016-05-09 09:49:01 +00:00
|
|
|
class ClubAccount(models.Model):
|
2016-04-20 00:07:01 +00:00
|
|
|
name = models.CharField(_('name'), max_length=30)
|
2016-05-09 09:49:01 +00:00
|
|
|
club = models.OneToOneField(Club, related_name="club_account")
|
2016-04-20 00:07:01 +00:00
|
|
|
bank_account = models.ForeignKey(BankAccount, related_name="club_accounts")
|
|
|
|
|
2016-05-09 09:49:01 +00:00
|
|
|
def is_owned_by(self, user):
|
|
|
|
"""
|
|
|
|
Method to see if that object can be edited by the given user
|
|
|
|
"""
|
|
|
|
if user.is_in_group(settings.SITH_GROUPS['accounting-admin']['name']):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def can_be_edited_by(self, user):
|
|
|
|
"""
|
|
|
|
Method to see if that object can be edited by the given user
|
|
|
|
"""
|
|
|
|
m = self.club.get_membership_for(user)
|
|
|
|
if m is not None and m.role >= 7:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2016-04-20 01:01:14 +00:00
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse('accounting:club_details', kwargs={'c_account_id': self.id})
|
|
|
|
|
2016-04-20 00:07:01 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2016-05-09 09:49:01 +00:00
|
|
|
class GeneralJournal(models.Model):
|
2016-01-28 15:53:37 +00:00
|
|
|
"""
|
2016-04-20 00:07:01 +00:00
|
|
|
Class storing all the operations for a period of time
|
2016-01-28 15:53:37 +00:00
|
|
|
"""
|
|
|
|
start_date = models.DateField(_('start date'))
|
|
|
|
end_date = models.DateField(_('end date'), null=True, blank=True, default=None)
|
|
|
|
name = models.CharField(_('name'), max_length=30)
|
|
|
|
closed = models.BooleanField(_('is closed'), default=False)
|
2016-04-20 00:07:01 +00:00
|
|
|
club_account = models.ForeignKey(ClubAccount, related_name="journals", null=False)
|
2016-06-20 13:47:19 +00:00
|
|
|
amount = CurrencyField(_('amount'), default=0)
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(GeneralJournal, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if self.id == None:
|
|
|
|
amount = 0
|
|
|
|
super(GeneralJournal, self).save(*args, **kwargs)
|
|
|
|
|
|
|
|
def can_be_created_by(user):
|
|
|
|
"""
|
|
|
|
Method to see if an object can be created by the given user
|
|
|
|
"""
|
|
|
|
if user.is_in_group(settings.SITH_GROUPS['accounting-admin']['name']): # TODO: add the treasurer of the club
|
|
|
|
return True
|
|
|
|
return False
|
2016-01-28 15:53:37 +00:00
|
|
|
|
2016-05-09 09:49:01 +00:00
|
|
|
def is_owned_by(self, user):
|
|
|
|
"""
|
|
|
|
Method to see if that object can be edited by the given user
|
|
|
|
"""
|
|
|
|
if user.is_in_group(settings.SITH_GROUPS['accounting-admin']['name']):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def can_be_edited_by(self, user):
|
|
|
|
"""
|
|
|
|
Method to see if that object can be edited by the given user
|
|
|
|
"""
|
|
|
|
if self.club_account.can_be_edited_by(user):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2016-05-03 06:50:54 +00:00
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse('accounting:journal_details', kwargs={'j_id': self.id})
|
|
|
|
|
2016-01-28 15:53:37 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2016-05-09 09:49:01 +00:00
|
|
|
class AccountingType(models.Model):
|
2016-04-20 00:07:01 +00:00
|
|
|
"""
|
|
|
|
Class describing the accounting types.
|
2016-01-28 15:53:37 +00:00
|
|
|
|
2016-04-20 00:07:01 +00:00
|
|
|
Thoses are numbers used in accounting to classify operations
|
2016-01-28 15:53:37 +00:00
|
|
|
"""
|
2016-04-20 00:07:01 +00:00
|
|
|
code = models.CharField(_('code'), max_length=16) # TODO: add number validator
|
|
|
|
label = models.CharField(_('label'), max_length=60)
|
2016-04-20 01:01:14 +00:00
|
|
|
movement_type = models.CharField(_('movement type'), choices=[('credit', 'Credit'), ('debit', 'Debit'), ('neutral', 'Neutral')], max_length=12)
|
2016-04-20 00:07:01 +00:00
|
|
|
|
2016-05-09 09:49:01 +00:00
|
|
|
def is_owned_by(self, user):
|
|
|
|
"""
|
|
|
|
Method to see if that object can be edited by the given user
|
|
|
|
"""
|
|
|
|
if user.is_in_group(settings.SITH_GROUPS['accounting-admin']['name']):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2016-05-03 06:50:54 +00:00
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse('accounting:type_list')
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.movement_type+" - "+self.code+" - "+self.label
|
|
|
|
|
2016-05-09 09:49:01 +00:00
|
|
|
class Operation(models.Model):
|
2016-04-20 00:07:01 +00:00
|
|
|
"""
|
|
|
|
An operation is a line in the journal, a debit or a credit
|
2016-01-28 15:53:37 +00:00
|
|
|
"""
|
2016-05-03 06:50:54 +00:00
|
|
|
journal = models.ForeignKey(GeneralJournal, related_name="operations", null=False)
|
2016-05-30 10:23:59 +00:00
|
|
|
amount = CurrencyField(_('amount'))
|
2016-04-20 00:07:01 +00:00
|
|
|
date = models.DateField(_('date'))
|
|
|
|
remark = models.TextField(_('remark'), max_length=255)
|
|
|
|
mode = models.CharField(_('payment method'), max_length=255, choices=settings.SITH_ACCOUNTING_PAYMENT_METHOD)
|
|
|
|
cheque_number = models.IntegerField(_('cheque number'))
|
|
|
|
invoice = models.FileField(upload_to='invoices', null=True, blank=True)
|
|
|
|
done = models.BooleanField(_('is done'), default=False)
|
|
|
|
type = models.ForeignKey(AccountingType, related_name="operations")
|
2016-01-28 15:53:37 +00:00
|
|
|
|
2016-05-09 09:49:01 +00:00
|
|
|
def is_owned_by(self, user):
|
|
|
|
"""
|
|
|
|
Method to see if that object can be edited by the given user
|
|
|
|
"""
|
|
|
|
if user.is_in_group(settings.SITH_GROUPS['accounting-admin']['name']):
|
|
|
|
return True
|
|
|
|
m = self.journal.club_account.get_membership_for(user)
|
|
|
|
if m is not None and m.role >= 7:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def can_be_edited_by(self, user):
|
|
|
|
"""
|
|
|
|
Method to see if that object can be edited by the given user
|
|
|
|
"""
|
|
|
|
if self.journal.can_be_edited_by(user):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2016-05-03 06:50:54 +00:00
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse('accounting:journal_details', kwargs={'j_id': self.journal.id})
|
|
|
|
|
2016-01-28 15:53:37 +00:00
|
|
|
def __str__(self):
|
2016-05-03 06:50:54 +00:00
|
|
|
return str(self.id)+" - "+str(self.date)
|
2016-01-28 15:53:37 +00:00
|
|
|
|