2018-06-26 20:14:16 +10:00
|
|
|
# Society Self-Service
|
2019-06-19 18:26:34 +10:00
|
|
|
# Copyright © 2018-2019 Yingtong Li (RunasSudo)
|
2018-06-26 20:14:16 +10:00
|
|
|
#
|
|
|
|
# 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 <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
|
|
|
|
from django.db import models
|
2019-12-29 00:30:30 +11:00
|
|
|
from django.utils import timezone
|
2018-06-26 20:14:16 +10:00
|
|
|
from jsonfield import JSONField
|
|
|
|
|
|
|
|
from enum import Enum
|
|
|
|
|
2019-12-28 19:11:22 +11:00
|
|
|
class DescriptionEnum(Enum):
|
|
|
|
def __new__(cls, value, description):
|
|
|
|
obj = object.__new__(cls)
|
|
|
|
obj._value_ = value
|
|
|
|
obj.description = description
|
|
|
|
return obj
|
|
|
|
|
2018-06-26 20:14:16 +10:00
|
|
|
class Budget(models.Model):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class BudgetComment(models.Model):
|
|
|
|
budget = models.ForeignKey(Budget, on_delete=models.CASCADE)
|
|
|
|
author = models.ForeignKey(User, on_delete=models.PROTECT, related_name='+')
|
|
|
|
time = models.DateTimeField()
|
|
|
|
content = models.TextField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ['id']
|
|
|
|
|
2019-12-28 19:11:22 +11:00
|
|
|
class BudgetState(DescriptionEnum):
|
2018-06-26 20:14:16 +10:00
|
|
|
DRAFT = 10, 'Draft'
|
|
|
|
RESUBMIT = 20, 'Returned for redrafting'
|
|
|
|
AWAIT_REVIEW = 30, 'Awaiting Treasury review'
|
|
|
|
ENDORSED = 40, 'Endorsed by Treasury, awaiting committee approval'
|
|
|
|
APPROVED = 50, 'Approved'
|
2019-06-19 19:46:24 +10:00
|
|
|
#CANCELLED = 60, 'Cancelled'
|
|
|
|
|
2019-12-28 19:11:22 +11:00
|
|
|
class BudgetAction(DescriptionEnum):
|
2019-06-19 19:46:24 +10:00
|
|
|
CREATE = 5, 'Created'
|
|
|
|
EDIT = 10, 'Edited'
|
|
|
|
UPDATE_STATE = 20, 'Updated state'
|
2018-06-26 20:14:16 +10:00
|
|
|
|
|
|
|
class BudgetRevision(models.Model):
|
|
|
|
budget = models.ForeignKey(Budget, on_delete=models.CASCADE)
|
|
|
|
name = models.CharField(max_length=100)
|
2019-06-19 17:03:52 +10:00
|
|
|
date = models.DateField()
|
2018-06-26 20:14:16 +10:00
|
|
|
contributors = models.ManyToManyField(User, related_name='+')
|
|
|
|
comments = models.TextField()
|
|
|
|
|
|
|
|
author = models.ForeignKey(User, on_delete=models.PROTECT, related_name='+')
|
|
|
|
time = models.DateTimeField()
|
|
|
|
|
2019-06-19 17:03:52 +10:00
|
|
|
state = models.IntegerField(choices=[(v.value, v.description) for v in BudgetState])
|
2018-06-26 20:14:16 +10:00
|
|
|
|
|
|
|
revenue = JSONField(default=[])
|
|
|
|
revenue_comments = models.TextField()
|
|
|
|
|
|
|
|
expense = JSONField(default=[])
|
2019-06-19 17:38:20 +10:00
|
|
|
expense_no_emergency_fund = models.BooleanField()
|
2018-06-26 20:14:16 +10:00
|
|
|
expense_comments = models.TextField()
|
|
|
|
|
2019-06-19 19:46:24 +10:00
|
|
|
action = models.IntegerField(choices=[(v.value, v.description) for v in BudgetAction])
|
|
|
|
|
2019-12-29 00:30:30 +11:00
|
|
|
class Meta:
|
|
|
|
ordering = ['id']
|
|
|
|
|
2019-06-19 19:46:24 +10:00
|
|
|
def copy(self):
|
|
|
|
contributors = list(self.contributors.all())
|
|
|
|
self.pk, self.id = None, None
|
|
|
|
self.save()
|
|
|
|
self.contributors.add(*contributors)
|
|
|
|
|
2019-12-29 00:30:30 +11:00
|
|
|
def update_state(self, user, state):
|
|
|
|
self.copy()
|
|
|
|
self.author = user
|
|
|
|
self.time = timezone.now()
|
|
|
|
self.state = state.value
|
|
|
|
self.action = BudgetAction.UPDATE_STATE.value
|
|
|
|
self.save()
|
|
|
|
|
|
|
|
# Access control
|
|
|
|
|
2019-12-28 19:11:22 +11:00
|
|
|
def can_view(self, user):
|
|
|
|
if user == self.author:
|
|
|
|
return True
|
|
|
|
if user in self.contributors.all():
|
|
|
|
return True
|
|
|
|
if user.groups.filter(name='Treasury').exists():
|
|
|
|
return True
|
2019-12-29 00:30:30 +11:00
|
|
|
if (self.state == BudgetState.ENDORSED.value or self.state == BudgetState.APPROVED.value) and user.groups.filter(name='Committee').exists():
|
2019-12-28 19:11:22 +11:00
|
|
|
return True
|
2019-12-29 00:30:30 +11:00
|
|
|
|
2019-12-28 19:11:22 +11:00
|
|
|
return False
|
|
|
|
|
2019-12-29 00:30:30 +11:00
|
|
|
def can_edit(self, user):
|
|
|
|
# Cannot edit if cannot view
|
|
|
|
if not self.can_view(user):
|
|
|
|
return False
|
|
|
|
|
|
|
|
# No one can edit if already approved
|
|
|
|
if self.state == BudgetState.APPROVED.value:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Only Treasurer or Secretary may edit if submitted
|
|
|
|
if self.state != BudgetState.DRAFT.value and self.state != BudgetState.RESUBMIT.value:
|
|
|
|
if user.groups.filter(name='Treasury').exists() or user.groups.filter(name='Secretary').exists():
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Otherwise the submitter may edit
|
|
|
|
if user == self.author:
|
|
|
|
return True
|
|
|
|
if user in self.contributors.all():
|
|
|
|
return True
|
|
|
|
if user.groups.filter(name='Treasury').exists():
|
|
|
|
return True
|
|
|
|
|
|
|
|
# Otherwise cannot edit
|
|
|
|
return False
|
|
|
|
|
|
|
|
def can_submit(self, user):
|
|
|
|
if not self.can_edit(user):
|
|
|
|
return False
|
|
|
|
if self.state == BudgetState.DRAFT.value or self.state == BudgetState.RESUBMIT.value:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def can_withdraw(self, user):
|
|
|
|
if not self.can_edit(user):
|
|
|
|
return False
|
|
|
|
if self.state == BudgetState.AWAIT_REVIEW.value or self.state == BudgetState.ENDORSED.value:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def can_endorse(self, user):
|
|
|
|
if not self.can_edit(user):
|
|
|
|
return False
|
|
|
|
if not user.groups.filter(name='Treasury').exists():
|
|
|
|
return False
|
|
|
|
if self.state == BudgetState.AWAIT_REVIEW.value:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def can_return(self, user):
|
|
|
|
return self.can_endorse(user)
|
|
|
|
|
|
|
|
def can_approve(self, user):
|
|
|
|
if not self.can_edit(user):
|
|
|
|
return False
|
|
|
|
if not user.groups.filter(name='Secretary').exists():
|
|
|
|
return False
|
|
|
|
if self.state == BudgetState.ENDORSED.value:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def can_cmtereturn(self, user):
|
|
|
|
return self.can_approve(user)
|
2019-12-28 19:11:22 +11:00
|
|
|
|
|
|
|
class ClaimState(DescriptionEnum):
|
|
|
|
DRAFT = 10, 'Draft'
|
|
|
|
RESUBMIT = 20, 'Returned for redrafting'
|
|
|
|
AWAIT_REVIEW = 30, 'Awaiting Treasury approval'
|
|
|
|
APPROVED = 40, 'Approved by Treasury, awaiting payment'
|
|
|
|
PAID = 50, 'Paid'
|
|
|
|
|
|
|
|
class ClaimAction(DescriptionEnum):
|
|
|
|
CREATE = 5, 'Created'
|
|
|
|
EDIT = 10, 'Edited'
|
|
|
|
UPDATE_STATE = 20, 'Updated state'
|
|
|
|
|
|
|
|
class ReimbursementClaim(models.Model):
|
|
|
|
purpose = models.CharField(max_length=100)
|
|
|
|
date = models.DateField()
|
2019-12-28 23:23:56 +11:00
|
|
|
budget_id = models.CharField(max_length=20)
|
2019-12-28 19:11:22 +11:00
|
|
|
comments = models.TextField()
|
|
|
|
|
|
|
|
author = models.ForeignKey(User, on_delete=models.PROTECT, related_name='+')
|
|
|
|
time = models.DateTimeField()
|
|
|
|
|
|
|
|
state = models.IntegerField(choices=[(v.value, v.description) for v in ClaimState])
|
|
|
|
|
|
|
|
items = JSONField(default=[])
|
|
|
|
|
2020-01-04 16:50:31 +11:00
|
|
|
payee_name = models.TextField()
|
|
|
|
payee_bsb = models.CharField(max_length=7)
|
|
|
|
payee_account = models.TextField(max_length=20)
|
|
|
|
|
2019-12-29 00:30:30 +11:00
|
|
|
def update_state(self, user, state):
|
|
|
|
self.state = state.value
|
|
|
|
self.save()
|
|
|
|
|
|
|
|
claim_history = ClaimHistory()
|
|
|
|
claim_history.claim = self
|
|
|
|
claim_history.author = user
|
|
|
|
claim_history.state = self.state
|
|
|
|
claim_history.time = timezone.now()
|
|
|
|
claim_history.action = ClaimAction.UPDATE_STATE.value
|
|
|
|
claim_history.save()
|
|
|
|
|
|
|
|
# Access control
|
|
|
|
|
2019-12-28 19:11:22 +11:00
|
|
|
def can_view(self, user):
|
|
|
|
if user == self.author:
|
|
|
|
return True
|
|
|
|
if user.groups.filter(name='Treasury').exists():
|
|
|
|
return True
|
|
|
|
return False
|
2019-12-29 00:30:30 +11:00
|
|
|
|
|
|
|
def can_edit(self, user):
|
|
|
|
# Cannot edit if cannot view
|
|
|
|
if not self.can_view(user):
|
|
|
|
return False
|
|
|
|
|
|
|
|
# No one can edit if already paid
|
|
|
|
if self.state == ClaimState.PAID.value:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Only Treasurer may edit if submitted
|
|
|
|
if self.state != ClaimState.DRAFT.value and self.state != ClaimState.RESUBMIT.value:
|
|
|
|
if user.groups.filter(name='Treasury').exists():
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Otherwise the submitter or Treasurer may edit
|
|
|
|
if user == self.author:
|
|
|
|
return True
|
|
|
|
if user in self.contributors.all():
|
|
|
|
return True
|
|
|
|
if user.groups.filter(name='Treasury').exists():
|
|
|
|
return True
|
|
|
|
|
|
|
|
# Otherwise cannot edit
|
|
|
|
return False
|
|
|
|
|
|
|
|
def can_submit(self, user):
|
|
|
|
if not self.can_edit(user):
|
|
|
|
return False
|
|
|
|
if self.state == ClaimState.DRAFT.value or self.state == ClaimState.RESUBMIT.value:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def can_withdraw(self, user):
|
|
|
|
if not self.can_edit(user):
|
|
|
|
return False
|
|
|
|
if self.state == ClaimState.AWAIT_REVIEW.value or self.state == ClaimState.APPROVED.value:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def can_approve(self, user):
|
|
|
|
if not self.can_edit(user):
|
|
|
|
return False
|
|
|
|
if not user.groups.filter(name='Treasury').exists():
|
|
|
|
return False
|
|
|
|
if self.state == ClaimState.AWAIT_REVIEW.value:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def can_return(self, user):
|
|
|
|
return self.can_approve(user)
|
2019-12-28 19:11:22 +11:00
|
|
|
|
|
|
|
class ClaimReceipt(models.Model):
|
|
|
|
claim = models.ForeignKey(ReimbursementClaim, on_delete=models.CASCADE)
|
2020-01-04 16:50:31 +11:00
|
|
|
uploaded_file = models.FileField(upload_to='receipt_uploads/%Y/%m/%d/')
|
2019-12-28 19:11:22 +11:00
|
|
|
|
|
|
|
class ClaimComment(models.Model):
|
|
|
|
claim = models.ForeignKey(ReimbursementClaim, on_delete=models.CASCADE)
|
|
|
|
author = models.ForeignKey(User, on_delete=models.PROTECT, related_name='+')
|
|
|
|
time = models.DateTimeField()
|
|
|
|
content = models.TextField()
|
|
|
|
|
2018-06-26 20:14:16 +10:00
|
|
|
class Meta:
|
|
|
|
ordering = ['id']
|
2019-12-28 19:11:22 +11:00
|
|
|
|
|
|
|
class ClaimHistory(models.Model):
|
|
|
|
claim = models.ForeignKey(ReimbursementClaim, on_delete=models.CASCADE)
|
|
|
|
author = models.ForeignKey(User, on_delete=models.PROTECT, related_name='+')
|
|
|
|
state = models.IntegerField(choices=[(v.value, v.description) for v in ClaimState])
|
|
|
|
time = models.DateTimeField()
|
|
|
|
action = models.IntegerField(choices=[(v.value, v.description) for v in ClaimAction])
|