pre sale & free sale deployment
This commit is contained in:
@@ -8,7 +8,10 @@ from apps.product.services.services import (
|
||||
from apps.pos_device.services.services import pos_organizations_sharing_information
|
||||
from apps.pos_device.pos.api.v1.serializers.device import DeviceSerializer
|
||||
from apps.product.exceptions import DistributionWeightException
|
||||
from apps.warehouse.services.services import create_extra_sale
|
||||
from apps.warehouse.services.services import (
|
||||
create_extra_sale,
|
||||
create_pre_sale
|
||||
)
|
||||
from apps.herd.pos.api.v1.serializers import RancherSerializer
|
||||
from apps.product.models import QuotaDistribution, Product
|
||||
from apps.warehouse import models as warehouse_models
|
||||
@@ -76,12 +79,12 @@ class InventoryEntrySerializer(serializers.ModelSerializer):
|
||||
),
|
||||
'base_prices': [
|
||||
{
|
||||
"text": "قیمت درب کارخانه", # noqa
|
||||
"text": "درب کارخانه", # noqa
|
||||
"name": "base_price_factory",
|
||||
"value": instance.distribution.quota.base_price_factory
|
||||
},
|
||||
{
|
||||
"text": "قیمت درب اتحادیه", # noqa
|
||||
"text": "درب اتحادیه", # noqa
|
||||
"name": "base_price_cooperative",
|
||||
"value": instance.distribution.quota.base_price_cooperative
|
||||
}
|
||||
@@ -146,9 +149,12 @@ class InventoryQuotaSaleTransactionSerializer(serializers.ModelSerializer):
|
||||
)
|
||||
total_price += item.total_price
|
||||
|
||||
# create extra sale
|
||||
# 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)
|
||||
|
||||
transaction.transaction_price = total_price
|
||||
transaction.save()
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from apps.herd.services.services import rancher_quota_weight, get_rancher_statistics
|
||||
from rest_framework.exceptions import APIException
|
||||
from apps.warehouse.models import (
|
||||
InventoryEntry,
|
||||
InventoryQuotaSaleTransaction,
|
||||
@@ -86,6 +87,7 @@ def create_extra_sale(
|
||||
|
||||
# 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()
|
||||
|
||||
# set transaction as free sale
|
||||
@@ -112,10 +114,26 @@ def create_pre_sale(
|
||||
"""
|
||||
|
||||
if sale_item.quota_distribution.pre_sale:
|
||||
# create pre sale
|
||||
pre_sale = QuotaPreSaleItem.objects.create(
|
||||
transaction=transaction,
|
||||
sale_item=sale_item,
|
||||
distribution=sale_item.quota_distribution,
|
||||
weight=sale_item.weight
|
||||
)
|
||||
try:
|
||||
# create pre sale
|
||||
pre_sale = QuotaPreSaleItem.objects.create(
|
||||
transaction=transaction,
|
||||
sale_item=sale_item,
|
||||
distribution=sale_item.quota_distribution,
|
||||
weight=sale_item.weight
|
||||
)
|
||||
|
||||
# set transaction as pre sale
|
||||
transaction.pre_sale_state = True
|
||||
transaction.save()
|
||||
|
||||
# set sale item as pre sale
|
||||
sale_item.is_pre_sale = True
|
||||
sale_item.quota_distribution.pre_sale_balance += sale_item.weight
|
||||
sale_item.save()
|
||||
|
||||
return pre_sale
|
||||
except APIException as e:
|
||||
pass
|
||||
pass
|
||||
|
||||
|
||||
@@ -22,11 +22,31 @@ def warehouse_sold_and_balance(quota_distribution: QuotaDistribution):
|
||||
quota_distribution.been_sold = total_sold
|
||||
quota_distribution.warehouse_balance = quota_distribution.warehouse_entry - total_sold
|
||||
if quota_distribution.warehouse_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 total_extra_sales_weight != 0:
|
||||
quota_distribution.warehouse_balance = quota_distribution.warehouse_entry - total_extra_sales_weight
|
||||
quota_distribution.save(update_fields=['been_sold', 'warehouse_balance'])
|
||||
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
|
||||
|
||||
# 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
|
||||
quota_distribution.save(update_fields=['been_sold', 'warehouse_balance', 'free_sale_balance', 'pre_sale_balance'])
|
||||
|
||||
|
||||
@receiver(post_save, sender=InventoryEntry)
|
||||
@@ -38,8 +58,8 @@ def update_distribution_warehouse_entry(sender, instance, **kwargs):
|
||||
|
||||
@receiver(post_save, sender=InventoryQuotaSaleItem)
|
||||
@receiver(post_delete, sender=InventoryQuotaSaleItem)
|
||||
def update_distribution_warehouse_sold_and_balance(sender, instance, **kwargs):
|
||||
if instance.quota_distribution:
|
||||
def update_distribution_warehouse_sold_and_balance(sender, instance: InventoryQuotaSaleItem, **kwargs):
|
||||
if instance.quota_distribution and not instance.quota_distribution.pre_sale:
|
||||
warehouse_sold_and_balance(
|
||||
quota_distribution=instance.quota_distribution,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user