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

Code Changes views/clients/create.py #356

Closed
Closed
Show file tree
Hide file tree
Changes from 4 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
41 changes: 22 additions & 19 deletions backend/views/core/clients/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def create_client(request: HtmxHttpRequest):
"address": request.POST.get("client_address"),
"phone_number": request.POST.get("client_phone"),
"company": request.POST.get("company_name"),
"is_representative": (True if request.POST.get("is_representative") == "on" else False),
"is_representative": request.POST.get("is_representative") == "on",
}

error = validate_client_create(client_details)
Expand All @@ -25,35 +25,38 @@ def create_client(request: HtmxHttpRequest):
messages.error(request, error)
return redirect("clients create")

if request.user.logged_in_as_team:
client = Client.objects.create(
organization=request.user.logged_in_as_team,
)
client = create_client_instance(request.user, client_details)

if not client:
messages.error(request, "Failed to create client - an unknown error occurred")
else:
client = Client.objects.create(
user=request.user,
)
messages.success(request, f"Client created successfully (#{client.id})")

for model_field, new_value in client_details.items():
setattr(client, model_field, new_value)
return redirect("clients dashboard")

client.save()

if client:
messages.success(request, f"Client created successfully (#{client.id})")
else:
messages.error(request, "Failed to create client - an unknown error occurred")
return redirect("clients dashboard")
def create_client_instance(user, client_details):
organization = user.logged_in_as_team if user.logged_in_as_team else None
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to support adding customers under an individual user aswell as organisation

try:
client = Client.objects.create(organization=organization, **client_details)
return client
except Exception as e:
print(f"Failed to create client: {e}")
return None


def validate_client_create(client_details):
if not client_details.get("name"):
name = client_details.get("name")
company = client_details.get("company")
is_representative = client_details.get("is_representative")

if not name:
return "Please provide at least a client name"

if len(client_details.get("name")) < 3:
if len(name) < 3:
return "Client name must be at least 3 characters"

if client_details.get("is_representative") and not client_details.get("company"):
if is_representative and not company:
return "Please provide a company name if you are creating a representative"

return None
3 changes: 2 additions & 1 deletion backend/views/core/invoices/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ def invoices_dashboard(request: HtmxHttpRequest):
def invoices_dashboard_id(request: HtmxHttpRequest, invoice_id):
if invoice_id == "create":
return redirect("invoices:create")
elif not isinstance(invoice_id, int):
elif not invoice_id.isdigit():
messages.error(request, "Invalid invoice ID")
return redirect("invoices:dashboard")

try:
Invoice.objects.get(id=invoice_id)
except Invoice.DoesNotExist:
return redirect("invoices:dashboard")

return render(request, "pages/invoices/dashboard/dashboard.html")
45 changes: 26 additions & 19 deletions backend/views/core/invoices/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,33 @@ def invoice_get_existing_data(invoice_obj):
"currency_symbol": invoice_obj.get_currency_symbol(),
"rows": invoice_obj.items.all(),
}
if invoice_obj.client_to:
stored_data["to_name"] = invoice_obj.client_to.name
stored_data["to_company"] = invoice_obj.client_to.company
stored_data["is_representative"] = invoice_obj.client_to.is_representative
# stored_data["to_address"] = invoice_obj.client_to.address
# stored_data["to_city"] = invoice_obj.client_to.city
# stored_data["to_county"] = invoice_obj.client_to.county
# stored_data["to_country"] = invoice_obj.client_to.country
else:
stored_data["to_name"] = invoice_obj.client_name
stored_data["to_company"] = invoice_obj.client_company
stored_data["to_address"] = invoice_obj.client_address
stored_data["to_city"] = invoice_obj.client_city
stored_data["to_county"] = invoice_obj.client_county
stored_data["to_country"] = invoice_obj.client_country
stored_data["is_representative"] = invoice_obj.client_is_representative

if invoice_obj.client_to:
stored_data["existing_client"] = invoice_obj.client_to

client_to = invoice_obj.client_to
if client_to:
stored_data.update(
{
"to_name": client_to.name,
"to_company": client_to.company,
"is_representative": client_to.representative,
# "to_address": client_to.address,
# "to_city": client_to.city,
# "to_county": client_to.county,
# "to_country": client_to.country
"existing_client": client_to,
}
)
else:
stored_data.update(
{
"to_name": invoice_obj.client_name,
"to_company": invoice_obj.client_company,
"to_address": invoice_obj.client_address,
"to_city": invoice_obj.client_city,
"to_county": invoice_obj.client_county,
"to_country": invoice_obj.client_country,
"is_representative": invoice_obj.client_is_representative,
}
)
return stored_data


Expand Down
3 changes: 3 additions & 0 deletions backend/views/core/invoices/manage_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from backend.types.htmx import HtmxHttpRequest


# Function to manage access codes for an invoice
def manage_access(request: HtmxHttpRequest, invoice_id):
try:
invoice = Invoice.objects.prefetch_related("invoice_urls").get(id=invoice_id, user=request.user)
Expand All @@ -23,6 +24,7 @@ def manage_access(request: HtmxHttpRequest, invoice_id):
)


# Function to create a new access code for an invoice
@quota_usage_check("invoices-access_codes", 1, api=True, htmx=True)
def create_code(request: HtmxHttpRequest, invoice_id):
if not request.htmx:
Expand All @@ -49,6 +51,7 @@ def create_code(request: HtmxHttpRequest, invoice_id):
)


# Function to delete an access code for an invoice
def delete_code(request: HtmxHttpRequest, code):
if request.method != "DELETE" or not request.htmx:
return HttpResponse("Request invalid", status=400)
Expand Down
7 changes: 4 additions & 3 deletions backend/views/core/invoices/overview.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ def manage_invoice(request: HtmxHttpRequest, invoice_id: str):
messages.error(request, "Invalid invoice ID")
return redirect("invoices:dashboard")

invoice = Invoice.objects.get(id=invoice_id)

if not invoice:
try:
invoice = Invoice.objects.get(id=invoice_id)
except Invoice.DoesNotExist:
messages.error(request, "Invoice not found")
return redirect("invoices:dashboard")

print(context | {"invoice": invoice})
Expand Down
6 changes: 1 addition & 5 deletions backend/views/core/invoices/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,4 @@ def view_schedules(request: HtmxHttpRequest, invoice_id) -> HttpResponse:
if quota_limit.strict_goes_above_limit(request.user):
context["quota_breached"] = True

return render(
request,
"pages/invoices/schedules/view.html",
context,
)
return render(request, "pages/invoices/schedules/view.html", context)
7 changes: 2 additions & 5 deletions backend/views/core/invoices/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

from django.contrib import messages
from django.contrib.auth.models import AnonymousUser
from django.shortcuts import redirect
from django.shortcuts import render
from django.shortcuts import redirect, render
from login_required import login_not_required

from backend.models import Invoice
from backend.models import InvoiceURL
from backend.models import UserSettings
from backend.models import Invoice, InvoiceURL, UserSettings


def preview(request, invoice_id):
Expand Down
Loading
Loading