Files
goodtimes/manage_wallets/models.py
2024-04-24 13:45:16 +05:30

154 lines
5.4 KiB
Python

import uuid
from django.db import models
from accounts.models import BaseModel, IAmPrincipal, IAmPrincipalType
from django.db.models.signals import post_save
from django.dispatch import receiver
from manage_referrals.models import ReferralRecordReward
from manage_subscriptions.models import PrincipalSubscription
# Create your models here.
class Wallet(BaseModel):
principal = models.OneToOneField(IAmPrincipal, on_delete=models.CASCADE)
balance = models.DecimalField(max_digits=14, decimal_places=2, default=0.00)
deposit = models.DecimalField(max_digits=14, decimal_places=2, default=0.00)
earnings = models.DecimalField(max_digits=14, decimal_places=2, default=0.00)
coins = models.BigIntegerField(default=0)
withdrawal_balance = models.DecimalField(
max_digits=14, decimal_places=2, default=0.00
)
class Meta:
db_table = "wallet"
def __str__(self):
return f"Balance: {self.balance} For Principal: {self.principal}"
@receiver(post_save, sender=IAmPrincipal)
def create_wallet(sender, instance, created, **kwargs):
if created:
wallet = Wallet.objects.create(principal=instance)
class TransactionType(models.TextChoices):
PAYMENT = "payment", "Payment"
DEPOSIT = "deposit", "Deposit"
WITHDRAW = "withdraw", "Withdraw"
CREDIT = "credit", "Credit"
class TransactionStatus(models.TextChoices):
SUCCESS = "success", "Success"
FAIL = "fail", "Fail"
INITIATE = "initiate", "Initiate"
class PaymentMethod(models.TextChoices):
CARD = "card", "Card"
UPI = "upi", "UPI"
COIN = "coin", "Coin"
class Transaction(BaseModel):
principal = models.ForeignKey(IAmPrincipal, on_delete=models.CASCADE)
principal_subscription = models.ForeignKey(
PrincipalSubscription,
related_name="transaction_principal_subscription",
on_delete=models.SET_NULL,
blank=True,
null=True,
)
transaction_type = models.CharField(
max_length=10,
choices=TransactionType.choices,
)
payment_method = models.CharField(
max_length=10,
choices=PaymentMethod.choices,
)
transaction_status = models.CharField(
max_length=10,
choices=TransactionStatus.choices,
default=TransactionStatus.INITIATE,
)
amount = models.DecimalField(max_digits=14, decimal_places=2, default=0.00)
coins = models.IntegerField(default=0)
comment = models.CharField(max_length=200, null=True, blank=True)
order_id = models.CharField(unique=True, max_length=255, null=True, blank=True)
product_id = models.CharField(unique=True, max_length=255, null=True, blank=True)
reference_id = models.CharField(max_length=255, null=True, blank=True)
def save(self, *args, **kwargs):
if self.order_id is None and self.created_on and self.id:
self.order_id = self.created_on.strftime("GDTMS%Y%m%dODR") + str(self.id)
return super().save(*args, **kwargs)
class Meta:
db_table = "transaction_history"
def __str__(self):
return f"principal: {self.principal}, type: {self.transaction_type}, status: {self.transaction_status}, amount: {self.amount}"
class StripeConnectAccount(BaseModel):
principal = models.ForeignKey(IAmPrincipal, on_delete=models.CASCADE)
stripe_connect_id = models.CharField(max_length=255, unique=True)
charges_enabled = models.BooleanField(default=False)
transfers_enabled = models.BooleanField(default=False)
details_submitted = models.BooleanField(default=False)
class Meta:
db_table = "stripe_connect"
def __str__(self):
return f"principal: {self.principal}, type: {self.stripe_connect_id}"
class PrincipalBankAccount(BaseModel):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
principal = models.ForeignKey(IAmPrincipal, on_delete=models.CASCADE)
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
account_no = models.CharField(max_length=255)
sort_code = models.CharField(max_length=255)
is_verified = models.BooleanField(default=False)
class Meta:
db_table = "bank_account"
def __str__(self):
return f"principal: {self.principal}, account_no: {self.account_no}"
class WithdrawalRequest(BaseModel):
STATUS_CHOICES = [
("submitted", "Submitted"),
("review", "Review"),
("processing", "Under Process"),
("transferred", "Transferred"),
("dispute", "Dispute"),
("denied", "Denied"),
]
# Update this line to reference IAmPrincipal
principal = models.ForeignKey(
IAmPrincipal, on_delete=models.CASCADE, related_name="withdrawal_requests"
)
coins = models.IntegerField(default=0)
token = models.CharField(max_length=255, blank=True, null=True)
amount = models.DecimalField(max_digits=14, decimal_places=2, default=0.00)
ref_image = models.ImageField(upload_to="withdrawal", null=True, blank=True)
ref_id = models.CharField(unique=True, max_length=255, null=True, blank=True)
notes = models.TextField(null=True, blank=True)
reply = models.TextField(null=True, blank=True)
status = models.CharField(
max_length=20, choices=STATUS_CHOICES, default="submitted"
)
class Meta:
db_table = "withdraw_request"
def __str__(self):
return f"Withdrawal Request by {self.principal.get_full_name()} on {self.created_on.strftime('%Y-%m-%d')}"