fix - product filtering on distribuition dashboard
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
|
|
||||||
from apps.product.models import Quota, OrganizationQuotaStats, QuotaDistribution
|
from apps.product.models import Quota, OrganizationQuotaStats
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
class Command(BaseCommand):
|
||||||
@@ -11,102 +11,70 @@ class Command(BaseCommand):
|
|||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
quotas = Quota.objects.prefetch_related('assigned_organizations').select_related(
|
quotas = Quota.objects.prefetch_related('assigned_organizations').select_related(
|
||||||
'registerer_organization', 'product'
|
'registerer_organization', 'product'
|
||||||
)
|
).all()
|
||||||
print("sssss")
|
|
||||||
created = 0
|
created = 0
|
||||||
existing = 0
|
existing = 0
|
||||||
updated = 0
|
updated = 0
|
||||||
merged = 0
|
merged = 0
|
||||||
|
|
||||||
for quota in quotas:
|
for quota in quotas:
|
||||||
|
create_org_stat_by_quota(quota)
|
||||||
|
create_org_stat_by_distribution(quota)
|
||||||
|
|
||||||
# 1) registerer org
|
|
||||||
target_orgs = {quota.registerer_organization}
|
|
||||||
|
|
||||||
# 2) assigned orgs
|
def create_org_stat_by_quota(quota: Quota):
|
||||||
# for org in quota.assigned_organizations.all():
|
was_created = 0
|
||||||
# target_orgs.add(org)
|
is_created = 0
|
||||||
|
|
||||||
for org in target_orgs:
|
org_quota_stat, created = OrganizationQuotaStats.objects.get_or_create(
|
||||||
if not org:
|
quota=quota,
|
||||||
continue
|
organization=quota.registerer_organization,
|
||||||
|
total_amount=quota.quota_weight,
|
||||||
|
total_distributed=quota.quota_distributed,
|
||||||
|
remaining_amount=quota.remaining_weight,
|
||||||
|
free_sale_balance=quota.free_sale_balance,
|
||||||
|
pre_sale_balance=quota.pre_sale_balance,
|
||||||
|
stat_type='quota'
|
||||||
|
)
|
||||||
|
|
||||||
qs = OrganizationQuotaStats.objects.filter(
|
if created:
|
||||||
quota=quota,
|
is_created += 1
|
||||||
organization=org,
|
else:
|
||||||
)
|
was_created += 1
|
||||||
|
|
||||||
if qs.exists():
|
print('is_created:', is_created, '\n', 'was_created:', was_created)
|
||||||
# If multiple exist → keep one, delete others
|
|
||||||
if qs.count() > 1:
|
|
||||||
first = qs.first()
|
|
||||||
qs.exclude(id=first.id).delete()
|
|
||||||
|
|
||||||
existing += 1
|
|
||||||
# continue
|
|
||||||
|
|
||||||
# Create new one
|
def create_org_stat_by_distribution(quota: Quota):
|
||||||
# quota_stat = OrganizationQuotaStats.objects.create(
|
main_stat = OrganizationQuotaStats.objects.get(quota=quota, stat_type="quota")
|
||||||
# quota=quota,
|
distributions = quota.distributions_assigned.filter()
|
||||||
# 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 ----
|
was_created = 0
|
||||||
distributions = QuotaDistribution.objects.select_related(
|
created = 0
|
||||||
"assigned_organization", "quota"
|
|
||||||
)
|
|
||||||
print(len(distributions))
|
|
||||||
|
|
||||||
for dist in distributions:
|
for dist in distributions:
|
||||||
org = dist.assigned_organization
|
org_quota_stat, created = OrganizationQuotaStats.objects.get_or_create(
|
||||||
quota = dist.quota
|
quota=quota,
|
||||||
|
organization=dist.assigned_organization,
|
||||||
|
total_amount=dist.weight,
|
||||||
|
total_distributed=dist.distributed,
|
||||||
|
remaining_amount=dist.remaining_weight,
|
||||||
|
free_sale_balance=dist.free_sale_balance,
|
||||||
|
pre_sale_balance=dist.pre_sale_balance,
|
||||||
|
inventory_received=dist.warehouse_entry,
|
||||||
|
inventory_entry_balance=dist.warehouse_balance,
|
||||||
|
sold_amount=dist.been_sold,
|
||||||
|
stat_type="distributions",
|
||||||
|
)
|
||||||
|
|
||||||
if not quota or not org:
|
if created:
|
||||||
continue
|
created += 1
|
||||||
|
else:
|
||||||
|
was_created += 1
|
||||||
|
|
||||||
dist_qs = OrganizationQuotaStats.objects.filter(
|
main_stat.distributions.add(dist)
|
||||||
quota=quota, organization=org
|
|
||||||
)
|
|
||||||
|
|
||||||
if dist_qs.exists():
|
print(org_quota_stat)
|
||||||
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)
|
print('created:', created, '\n', 'was_created:', was_created)
|
||||||
# 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_qs = OrganizationQuotaStats.objects.create(
|
|
||||||
quota=quota,
|
|
||||||
organization=org,
|
|
||||||
total_amount=dist.weight,
|
|
||||||
total_distributed=dist.distributed,
|
|
||||||
remaining_amount=dist.remaining_weight,
|
|
||||||
free_sale_balance=dist.free_sale_balance,
|
|
||||||
pre_sale_balance=dist.pre_sale_balance,
|
|
||||||
stat_type="distribution",
|
|
||||||
)
|
|
||||||
qs.first().distributions.add(dist)
|
|
||||||
created += 1
|
|
||||||
|
|
||||||
self.stdout.write(
|
|
||||||
self.style.SUCCESS(
|
|
||||||
f"OrganizationQuotaStats Rebuild Completed:"
|
|
||||||
f"\n Created: {created}"
|
|
||||||
f"\n Updated: {updated}"
|
|
||||||
f"\n Merged: {merged}"
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|||||||
@@ -14,28 +14,36 @@ class QuotaDashboardService:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_dashboard(self, org: Organization, start_date: str = None, end_date: str = None,
|
def get_dashboard(self, org: Organization, start_date: str = None, end_date: str = None,
|
||||||
search_fields: list[str] = None, quota_is_closed: bool = False, query_string: str = None):
|
search_fields: list[str] = None, quota_is_closed: bool = False, query_string: str = None,
|
||||||
|
product_id: int = None,
|
||||||
|
):
|
||||||
|
|
||||||
distribution_number = 0
|
distribution_number = 0
|
||||||
|
base_filter = {} # filter queries
|
||||||
|
|
||||||
|
if product_id:
|
||||||
|
base_filter['quota__product_id'] = product_id
|
||||||
|
if quota_is_closed:
|
||||||
|
base_filter['quota__is_closed'] = quota_is_closed
|
||||||
|
|
||||||
if org.type.key == 'ADM':
|
if org.type.key == 'ADM':
|
||||||
org_quota_stats = OrganizationQuotaStats.objects.filter(
|
org_quota_stats = OrganizationQuotaStats.objects.filter(
|
||||||
stat_type='quota',
|
stat_type='quota',
|
||||||
quota__is_closed=quota_is_closed
|
**base_filter
|
||||||
)
|
)
|
||||||
dist_org_quota_stats = OrganizationQuotaStats.objects.filter(
|
dist_org_quota_stats = OrganizationQuotaStats.objects.filter(
|
||||||
stat_type='distribution',
|
stat_type='distribution',
|
||||||
quota__is_closed=quota_is_closed
|
**base_filter
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
org_quota_stats = OrganizationQuotaStats.objects.filter(
|
org_quota_stats = OrganizationQuotaStats.objects.filter(
|
||||||
organization=org,
|
organization=org,
|
||||||
quota__is_closed=quota_is_closed,
|
**base_filter
|
||||||
)
|
)
|
||||||
dist_org_quota_stats = OrganizationQuotaStats.objects.filter(
|
dist_org_quota_stats = OrganizationQuotaStats.objects.filter(
|
||||||
stat_type='distribution',
|
stat_type='distribution',
|
||||||
organization=org,
|
organization=org,
|
||||||
quota__is_closed=quota_is_closed
|
**base_filter
|
||||||
)
|
)
|
||||||
|
|
||||||
# filter queryset (transactions & items) by date
|
# filter queryset (transactions & items) by date
|
||||||
@@ -49,18 +57,18 @@ class QuotaDashboardService:
|
|||||||
query_string=query_string
|
query_string=query_string
|
||||||
).apply()
|
).apply()
|
||||||
|
|
||||||
for stat in org_quota_stats.distinct('quota'):
|
quota_ids = org_quota_stats.values_list("quota_id", flat=True)
|
||||||
if org.type.key == 'ADM':
|
if org.type.key == 'ADM':
|
||||||
distribution_number += QuotaDistribution.objects.select_related('quota').filter(
|
distribution_number += QuotaDistribution.objects.select_related('quota').filter(
|
||||||
quota=stat.quota,
|
quota_id__in=quota_ids,
|
||||||
quota__is_closed=False
|
quota__is_closed=False
|
||||||
).count()
|
).count()
|
||||||
else:
|
else:
|
||||||
distribution_number += QuotaDistribution.objects.filter(
|
distribution_number += QuotaDistribution.objects.filter(
|
||||||
Q(assigner_organization=org),
|
Q(assigner_organization=org),
|
||||||
quota=stat.quota,
|
quota_id__in=quota_ids,
|
||||||
quota__is_closed=False
|
quota__is_closed=False
|
||||||
).count()
|
).count()
|
||||||
|
|
||||||
org_quota_stats = org_quota_stats.aggregate(
|
org_quota_stats = org_quota_stats.aggregate(
|
||||||
total_quotas=Count("quota", distinct=True),
|
total_quotas=Count("quota", distinct=True),
|
||||||
|
|||||||
Reference in New Issue
Block a user