2018-06-26 20:14:16 +10:00
|
|
|
# Society Self-Service
|
2023-04-30 23:12:18 +10:00
|
|
|
# Copyright © 2018–2023 Yingtong Li (RunasSudo)
|
|
|
|
# Copyright © 2023 MUMUS Inc.
|
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
|
|
|
|
|
2023-05-01 18:28:24 +10:00
|
|
|
from django.conf import settings
|
2018-06-26 20:14:16 +10:00
|
|
|
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
|
|
|
|
|
2020-01-05 17:54:09 +11:00
|
|
|
from decimal import Decimal
|
2018-06-26 20:14:16 +10:00
|
|
|
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'
|
2020-07-15 16:42:03 +10:00
|
|
|
WITHDRAWN = 15, 'Withdrawn'
|
2018-06-26 20:14:16 +10:00
|
|
|
RESUBMIT = 20, 'Returned for redrafting'
|
|
|
|
AWAIT_REVIEW = 30, 'Awaiting Treasury review'
|
|
|
|
ENDORSED = 40, 'Endorsed by Treasury, awaiting committee approval'
|
|
|
|
APPROVED = 50, 'Approved'
|
2020-07-15 16:42:03 +10:00
|
|
|
CANCELLED = 60, 'Cancelled'
|
2019-06-19 19:46:24 +10:00
|
|
|
|
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()
|
2023-05-01 17:16:26 +10:00
|
|
|
approver = models.CharField(max_length=100)
|
2018-06-26 20:14:16 +10:00
|
|
|
|
2020-01-17 21:12:07 +11:00
|
|
|
event_dt = models.DateTimeField(null=True)
|
2020-01-25 23:17:21 +11:00
|
|
|
event_attendees = models.CharField(max_length=20, null=True)
|
2020-01-17 21:12:07 +11:00
|
|
|
|
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()
|
|
|
|
|
2023-04-30 23:12:18 +10:00
|
|
|
ticketing_fee_proportion = models.FloatField()
|
|
|
|
ticketing_fee_fixed = models.FloatField()
|
|
|
|
|
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()
|
|
|
|
|
2020-01-17 21:30:52 +11:00
|
|
|
def get_revenue_total(self):
|
|
|
|
total = Decimal(0)
|
|
|
|
for item in self.revenue:
|
2022-02-28 19:14:20 +11:00
|
|
|
try:
|
|
|
|
total += Decimal(item['Unit price']) * Decimal(item['Units'])
|
|
|
|
if item['IWT'] and item['Unit price'] > 0:
|
2023-04-30 23:12:18 +10:00
|
|
|
total -= (Decimal(item['Unit price']) * Decimal(self.ticketing_fee_proportion) + Decimal(self.ticketing_fee_fixed)) * item['Units']
|
2022-02-28 19:14:20 +11:00
|
|
|
except TypeError:
|
|
|
|
# Invalid unit price, etc.
|
|
|
|
pass
|
2020-01-17 21:30:52 +11:00
|
|
|
return total
|
|
|
|
|
|
|
|
def get_expense_total(self):
|
|
|
|
total = Decimal(0)
|
|
|
|
for item in self.expense:
|
2022-02-28 19:14:20 +11:00
|
|
|
try:
|
|
|
|
total += Decimal(item['Unit price']) * Decimal(item['Units'])
|
|
|
|
except TypeError:
|
|
|
|
# Invalid unit price, etc.
|
|
|
|
pass
|
2020-01-17 21:30:52 +11:00
|
|
|
if not self.expense_no_emergency_fund:
|
|
|
|
total *= Decimal('1.05')
|
|
|
|
return total
|
|
|
|
|
2019-12-29 00:30:30 +11:00
|
|
|
# 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
|
2020-07-15 16:42:03 +10:00
|
|
|
if self.state in (BudgetState.AWAIT_REVIEW.value, BudgetState.ENDORSED.value, 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
|
2020-07-15 16:42:03 +10:00
|
|
|
if self.state not in (BudgetState.DRAFT.value, BudgetState.RESUBMIT.value, BudgetState.WITHDRAWN.value):
|
2019-12-29 00:30:30 +11:00
|
|
|
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
|
2020-07-15 16:42:03 +10:00
|
|
|
if self.state in (BudgetState.DRAFT.value, BudgetState.RESUBMIT.value, BudgetState.WITHDRAWN.value):
|
2019-12-29 00:30:30 +11:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def can_withdraw(self, user):
|
2020-06-07 21:36:10 +10:00
|
|
|
if not self.can_view(user):
|
2019-12-29 00:30:30 +11:00
|
|
|
return False
|
2020-06-07 21:36:10 +10:00
|
|
|
if user != self.author and user not in self.contributors.all() and not user.groups.filter(name='Treasury').exists():
|
|
|
|
return False
|
|
|
|
|
2020-07-15 16:42:03 +10:00
|
|
|
if self.state in (BudgetState.DRAFT.value, BudgetState.RESUBMIT.value, BudgetState.AWAIT_REVIEW.value, BudgetState.ENDORSED.value):
|
2019-12-29 00:30:30 +11:00
|
|
|
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
|
2020-07-15 16:42:03 +10:00
|
|
|
if self.state in (BudgetState.AWAIT_REVIEW.value, BudgetState.DRAFT.value, BudgetState.RESUBMIT.value, BudgetState.WITHDRAWN.value):
|
2019-12-29 00:30:30 +11:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def can_return(self, user):
|
2020-01-25 23:18:56 +11:00
|
|
|
if self.state != BudgetState.AWAIT_REVIEW.value:
|
|
|
|
return False
|
2019-12-29 00:30:30 +11:00
|
|
|
return self.can_endorse(user)
|
|
|
|
|
|
|
|
def can_approve(self, user):
|
|
|
|
if not self.can_edit(user):
|
|
|
|
return False
|
2020-01-25 23:19:20 +11:00
|
|
|
if not user.groups.filter(name='Secretary').exists() and not user.groups.filter(name='Treasury').exists():
|
2019-12-29 00:30:30 +11:00
|
|
|
return False
|
2020-03-02 14:31:19 +11:00
|
|
|
if self.state != BudgetState.APPROVED.value:
|
2019-12-29 00:30:30 +11:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def can_cmtereturn(self, user):
|
2020-03-02 14:31:19 +11:00
|
|
|
if self.state == BudgetState.APPROVED.value:
|
2020-01-25 23:18:56 +11:00
|
|
|
return False
|
2019-12-29 00:30:30 +11:00
|
|
|
return self.can_approve(user)
|
2020-07-15 16:42:03 +10:00
|
|
|
|
|
|
|
def can_cancel(self, user):
|
|
|
|
if not self.can_view(user):
|
|
|
|
return False
|
|
|
|
if not user.groups.filter(name='Secretary').exists() and not user.groups.filter(name='Treasury').exists():
|
|
|
|
return False
|
|
|
|
if self.state != BudgetState.APPROVED.value:
|
|
|
|
return False
|
|
|
|
return True
|
2023-05-01 18:28:24 +10:00
|
|
|
|
|
|
|
def can_vote(self, user):
|
|
|
|
if not settings.BUDGET_ENABLE_VOTING:
|
|
|
|
return False
|
|
|
|
if self.state == BudgetState.ENDORSED.value and user.groups.filter(name=self.approver).exists():
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
class BudgetVoteType(DescriptionEnum):
|
|
|
|
IN_FAVOUR = 1, 'In favour'
|
|
|
|
AGAINST = -1, 'Against'
|
|
|
|
ABSTAIN = 0, 'Abstain'
|
|
|
|
|
|
|
|
class BudgetVote(models.Model):
|
|
|
|
revision = models.ForeignKey(BudgetRevision, on_delete=models.CASCADE)
|
|
|
|
voter = models.ForeignKey(User, on_delete=models.PROTECT, related_name='+')
|
|
|
|
time = models.DateTimeField()
|
|
|
|
is_current = models.BooleanField()
|
|
|
|
vote_type = models.IntegerField(choices=[(v.value, v.description) for v in BudgetVoteType])
|
2019-12-28 19:11:22 +11:00
|
|
|
|
|
|
|
class ClaimState(DescriptionEnum):
|
|
|
|
DRAFT = 10, 'Draft'
|
2020-07-15 17:03:52 +10:00
|
|
|
WITHDRAWN = 15, 'Withdrawn'
|
2019-12-28 19:11:22 +11:00
|
|
|
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)
|
|
|
|
|
2020-01-05 17:54:09 +11:00
|
|
|
def get_total(self):
|
|
|
|
total = Decimal(0)
|
|
|
|
for item in self.items:
|
2022-02-28 19:14:20 +11:00
|
|
|
try:
|
|
|
|
total += Decimal(item['Unit price']) * Decimal(item['Units'])
|
|
|
|
except TypeError:
|
|
|
|
# Invalid unit price, etc.
|
|
|
|
pass
|
2020-01-05 17:54:09 +11:00
|
|
|
return total
|
|
|
|
|
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
|
2020-07-15 17:03:52 +10:00
|
|
|
if self.state not in (ClaimState.DRAFT.value, ClaimState.RESUBMIT.value, ClaimState.WITHDRAWN.value):
|
2019-12-29 00:30:30 +11:00
|
|
|
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.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
|
2020-07-15 17:03:52 +10:00
|
|
|
if self.state in (ClaimState.DRAFT.value, ClaimState.RESUBMIT.value, ClaimState.WITHDRAWN.value):
|
2019-12-29 00:30:30 +11:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def can_withdraw(self, user):
|
2020-06-08 01:11:10 +10:00
|
|
|
if not self.can_view(user):
|
2019-12-29 00:30:30 +11:00
|
|
|
return False
|
2020-06-08 01:11:10 +10:00
|
|
|
if user != self.author and not user.groups.filter(name='Treasury').exists():
|
|
|
|
return False
|
|
|
|
|
2020-07-15 17:03:52 +10:00
|
|
|
if self.state in (ClaimState.AWAIT_REVIEW.value, ClaimState.APPROVED.value, ClaimState.DRAFT.value, ClaimState.RESUBMIT.value):
|
2019-12-29 00:30:30 +11:00
|
|
|
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
|
2020-07-15 17:03:52 +10:00
|
|
|
if self.state in (ClaimState.DRAFT.value, ClaimState.RESUBMIT.value, ClaimState.AWAIT_REVIEW.value, ClaimState.WITHDRAWN.value):
|
2019-12-29 00:30:30 +11:00
|
|
|
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])
|