Files
goodtimes/manage_wallets/models.py
rizwanisready db213d3228 wrong commit
2024-02-29 13:25:50 +05:30

89 lines
2.9 KiB
Python

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)
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"
class TransactionStatus(models.TextChoices):
SUCCESS = "success", "Success"
FAIL = "fail", "Fail"
INITIATE = "initiate", "Initiate"
class PaymentMethod(models.TextChoices):
CARD = "card", "Card"
UPI = "upi", "UPI"
class Transaction(BaseModel):
principal = models.ForeignKey(IAmPrincipal, on_delete=models.CASCADE)
principal_type = models.ForeignKey(
IAmPrincipalType,
related_name="transaction_principal_type",
on_delete=models.CASCADE,
)
principal_subscription = models.ForeignKey(
PrincipalSubscription,
related_name="transaction_principal_subscription",
on_delete=models.CASCADE,
)
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)
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}"