add & fix - transaction dashboard / edit distribution with stat validation bug
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from apps.core.models import SystemConfig
|
||||
from apps.herd.services.services import get_rancher_statistic_by_herd, rancher_quota_weight
|
||||
from apps.livestock.web.api.v1.serializers import LiveStockTypeSerializer
|
||||
from apps.pos_device.services.services import pos_organizations_sharing_information
|
||||
from apps.product import models as product_models
|
||||
from apps.product.services.services import quota_live_stock_allocation_info, quota_incentive_plans_info, \
|
||||
quota_attribute_value, quota_pricing_items_by_type
|
||||
from apps.product.web.api.v1.serializers import product_serializers
|
||||
|
||||
|
||||
@@ -239,4 +244,81 @@ class QuotaLiveStockAgeLimitationSerializer(serializers.ModelSerializer):
|
||||
class OrganizationQuotaStatsSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = product_models.OrganizationQuotaStats
|
||||
fields = '__all__'
|
||||
fields = [
|
||||
'id',
|
||||
'create_date',
|
||||
'modify_date',
|
||||
'creator_info',
|
||||
'modifier_info',
|
||||
]
|
||||
|
||||
def to_representation(self, instance: product_models.OrganizationQuotaStats):
|
||||
""" Custom output of serializer """
|
||||
|
||||
representation = super().to_representation(instance)
|
||||
|
||||
organization = self.context['organization']
|
||||
rancher = self.context['rancher']
|
||||
device = self.context['device']
|
||||
|
||||
representation['weight'] = instance.total_amount
|
||||
representation['remaining_weight'] = instance.remaining_amount
|
||||
representation['distributed'] = instance.total_distributed
|
||||
representation['warehouse_entry'] = instance.inventory_received
|
||||
representation['warehouse_balance'] = instance.inventory_entry_balance
|
||||
representation['been_sold'] = instance.sold_amount
|
||||
representation['pre_sale_balance'] = instance.quota.pre_sale_balance
|
||||
representation['free_sale_balance'] = instance.quota.free_sale_balance
|
||||
|
||||
representation['free_sale'] = instance.quota.free_sale
|
||||
representation['pre_sale'] = instance.quota.pre_sale
|
||||
|
||||
if instance.quota:
|
||||
representation['quota'] = {
|
||||
'quota_identity': instance.quota.quota_id,
|
||||
'quota_weight': instance.quota.quota_weight,
|
||||
'pos_sale_type': instance.quota.pos_sale_type,
|
||||
'quota_livestock_allocations': quota_live_stock_allocation_info(
|
||||
instance.quota
|
||||
),
|
||||
'quota_incentive_plans': quota_incentive_plans_info(instance.quota, rancher),
|
||||
'quota_sale_license': instance.quota.sale_license,
|
||||
'has_sale_license': instance.quota.is_in_valid_time(),
|
||||
}
|
||||
|
||||
sale_limitation = SystemConfig.get(
|
||||
"IGNORE_ALL_RANCHER_PURCHASE_LIMITS",
|
||||
quota_identity=instance.quota.quota_id
|
||||
)
|
||||
representation['product'] = {
|
||||
'image': instance.quota.product.img,
|
||||
'name': instance.quota.product.name,
|
||||
'id': instance.quota.product.id,
|
||||
'free_sale_for_all': sale_limitation if sale_limitation else False,
|
||||
'free_sale_for_this_rancher': rancher.ignore_purchase_limit
|
||||
}
|
||||
|
||||
sharing_list = pos_organizations_sharing_information(
|
||||
device,
|
||||
instance.quota,
|
||||
owner_org=organization
|
||||
)
|
||||
|
||||
representation['pricing'] = { # noqa
|
||||
'main_account_sheba': "IR" + organization.bank_information.first().sheba,
|
||||
'pricing_attributes': quota_attribute_value(instance.quota),
|
||||
'sharing': sharing_list,
|
||||
'base_prices': quota_pricing_items_by_type(instance.quota, sharing=sharing_list)
|
||||
}
|
||||
|
||||
if 'rancher' in self.context.keys():
|
||||
# rancher herd & live stock statistics
|
||||
livestock_counts_list, livestock_counts_dict = get_rancher_statistic_by_herd(rancher)
|
||||
representation['rancher_statistics'] = livestock_counts_list
|
||||
|
||||
# rancher live stock statistics by quota distributions
|
||||
representation['rancher_quota_weight_statistics'] = rancher_quota_weight(
|
||||
rancher, quota=instance.quota
|
||||
)
|
||||
|
||||
return representation
|
||||
|
||||
@@ -230,7 +230,7 @@ class OrganizationQuotaStatsViewSet(viewsets.ModelViewSet, DynamicSearchMixin, P
|
||||
Q(quota__pre_sale=True) | Q(quota__free_sale=True) | Q(inventory_received__gt=0)
|
||||
)
|
||||
).order_by('-create_date')
|
||||
|
||||
print(quotas)
|
||||
# check quota distributions for rancher
|
||||
# available_distributions = [
|
||||
# distribution for distribution in distributions if (
|
||||
|
||||
@@ -84,7 +84,8 @@ def update_product_stats(instance: Product, distribution: QuotaDistribution = No
|
||||
QuotaStatsValidator.validate_assigner_has_enough(
|
||||
organization,
|
||||
distribution.quota,
|
||||
distribution.weight
|
||||
distribution.weight,
|
||||
allow_zero=True
|
||||
)
|
||||
if ProductStats.objects.filter(
|
||||
organization=organization,
|
||||
|
||||
@@ -50,14 +50,14 @@ class QuotaStatsValidator:
|
||||
if amount < 0:
|
||||
raise DistributionException("مقدار وزن نباید منفی باشد", status.HTTP_403_FORBIDDEN) # noqa
|
||||
|
||||
if remaining < amount and not allow_zero:
|
||||
if remaining + amount < amount and not allow_zero:
|
||||
if update_operation:
|
||||
raise DistributionException(
|
||||
"مقدار وزن ویرایش شده از وزن باقیمانده بیشتر است", # noqa
|
||||
status.HTTP_403_FORBIDDEN
|
||||
)
|
||||
raise DistributionException(
|
||||
f"تخصیص مقدار {amount} بیشتر از مقدار باقیمانده {remaining} برای سازمان {assigner_org.name} میباشد.",
|
||||
f"تخصیص مقدار {amount} بیشتر از مقدار باقیمانده {remaining + amount} برای سازمان {assigner_org.name} میباشد.",
|
||||
# noqa
|
||||
status.HTTP_403_FORBIDDEN
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user