stake holders sharing & quota distribution to CMP - list of stake holder sharings

This commit is contained in:
2025-09-07 13:51:39 +03:30
parent bcc79d2c30
commit 6bac1bbd45
13 changed files with 298 additions and 15 deletions

View File

@@ -1,3 +1,4 @@
from apps.product.web.api.v1.serializers.quota_distribution_serializers import QuotaDistributionSerializer
from apps.authentication.api.v1.serializers.serializer import BankAccountSerializer
from apps.pos_device.web.api.v1.serilaizers import client as client_serializer
from rest_framework.serializers import ModelSerializer
@@ -85,4 +86,28 @@ class StakeHoldersSerializer(ModelSerializer):
instance.organization.bank_information.all().first()
).data
representation['organization'] = {
'name': instance.organization.name,
'id': instance.organization.id
}
return representation
class StakeHolderShareAmountSerializer(ModelSerializer):
class Meta:
model = pos_models.StakeHolderShareAmount
fields = '__all__'
def to_representation(self, instance):
representation = super().to_representation(instance)
# distribution information
representation['quota_distribution'] = QuotaDistributionSerializer(
instance.quota_distribution
).data
# stakeholders information
representation['stakeholders'] = StakeHoldersSerializer(instance.stakeholders).data
return representation

View File

@@ -10,6 +10,7 @@ router.register(r'provider', device_views.ProviderCompanyViewSet, basename='prov
router.register(r'device', device_views.DeviceViewSet, basename='device')
router.register(r'device_assignment', device_views.DeviceAssignmentViewSet, basename='device_assignment')
router.register(r'stake_holders', device_views.StakeHoldersViewSet, basename='stake_holders')
router.register(r'holders_share', device_views.StakeHolderShareAmountViewSet, basename='holders_share')
urlpatterns = [
path('v1/pos/', include(router.urls))

View File

@@ -1,7 +1,7 @@
import random
import string
from datetime import timedelta
from apps.product.web.api.v1.viewsets.quota_distribution_api import QuotaDistributionViewSet
from apps.pos_device.web.api.v1.serilaizers import device as device_serializer
from apps.authentication.exceptions import OrganizationBankAccountException
from apps.authorization.api.v1.serializers import UserRelationSerializer
@@ -337,3 +337,102 @@ class StakeHoldersViewSet(viewsets.ModelViewSet, DynamicSearchMixin, SoftDeleteM
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
@action(
methods=['get'],
detail=False,
url_path='list_by_organization',
url_name='list_by_organization',
name='list_by_organization',
)
def list_by_organization(self, request):
""" list of stakeholders by organization """
org = get_organization_by_user(request.user)
stakeholders = self.queryset.filter(
assignment__client__organization=org,
organization__type__key='CMP'
)
# paginate stakeholders
page = self.paginate_queryset(stakeholders)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
class StakeHolderShareAmountViewSet(viewsets.ModelViewSet, DynamicSearchMixin, SoftDeleteMixin):
queryset = pos_models.StakeHolderShareAmount.objects.select_related('quota_distribution', 'stakeholders')
serializer_class = device_serializer.StakeHolderShareAmountSerializer
@transaction.atomic
def create(self, request, *args, **kwargs):
""" create share amount for company stakeholders """
data = request.data.copy()
organization = get_organization_by_user(request.user)
data.update({'registering_organization': organization.id})
assigner_organization = get_organization_by_user(request.user)
data['distribution'].update({'assigner_organization': assigner_organization.id})
# create distribution
if 'distribution' in data.keys():
distribution = CustomOperations().custom_create(
request=request,
view=QuotaDistributionViewSet(),
data=data['distribution']
)
data.update({'quota_distribution': distribution['id']})
serializer = self.serializer_class(data=data)
if serializer.is_valid(raise_exception=True):
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_403_FORBIDDEN)
@action(
methods=['get'],
detail=False,
url_path='my_sharing_distributes',
url_name='my_sharing_distributes',
name='my_sharing_distributes',
)
def my_shared_distributes(self, request):
""" list of my shared stakeholders with detail """
organization = get_organization_by_user(request.user)
stakeholders_sharing = self.queryset.filter(
registering_organization=organization
).order_by('-create_date')
# paginate stakeholders
page = self.paginate_queryset(stakeholders_sharing)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
@action(
methods=['get'],
detail=True,
url_path='shared_by_distribution',
url_name='shared_by_distribution',
name='shared_by_distribution',
)
@transaction.atomic
def shared_by_distribution(self, request, pk=None):
""" list of shared stakeholder with distribution """
stakeholder_sharing = self.queryset.filter(
quota_distribution_id=pk
).order_by('-create_date')
# paginate stakeholders
page = self.paginate_queryset(stakeholder_sharing)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)