add - filter on transaction dashboard
This commit is contained in:
@@ -5,13 +5,14 @@ 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.warehouse.models import InventoryQuotaSaleTransaction, InventoryQuotaSaleItem
|
||||
|
||||
|
||||
class TransactionDashboardService:
|
||||
|
||||
@staticmethod
|
||||
def get_dashboard(org: Organization):
|
||||
def get_dashboard(org: Organization, start_date: str = None, end_date: str = None, status: str = None):
|
||||
|
||||
orgs_child = get_all_org_child(org=org)
|
||||
orgs_child.append(org)
|
||||
@@ -30,6 +31,19 @@ class TransactionDashboardService:
|
||||
transaction__seller_organization__in=orgs_child
|
||||
).select_related("gov_product", "free_product")
|
||||
|
||||
# filter queryset by date
|
||||
if start_date and end_date:
|
||||
transactions = DynamicSearchService(
|
||||
queryset=transactions,
|
||||
start=start_date,
|
||||
end=end_date,
|
||||
date_field="create_date",
|
||||
).apply()
|
||||
|
||||
# filer by transaction status
|
||||
if status:
|
||||
transactions = transactions.filter(transaction_status=status)
|
||||
|
||||
transaction_stats = transactions.aggregate(
|
||||
total_transactions=Count("id"),
|
||||
success_transactions=Count("id", filter=Q(transaction_status="success")),
|
||||
@@ -89,7 +103,7 @@ class TransactionDashboardService:
|
||||
for item in items_by_product.get(pid, []):
|
||||
if item.item_share:
|
||||
for share in item.item_share:
|
||||
# share: {"name": ..., "price": ..., "shaba": ...}
|
||||
# share: {"name": "", "price": "", ....}
|
||||
name = share.get("name")
|
||||
price = share.get("price", 0)
|
||||
|
||||
|
||||
@@ -230,9 +230,20 @@ class InventoryQuotaSaleTransactionViewSet(
|
||||
"""
|
||||
dashboard of full detail of all my transactions
|
||||
"""
|
||||
query_param = self.request.query_params # noqa
|
||||
|
||||
start_date = query_param.get('start') if 'start' in query_param.keys() else None
|
||||
end_date = query_param.get('end') if 'end' in query_param.keys() else None
|
||||
transaction_status = query_param.get('status') if 'status' in query_param.keys() else None
|
||||
|
||||
org = get_organization_by_user(request.user)
|
||||
transaction_dashboard_data = self.get_dashboard(org)
|
||||
# filer by date & transaction status
|
||||
transaction_dashboard_data = self.get_dashboard(
|
||||
org,
|
||||
start_date=start_date,
|
||||
end_date=end_date,
|
||||
status=transaction_status
|
||||
)
|
||||
|
||||
return Response(transaction_dashboard_data, status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user