fix - rancher quota weight reamining weight / free sale
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
|
from django.db.models import Count
|
||||||
|
|
||||||
|
from apps.herd.models import Rancher
|
||||||
|
|
||||||
logger = logging.getLogger("merge_duplicate_ranchers")
|
logger = logging.getLogger("merge_duplicate_ranchers")
|
||||||
handler = logging.StreamHandler()
|
handler = logging.StreamHandler()
|
||||||
@@ -11,6 +14,24 @@ logger.setLevel(logging.INFO)
|
|||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
class Command(BaseCommand):
|
||||||
|
help = "Merge ranchers with duplicate national_code and reassign their herds to one main rancher"
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
pass
|
duplicates = (
|
||||||
|
Rancher.objects.values("national_code")
|
||||||
|
.annotate(count=Count("id"))
|
||||||
|
.filter(count__gt=1)
|
||||||
|
)
|
||||||
|
|
||||||
|
for duplicate in duplicates:
|
||||||
|
ranchers = Rancher.objects.filter(national_code=duplicate["national_code"])
|
||||||
|
main_rancher = ranchers.first()
|
||||||
|
for rancher in ranchers:
|
||||||
|
if rancher != main_rancher:
|
||||||
|
logger.info(f"Merging rancher {rancher.id} into {main_rancher.id}")
|
||||||
|
rancher.herd.update(rancher=main_rancher)
|
||||||
|
rancher.soft_delete()
|
||||||
|
logger.info(f"Rancher {rancher.id} merged into {main_rancher.id}")
|
||||||
|
logger.info(f"Rancher {rancher.id} deleted")
|
||||||
|
|
||||||
|
self.stdout.write(self.style.SUCCESS("🎯 Merge process completed successfully."))
|
||||||
|
|||||||
@@ -150,13 +150,21 @@ def rancher_quota_weight(
|
|||||||
# incentive plan quantity by this livestock type
|
# incentive plan quantity by this livestock type
|
||||||
rancher_plan_weight = rancher_plans.first().allowed_quantity * item.quantity_kg
|
rancher_plan_weight = rancher_plans.first().allowed_quantity * item.quantity_kg
|
||||||
total_weight += rancher_plan_weight
|
total_weight += rancher_plan_weight
|
||||||
|
print(total_weight)
|
||||||
# get rancher remaining usage of quota for purchase
|
# get rancher remaining usage of quota for purchase
|
||||||
rancher_remaining_usage = RancherService.get_total_used_weight(rancher, InventoryQuotaSaleItem)
|
rancher_remaining_usage = RancherService.get_total_used_weight(rancher, InventoryQuotaSaleItem)
|
||||||
|
|
||||||
|
if total_weight - rancher_remaining_usage < 0:
|
||||||
|
remaining_weight = 0
|
||||||
|
free_sale = rancher_remaining_usage - total_weight
|
||||||
|
else:
|
||||||
|
remaining_weight = total_weight - rancher_remaining_usage
|
||||||
|
free_sale = 0
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"total_weight": total_weight,
|
"total_weight": total_weight,
|
||||||
"remaining_weight": total_weight - rancher_remaining_usage,
|
"remaining_weight": remaining_weight,
|
||||||
|
'free_sale': free_sale,
|
||||||
"rancher_temporary_livestock": rancher.without_herd,
|
"rancher_temporary_livestock": rancher.without_herd,
|
||||||
"by_type": [{
|
"by_type": [{
|
||||||
"name": key,
|
"name": key,
|
||||||
|
|||||||
Reference in New Issue
Block a user