fix - change base of pre sale / extra sale on quota stat & total purchase of rancher in Quota usage calculation

This commit is contained in:
2025-11-26 15:50:35 +03:30
parent 817f26519d
commit 65bdb539bc
7 changed files with 51 additions and 26 deletions

View File

@@ -1,5 +1,11 @@
from apps.herd.services.services import rancher_quota_weight, get_rancher_statistics
import typing
from django.db.models import Sum
from rest_framework.exceptions import APIException
from apps.core.models import SystemConfig
from apps.herd.services.services import rancher_quota_weight
from apps.product.models import QuotaDistribution
from apps.warehouse.models import (
InventoryEntry,
InventoryQuotaSaleTransaction,
@@ -7,10 +13,6 @@ from apps.warehouse.models import (
ExtraSale,
QuotaPreSaleItem
)
from apps.product.models import QuotaDistribution
from apps.core.models import SystemConfig
from django.db.models import Sum
import typing
def get_total_sold(rancher, inventory_entry: InventoryEntry = None, distribution: QuotaDistribution = None):
@@ -74,21 +76,22 @@ def create_extra_sale(
more than distribution warehouse balance
"""
if sale_item.quota_distribution.free_sale:
if sale_item.weight > sale_item.quota_distribution.warehouse_balance:
if sale_item.quota_stat.quota.free_sale:
if sale_item.weight > sale_item.quota_stat.inventory_entry_balance:
# calculate extra weight between item weight and warehouse balance
extra_weight = sale_item.weight - sale_item.quota_distribution.warehouse_balance
extra_weight = sale_item.weight - sale_item.quota_stat.inventory_entry_balance
extra_sale = ExtraSale.objects.create( # create extra sale
transaction=transaction,
sale_item=sale_item,
distribution=sale_item.quota_distribution,
quota_stat=sale_item.quota_stat,
weight=extra_weight
)
# set distribution warehouse balance to 0 because we have extra sale
sale_item.quota_distribution.warehouse_balance = 0
sale_item.quota_distribution.free_sale_balance += extra_weight
sale_item.quota_distribution.save()
sale_item.quota_stat.inventory_entry_balance = 0
sale_item.quota_stat.free_sale_balance += extra_weight
sale_item.quota_stat.save()
# set transaction as free sale
transaction.free_sale_state = True
@@ -113,13 +116,14 @@ def create_pre_sale(
Create pre_sale for distributions
"""
if sale_item.quota_distribution.pre_sale:
if sale_item.quota_stat.quota.pre_sale:
try:
# create pre sale
pre_sale = QuotaPreSaleItem.objects.create(
transaction=transaction,
sale_item=sale_item,
distribution=sale_item.quota_distribution,
quota_stat=sale_item.quota_stat,
weight=sale_item.weight
)
@@ -129,11 +133,10 @@ def create_pre_sale(
# set sale item as pre sale
sale_item.is_pre_sale = True
sale_item.quota_distribution.pre_sale_balance += sale_item.weight
sale_item.quota_stat.pre_sale_balance += sale_item.weight
sale_item.save()
return pre_sale
except APIException as e:
pass
pass