qrtr-services/qrtr_account/models.py
2023-08-30 21:45:33 -06:00

136 lines
4.7 KiB
Python

from django.db import models
from user.models import User
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
class Account(models.Model):
owner = models.ForeignKey(User, on_delete=models.CASCADE,
related_name="owned_accounts")
admin_users = models.ManyToManyField(User, related_name="admin_accounts",
blank=True)
view_users = models.ManyToManyField(User, related_name="view_accounts",
blank=True)
name = models.CharField(max_length=250)
@property
def qid(self):
return f"A{self.pk}"
def __str__(self):
return f"{self.name}"
class Institution(models.Model):
name = models.CharField(max_length=255)
@property
def qid(self):
return f"I{self.pk}"
def __str__(self):
return f"{self.name}"
class BankAccount(models.Model):
qrtr_account = models.ForeignKey(Account, on_delete=models.CASCADE)
connection = models.ForeignKey('connection.Connection',
on_delete=models.CASCADE)
institution = models.ForeignKey(Institution, on_delete=models.CASCADE,
related_name="bank_accounts")
acc_id = models.CharField(max_length=250, primary_key=True)
nickname = models.CharField(max_length=250)
official_name = models.CharField(max_length=250,blank=True, null=True)
balance_limit = models.DecimalField(decimal_places=3, max_digits=100, blank=True, null=True)
balance = models.DecimalField(decimal_places=3, max_digits=100)
ac_type = models.CharField(max_length=250, blank=True)
ac_subtype = models.CharField(max_length=250, blank=True)
mask = models.CharField(max_length=4,blank=True)
@property
def qid(self):
return f"B{self.pk}"
def __str__(self):
return f"{self.nickname}"
class Slice(models.Model):
name = models.CharField(max_length=250)
icon = models.CharField(max_length=250)
balance = models.DecimalField(decimal_places=3, max_digits=100, null=True, blank=True)
description = models.TextField(max_length=255, null=True, blank=True)
avail_parents = models.Q(
app_label='qrtr_account',
model='bank') | models.Q(
app_label='qrtr_account',
model='slice')
parent_type = models.ForeignKey(
ContentType,
limit_choices_to=avail_parents,
on_delete=models.CASCADE, null=True, blank=True)
parent_id = models.PositiveIntegerField(null=True)
bank_acc = models.ForeignKey(BankAccount, on_delete=models.CASCADE, null=True, blank=True)
is_unsliced = models.BooleanField(default=False)
slice_of = GenericForeignKey('parent_type', 'parent_id')
@property
def qid(self):
return f"S{self.pk}"
def __str__(self):
return f"{self.name}"
class Schedule(models.Model):
name = models.CharField(max_length=255)
# TODO: Hook this up to an events system for Payday scheduling
class Rule(models.Model):
kinds = [("refill", "Refill"), ("increase", "Increase"), ("goal", "Goal")]
kind = models.CharField(choices=kinds, max_length=255)
when_to_run = models.ForeignKey(Schedule, on_delete=models.CASCADE)
amount_type = models.CharField(
choices=[
("quantity",
"Quantity"),
("round",
"Round"),
("percent",
"Percent")],
default="quantity",
max_length=20)
amount = models.DecimalField(decimal_places=3, max_digits=100)
source = models.ForeignKey(
Slice,
on_delete=models.CASCADE,
related_name="rule_source_set")
destination = models.ForeignKey(
Slice,
on_delete=models.CASCADE,
related_name="rule_destination_set")
class Transaction(models.Model):
authorized_date = models.DateField(null=True)
bank = models.ForeignKey(BankAccount, on_delete=models.CASCADE,
related_name='transactions')
name = models.CharField(max_length=255)
details = models.JSONField()
slice = models.ForeignKey(Slice, on_delete=models.SET_NULL, null=True, related_name='transactions')
trans_id = models.CharField(max_length=255)
updated_at = models.DateTimeField(auto_now=True)
created_at = models.DateTimeField(auto_now_add=True)
is_split = models.BooleanField(default=False)
split_parent = models.ForeignKey("Transaction", on_delete=models.CASCADE, blank=True, null=True, related_name="split_children")
@property
def qid(self):
return f"T{self.pk}"
def __str__(self):
return f"{self.bank} - {self.authorized_date} - {self.name} - {self.details.get('amount')}"