diff --git a/inventory/models.py b/inventory/models.py index 64b604d..1b8b11b 100644 --- a/inventory/models.py +++ b/inventory/models.py @@ -13,6 +13,16 @@ class Warehouse(models.Model): address = models.TextField() have_freeze = models.BooleanField() + @property + def stock_percentage(self) -> float: + inventories = Inventory.objects.filter(warehouse=self) + total_stock = 0 + total_max_stock = 0 + for inventory in inventories: + total_stock += inventory.current_stock + total_max_stock += inventory.max_stock + return total_stock / total_max_stock * 100 + def __str__(self): return f"{self.name}" @@ -31,7 +41,7 @@ class Inventory(models.Model): current_stock = models.IntegerField() @property - def stock_percentage(self): + def stock_percentage(self) -> float: return self.current_stock / self.max_stock * 100 def __str__(self): diff --git a/inventory/urls.py b/inventory/urls.py index 647ec66..6317246 100644 --- a/inventory/urls.py +++ b/inventory/urls.py @@ -1,6 +1,6 @@ from django.urls import path, include -from inventory.views import test +from inventory.views import OverviewView urlpatterns = [ - path('overview/', test, name='overview'), + path('overview/', OverviewView.as_view(), name='overview'), ] diff --git a/inventory/utils.py b/inventory/utils.py new file mode 100644 index 0000000..9f13884 --- /dev/null +++ b/inventory/utils.py @@ -0,0 +1,21 @@ +from inventory.models import Inventory +from transaction.models import Supply + +def stock_percentage_all() -> float: + inventories = Inventory.objects.all() + total_stock = 0 + total_max_stock = 0 + for inventory in inventories: + total_stock += inventory.current_stock + total_max_stock += inventory.max_stock + if total_max_stock == 0: + return 0 + return total_stock / total_max_stock * 100 + + +def count_pending_supply() -> int: + pending_count = 0 + for supply in Supply.objects.all(): + if supply.pending_status(): + pending_count += 1 + return pending_count \ No newline at end of file diff --git a/inventory/views.py b/inventory/views.py index 413d02d..68135e4 100644 --- a/inventory/views.py +++ b/inventory/views.py @@ -1,6 +1,24 @@ from django.shortcuts import render +from django.views.generic import TemplateView +from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.decorators import login_required +from inventory.models import Warehouse +from inventory.utils import stock_percentage_all, count_pending_supply +from transaction.models import Customer + @login_required -def test(request): - return render(request, 'inventory/index.html') \ No newline at end of file +def Over(request): + return render(request, 'inventory/index.html') + +class OverviewView(TemplateView, LoginRequiredMixin): + template_name = "inventory/overview.html" + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context['warehouse_list'] = Warehouse.objects.all() + context['warehouse_count'] = Warehouse.objects.count() + context['customer_count'] = Customer.objects.count() + context['stock_percentage'] = stock_percentage_all() + context['pending_supply'] = count_pending_supply() + return context \ No newline at end of file diff --git a/templates/inventory/index.html b/templates/inventory/overview.html similarity index 82% rename from templates/inventory/index.html rename to templates/inventory/overview.html index f855ec9..b0d94db 100644 --- a/templates/inventory/index.html +++ b/templates/inventory/overview.html @@ -32,7 +32,7 @@
@@ -116,18 +116,6 @@