From a4bfa46e5dda240f2e8c958eeb174d76c8461ff6 Mon Sep 17 00:00:00 2001 From: Mojtaba-z Date: Wed, 26 Nov 2025 17:39:46 +0330 Subject: [PATCH] fix - warehouse of quota stat in pos deviec complete - v2 --- apps/authentication/apps.py | 2 +- apps/authorization/apps.py | 2 +- apps/herd/services/services.py | 1 + apps/livestock/apps.py | 2 +- apps/product/apps.py | 2 +- apps/warehouse/apps.py | 3 +- apps/warehouse/pos/api/v2/serializers.py | 8 ++-- apps/warehouse/signals/signals_v2.py | 51 ++++++++++++------------ 8 files changed, 38 insertions(+), 33 deletions(-) diff --git a/apps/authentication/apps.py b/apps/authentication/apps.py index 6aba299..2583095 100644 --- a/apps/authentication/apps.py +++ b/apps/authentication/apps.py @@ -6,4 +6,4 @@ class AuthenticationConfig(AppConfig): name = 'apps.authentication' def ready(self): - import apps.authentication.signals + import apps.authentication.signals # noqa diff --git a/apps/authorization/apps.py b/apps/authorization/apps.py index 4aa32c8..7b99114 100644 --- a/apps/authorization/apps.py +++ b/apps/authorization/apps.py @@ -6,4 +6,4 @@ class AuthorizationConfig(AppConfig): name = 'apps.authorization' def ready(self): - import apps.authorization.signals + import apps.authorization.signals # noqa diff --git a/apps/herd/services/services.py b/apps/herd/services/services.py index f0b6caf..f09b1e3 100644 --- a/apps/herd/services/services.py +++ b/apps/herd/services/services.py @@ -160,6 +160,7 @@ def rancher_quota_weight( InventoryQuotaSaleItem, quota_stat=quota_stat ) + print(rancher_remaining_usage) if total_weight - rancher_remaining_usage < 0: remaining_weight = 0 diff --git a/apps/livestock/apps.py b/apps/livestock/apps.py index 92dcc8c..5902ee7 100644 --- a/apps/livestock/apps.py +++ b/apps/livestock/apps.py @@ -6,4 +6,4 @@ class LivestockConfig(AppConfig): name = 'apps.livestock' def ready(self): - import apps.livestock.signals + import apps.livestock.signals # noqa diff --git a/apps/product/apps.py b/apps/product/apps.py index 427f54b..f53eddb 100644 --- a/apps/product/apps.py +++ b/apps/product/apps.py @@ -6,4 +6,4 @@ class ProductConfig(AppConfig): name = 'apps.product' def ready(self): - import apps.product.signals + import apps.product.signals # noqa diff --git a/apps/warehouse/apps.py b/apps/warehouse/apps.py index 3f27647..f917bee 100644 --- a/apps/warehouse/apps.py +++ b/apps/warehouse/apps.py @@ -6,4 +6,5 @@ class WarehouseConfig(AppConfig): name = 'apps.warehouse' def ready(self): - pass + import apps.warehouse.signals # noqa + import apps.warehouse.signals.signals_v2 # noqa diff --git a/apps/warehouse/pos/api/v2/serializers.py b/apps/warehouse/pos/api/v2/serializers.py index dd8e1ab..77dd6dc 100644 --- a/apps/warehouse/pos/api/v2/serializers.py +++ b/apps/warehouse/pos/api/v2/serializers.py @@ -201,11 +201,13 @@ class InventoryQuotaSaleTransactionSerializer(serializers.ModelSerializer): gov_product = item_data.pop('gov_product', None) free_product = item_data.pop('free_product', None) + distribution_id = item_data.pop('quota_distribution') distribution = QuotaDistribution.objects.filter( - id=item_data.pop('quota_distribution') + id=distribution_id ).first() if 'quota_distribution' in item_data.keys() else None - quota_stat = OrganizationQuotaStats.objects.get(id=item_data.pop('quota_stat')) + quota_stat_id = item_data.pop('quota_stat') + quota_stat = OrganizationQuotaStats.objects.get(id=quota_stat_id) # create item for transaction item = warehouse_models.InventoryQuotaSaleItem.objects.create( @@ -223,7 +225,7 @@ class InventoryQuotaSaleTransactionSerializer(serializers.ModelSerializer): total_price += item.total_price # IF WE DO NOT HAVE DISTRIBUTION, THEN IT IS A FREE PRODUCT TRANSACTION - if 'quota_distribution' and 'quota_stat' in item_data.keys(): + if distribution_id and quota_stat_id in item_data.keys(): # create extra sale for distribution create_extra_sale(transaction=transaction, sale_item=item) diff --git a/apps/warehouse/signals/signals_v2.py b/apps/warehouse/signals/signals_v2.py index c609afe..9bb25f0 100644 --- a/apps/warehouse/signals/signals_v2.py +++ b/apps/warehouse/signals/signals_v2.py @@ -13,41 +13,42 @@ def warehouse_sold_and_balance(quota_stat: OrganizationQuotaStats): quota_stat.sold_amount = total_sold quota_stat.inventory_entry_balance = quota_stat.inventory_received - total_sold - # if quota_stat.inventory_entry_balance >= 0: - # calculate extra sales & mines total extra sales weight from new inventory entry - # and set the warehouse balance + if quota_stat.inventory_entry_balance >= 0: + # calculate extra sales & mines total extra sales weight from new inventory entry + # and set the warehouse balance - # extra_sales = quota_distribution.extra_sales.all() - # total_extra_sales_weight = extra_sales.aggregate(total=Sum('weight'))['total'] or 0 - # if quota_distribution.free_sale_balance != 0: - # if quota_distribution.warehouse_balance >= quota_distribution.free_sale_balance: - # quota_distribution.warehouse_balance -= total_extra_sales_weight - # quota_distribution.free_sale_balance = 0 - # else: - # quota_distribution.free_sale_balance -= quota_distribution.warehouse_balance - # quota_distribution.warehouse_balance = 0 + extra_sales = quota_stat.extra_sales.all() + total_extra_sales_weight = extra_sales.aggregate(total=Sum('weight'))['total'] or 0 + if quota_stat.free_sale_balance != 0: + if quota_stat.inventory_entry_balance >= quota_stat.free_sale_balance: + quota_stat.inventory_entry_balance -= total_extra_sales_weight + quota_stat.free_sale_balance = 0 + else: + quota_stat.free_sale_balance -= quota_stat.inventory_entry_balance + quota_stat.inventory_entry_balance = 0 - # calculate pre_sales & mines total pre_sales weight from new inventory entry - # and set the warehouse balance + # calculate pre_sales & mines total pre_sales weight from new inventory entry + # and set the warehouse balance - # pre_sales = quota_distribution.pre_sales.all() - # total_pre_sales_weight = pre_sales.aggregate(total=Sum('weight'))['total'] or 0 - # if total_pre_sales_weight != 0: - # if quota_distribution.warehouse_balance >= quota_distribution.pre_sale_balance: - # quota_distribution.warehouse_balance -= total_pre_sales_weight - # quota_distribution.pre_sale_balance = 0 - # else: - # quota_distribution.pre_sale_balance -= quota_distribution.warehouse_balance - # quota_distribution.warehouse_balance = 0 + pre_sales = quota_stat.pre_sales.all() + total_pre_sales_weight = pre_sales.aggregate(total=Sum('weight'))['total'] or 0 + if total_pre_sales_weight != 0: + if quota_stat.inventory_entry_balance >= quota_stat.pre_sale_balance: + quota_stat.inventory_entry_balance -= total_pre_sales_weight + quota_stat.pre_sale_balance = 0 + else: + quota_stat.pre_sale_balance -= quota_stat.inventory_entry_balance + quota_stat.inventory_entry_balance = 0 - # 'free_sale_balance', 'pre_sale_balance' - quota_stat.save(update_fields=['sold_amount', 'inventory_entry_balance']) + quota_stat.save(update_fields=['sold_amount', 'inventory_entry_balance', 'free_sale_balance', 'pre_sale_balance']) @receiver(post_save, sender=InventoryQuotaSaleItem) @receiver(post_delete, sender=InventoryQuotaSaleItem) def update_distribution_warehouse_sold_and_balance(sender, instance: InventoryQuotaSaleItem, **kwargs): + print(instance.quota_stat) + print(instance.quota_stat.quota) # if object exists & pre sale is true if instance.quota_stat and not instance.quota_stat.quota.pre_sale: # if transaction status is success and warehouse management Done once, inventory_calculation set to true