Skip to content

Commit

Permalink
fixed multiple bugs with editing invoices
Browse files Browse the repository at this point in the history
  • Loading branch information
TreyWW committed Nov 15, 2024
1 parent 497b205 commit 40c57b0
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 42 deletions.
11 changes: 3 additions & 8 deletions backend/core/api/base/modal.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
from backend.core.service.defaults.get import get_account_defaults


# from backend.utils.quota_limit_ops import quota_usage_check_under


# Still working on


def open_modal(request: WebRequest, modal_name, context_type=None, context_value=None):
try:
context = {}
Expand Down Expand Up @@ -58,7 +52,7 @@ def open_modal(request: WebRequest, modal_name, context_type=None, context_value
elif context_type == "edit_invoice_to":
invoice = context_value
try:
invoice = Invoice.objects.get(user=request.user, id=invoice)
invoice = Invoice.filter_by_owner(request.actor).get(id=invoice)
except Invoice.DoesNotExist:
return render(request, template_name, context)

Expand All @@ -74,13 +68,14 @@ def open_modal(request: WebRequest, modal_name, context_type=None, context_value
context["to_name"] = invoice.client_name
context["to_company"] = invoice.client_company
context["to_email"] = invoice.client_email
context["is_representative"] = invoice.client_is_representative
context["to_address"] = (
invoice.client_address
) # context["to_city"] = invoice.client_city # context["to_county"] = invoice.client_county # context["to_country"] = invoice.client_country
elif context_type == "edit_invoice_from":
invoice = context_value
try:
invoice = Invoice.objects.get(user=request.user, id=invoice)
invoice = Invoice.filter_by_owner(request.actor).get(id=invoice)
except Invoice.DoesNotExist:
return render(request, template_name, context)

Expand Down
2 changes: 1 addition & 1 deletion backend/core/service/invoices/common/create/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def save_invoice_common(request: WebRequest, invoice_items, invoice: Invoice | I

if request.POST.get("selected_client"):
try:
client = Client.objects.get(user=request.user, id=request.POST.get("selected_client", ""))
client = Client.filter_by_owner(request.actor).get(id=request.POST.get("selected_client"))
invoice.client_to = client
except Client.DoesNotExist:
messages.error(request, "Client not found")
Expand Down
5 changes: 1 addition & 4 deletions backend/core/service/invoices/common/create/services/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ def add(request: APIRequest | HtmxHttpRequest):
context: dict = {}
existing_service = request.POST.get("existing_service", 0)

try:
existing_service_obj = InvoiceProduct.objects.get(user=request.user, id=existing_service)
except InvoiceProduct.DoesNotExist:
existing_service_obj = None
existing_service_obj = InvoiceProduct.filter_by_owner(request.actor).filter(id=existing_service).first()

list_hours = request.POST.getlist("hours[]")
list_service_name = request.POST.getlist("service_name[]")
Expand Down
7 changes: 6 additions & 1 deletion backend/finance/api/invoices/create/set_destination.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ def set_destination_to(request: HtmxHttpRequest):

if selected_client:
try:
client = Client.objects.get(user=request.user, id=selected_client)
if request.team:
client = Client.objects.filter(organization=request.team)
else:
client = Client.objects.filter(user=request.user)

client = client.get(id=selected_client)
context["existing_client"] = client
except Client.DoesNotExist:
messages.error(request, "Client not found")
Expand Down
28 changes: 22 additions & 6 deletions backend/finance/views/invoices/single/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.shortcuts import render, redirect
from django.views.decorators.http import require_http_methods

from backend.core.types.requests import WebRequest
from backend.decorators import web_require_scopes
from backend.finance.models import Invoice, Client, InvoiceItem
from backend.core.types.htmx import HtmxHttpRequest
Expand Down Expand Up @@ -78,7 +79,7 @@ def invoice_edit_page_get(request, invoice_id):

# when user changes/modifies any of the fields with new information (during edit invoice)
@require_http_methods(["POST"])
def edit_invoice(request: HtmxHttpRequest, invoice_id):
def edit_invoice(request: WebRequest, invoice_id):
try:
invoice = Invoice.objects.get(id=invoice_id)
except Invoice.DoesNotExist:
Expand All @@ -90,7 +91,7 @@ def edit_invoice(request: HtmxHttpRequest, invoice_id):
status=403,
)

attributes_to_updates = {
attributes_to_update = {
"date_due": datetime.strptime(request.POST.get("date_due"), "%Y-%m-%d").date(), # type: ignore[arg-type]
"date_issued": request.POST.get("date_issued"),
"self_name": request.POST.get("from_name"),
Expand All @@ -105,19 +106,32 @@ def edit_invoice(request: HtmxHttpRequest, invoice_id):
"reference": request.POST.get("reference"),
"sort_code": request.POST.get("sort_code"),
"account_number": request.POST.get("account_number"),
"account_holder_name": request.POST.get("account_holder_name"),
"account_holder_name": request.POST.get("account_holder_name")
}

client_to_id = request.POST.get("selected_client")
try:
client_to_obj = Client.objects.get(id=client_to_id, user=request.user) # type: ignore[misc]
client_to_obj = Client.filter_by_owner(request.actor).get(id=client_to_id)
except (Client.DoesNotExist, ValueError):
client_to_obj = None

client_attrs = {
"client_name": request.POST.get("to_name"),
"client_company": request.POST.get("to_company"),
"client_email": request.POST.get("to_email"),
"client_address": request.POST.get("to_address"),
"client_city": request.POST.get("to_city"),
"client_county": request.POST.get("to_county"),
"client_country": request.POST.get("to_country"),
}

if client_to_obj:
invoice.client_to = client_to_obj

for att in client_attrs.keys():
setattr(invoice, att, None)
else:
attributes_to_updates.update(
attributes_to_update.update(
{
"client_name": request.POST.get("to_name"),
"client_company": request.POST.get("to_company"),
Expand All @@ -126,10 +140,12 @@ def edit_invoice(request: HtmxHttpRequest, invoice_id):
"client_city": request.POST.get("to_city"),
"client_county": request.POST.get("to_county"),
"client_country": request.POST.get("to_country"),
"client_is_representative": True if request.POST.get("is_representative") == "on" else False,
"client_to": None
}
)

for column_name, new_value in attributes_to_updates.items():
for column_name, new_value in attributes_to_update.items():
setattr(invoice, column_name, new_value)

invoice_items = [
Expand Down
42 changes: 22 additions & 20 deletions frontend/templates/modals/invoices_to_destination.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
Single
<input type="checkbox"
name="is_representative"
class="toggle toggle-primary" />
class="toggle toggle-primary" {% if is_representative %}checked{% endif %}/>
Representative
</label>
</div>
Expand All @@ -38,55 +38,55 @@
<input type="text"
name="name"
placeholder="John Smith"
value="{{ to_name }}"
value="{{ to_name | default_if_none:"" }}"
class="input input-bordered max-w-full">
</div>
<div class="form-control">
<label class="label">Company</label>
<input type="text"
name="company"
placeholder="Google"
value="{{ to_company }}"
value="{{ to_company | default_if_none:"" }}"
class="input input-bordered max-w-full">
</div>
<div class="form-control">
<label class="label">Email Address</label>
<input type="email"
name="email"
placeholder="[email protected]"
value="{{ to_email }}"
value="{{ to_email | default_if_none:"" }}"
class="input input-bordered max-w-full">
</div>
<div class="form-control">
<label class="label">Address</label>
<input type="text"
name="address"
placeholder="128 Road"
value="{{ to_address }}"
value="{{ to_address | default_if_none:"" }}"
class="input input-bordered max-w-full">
</div>
<div class="form-control">
<label class="label">City</label>
<input type="text"
name="city"
placeholder="Oxford"
value="{{ to_city }}"
value="{{ to_city | default_if_none:"" }}"
class="input input-bordered max-w-full">
</div>
<div class="form-control">
<label class="label">County</label>
<input type="text"
name="county"
placeholder="Oxfordshire"
value="{{ to_county }}"
value="{{ to_county | default_if_none:"" }}"
class="input input-bordered max-w-full">
</div>
<div class="form-control">
<label class="label">Country</label>
<input type="text"
name="country"
placeholder="England"
value="{{ to_country }}"
value="{{ to_country | default_if_none:"" }}"
class="input input-bordered max-w-full">
</div>
<div class="modal-action">
Expand All @@ -99,24 +99,26 @@
hx-target="#to_destination">Save</button>
<button type="reset" class="btn btn-error">Reset</button>
<button type="button"
onclick="document.getElementById ('modal_invoices_to_destination').close();"
onclick="document.getElementById('modal_invoices_to_destination').close();"
class="btn">Close</button>
</div>
</form>
<script>
var existing_button = document.querySelector('button[data-button="existing_Client"]')
var client_dropdown = document.querySelector('select[data-selection="client"]')
var existing_button = document.querySelector('button[data-button="existing_Client"]')
var client_dropdown = document.querySelector('select[data-selection="client"]')

function check_selected_client() {
if (client_dropdown.value !== 'View Clients' && client_dropdown.value !== 'Loading clients...') {
existing_button.disabled = false
} else {
existing_button.disabled = true
}
}
function check_selected_client() {
console.log(`check running: ${client_dropdown.value}`)
if (client_dropdown.value !== 'View Clients' && client_dropdown.value !== 'Loading clients...') {
existing_button.disabled = false
} else {
existing_button.disabled = true
}
}

client_dropdown.addEventListener('change', check_selected_client);
check_selected_client()
client_dropdown.addEventListener('change', check_selected_client);
client_dropdown.addEventListener('htmx:afterProcessNode', check_selected_client);
check_selected_client()
</script>
{% endfill %}
{% endcomponent_block %}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ <h3 class="text-sm text-natural font-semibold block lg:hidden ms-3">To</h3>
<p class="text-sm">{{ to_county | default:"No county" }}</p>
<p class="text-sm">{{ to_country | default:"No country" }}</p>
<p class="text-sm">
{% if is_representative == "on" %}
{% if is_representative == "on" or to_is_representative == "on" %}
Is a
{% else %}
Is not a
Expand All @@ -52,5 +52,5 @@ <h3 class="text-sm text-natural font-semibold block lg:hidden ms-3">To</h3>
<input type="hidden" name="selected_client" value="{{ existing_client.id }}">
<input type="hidden"
name="is_representative"
value="{{ is_representative }}">
value="{{ is_representative | default:to_is_representative }}">
{% if not swapping %}</button>{% endif %}
7 changes: 7 additions & 0 deletions frontend/templates/pages/invoices/single/edit/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
{% block content %}
<form method="post" class="card bg-base-100 p-6 group">
{% csrf_token %}
<div class="flex flex-row flex-wrap gap-4">
<a class="btn btn-outline btn-primary btn-sm w-36 justify-self-end"
href="{% url 'finance:invoices:single:overview' invoice_id=invoice_object.id %}">
Back to overview
</a>
<h2 class="text-xl">Editing recurring invoice #{{ invoice_object.id }}</h2>
</div>
<div class="divider">STEP 1 - DESTINATIONS</div>
<div class="my-4 flex w-full flex-col">
<div class="mb-2 grid grid-cols-2">
Expand Down

0 comments on commit 40c57b0

Please sign in to comment.