Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added JSON export for pin transactions #43

Merged
merged 2 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion admin_board_view/static/AdminBoardView/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
}
Expand Down
7 changes: 7 additions & 0 deletions admin_board_view/templates/transactions.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ <h5>Export top-up transactions</h5>
<label for="to-date" class="form-label">To: </label>
<input type="date" id="to-date" class="form-control" value="{% now "Y-m-d" %}"/>
</div>
<div class="col">
<label for="export-type" class="form-label">Type of export: </label>
<select id="export-type" class="form-select">
<option value="pin">PIN</option>
<option value="mollie">Mollie</option>
</select>
</div>
</section>
<button id="export-top-ups" class="btn btn-primary mt-2">Export</button>
<hr/>
Expand Down
65 changes: 47 additions & 18 deletions admin_board_view/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 2 additions & 1 deletion mongoose_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

top_up_types = [
(1, "Pin"),
(2, "Credit card")
(2, "Credit card"),
(3, "Mollie")
]


Expand Down