diff --git a/apps/warehouse/services/warehouse_allocation_service.py b/apps/warehouse/services/warehouse_allocation_service.py index ce06ed0..e3b89af 100644 --- a/apps/warehouse/services/warehouse_allocation_service.py +++ b/apps/warehouse/services/warehouse_allocation_service.py @@ -55,13 +55,25 @@ class WarehouseAllocationService: ) org = entry.organization.parent_organization + parent_orgs = [] while org: - stat = OrganizationQuotaStats.objects.filter( - quota=entry.quota, - organization=org - ) - if stat.exists(): - stat = stat.first() - stat.inventory_received += entry.weight - stat.save() + # import parent org to list + parent_orgs.append(org) org = org.parent_organization + + parent_org_ids = [org.id for org in parent_orgs] + + target_org_ids = set(parent_org_ids) + + if entry.quota.registerer_organization.id not in target_org_ids: + target_org_ids.add(entry.quota.registerer_organization.id) + + stats = (OrganizationQuotaStats.objects.filter( + quota=entry.quota, + organization__in=target_org_ids + )) + + for stat in stats: + stat.inventory_received += entry.weight + + OrganizationQuotaStats.objects.bulk_update(stats, ["inventory_received"])