free sale of inventory entry system deployment - add dhi state to rancher

This commit is contained in:
2025-09-20 12:41:24 +03:30
parent ce6e468b13
commit e8f4c77e9f
12 changed files with 390 additions and 158 deletions

View File

@@ -1,8 +1,14 @@
from apps.herd.services.services import rancher_quota_weight, get_rancher_statistics
from apps.warehouse.models import InventoryEntry, InventoryQuotaSaleTransaction
from apps.warehouse.models import (
InventoryEntry,
InventoryQuotaSaleTransaction,
InventoryQuotaSaleItem,
ExtraSale
)
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):
@@ -53,3 +59,35 @@ def can_buy_from_inventory(rancher, inventory_entry: InventoryEntry = None, dist
total_sold = get_total_sold(inventory_entry, rancher)
return total_sold < total_allowed
def create_extra_sale(
sale_item: InventoryQuotaSaleItem,
transaction: InventoryQuotaSaleTransaction
) -> typing.Any:
"""
:param sale_item
:param transaction
create extra sale for items that weight are
more than distribution warehouse balance
"""
if sale_item.quota_distribution.free_sale:
if sale_item.weight > sale_item.quota_distribution.warehouse_balance:
# calculate extra weight between item weight and warehouse balance
extra_weight = sale_item.weight - sale_item.quota_distribution.warehouse_balance
extra_sale = ExtraSale.objects.create( # create extra sale
transaction=transaction,
sale_item=sale_item,
distribution=sale_item.quota_distribution,
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.save()
return extra_sale
pass
pass