change: change cash_transaction and payment_type models
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
# Generated by Django 5.2.4 on 2025-09-09 13:31
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('finance', '0010_expence'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='paymenttype',
|
||||
name='total_usd',
|
||||
field=models.PositiveBigIntegerField(default=0),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='paymenttype',
|
||||
name='total_uzs',
|
||||
field=models.PositiveBigIntegerField(default=0),
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='cashtransaction',
|
||||
name='payment_type',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='cashtransaction',
|
||||
name='payment_type',
|
||||
field=models.ManyToManyField(blank=True, related_name='cash_transactions', to='finance.paymenttype'),
|
||||
),
|
||||
]
|
||||
@@ -19,8 +19,8 @@ class CashTransactionFolder(BaseModel):
|
||||
|
||||
class CashTransaction(BaseModel):
|
||||
name = models.CharField(max_length=200, unique=True)
|
||||
payment_type = models.ForeignKey(
|
||||
PaymentType, on_delete=models.CASCADE, related_name='cash_transactions', null=True
|
||||
payment_type = models.ManyToManyField(
|
||||
PaymentType, related_name='cash_transactions', blank=True
|
||||
)
|
||||
employees = models.ManyToManyField(User, related_name='cash_transactions')
|
||||
status = models.BooleanField(default=False)
|
||||
|
||||
@@ -6,6 +6,8 @@ from core.apps.shared.models import BaseModel
|
||||
|
||||
class PaymentType(BaseModel):
|
||||
name = models.CharField(max_length=200, unique=True)
|
||||
total_uzs = models.PositiveBigIntegerField(default=0)
|
||||
total_usd = models.PositiveBigIntegerField(default=0)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@@ -5,6 +5,7 @@ from rest_framework import serializers
|
||||
from core.apps.finance.models import CashTransaction, CashTransactionFolder
|
||||
from core.apps.accounts.models import User
|
||||
from core.apps.finance.models import PaymentType
|
||||
from core.apps.finance.serializers.payment_type import PaymentTypeSerializer
|
||||
|
||||
|
||||
class CashTransactionEmployeeListSerializer(serializers.ModelSerializer):
|
||||
@@ -16,7 +17,7 @@ class CashTransactionEmployeeListSerializer(serializers.ModelSerializer):
|
||||
|
||||
|
||||
class CashTransactionListSerializer(serializers.ModelSerializer):
|
||||
payment_type = serializers.SerializerMethodField(method_name='get_payment_type')
|
||||
payment_type = PaymentTypeSerializer(many=True)
|
||||
employees = CashTransactionEmployeeListSerializer(many=True)
|
||||
|
||||
class Meta:
|
||||
@@ -25,12 +26,6 @@ class CashTransactionListSerializer(serializers.ModelSerializer):
|
||||
'id', 'name', 'payment_type', 'employees', 'status'
|
||||
]
|
||||
|
||||
def get_payment_type(self, obj):
|
||||
return {
|
||||
"id": obj.payment_type.id,
|
||||
"name": obj.payment_type.name
|
||||
}
|
||||
|
||||
|
||||
class CashTransactionUpdateSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
@@ -41,7 +36,7 @@ class CashTransactionUpdateSerializer(serializers.ModelSerializer):
|
||||
|
||||
|
||||
class CashTransactionCreateSerializer(serializers.Serializer):
|
||||
payment_type_id = serializers.UUIDField()
|
||||
payment_type_ids = serializers.ListSerializer(child=serializers.UUIDField(), write_only=True)
|
||||
employee_ids = serializers.ListSerializer(child=serializers.UUIDField(), write_only=True)
|
||||
name = serializers.CharField()
|
||||
status = serializers.BooleanField()
|
||||
@@ -53,27 +48,24 @@ class CashTransactionCreateSerializer(serializers.Serializer):
|
||||
return value
|
||||
|
||||
def validate(self, data):
|
||||
payment_type = PaymentType.objects.filter(id=data['payment_type_id']).first()
|
||||
if not payment_type:
|
||||
raise serializers.ValidationError("Payment Type not found")
|
||||
if data.get('folder_id'):
|
||||
folder = CashTransactionFolder.objects.filter(id=data.get('folder_id')).first()
|
||||
if not folder:
|
||||
raise serializers.ValidationError("Cash Transaction Folder not found")
|
||||
data['folder'] = folder
|
||||
data['payment_type'] = payment_type
|
||||
return data
|
||||
|
||||
def create(self, validated_data):
|
||||
with transaction.atomic():
|
||||
employee_ids = validated_data.pop('employee_ids', [])
|
||||
payment_type_ids = validated_data.pop('payment_type_ids', [])
|
||||
cash_transaction = CashTransaction.objects.create(
|
||||
name=validated_data.get('name'),
|
||||
payment_type=validated_data.get('payment_type'),
|
||||
status=validated_data.get('status'),
|
||||
folder=validated_data.get('folder')
|
||||
)
|
||||
cash_transaction.employees.set(employee_ids)
|
||||
cash_transaction.payment_type.set(payment_type_ids)
|
||||
cash_transaction.save()
|
||||
return cash_transaction
|
||||
|
||||
@@ -7,6 +7,6 @@ class PaymentTypeSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = PaymentType
|
||||
fields = [
|
||||
'id', 'name'
|
||||
'id', 'name', 'total_usd', 'total_uzs'
|
||||
]
|
||||
|
||||
@@ -13,20 +13,20 @@ class CashTransactionListApiView(generics.ListAPIView):
|
||||
permission_classes = [HasRolePermission]
|
||||
required_permissions = []
|
||||
serializer_class = serializers.CashTransactionListSerializer
|
||||
queryset = CashTransaction.objects.select_related('payment_type').prefetch_related('employees')
|
||||
queryset = CashTransaction.objects.prefetch_related('employees', 'payment_type')
|
||||
pagination_class = CustomPageNumberPagination
|
||||
|
||||
|
||||
class CashTransactionCreateApiView(generics.CreateAPIView):
|
||||
serializer_class = serializers.CashTransactionCreateSerializer
|
||||
queryset = CashTransaction.objects.all()
|
||||
queryset = CashTransaction.objects.prefetch_related('employees', 'payment_type')
|
||||
permission_classes = [HasRolePermission]
|
||||
required_permissions = ['project', 'project_folder']
|
||||
|
||||
|
||||
class CashTransactionUpdateApiView(generics.GenericAPIView):
|
||||
serializer_class = serializers.CashTransactionUpdateSerializer
|
||||
queryset = CashTransaction.objects.all()
|
||||
queryset = CashTransaction.objects.prefetch_related('employees', 'payment_type')
|
||||
permission_classes = [HasRolePermission]
|
||||
|
||||
def patch(self, request, id):
|
||||
|
||||
Reference in New Issue
Block a user