Skip to content

Commit

Permalink
feat: added the ability to save as a default fields such as FROM name…
Browse files Browse the repository at this point in the history
…, address, etc (#490)

Signed-off-by: Trey <[email protected]>
  • Loading branch information
TreyWW committed Sep 16, 2024
1 parent 260c89e commit e3977c5
Show file tree
Hide file tree
Showing 12 changed files with 304 additions and 133 deletions.
10 changes: 10 additions & 0 deletions backend/api/base/modal.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from backend.types.htmx import HtmxHttpRequest
from backend.types.requests import WebRequest
from backend.utils.feature_flags import get_feature_status
from backend.service.defaults.get import get_account_defaults


# from backend.utils.quota_limit_ops import quota_usage_check_under
Expand Down Expand Up @@ -91,6 +92,15 @@ def open_modal(request: WebRequest, modal_name, context_type=None, context_value
context["from_city"] = invoice.self_city
context["from_county"] = invoice.self_county
context["from_country"] = invoice.self_country
elif context_type == "create_invoice_from":
defaults = get_account_defaults(request.actor)

context["from_name"] = getattr(defaults, f"invoice_from_name")
context["from_company"] = getattr(defaults, f"invoice_from_company")
context["from_address"] = getattr(defaults, f"invoice_from_address")
context["from_city"] = getattr(defaults, f"invoice_from_city")
context["from_county"] = getattr(defaults, f"invoice_from_county")
context["from_country"] = getattr(defaults, f"invoice_from_country")
elif context_type == "invoice":
try:
invoice = Invoice.objects.get(id=context_value)
Expand Down
4 changes: 2 additions & 2 deletions backend/api/invoices/create/set_destination.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
def set_destination_to(request: HtmxHttpRequest):
context: dict = {"swapping": True}

context.update({key: request.POST.get(key, "") for key in to_get})
context.update({f"to_{key}": request.POST.get(key, "") for key in to_get})

use_existing = True if request.POST.get("use_existing") == "true" else False
selected_client = request.POST.get("selected_client") if use_existing else None
Expand All @@ -31,6 +31,6 @@ def set_destination_to(request: HtmxHttpRequest):
def set_destination_from(request: HtmxHttpRequest):
context: dict = {"swapping": True}

context.update({key: request.POST.get(key, "") for key in to_get})
context.update({f"from_{key}": request.POST.get(key, "") for key in to_get})

return render(request, "pages/invoices/create/destinations/_from_destination.html", context)
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Generated by Django 5.1 on 2024-09-16 19:47

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("backend", "0060_user_require_change_password"),
]

operations = [
migrations.AddField(
model_name="defaultvalues",
name="invoice_from_address",
field=models.CharField(blank=True, max_length=100, null=True),
),
migrations.AddField(
model_name="defaultvalues",
name="invoice_from_city",
field=models.CharField(blank=True, max_length=100, null=True),
),
migrations.AddField(
model_name="defaultvalues",
name="invoice_from_company",
field=models.CharField(blank=True, max_length=100, null=True),
),
migrations.AddField(
model_name="defaultvalues",
name="invoice_from_country",
field=models.CharField(blank=True, max_length=100, null=True),
),
migrations.AddField(
model_name="defaultvalues",
name="invoice_from_county",
field=models.CharField(blank=True, max_length=100, null=True),
),
migrations.AddField(
model_name="defaultvalues",
name="invoice_from_name",
field=models.CharField(blank=True, max_length=100, null=True),
),
]
7 changes: 7 additions & 0 deletions backend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,13 @@ class InvoiceDateType(models.TextChoices):
invoice_date_value = models.PositiveSmallIntegerField(default=15, null=False, blank=False)
invoice_date_type = models.CharField(max_length=20, choices=InvoiceDateType.choices, default=InvoiceDateType.day_of_month)

invoice_from_name = models.CharField(max_length=100, null=True, blank=True)
invoice_from_company = models.CharField(max_length=100, null=True, blank=True)
invoice_from_address = models.CharField(max_length=100, null=True, blank=True)
invoice_from_city = models.CharField(max_length=100, null=True, blank=True)
invoice_from_county = models.CharField(max_length=100, null=True, blank=True)
invoice_from_country = models.CharField(max_length=100, null=True, blank=True)

def get_issue_and_due_dates(self, issue_date: date | str | None = None) -> tuple[str, str]:
due: date
issue: date
Expand Down
18 changes: 18 additions & 0 deletions backend/service/defaults/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ def change_client_defaults(request: WebRequest, defaults: DefaultValues) -> Clie
else:
return ClientDefaultsServiceResponse(error_message=logo_ok)

DETAIL_INPUTS = {
"name": {"max_len": 100},
"company": {"max_len": 100},
"address": {"max_len": 100},
"city": {"max_len": 100},
"county": {"max_len": 100},
"country": {"max_len": 100},
}

for detail, value_dict in DETAIL_INPUTS.items():
input_post = request.POST.get(f"invoice_from_{detail}", "")

if len(input_post) > value_dict["max_len"]:
return ClientDefaultsServiceResponse(
error_message=f"Details - From {detail} is too long, max length is {value_dict['max_len']}"
)

setattr(defaults, f"invoice_from_{detail}", input_post)
defaults.save()
return ClientDefaultsServiceResponse(True)

Expand Down
15 changes: 14 additions & 1 deletion backend/service/invoices/common/create/get_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def global_get_invoice_context(request: WebRequest) -> CreateInvoiceContextServi
defaults = get_account_defaults(request.actor, client=None)

for item in ["name", "company", "address", "city", "county", "country"]:
context[f"{item}"] = request.GET.get(f"from_{item}", "")
context[f"from_{item}"] = request.GET.get(f"from_{item}", "")

if issue_date := request.GET.get("issue_date"):
try:
Expand Down Expand Up @@ -72,4 +72,17 @@ def global_get_invoice_context(request: WebRequest) -> CreateInvoiceContextServi
if account_number := request.GET.get("account_number"):
context["account_number"] = account_number

details_from = ["name", "company", "address", "city", "county", "country"]

print(context)

for detail in details_from:
detail_value = request.GET.get(f"from_{detail}", "")

if not detail_value:
detail_value = getattr(defaults, f"invoice_from_{detail}")

context[f"from_{detail}"] = detail_value

print(context)
return CreateInvoiceContextServiceResponse(True, CreateInvoiceContextTuple(defaults, context))
Loading

0 comments on commit e3977c5

Please sign in to comment.