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_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) coin = 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}"