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

@@ -247,6 +247,12 @@ class ExtraSale(BaseModel):
related_name='extra_sales',
null=True
)
quota_stat = models.ForeignKey(
product_models.OrganizationQuotaStats,
on_delete=models.CASCADE,
related_name='extra_sales',
null=True
)
transaction = models.ForeignKey(
InventoryQuotaSaleTransaction,
on_delete=models.CASCADE,
@@ -281,6 +287,12 @@ class QuotaPreSaleItem(BaseModel):
related_name='pre_sales',
null=True
)
quota_stat = models.ForeignKey(
product_models.OrganizationQuotaStats,
on_delete=models.CASCADE,
related_name='pre_sales',
null=True
)
transaction = models.ForeignKey(
InventoryQuotaSaleTransaction,
on_delete=models.CASCADE,

View File

@@ -22,6 +22,7 @@ from apps.product.services.services import (
from apps.warehouse import models as warehouse_models
from apps.warehouse.exceptions import WareHouseException
from apps.warehouse.services.quota_usage_services import QuotaUsageService
from apps.warehouse.services.services import create_extra_sale, create_pre_sale
class InventoryEntrySerializer(serializers.ModelSerializer):
@@ -223,11 +224,11 @@ class InventoryQuotaSaleTransactionSerializer(serializers.ModelSerializer):
# IF WE DO NOT HAVE DISTRIBUTION, THEN IT IS A FREE PRODUCT TRANSACTION
if 'quota_distribution' and 'quota_stat' in item_data.keys():
# # create extra sale for distribution
# create_extra_sale(transaction=transaction, sale_item=item)
#
# # create pre sale for distribution
# create_pre_sale(transaction=transaction, sale_item=item)
# create extra sale for distribution
create_extra_sale(transaction=transaction, sale_item=item)
# create pre sale for distribution
create_pre_sale(transaction=transaction, sale_item=item)
# calculate quota usage of rancher
QuotaUsageService.allocate_usage(

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