import - qoutas count in inventory entry dashbiard
This commit is contained in:
@@ -532,14 +532,14 @@ class QuotaViewSet(BaseViewSet, SoftDeleteMixin, QuotaDashboardService, viewsets
|
||||
base_query['product_id'] = product_id
|
||||
|
||||
queryset = self.filter_query(
|
||||
self.get_queryset(visibility_by_org_scope=True).filter(
|
||||
is_closed=False)) # return by search param or all objects
|
||||
self.get_queryset(
|
||||
visibility_by_org_scope=True).filter(
|
||||
is_closed=False
|
||||
)) # return by search param or all objects
|
||||
|
||||
# paginate queryset
|
||||
page = self.paginate_queryset(
|
||||
queryset.filter(
|
||||
Q(assigned_organizations=org) |
|
||||
Q(registerer_organization=org),
|
||||
org_quota_stats__inventory_received__gt=0,
|
||||
**base_query
|
||||
).order_by('-modify_date').distinct()
|
||||
|
||||
@@ -63,7 +63,7 @@ class InventoryEntry(BaseModel):
|
||||
return self.weight - self.total_sold
|
||||
|
||||
def __str__(self):
|
||||
return f"distribution: {self.distribution.distribution_id}-{self.organization.name}"
|
||||
return f"entry: {self.id}-{self.quota.quota_id}-{self.organization.name}"
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.entry_identity:
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
from django.db.models import Count, Sum
|
||||
from django.db.models import Count, Sum, Q
|
||||
from django.db.models.functions import Coalesce
|
||||
|
||||
from apps.authentication.models import Organization
|
||||
from apps.authentication.services.service import get_all_org_child
|
||||
from apps.core.services.filter.search import DynamicSearchService
|
||||
from apps.product.models import Quota
|
||||
from apps.warehouse.models import InventoryEntry
|
||||
|
||||
|
||||
@@ -27,12 +28,28 @@ class InventoryEntryDashboardService:
|
||||
|
||||
if org.type.key == 'ADM':
|
||||
inventory_entries = InventoryEntry.objects.filter(quota__is_closed=False)
|
||||
|
||||
# calculate quotas that have received inventory entry
|
||||
quotas = Quota.objects.filter(
|
||||
org_quota_stats__inventory_received__gt=0,
|
||||
is_closed=False,
|
||||
).distinct('id')
|
||||
|
||||
else:
|
||||
|
||||
inventory_entries = InventoryEntry.objects.filter(
|
||||
quota__is_closed=False,
|
||||
organization__in=child_orgs
|
||||
)
|
||||
|
||||
# calculate quotas that have received inventory entry
|
||||
quotas = Quota.objects.filter(
|
||||
Q(registerer_organization=org) |
|
||||
Q(assigned_organizations__in=child_orgs),
|
||||
org_quota_stats__inventory_received__gt=0,
|
||||
is_closed=False,
|
||||
).distinct('id')
|
||||
|
||||
if (start_date and end_date) or query_string:
|
||||
inventory_entries = DynamicSearchService(
|
||||
queryset=inventory_entries,
|
||||
@@ -42,10 +59,15 @@ class InventoryEntryDashboardService:
|
||||
date_field="create_date",
|
||||
query_string=query_string
|
||||
).apply()
|
||||
print(inventory_entries)
|
||||
|
||||
dashboard = inventory_entries.aggregate(
|
||||
total_entries=Count("id"),
|
||||
total_weight=Coalesce(Sum("weight"), 0),
|
||||
)
|
||||
|
||||
dashboard.update(
|
||||
total_quotas=quotas.count(),
|
||||
)
|
||||
|
||||
return dashboard
|
||||
|
||||
Reference in New Issue
Block a user