device login
This commit is contained in:
144
apps/product/pos/api/v1/viewsets/product_api.py
Normal file
144
apps/product/pos/api/v1/viewsets/product_api.py
Normal file
@@ -0,0 +1,144 @@
|
||||
import datetime
|
||||
from apps.product.pos.api.v1.serializers import product_serializers as product_serializers
|
||||
from apps.product.pos.api.v1.serializers import quota_serializers
|
||||
from common.helpers import get_organization_by_user
|
||||
from rest_framework.exceptions import APIException
|
||||
from apps.product import models as product_models
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework import viewsets, filters
|
||||
from rest_framework import status
|
||||
from django.db import transaction
|
||||
from django.db.models import Q
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
def trash(queryset, pk): # noqa
|
||||
""" sent object to trash """
|
||||
obj = queryset.get(id=pk)
|
||||
obj.trash = True
|
||||
obj.save()
|
||||
|
||||
|
||||
def delete(queryset, pk):
|
||||
""" full delete object """
|
||||
obj = queryset.get(id=pk)
|
||||
obj.delete()
|
||||
|
||||
|
||||
class ProductCategoryViewSet(viewsets.ModelViewSet):
|
||||
queryset = product_models.ProductCategory.objects.all()
|
||||
serializer_class = product_serializers.ProductCategorySerializer
|
||||
filter_backends = [filters.SearchFilter]
|
||||
search_fields = ['type', 'name']
|
||||
|
||||
@action(
|
||||
methods=['put'],
|
||||
detail=True,
|
||||
url_path='trash',
|
||||
url_name='trash',
|
||||
name='trash',
|
||||
)
|
||||
@transaction.atomic
|
||||
def trash(self, request, pk=None):
|
||||
""" Sent product to trash """
|
||||
try:
|
||||
trash(self.queryset, pk)
|
||||
except APIException as e:
|
||||
return Response(e, status.HTTP_204_NO_CONTENT)
|
||||
|
||||
@action(
|
||||
methods=['post'],
|
||||
detail=True,
|
||||
url_name='delete',
|
||||
url_path='delete',
|
||||
name='delete'
|
||||
)
|
||||
@transaction.atomic
|
||||
def delete(self, request, pk=None):
|
||||
""" Full delete of product object """
|
||||
try:
|
||||
delete(self.queryset, pk)
|
||||
return Response(status=status.HTTP_200_OK)
|
||||
except APIException as e:
|
||||
return Response(e, status=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
|
||||
class ProductViewSet(viewsets.ModelViewSet):
|
||||
queryset = product_models.Product.objects.all()
|
||||
serializer_class = product_serializers.ProductSerializer
|
||||
filter_backends = [filters.SearchFilter]
|
||||
search_fields = ['type', 'name']
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
""" custom list view """ #
|
||||
|
||||
queryset = self.filter_queryset(self.get_queryset().order_by('-create_date')) # noqa
|
||||
|
||||
page = self.paginate_queryset(queryset)
|
||||
if page is not None:
|
||||
serializer = self.get_serializer(page, many=True)
|
||||
return self.get_paginated_response(serializer.data)
|
||||
|
||||
serializer = self.get_serializer(queryset, many=True)
|
||||
return Response(serializer.data)
|
||||
|
||||
@action(
|
||||
methods=['get'],
|
||||
detail=True,
|
||||
url_path='related_quotas',
|
||||
url_name='related_quotas',
|
||||
name='related_quotas'
|
||||
)
|
||||
@transaction.atomic()
|
||||
def my_related_quotas_by_product(self, request, pk=None):
|
||||
""" quotas that related to my organization and product """
|
||||
|
||||
organization = get_organization_by_user(request.user)
|
||||
quota = product_models.Quota.objects.filter(
|
||||
Q(
|
||||
distributions_assigned__in=product_models.QuotaDistribution.objects.filter(
|
||||
Q(assigned_organization=organization) |
|
||||
Q(assigner_organization=organization)
|
||||
)
|
||||
) |
|
||||
Q(registerer_organization=organization),
|
||||
product=self.get_object()
|
||||
).distinct()
|
||||
|
||||
page = self.paginate_queryset(quota)
|
||||
if page is not None:
|
||||
serializer = quota_serializers.QuotaSerializer(page, many=True)
|
||||
return self.get_paginated_response(serializer.data)
|
||||
|
||||
@action(
|
||||
methods=['put'],
|
||||
detail=True,
|
||||
url_path='trash',
|
||||
url_name='trash',
|
||||
name='trash',
|
||||
)
|
||||
@transaction.atomic
|
||||
def trash(self, request, pk=None):
|
||||
""" Sent product to trash """
|
||||
try:
|
||||
trash(self.queryset, pk)
|
||||
except APIException as e:
|
||||
return Response(e, status.HTTP_204_NO_CONTENT)
|
||||
|
||||
@action(
|
||||
methods=['post'],
|
||||
detail=True,
|
||||
url_name='delete',
|
||||
url_path='delete',
|
||||
name='delete'
|
||||
)
|
||||
@transaction.atomic
|
||||
def delete(self, request, pk=None):
|
||||
""" Full delete of product object """
|
||||
try:
|
||||
delete(self.queryset, pk)
|
||||
return Response(status=status.HTTP_200_OK)
|
||||
except APIException as e:
|
||||
return Response(e, status=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
347
apps/product/pos/api/v1/viewsets/quota_api.py
Normal file
347
apps/product/pos/api/v1/viewsets/quota_api.py
Normal file
@@ -0,0 +1,347 @@
|
||||
from apps.product.pos.api.v1.serializers import quota_distribution_serializers
|
||||
from apps.product.web.api.v1.serializers import quota_serializers
|
||||
from apps.product.exceptions import QuotaExpiredTimeException
|
||||
from apps.core.mixins.search_mixin import DynamicSearchMixin
|
||||
from apps.core.pagination import CustomPageNumberPagination
|
||||
from apps.product.web.api.v1.viewsets import product_api
|
||||
from common.helpers import get_organization_by_user
|
||||
from rest_framework.exceptions import APIException
|
||||
from apps.product import models as product_models
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework import viewsets, filters
|
||||
from common.tools import CustomOperations
|
||||
from rest_framework import status
|
||||
from django.db import transaction
|
||||
from django.db.models import Q
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
def trash(queryset, pk): # noqa
|
||||
""" sent object to trash """
|
||||
obj = queryset.get(id=pk)
|
||||
obj.trash = True
|
||||
obj.save()
|
||||
|
||||
|
||||
def delete(queryset, pk):
|
||||
""" full delete object """
|
||||
obj = queryset.get(id=pk)
|
||||
obj.delete()
|
||||
|
||||
|
||||
class QuotaViewSet(viewsets.ModelViewSet, DynamicSearchMixin): # noqa
|
||||
""" apis for product quota """
|
||||
|
||||
queryset = product_models.Quota.objects.all()
|
||||
serializer_class = quota_serializers.QuotaSerializer
|
||||
filter_backends = [filters.SearchFilter]
|
||||
CustomPageNumberPagination.page_size = 5
|
||||
search_fields = [
|
||||
"registerer_organization__name",
|
||||
"quota_id",
|
||||
"product__name",
|
||||
"sale_type",
|
||||
"sale_unit__unit",
|
||||
"group",
|
||||
]
|
||||
|
||||
@action(
|
||||
methods=['get'],
|
||||
detail=False,
|
||||
url_path='active_quotas',
|
||||
url_name='active_quotas',
|
||||
name='active_quotas'
|
||||
)
|
||||
@transaction.atomic
|
||||
def active_quotas(self, request):
|
||||
""" list of organization active quotas """
|
||||
|
||||
queryset = self.filter_query(self.queryset) # return by search param or all objects
|
||||
|
||||
organization = get_organization_by_user(request.user)
|
||||
|
||||
# paginate queryset
|
||||
page = self.paginate_queryset(
|
||||
queryset.filter(
|
||||
Q(registerer_organization=organization),
|
||||
Q(is_closed=False)
|
||||
).order_by('-modify_date')
|
||||
)
|
||||
if page is not None:
|
||||
serializer = self.get_serializer(page, many=True)
|
||||
return self.get_paginated_response(serializer.data)
|
||||
|
||||
@action(
|
||||
methods=['get'],
|
||||
detail=False,
|
||||
url_path='closed_quotas',
|
||||
url_name='closed_quotas',
|
||||
name='closed_quotas'
|
||||
)
|
||||
@transaction.atomic
|
||||
def closed_quotas(self, request):
|
||||
""" list of organization closed quotas """
|
||||
|
||||
queryset = self.filter_query(self.queryset) # return by search param or all objects
|
||||
|
||||
organization = get_organization_by_user(request.user)
|
||||
|
||||
# paginate queryset
|
||||
page = self.paginate_queryset(
|
||||
queryset.filter(
|
||||
Q(registerer_organization=organization),
|
||||
Q(is_closed=True)
|
||||
).order_by('-modify_date')
|
||||
)
|
||||
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='distributions_by_quota',
|
||||
url_name='distributions_by_quota',
|
||||
name='distributions_by_quota'
|
||||
)
|
||||
def get_distributions_by_quota(self, request, pk=None):
|
||||
""" list of distributions by quota """
|
||||
|
||||
try:
|
||||
quota = self.get_object()
|
||||
queryset = self.filter_query(
|
||||
quota.distributions_assigned.all().order_by('-modify_date')
|
||||
) # return by search param or all objects
|
||||
|
||||
# paginate queryset
|
||||
page = self.paginate_queryset(
|
||||
queryset
|
||||
)
|
||||
if page is not None:
|
||||
serializer = quota_distribution_serializers.QuotaDistributionSerializer(
|
||||
page, many=True
|
||||
)
|
||||
return self.get_paginated_response(serializer.data)
|
||||
except Exception as e:
|
||||
raise APIException("none object", code=403)
|
||||
|
||||
@action(
|
||||
methods=['get'],
|
||||
detail=True,
|
||||
url_path='quotas_information',
|
||||
url_name='quotas_information',
|
||||
name='quotas_information'
|
||||
)
|
||||
@transaction.atomic
|
||||
def quotas_information_by_product(self, request, pk=None):
|
||||
""" get quotas information of a product """
|
||||
|
||||
quotas = self.queryset.select_related('product').filter(
|
||||
product_id=pk, is_closed=False
|
||||
)
|
||||
|
||||
try:
|
||||
quota_serializer = self.serializer_class(quotas, many=True).data
|
||||
return Response(quota_serializer, status=status.HTTP_200_OK)
|
||||
except APIException as e:
|
||||
raise APIException(detail="data error", code=400)
|
||||
|
||||
@action(
|
||||
methods=['get'],
|
||||
detail=False,
|
||||
url_path='quotas_info_by_org',
|
||||
url_name='quotas_info_by_org',
|
||||
name='quotas_info_by_org'
|
||||
)
|
||||
def quotas_information_by_organization(self, request):
|
||||
""" get quotas information of an organization """
|
||||
|
||||
quotas = self.queryset.filter(
|
||||
Q(assigned_organizations=get_organization_by_user(request.user)) |
|
||||
Q(registerer_organization=get_organization_by_user(request.user))
|
||||
)
|
||||
|
||||
serializer = self.serializer_class(quotas, many=True).data
|
||||
return Response(serializer, status=status.HTTP_200_OK)
|
||||
|
||||
@action(
|
||||
methods=['put'],
|
||||
detail=True,
|
||||
url_path='trash',
|
||||
url_name='trash',
|
||||
name='trash',
|
||||
)
|
||||
@transaction.atomic
|
||||
def trash(self, request, pk=None):
|
||||
""" Sent quota to trash """
|
||||
try:
|
||||
trash(self.queryset, pk)
|
||||
except APIException as e:
|
||||
return Response(e, status.HTTP_204_NO_CONTENT)
|
||||
|
||||
@action(
|
||||
methods=['post'],
|
||||
detail=True,
|
||||
url_name='delete',
|
||||
url_path='delete',
|
||||
name='delete'
|
||||
)
|
||||
@transaction.atomic
|
||||
def delete(self, request, pk=None):
|
||||
""" Full delete of quota object """
|
||||
try:
|
||||
delete(self.queryset, pk)
|
||||
return Response(status=status.HTTP_200_OK)
|
||||
except APIException as e:
|
||||
return Response(e, status=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
|
||||
class QuotaIncentiveAssignmentViewSet(viewsets.ModelViewSet): # noqa
|
||||
""" apis for incentive assignment """
|
||||
|
||||
queryset = product_models.QuotaIncentiveAssignment.objects.all()
|
||||
serializer_class = quota_serializers.QuotaIncentiveAssignmentSerializer
|
||||
|
||||
@action(
|
||||
methods=['put'],
|
||||
detail=True,
|
||||
url_path='trash',
|
||||
url_name='trash',
|
||||
name='trash',
|
||||
)
|
||||
@transaction.atomic
|
||||
def trash(self, request, pk=None):
|
||||
""" Sent quota incentive assignment to trash """
|
||||
try:
|
||||
trash(self.queryset, pk)
|
||||
except APIException as e:
|
||||
return Response(e, status.HTTP_204_NO_CONTENT)
|
||||
|
||||
@action(
|
||||
methods=['post'],
|
||||
detail=True,
|
||||
url_name='delete',
|
||||
url_path='delete',
|
||||
name='delete'
|
||||
)
|
||||
@transaction.atomic
|
||||
def delete(self, request, pk=None):
|
||||
""" Full delete of quota incentive assignment object """
|
||||
try:
|
||||
delete(self.queryset, pk)
|
||||
return Response(status=status.HTTP_200_OK)
|
||||
except APIException as e:
|
||||
return Response(e, status=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
|
||||
class QuotaBrokerValueViewSet(viewsets.ModelViewSet): # noqa
|
||||
""" apis for quota broker value """
|
||||
|
||||
queryset = product_models.QuotaBrokerValue.objects.all()
|
||||
serializer_class = quota_serializers.QuotaBrokerValueSerializer
|
||||
|
||||
@action(
|
||||
methods=['put'],
|
||||
detail=True,
|
||||
url_path='trash',
|
||||
url_name='trash',
|
||||
name='trash',
|
||||
)
|
||||
@transaction.atomic
|
||||
def trash(self, request, pk=None):
|
||||
""" Sent quota broker value to trash """
|
||||
try:
|
||||
trash(self.queryset, pk)
|
||||
except APIException as e:
|
||||
return Response(e, status.HTTP_204_NO_CONTENT)
|
||||
|
||||
@action(
|
||||
methods=['post'],
|
||||
detail=True,
|
||||
url_name='delete',
|
||||
url_path='delete',
|
||||
name='delete'
|
||||
)
|
||||
@transaction.atomic
|
||||
def delete(self, request, pk=None):
|
||||
""" Full delete of quota broker value object """
|
||||
try:
|
||||
delete(self.queryset, pk)
|
||||
return Response(status=status.HTTP_200_OK)
|
||||
except APIException as e:
|
||||
return Response(e, status=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
|
||||
class QuotaLiveStockAllocationViewSet(viewsets.ModelViewSet):
|
||||
""" apis for quota livestock allocation """
|
||||
|
||||
queryset = product_models.QuotaLivestockAllocation.objects.all()
|
||||
serializer_class = quota_serializers.QuotaLiveStockAllocationSerializer
|
||||
|
||||
@action(
|
||||
methods=['put'],
|
||||
detail=True,
|
||||
url_path='trash',
|
||||
url_name='trash',
|
||||
name='trash',
|
||||
)
|
||||
@transaction.atomic
|
||||
def trash(self, request, pk=None):
|
||||
""" Sent quota livestock allocation to trash """
|
||||
try:
|
||||
trash(self.queryset, pk)
|
||||
except APIException as e:
|
||||
return Response(e, status.HTTP_204_NO_CONTENT)
|
||||
|
||||
@action(
|
||||
methods=['post'],
|
||||
detail=True,
|
||||
url_name='delete',
|
||||
url_path='delete',
|
||||
name='delete'
|
||||
)
|
||||
@transaction.atomic
|
||||
def delete(self, request, pk=None):
|
||||
""" Full delete of quota livestock allocation object """
|
||||
try:
|
||||
delete(self.queryset, pk)
|
||||
return Response(status=status.HTTP_200_OK)
|
||||
except APIException as e:
|
||||
return Response(e, status=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
|
||||
class QuotaLiveStockAgeLimitation(viewsets.ModelViewSet):
|
||||
queryset = product_models.QuotaLiveStockAgeLimitation.objects.all() # noqa
|
||||
serializer_class = quota_serializers.QuotaLiveStockAgeLimitationSerializer
|
||||
|
||||
@action(
|
||||
methods=['put'],
|
||||
detail=True,
|
||||
url_path='trash',
|
||||
url_name='trash',
|
||||
name='trash',
|
||||
)
|
||||
@transaction.atomic
|
||||
def trash(self, request, pk=None):
|
||||
""" Sent quota livestock age limitation to trash """
|
||||
try:
|
||||
trash(self.queryset, pk)
|
||||
except APIException as e:
|
||||
return Response(e, status.HTTP_204_NO_CONTENT)
|
||||
|
||||
@action(
|
||||
methods=['post'],
|
||||
detail=True,
|
||||
url_name='delete',
|
||||
url_path='delete',
|
||||
name='delete'
|
||||
)
|
||||
@transaction.atomic
|
||||
def delete(self, request, pk=None):
|
||||
""" Full delete of quota livestock age limitation object """
|
||||
try:
|
||||
delete(self.queryset, pk)
|
||||
return Response(status=status.HTTP_200_OK)
|
||||
except APIException as e:
|
||||
return Response(e, status=status.HTTP_204_NO_CONTENT)
|
||||
128
apps/product/pos/api/v1/viewsets/quota_distribution_api.py
Normal file
128
apps/product/pos/api/v1/viewsets/quota_distribution_api.py
Normal file
@@ -0,0 +1,128 @@
|
||||
from apps.product.pos.api.v1.serializers import quota_distribution_serializers as distribution_serializers
|
||||
from apps.core.mixins.search_mixin import DynamicSearchMixin
|
||||
from apps.core.pagination import CustomPageNumberPagination
|
||||
from common.helpers import get_organization_by_user
|
||||
from rest_framework.exceptions import APIException
|
||||
from apps.product import models as product_models
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework import viewsets, filters
|
||||
from rest_framework import status
|
||||
from django.db import transaction
|
||||
from django.db.models import Q
|
||||
|
||||
|
||||
def trash(queryset, pk): # noqa
|
||||
""" sent object to trash """
|
||||
obj = queryset.get(id=pk)
|
||||
obj.trash = True
|
||||
obj.save()
|
||||
|
||||
|
||||
def delete(queryset, pk):
|
||||
""" full delete object """
|
||||
obj = queryset.get(id=pk)
|
||||
obj.delete()
|
||||
|
||||
|
||||
class QuotaDistributionViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
|
||||
""" quota distribution apis """
|
||||
|
||||
queryset = product_models.QuotaDistribution.objects.all()
|
||||
serializer_class = distribution_serializers.QuotaDistributionSerializer
|
||||
filter_backends = [filters.SearchFilter]
|
||||
CustomPageNumberPagination.page_size = 5
|
||||
search_fields = [
|
||||
"assigner_organization__name",
|
||||
"assigned_organization__name",
|
||||
"distribution_id",
|
||||
"quota__quota_id",
|
||||
"quota__product__name",
|
||||
"quota__sale_type",
|
||||
"quota__group",
|
||||
]
|
||||
date_field = "create_date"
|
||||
|
||||
@action(
|
||||
methods=['get'],
|
||||
detail=False,
|
||||
url_name='my_distributions',
|
||||
url_path='my_distributions',
|
||||
name='my_distributions'
|
||||
)
|
||||
def my_distributions(self, request):
|
||||
""" list of my distributions """
|
||||
|
||||
queryset = self.filter_query(self.queryset) # return by search param or all objects
|
||||
organization = get_organization_by_user(request.user)
|
||||
|
||||
query = self.request.query_params
|
||||
if query.get('param') == 'assigned':
|
||||
# paginate queryset
|
||||
page = self.paginate_queryset(
|
||||
queryset.filter(
|
||||
Q(assigned_organization=organization)
|
||||
).order_by('-modify_date')
|
||||
)
|
||||
|
||||
elif query.get('param') == 'assigner':
|
||||
# paginate queryset
|
||||
page = self.paginate_queryset(
|
||||
queryset.filter(
|
||||
Q(assigner_organization=organization)
|
||||
).order_by('-modify_date')
|
||||
)
|
||||
|
||||
elif query.get('param') == 'all':
|
||||
# paginate queryset
|
||||
page = self.paginate_queryset(
|
||||
queryset.filter(
|
||||
Q(assigner_organization=organization) |
|
||||
Q(assigned_organization=organization)
|
||||
).order_by('-modify_date')
|
||||
)
|
||||
|
||||
if page is not None: # noqa
|
||||
serializer = self.get_serializer(page, many=True) # noqa
|
||||
return self.get_paginated_response(serializer.data)
|
||||
|
||||
@action(
|
||||
methods=['put'],
|
||||
detail=True,
|
||||
url_path='trash',
|
||||
url_name='trash',
|
||||
name='trash',
|
||||
)
|
||||
@transaction.atomic
|
||||
def trash(self, request, pk=None):
|
||||
""" Sent quota distribution to trash """
|
||||
quota_distribution = self.get_object()
|
||||
|
||||
# check if distribution has inventory entry
|
||||
if quota_distribution.inventory_entry.exists():
|
||||
raise APIException(
|
||||
"امکان حذف این توزیع وجود ندارد. ورود به انبار برای آن ثبت شده است", # noqa
|
||||
status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
try:
|
||||
trash(self.queryset, pk)
|
||||
except APIException as e:
|
||||
return Response(e, status.HTTP_204_NO_CONTENT)
|
||||
|
||||
@transaction.atomic
|
||||
def destroy(self, request, pk=None, *args, **kwargs):
|
||||
""" Full delete of quota distribution object """
|
||||
|
||||
quota_distribution = self.get_object()
|
||||
|
||||
# check if distribution has inventory entry
|
||||
if quota_distribution.inventory_entry.exists():
|
||||
raise APIException(
|
||||
"امکان حذف این توزیع وجود ندارد. ورود به انبار برای آن ثبت شده است", # noqa
|
||||
status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
try:
|
||||
delete(self.queryset, pk)
|
||||
return Response(status=status.HTTP_200_OK)
|
||||
except Exception as e:
|
||||
return Response(e, status=status.HTTP_204_NO_CONTENT)
|
||||
Reference in New Issue
Block a user