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
|
base_query['product_id'] = product_id
|
||||||
|
|
||||||
queryset = self.filter_query(
|
queryset = self.filter_query(
|
||||||
self.get_queryset(visibility_by_org_scope=True).filter(
|
self.get_queryset(
|
||||||
is_closed=False)) # return by search param or all objects
|
visibility_by_org_scope=True).filter(
|
||||||
|
is_closed=False
|
||||||
|
)) # return by search param or all objects
|
||||||
|
|
||||||
# paginate queryset
|
# paginate queryset
|
||||||
page = self.paginate_queryset(
|
page = self.paginate_queryset(
|
||||||
queryset.filter(
|
queryset.filter(
|
||||||
Q(assigned_organizations=org) |
|
|
||||||
Q(registerer_organization=org),
|
|
||||||
org_quota_stats__inventory_received__gt=0,
|
org_quota_stats__inventory_received__gt=0,
|
||||||
**base_query
|
**base_query
|
||||||
).order_by('-modify_date').distinct()
|
).order_by('-modify_date').distinct()
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ class InventoryEntry(BaseModel):
|
|||||||
return self.weight - self.total_sold
|
return self.weight - self.total_sold
|
||||||
|
|
||||||
def __str__(self):
|
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):
|
def save(self, *args, **kwargs):
|
||||||
if not self.entry_identity:
|
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 django.db.models.functions import Coalesce
|
||||||
|
|
||||||
from apps.authentication.models import Organization
|
from apps.authentication.models import Organization
|
||||||
from apps.authentication.services.service import get_all_org_child
|
from apps.authentication.services.service import get_all_org_child
|
||||||
from apps.core.services.filter.search import DynamicSearchService
|
from apps.core.services.filter.search import DynamicSearchService
|
||||||
|
from apps.product.models import Quota
|
||||||
from apps.warehouse.models import InventoryEntry
|
from apps.warehouse.models import InventoryEntry
|
||||||
|
|
||||||
|
|
||||||
@@ -27,12 +28,28 @@ class InventoryEntryDashboardService:
|
|||||||
|
|
||||||
if org.type.key == 'ADM':
|
if org.type.key == 'ADM':
|
||||||
inventory_entries = InventoryEntry.objects.filter(quota__is_closed=False)
|
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:
|
else:
|
||||||
|
|
||||||
inventory_entries = InventoryEntry.objects.filter(
|
inventory_entries = InventoryEntry.objects.filter(
|
||||||
quota__is_closed=False,
|
quota__is_closed=False,
|
||||||
organization__in=child_orgs
|
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:
|
if (start_date and end_date) or query_string:
|
||||||
inventory_entries = DynamicSearchService(
|
inventory_entries = DynamicSearchService(
|
||||||
queryset=inventory_entries,
|
queryset=inventory_entries,
|
||||||
@@ -42,10 +59,15 @@ class InventoryEntryDashboardService:
|
|||||||
date_field="create_date",
|
date_field="create_date",
|
||||||
query_string=query_string
|
query_string=query_string
|
||||||
).apply()
|
).apply()
|
||||||
|
print(inventory_entries)
|
||||||
|
|
||||||
dashboard = inventory_entries.aggregate(
|
dashboard = inventory_entries.aggregate(
|
||||||
total_entries=Count("id"),
|
total_entries=Count("id"),
|
||||||
total_weight=Coalesce(Sum("weight"), 0),
|
total_weight=Coalesce(Sum("weight"), 0),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
dashboard.update(
|
||||||
|
total_quotas=quotas.count(),
|
||||||
|
)
|
||||||
|
|
||||||
return dashboard
|
return dashboard
|
||||||
|
|||||||
Reference in New Issue
Block a user