fix - inventory update with registerer orgnaizaion of quota if not in parent orgs
This commit is contained in:
@@ -138,21 +138,33 @@ class QuotaStatsService:
|
|||||||
stat_queryset = OrganizationQuotaStats.objects.select_related('quota', 'organization')
|
stat_queryset = OrganizationQuotaStats.objects.select_related('quota', 'organization')
|
||||||
update_my_remaining_stat = True
|
update_my_remaining_stat = True
|
||||||
|
|
||||||
|
parent_orgs = []
|
||||||
|
|
||||||
while org:
|
while org:
|
||||||
stat = stat_queryset.filter(
|
parent_orgs.append(org)
|
||||||
organization=org,
|
|
||||||
quota=quota
|
|
||||||
).first()
|
|
||||||
if stat:
|
|
||||||
stat.inventory_received = (stat.inventory_received or 0) + diff
|
|
||||||
|
|
||||||
# just update my own stat remaining quota weight
|
|
||||||
if update_my_remaining_stat:
|
|
||||||
stat.remaining_amount -= diff
|
|
||||||
update_my_remaining_stat = False
|
|
||||||
|
|
||||||
if stat.inventory_received < 0:
|
|
||||||
stat.inventory_received = 0
|
|
||||||
stat.save(update_fields=['inventory_received', 'remaining_amount'])
|
|
||||||
|
|
||||||
org = org.parent_organization
|
org = org.parent_organization
|
||||||
|
|
||||||
|
parent_orgs_ids = [org.id for org in parent_orgs]
|
||||||
|
|
||||||
|
target_org_ids = set(parent_orgs_ids)
|
||||||
|
|
||||||
|
if quota.registerer_organization.id not in target_org_ids:
|
||||||
|
target_org_ids.add(quota.registerer_organization.id)
|
||||||
|
|
||||||
|
stats = (stat_queryset.filter(
|
||||||
|
quota=quota,
|
||||||
|
organization__in=target_org_ids
|
||||||
|
))
|
||||||
|
|
||||||
|
for stat in stats:
|
||||||
|
stat.inventory_received = (stat.inventory_received or 0) + diff
|
||||||
|
|
||||||
|
# just update my own stat remaining quota weight
|
||||||
|
if update_my_remaining_stat:
|
||||||
|
stat.remaining_amount -= diff
|
||||||
|
update_my_remaining_stat = False
|
||||||
|
|
||||||
|
if stat.inventory_received < 0:
|
||||||
|
stat.inventory_received = 0
|
||||||
|
|
||||||
|
OrganizationQuotaStats.objects.bulk_update(stats, ['inventory_received', 'remaining_amount'])
|
||||||
|
|||||||
Reference in New Issue
Block a user