47 lines
1.8 KiB
Python
47 lines
1.8 KiB
Python
from django.db import models
|
||
from core.apps.management.models import Device
|
||
|
||
class Expense(models.Model):
|
||
class ExpenseType(models.TextChoices):
|
||
RENT = "rent", "Ijara"
|
||
SALARY = "salary", "Maosh"
|
||
UTILITIES = "utilities", "Kommunal to‘lovlar"
|
||
MAINTENANCE = "maintenance", "Texnik xizmat"
|
||
FOOD = "food", "Oziq-ovqat"
|
||
TRANSPORT = "transport", "Yo'lkira"
|
||
BUY_TOYS = "buy_toys", "Oʻyinchoqlar sotib olish"
|
||
OTHER = "other", "Boshqa"
|
||
|
||
amount = models.DecimalField(max_digits=12, decimal_places=2)
|
||
expense_type = models.CharField(
|
||
max_length=20,
|
||
choices=ExpenseType.choices,
|
||
default=ExpenseType.OTHER,
|
||
)
|
||
|
||
# Conditional fields
|
||
employee = models.ForeignKey("accounts.User", related_name="salaries", null=True, blank=True, on_delete=models.PROTECT)
|
||
device = models.ForeignKey(Device, null=True, blank=True, on_delete=models.PROTECT)
|
||
|
||
created_by = models.ForeignKey("accounts.User", on_delete=models.PROTECT)
|
||
confirmed_by = models.ForeignKey(
|
||
"accounts.User", on_delete=models.PROTECT,
|
||
null=True, blank=True, related_name="confirmed_expenses"
|
||
)
|
||
|
||
is_confirmed = models.BooleanField(default=False)
|
||
created_at = models.DateTimeField(auto_now_add=True)
|
||
|
||
def clean(self):
|
||
from django.core.exceptions import ValidationError
|
||
|
||
# Salary requires employee
|
||
if self.expense_type == self.ExpenseType.SALARY and not self.employee:
|
||
raise ValidationError({"employee": "Employee must be set for Salary expenses."})
|
||
|
||
# Device required for rent/utilities/maintenance
|
||
if self.expense_type in [
|
||
self.ExpenseType.RENT,
|
||
self.ExpenseType.MAINTENANCE
|
||
] and not self.device:
|
||
raise ValidationError({"device": "Device must be set for this type of expense."}) |