device login
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from django.db import models
|
||||
from django.conf import settings
|
||||
from crum import get_current_user
|
||||
from django.contrib.auth.models import AnonymousUser
|
||||
|
||||
|
||||
class BaseModel(models.Model):
|
||||
@@ -29,6 +30,7 @@ class BaseModel(models.Model):
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
user = get_current_user() # get user object
|
||||
if not isinstance(user, AnonymousUser):
|
||||
self.modified_by = user
|
||||
if not self.creator_info:
|
||||
self.created_by = user
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
# Generated by Django 5.0 on 2025-08-17 12:03
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('pos_device', '0059_device_pre_registered_alter_device_acceptor_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='sessions',
|
||||
name='latitude',
|
||||
field=models.FloatField(default=0, null=True),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='sessions',
|
||||
name='longitude',
|
||||
field=models.FloatField(default=0, null=True),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='sessions',
|
||||
name='version',
|
||||
field=models.IntegerField(default=0, null=True),
|
||||
),
|
||||
]
|
||||
@@ -127,13 +127,13 @@ class Sessions(BaseModel):
|
||||
)
|
||||
name = models.CharField(max_length=250, null=True)
|
||||
password = models.CharField(max_length=25, null=True)
|
||||
version = models.IntegerField(default=0)
|
||||
version = models.IntegerField(default=0, null=True)
|
||||
mac = models.CharField(max_length=50, null=True)
|
||||
ip = models.CharField(max_length=15, default='0.0.0.0')
|
||||
sdk = models.TextField(null=True)
|
||||
serial = models.TextField(null=True)
|
||||
latitude = models.FloatField(default=0)
|
||||
longitude = models.FloatField(default=0)
|
||||
latitude = models.FloatField(default=0, null=True)
|
||||
longitude = models.FloatField(default=0, null=True)
|
||||
|
||||
def __str__(self):
|
||||
return f'Session: {self.name}-{self.version}-{self.id}'
|
||||
|
||||
@@ -42,13 +42,13 @@ class POSDeviceViewSet(viewsets.ModelViewSet):
|
||||
|
||||
# check if device exists
|
||||
if 'device_identity' in request.data.keys():
|
||||
device = self.device_queryset.filter(device_identity=request.data['device_identity'])
|
||||
device = self.device_queryset.filter(device_identity=request.data['device_identity']).first()
|
||||
else:
|
||||
device = self.device_queryset.filter(serial=serial).first()
|
||||
|
||||
# activate device
|
||||
if device:
|
||||
if not device.is_activated:
|
||||
if not device.is_activated and not device.pre_registered:
|
||||
device.is_activated = True
|
||||
device.save()
|
||||
|
||||
@@ -71,16 +71,15 @@ class POSDeviceViewSet(viewsets.ModelViewSet):
|
||||
|
||||
pre_device = pos_models.Device.objects.create(
|
||||
serial=serial,
|
||||
sdk=sdk,
|
||||
organization=organization,
|
||||
pre_regitered=True,
|
||||
pre_registered=True,
|
||||
is_activated=False
|
||||
)
|
||||
|
||||
return Response({
|
||||
"message": "device pre-registered",
|
||||
"device_identity": pre_device.device_identity
|
||||
}, status=status.HTTP_200_OK)
|
||||
}, status=status.HTTP_412_PRECONDITION_FAILED)
|
||||
|
||||
@action(
|
||||
methods=['post'],
|
||||
@@ -93,10 +92,11 @@ class POSDeviceViewSet(viewsets.ModelViewSet):
|
||||
def merge_devices(self, request):
|
||||
""" merge pre register device & device has registered by psp user """
|
||||
|
||||
pre_device = self.device_queryset.get(device_identity=request.data['pre_device'])
|
||||
real_device = self.device_queryset.get(device_identity=request.data['real_device'])
|
||||
pre_device = self.device_queryset.get(device_identity=request.data['pre_device_identity'])
|
||||
real_device = self.device_queryset.get(device_identity=request.data['real_device_identity'])
|
||||
|
||||
real_device.device_identity = pre_device.device_identity
|
||||
real_device.is_activated = True
|
||||
real_device.save()
|
||||
|
||||
pre_device.delete()
|
||||
|
||||
@@ -1 +1,11 @@
|
||||
import typing
|
||||
|
||||
|
||||
def get_device_organization(assignment: object = None, device_serial: str = None) -> typing.Any:
|
||||
""" get device owner (organization) information """
|
||||
|
||||
organization = assignment.objects.filter( # noqa
|
||||
device__serial=device_serial
|
||||
).first()
|
||||
|
||||
return organization
|
||||
|
||||
157
apps/product/pos/api/v1/serializers/product_serializers.py
Normal file
157
apps/product/pos/api/v1/serializers/product_serializers.py
Normal file
@@ -0,0 +1,157 @@
|
||||
from rest_framework import serializers
|
||||
from apps.product import models as product_models
|
||||
from apps.authorization.api.v1 import serializers as authorize_serializers
|
||||
from apps.authentication.api.v1.serializers.serializer import OrganizationSerializer, OrganizationTypeSerializer
|
||||
|
||||
|
||||
class ProductCategorySerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = product_models.ProductCategory
|
||||
fields = '__all__'
|
||||
|
||||
|
||||
class ProductSerializer(serializers.ModelSerializer):
|
||||
""" Serializer of product """
|
||||
|
||||
class Meta:
|
||||
model = product_models.Product
|
||||
fields = '__all__'
|
||||
|
||||
def to_representation(self, instance):
|
||||
""" Custom output of product serializer """
|
||||
|
||||
representation = super().to_representation(instance)
|
||||
if instance.category:
|
||||
representation['category'] = {
|
||||
'id': instance.category.id,
|
||||
'name': instance.category.name
|
||||
}
|
||||
|
||||
return representation
|
||||
|
||||
|
||||
class ProductStatsSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = product_models.ProductStats
|
||||
fields = '__all__'
|
||||
|
||||
def to_representation(self, instance):
|
||||
""" custom output of product stat """
|
||||
|
||||
representation = super().to_representation(instance)
|
||||
|
||||
representation['product'] = {
|
||||
'id': instance.product.id,
|
||||
'name': instance.product.name
|
||||
}
|
||||
|
||||
return representation
|
||||
|
||||
|
||||
class AttributeSerializer(serializers.ModelSerializer):
|
||||
""" serialize attributes of reference product """
|
||||
|
||||
class Meta:
|
||||
model = product_models.Attribute
|
||||
fields = '__all__'
|
||||
|
||||
def to_representation(self, instance):
|
||||
representation = super().to_representation(instance)
|
||||
if instance.product:
|
||||
representation['product'] = {
|
||||
'id': instance.product.id,
|
||||
'name': instance.product.name
|
||||
}
|
||||
|
||||
if instance.type:
|
||||
representation['type'] = {
|
||||
'id': instance.type.id,
|
||||
'unit': instance.type.unit
|
||||
}
|
||||
return representation
|
||||
|
||||
|
||||
class AttributeValueSerializer(serializers.ModelSerializer):
|
||||
""" serialize attribute values for child products """
|
||||
|
||||
class Meta:
|
||||
model = product_models.AttributeValue
|
||||
fields = [
|
||||
"id",
|
||||
"quota",
|
||||
"attribute",
|
||||
"value",
|
||||
]
|
||||
|
||||
def to_representation(self, instance):
|
||||
""" Custom Output of attribute values """
|
||||
representation = super().to_representation(instance)
|
||||
if instance.attribute:
|
||||
representation['attribute_name'] = instance.attribute.name
|
||||
|
||||
return representation
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
instance.quota = validated_data.get('quota', instance.quota)
|
||||
instance.attribute = validated_data.get('attribute', instance.attribute)
|
||||
instance.value = validated_data.get('value', instance.value)
|
||||
instance.save()
|
||||
|
||||
return instance
|
||||
|
||||
|
||||
class BrokerSerializer(serializers.ModelSerializer):
|
||||
""" serialize product broker """
|
||||
|
||||
class Meta:
|
||||
model = product_models.Broker
|
||||
fields = '__all__'
|
||||
depth = 0
|
||||
|
||||
def to_representation(self, instance):
|
||||
representation = super().to_representation(instance)
|
||||
if instance.organization_type:
|
||||
representation['organization_type'] = OrganizationTypeSerializer(
|
||||
instance.organization_type
|
||||
).data
|
||||
|
||||
if instance.product:
|
||||
representation['product'] = ProductSerializer(
|
||||
instance.product
|
||||
).data
|
||||
|
||||
if instance.calculation_strategy:
|
||||
representation['calculation_strategy'] = {
|
||||
'id': instance.calculation_strategy.id,
|
||||
'unit': instance.calculation_strategy.unit
|
||||
}
|
||||
|
||||
return representation
|
||||
|
||||
|
||||
class SaleUnitSerializer(serializers.ModelSerializer):
|
||||
""" serialize unit of products for sale """
|
||||
|
||||
class Meta:
|
||||
model = product_models.SaleUnit
|
||||
fields = [
|
||||
"id",
|
||||
"product",
|
||||
"unit",
|
||||
"required",
|
||||
]
|
||||
|
||||
def to_representation(self, instance):
|
||||
representation = super().to_representation(instance)
|
||||
if instance.product:
|
||||
representation['product'] = {
|
||||
'id': instance.product.id,
|
||||
'name': instance.product.name
|
||||
}
|
||||
return representation
|
||||
|
||||
|
||||
class IncentivePlanSerializer(serializers.ModelSerializer): # noqa
|
||||
class Meta:
|
||||
model = product_models.IncentivePlan
|
||||
fields = '__all__'
|
||||
@@ -0,0 +1,94 @@
|
||||
from rest_framework import serializers
|
||||
from apps.product import models as product_models
|
||||
from rest_framework.exceptions import APIException
|
||||
from apps.product.web.api.v1.serializers.quota_serializers import QuotaSerializer
|
||||
from django.db import models
|
||||
from apps.product.exceptions import (
|
||||
QuotaWeightException,
|
||||
QuotaClosedException,
|
||||
QuotaExpiredTimeException,
|
||||
QuotaLimitByOrganizationException
|
||||
)
|
||||
|
||||
|
||||
class QuotaDistributionSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = product_models.QuotaDistribution
|
||||
fields = '__all__'
|
||||
extra_kwargs = {
|
||||
'assigner_organization': {
|
||||
'required': False
|
||||
}
|
||||
}
|
||||
|
||||
def validate(self, data):
|
||||
"""
|
||||
@ to validate if distribution weight
|
||||
more than quota weight raise exception
|
||||
@ if quota is closed raise exception
|
||||
@ if quota has organization limit, before distribution
|
||||
check assigned organization
|
||||
"""
|
||||
|
||||
quota = data['quota']
|
||||
assigned_organization = data['assigned_organization']
|
||||
amount = data['weight']
|
||||
instance_id = self.instance.id if self.instance else None
|
||||
|
||||
# check quota expired time
|
||||
if not quota.is_in_valid_time():
|
||||
raise QuotaExpiredTimeException()
|
||||
|
||||
# check if quota is closed
|
||||
if quota.is_closed:
|
||||
raise QuotaClosedException()
|
||||
|
||||
# check if assigned organization is in quota limitation
|
||||
if quota.has_organization_limit is True and assigned_organization not in quota.limit_by_organizations.all():
|
||||
raise QuotaLimitByOrganizationException()
|
||||
|
||||
# total quota distributions weight
|
||||
total = product_models.QuotaDistribution.objects.filter(
|
||||
quota=quota
|
||||
).exclude(id=instance_id).aggregate(
|
||||
total=models.Sum('weight')
|
||||
)['total'] or 0
|
||||
if total + amount > quota.quota_weight:
|
||||
raise QuotaWeightException()
|
||||
|
||||
if self.instance:
|
||||
# total warehouse inventory entry
|
||||
total_entry = self.instance.inventory_entry.aggregate(
|
||||
total=models.Sum('weight')
|
||||
)['total'] or 0
|
||||
|
||||
# if inventory entry weight is bigger than distribute weight in edit distribution
|
||||
if total_entry > self.instance.weight:
|
||||
raise APIException("وزن وارد شده کمتر از وزن ورودی به انبار است", code=403) # noqa
|
||||
|
||||
# if weight is more than distribution remaining weight
|
||||
if self.instance.weight > self.instance.remaining_weight:
|
||||
raise APIException("وزن وارد شده بیشتر از وزن باقیمانده است", code=403) # noqa
|
||||
|
||||
return data
|
||||
|
||||
def to_representation(self, instance):
|
||||
""" Custom output of serializer """
|
||||
|
||||
representation = super().to_representation(instance)
|
||||
if instance.quota:
|
||||
representation['quota'] = QuotaSerializer(instance.quota).data
|
||||
|
||||
if instance.assigned_organization:
|
||||
representation['assigned_organization'] = {
|
||||
'organization': instance.assigned_organization.name,
|
||||
'id': instance.assigned_organization.id
|
||||
}
|
||||
|
||||
if instance.assigner_organization:
|
||||
representation['assigner_organization'] = {
|
||||
'organization': instance.assigner_organization.name,
|
||||
'id': instance.assigner_organization.id
|
||||
}
|
||||
|
||||
return representation
|
||||
237
apps/product/pos/api/v1/serializers/quota_serializers.py
Normal file
237
apps/product/pos/api/v1/serializers/quota_serializers.py
Normal file
@@ -0,0 +1,237 @@
|
||||
from apps.authentication.api.v1.serializers.serializer import OrganizationSerializer
|
||||
from apps.authorization.api.v1 import serializers as authorize_serializers
|
||||
from apps.product.web.api.v1.serializers import product_serializers
|
||||
from apps.livestock.web.api.v1.serializers import LiveStockTypeSerializer
|
||||
from apps.product import models as product_models
|
||||
from rest_framework import serializers
|
||||
|
||||
|
||||
class QuotaSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = product_models.Quota
|
||||
fields = '__all__'
|
||||
|
||||
def to_representation(self, instance):
|
||||
representation = super().to_representation(instance)
|
||||
if isinstance(instance, product_models.Quota):
|
||||
if instance.sale_unit:
|
||||
representation['sale_unit'] = product_serializers.SaleUnitSerializer(
|
||||
instance.sale_unit
|
||||
).data
|
||||
representation['product'] = {"product": instance.product.name, "product_id": instance.product.id}
|
||||
representation['incentive_plan'] = QuotaIncentiveAssignmentSerializer(
|
||||
instance.incentive_assignments.all(),
|
||||
many=True
|
||||
).data
|
||||
|
||||
representation['attribute_values'] = product_serializers.AttributeValueSerializer(
|
||||
instance.attribute_values.all(),
|
||||
many=True
|
||||
).data
|
||||
|
||||
representation['brokers'] = QuotaBrokerValueSerializer(
|
||||
instance.broker_values.all(),
|
||||
many=True
|
||||
).data
|
||||
|
||||
representation['livestock_allocations'] = QuotaLiveStockAllocationSerializer(
|
||||
instance.livestock_allocations.all(),
|
||||
many=True
|
||||
).data
|
||||
|
||||
representation['livestock_limitations'] = QuotaLiveStockAgeLimitationSerializer(
|
||||
instance.livestock_age_limitations.all(),
|
||||
many=True
|
||||
).data
|
||||
|
||||
representation['limit_by_organizations'] = [
|
||||
{"name": limit.name, "id": limit.id} for limit in instance.limit_by_organizations.all()
|
||||
]
|
||||
|
||||
return representation
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
""" Custom Update """
|
||||
|
||||
instance.quota_id = validated_data.get('quota_id', instance.quota_id)
|
||||
instance.quota_code = validated_data.get('quota_code', instance.quota_code)
|
||||
instance.quota_weight = validated_data.get('quota_weight', instance.quota_weight)
|
||||
instance.remaining_weight = validated_data.get('remaining_weight', instance.remaining_weight)
|
||||
instance.quota_distributed = validated_data.get('quota_distributed', instance.quota_distributed)
|
||||
instance.product = validated_data.get('product', instance.product)
|
||||
instance.sale_type = validated_data.get('sale_type', instance.sale_type)
|
||||
instance.sale_unit = validated_data.get('sale_unit', instance.sale_type)
|
||||
instance.month_choices = validated_data.get('month_choices', instance.month_choices)
|
||||
instance.sale_license = validated_data.get('sale_license', instance.sale_license)
|
||||
instance.group = validated_data.get('group', instance.group)
|
||||
instance.has_distribution_limit = validated_data.get('has_distribution_limit', instance.has_distribution_limit)
|
||||
instance.distribution_mode = validated_data.get('distribution_mode', instance.distribution_mode)
|
||||
instance.has_organization_limit = validated_data.get('has_organization_limit', instance.has_organization_limit)
|
||||
instance.base_price_factory = validated_data.get('base_price_factory', instance.base_price_factory)
|
||||
instance.base_price_cooperative = validated_data.get('base_price_cooperative', instance.base_price_cooperative)
|
||||
instance.final_price = validated_data.get('final_price', instance.final_price)
|
||||
instance.is_closed = validated_data.get('is_closed', instance.is_closed)
|
||||
instance.closed_at = validated_data.get('closed_at', instance.closed_at)
|
||||
instance.save()
|
||||
|
||||
# update assigned organization many to many
|
||||
if 'assigned_organizations' in validated_data.keys():
|
||||
instance.assigned_organizations.clear()
|
||||
instance.assigned_organizations.add(
|
||||
*(validated_data.get('assigned_organizations', instance.assigned_organizations))
|
||||
)
|
||||
|
||||
# update organization limit many to many
|
||||
if 'limit_by_organizations' in validated_data.keys():
|
||||
instance.limit_by_organizations.clear()
|
||||
instance.limit_by_organizations.add(
|
||||
*(validated_data.get('limit_by_organizations', instance.limit_by_organizations))
|
||||
)
|
||||
else:
|
||||
instance.limit_by_organizations.clear()
|
||||
|
||||
return instance
|
||||
|
||||
|
||||
class QuotaStatsSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = product_models.QuotaStats
|
||||
fields = '__all__'
|
||||
|
||||
|
||||
class QuotaIncentiveAssignmentSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = product_models.QuotaIncentiveAssignment
|
||||
fields = [
|
||||
"id",
|
||||
"quota",
|
||||
"incentive_plan",
|
||||
"heavy_value",
|
||||
"light_value",
|
||||
]
|
||||
|
||||
def to_representation(self, instance):
|
||||
""" Custom Output for incentive plans """
|
||||
|
||||
representation = super().to_representation(instance)
|
||||
representation['incentive_plan_name'] = instance.incentive_plan.name
|
||||
|
||||
return representation
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
""" Custom Update """
|
||||
|
||||
instance.quota = validated_data.get('quota', instance.quota)
|
||||
instance.incentive_plan = validated_data.get('incentive_plan', instance.incentive_plan)
|
||||
instance.heavy_value = validated_data.get('heavy_value', instance.heavy_value)
|
||||
instance.light_value = validated_data.get('light_value', instance.light_value)
|
||||
instance.save()
|
||||
|
||||
return instance
|
||||
|
||||
|
||||
class QuotaBrokerValueSerializer(serializers.ModelSerializer): # noqa
|
||||
class Meta:
|
||||
model = product_models.QuotaBrokerValue
|
||||
fields = [
|
||||
"id",
|
||||
"quota",
|
||||
"broker",
|
||||
"value",
|
||||
]
|
||||
|
||||
def to_representation(self, instance):
|
||||
""" Custom Output of broker values """
|
||||
|
||||
representation = super().to_representation(instance)
|
||||
representation['broker_name'] = instance.broker.name
|
||||
|
||||
return representation
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
""" Custom Update """
|
||||
|
||||
instance.quota = validated_data.get('quota', instance.quota)
|
||||
instance.broker = validated_data.get('broker', instance.broker)
|
||||
instance.value = validated_data.get('value', instance.value)
|
||||
instance.save()
|
||||
|
||||
return instance
|
||||
|
||||
|
||||
class QuotaLiveStockAllocationSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = product_models.QuotaLivestockAllocation
|
||||
fields = [
|
||||
"id",
|
||||
"quota",
|
||||
"livestock_group",
|
||||
"livestock_type",
|
||||
"livestock_subtype",
|
||||
"quantity_kg",
|
||||
]
|
||||
extra_kwargs = {
|
||||
'livestock_group': {
|
||||
'required': False
|
||||
},
|
||||
'livestock_type': {
|
||||
'required': False
|
||||
},
|
||||
'livestock_subtype': {
|
||||
'required': False
|
||||
}
|
||||
}
|
||||
|
||||
def to_representation(self, instance):
|
||||
""" custom output for live stock type """
|
||||
|
||||
representation = super().to_representation(instance)
|
||||
if isinstance(instance, product_models.QuotaLivestockAllocation):
|
||||
representation['livestock_type'] = LiveStockTypeSerializer(instance.livestock_type).data
|
||||
|
||||
return representation
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
""" Custom Update """
|
||||
|
||||
instance.quota = validated_data.get('quota', instance.quota)
|
||||
instance.livestock_group = validated_data.get('livestock_group', instance.livestock_group)
|
||||
instance.livestock_type = validated_data.get('livestock_type', instance.livestock_type)
|
||||
instance.livestock_subtype = validated_data.get('livestock_subtype', instance.livestock_subtype)
|
||||
instance.save()
|
||||
|
||||
return instance
|
||||
|
||||
|
||||
class QuotaLiveStockAgeLimitationSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = product_models.QuotaLiveStockAgeLimitation
|
||||
fields = [
|
||||
"id",
|
||||
"quota",
|
||||
"livestock_type",
|
||||
"livestock_subtype",
|
||||
"age_month",
|
||||
]
|
||||
|
||||
def to_representation(self, instance):
|
||||
""" custom output for livestock type """
|
||||
|
||||
representation = super().to_representation(instance)
|
||||
if isinstance(instance, product_models.QuotaLiveStockAgeLimitation):
|
||||
representation['livestock_type'] = LiveStockTypeSerializer(
|
||||
instance.livestock_type
|
||||
).data
|
||||
|
||||
return representation
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
""" Custom Update """
|
||||
|
||||
instance.quota = validated_data.get('quota', instance.quota)
|
||||
instance.livestock_type = validated_data.get('livestock_type', instance.livestock_type)
|
||||
instance.livestock_subtype = validated_data.get('livestock_subtype', instance.livestock_subtype)
|
||||
instance.age_month = validated_data.get('age_month', instance.age_month)
|
||||
instance.save()
|
||||
|
||||
return instance
|
||||
@@ -0,0 +1,8 @@
|
||||
from django.urls import path, include
|
||||
from rest_framework.routers import DefaultRouter
|
||||
|
||||
router = DefaultRouter()
|
||||
|
||||
urlpatterns = [
|
||||
path('v1/', include(router.urls))
|
||||
]
|
||||
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)
|
||||
@@ -11,8 +11,6 @@ from rest_framework import status
|
||||
from django.db import transaction
|
||||
from django.db.models import Q
|
||||
from datetime import datetime
|
||||
from asgiref.sync import sync_to_async, async_to_sync
|
||||
from django.http import JsonResponse
|
||||
|
||||
|
||||
def trash(queryset, pk): # noqa
|
||||
|
||||
Reference in New Issue
Block a user