Files
xvatayka/core/apps/management/models/expense.py
Abdulaziz Axmadaliyev 3691e2d068 Initial commit
2026-02-17 19:05:54 +05:00

45 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from django.db import models
from core.apps.management.models import Device
class Expense(models.Model):
class ExpenseType(models.TextChoices):
SALARY = "salary", "Maosh"
UTILITIES = "utilities", "Kommunal tolovlar"
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"
)
comment = models.TextField(blank=True, null=True)
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.MAINTENANCE
] and not self.device:
raise ValidationError({"device": "Device must be set for this type of expense."})