diff --git a/apps/warehouse/migrations/0014_remove_inventoryquotasaletransaction_buyer_user_and_more.py b/apps/warehouse/migrations/0014_remove_inventoryquotasaletransaction_buyer_user_and_more.py new file mode 100644 index 0000000..7933bcc --- /dev/null +++ b/apps/warehouse/migrations/0014_remove_inventoryquotasaletransaction_buyer_user_and_more.py @@ -0,0 +1,34 @@ +# Generated by Django 5.0 on 2025-08-25 06:20 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('warehouse', '0013_inventoryquotasaletransaction_pos_device_and_more'), + ] + + operations = [ + migrations.RemoveField( + model_name='inventoryquotasaletransaction', + name='buyer_user', + ), + migrations.RemoveField( + model_name='inventoryquotasaletransaction', + name='is_active', + ), + migrations.RemoveField( + model_name='inventoryquotasaletransaction', + name='sale_status', + ), + migrations.RemoveField( + model_name='inventoryquotasaletransaction', + name='seller_user', + ), + migrations.AddField( + model_name='inventoryquotasaletransaction', + name='transaction_status', + field=models.CharField(choices=[('success', 'SUCCESS'), ('waiting', 'WAITING'), ('failed', 'Failed')], max_length=25, null=True), + ), + ] diff --git a/apps/warehouse/models.py b/apps/warehouse/models.py index 26cfa78..8d0cd45 100644 --- a/apps/warehouse/models.py +++ b/apps/warehouse/models.py @@ -74,18 +74,6 @@ class InventoryQuotaSaleTransaction(BaseModel): related_name='inventory_sales', null=True ) - buyer_user = models.ForeignKey( - User, - on_delete=models.CASCADE, - related_name='buyer_sale_transactions', - null=True - ) - seller_user = models.ForeignKey( - User, - on_delete=models.CASCADE, - related_name='seller_sale_transactions', - null=True - ) weight = models.DecimalField(max_digits=12, decimal_places=2, null=True) delivery_address = models.TextField(blank=True, null=True) product = models.ForeignKey( @@ -98,8 +86,12 @@ class InventoryQuotaSaleTransaction(BaseModel): description = models.TextField(blank=True, null=True) herd_owners_number = models.PositiveBigIntegerField(default=0) transactions_number = models.PositiveBigIntegerField(default=0) - sale_status = models.BooleanField(default=False) - is_active = models.BooleanField(default=0) + status_type = ( + ('success', 'SUCCESS'), + ('waiting', 'WAITING'), + ('failed', 'Failed'), + ) + transaction_status = models.CharField(choices=status_type, max_length=25, null=True) def buyers_count(self): """ number of buyers from specific inventory """ diff --git a/apps/warehouse/pos/api/v1/api.py b/apps/warehouse/pos/api/v1/api.py index e8e6d61..b969601 100644 --- a/apps/warehouse/pos/api/v1/api.py +++ b/apps/warehouse/pos/api/v1/api.py @@ -72,8 +72,45 @@ class InventoryEntryViewSet(viewsets.ModelViewSet, DynamicSearchMixin, POSDevice return self.get_paginated_response(serializer.data) -class InventoryQuotaSaleTransactionViewSet(viewsets.ModelViewSet): +class InventoryQuotaSaleTransactionViewSet(viewsets.ModelViewSet, DynamicSearchMixin, POSDeviceMixin): queryset = warehouse_models.InventoryQuotaSaleTransaction.objects.all() serializer_class = warehouse_serializers.InventoryQuotaSaleTransactionSerializer - filter_backends = [filters.SearchFilter] - search_fields = [''] + search_fields = [ + "rancher__union_name", + "rancher__union_code", + "rancher__first_name", + "rancher__last_name", + "rancher__national_code", + "pos_device__device_identity", + "pos_device__serial", + "transaction_id", + "seller_organization__name", + "quota_distribution__distribution_id", + "inventory_entry__distribution__distribution_id", + "product__name", + "transaction_status", + ] + date_field = "create_date" + + @action( + methods=['get'], + detail=False, + url_path='transactions', + url_name='transactions', + name='transactions', + ) + @transaction.atomic + def transactions(self, request): + """ pos transactions list """ + + # get device object + device = self.get_pos_device() + + queryset = self.queryset.filter(pos_device=device) + queryset = self.filter_query(queryset) + + # paginate & response + page = self.paginate_queryset(queryset) + if page is not None: + serializer = self.get_serializer(page, many=True) + return self.get_paginated_response(serializer.data) \ No newline at end of file diff --git a/apps/warehouse/pos/api/v1/serializers.py b/apps/warehouse/pos/api/v1/serializers.py index ae5c579..d10ff1e 100644 --- a/apps/warehouse/pos/api/v1/serializers.py +++ b/apps/warehouse/pos/api/v1/serializers.py @@ -1,10 +1,9 @@ +from apps.pos_device.pos.api.v1.serializers.device import DeviceSerializer +from apps.herd.pos.api.v1.serializers import RancherSerializer from apps.warehouse.exceptions import ( - InventoryEntryWeightException, TotalInventorySaleException ) -from apps.product.exceptions import QuotaExpiredTimeException from apps.warehouse import models as warehouse_models -from apps.authorization.models import UserRelations from rest_framework import serializers from django.db import models @@ -52,7 +51,7 @@ class InventoryEntrySerializer(serializers.ModelSerializer): class InventoryQuotaSaleTransactionSerializer(serializers.ModelSerializer): - class Meta: + class Meta: # noqa model = warehouse_models.InventoryQuotaSaleTransaction fields = '__all__' depth = 0 @@ -74,20 +73,15 @@ class InventoryQuotaSaleTransactionSerializer(serializers.ModelSerializer): return attrs - def create(self, validated_data): - """ Custom create & set some parameters like seller & buyer """ + def to_representation(self, instance): + """ customize output of transactions serializer """ - distribution = validated_data['quota_distribution'] - seller_organization = distribution.assigned_organization + representation = super().to_representation(instance) - user = self.context['request'].user - buyer_user = user - seller_user = validated_data['inventory_entry'].created_by + representation['rancher'] = RancherSerializer(instance.rancher).data + representation['pos_device'] = DeviceSerializer(instance.pos_device).data + representation['seller_organization'] = instance.seller_organization.name + representation['inventory_entry'] = InventoryEntrySerializer(instance.inventory_entry).data - return warehouse_models.InventoryQuotaSaleTransaction.objects.create( - seller_organization=seller_organization, - seller_user=seller_user, - buyer_user=buyer_user, - **validated_data - ) + return representation