import - distributioin stat type for distribution tab
This commit is contained in:
@@ -12,7 +12,7 @@ class Command(BaseCommand):
|
||||
quotas = Quota.objects.prefetch_related('assigned_organizations').select_related(
|
||||
'registerer_organization', 'product'
|
||||
)
|
||||
|
||||
print("sssss")
|
||||
created = 0
|
||||
existing = 0
|
||||
updated = 0
|
||||
@@ -43,23 +43,24 @@ class Command(BaseCommand):
|
||||
qs.exclude(id=first.id).delete()
|
||||
|
||||
existing += 1
|
||||
continue
|
||||
# continue
|
||||
|
||||
# Create new one
|
||||
quota_stat = OrganizationQuotaStats.objects.create(
|
||||
quota=quota,
|
||||
organization=org,
|
||||
total_amount=quota.quota_weight,
|
||||
remaining_amount=quota.remaining_weight,
|
||||
total_distributed=quota.quota_distributed,
|
||||
stat_type="quota"
|
||||
)
|
||||
created += 1
|
||||
# quota_stat = OrganizationQuotaStats.objects.create(
|
||||
# quota=quota,
|
||||
# organization=org,
|
||||
# total_amount=quota.quota_weight,
|
||||
# remaining_amount=quota.remaining_weight,
|
||||
# total_distributed=quota.quota_distributed,
|
||||
# stat_type="quota"
|
||||
# )
|
||||
# created += 1
|
||||
|
||||
# ---- 2) Create OrganizationQuotaStats for each QuotaDistribution ----
|
||||
distributions = QuotaDistribution.objects.select_related(
|
||||
"assigned_organization", "quota"
|
||||
)
|
||||
print(len(distributions))
|
||||
|
||||
for dist in distributions:
|
||||
org = dist.assigned_organization
|
||||
@@ -68,27 +69,27 @@ class Command(BaseCommand):
|
||||
if not quota or not org:
|
||||
continue
|
||||
|
||||
qs = OrganizationQuotaStats.objects.filter(
|
||||
dist_qs = OrganizationQuotaStats.objects.filter(
|
||||
quota=quota, organization=org
|
||||
)
|
||||
|
||||
if qs.exists():
|
||||
if qs.count() > 1:
|
||||
first = qs.first()
|
||||
qs.exclude(id=first.id).delete()
|
||||
if dist_qs.exists():
|
||||
if dist_qs.count() > 1:
|
||||
first = dist_qs.first()
|
||||
dist_qs.exclude(id=first.id).delete()
|
||||
merged += 1
|
||||
|
||||
# Update existing record with distribution weight (optional)
|
||||
record = qs.first()
|
||||
record.total_distributed += dist.weight
|
||||
record.total_amount += dist.weight
|
||||
record.remaining_amount = max(record.total_amount - record.sold_amount, 0)
|
||||
record.save()
|
||||
updated += 1
|
||||
# record = qs.first()
|
||||
# record.total_distributed += dist.weight
|
||||
# record.total_amount += dist.weight
|
||||
# record.remaining_amount = max(record.total_amount - record.sold_amount, 0)
|
||||
# record.save()
|
||||
# updated += 1
|
||||
|
||||
else:
|
||||
# Create new stats for this distribution
|
||||
dist_stat = OrganizationQuotaStats.objects.create(
|
||||
dist_qs = OrganizationQuotaStats.objects.create(
|
||||
quota=quota,
|
||||
organization=org,
|
||||
total_amount=dist.weight,
|
||||
@@ -98,7 +99,7 @@ class Command(BaseCommand):
|
||||
pre_sale_balance=dist.pre_sale_balance,
|
||||
stat_type="distribution",
|
||||
)
|
||||
quota_stat.distributions.add(dist)
|
||||
qs.first().distributions.add(dist)
|
||||
created += 1
|
||||
|
||||
self.stdout.write(
|
||||
|
||||
Reference in New Issue
Block a user