summaryrefslogtreecommitdiff
path: root/src/ui
diff options
context:
space:
mode:
Diffstat (limited to 'src/ui')
-rw-r--r--src/ui/helpers.cpp2
-rw-r--r--src/ui/ui_contacts.cpp9
-rw-r--r--src/ui/ui_invoices.cpp94
-rw-r--r--src/ui/ui_main.cpp11
-rw-r--r--src/ui/ui_settings.cpp4
5 files changed, 76 insertions, 44 deletions
diff --git a/src/ui/helpers.cpp b/src/ui/helpers.cpp
index 7bde384..6ca1bd2 100644
--- a/src/ui/helpers.cpp
+++ b/src/ui/helpers.cpp
@@ -59,6 +59,7 @@ int TextInputWithAutocomplete(const char* label, const char* hint, char* buffer,
for (int i = 0; i < suggestion_count; ++i)
{
+ ImGui::PushID(i);
if (ImGui::Selectable(suggestions[i]))
{
// Copy selected suggestion to buffer
@@ -67,6 +68,7 @@ int TextInputWithAutocomplete(const char* label, const char* hint, char* buffer,
result = i;
is_open = false;
}
+ ImGui::PopID();
}
ImGui::EndChild();
diff --git a/src/ui/ui_contacts.cpp b/src/ui/ui_contacts.cpp
index a55fd73..dbe6625 100644
--- a/src/ui/ui_contacts.cpp
+++ b/src/ui/ui_contacts.cpp
@@ -1,4 +1,5 @@
#include <stdio.h>
+#include <stdlib.h>
#include "strops.hpp"
#include "ui.hpp"
@@ -99,7 +100,8 @@ void draw_contact_form_ex(contact* buffer, bool viewing_only = false, bool with_
for (int i = 0; i < autocomplete_count; i++)
{
- autocomplete_strings[i] = autocomplete_list[i].name;
+ autocomplete_strings[i] = (char*)malloc(200);
+ snprintf(autocomplete_strings[i], 200, "%s (%s %s)", autocomplete_list[i].name, autocomplete_list[i].address.address1, autocomplete_list[i].address.address2);
}
int autocomplete_index = TextInputWithAutocomplete(localize("contact.form.fullname"), localize("contact.form.fullname"),
@@ -113,6 +115,11 @@ void draw_contact_form_ex(contact* buffer, bool viewing_only = false, bool with_
{
memcpy(buffer, &autocomplete_list[autocomplete_index], sizeof(contact));
}
+
+ for (int i = 0; i < autocomplete_count; i++)
+ {
+ free(autocomplete_strings[i]);
+ }
}
else ImGui::InputTextWithHint(localize("contact.form.fullname"), localize("contact.form.fullname"), buffer->name, IM_ARRAYSIZE(buffer->name));
ImGui::SameLine();ui_helper_draw_required_tag();
diff --git a/src/ui/ui_invoices.cpp b/src/ui/ui_invoices.cpp
index c08dd76..320392a 100644
--- a/src/ui/ui_invoices.cpp
+++ b/src/ui/ui_invoices.cpp
@@ -11,20 +11,25 @@
#include "administration.hpp"
#include "locales.hpp"
-void ui_draw_address_form(address* buffer);
static view_state current_view_state = view_state::LIST;
static invoice active_invoice = {0};
+static invoice selected_for_removal = {0};
cost_center* cost_center_list_buffer = 0;
country_tax_bracket* tax_bracket_list_buffer = 0;
+project* project_list_buffer = 0;
+billing_item* invoice_items_buffer = 0;
+void ui_draw_address_form(address* buffer);
void draw_contact_form_ex(contact* buffer, bool viewing_only = false, bool with_autocomplete = false, bool* on_autocomplete = 0);
void ui_destroy_invoices()
{
free(cost_center_list_buffer);
free(tax_bracket_list_buffer);
+ free(project_list_buffer);
+ free(invoice_items_buffer);
}
void ui_setup_invoices()
@@ -33,10 +38,16 @@ void ui_setup_invoices()
active_invoice = administration_invoice_create_empty();
u32 costcenter_count = administration_cost_center_count();
- cost_center_list_buffer = (cost_center*) malloc(sizeof(cost_center) * costcenter_count); // @leak
+ cost_center_list_buffer = (cost_center*) malloc(sizeof(cost_center) * costcenter_count);
u32 tax_bracket_count = administration_tax_bracket_count();
- tax_bracket_list_buffer = (country_tax_bracket*) malloc(sizeof(country_tax_bracket) * tax_bracket_count); // @leak
+ tax_bracket_list_buffer = (country_tax_bracket*) malloc(sizeof(country_tax_bracket) * tax_bracket_count);
+
+ u32 project_count = administration_project_count();
+ project_list_buffer = (project*) malloc(sizeof(project) * project_count);
+
+ u32 invoice_items_count = MAX_BILLING_ITEMS;
+ invoice_items_buffer = (billing_item*)malloc(sizeof(billing_item) * invoice_items_count);
}
void draw_tax_bracket_selector(char* tax_bracket_id)
@@ -185,10 +196,8 @@ void draw_costcenter_selector(char* costcenter_id)
void draw_project_selector(char* project_id)
{
project* selected_project = NULL;
-
- u32 project_count = administration_project_count();
- project* buffer = (project*) malloc(sizeof(project) * project_count);
- project_count = administration_project_get_all(buffer);
+ project* buffer = project_list_buffer;
+ u32 project_count = administration_project_get_all(buffer);
// Select project by given id.
if (strlen(project_id) > 0)
@@ -219,15 +228,12 @@ void draw_project_selector(char* project_id)
if (selected_project_index != -1) {
strops_copy(project_id, buffer[selected_project_index].id, MAX_LEN_ID);
}
-
- free(buffer);
}
static void draw_invoice_items_form(invoice* invoice)
{
- u32 invoice_items = administration_billing_items_count(invoice);
- billing_item* buffer = (billing_item*)malloc(sizeof(billing_item) * invoice_items);
- administration_billing_item_get_all_for_invoice(invoice, buffer);
+ billing_item* buffer = invoice_items_buffer;
+ u32 invoice_items = administration_billing_item_get_all_for_invoice(invoice, buffer);
if (ImGui::BeginTable("TableBillingItems", 9, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg)) {
@@ -343,8 +349,6 @@ static void draw_invoice_items_form(invoice* invoice)
ImGui::EndTable();
}
-
- free(buffer);
}
static void draw_invoice_form(invoice* buffer, bool viewing_only = false)
@@ -396,12 +400,20 @@ static void draw_invoice_form(invoice* buffer, bool viewing_only = false)
bool on_autocomplete;
draw_contact_form_ex(&buffer->customer, false, true, &on_autocomplete);
- // TODO: check if customer info is equal to contact stored in customer id, in case we select from dropdown and edit data after,
- // this should be handled as a new contact and customer_id should be set to "" so we create a new contact.
-
if (on_autocomplete) {
strops_copy(buffer->customer_id, buffer->customer.id, sizeof(buffer->customer_id));
}
+
+ // Check if contact info is equal to contact stored in customer id, in case we select from dropdown and edit data after,
+ // this should be handled as a new contact and customer_id should be set to "" so we create a new contact.
+ contact lookup_buffer;
+ if (administration_contact_get_by_id(&lookup_buffer, buffer->customer_id))
+ {
+ if (!administration_contact_equals(lookup_buffer, buffer->customer))
+ {
+ buffer->customer_id[0] = '\0';
+ }
+ }
// 8. (optional) shipping address.
ImGui::Checkbox("Shipping information differs from billing information (triangulation)", &buffer->is_triangulation);
@@ -424,11 +436,14 @@ static void draw_invoice_form(invoice* buffer, bool viewing_only = false)
ImGui::Spacing();
// 11. New billing item button.
+ bool max_items_reached = administration_billing_item_count(buffer) >= MAX_BILLING_ITEMS;
+ if (max_items_reached) ImGui::BeginDisabled();
if (ImGui::Button(localize("+ Billing item")))
{
billing_item item = administration_billing_item_create_empty();
administration_billing_item_add_to_invoice(buffer, item);
}
+ if (max_items_reached) ImGui::EndDisabled();
// 12. Dropdown for invoice currency.
ImGui::SameLine();
@@ -530,34 +545,31 @@ static void ui_draw_invoices_list()
active_invoice = administration_invoice_create_copy(&c); // We create a copy because of billing item list pointers.
current_view_state = view_state::EDIT;
}
+
+ ImGui::SameLine();
+ snprintf(btn_name, sizeof(btn_name), "%s##%d", localize("form.delete"), i);
+ if (ImGui::Button(btn_name)) {
+ selected_for_removal = c;
+ ImGui::OpenPopup("ConfirmDeletePopup");
+ }
}
-
- // ImGui::SameLine();
- // if (administration_contact_can_be_deleted(c))
- // {
- // snprintf(btn_name, sizeof(btn_name), "%s##%d", localize("form.delete"), i);
- // if (ImGui::Button(btn_name)) {
- // selected_for_removal = c;
- // ImGui::OpenPopup("ConfirmDeletePopup");
- // }
- // }
}
// Confirmation popup before contact is deleted definitively.
- // if (ImGui::BeginPopupModal("ConfirmDeletePopup", nullptr, ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoTitleBar)) {
- // ImGui::Text(localize("form.confirmDelete"));
- // ImGui::Separator();
-
- // if (ImGui::Button(localize("form.yes"), ImVec2(120, 0))) {
- // administration_contact_remove(selected_for_removal);
- // ImGui::CloseCurrentPopup();
- // }
- // ImGui::SameLine();
- // if (ImGui::Button(localize("form.no"), ImVec2(120, 0))) {
- // ImGui::CloseCurrentPopup();
- // }
- // ImGui::EndPopup();
- // }
+ if (ImGui::BeginPopupModal("ConfirmDeletePopup", nullptr, ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoTitleBar)) {
+ ImGui::Text(localize("form.confirmDelete"));
+ ImGui::Separator();
+
+ if (ImGui::Button(localize("form.yes"), ImVec2(120, 0))) {
+ administration_invoice_remove(&selected_for_removal);
+ ImGui::CloseCurrentPopup();
+ }
+ ImGui::SameLine();
+ if (ImGui::Button(localize("form.no"), ImVec2(120, 0))) {
+ ImGui::CloseCurrentPopup();
+ }
+ ImGui::EndPopup();
+ }
ImGui::EndTable();
}
diff --git a/src/ui/ui_main.cpp b/src/ui/ui_main.cpp
index d8fd5d4..19ab6b4 100644
--- a/src/ui/ui_main.cpp
+++ b/src/ui/ui_main.cpp
@@ -37,8 +37,19 @@ void (*setupcalls[dashboard_view_state::END])(void) = {
ui_setup_settings,
};
+void (*destroycalls[dashboard_view_state::END])(void) = {
+ ui_destroy_invoices,
+ 0,
+ 0,
+ 0,
+ 0,
+ ui_destroy_settings,
+ 0,
+};
+
static void set_dashboard_state(dashboard_view_state state)
{
+ if (dashboard_state != dashboard_view_state::END && destroycalls[dashboard_state]) destroycalls[dashboard_state]();
dashboard_state = state;
if (setupcalls[dashboard_state]) setupcalls[dashboard_state]();
}
diff --git a/src/ui/ui_settings.cpp b/src/ui/ui_settings.cpp
index 69c4f54..3f9ab6d 100644
--- a/src/ui/ui_settings.cpp
+++ b/src/ui/ui_settings.cpp
@@ -11,11 +11,11 @@ extern void draw_contact_form(contact* buffer, bool viewing_only = false);
static contact company_info;
-country_tax_bracket* tax_brackets;
u32 tax_bracket_count;
+country_tax_bracket* tax_brackets = 0;
-cost_center* cost_centers;
u32 cost_center_count;
+cost_center* cost_centers = 0;
void ui_destroy_settings()
{