diff --git a/admin_board_view/static/AdminBoardView/settings.js b/admin_board_view/static/AdminBoardView/settings.js index ccc183c..c4e429d 100644 --- a/admin_board_view/static/AdminBoardView/settings.js +++ b/admin_board_view/static/AdminBoardView/settings.js @@ -166,7 +166,9 @@ if (exportTransactions) { exportTransactions.addEventListener("click", e => { const from = document.getElementById("from-date").value; const to = document.getElementById("to-date").value; - const url = `/transactions/export?start_date=${from}&end_date=${to}`; + const export_dropdown = document.getElementById("export-type") + const export_type = export_dropdown.options[export_dropdown.selectedIndex].value; + const url = `/transactions/export?type=${export_type}&start_date=${from}&end_date=${to}`; window.open(url, "_blank"); }); } diff --git a/admin_board_view/templates/transactions.html b/admin_board_view/templates/transactions.html index c7d885c..0f58685 100644 --- a/admin_board_view/templates/transactions.html +++ b/admin_board_view/templates/transactions.html @@ -16,6 +16,13 @@
Export top-up transactions
+
+ + +

diff --git a/admin_board_view/views.py b/admin_board_view/views.py index c0a3225..c329e96 100644 --- a/admin_board_view/views.py +++ b/admin_board_view/views.py @@ -203,27 +203,56 @@ def export_sale_transactions(request): Returns: HttpResponse: The csv file containing the sale transactions in the given date range. """ - try: - if not request.GET.get('start_date') or not request.GET.get('end_date'): - return HttpResponse("No date range given.", status=400) - - # Get the date range from the request - start_date = request.GET.get('start_date') - end_date = request.GET.get('end_date') - current_date = timezone.now().strftime("%Y-%m-%d") - - # Get the transactions in the date range - top_up_range = TopUpTransaction.objects.filter(date__range=[start_date, end_date]).all() + # Only allow export for authanticated users + if not request.user.is_superuser: + return HttpResponse("You are not authenticated.", status=401) - # Setup the export "csv" - response_string = f'Factuurdatum,{current_date},ideal - {start_date} / {end_date},02,473\n' + try: + req_get = request.GET + export_type = req_get.get('type') + start_date = req_get.get('start_date') + end_date = req_get.get('end_date') + if export_type == 'mollie': + if not start_date or not end_date: + return HttpResponse("No date range given.", status=400) + + # Get the date range from the request + current_date = timezone.now().strftime("%Y-%m-%d") + + # Get the transactions in the date range + top_up_range = TopUpTransaction.objects.filter(date__range=[start_date, end_date], type=3).all() + + # Setup the export "csv" + response_string = f'Factuurdatum,{current_date},ideal - {start_date} / {end_date},02,473\n' + + # Add the transactions to the export "csv" + for t in top_up_range: + response_string += f'"",8002,Mongoose - {t.id},9,{t.transaction_sum},""\n' + + # Return the export "csv" + return HttpResponse(response_string, content_type='text/csv') + elif export_type == 'pin': + if not start_date or not end_date: + return HttpResponse("No date range given.", status=400) + + # Get the date range from the request + current_date = timezone.now().strftime("%Y-%m-%d") + + # Get the transactions in the date range + if start_date and end_date: + top_up_range = TopUpTransaction.objects.filter(date__range=[start_date, end_date], type=1).all() + else: + top_up_range = TopUpTransaction.objects.filter(date=current_date, type=1).all() - # Add the transactions to the export "csv" - for t in top_up_range: - response_string += f'"",8002,Mongoose - {t.id},9,{t.transaction_sum},""\n' + # Turn transaction result into JSON + json_resp = json.dumps([{"member_id": t.user_id.id, "name": t.user_id.name, + "price": float(t.transaction_sum), "date": t.date.strftime("%Y-%m-%d")} + for t in top_up_range]) - # Return the export "csv" - return HttpResponse(response_string, content_type='text/csv') + # Return the created json + return HttpResponse(json_resp, content_type='application/json') + else: + return HttpResponse("Export type not found", status=400) except Exception as e: print(e) return HttpResponse("Something went wrong whilst trying to export the sale transactions.", status=400) diff --git a/mongoose_app/models.py b/mongoose_app/models.py index dc2ca5b..25664c5 100644 --- a/mongoose_app/models.py +++ b/mongoose_app/models.py @@ -10,7 +10,8 @@ top_up_types = [ (1, "Pin"), - (2, "Credit card") + (2, "Credit card"), + (3, "Mollie") ]