diff --git a/backend/api/invoices/manage.py b/backend/api/invoices/manage.py index c1e6e9a24..87c647f5a 100644 --- a/backend/api/invoices/manage.py +++ b/backend/api/invoices/manage.py @@ -59,7 +59,7 @@ def preview_invoice(request: HttpRequest, invoice_id) -> SuccessResponse | Error if invoice.user != request.user: return ErrorResponse("You don't have access to this invoice") try: - currency_symbol = request.user.user_profile.get_currency_symbol + currency_symbol = request.user.user_profile.get_currency_symbol() except UserSettings.DoesNotExist: currency_symbol = "$" diff --git a/backend/api/settings/currency.py b/backend/api/settings/currency.py index dc097abd3..788a39e95 100644 --- a/backend/api/settings/currency.py +++ b/backend/api/settings/currency.py @@ -3,7 +3,7 @@ from django.shortcuts import render from django.views.decorators.http import require_http_methods -from backend.models import UserSettings # Replace with your actual model +from backend.models import UserSettings @require_http_methods(["POST"]) diff --git a/backend/migrations/0027_invoice_currency.py b/backend/migrations/0027_invoice_currency.py new file mode 100644 index 000000000..f2c9a9de4 --- /dev/null +++ b/backend/migrations/0027_invoice_currency.py @@ -0,0 +1,30 @@ +# Generated by Django 5.0.3 on 2024-03-31 23:19 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("backend", "0026_invoice_discount_amount_invoice_discount_percentage"), + ] + + operations = [ + migrations.AddField( + model_name="invoice", + name="currency", + field=models.CharField( + choices=[ + ("GBP", "British Pound Sterling"), + ("EUR", "Euro"), + ("USD", "United States Dollar"), + ("JPY", "Japanese Yen"), + ("INR", "Indian Rupee"), + ("AUD", "Australian Dollar"), + ("CAD", "Canadian Dollar"), + ], + default="GBP", + max_length=3, + ), + ), + ] diff --git a/backend/models.py b/backend/models.py index 95438a9d4..6c2bf6989 100644 --- a/backend/models.py +++ b/backend/models.py @@ -300,6 +300,11 @@ class Invoice(models.Model): payment_status = models.CharField(max_length=10, choices=STATUS_CHOICES, default="pending") items = models.ManyToManyField(InvoiceItem) + currency = models.CharField( + max_length=3, + default="GBP", + choices=[(code, info["name"]) for code, info in UserSettings.CURRENCIES.items()], + ) date_created = models.DateTimeField(auto_now_add=True) date_due = models.DateField() @@ -388,6 +393,9 @@ def has_access(self, user: User) -> bool: else: return self.user == user + def get_currency_symbol(self): + return UserSettings.CURRENCIES.get(self.currency, {}).get("symbol", "$") + class InvoiceURL(models.Model): uuid = ShortUUIDField(length=8, primary_key=True) diff --git a/backend/views/core/invoices/create.py b/backend/views/core/invoices/create.py index d9fbfdc3e..de92b81c8 100644 --- a/backend/views/core/invoices/create.py +++ b/backend/views/core/invoices/create.py @@ -26,10 +26,12 @@ def invoice_page_post(request: HttpRequest): request.POST.getlist("price_per_hour[]"), ) ] + currency = request.user.user_profile.currency invoice = Invoice( date_due=datetime.strptime(request.POST.get("date_due"), "%Y-%m-%d").date(), date_issued=request.POST.get("date_issued"), + currency=currency, ) if request.user.logged_in_as_team: diff --git a/backend/views/core/invoices/edit.py b/backend/views/core/invoices/edit.py index 2b2ff3422..550aef53c 100644 --- a/backend/views/core/invoices/edit.py +++ b/backend/views/core/invoices/edit.py @@ -26,6 +26,7 @@ def invoice_get_existing_data(invoice_obj): "og_issue_date": invoice_obj.date_issued, "og_due_date": invoice_obj.date_due, "invoice_object": invoice_obj, + "currency_symbol": invoice_obj.get_currency_symbol(), "rows": invoice_obj.items.all(), } if invoice_obj.client_to: diff --git a/backend/views/core/invoices/view.py b/backend/views/core/invoices/view.py index 1ef6ef7d8..3be00d35b 100644 --- a/backend/views/core/invoices/view.py +++ b/backend/views/core/invoices/view.py @@ -22,7 +22,7 @@ def preview(request, invoice_id): return redirect("invoices:dashboard") try: - currency_symbol = request.user.user_profile.get_currency_symbol + currency_symbol = invoice.get_currency_symbol() except UserSettings.DoesNotExist: currency_symbol = "$" @@ -49,7 +49,7 @@ def view(request, uuid): return redirect("index") try: - currency_symbol = request.user.user_profile.get_currency_symbol + currency_symbol = request.user.user_profile.get_currency_symbol() except UserSettings.DoesNotExist: currency_symbol = "$" diff --git a/backend/views/core/settings/view.py b/backend/views/core/settings/view.py index 1f94d04f2..2ce9e4a28 100644 --- a/backend/views/core/settings/view.py +++ b/backend/views/core/settings/view.py @@ -1,11 +1,12 @@ from PIL import Image from django.contrib.auth import update_session_auth_hash from django.contrib.sessions.models import Session +from django.contrib import messages +from django.shortcuts import redirect from django.http import HttpRequest from django.shortcuts import render -from backend.decorators import * -from backend.models import * +from backend.models import UserSettings def settings_page(request: HttpRequest): @@ -27,9 +28,9 @@ def settings_page(request: HttpRequest): if request.method == "POST" and request.htmx: section = request.POST.get("section") - profile_picture = request.FILES.get("profile_image") if section == "profile_picture": + profile_picture = request.FILES.get("profile_image") if profile_picture: try: # Max file size is 10MB (Change the first number to determine the size in MB) diff --git a/frontend/templates/pages/invoices/create/_services_table_body.html b/frontend/templates/pages/invoices/create/_services_table_body.html index f17cb7cdf..39124f3e6 100644 --- a/frontend/templates/pages/invoices/create/_services_table_body.html +++ b/frontend/templates/pages/invoices/create/_services_table_body.html @@ -28,17 +28,17 @@ - £{{ row.price_per_hour }} + {{ currency_symbol }}{{ row.price_per_hour }}
INV #{{ invoice.invoice_id|default:invoice.id }}
Invoice date: {{ invoice.date_issued|date:"d/m/Y" }}
Due date: {{ invoice.date_due|date:"d/m/Y" }}
-Balance Due: {{ currency_symbol }}{{ invoice.get_total_price }}
+Balance Due: {{ invoice.get_currency_symbol }}{{ invoice.get_total_price }}
@@ -75,7 +75,7 @@