From 94d177a04195f018d0c019291dac246ea93fb96a Mon Sep 17 00:00:00 2001 From: Nyasha Chizampeni Date: Wed, 28 Aug 2024 10:44:02 +0200 Subject: [PATCH] updated admin dashboard --- dashboard/helpers/dashboard_data.py | 42 +++++++++++++---- dashboard/urls.py | 2 +- stock/models.py | 2 +- templates/dashboard/admin.html | 71 +++++++++-------------------- 4 files changed, 55 insertions(+), 62 deletions(-) diff --git a/dashboard/helpers/dashboard_data.py b/dashboard/helpers/dashboard_data.py index 62def5f..8fe3007 100644 --- a/dashboard/helpers/dashboard_data.py +++ b/dashboard/helpers/dashboard_data.py @@ -176,23 +176,45 @@ def get_all_sites_monthly_sales(self): def get_all_sites_remaining_stock(self): - return {} - + data = [] + sites = self.user.company.sites.all() - def get_all_sites_total_stock_sales(self): - return {} + for site in sites: + remaining_stock = Stock.objects.filter(site=site).aggregate(Sum('quantity'))['quantity__sum'] or 0 + data.append({'site':site, 'remaining_stock':f"{remaining_stock}/{site.capacity}", 'remaining_stock_percentage':round(remaining_stock/site.capacity*100)}) + return data - def get_all_sites_requisitions(self): + def get_all_sites_sales_bar_graph_totals(self): return {} - - def get_alll_sites_sales_totals(self): + def get_all_site_sales_line_graph_monthly_totals(self): return {} - def get_company_data(self): + total_sales = 0 + sites = self.user.company.sites.all() + for site in sites: + total_sales += Transaction.objects.filter(site=site).aggregate( + total_sales=Sum(F('quantity') * F('unit_cost')) + )['total_sales'] * site.stock.first().price or 0 + return { 'company': self.user.company, - 'capacity': sum(site.capacity for site in self.user.company.sites.all()) + 'capacity': sum(site.capacity for site in self.user.company.sites.all()), + 'total_sales': total_sales, + 'monthly_average':total_sales / 12, + 'weekly_average':total_sales / 52, + 'daily_average':total_sales / 365, } - \ No newline at end of file + + + def get_company_site_stats(self): + sites = self.user.company.sites.all() + data = [] + total_quantity = 0 + for site in sites: + total_quantity += Transaction.objects.filter(site=site).aggregate( + total_quantity=Sum(F('quantity') * F('unit_cost')) + )['total_quantity'] + data.append({'site':site, 'total_requisitions':site.requisitions.all().count(),'total_sales':total_quantity* site.stock.first().price or 0, 'total_quantity':total_quantity }) + return data \ No newline at end of file diff --git a/dashboard/urls.py b/dashboard/urls.py index 4fae78d..bb8532d 100644 --- a/dashboard/urls.py +++ b/dashboard/urls.py @@ -4,6 +4,6 @@ urlpatterns = [ path('', DashboardView.as_view(), name='dashboard'), - path('/admin-dash', AdminDashboardView.as_view(), name='admin_dashboard'), + path('admin-dash', AdminDashboardView.as_view(), name='admin_dashboard'), path('reports/', generate_reports, name='report_create'), ] \ No newline at end of file diff --git a/stock/models.py b/stock/models.py index 691e5a6..de57503 100644 --- a/stock/models.py +++ b/stock/models.py @@ -25,7 +25,7 @@ def __str__(self): return f"{self.name}" class Reciept(models.Model): - site = models.ForeignKey('accounts.Site', on_delete=models.SET_NULL, null=True, blank=True) + site = models.ForeignKey('accounts.Site', on_delete=models.SET_NULL, null=True, blank=True, related_name='requisitions') quantity = models.PositiveIntegerField() stock = models.ForeignKey(Stock, related_name='reciepts', on_delete=models.SET_NULL, null=True, blank=True) description = models.TextField(blank=True, null=True) diff --git a/templates/dashboard/admin.html b/templates/dashboard/admin.html index 49aaf49..36c6623 100644 --- a/templates/dashboard/admin.html +++ b/templates/dashboard/admin.html @@ -48,54 +48,25 @@
Site Remaining Stock
@@ -111,33 +82,33 @@
Total Sales Per Site
+ {% for data in dashboard_data.get_company_site_stats %}
Site
+
Requisitions
Quantity
-
Users
Amount
- Google Chrome + {{ data.site }}
- 1,641 + {{ data.total_requisitions }}
-
-
-
-
72.84%
-
+ +
+ {{ data.total_quantity }}kg
+
- 22.62% + ${{ data.total_sales }}
- + {% endfor %}
@@ -170,7 +141,7 @@
Organisation Statistics
  • Total Sales
    -
    {{ dashboard_data.get_stock_data.total_requisitions }}
    +
    ${{ dashboard_data.get_company_data.total_sales }}
  • @@ -209,17 +180,17 @@
    Total Sales Per Month
    Monthly
    -
    ${{ dashboard_data.get_sales_plot_data.monthly_average|format_amount }}
    +
    ${{ dashboard_data.get_company_data.monthly_average|format_amount }}
    Weekly
    -
    ${{ dashboard_data.get_sales_plot_data.weekly_average|format_amount }}
    +
    ${{ dashboard_data.get_company_data.weekly_average|format_amount }}
    Daily (Avg)
    -
    ${{ dashboard_data.get_sales_plot_data.daily_average|format_amount }}
    +
    ${{ dashboard_data.get_company_data.daily_average|format_amount }}