diff --git a/mongoose_app/models.py b/mongoose_app/models.py index 35132df..2eda500 100644 --- a/mongoose_app/models.py +++ b/mongoose_app/models.py @@ -8,6 +8,7 @@ from django.core.validators import MaxValueValidator, MinValueValidator #from django.utils.translation import Trans + class Category(models.Model): name = models.CharField(max_length=30) alcoholic = models.BooleanField(default=False) @@ -92,6 +93,23 @@ def show_price(self): class Meta: verbose_name_plural = "Products" + +class NamedTransactionProductTotal(models.Model): + name = models.CharField(max_length=60) + product_id = models.IntegerField() + amount = models.IntegerField() + + class Meta: + managed = False + + def serialize(self) -> dict: + return { + 'name': self.name, + 'id': self.product_id, + 'amount': self.amount + } + + # Every transaction has at least a link with a user, an id and a sum # Whether this sum has a negative or positive influence on the total credit depends on the type of transaction class Transaction(models.Model): diff --git a/mongoose_app/urls.py b/mongoose_app/urls.py index 52b1b84..0d61f47 100644 --- a/mongoose_app/urls.py +++ b/mongoose_app/urls.py @@ -9,6 +9,7 @@ path('card', views.get_card, name='get_card'), path('products', views.get_products, name='get_products'), path('confirm', views.confirm_card, name='confirm_card'), + path('product_transactions', views.get_product_transactions, name="get_product_transactions"), # POST endpoints path('transaction', views.create_transaction, name='create_transaction'), diff --git a/mongoose_app/views.py b/mongoose_app/views.py index b2648eb..db60345 100644 --- a/mongoose_app/views.py +++ b/mongoose_app/views.py @@ -6,7 +6,7 @@ from django.conf import settings from .middleware import authenticated -from .models import CardConfirmation, Category, Card, Product, ProductTransactions, SaleTransaction, User +from .models import CardConfirmation, Category, Card, Product, ProductTransactions, SaleTransaction, User, NamedTransactionProductTotal from datetime import datetime, date from django.views.decorators.csrf import csrf_exempt import requests @@ -61,6 +61,54 @@ def get_products(request): serialized_categories = [c.serialize() for c in categories] return JsonResponse(serialized_categories, safe=False) +@authenticated +@require_http_methods(['GET']) +def get_product_transactions(request): + """ + Retrieve all historical sale transactions + """ + query_for_date_str = request.GET.get('date') + if query_for_date_str: + query_for_dt = datetime.fromtimestamp(int(query_for_date_str)) + else: + query_for_dt = datetime.now() + query_for_date = datetime(query_for_dt.year, query_for_dt.month, query_for_dt.day) + + transactions = ProductTransactions.objects.all() + + product_counts = {} + product_names = {} + + for transaction_product in transactions: + transaction = transaction_product.transaction_id + + transaction_dt = datetime(transaction.date.year, transaction.date.month, transaction.date.day) + + if transaction_dt != query_for_date: + continue + + product = transaction_product.product_id + + if product.id in product_counts: + product_counts[product.id] = product_counts[product.id] + 1 + else: + product_counts[product.id] = 1 + + if product.id not in product_names: + product_names[product.id] = product.name + + result = [] + + for product_id in product_counts: + result.append(NamedTransactionProductTotal( + product_id=product_id, + amount=product_counts[product_id], + name=product_names[product_id] + )) + + serialized_result = [c.serialize() for c in result] + return JsonResponse(serialized_result, safe=False) + # POST endpoints @csrf_exempt