Files
sarlink-portal-api/billing/models.py
i701 1554829b9a
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 4m42s
feat(wallet): implement wallet transaction model, views, and serializers for fund management
2025-07-25 14:38:34 +05:00

122 lines
3.9 KiB
Python

from django.db import models
from django.utils import timezone
import uuid
from django.conf import settings
from devices.models import Device
# Create your models here.
user = settings.AUTH_USER_MODEL
# Create your models here.
class Payment(models.Model):
PAYMENT_TYPES = [
("WALLET", "Wallet"),
("TRANSFER", "Transfer"),
]
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
mib_reference = models.CharField(default="", null=True, blank=True)
number_of_months = models.IntegerField()
amount = models.FloatField()
paid = models.BooleanField(default=False)
user = models.ForeignKey(user, on_delete=models.CASCADE, related_name="payments")
paid_at = models.DateTimeField(null=True, blank=True)
method = models.CharField(max_length=255, choices=PAYMENT_TYPES, default="TRANSFER")
expiry_notification_sent = models.BooleanField(default=False)
expires_at = models.DateTimeField(null=True, blank=True)
created_at = models.DateTimeField(default=timezone.now)
updated_at = models.DateTimeField(auto_now=True)
devices = models.ManyToManyField(Device, related_name="payments")
status = models.CharField(
max_length=20,
choices=[
("PENDING", "Pending"),
("PAID", "Paid"),
("CANCELLED", "Cancelled"),
],
default="PENDING",
)
@property
def is_expired(self):
if self.expires_at is None:
return False
return timezone.now() > self.expires_at
def __str__(self):
return f"Payment by {self.user}"
class Meta:
ordering = ["-created_at"]
class BillFormula(models.Model):
formula = models.CharField(max_length=255)
base_amount = models.FloatField()
discount_percentage = models.FloatField()
created_at = models.DateTimeField(default=timezone.now)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.formula
class Topup(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
amount = models.FloatField()
user = models.ForeignKey(user, on_delete=models.CASCADE, related_name="topups")
paid = models.BooleanField(default=False)
paid_at = models.DateTimeField(null=True, blank=True)
status = models.CharField(
max_length=20,
choices=[
("PENDING", "Pending"),
("PAID", "Paid"),
("CANCELLED", "Cancelled"),
],
default="PENDING",
)
mib_reference = models.CharField(default="", null=True, blank=True)
expires_at = models.DateTimeField(null=True, blank=True)
expiry_notification_sent = models.BooleanField(default=False)
created_at = models.DateTimeField(default=timezone.now)
updated_at = models.DateTimeField(auto_now=True)
@property
def is_expired(self):
if self.expires_at is None:
return False
return timezone.now() > self.expires_at
def __str__(self):
return f"Topup for {self.user}"
class Meta:
ordering = ["-created_at"]
class WalletTransaction(models.Model):
TRANSACTION_TYPES = [
("TOPUP", "Topup"),
("DEBIT", "Debit"),
]
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
related_name="wallet_transactions",
)
amount = models.FloatField()
transaction_type = models.CharField(max_length=10, choices=TRANSACTION_TYPES)
description = models.TextField(blank=True, null=True)
reference_id = models.CharField(max_length=255, blank=True, null=True)
created_at = models.DateTimeField(default=timezone.now)
def __str__(self):
return f"{self.transaction_type} {self.amount} ({self.user.username})"
class Meta:
ordering = ["-created_at"]