Skip to content

Commit

Permalink
255: User's currency is not used in invoices (#260)
Browse files Browse the repository at this point in the history
* 255: User's currency is not used in invoices
- added currency to the Invoice model during invoice creation
  • Loading branch information
introkun authored Apr 1, 2024
1 parent 0358e1d commit 19b374f
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 18 deletions.
2 changes: 1 addition & 1 deletion backend/api/invoices/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "$"

Expand Down
2 changes: 1 addition & 1 deletion backend/api/settings/currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down
30 changes: 30 additions & 0 deletions backend/migrations/0027_invoice_currency.py
Original file line number Diff line number Diff line change
@@ -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,
),
),
]
8 changes: 8 additions & 0 deletions backend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions backend/views/core/invoices/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions backend/views/core/invoices/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions backend/views/core/invoices/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "$"

Expand All @@ -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 = "$"

Expand Down
7 changes: 4 additions & 3 deletions backend/views/core/settings/view.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@
<input class="hidden"
name="price_per_hour[]"
value="{{ row.price_per_hour }}">
£{{ row.price_per_hour }}
{{ currency_symbol }}{{ row.price_per_hour }}
</td>
<td>
{% if row.id %}
<input class="hidden"
name="total_price[]"
value="{{ row.hours|mul:row.price_per_hour|floatformat:2 }}">
£{{ row.hours|mul:row.price_per_hour|floatformat:2|intcomma }}
{{ currency_symbol }}{{ row.hours|mul:row.price_per_hour|floatformat:2|intcomma }}
{% else %}
<input class="hidden" name="total_price[]" value="{{ row.total_price }}">
£{{ row.total_price|floatformat:2|intcomma }}
{{ currency_symbol }}{{ row.total_price|floatformat:2|intcomma }}
{% endif %}
</td>
<td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</div>
</div>
</td>
<td>£{{ invoice.amount | default_if_none:0 | floatformat:2 | intcomma }}</td>
<td>{{ invoice.get_currency_symbol }}{{ invoice.amount | default_if_none:0 | floatformat:2 | intcomma }}</td>
<td>
{% component "pages:invoices:dashboard:payment_status_badge" status=invoice.payment_status inv_id=invoice.id %}
</td>
Expand Down
14 changes: 7 additions & 7 deletions frontend/templates/pages/invoices/view/invoice.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ <h2 class="text-2xl font-bold text-gray-800">INVOICE</h2>
<p class="mt-2 text-gray-600">INV #{{ invoice.invoice_id|default:invoice.id }}</p>
<p class="text-gray-600">Invoice date: {{ invoice.date_issued|date:"d/m/Y" }}</p>
<p class="text-gray-600">Due date: {{ invoice.date_due|date:"d/m/Y" }}</p>
<p class="text-gray-600">Balance Due: {{ currency_symbol }}{{ invoice.get_total_price }}</p>
<p class="text-gray-600">Balance Due: {{ invoice.get_currency_symbol }}{{ invoice.get_total_price }}</p>
</div>
</div>
<!-- Client info -->
Expand Down Expand Up @@ -75,7 +75,7 @@ <h2 class="text-2xl font-bold text-gray-800">INVOICE</h2>
<span class="text-sm text-gray-900">{{ item.price_per_hour }}</span>
</td>
<td class="px-6 py-4 text-right whitespace-nowrap">
<span class="text-sm text-gray-900">{{ currency_symbol }}{{ item.get_total_price | floatformat:2 }}</span>
<span class="text-sm text-gray-900">{{ invoice.get_currency_symbol }}{{ item.get_total_price | floatformat:2 }}</span>
</td>
</tr>
{% endfor %}
Expand All @@ -87,7 +87,7 @@ <h2 class="text-2xl font-bold text-gray-800">INVOICE</h2>
Subtotal
</th>
<td class="px-6 py-3 text-right text-sm font-medium text-gray-500 uppercase tracking-wider">
{{ currency_symbol }}{{ invoice.get_subtotal }}
{{ invoice.get_currency_symbol }}{{ invoice.get_subtotal }}
</td>
</tr>
{% if tax %}
Expand All @@ -98,7 +98,7 @@ <h2 class="text-2xl font-bold text-gray-800">INVOICE</h2>
Tax
</th>
<td class="px-6 py-3 text-right text-sm font-medium text-gray-500 uppercase tracking-wider">
{{ currency_symbol }}0.00
{{ invoice.get_currency_symbol }}0.00
</td>
</tr>
{% endif %}
Expand All @@ -109,7 +109,7 @@ <h2 class="text-2xl font-bold text-gray-800">INVOICE</h2>
Discount
</th>
<td class="px-6 py-3 text-right text-sm font-medium text-error uppercase tracking-wider text-balance">
{{ invoice.discount_percentage }}% off (-{{ currency_symbol }}{{ invoice.get_percentage_amount }})
{{ invoice.discount_percentage }}% off (-{{ invoice.get_currency_symbol }}{{ invoice.get_percentage_amount }})
</td>
</tr>
{% endif %}
Expand All @@ -120,7 +120,7 @@ <h2 class="text-2xl font-bold text-gray-800">INVOICE</h2>
{% if not invoice.discount_percentage %}Discount{% endif %}
</th>
<td class="px-6 py-3 text-right text-sm font-medium text-error uppercase tracking-wider">
-{{ currency_symbol }}{{ invoice.discount_amount }}
-{{ invoice.get_currency_symbol }}{{ invoice.discount_amount }}
</td>
</tr>
{% endif %}
Expand All @@ -130,7 +130,7 @@ <h2 class="text-2xl font-bold text-gray-800">INVOICE</h2>
Total
</th>
<td class="px-6 py-3 text-right text-sm font-bold text-gray-900 uppercase tracking-wider">
{{ currency_symbol }}{{ invoice.get_total_price }}
{{ invoice.get_currency_symbol }}{{ invoice.get_total_price }}
</td>
</tr>
</tfoot>
Expand Down

0 comments on commit 19b374f

Please sign in to comment.